From 1e2a86374434bfb2a0de1368a1469ce159a0bdbf Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Tue, 2 Jan 2018 14:59:25 -0500 Subject: [PATCH] Simplifying BreadthFirstSearchPathManyDestinations --- ICD.Common.Utils/RecursionUtils.cs | 51 ++++++++++++++---------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/ICD.Common.Utils/RecursionUtils.cs b/ICD.Common.Utils/RecursionUtils.cs index 9dcadca..1cbc435 100644 --- a/ICD.Common.Utils/RecursionUtils.cs +++ b/ICD.Common.Utils/RecursionUtils.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Linq; using ICD.Common.Properties; using ICD.Common.Utils.Collections; -using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils { @@ -101,10 +100,10 @@ namespace ICD.Common.Utils } [NotNull] - public static Dictionary> BreadthFirstSearchManyDestinations(T root, - Dictionary destinations, - Func> - getChildren) + public static Dictionary> BreadthFirstSearchManyDestinations(T root, + IEnumerable destinations, + Func> + getChildren) { if (getChildren == null) throw new ArgumentNullException("getChildren"); @@ -113,13 +112,13 @@ namespace ICD.Common.Utils } [NotNull] - public static Dictionary> BreadthFirstSearchPathManyDestinations(T root, - Dictionary - destinations, - Func> - getChildren, - IEqualityComparer - comparer) + public static Dictionary> BreadthFirstSearchPathManyDestinations(T root, + IEnumerable + destinations, + Func> + getChildren, + IEqualityComparer + comparer) { if (destinations == null) throw new ArgumentNullException("destinations"); @@ -130,21 +129,20 @@ namespace ICD.Common.Utils if (comparer == null) throw new ArgumentNullException("comparer"); - Dictionary destinationsToBeProcessed = new Dictionary(destinations); - List destinationsProcessed = new List(); - Dictionary> pathsToReturn = new Dictionary>(); + IcdHashSet destinationsToBeProcessed = new IcdHashSet(destinations); + IcdHashSet destinationsProcessed = new IcdHashSet(); + Dictionary> pathsToReturn = new Dictionary>(); // Edge case, root is the destination - foreach ( - KeyValuePair destination in - destinationsToBeProcessed.Where(destination => comparer.Equals(root, destination.Value))) + foreach (T destination in + destinationsToBeProcessed.Where(destination => comparer.Equals(root, destination))) { - destinationsProcessed.Add(destination.Value); - pathsToReturn.Add(destination.Key, new[] {root}); + destinationsProcessed.Add(destination); + pathsToReturn.Add(destination, new[] {root}); } foreach (T destination in destinationsProcessed) - destinationsToBeProcessed.RemoveValue(destination); + destinationsToBeProcessed.Remove(destination); destinationsProcessed.Clear(); if (destinationsToBeProcessed.Count == 0) return pathsToReturn; @@ -164,16 +162,15 @@ namespace ICD.Common.Utils nodeParents.Add(node, current); T closureNode = node; - foreach ( - KeyValuePair destination in - destinationsToBeProcessed.Where(destination => comparer.Equals(closureNode, destination.Value))) + foreach (T destination in + destinationsToBeProcessed.Where(destination => comparer.Equals(closureNode, destination))) { - destinationsProcessed.Add(destination.Value); - pathsToReturn.Add(destination.Key, GetPath(destination.Value, nodeParents).Reverse()); + destinationsProcessed.Add(destination); + pathsToReturn.Add(destination, GetPath(destination, nodeParents).Reverse()); } foreach (T destination in destinationsProcessed) - destinationsToBeProcessed.RemoveValue(destination); + destinationsToBeProcessed.Remove(destination); destinationsProcessed.Clear();