mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
refactor: Tidying
This commit is contained in:
@@ -3,6 +3,7 @@ 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
|
||||||
{
|
{
|
||||||
@@ -97,23 +98,23 @@ namespace ICD.Common.Utils
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true if there is a path from the given root to the given child node.
|
/// Returns true if there is a path from the given root to the given destination node.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="root"></param>
|
/// <param name="root"></param>
|
||||||
/// <param name="child"></param>
|
/// <param name="destination"></param>
|
||||||
/// <param name="getChildren"></param>
|
/// <param name="getChildren"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool BreadthFirstSearch<T>(T root, T child, Func<T, IEnumerable<T>> getChildren)
|
public static bool BreadthFirstSearch<T>(T root, T destination, Func<T, IEnumerable<T>> getChildren)
|
||||||
{
|
{
|
||||||
if (getChildren == null)
|
if (getChildren == null)
|
||||||
throw new ArgumentNullException("getChildren");
|
throw new ArgumentNullException("getChildren");
|
||||||
|
|
||||||
return BreadthFirstSearch(root, child, getChildren, EqualityComparer<T>.Default);
|
return BreadthFirstSearch(root, destination, getChildren, EqualityComparer<T>.Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true if there is a path from the given root to the given child node.
|
/// Returns true if there is a path from the given root to the given destination node.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="root"></param>
|
/// <param name="root"></param>
|
||||||
@@ -159,9 +160,9 @@ namespace ICD.Common.Utils
|
|||||||
Queue<T> process = new Queue<T>();
|
Queue<T> process = new Queue<T>();
|
||||||
process.Enqueue(root);
|
process.Enqueue(root);
|
||||||
|
|
||||||
while (process.Count > 0)
|
T current;
|
||||||
|
while (process.Dequeue(out current))
|
||||||
{
|
{
|
||||||
T current = process.Dequeue();
|
|
||||||
yield return current;
|
yield return current;
|
||||||
|
|
||||||
foreach (T child in getChildren(current))
|
foreach (T child in getChildren(current))
|
||||||
@@ -214,10 +215,9 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
Dictionary<T, T> nodeParents = new Dictionary<T, T>(comparer);
|
Dictionary<T, T> nodeParents = new Dictionary<T, T>(comparer);
|
||||||
|
|
||||||
while (queue.Count > 0)
|
T current;
|
||||||
|
while (queue.Dequeue(out current))
|
||||||
{
|
{
|
||||||
T current = queue.Dequeue();
|
|
||||||
|
|
||||||
foreach (T node in getChildren(current))
|
foreach (T node in getChildren(current))
|
||||||
{
|
{
|
||||||
if (nodeParents.ContainsKey(node))
|
if (nodeParents.ContainsKey(node))
|
||||||
|
|||||||
Reference in New Issue
Block a user