Take.cs 11 KB

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