mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 21:24:58 +00:00
Simplifying BreadthFirstSearchPathManyDestinations
This commit is contained in:
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
using ICD.Common.Utils.Collections;
|
using ICD.Common.Utils.Collections;
|
||||||
using ICD.Common.Utils.Extensions;
|
|
||||||
|
|
||||||
namespace ICD.Common.Utils
|
namespace ICD.Common.Utils
|
||||||
{
|
{
|
||||||
@@ -101,8 +100,8 @@ namespace ICD.Common.Utils
|
|||||||
}
|
}
|
||||||
|
|
||||||
[NotNull]
|
[NotNull]
|
||||||
public static Dictionary<T2, IEnumerable<T>> BreadthFirstSearchManyDestinations<T, T2>(T root,
|
public static Dictionary<T, IEnumerable<T>> BreadthFirstSearchManyDestinations<T>(T root,
|
||||||
Dictionary<T2, T> destinations,
|
IEnumerable<T> destinations,
|
||||||
Func<T, IEnumerable<T>>
|
Func<T, IEnumerable<T>>
|
||||||
getChildren)
|
getChildren)
|
||||||
{
|
{
|
||||||
@@ -113,8 +112,8 @@ namespace ICD.Common.Utils
|
|||||||
}
|
}
|
||||||
|
|
||||||
[NotNull]
|
[NotNull]
|
||||||
public static Dictionary<T2, IEnumerable<T>> BreadthFirstSearchPathManyDestinations<T, T2>(T root,
|
public static Dictionary<T, IEnumerable<T>> BreadthFirstSearchPathManyDestinations<T>(T root,
|
||||||
Dictionary<T2, T>
|
IEnumerable<T>
|
||||||
destinations,
|
destinations,
|
||||||
Func<T, IEnumerable<T>>
|
Func<T, IEnumerable<T>>
|
||||||
getChildren,
|
getChildren,
|
||||||
@@ -130,21 +129,20 @@ namespace ICD.Common.Utils
|
|||||||
if (comparer == null)
|
if (comparer == null)
|
||||||
throw new ArgumentNullException("comparer");
|
throw new ArgumentNullException("comparer");
|
||||||
|
|
||||||
Dictionary<T2, T> destinationsToBeProcessed = new Dictionary<T2, T>(destinations);
|
IcdHashSet<T> destinationsToBeProcessed = new IcdHashSet<T>(destinations);
|
||||||
List<T> destinationsProcessed = new List<T>();
|
IcdHashSet<T> destinationsProcessed = new IcdHashSet<T>();
|
||||||
Dictionary<T2, IEnumerable<T>> pathsToReturn = new Dictionary<T2, IEnumerable<T>>();
|
Dictionary<T, IEnumerable<T>> pathsToReturn = new Dictionary<T, IEnumerable<T>>();
|
||||||
|
|
||||||
// Edge case, root is the destination
|
// Edge case, root is the destination
|
||||||
foreach (
|
foreach (T destination in
|
||||||
KeyValuePair<T2, T> destination in
|
destinationsToBeProcessed.Where(destination => comparer.Equals(root, destination)))
|
||||||
destinationsToBeProcessed.Where(destination => comparer.Equals(root, destination.Value)))
|
|
||||||
{
|
{
|
||||||
destinationsProcessed.Add(destination.Value);
|
destinationsProcessed.Add(destination);
|
||||||
pathsToReturn.Add(destination.Key, new[] {root});
|
pathsToReturn.Add(destination, new[] {root});
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (T destination in destinationsProcessed)
|
foreach (T destination in destinationsProcessed)
|
||||||
destinationsToBeProcessed.RemoveValue(destination);
|
destinationsToBeProcessed.Remove(destination);
|
||||||
destinationsProcessed.Clear();
|
destinationsProcessed.Clear();
|
||||||
if (destinationsToBeProcessed.Count == 0)
|
if (destinationsToBeProcessed.Count == 0)
|
||||||
return pathsToReturn;
|
return pathsToReturn;
|
||||||
@@ -164,16 +162,15 @@ namespace ICD.Common.Utils
|
|||||||
nodeParents.Add(node, current);
|
nodeParents.Add(node, current);
|
||||||
|
|
||||||
T closureNode = node;
|
T closureNode = node;
|
||||||
foreach (
|
foreach (T destination in
|
||||||
KeyValuePair<T2, T> destination in
|
destinationsToBeProcessed.Where(destination => comparer.Equals(closureNode, destination)))
|
||||||
destinationsToBeProcessed.Where(destination => comparer.Equals(closureNode, destination.Value)))
|
|
||||||
{
|
{
|
||||||
destinationsProcessed.Add(destination.Value);
|
destinationsProcessed.Add(destination);
|
||||||
pathsToReturn.Add(destination.Key, GetPath(destination.Value, nodeParents).Reverse());
|
pathsToReturn.Add(destination, GetPath(destination, nodeParents).Reverse());
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (T destination in destinationsProcessed)
|
foreach (T destination in destinationsProcessed)
|
||||||
destinationsToBeProcessed.RemoveValue(destination);
|
destinationsToBeProcessed.Remove(destination);
|
||||||
|
|
||||||
destinationsProcessed.Clear();
|
destinationsProcessed.Clear();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user