Skip.cs 13 KB

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