Take.cs 12 KB

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