Skip.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. throw new ArgumentNullException(nameof(source));
  16. if (count <= 0)
  17. {
  18. // Return source if not actually skipping, but only if it's a type from here, to avoid
  19. // issues if collections are used as keys or otherwise must not be aliased.
  20. if (source is AsyncIterator<TSource>)
  21. {
  22. return source;
  23. }
  24. count = 0;
  25. }
  26. return new SkipAsyncIterator<TSource>(source, count);
  27. }
  28. public static IAsyncEnumerable<TSource> SkipLast<TSource>(this IAsyncEnumerable<TSource> source, int count)
  29. {
  30. if (source == null)
  31. throw new ArgumentNullException(nameof(source));
  32. if (count <= 0)
  33. {
  34. // Return source if not actually skipping, but only if it's a type from here, to avoid
  35. // issues if collections are used as keys or otherwise must not be aliased.
  36. if (source is AsyncIterator<TSource>)
  37. {
  38. return source;
  39. }
  40. count = 0;
  41. }
  42. return new SkipLastAsyncIterator<TSource>(source, count);
  43. }
  44. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  45. {
  46. if (source == null)
  47. throw new ArgumentNullException(nameof(source));
  48. if (predicate == null)
  49. throw new ArgumentNullException(nameof(predicate));
  50. return new SkipWhileAsyncIterator<TSource>(source, predicate);
  51. }
  52. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  53. {
  54. if (source == null)
  55. throw new ArgumentNullException(nameof(source));
  56. if (predicate == null)
  57. throw new ArgumentNullException(nameof(predicate));
  58. return new SkipWhileWithIndexAsyncIterator<TSource>(source, predicate);
  59. }
  60. private sealed class SkipAsyncIterator<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 SkipAsyncIterator(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 SkipAsyncIterator<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. // skip elements as requested
  93. while (currentCount > 0 && await enumerator.MoveNext(cancellationToken)
  94. .ConfigureAwait(false))
  95. {
  96. currentCount--;
  97. }
  98. if (currentCount <= 0)
  99. {
  100. state = AsyncIteratorState.Iterating;
  101. goto case AsyncIteratorState.Iterating;
  102. }
  103. break;
  104. case AsyncIteratorState.Iterating:
  105. if (await enumerator.MoveNext(cancellationToken)
  106. .ConfigureAwait(false))
  107. {
  108. current = enumerator.Current;
  109. return true;
  110. }
  111. break;
  112. }
  113. Dispose();
  114. return false;
  115. }
  116. }
  117. private sealed class SkipLastAsyncIterator<TSource> : AsyncIterator<TSource>
  118. {
  119. private readonly int count;
  120. private readonly IAsyncEnumerable<TSource> source;
  121. private IAsyncEnumerator<TSource> enumerator;
  122. private Queue<TSource> queue;
  123. public SkipLastAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  124. {
  125. Debug.Assert(source != null);
  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. }