From 44e36e32727475c199b028f91c5610aa2aec6374 Mon Sep 17 00:00:00 2001 From: leandromoh Date: Mon, 24 Dec 2018 18:13:18 -0200 Subject: [PATCH 1/2] removed duplicated --- MoreLinq/Traverse.cs | 45 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/MoreLinq/Traverse.cs b/MoreLinq/Traverse.cs index 4fb46b5b8..87cc58bf9 100644 --- a/MoreLinq/Traverse.cs +++ b/MoreLinq/Traverse.cs @@ -18,6 +18,7 @@ namespace MoreLinq { using System; + using System.Collections; using System.Collections.Generic; using System.Linq; @@ -46,21 +47,8 @@ public partial class MoreEnumerable public static IEnumerable TraverseBreadthFirst(T root, Func> childrenSelector) { - if (childrenSelector == null) throw new ArgumentNullException(nameof(childrenSelector)); - - return _(); IEnumerable _() - { - var queue = new Queue(); - queue.Enqueue(root); - - while (queue.Count != 0) - { - var current = queue.Dequeue(); - yield return current; - foreach (var child in childrenSelector(current)) - queue.Enqueue(child); - } - } + var queue = new Queue(); + return TraverseImpl(root, childrenSelector, queue.Enqueue, queue.Dequeue, queue); } /// @@ -85,22 +73,33 @@ public static IEnumerable TraverseBreadthFirst(T root, Func public static IEnumerable TraverseDepthFirst(T root, Func> childrenSelector) + { + // because a stack pops the elements out in LIFO order, we need to push them in reverse + // if we want to traverse the returned list in the same order as was returned to us + + var stack = new Stack(); + return TraverseImpl(root, x => childrenSelector(x).Reverse(), stack.Push, stack.Pop, stack); + } + + static IEnumerable TraverseImpl( + T root, + Func> childrenSelector, + Action Push, + Func Pop, + ICollection collection) { if (childrenSelector == null) throw new ArgumentNullException(nameof(childrenSelector)); return _(); IEnumerable _() { - var stack = new Stack(); - stack.Push(root); + Push(root); - while (stack.Count != 0) + while (collection.Count != 0) { - var current = stack.Pop(); + var current = Pop(); yield return current; - // because a stack pops the elements out in LIFO order, we need to push them in reverse - // if we want to traverse the returned list in the same order as was returned to us - foreach (var child in childrenSelector(current).Reverse()) - stack.Push(child); + foreach (var child in childrenSelector(current)) + Push(child); } } } From 60ee09de9b4e8735ebd745b012a1dcc4a2a76e69 Mon Sep 17 00:00:00 2001 From: leandromoh Date: Mon, 24 Dec 2018 18:54:20 -0200 Subject: [PATCH 2/2] fix validation --- MoreLinq/Traverse.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/MoreLinq/Traverse.cs b/MoreLinq/Traverse.cs index 87cc58bf9..2745ab653 100644 --- a/MoreLinq/Traverse.cs +++ b/MoreLinq/Traverse.cs @@ -47,6 +47,8 @@ public partial class MoreEnumerable public static IEnumerable TraverseBreadthFirst(T root, Func> childrenSelector) { + if (childrenSelector == null) throw new ArgumentNullException(nameof(childrenSelector)); + var queue = new Queue(); return TraverseImpl(root, childrenSelector, queue.Enqueue, queue.Dequeue, queue); } @@ -74,6 +76,8 @@ public static IEnumerable TraverseBreadthFirst(T root, Func TraverseDepthFirst(T root, Func> childrenSelector) { + if (childrenSelector == null) throw new ArgumentNullException(nameof(childrenSelector)); + // because a stack pops the elements out in LIFO order, we need to push them in reverse // if we want to traverse the returned list in the same order as was returned to us @@ -88,8 +92,6 @@ static IEnumerable TraverseImpl( Func Pop, ICollection collection) { - if (childrenSelector == null) throw new ArgumentNullException(nameof(childrenSelector)); - return _(); IEnumerable _() { Push(root);