SkipWhile.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static IUniTaskAsyncEnumerable<TSource> SkipWhile<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  12. return new SkipWhile<TSource>(source, predicate);
  13. }
  14. public static IUniTaskAsyncEnumerable<TSource> SkipWhile<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, Boolean> predicate)
  15. {
  16. Error.ThrowArgumentNullException(source, nameof(source));
  17. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  18. return new SkipWhileInt<TSource>(source, predicate);
  19. }
  20. public static IUniTaskAsyncEnumerable<TSource> SkipWhileAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate)
  21. {
  22. Error.ThrowArgumentNullException(source, nameof(source));
  23. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  24. return new SkipWhileAwait<TSource>(source, predicate);
  25. }
  26. public static IUniTaskAsyncEnumerable<TSource> SkipWhileAwait<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask<Boolean>> predicate)
  27. {
  28. Error.ThrowArgumentNullException(source, nameof(source));
  29. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  30. return new SkipWhileIntAwait<TSource>(source, predicate);
  31. }
  32. public static IUniTaskAsyncEnumerable<TSource> SkipWhileAwaitWithCancellation<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate)
  33. {
  34. Error.ThrowArgumentNullException(source, nameof(source));
  35. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  36. return new SkipWhileAwaitWithCancellation<TSource>(source, predicate);
  37. }
  38. public static IUniTaskAsyncEnumerable<TSource> SkipWhileAwaitWithCancellation<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask<Boolean>> predicate)
  39. {
  40. Error.ThrowArgumentNullException(source, nameof(source));
  41. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  42. return new SkipWhileIntAwaitWithCancellation<TSource>(source, predicate);
  43. }
  44. }
  45. internal sealed class SkipWhile<TSource> : IUniTaskAsyncEnumerable<TSource>
  46. {
  47. readonly IUniTaskAsyncEnumerable<TSource> source;
  48. readonly Func<TSource, bool> predicate;
  49. public SkipWhile(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  50. {
  51. this.source = source;
  52. this.predicate = predicate;
  53. }
  54. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  55. {
  56. return new _SkipWhile(source, predicate, cancellationToken);
  57. }
  58. class _SkipWhile : AsyncEnumeratorBase<TSource, TSource>
  59. {
  60. Func<TSource, bool> predicate;
  61. public _SkipWhile(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  62. : base(source, cancellationToken)
  63. {
  64. this.predicate = predicate;
  65. }
  66. protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
  67. {
  68. if (sourceHasCurrent)
  69. {
  70. if (predicate == null || !predicate(SourceCurrent))
  71. {
  72. predicate = null;
  73. Current = SourceCurrent;
  74. result = true;
  75. return true;
  76. }
  77. else
  78. {
  79. result = default;
  80. return false;
  81. }
  82. }
  83. result = false;
  84. return true;
  85. }
  86. }
  87. }
  88. internal sealed class SkipWhileInt<TSource> : IUniTaskAsyncEnumerable<TSource>
  89. {
  90. readonly IUniTaskAsyncEnumerable<TSource> source;
  91. readonly Func<TSource, int, bool> predicate;
  92. public SkipWhileInt(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  93. {
  94. this.source = source;
  95. this.predicate = predicate;
  96. }
  97. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  98. {
  99. return new _SkipWhileInt(source, predicate, cancellationToken);
  100. }
  101. class _SkipWhileInt : AsyncEnumeratorBase<TSource, TSource>
  102. {
  103. Func<TSource, int, bool> predicate;
  104. int index;
  105. public _SkipWhileInt(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate, CancellationToken cancellationToken)
  106. : base(source, cancellationToken)
  107. {
  108. this.predicate = predicate;
  109. }
  110. protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
  111. {
  112. if (sourceHasCurrent)
  113. {
  114. if (predicate == null || !predicate(SourceCurrent, checked(index++)))
  115. {
  116. predicate = null;
  117. Current = SourceCurrent;
  118. result = true;
  119. return true;
  120. }
  121. else
  122. {
  123. result = default;
  124. return false;
  125. }
  126. }
  127. result = false;
  128. return true;
  129. }
  130. }
  131. }
  132. internal sealed class SkipWhileAwait<TSource> : IUniTaskAsyncEnumerable<TSource>
  133. {
  134. readonly IUniTaskAsyncEnumerable<TSource> source;
  135. readonly Func<TSource, UniTask<bool>> predicate;
  136. public SkipWhileAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<bool>> predicate)
  137. {
  138. this.source = source;
  139. this.predicate = predicate;
  140. }
  141. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  142. {
  143. return new _SkipWhileAwait(source, predicate, cancellationToken);
  144. }
  145. class _SkipWhileAwait : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
  146. {
  147. Func<TSource, UniTask<bool>> predicate;
  148. public _SkipWhileAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<bool>> predicate, CancellationToken cancellationToken)
  149. : base(source, cancellationToken)
  150. {
  151. this.predicate = predicate;
  152. }
  153. protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
  154. {
  155. if (predicate == null)
  156. {
  157. return CompletedTasks.False;
  158. }
  159. return predicate(sourceCurrent);
  160. }
  161. protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
  162. {
  163. if (!awaitResult)
  164. {
  165. predicate = null;
  166. Current = SourceCurrent;
  167. terminateIteration= false;
  168. return true;
  169. }
  170. else
  171. {
  172. terminateIteration= false;
  173. return false;
  174. }
  175. }
  176. }
  177. }
  178. internal sealed class SkipWhileIntAwait<TSource> : IUniTaskAsyncEnumerable<TSource>
  179. {
  180. readonly IUniTaskAsyncEnumerable<TSource> source;
  181. readonly Func<TSource, int, UniTask<bool>> predicate;
  182. public SkipWhileIntAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<bool>> predicate)
  183. {
  184. this.source = source;
  185. this.predicate = predicate;
  186. }
  187. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  188. {
  189. return new _SkipWhileIntAwait(source, predicate, cancellationToken);
  190. }
  191. class _SkipWhileIntAwait : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
  192. {
  193. Func<TSource, int, UniTask<bool>> predicate;
  194. int index;
  195. public _SkipWhileIntAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<bool>> predicate, CancellationToken cancellationToken)
  196. : base(source, cancellationToken)
  197. {
  198. this.predicate = predicate;
  199. }
  200. protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
  201. {
  202. if (predicate == null)
  203. {
  204. return CompletedTasks.False;
  205. }
  206. return predicate(sourceCurrent, checked(index++));
  207. }
  208. protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
  209. {
  210. terminateIteration= false;
  211. if (!awaitResult)
  212. {
  213. predicate = null;
  214. Current = SourceCurrent;
  215. return true;
  216. }
  217. else
  218. {
  219. return false;
  220. }
  221. }
  222. }
  223. }
  224. internal sealed class SkipWhileAwaitWithCancellation<TSource> : IUniTaskAsyncEnumerable<TSource>
  225. {
  226. readonly IUniTaskAsyncEnumerable<TSource> source;
  227. readonly Func<TSource, CancellationToken, UniTask<bool>> predicate;
  228. public SkipWhileAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<bool>> predicate)
  229. {
  230. this.source = source;
  231. this.predicate = predicate;
  232. }
  233. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  234. {
  235. return new _SkipWhileAwaitWithCancellation(source, predicate, cancellationToken);
  236. }
  237. class _SkipWhileAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
  238. {
  239. Func<TSource, CancellationToken, UniTask<bool>> predicate;
  240. public _SkipWhileAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<bool>> predicate, CancellationToken cancellationToken)
  241. : base(source, cancellationToken)
  242. {
  243. this.predicate = predicate;
  244. }
  245. protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
  246. {
  247. if (predicate == null)
  248. {
  249. return CompletedTasks.False;
  250. }
  251. return predicate(sourceCurrent, cancellationToken);
  252. }
  253. protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
  254. {
  255. terminateIteration= false;
  256. if (!awaitResult)
  257. {
  258. predicate = null;
  259. Current = SourceCurrent;
  260. return true;
  261. }
  262. else
  263. {
  264. return false;
  265. }
  266. }
  267. }
  268. }
  269. internal sealed class SkipWhileIntAwaitWithCancellation<TSource> : IUniTaskAsyncEnumerable<TSource>
  270. {
  271. readonly IUniTaskAsyncEnumerable<TSource> source;
  272. readonly Func<TSource, int, CancellationToken, UniTask<bool>> predicate;
  273. public SkipWhileIntAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<bool>> predicate)
  274. {
  275. this.source = source;
  276. this.predicate = predicate;
  277. }
  278. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  279. {
  280. return new _SkipWhileIntAwaitWithCancellation(source, predicate, cancellationToken);
  281. }
  282. class _SkipWhileIntAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
  283. {
  284. Func<TSource, int, CancellationToken, UniTask<bool>> predicate;
  285. int index;
  286. public _SkipWhileIntAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<bool>> predicate, CancellationToken cancellationToken)
  287. : base(source, cancellationToken)
  288. {
  289. this.predicate = predicate;
  290. }
  291. protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
  292. {
  293. if (predicate == null)
  294. {
  295. return CompletedTasks.False;
  296. }
  297. return predicate(sourceCurrent, checked(index++), cancellationToken);
  298. }
  299. protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
  300. {
  301. terminateIteration= false;
  302. if (!awaitResult)
  303. {
  304. predicate = null;
  305. Current = SourceCurrent;
  306. return true;
  307. }
  308. else
  309. {
  310. return false;
  311. }
  312. }
  313. }
  314. }
  315. }