TakeWhile.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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> TakeWhile<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 TakeWhile<TSource>(source, predicate);
  13. }
  14. public static IUniTaskAsyncEnumerable<TSource> TakeWhile<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 TakeWhileInt<TSource>(source, predicate);
  19. }
  20. public static IUniTaskAsyncEnumerable<TSource> TakeWhileAwait<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 TakeWhileAwait<TSource>(source, predicate);
  25. }
  26. public static IUniTaskAsyncEnumerable<TSource> TakeWhileAwait<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 TakeWhileIntAwait<TSource>(source, predicate);
  31. }
  32. public static IUniTaskAsyncEnumerable<TSource> TakeWhileAwaitWithCancellation<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 TakeWhileAwaitWithCancellation<TSource>(source, predicate);
  37. }
  38. public static IUniTaskAsyncEnumerable<TSource> TakeWhileAwaitWithCancellation<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 TakeWhileIntAwaitWithCancellation<TSource>(source, predicate);
  43. }
  44. }
  45. internal sealed class TakeWhile<TSource> : IUniTaskAsyncEnumerable<TSource>
  46. {
  47. readonly IUniTaskAsyncEnumerable<TSource> source;
  48. readonly Func<TSource, bool> predicate;
  49. public TakeWhile(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 _TakeWhile(source, predicate, cancellationToken);
  57. }
  58. class _TakeWhile : AsyncEnumeratorBase<TSource, TSource>
  59. {
  60. Func<TSource, bool> predicate;
  61. public _TakeWhile(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(SourceCurrent))
  71. {
  72. Current = SourceCurrent;
  73. result = true;
  74. return true;
  75. }
  76. }
  77. result = false;
  78. return true;
  79. }
  80. }
  81. }
  82. internal sealed class TakeWhileInt<TSource> : IUniTaskAsyncEnumerable<TSource>
  83. {
  84. readonly IUniTaskAsyncEnumerable<TSource> source;
  85. readonly Func<TSource, int, bool> predicate;
  86. public TakeWhileInt(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  87. {
  88. this.source = source;
  89. this.predicate = predicate;
  90. }
  91. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  92. {
  93. return new _TakeWhileInt(source, predicate, cancellationToken);
  94. }
  95. class _TakeWhileInt : AsyncEnumeratorBase<TSource, TSource>
  96. {
  97. readonly Func<TSource, int, bool> predicate;
  98. int index;
  99. public _TakeWhileInt(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate, CancellationToken cancellationToken)
  100. : base(source, cancellationToken)
  101. {
  102. this.predicate = predicate;
  103. }
  104. protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
  105. {
  106. if (sourceHasCurrent)
  107. {
  108. if (predicate(SourceCurrent, checked(index++)))
  109. {
  110. Current = SourceCurrent;
  111. result = true;
  112. return true;
  113. }
  114. }
  115. result = false;
  116. return true;
  117. }
  118. }
  119. }
  120. internal sealed class TakeWhileAwait<TSource> : IUniTaskAsyncEnumerable<TSource>
  121. {
  122. readonly IUniTaskAsyncEnumerable<TSource> source;
  123. readonly Func<TSource, UniTask<bool>> predicate;
  124. public TakeWhileAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<bool>> predicate)
  125. {
  126. this.source = source;
  127. this.predicate = predicate;
  128. }
  129. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  130. {
  131. return new _TakeWhileAwait(source, predicate, cancellationToken);
  132. }
  133. class _TakeWhileAwait : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
  134. {
  135. Func<TSource, UniTask<bool>> predicate;
  136. public _TakeWhileAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<bool>> predicate, CancellationToken cancellationToken)
  137. : base(source, cancellationToken)
  138. {
  139. this.predicate = predicate;
  140. }
  141. protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
  142. {
  143. return predicate(sourceCurrent);
  144. }
  145. protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
  146. {
  147. if (awaitResult)
  148. {
  149. Current = SourceCurrent;
  150. terminateIteration = false;
  151. return true;
  152. }
  153. else
  154. {
  155. terminateIteration = true;
  156. return false;
  157. }
  158. }
  159. }
  160. }
  161. internal sealed class TakeWhileIntAwait<TSource> : IUniTaskAsyncEnumerable<TSource>
  162. {
  163. readonly IUniTaskAsyncEnumerable<TSource> source;
  164. readonly Func<TSource, int, UniTask<bool>> predicate;
  165. public TakeWhileIntAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<bool>> predicate)
  166. {
  167. this.source = source;
  168. this.predicate = predicate;
  169. }
  170. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  171. {
  172. return new _TakeWhileIntAwait(source, predicate, cancellationToken);
  173. }
  174. class _TakeWhileIntAwait : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
  175. {
  176. readonly Func<TSource, int, UniTask<bool>> predicate;
  177. int index;
  178. public _TakeWhileIntAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<bool>> predicate, CancellationToken cancellationToken)
  179. : base(source, cancellationToken)
  180. {
  181. this.predicate = predicate;
  182. }
  183. protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
  184. {
  185. return predicate(sourceCurrent, checked(index++));
  186. }
  187. protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
  188. {
  189. if (awaitResult)
  190. {
  191. Current = SourceCurrent;
  192. terminateIteration = false;
  193. return true;
  194. }
  195. else
  196. {
  197. terminateIteration = true;
  198. return false;
  199. }
  200. }
  201. }
  202. }
  203. internal sealed class TakeWhileAwaitWithCancellation<TSource> : IUniTaskAsyncEnumerable<TSource>
  204. {
  205. readonly IUniTaskAsyncEnumerable<TSource> source;
  206. readonly Func<TSource, CancellationToken, UniTask<bool>> predicate;
  207. public TakeWhileAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<bool>> predicate)
  208. {
  209. this.source = source;
  210. this.predicate = predicate;
  211. }
  212. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  213. {
  214. return new _TakeWhileAwaitWithCancellation(source, predicate, cancellationToken);
  215. }
  216. class _TakeWhileAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
  217. {
  218. Func<TSource, CancellationToken, UniTask<bool>> predicate;
  219. public _TakeWhileAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<bool>> predicate, CancellationToken cancellationToken)
  220. : base(source, cancellationToken)
  221. {
  222. this.predicate = predicate;
  223. }
  224. protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
  225. {
  226. return predicate(sourceCurrent, cancellationToken);
  227. }
  228. protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
  229. {
  230. if (awaitResult)
  231. {
  232. Current = SourceCurrent;
  233. terminateIteration = false;
  234. return true;
  235. }
  236. else
  237. {
  238. terminateIteration = true;
  239. return false;
  240. }
  241. }
  242. }
  243. }
  244. internal sealed class TakeWhileIntAwaitWithCancellation<TSource> : IUniTaskAsyncEnumerable<TSource>
  245. {
  246. readonly IUniTaskAsyncEnumerable<TSource> source;
  247. readonly Func<TSource, int, CancellationToken, UniTask<bool>> predicate;
  248. public TakeWhileIntAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<bool>> predicate)
  249. {
  250. this.source = source;
  251. this.predicate = predicate;
  252. }
  253. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  254. {
  255. return new _TakeWhileIntAwaitWithCancellation(source, predicate, cancellationToken);
  256. }
  257. class _TakeWhileIntAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
  258. {
  259. readonly Func<TSource, int, CancellationToken, UniTask<bool>> predicate;
  260. int index;
  261. public _TakeWhileIntAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<bool>> predicate, CancellationToken cancellationToken)
  262. : base(source, cancellationToken)
  263. {
  264. this.predicate = predicate;
  265. }
  266. protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
  267. {
  268. return predicate(sourceCurrent, checked(index++), cancellationToken);
  269. }
  270. protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
  271. {
  272. if (awaitResult)
  273. {
  274. Current = SourceCurrent;
  275. terminateIteration = false;
  276. return true;
  277. }
  278. else
  279. {
  280. terminateIteration = true;
  281. return false;
  282. }
  283. }
  284. }
  285. }
  286. }