Aggregate.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static Task<TSource> AggregateAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken = default)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. if (accumulator == null)
  16. throw Error.ArgumentNull(nameof(accumulator));
  17. return Core(source, accumulator, cancellationToken);
  18. async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, TSource, TSource> _accumulator, CancellationToken _cancellationToken)
  19. {
  20. #if CSHARP8
  21. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  22. {
  23. if (!await e.MoveNextAsync())
  24. {
  25. throw Error.NoElements();
  26. }
  27. var acc = e.Current;
  28. while (await e.MoveNextAsync())
  29. {
  30. acc = _accumulator(acc, e.Current);
  31. }
  32. return acc;
  33. }
  34. #else
  35. var e = _source.GetAsyncEnumerator(_cancellationToken);
  36. try
  37. {
  38. if (!await e.MoveNextAsync().ConfigureAwait(false))
  39. {
  40. throw Error.NoElements();
  41. }
  42. var acc = e.Current;
  43. while (await e.MoveNextAsync().ConfigureAwait(false))
  44. {
  45. acc = _accumulator(acc, e.Current);
  46. }
  47. return acc;
  48. }
  49. finally
  50. {
  51. await e.DisposeAsync().ConfigureAwait(false);
  52. }
  53. #endif
  54. }
  55. }
  56. public static Task<TSource> AggregateAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator, CancellationToken cancellationToken = default)
  57. {
  58. if (source == null)
  59. throw Error.ArgumentNull(nameof(source));
  60. if (accumulator == null)
  61. throw Error.ArgumentNull(nameof(accumulator));
  62. return Core(source, accumulator, cancellationToken);
  63. async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, TSource, ValueTask<TSource>> _accumulator, CancellationToken _cancellationToken)
  64. {
  65. #if CSHARP8
  66. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  67. {
  68. if (!await e.MoveNextAsync())
  69. {
  70. throw Error.NoElements();
  71. }
  72. var acc = e.Current;
  73. while (await e.MoveNextAsync())
  74. {
  75. acc = await _accumulator(acc, e.Current).ConfigureAwait(false);
  76. }
  77. return acc;
  78. }
  79. #else
  80. var e = _source.GetAsyncEnumerator(_cancellationToken);
  81. try
  82. {
  83. if (!await e.MoveNextAsync().ConfigureAwait(false))
  84. {
  85. throw Error.NoElements();
  86. }
  87. var acc = e.Current;
  88. while (await e.MoveNextAsync().ConfigureAwait(false))
  89. {
  90. acc = await _accumulator(acc, e.Current).ConfigureAwait(false);
  91. }
  92. return acc;
  93. }
  94. finally
  95. {
  96. await e.DisposeAsync().ConfigureAwait(false);
  97. }
  98. #endif
  99. }
  100. }
  101. #if !NO_DEEP_CANCELLATION
  102. public static Task<TSource> AggregateAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator, CancellationToken cancellationToken = default)
  103. {
  104. if (source == null)
  105. throw Error.ArgumentNull(nameof(source));
  106. if (accumulator == null)
  107. throw Error.ArgumentNull(nameof(accumulator));
  108. return Core(source, accumulator, cancellationToken);
  109. async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> _accumulator, CancellationToken _cancellationToken)
  110. {
  111. #if CSHARP8
  112. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  113. {
  114. if (!await e.MoveNextAsync())
  115. {
  116. throw Error.NoElements();
  117. }
  118. var acc = e.Current;
  119. while (await e.MoveNextAsync())
  120. {
  121. acc = await _accumulator(acc, e.Current, _cancellationToken).ConfigureAwait(false);
  122. }
  123. return acc;
  124. }
  125. #else
  126. var e = _source.GetAsyncEnumerator(_cancellationToken);
  127. try
  128. {
  129. if (!await e.MoveNextAsync().ConfigureAwait(false))
  130. {
  131. throw Error.NoElements();
  132. }
  133. var acc = e.Current;
  134. while (await e.MoveNextAsync().ConfigureAwait(false))
  135. {
  136. acc = await _accumulator(acc, e.Current, _cancellationToken).ConfigureAwait(false);
  137. }
  138. return acc;
  139. }
  140. finally
  141. {
  142. await e.DisposeAsync().ConfigureAwait(false);
  143. }
  144. #endif
  145. }
  146. }
  147. #endif
  148. public static Task<TAccumulate> AggregateAsync<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken = default)
  149. {
  150. if (source == null)
  151. throw Error.ArgumentNull(nameof(source));
  152. if (accumulator == null)
  153. throw Error.ArgumentNull(nameof(accumulator));
  154. return Core(source, seed, accumulator, cancellationToken);
  155. async Task<TAccumulate> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, TAccumulate> _accumulator, CancellationToken _cancellationToken)
  156. {
  157. var acc = _seed;
  158. #if CSHARP8
  159. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  160. {
  161. acc = _accumulator(acc, item);
  162. }
  163. #else
  164. var e = _source.GetAsyncEnumerator(_cancellationToken);
  165. try
  166. {
  167. while (await e.MoveNextAsync().ConfigureAwait(false))
  168. {
  169. acc = _accumulator(acc, e.Current);
  170. }
  171. }
  172. finally
  173. {
  174. await e.DisposeAsync().ConfigureAwait(false);
  175. }
  176. #endif
  177. return acc;
  178. }
  179. }
  180. public static Task<TAccumulate> AggregateAsync<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
  181. {
  182. if (source == null)
  183. throw Error.ArgumentNull(nameof(source));
  184. if (accumulator == null)
  185. throw Error.ArgumentNull(nameof(accumulator));
  186. return Core(source, seed, accumulator, cancellationToken);
  187. async Task<TAccumulate> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> _accumulator, CancellationToken _cancellationToken)
  188. {
  189. var acc = _seed;
  190. #if CSHARP8
  191. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  192. {
  193. acc = await _accumulator(acc, item).ConfigureAwait(false);
  194. }
  195. #else
  196. var e = _source.GetAsyncEnumerator(_cancellationToken);
  197. try
  198. {
  199. while (await e.MoveNextAsync().ConfigureAwait(false))
  200. {
  201. acc = await _accumulator(acc, e.Current).ConfigureAwait(false);
  202. }
  203. }
  204. finally
  205. {
  206. await e.DisposeAsync().ConfigureAwait(false);
  207. }
  208. #endif
  209. return acc;
  210. }
  211. }
  212. #if !NO_DEEP_CANCELLATION
  213. public static Task<TAccumulate> AggregateAsync<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
  214. {
  215. if (source == null)
  216. throw Error.ArgumentNull(nameof(source));
  217. if (accumulator == null)
  218. throw Error.ArgumentNull(nameof(accumulator));
  219. return Core(source, seed, accumulator, cancellationToken);
  220. async Task<TAccumulate> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> _accumulator, CancellationToken _cancellationToken)
  221. {
  222. var acc = _seed;
  223. #if CSHARP8
  224. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  225. {
  226. acc = await _accumulator(acc, item, _cancellationToken).ConfigureAwait(false);
  227. }
  228. #else
  229. var e = _source.GetAsyncEnumerator(_cancellationToken);
  230. try
  231. {
  232. while (await e.MoveNextAsync().ConfigureAwait(false))
  233. {
  234. acc = await _accumulator(acc, e.Current, _cancellationToken).ConfigureAwait(false);
  235. }
  236. }
  237. finally
  238. {
  239. await e.DisposeAsync().ConfigureAwait(false);
  240. }
  241. #endif
  242. return acc;
  243. }
  244. }
  245. #endif
  246. public static Task<TResult> AggregateAsync<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken = default)
  247. {
  248. if (source == null)
  249. throw Error.ArgumentNull(nameof(source));
  250. if (accumulator == null)
  251. throw Error.ArgumentNull(nameof(accumulator));
  252. if (resultSelector == null)
  253. throw Error.ArgumentNull(nameof(resultSelector));
  254. return Core(source, seed, accumulator, resultSelector, cancellationToken);
  255. async Task<TResult> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, TAccumulate> _accumulator, Func<TAccumulate, TResult> _resultSelector, CancellationToken _cancellationToken)
  256. {
  257. var acc = _seed;
  258. #if CSHARP8
  259. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  260. {
  261. acc = _accumulator(acc, item);
  262. }
  263. #else
  264. var e = _source.GetAsyncEnumerator(_cancellationToken);
  265. try
  266. {
  267. while (await e.MoveNextAsync().ConfigureAwait(false))
  268. {
  269. acc = _accumulator(acc, e.Current);
  270. }
  271. }
  272. finally
  273. {
  274. await e.DisposeAsync().ConfigureAwait(false);
  275. }
  276. #endif
  277. return _resultSelector(acc);
  278. }
  279. }
  280. public static Task<TResult> AggregateAsync<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, Func<TAccumulate, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
  281. {
  282. if (source == null)
  283. throw Error.ArgumentNull(nameof(source));
  284. if (accumulator == null)
  285. throw Error.ArgumentNull(nameof(accumulator));
  286. if (resultSelector == null)
  287. throw Error.ArgumentNull(nameof(resultSelector));
  288. return Core(source, seed, accumulator, resultSelector, cancellationToken);
  289. async Task<TResult> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> _accumulator, Func<TAccumulate, ValueTask<TResult>> _resultSelector, CancellationToken _cancellationToken)
  290. {
  291. var acc = _seed;
  292. #if CSHARP8
  293. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  294. {
  295. acc = await _accumulator(acc, item).ConfigureAwait(false);
  296. }
  297. #else
  298. var e = _source.GetAsyncEnumerator(_cancellationToken);
  299. try
  300. {
  301. while (await e.MoveNextAsync().ConfigureAwait(false))
  302. {
  303. acc = await _accumulator(acc, e.Current).ConfigureAwait(false);
  304. }
  305. }
  306. finally
  307. {
  308. await e.DisposeAsync().ConfigureAwait(false);
  309. }
  310. #endif
  311. return await _resultSelector(acc).ConfigureAwait(false);
  312. }
  313. }
  314. #if !NO_DEEP_CANCELLATION
  315. public static Task<TResult> AggregateAsync<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, Func<TAccumulate, CancellationToken, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
  316. {
  317. if (source == null)
  318. throw Error.ArgumentNull(nameof(source));
  319. if (accumulator == null)
  320. throw Error.ArgumentNull(nameof(accumulator));
  321. if (resultSelector == null)
  322. throw Error.ArgumentNull(nameof(resultSelector));
  323. return Core(source, seed, accumulator, resultSelector, cancellationToken);
  324. async Task<TResult> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> _accumulator, Func<TAccumulate, CancellationToken, ValueTask<TResult>> _resultSelector, CancellationToken _cancellationToken)
  325. {
  326. var acc = _seed;
  327. #if CSHARP8
  328. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  329. {
  330. acc = await _accumulator(acc, item, _cancellationToken).ConfigureAwait(false);
  331. }
  332. #else
  333. var e = _source.GetAsyncEnumerator(_cancellationToken);
  334. try
  335. {
  336. while (await e.MoveNextAsync().ConfigureAwait(false))
  337. {
  338. acc = await _accumulator(acc, e.Current, _cancellationToken).ConfigureAwait(false);
  339. }
  340. }
  341. finally
  342. {
  343. await e.DisposeAsync().ConfigureAwait(false);
  344. }
  345. #endif
  346. return await _resultSelector(acc, _cancellationToken).ConfigureAwait(false);
  347. }
  348. }
  349. #endif
  350. }
  351. }