Expand.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. {
  16. throw new ArgumentNullException(nameof(source));
  17. }
  18. if (selector == null)
  19. {
  20. throw new ArgumentNullException(nameof(selector));
  21. }
  22. return new ExpandAsyncIterator<TSource>(source, selector);
  23. }
  24. private sealed class ExpandAsyncIterator<TSource> : AsyncIterator<TSource>
  25. {
  26. private readonly Func<TSource, IAsyncEnumerable<TSource>> selector;
  27. private readonly IAsyncEnumerable<TSource> source;
  28. private IAsyncEnumerator<TSource> enumerator;
  29. private Queue<IAsyncEnumerable<TSource>> queue;
  30. public ExpandAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, IAsyncEnumerable<TSource>> selector)
  31. {
  32. Debug.Assert(source != null);
  33. Debug.Assert(selector != null);
  34. this.source = source;
  35. this.selector = selector;
  36. }
  37. public override AsyncIterator<TSource> Clone()
  38. {
  39. return new ExpandAsyncIterator<TSource>(source, selector);
  40. }
  41. public override void Dispose()
  42. {
  43. if (enumerator != null)
  44. {
  45. enumerator.Dispose();
  46. enumerator = null;
  47. }
  48. queue = null;
  49. base.Dispose();
  50. }
  51. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  52. {
  53. switch (state)
  54. {
  55. case AsyncIteratorState.Allocated:
  56. queue = new Queue<IAsyncEnumerable<TSource>>();
  57. queue.Enqueue(source);
  58. state = AsyncIteratorState.Iterating;
  59. goto case AsyncIteratorState.Iterating;
  60. case AsyncIteratorState.Iterating:
  61. while (true)
  62. {
  63. if (enumerator == null)
  64. {
  65. if (queue.Count > 0)
  66. {
  67. var src = queue.Dequeue();
  68. enumerator?.Dispose();
  69. enumerator = src.GetEnumerator();
  70. continue; // loop
  71. }
  72. break; // while
  73. }
  74. if (await enumerator.MoveNext(cancellationToken)
  75. .ConfigureAwait(false))
  76. {
  77. var item = enumerator.Current;
  78. var next = selector(item);
  79. queue.Enqueue(next);
  80. current = item;
  81. return true;
  82. }
  83. enumerator.Dispose();
  84. enumerator = null;
  85. }
  86. break; // case
  87. }
  88. Dispose();
  89. return false;
  90. }
  91. }
  92. }
  93. }