refactor: Tidying

This commit is contained in:
Chris Cameron
2018-10-08 15:06:03 -04:00
parent e01e41c449
commit 6540ab5177
2 changed files with 28 additions and 20 deletions

View File

@@ -178,12 +178,12 @@ namespace ICD.Common.Utils.Tests
[Test] [Test]
public void BreadthFirstSearchManyDestinationsTest() public void BreadthFirstSearchManyDestinationsTest()
{ {
Assert.Throws<ArgumentNullException>(() => RecursionUtils.BreadthFirstSearchManyDestinations(1, new[] { 1 }, null)); Assert.Throws<ArgumentNullException>(() => RecursionUtils.BreadthFirstSearchPathManyDestinations(1, new[] { 1 }, null));
Assert.Throws<ArgumentNullException>(() => RecursionUtils.BreadthFirstSearchManyDestinations(1, null, Graph)); Assert.Throws<ArgumentNullException>(() => RecursionUtils.BreadthFirstSearchPathManyDestinations(1, null, Graph));
Assert.AreEqual(0, RecursionUtils.BreadthFirstSearchManyDestinations(1, new[] { 5 }, Graph).Count()); Assert.AreEqual(0, RecursionUtils.BreadthFirstSearchPathManyDestinations(1, new[] { 5 }, Graph).Count());
Dictionary<int, IEnumerable<int>> paths = Dictionary<int, IEnumerable<int>> paths =
RecursionUtils.BreadthFirstSearchManyDestinations(1, new[] {21, 22, 31, 43, 62}, WideGraph) RecursionUtils.BreadthFirstSearchPathManyDestinations(1, new[] {21, 22, 31, 43, 62}, WideGraph)
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
//Make sure all paths were found //Make sure all paths were found

View File

@@ -235,12 +235,16 @@ namespace ICD.Common.Utils
return null; return null;
} }
/// <summary>
[NotNull] /// Returns the shortest path from root to each destination via breadth-first search.
public static IEnumerable<KeyValuePair<T, IEnumerable<T>>> BreadthFirstSearchManyDestinations<T>(T root, /// </summary>
IEnumerable<T> destinations, /// <typeparam name="T"></typeparam>
Func<T, IEnumerable<T>> /// <param name="root"></param>
getChildren) /// <param name="destinations"></param>
/// <param name="getChildren"></param>
/// <returns></returns>
public static IEnumerable<KeyValuePair<T, IEnumerable<T>>>
BreadthFirstSearchPathManyDestinations<T>(T root, IEnumerable<T> destinations, Func<T, IEnumerable<T>> getChildren)
{ {
if (destinations == null) if (destinations == null)
throw new ArgumentNullException("destinations"); throw new ArgumentNullException("destinations");
@@ -251,14 +255,18 @@ namespace ICD.Common.Utils
return BreadthFirstSearchPathManyDestinations(root, destinations, getChildren, EqualityComparer<T>.Default); return BreadthFirstSearchPathManyDestinations(root, destinations, getChildren, EqualityComparer<T>.Default);
} }
[NotNull] /// <summary>
public static IEnumerable<KeyValuePair<T, IEnumerable<T>>> BreadthFirstSearchPathManyDestinations<T>(T root, /// Returns the shortest path from root to each destination via breadth-first search.
IEnumerable<T> /// </summary>
destinations, /// <typeparam name="T"></typeparam>
Func<T, IEnumerable<T>> /// <param name="root"></param>
getChildren, /// <param name="destinations"></param>
IEqualityComparer<T> /// <param name="getChildren"></param>
comparer) /// <param name="comparer"></param>
/// <returns></returns>
public static IEnumerable<KeyValuePair<T, IEnumerable<T>>>
BreadthFirstSearchPathManyDestinations<T>(T root, IEnumerable<T> destinations, Func<T, IEnumerable<T>> getChildren,
IEqualityComparer<T> comparer)
{ {
if (destinations == null) if (destinations == null)
throw new ArgumentNullException("destinations"); throw new ArgumentNullException("destinations");
@@ -270,7 +278,6 @@ 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);
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
@@ -303,7 +310,8 @@ namespace ICD.Common.Utils
{ {
destinationsToBeProcessed.Remove(destination); destinationsToBeProcessed.Remove(destination);
yield return new KeyValuePair<T, IEnumerable<T>>(destination, GetPath(destination, root, nodeParents, comparer).Reverse()); yield return
new KeyValuePair<T, IEnumerable<T>>(destination, GetPath(destination, root, nodeParents, comparer).Reverse());
} }
if (destinationsToBeProcessed.Count == 0) if (destinationsToBeProcessed.Count == 0)