Take.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. if (!isDone)
  135. {
  136. if (await enumerator.MoveNext(cancellationToken)
  137. .ConfigureAwait(false))
  138. {
  139. if (count > 0)
  140. {
  141. var item = enumerator.Current;
  142. if (queue.Count >= count)
  143. {
  144. queue.Dequeue();
  145. }
  146. queue.Enqueue(item);
  147. }
  148. }
  149. else
  150. {
  151. isDone = true;
  152. // Dispose early here as we can
  153. enumerator.Dispose();
  154. enumerator = null;
  155. }
  156. goto case AsyncIteratorState.Iterating; // loop until queue is drained
  157. }
  158. if (queue.Count > 0)
  159. {
  160. current = queue.Dequeue();
  161. return true;
  162. }
  163. break;
  164. }
  165. Dispose();
  166. return false;
  167. }
  168. }
  169. private sealed class TakeWhileAsyncIterator<TSource> : AsyncIterator<TSource>
  170. {
  171. private readonly Func<TSource, bool> predicate;
  172. private readonly IAsyncEnumerable<TSource> source;
  173. private IAsyncEnumerator<TSource> enumerator;
  174. public TakeWhileAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  175. {
  176. Debug.Assert(predicate != null);
  177. Debug.Assert(source != null);
  178. this.source = source;
  179. this.predicate = predicate;
  180. }
  181. public override AsyncIterator<TSource> Clone()
  182. {
  183. return new TakeWhileAsyncIterator<TSource>(source, predicate);
  184. }
  185. public override void Dispose()
  186. {
  187. if (enumerator != null)
  188. {
  189. enumerator.Dispose();
  190. enumerator = null;
  191. }
  192. base.Dispose();
  193. }
  194. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  195. {
  196. switch (state)
  197. {
  198. case AsyncIteratorState.Allocated:
  199. enumerator = source.GetEnumerator();
  200. state = AsyncIteratorState.Iterating;
  201. goto case AsyncIteratorState.Iterating;
  202. case AsyncIteratorState.Iterating:
  203. if (await enumerator.MoveNext(cancellationToken)
  204. .ConfigureAwait(false))
  205. {
  206. var item = enumerator.Current;
  207. if (!predicate(item))
  208. {
  209. break;
  210. }
  211. current = item;
  212. return true;
  213. }
  214. break;
  215. }
  216. Dispose();
  217. return false;
  218. }
  219. }
  220. private sealed class TakeWhileWithIndexAsyncIterator<TSource> : AsyncIterator<TSource>
  221. {
  222. private readonly Func<TSource, int, bool> predicate;
  223. private readonly IAsyncEnumerable<TSource> source;
  224. private IAsyncEnumerator<TSource> enumerator;
  225. private int index;
  226. public TakeWhileWithIndexAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  227. {
  228. Debug.Assert(predicate != null);
  229. Debug.Assert(source != null);
  230. this.source = source;
  231. this.predicate = predicate;
  232. }
  233. public override AsyncIterator<TSource> Clone()
  234. {
  235. return new TakeWhileWithIndexAsyncIterator<TSource>(source, predicate);
  236. }
  237. public override void Dispose()
  238. {
  239. if (enumerator != null)
  240. {
  241. enumerator.Dispose();
  242. enumerator = null;
  243. }
  244. base.Dispose();
  245. }
  246. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  247. {
  248. switch (state)
  249. {
  250. case AsyncIteratorState.Allocated:
  251. enumerator = source.GetEnumerator();
  252. index = -1;
  253. state = AsyncIteratorState.Iterating;
  254. goto case AsyncIteratorState.Iterating;
  255. case AsyncIteratorState.Iterating:
  256. if (await enumerator.MoveNext(cancellationToken)
  257. .ConfigureAwait(false))
  258. {
  259. var item = enumerator.Current;
  260. checked
  261. {
  262. index++;
  263. }
  264. if (!predicate(item, index))
  265. {
  266. break;
  267. }
  268. current = item;
  269. return true;
  270. }
  271. break;
  272. }
  273. Dispose();
  274. return false;
  275. }
  276. }
  277. }
  278. }