Take.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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> Take<TSource>(this IAsyncEnumerable<TSource> source, int count)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. if (count <= 0)
  17. {
  18. return Empty<TSource>();
  19. }
  20. return new TakeAsyncIterator<TSource>(source, count);
  21. }
  22. public static IAsyncEnumerable<TSource> TakeLast<TSource>(this IAsyncEnumerable<TSource> source, int count)
  23. {
  24. if (source == null)
  25. throw new ArgumentNullException(nameof(source));
  26. if (count <= 0)
  27. {
  28. return Empty<TSource>();
  29. }
  30. return new TakeLastAsyncIterator<TSource>(source, count);
  31. }
  32. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. if (predicate == null)
  37. throw new ArgumentNullException(nameof(predicate));
  38. return new TakeWhileAsyncIterator<TSource>(source, predicate);
  39. }
  40. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  41. {
  42. if (source == null)
  43. throw new ArgumentNullException(nameof(source));
  44. if (predicate == null)
  45. throw new ArgumentNullException(nameof(predicate));
  46. return new TakeWhileWithIndexAsyncIterator<TSource>(source, predicate);
  47. }
  48. private sealed class TakeAsyncIterator<TSource> : AsyncIterator<TSource>
  49. {
  50. private readonly int count;
  51. private readonly IAsyncEnumerable<TSource> source;
  52. private int currentCount;
  53. private IAsyncEnumerator<TSource> enumerator;
  54. public TakeAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  55. {
  56. Debug.Assert(source != null);
  57. this.source = source;
  58. this.count = count;
  59. currentCount = count;
  60. }
  61. public override AsyncIterator<TSource> Clone()
  62. {
  63. return new TakeAsyncIterator<TSource>(source, count);
  64. }
  65. public override void Dispose()
  66. {
  67. if (enumerator != null)
  68. {
  69. enumerator.Dispose();
  70. enumerator = null;
  71. }
  72. base.Dispose();
  73. }
  74. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  75. {
  76. switch (state)
  77. {
  78. case AsyncIteratorState.Allocated:
  79. enumerator = source.GetEnumerator();
  80. state = AsyncIteratorState.Iterating;
  81. goto case AsyncIteratorState.Iterating;
  82. case AsyncIteratorState.Iterating:
  83. if (currentCount > 0 && await enumerator.MoveNext(cancellationToken)
  84. .ConfigureAwait(false))
  85. {
  86. current = enumerator.Current;
  87. currentCount--;
  88. return true;
  89. }
  90. break;
  91. }
  92. Dispose();
  93. return false;
  94. }
  95. }
  96. private sealed class TakeLastAsyncIterator<TSource> : AsyncIterator<TSource>
  97. {
  98. private readonly int count;
  99. private readonly IAsyncEnumerable<TSource> source;
  100. private IAsyncEnumerator<TSource> enumerator;
  101. private bool isDone;
  102. private Queue<TSource> queue;
  103. public TakeLastAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  104. {
  105. Debug.Assert(source != null);
  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. }