Scan.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerableEx
  11. {
  12. // NB: Implementations of Scan never yield the first element, unlike the behavior of Aggregate on a sequence with one
  13. // element, which returns the first element (or the seed if given an empty sequence). This is compatible with Rx
  14. // but one could argue whether it was the right default.
  15. public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
  16. {
  17. if (source == null)
  18. throw Error.ArgumentNull(nameof(source));
  19. if (accumulator == null)
  20. throw Error.ArgumentNull(nameof(accumulator));
  21. #if USE_ASYNC_ITERATOR
  22. return AsyncEnumerable.Create(Core);
  23. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  24. {
  25. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  26. {
  27. if (!await e.MoveNextAsync())
  28. {
  29. yield break;
  30. }
  31. var res = e.Current;
  32. while (await e.MoveNextAsync())
  33. {
  34. res = accumulator(res, e.Current);
  35. yield return res;
  36. }
  37. }
  38. }
  39. #else
  40. return new ScanAsyncEnumerable<TSource>(source, accumulator);
  41. #endif
  42. }
  43. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
  44. {
  45. if (source == null)
  46. throw Error.ArgumentNull(nameof(source));
  47. if (accumulator == null)
  48. throw Error.ArgumentNull(nameof(accumulator));
  49. #if USE_ASYNC_ITERATOR
  50. return AsyncEnumerable.Create(Core);
  51. async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
  52. {
  53. var res = seed;
  54. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  55. {
  56. res = accumulator(res, item);
  57. yield return res;
  58. }
  59. }
  60. #else
  61. return new ScanAsyncEnumerable<TSource, TAccumulate>(source, seed, accumulator);
  62. #endif
  63. }
  64. public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator)
  65. {
  66. if (source == null)
  67. throw Error.ArgumentNull(nameof(source));
  68. if (accumulator == null)
  69. throw Error.ArgumentNull(nameof(accumulator));
  70. #if USE_ASYNC_ITERATOR
  71. return AsyncEnumerable.Create(Core);
  72. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  73. {
  74. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  75. {
  76. if (!await e.MoveNextAsync())
  77. {
  78. yield break;
  79. }
  80. var res = e.Current;
  81. while (await e.MoveNextAsync())
  82. {
  83. res = await accumulator(res, e.Current).ConfigureAwait(false);
  84. yield return res;
  85. }
  86. }
  87. }
  88. #else
  89. return new ScanAsyncEnumerableWithTask<TSource>(source, accumulator);
  90. #endif
  91. }
  92. #if !NO_DEEP_CANCELLATION
  93. public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator)
  94. {
  95. if (source == null)
  96. throw Error.ArgumentNull(nameof(source));
  97. if (accumulator == null)
  98. throw Error.ArgumentNull(nameof(accumulator));
  99. #if USE_ASYNC_ITERATOR
  100. return AsyncEnumerable.Create(Core);
  101. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  102. {
  103. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  104. {
  105. if (!await e.MoveNextAsync())
  106. {
  107. yield break;
  108. }
  109. var res = e.Current;
  110. while (await e.MoveNextAsync())
  111. {
  112. res = await accumulator(res, e.Current, cancellationToken).ConfigureAwait(false);
  113. yield return res;
  114. }
  115. }
  116. }
  117. #else
  118. return new ScanAsyncEnumerableWithTaskAndCancellation<TSource>(source, accumulator);
  119. #endif
  120. }
  121. #endif
  122. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator)
  123. {
  124. if (source == null)
  125. throw Error.ArgumentNull(nameof(source));
  126. if (accumulator == null)
  127. throw Error.ArgumentNull(nameof(accumulator));
  128. #if USE_ASYNC_ITERATOR
  129. return AsyncEnumerable.Create(Core);
  130. async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
  131. {
  132. var res = seed;
  133. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  134. {
  135. res = await accumulator(res, item).ConfigureAwait(false);
  136. yield return res;
  137. }
  138. }
  139. #else
  140. return new ScanAsyncEnumerableWithTask<TSource, TAccumulate>(source, seed, accumulator);
  141. #endif
  142. }
  143. #if !NO_DEEP_CANCELLATION
  144. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator)
  145. {
  146. if (source == null)
  147. throw Error.ArgumentNull(nameof(source));
  148. if (accumulator == null)
  149. throw Error.ArgumentNull(nameof(accumulator));
  150. #if USE_ASYNC_ITERATOR
  151. return AsyncEnumerable.Create(Core);
  152. async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
  153. {
  154. var res = seed;
  155. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  156. {
  157. res = await accumulator(res, item, cancellationToken).ConfigureAwait(false);
  158. yield return res;
  159. }
  160. }
  161. #else
  162. return new ScanAsyncEnumerableWithTaskAndCancellation<TSource, TAccumulate>(source, seed, accumulator);
  163. #endif
  164. }
  165. #endif
  166. #if !USE_ASYNC_ITERATOR
  167. private sealed class ScanAsyncEnumerable<TSource> : AsyncIterator<TSource>
  168. {
  169. private readonly Func<TSource, TSource, TSource> _accumulator;
  170. private readonly IAsyncEnumerable<TSource> _source;
  171. private TSource _accumulated;
  172. private IAsyncEnumerator<TSource> _enumerator;
  173. private bool _hasSeed;
  174. public ScanAsyncEnumerable(IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
  175. {
  176. Debug.Assert(source != null);
  177. Debug.Assert(accumulator != null);
  178. _source = source;
  179. _accumulator = accumulator;
  180. }
  181. public override AsyncIteratorBase<TSource> Clone()
  182. {
  183. return new ScanAsyncEnumerable<TSource>(_source, _accumulator);
  184. }
  185. public override async ValueTask DisposeAsync()
  186. {
  187. if (_enumerator != null)
  188. {
  189. await _enumerator.DisposeAsync().ConfigureAwait(false);
  190. _enumerator = null;
  191. _accumulated = default;
  192. }
  193. await base.DisposeAsync().ConfigureAwait(false);
  194. }
  195. protected override async ValueTask<bool> MoveNextCore()
  196. {
  197. switch (_state)
  198. {
  199. case AsyncIteratorState.Allocated:
  200. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  201. _hasSeed = false;
  202. _accumulated = default;
  203. _state = AsyncIteratorState.Iterating;
  204. goto case AsyncIteratorState.Iterating;
  205. case AsyncIteratorState.Iterating:
  206. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  207. {
  208. var item = _enumerator.Current;
  209. if (!_hasSeed)
  210. {
  211. _hasSeed = true;
  212. _accumulated = item;
  213. continue; // loop
  214. }
  215. _accumulated = _accumulator(_accumulated, item);
  216. _current = _accumulated;
  217. return true;
  218. }
  219. break; // case
  220. }
  221. await DisposeAsync().ConfigureAwait(false);
  222. return false;
  223. }
  224. }
  225. private sealed class ScanAsyncEnumerable<TSource, TAccumulate> : AsyncIterator<TAccumulate>
  226. {
  227. private readonly Func<TAccumulate, TSource, TAccumulate> _accumulator;
  228. private readonly TAccumulate _seed;
  229. private readonly IAsyncEnumerable<TSource> _source;
  230. private TAccumulate _accumulated;
  231. private IAsyncEnumerator<TSource> _enumerator;
  232. public ScanAsyncEnumerable(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
  233. {
  234. Debug.Assert(source != null);
  235. Debug.Assert(accumulator != null);
  236. _source = source;
  237. _seed = seed;
  238. _accumulator = accumulator;
  239. }
  240. public override AsyncIteratorBase<TAccumulate> Clone()
  241. {
  242. return new ScanAsyncEnumerable<TSource, TAccumulate>(_source, _seed, _accumulator);
  243. }
  244. public override async ValueTask DisposeAsync()
  245. {
  246. if (_enumerator != null)
  247. {
  248. await _enumerator.DisposeAsync().ConfigureAwait(false);
  249. _enumerator = null;
  250. _accumulated = default;
  251. }
  252. await base.DisposeAsync().ConfigureAwait(false);
  253. }
  254. protected override async ValueTask<bool> MoveNextCore()
  255. {
  256. switch (_state)
  257. {
  258. case AsyncIteratorState.Allocated:
  259. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  260. _accumulated = _seed;
  261. _state = AsyncIteratorState.Iterating;
  262. goto case AsyncIteratorState.Iterating;
  263. case AsyncIteratorState.Iterating:
  264. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  265. {
  266. var item = _enumerator.Current;
  267. _accumulated = _accumulator(_accumulated, item);
  268. _current = _accumulated;
  269. return true;
  270. }
  271. break;
  272. }
  273. await DisposeAsync().ConfigureAwait(false);
  274. return false;
  275. }
  276. }
  277. private sealed class ScanAsyncEnumerableWithTask<TSource> : AsyncIterator<TSource>
  278. {
  279. private readonly Func<TSource, TSource, ValueTask<TSource>> _accumulator;
  280. private readonly IAsyncEnumerable<TSource> _source;
  281. private TSource _accumulated;
  282. private IAsyncEnumerator<TSource> _enumerator;
  283. private bool _hasSeed;
  284. public ScanAsyncEnumerableWithTask(IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator)
  285. {
  286. Debug.Assert(source != null);
  287. Debug.Assert(accumulator != null);
  288. _source = source;
  289. _accumulator = accumulator;
  290. }
  291. public override AsyncIteratorBase<TSource> Clone()
  292. {
  293. return new ScanAsyncEnumerableWithTask<TSource>(_source, _accumulator);
  294. }
  295. public override async ValueTask DisposeAsync()
  296. {
  297. if (_enumerator != null)
  298. {
  299. await _enumerator.DisposeAsync().ConfigureAwait(false);
  300. _enumerator = null;
  301. _accumulated = default;
  302. }
  303. await base.DisposeAsync().ConfigureAwait(false);
  304. }
  305. protected override async ValueTask<bool> MoveNextCore()
  306. {
  307. switch (_state)
  308. {
  309. case AsyncIteratorState.Allocated:
  310. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  311. _hasSeed = false;
  312. _accumulated = default;
  313. _state = AsyncIteratorState.Iterating;
  314. goto case AsyncIteratorState.Iterating;
  315. case AsyncIteratorState.Iterating:
  316. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  317. {
  318. var item = _enumerator.Current;
  319. if (!_hasSeed)
  320. {
  321. _hasSeed = true;
  322. _accumulated = item;
  323. continue; // loop
  324. }
  325. _accumulated = await _accumulator(_accumulated, item).ConfigureAwait(false);
  326. _current = _accumulated;
  327. return true;
  328. }
  329. break; // case
  330. }
  331. await DisposeAsync().ConfigureAwait(false);
  332. return false;
  333. }
  334. }
  335. #if !NO_DEEP_CANCELLATION
  336. private sealed class ScanAsyncEnumerableWithTaskAndCancellation<TSource> : AsyncIterator<TSource>
  337. {
  338. private readonly Func<TSource, TSource, CancellationToken, ValueTask<TSource>> _accumulator;
  339. private readonly IAsyncEnumerable<TSource> _source;
  340. private TSource _accumulated;
  341. private IAsyncEnumerator<TSource> _enumerator;
  342. private bool _hasSeed;
  343. public ScanAsyncEnumerableWithTaskAndCancellation(IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator)
  344. {
  345. Debug.Assert(source != null);
  346. Debug.Assert(accumulator != null);
  347. _source = source;
  348. _accumulator = accumulator;
  349. }
  350. public override AsyncIteratorBase<TSource> Clone()
  351. {
  352. return new ScanAsyncEnumerableWithTaskAndCancellation<TSource>(_source, _accumulator);
  353. }
  354. public override async ValueTask DisposeAsync()
  355. {
  356. if (_enumerator != null)
  357. {
  358. await _enumerator.DisposeAsync().ConfigureAwait(false);
  359. _enumerator = null;
  360. _accumulated = default;
  361. }
  362. await base.DisposeAsync().ConfigureAwait(false);
  363. }
  364. protected override async ValueTask<bool> MoveNextCore()
  365. {
  366. switch (_state)
  367. {
  368. case AsyncIteratorState.Allocated:
  369. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  370. _hasSeed = false;
  371. _accumulated = default;
  372. _state = AsyncIteratorState.Iterating;
  373. goto case AsyncIteratorState.Iterating;
  374. case AsyncIteratorState.Iterating:
  375. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  376. {
  377. var item = _enumerator.Current;
  378. if (!_hasSeed)
  379. {
  380. _hasSeed = true;
  381. _accumulated = item;
  382. continue; // loop
  383. }
  384. _accumulated = await _accumulator(_accumulated, item, _cancellationToken).ConfigureAwait(false);
  385. _current = _accumulated;
  386. return true;
  387. }
  388. break; // case
  389. }
  390. await DisposeAsync().ConfigureAwait(false);
  391. return false;
  392. }
  393. }
  394. #endif
  395. private sealed class ScanAsyncEnumerableWithTask<TSource, TAccumulate> : AsyncIterator<TAccumulate>
  396. {
  397. private readonly Func<TAccumulate, TSource, ValueTask<TAccumulate>> _accumulator;
  398. private readonly TAccumulate _seed;
  399. private readonly IAsyncEnumerable<TSource> _source;
  400. private TAccumulate _accumulated;
  401. private IAsyncEnumerator<TSource> _enumerator;
  402. public ScanAsyncEnumerableWithTask(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator)
  403. {
  404. Debug.Assert(source != null);
  405. Debug.Assert(accumulator != null);
  406. _source = source;
  407. _seed = seed;
  408. _accumulator = accumulator;
  409. }
  410. public override AsyncIteratorBase<TAccumulate> Clone()
  411. {
  412. return new ScanAsyncEnumerableWithTask<TSource, TAccumulate>(_source, _seed, _accumulator);
  413. }
  414. public override async ValueTask DisposeAsync()
  415. {
  416. if (_enumerator != null)
  417. {
  418. await _enumerator.DisposeAsync().ConfigureAwait(false);
  419. _enumerator = null;
  420. _accumulated = default;
  421. }
  422. await base.DisposeAsync().ConfigureAwait(false);
  423. }
  424. protected override async ValueTask<bool> MoveNextCore()
  425. {
  426. switch (_state)
  427. {
  428. case AsyncIteratorState.Allocated:
  429. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  430. _accumulated = _seed;
  431. _state = AsyncIteratorState.Iterating;
  432. goto case AsyncIteratorState.Iterating;
  433. case AsyncIteratorState.Iterating:
  434. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  435. {
  436. var item = _enumerator.Current;
  437. _accumulated = await _accumulator(_accumulated, item).ConfigureAwait(false);
  438. _current = _accumulated;
  439. return true;
  440. }
  441. break;
  442. }
  443. await DisposeAsync().ConfigureAwait(false);
  444. return false;
  445. }
  446. }
  447. #if !NO_DEEP_CANCELLATION
  448. private sealed class ScanAsyncEnumerableWithTaskAndCancellation<TSource, TAccumulate> : AsyncIterator<TAccumulate>
  449. {
  450. private readonly Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> _accumulator;
  451. private readonly TAccumulate _seed;
  452. private readonly IAsyncEnumerable<TSource> _source;
  453. private TAccumulate _accumulated;
  454. private IAsyncEnumerator<TSource> _enumerator;
  455. public ScanAsyncEnumerableWithTaskAndCancellation(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator)
  456. {
  457. Debug.Assert(source != null);
  458. Debug.Assert(accumulator != null);
  459. _source = source;
  460. _seed = seed;
  461. _accumulator = accumulator;
  462. }
  463. public override AsyncIteratorBase<TAccumulate> Clone()
  464. {
  465. return new ScanAsyncEnumerableWithTaskAndCancellation<TSource, TAccumulate>(_source, _seed, _accumulator);
  466. }
  467. public override async ValueTask DisposeAsync()
  468. {
  469. if (_enumerator != null)
  470. {
  471. await _enumerator.DisposeAsync().ConfigureAwait(false);
  472. _enumerator = null;
  473. _accumulated = default;
  474. }
  475. await base.DisposeAsync().ConfigureAwait(false);
  476. }
  477. protected override async ValueTask<bool> MoveNextCore()
  478. {
  479. switch (_state)
  480. {
  481. case AsyncIteratorState.Allocated:
  482. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  483. _accumulated = _seed;
  484. _state = AsyncIteratorState.Iterating;
  485. goto case AsyncIteratorState.Iterating;
  486. case AsyncIteratorState.Iterating:
  487. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  488. {
  489. var item = _enumerator.Current;
  490. _accumulated = await _accumulator(_accumulated, item, _cancellationToken).ConfigureAwait(false);
  491. _current = _accumulated;
  492. return true;
  493. }
  494. break;
  495. }
  496. await DisposeAsync().ConfigureAwait(false);
  497. return false;
  498. }
  499. }
  500. #endif
  501. #endif
  502. }
  503. }