Skip.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TSource> Skip<TSource>(this IAsyncEnumerable<TSource> source, int count)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (count < 0)
  18. throw new ArgumentOutOfRangeException(nameof(count));
  19. return Create(() =>
  20. {
  21. var e = source.GetEnumerator();
  22. var n = count;
  23. var cts = new CancellationTokenDisposable();
  24. var d = Disposable.Create(cts, e);
  25. var f = default(Func<CancellationToken, Task<bool>>);
  26. f = async ct =>
  27. {
  28. var moveNext = await e.MoveNext(ct)
  29. .ConfigureAwait(false);
  30. if (n == 0)
  31. {
  32. return moveNext;
  33. }
  34. --n;
  35. if (!moveNext)
  36. {
  37. return false;
  38. }
  39. return await f(ct)
  40. .ConfigureAwait(false);
  41. };
  42. return Create(
  43. ct => f(cts.Token),
  44. () => e.Current,
  45. d.Dispose,
  46. e
  47. );
  48. });
  49. }
  50. public static IAsyncEnumerable<TSource> SkipLast<TSource>(this IAsyncEnumerable<TSource> source, int count)
  51. {
  52. if (source == null)
  53. throw new ArgumentNullException(nameof(source));
  54. if (count < 0)
  55. throw new ArgumentOutOfRangeException(nameof(count));
  56. return Create(() =>
  57. {
  58. var e = source.GetEnumerator();
  59. var cts = new CancellationTokenDisposable();
  60. var d = Disposable.Create(cts, e);
  61. var q = new Queue<TSource>();
  62. var current = default(TSource);
  63. var f = default(Func<CancellationToken, Task<bool>>);
  64. f = async ct =>
  65. {
  66. if (await e.MoveNext(ct)
  67. .ConfigureAwait(false))
  68. {
  69. var item = e.Current;
  70. q.Enqueue(item);
  71. if (q.Count > count)
  72. {
  73. current = q.Dequeue();
  74. return true;
  75. }
  76. return await f(ct)
  77. .ConfigureAwait(false);
  78. }
  79. return false;
  80. };
  81. return Create(
  82. f,
  83. () => current,
  84. d.Dispose,
  85. e
  86. );
  87. });
  88. }
  89. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  90. {
  91. if (source == null)
  92. throw new ArgumentNullException(nameof(source));
  93. if (predicate == null)
  94. throw new ArgumentNullException(nameof(predicate));
  95. return Create(() =>
  96. {
  97. var e = source.GetEnumerator();
  98. var skipping = true;
  99. var cts = new CancellationTokenDisposable();
  100. var d = Disposable.Create(cts, e);
  101. var f = default(Func<CancellationToken, Task<bool>>);
  102. f = async ct =>
  103. {
  104. if (skipping)
  105. {
  106. if (await e.MoveNext(ct)
  107. .ConfigureAwait(false))
  108. {
  109. if (predicate(e.Current))
  110. return await f(ct)
  111. .ConfigureAwait(false);
  112. skipping = false;
  113. return true;
  114. }
  115. return false;
  116. }
  117. return await e.MoveNext(ct)
  118. .ConfigureAwait(false);
  119. };
  120. return Create(
  121. f,
  122. () => e.Current,
  123. d.Dispose,
  124. e
  125. );
  126. });
  127. }
  128. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  129. {
  130. if (source == null)
  131. throw new ArgumentNullException(nameof(source));
  132. if (predicate == null)
  133. throw new ArgumentNullException(nameof(predicate));
  134. return Create(() =>
  135. {
  136. var e = source.GetEnumerator();
  137. var skipping = true;
  138. var index = 0;
  139. var cts = new CancellationTokenDisposable();
  140. var d = Disposable.Create(cts, e);
  141. var f = default(Func<CancellationToken, Task<bool>>);
  142. f = async ct =>
  143. {
  144. if (skipping)
  145. {
  146. if (await e.MoveNext(ct)
  147. .ConfigureAwait(false))
  148. {
  149. if (predicate(e.Current, checked(index++)))
  150. return await f(ct)
  151. .ConfigureAwait(false);
  152. skipping = false;
  153. return true;
  154. }
  155. return false;
  156. }
  157. return await e.MoveNext(ct)
  158. .ConfigureAwait(false);
  159. };
  160. return Create(
  161. f,
  162. () => e.Current,
  163. d.Dispose,
  164. e
  165. );
  166. });
  167. }
  168. }
  169. }