Aggregate.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 AggregateCore(source, accumulator, cancellationToken);
  18. }
  19. public static Task<TSource> AggregateAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator, CancellationToken cancellationToken = default)
  20. {
  21. if (source == null)
  22. throw Error.ArgumentNull(nameof(source));
  23. if (accumulator == null)
  24. throw Error.ArgumentNull(nameof(accumulator));
  25. return AggregateCore(source, accumulator, cancellationToken);
  26. }
  27. #if !NO_DEEP_CANCELLATION
  28. public static Task<TSource> AggregateAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator, CancellationToken cancellationToken = default)
  29. {
  30. if (source == null)
  31. throw Error.ArgumentNull(nameof(source));
  32. if (accumulator == null)
  33. throw Error.ArgumentNull(nameof(accumulator));
  34. return AggregateCore(source, accumulator, cancellationToken);
  35. }
  36. #endif
  37. public static Task<TAccumulate> AggregateAsync<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken = default)
  38. {
  39. if (source == null)
  40. throw Error.ArgumentNull(nameof(source));
  41. if (accumulator == null)
  42. throw Error.ArgumentNull(nameof(accumulator));
  43. return AggregateCore(source, seed, accumulator, x => x, cancellationToken);
  44. }
  45. public static Task<TAccumulate> AggregateAsync<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
  46. {
  47. if (source == null)
  48. throw Error.ArgumentNull(nameof(source));
  49. if (accumulator == null)
  50. throw Error.ArgumentNull(nameof(accumulator));
  51. return AggregateCore(source, seed, accumulator, cancellationToken);
  52. }
  53. #if !NO_DEEP_CANCELLATION
  54. public static Task<TAccumulate> AggregateAsync<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
  55. {
  56. if (source == null)
  57. throw Error.ArgumentNull(nameof(source));
  58. if (accumulator == null)
  59. throw Error.ArgumentNull(nameof(accumulator));
  60. return AggregateCore(source, seed, accumulator, cancellationToken);
  61. }
  62. #endif
  63. 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)
  64. {
  65. if (source == null)
  66. throw Error.ArgumentNull(nameof(source));
  67. if (accumulator == null)
  68. throw Error.ArgumentNull(nameof(accumulator));
  69. if (resultSelector == null)
  70. throw Error.ArgumentNull(nameof(resultSelector));
  71. return AggregateCore(source, seed, accumulator, resultSelector, cancellationToken);
  72. }
  73. 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)
  74. {
  75. if (source == null)
  76. throw Error.ArgumentNull(nameof(source));
  77. if (accumulator == null)
  78. throw Error.ArgumentNull(nameof(accumulator));
  79. if (resultSelector == null)
  80. throw Error.ArgumentNull(nameof(resultSelector));
  81. return AggregateCore(source, seed, accumulator, resultSelector, cancellationToken);
  82. }
  83. #if !NO_DEEP_CANCELLATION
  84. 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)
  85. {
  86. if (source == null)
  87. throw Error.ArgumentNull(nameof(source));
  88. if (accumulator == null)
  89. throw Error.ArgumentNull(nameof(accumulator));
  90. if (resultSelector == null)
  91. throw Error.ArgumentNull(nameof(resultSelector));
  92. return AggregateCore(source, seed, accumulator, resultSelector, cancellationToken);
  93. }
  94. #endif
  95. private static async Task<TResult> AggregateCore<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken)
  96. {
  97. var acc = seed;
  98. var e = source.GetAsyncEnumerator(cancellationToken);
  99. try
  100. {
  101. while (await e.MoveNextAsync().ConfigureAwait(false))
  102. {
  103. acc = accumulator(acc, e.Current);
  104. }
  105. }
  106. finally
  107. {
  108. await e.DisposeAsync().ConfigureAwait(false);
  109. }
  110. return resultSelector(acc);
  111. }
  112. private static async Task<TSource> AggregateCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
  113. {
  114. var e = source.GetAsyncEnumerator(cancellationToken);
  115. try
  116. {
  117. if (!await e.MoveNextAsync().ConfigureAwait(false))
  118. {
  119. throw Error.NoElements();
  120. }
  121. var acc = e.Current;
  122. while (await e.MoveNextAsync().ConfigureAwait(false))
  123. {
  124. acc = accumulator(acc, e.Current);
  125. }
  126. return acc;
  127. }
  128. finally
  129. {
  130. await e.DisposeAsync().ConfigureAwait(false);
  131. }
  132. }
  133. private static async Task<TResult> AggregateCore<TSource, TResult>(IAsyncEnumerable<TSource> source, TResult seed, Func<TResult, TSource, ValueTask<TResult>> accumulator, CancellationToken cancellationToken)
  134. {
  135. var acc = seed;
  136. var e = source.GetAsyncEnumerator(cancellationToken);
  137. try
  138. {
  139. while (await e.MoveNextAsync().ConfigureAwait(false))
  140. {
  141. acc = await accumulator(acc, e.Current).ConfigureAwait(false);
  142. }
  143. }
  144. finally
  145. {
  146. await e.DisposeAsync().ConfigureAwait(false);
  147. }
  148. return acc;
  149. }
  150. #if !NO_DEEP_CANCELLATION
  151. private static async Task<TResult> AggregateCore<TSource, TResult>(IAsyncEnumerable<TSource> source, TResult seed, Func<TResult, TSource, CancellationToken, ValueTask<TResult>> accumulator, CancellationToken cancellationToken)
  152. {
  153. var acc = seed;
  154. var e = source.GetAsyncEnumerator(cancellationToken);
  155. try
  156. {
  157. while (await e.MoveNextAsync().ConfigureAwait(false))
  158. {
  159. acc = await accumulator(acc, e.Current, cancellationToken).ConfigureAwait(false);
  160. }
  161. }
  162. finally
  163. {
  164. await e.DisposeAsync().ConfigureAwait(false);
  165. }
  166. return acc;
  167. }
  168. #endif
  169. private static async Task<TResult> AggregateCore<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, Func<TAccumulate, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken)
  170. {
  171. var acc = seed;
  172. var e = source.GetAsyncEnumerator(cancellationToken);
  173. try
  174. {
  175. while (await e.MoveNextAsync().ConfigureAwait(false))
  176. {
  177. acc = await accumulator(acc, e.Current).ConfigureAwait(false);
  178. }
  179. }
  180. finally
  181. {
  182. await e.DisposeAsync().ConfigureAwait(false);
  183. }
  184. return await resultSelector(acc).ConfigureAwait(false);
  185. }
  186. #if !NO_DEEP_CANCELLATION
  187. private static async Task<TResult> AggregateCore<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, Func<TAccumulate, CancellationToken, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken)
  188. {
  189. var acc = seed;
  190. var e = source.GetAsyncEnumerator(cancellationToken);
  191. try
  192. {
  193. while (await e.MoveNextAsync().ConfigureAwait(false))
  194. {
  195. acc = await accumulator(acc, e.Current, cancellationToken).ConfigureAwait(false);
  196. }
  197. }
  198. finally
  199. {
  200. await e.DisposeAsync().ConfigureAwait(false);
  201. }
  202. return await resultSelector(acc, cancellationToken).ConfigureAwait(false);
  203. }
  204. #endif
  205. private static async Task<TSource> AggregateCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator, CancellationToken cancellationToken)
  206. {
  207. var e = source.GetAsyncEnumerator(cancellationToken);
  208. try
  209. {
  210. if (!await e.MoveNextAsync().ConfigureAwait(false))
  211. {
  212. throw Error.NoElements();
  213. }
  214. var acc = e.Current;
  215. while (await e.MoveNextAsync().ConfigureAwait(false))
  216. {
  217. acc = await accumulator(acc, e.Current).ConfigureAwait(false);
  218. }
  219. return acc;
  220. }
  221. finally
  222. {
  223. await e.DisposeAsync().ConfigureAwait(false);
  224. }
  225. }
  226. #if !NO_DEEP_CANCELLATION
  227. private static async Task<TSource> AggregateCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator, CancellationToken cancellationToken)
  228. {
  229. var e = source.GetAsyncEnumerator(cancellationToken);
  230. try
  231. {
  232. if (!await e.MoveNextAsync().ConfigureAwait(false))
  233. {
  234. throw Error.NoElements();
  235. }
  236. var acc = e.Current;
  237. while (await e.MoveNextAsync().ConfigureAwait(false))
  238. {
  239. acc = await accumulator(acc, e.Current, cancellationToken).ConfigureAwait(false);
  240. }
  241. return acc;
  242. }
  243. finally
  244. {
  245. await e.DisposeAsync().ConfigureAwait(false);
  246. }
  247. }
  248. #endif
  249. }
  250. }