Skip.cs 7.2 KB

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