SkipWhile.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (predicate == null)
  16. throw new ArgumentNullException(nameof(predicate));
  17. return new SkipWhileAsyncIterator<TSource>(source, predicate);
  18. }
  19. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (predicate == null)
  24. throw new ArgumentNullException(nameof(predicate));
  25. return new SkipWhileWithIndexAsyncIterator<TSource>(source, predicate);
  26. }
  27. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (predicate == null)
  32. throw new ArgumentNullException(nameof(predicate));
  33. return new SkipWhileAsyncIteratorWithTask<TSource>(source, predicate);
  34. }
  35. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Task<bool>> predicate)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. if (predicate == null)
  40. throw new ArgumentNullException(nameof(predicate));
  41. return new SkipWhileWithIndexAsyncIteratorWithTask<TSource>(source, predicate);
  42. }
  43. private sealed class SkipWhileAsyncIterator<TSource> : AsyncIterator<TSource>
  44. {
  45. private readonly Func<TSource, bool> predicate;
  46. private readonly IAsyncEnumerable<TSource> source;
  47. private bool doMoveNext;
  48. private IAsyncEnumerator<TSource> enumerator;
  49. public SkipWhileAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  50. {
  51. Debug.Assert(predicate != null);
  52. Debug.Assert(source != null);
  53. this.source = source;
  54. this.predicate = predicate;
  55. }
  56. public override AsyncIterator<TSource> Clone()
  57. {
  58. return new SkipWhileAsyncIterator<TSource>(source, predicate);
  59. }
  60. public override async Task DisposeAsync()
  61. {
  62. if (enumerator != null)
  63. {
  64. await enumerator.DisposeAsync().ConfigureAwait(false);
  65. enumerator = null;
  66. }
  67. await base.DisposeAsync().ConfigureAwait(false);
  68. }
  69. protected override async Task<bool> MoveNextCore()
  70. {
  71. switch (state)
  72. {
  73. case AsyncIteratorState.Allocated:
  74. enumerator = source.GetAsyncEnumerator();
  75. // skip elements as requested
  76. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  77. {
  78. var element = enumerator.Current;
  79. if (!predicate(element))
  80. {
  81. doMoveNext = false;
  82. state = AsyncIteratorState.Iterating;
  83. goto case AsyncIteratorState.Iterating;
  84. }
  85. }
  86. break;
  87. case AsyncIteratorState.Iterating:
  88. if (doMoveNext && await enumerator.MoveNextAsync().ConfigureAwait(false))
  89. {
  90. current = enumerator.Current;
  91. return true;
  92. }
  93. if (!doMoveNext)
  94. {
  95. current = enumerator.Current;
  96. doMoveNext = true;
  97. return true;
  98. }
  99. break;
  100. }
  101. await DisposeAsync().ConfigureAwait(false);
  102. return false;
  103. }
  104. }
  105. private sealed class SkipWhileWithIndexAsyncIterator<TSource> : AsyncIterator<TSource>
  106. {
  107. private readonly Func<TSource, int, bool> predicate;
  108. private readonly IAsyncEnumerable<TSource> source;
  109. private bool doMoveNext;
  110. private IAsyncEnumerator<TSource> enumerator;
  111. private int index;
  112. public SkipWhileWithIndexAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  113. {
  114. Debug.Assert(predicate != null);
  115. Debug.Assert(source != null);
  116. this.source = source;
  117. this.predicate = predicate;
  118. }
  119. public override AsyncIterator<TSource> Clone()
  120. {
  121. return new SkipWhileWithIndexAsyncIterator<TSource>(source, predicate);
  122. }
  123. public override async Task DisposeAsync()
  124. {
  125. if (enumerator != null)
  126. {
  127. await enumerator.DisposeAsync().ConfigureAwait(false);
  128. enumerator = null;
  129. }
  130. await base.DisposeAsync().ConfigureAwait(false);
  131. }
  132. protected override async Task<bool> MoveNextCore()
  133. {
  134. switch (state)
  135. {
  136. case AsyncIteratorState.Allocated:
  137. enumerator = source.GetAsyncEnumerator();
  138. index = -1;
  139. // skip elements as requested
  140. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  141. {
  142. checked
  143. {
  144. index++;
  145. }
  146. var element = enumerator.Current;
  147. if (!predicate(element, index))
  148. {
  149. doMoveNext = false;
  150. state = AsyncIteratorState.Iterating;
  151. goto case AsyncIteratorState.Iterating;
  152. }
  153. }
  154. break;
  155. case AsyncIteratorState.Iterating:
  156. if (doMoveNext && await enumerator.MoveNextAsync().ConfigureAwait(false))
  157. {
  158. current = enumerator.Current;
  159. return true;
  160. }
  161. if (!doMoveNext)
  162. {
  163. current = enumerator.Current;
  164. doMoveNext = true;
  165. return true;
  166. }
  167. break;
  168. }
  169. await DisposeAsync().ConfigureAwait(false);
  170. return false;
  171. }
  172. }
  173. private sealed class SkipWhileAsyncIteratorWithTask<TSource> : AsyncIterator<TSource>
  174. {
  175. private readonly Func<TSource, Task<bool>> predicate;
  176. private readonly IAsyncEnumerable<TSource> source;
  177. private bool doMoveNext;
  178. private IAsyncEnumerator<TSource> enumerator;
  179. public SkipWhileAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  180. {
  181. Debug.Assert(predicate != null);
  182. Debug.Assert(source != null);
  183. this.source = source;
  184. this.predicate = predicate;
  185. }
  186. public override AsyncIterator<TSource> Clone()
  187. {
  188. return new SkipWhileAsyncIteratorWithTask<TSource>(source, predicate);
  189. }
  190. public override async Task DisposeAsync()
  191. {
  192. if (enumerator != null)
  193. {
  194. await enumerator.DisposeAsync().ConfigureAwait(false);
  195. enumerator = null;
  196. }
  197. await base.DisposeAsync().ConfigureAwait(false);
  198. }
  199. protected override async Task<bool> MoveNextCore()
  200. {
  201. switch (state)
  202. {
  203. case AsyncIteratorState.Allocated:
  204. enumerator = source.GetAsyncEnumerator();
  205. // skip elements as requested
  206. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  207. {
  208. var element = enumerator.Current;
  209. if (!await predicate(element).ConfigureAwait(false))
  210. {
  211. doMoveNext = false;
  212. state = AsyncIteratorState.Iterating;
  213. goto case AsyncIteratorState.Iterating;
  214. }
  215. }
  216. break;
  217. case AsyncIteratorState.Iterating:
  218. if (doMoveNext && await enumerator.MoveNextAsync().ConfigureAwait(false))
  219. {
  220. current = enumerator.Current;
  221. return true;
  222. }
  223. if (!doMoveNext)
  224. {
  225. current = enumerator.Current;
  226. doMoveNext = true;
  227. return true;
  228. }
  229. break;
  230. }
  231. await DisposeAsync().ConfigureAwait(false);
  232. return false;
  233. }
  234. }
  235. private sealed class SkipWhileWithIndexAsyncIteratorWithTask<TSource> : AsyncIterator<TSource>
  236. {
  237. private readonly Func<TSource, int, Task<bool>> predicate;
  238. private readonly IAsyncEnumerable<TSource> source;
  239. private bool doMoveNext;
  240. private IAsyncEnumerator<TSource> enumerator;
  241. private int index;
  242. public SkipWhileWithIndexAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, int, Task<bool>> predicate)
  243. {
  244. Debug.Assert(predicate != null);
  245. Debug.Assert(source != null);
  246. this.source = source;
  247. this.predicate = predicate;
  248. }
  249. public override AsyncIterator<TSource> Clone()
  250. {
  251. return new SkipWhileWithIndexAsyncIteratorWithTask<TSource>(source, predicate);
  252. }
  253. public override async Task DisposeAsync()
  254. {
  255. if (enumerator != null)
  256. {
  257. await enumerator.DisposeAsync().ConfigureAwait(false);
  258. enumerator = null;
  259. }
  260. await base.DisposeAsync().ConfigureAwait(false);
  261. }
  262. protected override async Task<bool> MoveNextCore()
  263. {
  264. switch (state)
  265. {
  266. case AsyncIteratorState.Allocated:
  267. enumerator = source.GetAsyncEnumerator();
  268. index = -1;
  269. // skip elements as requested
  270. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  271. {
  272. checked
  273. {
  274. index++;
  275. }
  276. var element = enumerator.Current;
  277. if (!await predicate(element, index).ConfigureAwait(false))
  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.MoveNextAsync().ConfigureAwait(false))
  287. {
  288. current = enumerator.Current;
  289. return true;
  290. }
  291. if (!doMoveNext)
  292. {
  293. current = enumerator.Current;
  294. doMoveNext = true;
  295. return true;
  296. }
  297. break;
  298. }
  299. await DisposeAsync().ConfigureAwait(false);
  300. return false;
  301. }
  302. }
  303. }
  304. }