From bc61686fa8783ba7ef77fd1ea0d8f2a6f09e4265 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Wed, 20 Sep 2017 11:05:54 -0400 Subject: [PATCH] Initial commit of RecursionUtils --- .../ICD.Common.Utils_SimplSharp.csproj | 1 + ICD.Common.Utils/RecursionUtils.cs | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 ICD.Common.Utils/RecursionUtils.cs diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index 0774ec3..23bf212 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -87,6 +87,7 @@ + diff --git a/ICD.Common.Utils/RecursionUtils.cs b/ICD.Common.Utils/RecursionUtils.cs new file mode 100644 index 0000000..bef79d9 --- /dev/null +++ b/ICD.Common.Utils/RecursionUtils.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; + +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) + { + Queue process = new Queue(); + process.Enqueue(root); + + while (process.Count > 0) + { + T current = process.Dequeue(); + yield return current; + + foreach (T child in getChildren(current)) + process.Enqueue(child); + } + } + } +}