Skip.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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> Skip<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 source if not actually skipping, but only if it's a type from here, to avoid
  21. // issues if collections are used as keys or otherwise must not be aliased.
  22. if (source is AsyncIterator<TSource>)
  23. {
  24. return source;
  25. }
  26. count = 0;
  27. }
  28. return new SkipAsyncIterator<TSource>(source, count);
  29. }
  30. public static IAsyncEnumerable<TSource> SkipLast<TSource>(this IAsyncEnumerable<TSource> source, int count)
  31. {
  32. if (source == null)
  33. throw new ArgumentNullException(nameof(source));
  34. if (count <= 0)
  35. {
  36. // Return source if not actually skipping, but only if it's a type from here, to avoid
  37. // issues if collections are used as keys or otherwise must not be aliased.
  38. if (source is AsyncIterator<TSource>)
  39. {
  40. return source;
  41. }
  42. count = 0;
  43. }
  44. return new SkipLastAsyncIterator<TSource>(source, count);
  45. }
  46. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  47. {
  48. if (source == null)
  49. throw new ArgumentNullException(nameof(source));
  50. if (predicate == null)
  51. throw new ArgumentNullException(nameof(predicate));
  52. return new SkipWhileAsyncIterator<TSource>(source, predicate);
  53. }
  54. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  55. {
  56. if (source == null)
  57. throw new ArgumentNullException(nameof(source));
  58. if (predicate == null)
  59. throw new ArgumentNullException(nameof(predicate));
  60. return new SkipWhileWithIndexAsyncIterator<TSource>(source, predicate);
  61. }
  62. private sealed class SkipAsyncIterator<TSource> : AsyncIterator<TSource>
  63. {
  64. private readonly int count;
  65. private readonly IAsyncEnumerable<TSource> source;
  66. private int currentCount;
  67. private IAsyncEnumerator<TSource> enumerator;
  68. public SkipAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  69. {
  70. this.source = source;
  71. this.count = count;
  72. currentCount = count;
  73. }
  74. public override AsyncIterator<TSource> Clone()
  75. {
  76. return new SkipAsyncIterator<TSource>(source, count);
  77. }
  78. public override void Dispose()
  79. {
  80. if (enumerator != null)
  81. {
  82. enumerator.Dispose();
  83. enumerator = null;
  84. }
  85. base.Dispose();
  86. }
  87. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  88. {
  89. switch (state)
  90. {
  91. case AsyncIteratorState.Allocated:
  92. enumerator = source.GetEnumerator();
  93. // skip elements as requested
  94. while (currentCount > 0 && await enumerator.MoveNext(cancellationToken)
  95. .ConfigureAwait(false))
  96. {
  97. currentCount--;
  98. }
  99. if (currentCount <= 0)
  100. {
  101. state = AsyncIteratorState.Iterating;
  102. goto case AsyncIteratorState.Iterating;
  103. }
  104. break;
  105. case AsyncIteratorState.Iterating:
  106. if (await enumerator.MoveNext(cancellationToken)
  107. .ConfigureAwait(false))
  108. {
  109. current = enumerator.Current;
  110. return true;
  111. }
  112. break;
  113. }
  114. Dispose();
  115. return false;
  116. }
  117. }
  118. private sealed class SkipLastAsyncIterator<TSource> : AsyncIterator<TSource>
  119. {
  120. private readonly int count;
  121. private readonly IAsyncEnumerable<TSource> source;
  122. private IAsyncEnumerator<TSource> enumerator;
  123. private Queue<TSource> queue;
  124. public SkipLastAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  125. {
  126. this.source = source;
  127. this.count = count;
  128. }
  129. public override AsyncIterator<TSource> Clone()
  130. {
  131. return new SkipLastAsyncIterator<TSource>(source, count);
  132. }
  133. public override void Dispose()
  134. {
  135. if (enumerator != null)
  136. {
  137. enumerator.Dispose();
  138. enumerator = null;
  139. }
  140. queue = null; // release the memory
  141. base.Dispose();
  142. }
  143. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  144. {
  145. switch (state)
  146. {
  147. case AsyncIteratorState.Allocated:
  148. enumerator = source.GetEnumerator();
  149. queue = new Queue<TSource>();
  150. state = AsyncIteratorState.Iterating;
  151. goto case AsyncIteratorState.Iterating;
  152. case AsyncIteratorState.Iterating:
  153. if (await enumerator.MoveNext(cancellationToken)
  154. .ConfigureAwait(false))
  155. {
  156. var item = enumerator.Current;
  157. queue.Enqueue(item);
  158. if (queue.Count > count)
  159. {
  160. current = queue.Dequeue();
  161. return true;
  162. }
  163. goto case AsyncIteratorState.Iterating; // loop until either the await is false or we return an item
  164. }
  165. break;
  166. }
  167. Dispose();
  168. return false;
  169. }
  170. }
  171. private sealed class SkipWhileAsyncIterator<TSource> : AsyncIterator<TSource>
  172. {
  173. private readonly Func<TSource, bool> predicate;
  174. private readonly IAsyncEnumerable<TSource> source;
  175. private bool doMoveNext;
  176. private IAsyncEnumerator<TSource> enumerator;
  177. public SkipWhileAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  178. {
  179. Debug.Assert(predicate != null);
  180. Debug.Assert(source != null);
  181. this.source = source;
  182. this.predicate = predicate;
  183. }
  184. public override AsyncIterator<TSource> Clone()
  185. {
  186. return new SkipWhileAsyncIterator<TSource>(source, predicate);
  187. }
  188. public override void Dispose()
  189. {
  190. if (enumerator != null)
  191. {
  192. enumerator.Dispose();
  193. enumerator = null;
  194. }
  195. base.Dispose();
  196. }
  197. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  198. {
  199. switch (state)
  200. {
  201. case AsyncIteratorState.Allocated:
  202. enumerator = source.GetEnumerator();
  203. // skip elements as requested
  204. while (await enumerator.MoveNext(cancellationToken)
  205. .ConfigureAwait(false))
  206. {
  207. var element = enumerator.Current;
  208. if (!predicate(element))
  209. {
  210. doMoveNext = false;
  211. state = AsyncIteratorState.Iterating;
  212. goto case AsyncIteratorState.Iterating;
  213. }
  214. }
  215. break;
  216. case AsyncIteratorState.Iterating:
  217. if (doMoveNext && await enumerator.MoveNext(cancellationToken)
  218. .ConfigureAwait(false))
  219. {
  220. current = enumerator.Current;
  221. return true;
  222. }
  223. if (!doMoveNext)
  224. {
  225. current = enumerator.Current;
  226. doMoveNext = true;
  227. return true;
  228. }
  229. break;
  230. }
  231. Dispose();
  232. return false;
  233. }
  234. }
  235. private sealed class SkipWhileWithIndexAsyncIterator<TSource> : AsyncIterator<TSource>
  236. {
  237. private readonly Func<TSource, int, bool> predicate;
  238. private readonly IAsyncEnumerable<TSource> source;
  239. private bool doMoveNext;
  240. private IAsyncEnumerator<TSource> enumerator;
  241. private int index;
  242. public SkipWhileWithIndexAsyncIterator(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 SkipWhileWithIndexAsyncIterator<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. // skip elements as requested
  270. while (await enumerator.MoveNext(cancellationToken)
  271. .ConfigureAwait(false))
  272. {
  273. checked
  274. {
  275. index++;
  276. }
  277. var element = enumerator.Current;
  278. if (!predicate(element, index))
  279. {
  280. doMoveNext = false;
  281. state = AsyncIteratorState.Iterating;
  282. goto case AsyncIteratorState.Iterating;
  283. }
  284. }
  285. break;
  286. case AsyncIteratorState.Iterating:
  287. if (doMoveNext && await enumerator.MoveNext(cancellationToken)
  288. .ConfigureAwait(false))
  289. {
  290. current = enumerator.Current;
  291. return true;
  292. }
  293. if (!doMoveNext)
  294. {
  295. current = enumerator.Current;
  296. doMoveNext = true;
  297. return true;
  298. }
  299. break;
  300. }
  301. Dispose();
  302. return false;
  303. }
  304. }
  305. }
  306. }