-
Notifications
You must be signed in to change notification settings - Fork 416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactored Traverse implementation #553
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
namespace MoreLinq | ||
{ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
|
@@ -48,19 +49,8 @@ public static IEnumerable<T> TraverseBreadthFirst<T>(T root, Func<T, IEnumerable | |
{ | ||
if (childrenSelector == null) throw new ArgumentNullException(nameof(childrenSelector)); | ||
|
||
return _(); IEnumerable<T> _() | ||
{ | ||
var queue = new Queue<T>(); | ||
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<T>(); | ||
return TraverseImpl(root, childrenSelector, queue.Enqueue, queue.Dequeue, queue); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -88,19 +78,30 @@ public static IEnumerable<T> TraverseDepthFirst<T>(T root, Func<T, IEnumerable<T | |
{ | ||
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 | ||
|
||
var stack = new Stack<T>(); | ||
return TraverseImpl(root, x => childrenSelector(x).Reverse(), stack.Push, stack.Pop, stack); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can avoid the reverse by using a stack (or it is a queue), of children enumerator. We should take care of the dispose. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe that can be a separate change. |
||
} | ||
|
||
static IEnumerable<T> TraverseImpl<T>( | ||
T root, | ||
Func<T, IEnumerable<T>> childrenSelector, | ||
Action<T> Push, | ||
Func<T> Pop, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we have any other method naming arguments in Capital case. |
||
ICollection collection) | ||
{ | ||
return _(); IEnumerable<T> _() | ||
{ | ||
var stack = new Stack<T>(); | ||
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); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is a bit out of context here. Maybe it would be good to be explicit that this applies to the Reverse in the childrenSelector