Concat.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 AsyncEnumerableEx
  11. {
  12. public static IAsyncEnumerable<TSource> Concat<TSource>(this IAsyncEnumerable<IAsyncEnumerable<TSource>> sources)
  13. {
  14. if (sources == null)
  15. throw Error.ArgumentNull(nameof(sources));
  16. #if USE_ASYNC_ITERATOR
  17. return AsyncEnumerable.Create(Core);
  18. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  19. {
  20. await foreach (var source in sources.WithCancellation(cancellationToken).ConfigureAwait(false))
  21. {
  22. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  23. {
  24. yield return item;
  25. }
  26. }
  27. }
  28. #else
  29. return new ConcatAsyncEnumerableAsyncIterator<TSource>(sources);
  30. #endif
  31. }
  32. public static IAsyncEnumerable<TSource> Concat<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  33. {
  34. if (sources == null)
  35. throw Error.ArgumentNull(nameof(sources));
  36. #if USE_ASYNC_ITERATOR
  37. return AsyncEnumerable.Create(Core);
  38. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  39. {
  40. foreach (var source in sources)
  41. {
  42. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  43. {
  44. yield return item;
  45. }
  46. }
  47. }
  48. #else
  49. return ConcatCore(sources);
  50. #endif
  51. }
  52. public static IAsyncEnumerable<TSource> Concat<TSource>(params IAsyncEnumerable<TSource>[] sources)
  53. {
  54. if (sources == null)
  55. throw Error.ArgumentNull(nameof(sources));
  56. #if USE_ASYNC_ITERATOR
  57. return AsyncEnumerable.Create(Core);
  58. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  59. {
  60. foreach (var source in sources)
  61. {
  62. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  63. {
  64. yield return item;
  65. }
  66. }
  67. }
  68. #else
  69. return ConcatCore(sources);
  70. #endif
  71. }
  72. #if !USE_ASYNC_ITERATOR
  73. private static IAsyncEnumerable<TSource> ConcatCore<TSource>(IEnumerable<IAsyncEnumerable<TSource>> sources)
  74. {
  75. return new ConcatEnumerableAsyncIterator<TSource>(sources);
  76. }
  77. private sealed class ConcatEnumerableAsyncIterator<TSource> : AsyncIterator<TSource>
  78. {
  79. private readonly IEnumerable<IAsyncEnumerable<TSource>> _source;
  80. public ConcatEnumerableAsyncIterator(IEnumerable<IAsyncEnumerable<TSource>> source)
  81. {
  82. Debug.Assert(source != null);
  83. _source = source;
  84. }
  85. public override AsyncIteratorBase<TSource> Clone()
  86. {
  87. return new ConcatEnumerableAsyncIterator<TSource>(_source);
  88. }
  89. public override async ValueTask DisposeAsync()
  90. {
  91. if (_outerEnumerator != null)
  92. {
  93. _outerEnumerator.Dispose();
  94. _outerEnumerator = null;
  95. }
  96. if (_currentEnumerator != null)
  97. {
  98. await _currentEnumerator.DisposeAsync().ConfigureAwait(false);
  99. _currentEnumerator = null;
  100. }
  101. await base.DisposeAsync().ConfigureAwait(false);
  102. }
  103. // State machine vars
  104. private IEnumerator<IAsyncEnumerable<TSource>> _outerEnumerator;
  105. private IAsyncEnumerator<TSource> _currentEnumerator;
  106. private int _mode;
  107. private const int State_OuterNext = 1;
  108. private const int State_While = 4;
  109. protected override async ValueTask<bool> MoveNextCore()
  110. {
  111. switch (_state)
  112. {
  113. case AsyncIteratorState.Allocated:
  114. _outerEnumerator = _source.GetEnumerator();
  115. _mode = State_OuterNext;
  116. _state = AsyncIteratorState.Iterating;
  117. goto case AsyncIteratorState.Iterating;
  118. case AsyncIteratorState.Iterating:
  119. switch (_mode)
  120. {
  121. case State_OuterNext:
  122. if (_outerEnumerator.MoveNext())
  123. {
  124. // make sure we dispose the previous one if we're about to replace it
  125. if (_currentEnumerator != null)
  126. {
  127. await _currentEnumerator.DisposeAsync().ConfigureAwait(false);
  128. }
  129. _currentEnumerator = _outerEnumerator.Current.GetAsyncEnumerator(_cancellationToken);
  130. _mode = State_While;
  131. goto case State_While;
  132. }
  133. break;
  134. case State_While:
  135. if (await _currentEnumerator.MoveNextAsync().ConfigureAwait(false))
  136. {
  137. _current = _currentEnumerator.Current;
  138. return true;
  139. }
  140. // No more on the inner enumerator, move to the next outer
  141. goto case State_OuterNext;
  142. }
  143. await DisposeAsync().ConfigureAwait(false);
  144. break;
  145. }
  146. return false;
  147. }
  148. }
  149. private sealed class ConcatAsyncEnumerableAsyncIterator<TSource> : AsyncIterator<TSource>
  150. {
  151. private readonly IAsyncEnumerable<IAsyncEnumerable<TSource>> _source;
  152. public ConcatAsyncEnumerableAsyncIterator(IAsyncEnumerable<IAsyncEnumerable<TSource>> source)
  153. {
  154. Debug.Assert(source != null);
  155. _source = source;
  156. }
  157. public override AsyncIteratorBase<TSource> Clone()
  158. {
  159. return new ConcatAsyncEnumerableAsyncIterator<TSource>(_source);
  160. }
  161. public override async ValueTask DisposeAsync()
  162. {
  163. if (_outerEnumerator != null)
  164. {
  165. await _outerEnumerator.DisposeAsync().ConfigureAwait(false);
  166. _outerEnumerator = null;
  167. }
  168. if (_currentEnumerator != null)
  169. {
  170. await _currentEnumerator.DisposeAsync().ConfigureAwait(false);
  171. _currentEnumerator = null;
  172. }
  173. await base.DisposeAsync().ConfigureAwait(false);
  174. }
  175. // State machine vars
  176. private IAsyncEnumerator<IAsyncEnumerable<TSource>> _outerEnumerator;
  177. private IAsyncEnumerator<TSource> _currentEnumerator;
  178. private int _mode;
  179. private const int State_OuterNext = 1;
  180. private const int State_While = 4;
  181. protected override async ValueTask<bool> MoveNextCore()
  182. {
  183. switch (_state)
  184. {
  185. case AsyncIteratorState.Allocated:
  186. _outerEnumerator = _source.GetAsyncEnumerator(_cancellationToken);
  187. _mode = State_OuterNext;
  188. _state = AsyncIteratorState.Iterating;
  189. goto case AsyncIteratorState.Iterating;
  190. case AsyncIteratorState.Iterating:
  191. switch (_mode)
  192. {
  193. case State_OuterNext:
  194. if (await _outerEnumerator.MoveNextAsync().ConfigureAwait(false))
  195. {
  196. // make sure we dispose the previous one if we're about to replace it
  197. if (_currentEnumerator != null)
  198. {
  199. await _currentEnumerator.DisposeAsync().ConfigureAwait(false);
  200. }
  201. _currentEnumerator = _outerEnumerator.Current.GetAsyncEnumerator(_cancellationToken);
  202. _mode = State_While;
  203. goto case State_While;
  204. }
  205. break;
  206. case State_While:
  207. if (await _currentEnumerator.MoveNextAsync().ConfigureAwait(false))
  208. {
  209. _current = _currentEnumerator.Current;
  210. return true;
  211. }
  212. // No more on the inner enumerator, move to the next outer
  213. goto case State_OuterNext;
  214. }
  215. await DisposeAsync().ConfigureAwait(false);
  216. break;
  217. }
  218. return false;
  219. }
  220. }
  221. #endif
  222. }
  223. }