TakeWhile.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (predicate == null)
  16. throw new ArgumentNullException(nameof(predicate));
  17. return new TakeWhileAsyncIterator<TSource>(source, predicate);
  18. }
  19. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (predicate == null)
  24. throw new ArgumentNullException(nameof(predicate));
  25. return new TakeWhileWithIndexAsyncIterator<TSource>(source, predicate);
  26. }
  27. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (predicate == null)
  32. throw new ArgumentNullException(nameof(predicate));
  33. return new TakeWhileAsyncIteratorWithTask<TSource>(source, predicate);
  34. }
  35. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Task<bool>> predicate)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. if (predicate == null)
  40. throw new ArgumentNullException(nameof(predicate));
  41. return new TakeWhileWithIndexAsyncIteratorWithTask<TSource>(source, predicate);
  42. }
  43. private sealed class TakeWhileAsyncIterator<TSource> : AsyncIterator<TSource>
  44. {
  45. private readonly Func<TSource, bool> predicate;
  46. private readonly IAsyncEnumerable<TSource> source;
  47. private IAsyncEnumerator<TSource> enumerator;
  48. public TakeWhileAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  49. {
  50. Debug.Assert(predicate != null);
  51. Debug.Assert(source != null);
  52. this.source = source;
  53. this.predicate = predicate;
  54. }
  55. public override AsyncIterator<TSource> Clone()
  56. {
  57. return new TakeWhileAsyncIterator<TSource>(source, predicate);
  58. }
  59. public override async Task DisposeAsync()
  60. {
  61. if (enumerator != null)
  62. {
  63. await enumerator.DisposeAsync().ConfigureAwait(false);
  64. enumerator = null;
  65. }
  66. await base.DisposeAsync().ConfigureAwait(false);
  67. }
  68. protected override async Task<bool> MoveNextCore()
  69. {
  70. switch (state)
  71. {
  72. case AsyncIteratorState.Allocated:
  73. enumerator = source.GetAsyncEnumerator();
  74. state = AsyncIteratorState.Iterating;
  75. goto case AsyncIteratorState.Iterating;
  76. case AsyncIteratorState.Iterating:
  77. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  78. {
  79. var item = enumerator.Current;
  80. if (!predicate(item))
  81. {
  82. break;
  83. }
  84. current = item;
  85. return true;
  86. }
  87. break;
  88. }
  89. await DisposeAsync().ConfigureAwait(false);
  90. return false;
  91. }
  92. }
  93. private sealed class TakeWhileWithIndexAsyncIterator<TSource> : AsyncIterator<TSource>
  94. {
  95. private readonly Func<TSource, int, bool> predicate;
  96. private readonly IAsyncEnumerable<TSource> source;
  97. private IAsyncEnumerator<TSource> enumerator;
  98. private int index;
  99. public TakeWhileWithIndexAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  100. {
  101. Debug.Assert(predicate != null);
  102. Debug.Assert(source != null);
  103. this.source = source;
  104. this.predicate = predicate;
  105. }
  106. public override AsyncIterator<TSource> Clone()
  107. {
  108. return new TakeWhileWithIndexAsyncIterator<TSource>(source, predicate);
  109. }
  110. public override async Task DisposeAsync()
  111. {
  112. if (enumerator != null)
  113. {
  114. await enumerator.DisposeAsync().ConfigureAwait(false);
  115. enumerator = null;
  116. }
  117. await base.DisposeAsync().ConfigureAwait(false);
  118. }
  119. protected override async Task<bool> MoveNextCore()
  120. {
  121. switch (state)
  122. {
  123. case AsyncIteratorState.Allocated:
  124. enumerator = source.GetAsyncEnumerator();
  125. index = -1;
  126. state = AsyncIteratorState.Iterating;
  127. goto case AsyncIteratorState.Iterating;
  128. case AsyncIteratorState.Iterating:
  129. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  130. {
  131. var item = enumerator.Current;
  132. checked
  133. {
  134. index++;
  135. }
  136. if (!predicate(item, index))
  137. {
  138. break;
  139. }
  140. current = item;
  141. return true;
  142. }
  143. break;
  144. }
  145. await DisposeAsync().ConfigureAwait(false);
  146. return false;
  147. }
  148. }
  149. private sealed class TakeWhileAsyncIteratorWithTask<TSource> : AsyncIterator<TSource>
  150. {
  151. private readonly Func<TSource, Task<bool>> predicate;
  152. private readonly IAsyncEnumerable<TSource> source;
  153. private IAsyncEnumerator<TSource> enumerator;
  154. public TakeWhileAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  155. {
  156. Debug.Assert(predicate != null);
  157. Debug.Assert(source != null);
  158. this.source = source;
  159. this.predicate = predicate;
  160. }
  161. public override AsyncIterator<TSource> Clone()
  162. {
  163. return new TakeWhileAsyncIteratorWithTask<TSource>(source, predicate);
  164. }
  165. public override async Task DisposeAsync()
  166. {
  167. if (enumerator != null)
  168. {
  169. await enumerator.DisposeAsync().ConfigureAwait(false);
  170. enumerator = null;
  171. }
  172. await base.DisposeAsync().ConfigureAwait(false);
  173. }
  174. protected override async Task<bool> MoveNextCore()
  175. {
  176. switch (state)
  177. {
  178. case AsyncIteratorState.Allocated:
  179. enumerator = source.GetAsyncEnumerator();
  180. state = AsyncIteratorState.Iterating;
  181. goto case AsyncIteratorState.Iterating;
  182. case AsyncIteratorState.Iterating:
  183. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  184. {
  185. var item = enumerator.Current;
  186. if (!await predicate(item).ConfigureAwait(false))
  187. {
  188. break;
  189. }
  190. current = item;
  191. return true;
  192. }
  193. break;
  194. }
  195. await DisposeAsync().ConfigureAwait(false);
  196. return false;
  197. }
  198. }
  199. private sealed class TakeWhileWithIndexAsyncIteratorWithTask<TSource> : AsyncIterator<TSource>
  200. {
  201. private readonly Func<TSource, int, Task<bool>> predicate;
  202. private readonly IAsyncEnumerable<TSource> source;
  203. private IAsyncEnumerator<TSource> enumerator;
  204. private int index;
  205. public TakeWhileWithIndexAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, int, Task<bool>> predicate)
  206. {
  207. Debug.Assert(predicate != null);
  208. Debug.Assert(source != null);
  209. this.source = source;
  210. this.predicate = predicate;
  211. }
  212. public override AsyncIterator<TSource> Clone()
  213. {
  214. return new TakeWhileWithIndexAsyncIteratorWithTask<TSource>(source, predicate);
  215. }
  216. public override async Task DisposeAsync()
  217. {
  218. if (enumerator != null)
  219. {
  220. await enumerator.DisposeAsync().ConfigureAwait(false);
  221. enumerator = null;
  222. }
  223. await base.DisposeAsync().ConfigureAwait(false);
  224. }
  225. protected override async Task<bool> MoveNextCore()
  226. {
  227. switch (state)
  228. {
  229. case AsyncIteratorState.Allocated:
  230. enumerator = source.GetAsyncEnumerator();
  231. index = -1;
  232. state = AsyncIteratorState.Iterating;
  233. goto case AsyncIteratorState.Iterating;
  234. case AsyncIteratorState.Iterating:
  235. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  236. {
  237. var item = enumerator.Current;
  238. checked
  239. {
  240. index++;
  241. }
  242. if (!await predicate(item, index).ConfigureAwait(false))
  243. {
  244. break;
  245. }
  246. current = item;
  247. return true;
  248. }
  249. break;
  250. }
  251. await DisposeAsync().ConfigureAwait(false);
  252. return false;
  253. }
  254. }
  255. }
  256. }