Expand.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<TSource> Expand<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, IAsyncEnumerable<TSource>> selector)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. if (selector == null)
  17. throw new ArgumentNullException(nameof(selector));
  18. return new ExpandAsyncIterator<TSource>(source, selector);
  19. }
  20. private sealed class ExpandAsyncIterator<TSource> : AsyncIterator<TSource>
  21. {
  22. private readonly Func<TSource, IAsyncEnumerable<TSource>> selector;
  23. private readonly IAsyncEnumerable<TSource> source;
  24. private IAsyncEnumerator<TSource> enumerator;
  25. private Queue<IAsyncEnumerable<TSource>> queue;
  26. public ExpandAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, IAsyncEnumerable<TSource>> selector)
  27. {
  28. Debug.Assert(source != null);
  29. Debug.Assert(selector != null);
  30. this.source = source;
  31. this.selector = selector;
  32. }
  33. public override AsyncIterator<TSource> Clone()
  34. {
  35. return new ExpandAsyncIterator<TSource>(source, selector);
  36. }
  37. public override void Dispose()
  38. {
  39. if (enumerator != null)
  40. {
  41. enumerator.Dispose();
  42. enumerator = null;
  43. }
  44. queue = null;
  45. base.Dispose();
  46. }
  47. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  48. {
  49. switch (state)
  50. {
  51. case AsyncIteratorState.Allocated:
  52. queue = new Queue<IAsyncEnumerable<TSource>>();
  53. queue.Enqueue(source);
  54. state = AsyncIteratorState.Iterating;
  55. goto case AsyncIteratorState.Iterating;
  56. case AsyncIteratorState.Iterating:
  57. while (true)
  58. {
  59. if (enumerator == null)
  60. {
  61. if (queue.Count > 0)
  62. {
  63. var src = queue.Dequeue();
  64. enumerator?.Dispose();
  65. enumerator = src.GetEnumerator();
  66. continue; // loop
  67. }
  68. break; // while
  69. }
  70. if (await enumerator.MoveNext(cancellationToken)
  71. .ConfigureAwait(false))
  72. {
  73. var item = enumerator.Current;
  74. var next = selector(item);
  75. queue.Enqueue(next);
  76. current = item;
  77. return true;
  78. }
  79. enumerator.Dispose();
  80. enumerator = null;
  81. }
  82. break; // case
  83. }
  84. Dispose();
  85. return false;
  86. }
  87. }
  88. }
  89. }