Take.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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> Take<TSource>(this IAsyncEnumerable<TSource> source, int count)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (count <= 0)
  16. {
  17. return Empty<TSource>();
  18. }
  19. return new TakeAsyncIterator<TSource>(source, count);
  20. }
  21. public static IAsyncEnumerable<TSource> TakeLast<TSource>(this IAsyncEnumerable<TSource> source, int count)
  22. {
  23. if (source == null)
  24. throw new ArgumentNullException(nameof(source));
  25. if (count <= 0)
  26. {
  27. return Empty<TSource>();
  28. }
  29. return new TakeLastAsyncIterator<TSource>(source, count);
  30. }
  31. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (predicate == null)
  36. throw new ArgumentNullException(nameof(predicate));
  37. return new TakeWhileAsyncIterator<TSource>(source, predicate);
  38. }
  39. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  40. {
  41. if (source == null)
  42. throw new ArgumentNullException(nameof(source));
  43. if (predicate == null)
  44. throw new ArgumentNullException(nameof(predicate));
  45. return new TakeWhileWithIndexAsyncIterator<TSource>(source, predicate);
  46. }
  47. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  48. {
  49. if (source == null)
  50. throw new ArgumentNullException(nameof(source));
  51. if (predicate == null)
  52. throw new ArgumentNullException(nameof(predicate));
  53. return new TakeWhileAsyncIteratorWithTask<TSource>(source, predicate);
  54. }
  55. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Task<bool>> predicate)
  56. {
  57. if (source == null)
  58. throw new ArgumentNullException(nameof(source));
  59. if (predicate == null)
  60. throw new ArgumentNullException(nameof(predicate));
  61. return new TakeWhileWithIndexAsyncIteratorWithTask<TSource>(source, predicate);
  62. }
  63. private sealed class TakeAsyncIterator<TSource> : AsyncIterator<TSource>
  64. {
  65. private readonly int count;
  66. private readonly IAsyncEnumerable<TSource> source;
  67. private int currentCount;
  68. private IAsyncEnumerator<TSource> enumerator;
  69. public TakeAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  70. {
  71. Debug.Assert(source != null);
  72. this.source = source;
  73. this.count = count;
  74. currentCount = count;
  75. }
  76. public override AsyncIterator<TSource> Clone()
  77. {
  78. return new TakeAsyncIterator<TSource>(source, count);
  79. }
  80. public override async Task DisposeAsync()
  81. {
  82. if (enumerator != null)
  83. {
  84. await enumerator.DisposeAsync().ConfigureAwait(false);
  85. enumerator = null;
  86. }
  87. await base.DisposeAsync().ConfigureAwait(false);
  88. }
  89. protected override async Task<bool> MoveNextCore()
  90. {
  91. switch (state)
  92. {
  93. case AsyncIteratorState.Allocated:
  94. enumerator = source.GetAsyncEnumerator();
  95. state = AsyncIteratorState.Iterating;
  96. goto case AsyncIteratorState.Iterating;
  97. case AsyncIteratorState.Iterating:
  98. if (currentCount > 0 && await enumerator.MoveNextAsync().ConfigureAwait(false))
  99. {
  100. current = enumerator.Current;
  101. currentCount--;
  102. return true;
  103. }
  104. break;
  105. }
  106. await DisposeAsync().ConfigureAwait(false);
  107. return false;
  108. }
  109. }
  110. private sealed class TakeLastAsyncIterator<TSource> : AsyncIterator<TSource>
  111. {
  112. private readonly int count;
  113. private readonly IAsyncEnumerable<TSource> source;
  114. private IAsyncEnumerator<TSource> enumerator;
  115. private bool isDone;
  116. private Queue<TSource> queue;
  117. public TakeLastAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  118. {
  119. Debug.Assert(source != null);
  120. this.source = source;
  121. this.count = count;
  122. }
  123. public override AsyncIterator<TSource> Clone()
  124. {
  125. return new TakeLastAsyncIterator<TSource>(source, count);
  126. }
  127. public override async Task DisposeAsync()
  128. {
  129. if (enumerator != null)
  130. {
  131. await enumerator.DisposeAsync().ConfigureAwait(false);
  132. enumerator = null;
  133. }
  134. queue = null; // release the memory
  135. await base.DisposeAsync().ConfigureAwait(false);
  136. }
  137. protected override async Task<bool> MoveNextCore()
  138. {
  139. switch (state)
  140. {
  141. case AsyncIteratorState.Allocated:
  142. enumerator = source.GetAsyncEnumerator();
  143. queue = new Queue<TSource>();
  144. isDone = false;
  145. state = AsyncIteratorState.Iterating;
  146. goto case AsyncIteratorState.Iterating;
  147. case AsyncIteratorState.Iterating:
  148. while (true)
  149. {
  150. if (!isDone)
  151. {
  152. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  153. {
  154. if (count > 0)
  155. {
  156. var item = enumerator.Current;
  157. if (queue.Count >= count)
  158. {
  159. queue.Dequeue();
  160. }
  161. queue.Enqueue(item);
  162. }
  163. }
  164. else
  165. {
  166. isDone = true;
  167. // Dispose early here as we can
  168. await enumerator.DisposeAsync().ConfigureAwait(false);
  169. enumerator = null;
  170. }
  171. continue; // loop until queue is drained
  172. }
  173. if (queue.Count > 0)
  174. {
  175. current = queue.Dequeue();
  176. return true;
  177. }
  178. break; // while
  179. }
  180. break; // case
  181. }
  182. await DisposeAsync().ConfigureAwait(false);
  183. return false;
  184. }
  185. }
  186. private sealed class TakeWhileAsyncIterator<TSource> : AsyncIterator<TSource>
  187. {
  188. private readonly Func<TSource, bool> predicate;
  189. private readonly IAsyncEnumerable<TSource> source;
  190. private IAsyncEnumerator<TSource> enumerator;
  191. public TakeWhileAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  192. {
  193. Debug.Assert(predicate != null);
  194. Debug.Assert(source != null);
  195. this.source = source;
  196. this.predicate = predicate;
  197. }
  198. public override AsyncIterator<TSource> Clone()
  199. {
  200. return new TakeWhileAsyncIterator<TSource>(source, predicate);
  201. }
  202. public override async Task DisposeAsync()
  203. {
  204. if (enumerator != null)
  205. {
  206. await enumerator.DisposeAsync().ConfigureAwait(false);
  207. enumerator = null;
  208. }
  209. await base.DisposeAsync().ConfigureAwait(false);
  210. }
  211. protected override async Task<bool> MoveNextCore()
  212. {
  213. switch (state)
  214. {
  215. case AsyncIteratorState.Allocated:
  216. enumerator = source.GetAsyncEnumerator();
  217. state = AsyncIteratorState.Iterating;
  218. goto case AsyncIteratorState.Iterating;
  219. case AsyncIteratorState.Iterating:
  220. if (await enumerator.MoveNextAsync().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. await DisposeAsync().ConfigureAwait(false);
  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 async Task DisposeAsync()
  254. {
  255. if (enumerator != null)
  256. {
  257. await enumerator.DisposeAsync().ConfigureAwait(false);
  258. enumerator = null;
  259. }
  260. await base.DisposeAsync().ConfigureAwait(false);
  261. }
  262. protected override async Task<bool> MoveNextCore()
  263. {
  264. switch (state)
  265. {
  266. case AsyncIteratorState.Allocated:
  267. enumerator = source.GetAsyncEnumerator();
  268. index = -1;
  269. state = AsyncIteratorState.Iterating;
  270. goto case AsyncIteratorState.Iterating;
  271. case AsyncIteratorState.Iterating:
  272. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  273. {
  274. var item = enumerator.Current;
  275. checked
  276. {
  277. index++;
  278. }
  279. if (!predicate(item, index))
  280. {
  281. break;
  282. }
  283. current = item;
  284. return true;
  285. }
  286. break;
  287. }
  288. await DisposeAsync().ConfigureAwait(false);
  289. return false;
  290. }
  291. }
  292. private sealed class TakeWhileAsyncIteratorWithTask<TSource> : AsyncIterator<TSource>
  293. {
  294. private readonly Func<TSource, Task<bool>> predicate;
  295. private readonly IAsyncEnumerable<TSource> source;
  296. private IAsyncEnumerator<TSource> enumerator;
  297. public TakeWhileAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  298. {
  299. Debug.Assert(predicate != null);
  300. Debug.Assert(source != null);
  301. this.source = source;
  302. this.predicate = predicate;
  303. }
  304. public override AsyncIterator<TSource> Clone()
  305. {
  306. return new TakeWhileAsyncIteratorWithTask<TSource>(source, predicate);
  307. }
  308. public override async Task DisposeAsync()
  309. {
  310. if (enumerator != null)
  311. {
  312. await enumerator.DisposeAsync().ConfigureAwait(false);
  313. enumerator = null;
  314. }
  315. await base.DisposeAsync().ConfigureAwait(false);
  316. }
  317. protected override async Task<bool> MoveNextCore()
  318. {
  319. switch (state)
  320. {
  321. case AsyncIteratorState.Allocated:
  322. enumerator = source.GetAsyncEnumerator();
  323. state = AsyncIteratorState.Iterating;
  324. goto case AsyncIteratorState.Iterating;
  325. case AsyncIteratorState.Iterating:
  326. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  327. {
  328. var item = enumerator.Current;
  329. if (!await predicate(item).ConfigureAwait(false))
  330. {
  331. break;
  332. }
  333. current = item;
  334. return true;
  335. }
  336. break;
  337. }
  338. await DisposeAsync().ConfigureAwait(false);
  339. return false;
  340. }
  341. }
  342. private sealed class TakeWhileWithIndexAsyncIteratorWithTask<TSource> : AsyncIterator<TSource>
  343. {
  344. private readonly Func<TSource, int, Task<bool>> predicate;
  345. private readonly IAsyncEnumerable<TSource> source;
  346. private IAsyncEnumerator<TSource> enumerator;
  347. private int index;
  348. public TakeWhileWithIndexAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, int, Task<bool>> predicate)
  349. {
  350. Debug.Assert(predicate != null);
  351. Debug.Assert(source != null);
  352. this.source = source;
  353. this.predicate = predicate;
  354. }
  355. public override AsyncIterator<TSource> Clone()
  356. {
  357. return new TakeWhileWithIndexAsyncIteratorWithTask<TSource>(source, predicate);
  358. }
  359. public override async Task DisposeAsync()
  360. {
  361. if (enumerator != null)
  362. {
  363. await enumerator.DisposeAsync().ConfigureAwait(false);
  364. enumerator = null;
  365. }
  366. await base.DisposeAsync().ConfigureAwait(false);
  367. }
  368. protected override async Task<bool> MoveNextCore()
  369. {
  370. switch (state)
  371. {
  372. case AsyncIteratorState.Allocated:
  373. enumerator = source.GetAsyncEnumerator();
  374. index = -1;
  375. state = AsyncIteratorState.Iterating;
  376. goto case AsyncIteratorState.Iterating;
  377. case AsyncIteratorState.Iterating:
  378. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  379. {
  380. var item = enumerator.Current;
  381. checked
  382. {
  383. index++;
  384. }
  385. if (!await predicate(item, index).ConfigureAwait(false))
  386. {
  387. break;
  388. }
  389. current = item;
  390. return true;
  391. }
  392. break;
  393. }
  394. await DisposeAsync().ConfigureAwait(false);
  395. return false;
  396. }
  397. }
  398. }
  399. }