Simplifying BreadthFirstSearchPathManyDestinations

This commit is contained in:
Chris Cameron
2018-01-02 14:59:25 -05:00
parent e7afd8ccea
commit 1e2a863744

View File

@@ -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,10 +100,10 @@ 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)
{ {
if (getChildren == null) if (getChildren == null)
throw new ArgumentNullException("getChildren"); throw new ArgumentNullException("getChildren");
@@ -113,13 +112,13 @@ 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,
IEqualityComparer<T> IEqualityComparer<T>
comparer) comparer)
{ {
if (destinations == null) if (destinations == null)
throw new ArgumentNullException("destinations"); throw new ArgumentNullException("destinations");
@@ -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();