Skip to content

Commit

Permalink
Use Queue instead of Stack for traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
rameel committed Aug 26, 2024
1 parent 94c053a commit a6d5383
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Ramstack.Globbing/Traversal/FileTreeAsyncEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ private async IAsyncEnumerable<TResult> EnumerateAsync(CancellationTokenSource?

try
{
var stack = new Stack<(TEntry Directory, string Path)>();
stack.Push((_directory, ""));
var queue = new Queue<(TEntry Directory, string Path)>();
queue.Enqueue((_directory, ""));

while (stack.TryPop(out var e))
while (queue.TryDequeue(out var e))
{
await foreach (var entry in ChildrenSelector(e.Directory, cancellationToken).ConfigureAwait(false))
{
Expand All @@ -101,7 +101,7 @@ private async IAsyncEnumerable<TResult> EnumerateAsync(CancellationTokenSource?

if (ShouldRecursePredicate == null || ShouldRecursePredicate(entry))
if (PathHelper.IsPartialMatch(fullName, Patterns, Flags))
stack.Push((entry, fullName.ToString()));
queue.Enqueue((entry, fullName.ToString()));

if (ShouldIncludePredicate == null || ShouldIncludePredicate(entry))
if (PathHelper.IsMatch(fullName, Patterns, Flags))
Expand Down
8 changes: 4 additions & 4 deletions Ramstack.Globbing/Traversal/FileTreeEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ private IEnumerable<TResult> Enumerate()
{
var chars = ArrayPool<char>.Shared.Rent(512);

var stack = new Stack<(TEntry Directory, string Path)>();
stack.Push((_directory, ""));
var queue = new Queue<(TEntry Directory, string Path)>();
queue.Enqueue((_directory, ""));

while (stack.TryPop(out var e))
while (queue.TryDequeue(out var e))
{
foreach (var entry in ChildrenSelector(e.Directory))
{
Expand All @@ -89,7 +89,7 @@ private IEnumerable<TResult> Enumerate()

if (ShouldRecursePredicate == null || ShouldRecursePredicate(entry))
if (PathHelper.IsPartialMatch(fullName, Patterns, Flags))
stack.Push((entry, fullName.ToString()));
queue.Enqueue((entry, fullName.ToString()));

if (ShouldIncludePredicate == null || ShouldIncludePredicate(entry))
if (PathHelper.IsMatch(fullName, Patterns, Flags))
Expand Down

0 comments on commit a6d5383

Please sign in to comment.