mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-07-02 08:38:13 +00:00
Initial commit of RecursionUtils
This commit is contained in:
parent
f7cd51d3df
commit
bc61686fa8
2 changed files with 31 additions and 0 deletions
|
|
@ -87,6 +87,7 @@
|
|||
<Compile Include="Extensions\ListExtensions.cs" />
|
||||
<Compile Include="Properties\Annotations.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="RecursionUtils.cs" />
|
||||
<Compile Include="ReprBuilder.cs" />
|
||||
<Compile Include="Services\Logging\ILoggerService.cs" />
|
||||
<Compile Include="Services\Logging\LogItem.cs" />
|
||||
|
|
|
|||
30
ICD.Common.Utils/RecursionUtils.cs
Normal file
30
ICD.Common.Utils/RecursionUtils.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ICD.Common.Utils
|
||||
{
|
||||
public static class RecursionUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns all of the nodes in the tree via breadth-first search.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="root"></param>
|
||||
/// <param name="getChildren"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> BreadthFirstSearch<T>(T root, Func<T, IEnumerable<T>> getChildren)
|
||||
{
|
||||
Queue<T> process = new Queue<T>();
|
||||
process.Enqueue(root);
|
||||
|
||||
while (process.Count > 0)
|
||||
{
|
||||
T current = process.Dequeue();
|
||||
yield return current;
|
||||
|
||||
foreach (T child in getChildren(current))
|
||||
process.Enqueue(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue