mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-15 20:54:58 +00:00
perf: BreadthFirstSearchPathManyDestinations optimizations
This commit is contained in:
@@ -245,20 +245,16 @@ namespace ICD.Common.Utils
|
|||||||
throw new ArgumentNullException("comparer");
|
throw new ArgumentNullException("comparer");
|
||||||
|
|
||||||
IcdHashSet<T> destinationsToBeProcessed = new IcdHashSet<T>(destinations);
|
IcdHashSet<T> destinationsToBeProcessed = new IcdHashSet<T>(destinations);
|
||||||
IcdHashSet<T> destinationsProcessed = new IcdHashSet<T>();
|
|
||||||
Dictionary<T, IEnumerable<T>> pathsToReturn = new Dictionary<T, 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 (T destination in
|
foreach (T destination in
|
||||||
destinationsToBeProcessed.Where(destination => comparer.Equals(root, destination)))
|
destinationsToBeProcessed.Where(destination => comparer.Equals(root, destination)).ToArray())
|
||||||
{
|
{
|
||||||
destinationsProcessed.Add(destination);
|
destinationsToBeProcessed.Remove(destination);
|
||||||
pathsToReturn.Add(destination, new[] {root});
|
pathsToReturn.Add(destination, new[] {root});
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (T destination in destinationsProcessed)
|
|
||||||
destinationsToBeProcessed.Remove(destination);
|
|
||||||
destinationsProcessed.Clear();
|
|
||||||
if (destinationsToBeProcessed.Count == 0)
|
if (destinationsToBeProcessed.Count == 0)
|
||||||
return pathsToReturn;
|
return pathsToReturn;
|
||||||
|
|
||||||
@@ -278,17 +274,12 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
T closureNode = node;
|
T closureNode = node;
|
||||||
foreach (T destination in
|
foreach (T destination in
|
||||||
destinationsToBeProcessed.Where(destination => comparer.Equals(closureNode, destination)))
|
destinationsToBeProcessed.Where(destination => comparer.Equals(closureNode, destination)).ToArray())
|
||||||
{
|
{
|
||||||
destinationsProcessed.Add(destination);
|
destinationsToBeProcessed.Remove(destination);
|
||||||
pathsToReturn.Add(destination, GetPath(destination, root, nodeParents, comparer).Reverse());
|
pathsToReturn.Add(destination, GetPath(destination, root, nodeParents, comparer).Reverse());
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (T destination in destinationsProcessed)
|
|
||||||
destinationsToBeProcessed.Remove(destination);
|
|
||||||
|
|
||||||
destinationsProcessed.Clear();
|
|
||||||
|
|
||||||
if (destinationsToBeProcessed.Count == 0)
|
if (destinationsToBeProcessed.Count == 0)
|
||||||
return pathsToReturn;
|
return pathsToReturn;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user