Average.Generated.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 AsyncEnumerableEx
  10. {
  11. /// <summary>
  12. /// Computes the average of an async-enumerable sequence of <see cref="int" /> values that are obtained by invoking a transform function on each element of the input sequence.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">A sequence of values to calculate the average of.</param>
  16. /// <param name="selector">A transform function to apply to each element.</param>
  17. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  18. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  20. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  21. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  22. public static ValueTask<double> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken = default)
  23. {
  24. if (source == null)
  25. throw Error.ArgumentNull(nameof(source));
  26. if (selector == null)
  27. throw Error.ArgumentNull(nameof(selector));
  28. return Core(source, selector, cancellationToken);
  29. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  30. {
  31. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  32. {
  33. if (!await e.MoveNextAsync())
  34. {
  35. throw Error.NoElements();
  36. }
  37. long sum = selector(e.Current);
  38. long count = 1;
  39. checked
  40. {
  41. while (await e.MoveNextAsync())
  42. {
  43. sum += selector(e.Current);
  44. ++count;
  45. }
  46. }
  47. return (double)sum / count;
  48. }
  49. }
  50. }
  51. /// <summary>
  52. /// Computes the average of an async-enumerable sequence of <see cref="long" /> values that are obtained by invoking a transform function on each element of the input sequence.
  53. /// </summary>
  54. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  55. /// <param name="source">A sequence of values to calculate the average of.</param>
  56. /// <param name="selector">A transform function to apply to each element.</param>
  57. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  58. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>
  59. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  60. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  61. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  62. public static ValueTask<double> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken = default)
  63. {
  64. if (source == null)
  65. throw Error.ArgumentNull(nameof(source));
  66. if (selector == null)
  67. throw Error.ArgumentNull(nameof(selector));
  68. return Core(source, selector, cancellationToken);
  69. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  70. {
  71. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  72. {
  73. if (!await e.MoveNextAsync())
  74. {
  75. throw Error.NoElements();
  76. }
  77. long sum = selector(e.Current);
  78. long count = 1;
  79. checked
  80. {
  81. while (await e.MoveNextAsync())
  82. {
  83. sum += selector(e.Current);
  84. ++count;
  85. }
  86. }
  87. return (double)sum / count;
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// Computes the average of an async-enumerable sequence of <see cref="float" /> values that are obtained by invoking a transform function on each element of the input sequence.
  93. /// </summary>
  94. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  95. /// <param name="source">A sequence of values to calculate the average of.</param>
  96. /// <param name="selector">A transform function to apply to each element.</param>
  97. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  98. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>
  99. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  100. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  101. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  102. public static ValueTask<float> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken = default)
  103. {
  104. if (source == null)
  105. throw Error.ArgumentNull(nameof(source));
  106. if (selector == null)
  107. throw Error.ArgumentNull(nameof(selector));
  108. return Core(source, selector, cancellationToken);
  109. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  110. {
  111. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  112. {
  113. if (!await e.MoveNextAsync())
  114. {
  115. throw Error.NoElements();
  116. }
  117. double sum = selector(e.Current);
  118. long count = 1;
  119. checked
  120. {
  121. while (await e.MoveNextAsync())
  122. {
  123. sum += selector(e.Current);
  124. ++count;
  125. }
  126. }
  127. return (float)(sum / count);
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// Computes the average of an async-enumerable sequence of <see cref="double" /> values that are obtained by invoking a transform function on each element of the input sequence.
  133. /// </summary>
  134. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  135. /// <param name="source">A sequence of values to calculate the average of.</param>
  136. /// <param name="selector">A transform function to apply to each element.</param>
  137. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  138. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>
  139. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  140. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  141. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  142. public static ValueTask<double> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken = default)
  143. {
  144. if (source == null)
  145. throw Error.ArgumentNull(nameof(source));
  146. if (selector == null)
  147. throw Error.ArgumentNull(nameof(selector));
  148. return Core(source, selector, cancellationToken);
  149. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  150. {
  151. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  152. {
  153. if (!await e.MoveNextAsync())
  154. {
  155. throw Error.NoElements();
  156. }
  157. double sum = selector(e.Current);
  158. long count = 1;
  159. checked
  160. {
  161. while (await e.MoveNextAsync())
  162. {
  163. sum += selector(e.Current);
  164. ++count;
  165. }
  166. }
  167. return sum / count;
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// Computes the average of an async-enumerable sequence of <see cref="decimal" /> values that are obtained by invoking a transform function on each element of the input sequence.
  173. /// </summary>
  174. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  175. /// <param name="source">A sequence of values to calculate the average of.</param>
  176. /// <param name="selector">A transform function to apply to each element.</param>
  177. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  178. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>
  179. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  180. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  181. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  182. public static ValueTask<decimal> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken = default)
  183. {
  184. if (source == null)
  185. throw Error.ArgumentNull(nameof(source));
  186. if (selector == null)
  187. throw Error.ArgumentNull(nameof(selector));
  188. return Core(source, selector, cancellationToken);
  189. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  190. {
  191. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  192. {
  193. if (!await e.MoveNextAsync())
  194. {
  195. throw Error.NoElements();
  196. }
  197. decimal sum = selector(e.Current);
  198. long count = 1;
  199. checked
  200. {
  201. while (await e.MoveNextAsync())
  202. {
  203. sum += selector(e.Current);
  204. ++count;
  205. }
  206. }
  207. return sum / count;
  208. }
  209. }
  210. }
  211. /// <summary>
  212. /// Computes the average of an async-enumerable sequence of <see cref="Nullable{Int}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  213. /// </summary>
  214. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  215. /// <param name="source">A sequence of values to calculate the average of.</param>
  216. /// <param name="selector">A transform function to apply to each element.</param>
  217. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  218. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>
  219. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  220. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  221. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  222. public static ValueTask<double?> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken = default)
  223. {
  224. if (source == null)
  225. throw Error.ArgumentNull(nameof(source));
  226. if (selector == null)
  227. throw Error.ArgumentNull(nameof(selector));
  228. return Core(source, selector, cancellationToken);
  229. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  230. {
  231. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  232. {
  233. while (await e.MoveNextAsync())
  234. {
  235. var v = selector(e.Current);
  236. if (v.HasValue)
  237. {
  238. long sum = v.GetValueOrDefault();
  239. long count = 1;
  240. checked
  241. {
  242. while (await e.MoveNextAsync())
  243. {
  244. v = selector(e.Current);
  245. if (v.HasValue)
  246. {
  247. sum += v.GetValueOrDefault();
  248. ++count;
  249. }
  250. }
  251. }
  252. return (double)sum / count;
  253. }
  254. }
  255. }
  256. return null;
  257. }
  258. }
  259. /// <summary>
  260. /// Computes the average of an async-enumerable sequence of <see cref="Nullable{Long}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  261. /// </summary>
  262. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  263. /// <param name="source">A sequence of values to calculate the average of.</param>
  264. /// <param name="selector">A transform function to apply to each element.</param>
  265. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  266. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>
  267. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  268. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  269. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  270. public static ValueTask<double?> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken = default)
  271. {
  272. if (source == null)
  273. throw Error.ArgumentNull(nameof(source));
  274. if (selector == null)
  275. throw Error.ArgumentNull(nameof(selector));
  276. return Core(source, selector, cancellationToken);
  277. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  278. {
  279. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  280. {
  281. while (await e.MoveNextAsync())
  282. {
  283. var v = selector(e.Current);
  284. if (v.HasValue)
  285. {
  286. long sum = v.GetValueOrDefault();
  287. long count = 1;
  288. checked
  289. {
  290. while (await e.MoveNextAsync())
  291. {
  292. v = selector(e.Current);
  293. if (v.HasValue)
  294. {
  295. sum += v.GetValueOrDefault();
  296. ++count;
  297. }
  298. }
  299. }
  300. return (double)sum / count;
  301. }
  302. }
  303. }
  304. return null;
  305. }
  306. }
  307. /// <summary>
  308. /// Computes the average of an async-enumerable sequence of <see cref="Nullable{Float}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  309. /// </summary>
  310. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  311. /// <param name="source">A sequence of values to calculate the average of.</param>
  312. /// <param name="selector">A transform function to apply to each element.</param>
  313. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  314. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>
  315. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  316. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  317. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  318. public static ValueTask<float?> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken = default)
  319. {
  320. if (source == null)
  321. throw Error.ArgumentNull(nameof(source));
  322. if (selector == null)
  323. throw Error.ArgumentNull(nameof(selector));
  324. return Core(source, selector, cancellationToken);
  325. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  326. {
  327. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  328. {
  329. while (await e.MoveNextAsync())
  330. {
  331. var v = selector(e.Current);
  332. if (v.HasValue)
  333. {
  334. double sum = v.GetValueOrDefault();
  335. long count = 1;
  336. checked
  337. {
  338. while (await e.MoveNextAsync())
  339. {
  340. v = selector(e.Current);
  341. if (v.HasValue)
  342. {
  343. sum += v.GetValueOrDefault();
  344. ++count;
  345. }
  346. }
  347. }
  348. return (float)(sum / count);
  349. }
  350. }
  351. }
  352. return null;
  353. }
  354. }
  355. /// <summary>
  356. /// Computes the average of an async-enumerable sequence of <see cref="Nullable{Double}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  357. /// </summary>
  358. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  359. /// <param name="source">A sequence of values to calculate the average of.</param>
  360. /// <param name="selector">A transform function to apply to each element.</param>
  361. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  362. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>
  363. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  364. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  365. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  366. public static ValueTask<double?> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
  367. {
  368. if (source == null)
  369. throw Error.ArgumentNull(nameof(source));
  370. if (selector == null)
  371. throw Error.ArgumentNull(nameof(selector));
  372. return Core(source, selector, cancellationToken);
  373. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  374. {
  375. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  376. {
  377. while (await e.MoveNextAsync())
  378. {
  379. var v = selector(e.Current);
  380. if (v.HasValue)
  381. {
  382. double sum = v.GetValueOrDefault();
  383. long count = 1;
  384. checked
  385. {
  386. while (await e.MoveNextAsync())
  387. {
  388. v = selector(e.Current);
  389. if (v.HasValue)
  390. {
  391. sum += v.GetValueOrDefault();
  392. ++count;
  393. }
  394. }
  395. }
  396. return sum / count;
  397. }
  398. }
  399. }
  400. return null;
  401. }
  402. }
  403. /// <summary>
  404. /// Computes the average of an async-enumerable sequence of <see cref="Nullable{Decimal}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  405. /// </summary>
  406. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  407. /// <param name="source">A sequence of values to calculate the average of.</param>
  408. /// <param name="selector">A transform function to apply to each element.</param>
  409. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  410. /// <returns>An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>
  411. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  412. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  413. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  414. public static ValueTask<decimal?> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken = default)
  415. {
  416. if (source == null)
  417. throw Error.ArgumentNull(nameof(source));
  418. if (selector == null)
  419. throw Error.ArgumentNull(nameof(selector));
  420. return Core(source, selector, cancellationToken);
  421. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  422. {
  423. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  424. {
  425. while (await e.MoveNextAsync())
  426. {
  427. var v = selector(e.Current);
  428. if (v.HasValue)
  429. {
  430. decimal sum = v.GetValueOrDefault();
  431. long count = 1;
  432. checked
  433. {
  434. while (await e.MoveNextAsync())
  435. {
  436. v = selector(e.Current);
  437. if (v.HasValue)
  438. {
  439. sum += v.GetValueOrDefault();
  440. ++count;
  441. }
  442. }
  443. }
  444. return sum / count;
  445. }
  446. }
  447. }
  448. return null;
  449. }
  450. }
  451. }
  452. }