-
Notifications
You must be signed in to change notification settings - Fork 411
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?
Conversation
// 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe that can be a separate change.
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.
Honestly I'm not sure the deduplicated code really makes things more readable. There is a level on indirection on methods that are quite small. @atifaziz WDYT?
@@ -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 |
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
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 comment
The 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.
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe that can be a separate change.
Refactored Traverse implementation to avoid repetitive code.