Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions MoreLinq/Traverse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace MoreLinq
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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
Copy link
Member

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


var stack = new Stack<T>();
return TraverseImpl(root, x => childrenSelector(x).Reverse(), stack.Push, stack.Pop, stack);
Copy link
Contributor

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.

Copy link
Member

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.

}

static IEnumerable<T> TraverseImpl<T>(
T root,
Func<T, IEnumerable<T>> childrenSelector,
Action<T> Push,
Func<T> Pop,
Copy link
Member

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.

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);
}
}
}
Expand Down