Skip.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. while (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. }
  164. break;
  165. }
  166. Dispose();
  167. return false;
  168. }
  169. }
  170. private sealed class SkipWhileAsyncIterator<TSource> : AsyncIterator<TSource>
  171. {
  172. private readonly Func<TSource, bool> predicate;
  173. private readonly IAsyncEnumerable<TSource> source;
  174. private bool doMoveNext;
  175. private IAsyncEnumerator<TSource> enumerator;
  176. public SkipWhileAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  177. {
  178. Debug.Assert(predicate != null);
  179. Debug.Assert(source != null);
  180. this.source = source;
  181. this.predicate = predicate;
  182. }
  183. public override AsyncIterator<TSource> Clone()
  184. {
  185. return new SkipWhileAsyncIterator<TSource>(source, predicate);
  186. }
  187. public override void Dispose()
  188. {
  189. if (enumerator != null)
  190. {
  191. enumerator.Dispose();
  192. enumerator = null;
  193. }
  194. base.Dispose();
  195. }
  196. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  197. {
  198. switch (state)
  199. {
  200. case AsyncIteratorState.Allocated:
  201. enumerator = source.GetEnumerator();
  202. // skip elements as requested
  203. while (await enumerator.MoveNext(cancellationToken)
  204. .ConfigureAwait(false))
  205. {
  206. var element = enumerator.Current;
  207. if (!predicate(element))
  208. {
  209. doMoveNext = false;
  210. state = AsyncIteratorState.Iterating;
  211. goto case AsyncIteratorState.Iterating;
  212. }
  213. }
  214. break;
  215. case AsyncIteratorState.Iterating:
  216. if (doMoveNext && await enumerator.MoveNext(cancellationToken)
  217. .ConfigureAwait(false))
  218. {
  219. current = enumerator.Current;
  220. return true;
  221. }
  222. if (!doMoveNext)
  223. {
  224. current = enumerator.Current;
  225. doMoveNext = true;
  226. return true;
  227. }
  228. break;
  229. }
  230. Dispose();
  231. return false;
  232. }
  233. }
  234. private sealed class SkipWhileWithIndexAsyncIterator<TSource> : AsyncIterator<TSource>
  235. {
  236. private readonly Func<TSource, int, bool> predicate;
  237. private readonly IAsyncEnumerable<TSource> source;
  238. private bool doMoveNext;
  239. private IAsyncEnumerator<TSource> enumerator;
  240. private int index;
  241. public SkipWhileWithIndexAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  242. {
  243. Debug.Assert(predicate != null);
  244. Debug.Assert(source != null);
  245. this.source = source;
  246. this.predicate = predicate;
  247. }
  248. public override AsyncIterator<TSource> Clone()
  249. {
  250. return new SkipWhileWithIndexAsyncIterator<TSource>(source, predicate);
  251. }
  252. public override void Dispose()
  253. {
  254. if (enumerator != null)
  255. {
  256. enumerator.Dispose();
  257. enumerator = null;
  258. }
  259. base.Dispose();
  260. }
  261. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  262. {
  263. switch (state)
  264. {
  265. case AsyncIteratorState.Allocated:
  266. enumerator = source.GetEnumerator();
  267. index = -1;
  268. // skip elements as requested
  269. while (await enumerator.MoveNext(cancellationToken)
  270. .ConfigureAwait(false))
  271. {
  272. checked
  273. {
  274. index++;
  275. }
  276. var element = enumerator.Current;
  277. if (!predicate(element, index))
  278. {
  279. doMoveNext = false;
  280. state = AsyncIteratorState.Iterating;
  281. goto case AsyncIteratorState.Iterating;
  282. }
  283. }
  284. break;
  285. case AsyncIteratorState.Iterating:
  286. if (doMoveNext && await enumerator.MoveNext(cancellationToken)
  287. .ConfigureAwait(false))
  288. {
  289. current = enumerator.Current;
  290. return true;
  291. }
  292. if (!doMoveNext)
  293. {
  294. current = enumerator.Current;
  295. doMoveNext = true;
  296. return true;
  297. }
  298. break;
  299. }
  300. Dispose();
  301. return false;
  302. }
  303. }
  304. }
  305. }