From d1b87cc3295828648ca82c1a2b8fc999c259505f Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Tue, 2 Jan 2018 14:20:52 -0500 Subject: [PATCH] Whitespace, tidying --- ICD.Common.Utils/RecursionUtils.cs | 337 +++++++++++++++-------------- 1 file changed, 175 insertions(+), 162 deletions(-) diff --git a/ICD.Common.Utils/RecursionUtils.cs b/ICD.Common.Utils/RecursionUtils.cs index df9919d..1e0f942 100644 --- a/ICD.Common.Utils/RecursionUtils.cs +++ b/ICD.Common.Utils/RecursionUtils.cs @@ -7,199 +7,212 @@ using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils { - public static class RecursionUtils - { - /// - /// Returns all of the nodes in the tree via breadth-first search. - /// - /// - /// - /// - /// - public static IEnumerable BreadthFirstSearch(T root, Func> getChildren) - { - if (getChildren == null) - throw new ArgumentNullException("getChildren"); + public static class RecursionUtils + { + /// + /// Returns all of the nodes in the tree via breadth-first search. + /// + /// + /// + /// + /// + public static IEnumerable BreadthFirstSearch(T root, Func> getChildren) + { + if (getChildren == null) + throw new ArgumentNullException("getChildren"); - Queue process = new Queue(); - process.Enqueue(root); + Queue process = new Queue(); + process.Enqueue(root); - while (process.Count > 0) - { - T current = process.Dequeue(); - yield return current; + while (process.Count > 0) + { + T current = process.Dequeue(); + yield return current; - foreach (T child in getChildren(current)) - process.Enqueue(child); - } - } + foreach (T child in getChildren(current)) + process.Enqueue(child); + } + } - /// - /// Returns the shortest path from root to destination via breadth-first search. - /// - /// - /// - /// - /// - /// - [CanBeNull] - public static IEnumerable BreadthFirstSearchPath(T root, T destination, Func> getChildren) - { - if (getChildren == null) - throw new ArgumentNullException("getChildren"); + /// + /// Returns the shortest path from root to destination via breadth-first search. + /// + /// + /// + /// + /// + /// + [CanBeNull] + public static IEnumerable BreadthFirstSearchPath(T root, T destination, Func> getChildren) + { + if (getChildren == null) + throw new ArgumentNullException("getChildren"); - return BreadthFirstSearchPath(root, destination, getChildren, EqualityComparer.Default); - } + return BreadthFirstSearchPath(root, destination, getChildren, EqualityComparer.Default); + } - [NotNull] - public static Dictionary> BreadthFirstSearchManyDestinations(T root, Dictionary destinations, Func> getChildren) - { - if (getChildren == null) - throw new ArgumentNullException("getChildren"); + /// + /// Returns the shortest path from root to destination via breadth-first search. + /// + /// + /// + /// + /// + /// + /// + [CanBeNull] + public static IEnumerable BreadthFirstSearchPath(T root, T destination, Func> getChildren, + IEqualityComparer comparer) + { + if (getChildren == null) + throw new ArgumentNullException("getChildren"); - return BreadthFirstSearchPathManyDestinations(root, destinations, getChildren, EqualityComparer.Default); - } + if (comparer == null) + throw new ArgumentNullException("comparer"); - /// - /// Returns the shortest path from root to destination via breadth-first search. - /// - /// - /// - /// - /// - /// - /// - [CanBeNull] - public static IEnumerable BreadthFirstSearchPath(T root, T destination, Func> getChildren, - IEqualityComparer comparer) - { - if (getChildren == null) - throw new ArgumentNullException("getChildren"); + // Edge case - root and destination are the same + if (comparer.Equals(root, destination)) + return new[] {root}; - if (comparer == null) - throw new ArgumentNullException("comparer"); + Queue queue = new Queue(); + queue.Enqueue(root); - // Edge case - root and destination are the same - if (comparer.Equals(root, destination)) - return new[] { root }; + Dictionary nodeParents = new Dictionary(); - Queue queue = new Queue(); - queue.Enqueue(root); + while (queue.Count > 0) + { + T current = queue.Dequeue(); - Dictionary nodeParents = new Dictionary(); + foreach (T node in getChildren(current)) + { + if (nodeParents.ContainsKey(node)) + continue; - while (queue.Count > 0) - { - T current = queue.Dequeue(); + queue.Enqueue(node); + nodeParents.Add(node, current); - foreach (T node in getChildren(current)) - { - if (nodeParents.ContainsKey(node)) - continue; + // Found a path to the destination + if (comparer.Equals(node, destination)) + return GetPath(destination, nodeParents).Reverse(); + } + } - queue.Enqueue(node); - nodeParents.Add(node, current); + return null; + } - // Found a path to the destination - if (comparer.Equals(node, destination)) - return GetPath(destination, nodeParents).Reverse(); - } - } + [NotNull] + public static Dictionary> BreadthFirstSearchManyDestinations(T root, + Dictionary destinations, + Func> + getChildren) + { + if (getChildren == null) + throw new ArgumentNullException("getChildren"); - return null; - } + return BreadthFirstSearchPathManyDestinations(root, destinations, getChildren, EqualityComparer.Default); + } - [NotNull] - public static Dictionary> BreadthFirstSearchPathManyDestinations(T root, Dictionary destinations, - Func> getChildren, - IEqualityComparer comparer) - { - if (getChildren == null) - throw new ArgumentNullException("getChildren"); + [NotNull] + public static Dictionary> BreadthFirstSearchPathManyDestinations(T root, + Dictionary + destinations, + Func> + getChildren, + IEqualityComparer + comparer) + { + if (destinations == null) + throw new ArgumentNullException("destinations"); - if (comparer == null) - throw new ArgumentNullException("comparer"); + if (getChildren == null) + throw new ArgumentNullException("getChildren"); - Dictionary destinationsToBeProcessed = new Dictionary(destinations); - List destinationsProcessed = new List(); - Dictionary> pathsToReturn = new Dictionary>(); + if (comparer == null) + throw new ArgumentNullException("comparer"); - //Edge case, root is the destination - foreach (var destination in destinationsToBeProcessed.Where(destination => comparer.Equals(root, destination.Value))) - { - destinationsProcessed.Add(destination.Value); - pathsToReturn.Add(destination.Key, new[] { root }); - } + Dictionary destinationsToBeProcessed = new Dictionary(destinations); + List destinationsProcessed = new List(); + Dictionary> pathsToReturn = new Dictionary>(); - foreach (var destination in destinationsProcessed) - { - destinationsToBeProcessed.RemoveValue(destination); - } - destinationsProcessed.Clear(); - if (destinationsToBeProcessed.Count == 0) - { - return pathsToReturn; - } + // Edge case, root is the destination + foreach ( + KeyValuePair destination in + destinationsToBeProcessed.Where(destination => comparer.Equals(root, destination.Value))) + { + destinationsProcessed.Add(destination.Value); + pathsToReturn.Add(destination.Key, new[] {root}); + } - Queue queue = new Queue(); - queue.Enqueue(root); + foreach (T destination in destinationsProcessed) + { + destinationsToBeProcessed.RemoveValue(destination); + } + destinationsProcessed.Clear(); + if (destinationsToBeProcessed.Count == 0) + { + return pathsToReturn; + } - Dictionary nodeParents = new Dictionary(); + Queue queue = new Queue(); + queue.Enqueue(root); - while (queue.Count > 0) - { - T current = queue.Dequeue(); + Dictionary nodeParents = new Dictionary(); - foreach (T node in getChildren(current).Where(node => !nodeParents.ContainsKey(node))) - { - queue.Enqueue(node); - nodeParents.Add(node, current); + while (queue.Count > 0) + { + T current = queue.Dequeue(); - foreach (var destination in destinationsToBeProcessed.Where(destination => comparer.Equals(node, destination.Value))) - { - destinationsProcessed.Add(destination.Value); - pathsToReturn.Add(destination.Key, GetPath(destination.Value, nodeParents).Reverse()); - } - foreach (var destination in destinationsProcessed) - { - destinationsToBeProcessed.RemoveValue(destination); - } - destinationsProcessed.Clear(); - if (destinationsToBeProcessed.Count == 0) - { - return pathsToReturn; - } - } - } + foreach (T node in getChildren(current).Where(node => !nodeParents.ContainsKey(node))) + { + queue.Enqueue(node); + nodeParents.Add(node, current); - return pathsToReturn; - } + foreach ( + KeyValuePair destination in + destinationsToBeProcessed.Where(destination => comparer.Equals(node, destination.Value))) + { + destinationsProcessed.Add(destination.Value); + pathsToReturn.Add(destination.Key, GetPath(destination.Value, nodeParents).Reverse()); + } - /// - /// Walks through a map of nodes from the starting point. - /// - /// - /// - /// - /// - private static IEnumerable GetPath(T start, IDictionary nodeParents) - { - IcdHashSet visited = new IcdHashSet(); + foreach (T destination in destinationsProcessed) + destinationsToBeProcessed.RemoveValue(destination); - while (true) - { - yield return start; - visited.Add(start); + destinationsProcessed.Clear(); - T next; - if (!nodeParents.TryGetValue(start, out next)) - break; + if (destinationsToBeProcessed.Count == 0) + return pathsToReturn; + } + } - if (visited.Contains(next)) - break; + return pathsToReturn; + } - start = next; - } - } - } + /// + /// Walks through a map of nodes from the starting point. + /// + /// + /// + /// + /// + private static IEnumerable GetPath(T start, IDictionary nodeParents) + { + IcdHashSet visited = new IcdHashSet(); + + while (true) + { + yield return start; + visited.Add(start); + + T next; + if (!nodeParents.TryGetValue(start, out next)) + break; + + if (visited.Contains(next)) + break; + + start = next; + } + } + } }