Average.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static Task<double> Average(this IAsyncEnumerable<int> source)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. return Average(source, CancellationToken.None);
  18. }
  19. public static Task<double?> Average(this IAsyncEnumerable<int?> source)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. return Average(source, CancellationToken.None);
  24. }
  25. public static Task<double> Average(this IAsyncEnumerable<long> source)
  26. {
  27. if (source == null)
  28. throw new ArgumentNullException(nameof(source));
  29. return Average(source, CancellationToken.None);
  30. }
  31. public static Task<double?> Average(this IAsyncEnumerable<long?> source)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. return Average(source, CancellationToken.None);
  36. }
  37. public static Task<double> Average(this IAsyncEnumerable<double> source)
  38. {
  39. if (source == null)
  40. throw new ArgumentNullException(nameof(source));
  41. return Average(source, CancellationToken.None);
  42. }
  43. public static Task<double?> Average(this IAsyncEnumerable<double?> source)
  44. {
  45. if (source == null)
  46. throw new ArgumentNullException(nameof(source));
  47. return Average(source, CancellationToken.None);
  48. }
  49. public static Task<float> Average(this IAsyncEnumerable<float> source)
  50. {
  51. if (source == null)
  52. throw new ArgumentNullException(nameof(source));
  53. return Average(source, CancellationToken.None);
  54. }
  55. public static Task<float?> Average(this IAsyncEnumerable<float?> source)
  56. {
  57. if (source == null)
  58. throw new ArgumentNullException(nameof(source));
  59. return Average(source, CancellationToken.None);
  60. }
  61. public static Task<decimal> Average(this IAsyncEnumerable<decimal> source)
  62. {
  63. if (source == null)
  64. throw new ArgumentNullException(nameof(source));
  65. return Average(source, CancellationToken.None);
  66. }
  67. public static Task<decimal?> Average(this IAsyncEnumerable<decimal?> source)
  68. {
  69. if (source == null)
  70. throw new ArgumentNullException(nameof(source));
  71. return Average(source, CancellationToken.None);
  72. }
  73. public static Task<double?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector)
  74. {
  75. if (source == null)
  76. throw new ArgumentNullException(nameof(source));
  77. if (selector == null)
  78. throw new ArgumentNullException(nameof(selector));
  79. return Average(source, selector, CancellationToken.None);
  80. }
  81. public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector)
  82. {
  83. if (source == null)
  84. throw new ArgumentNullException(nameof(source));
  85. if (selector == null)
  86. throw new ArgumentNullException(nameof(selector));
  87. return Average(source, selector, CancellationToken.None);
  88. }
  89. public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector)
  90. {
  91. if (source == null)
  92. throw new ArgumentNullException(nameof(source));
  93. if (selector == null)
  94. throw new ArgumentNullException(nameof(selector));
  95. return Average(source, selector, CancellationToken.None);
  96. }
  97. public static Task<double?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector)
  98. {
  99. if (source == null)
  100. throw new ArgumentNullException(nameof(source));
  101. if (selector == null)
  102. throw new ArgumentNullException(nameof(selector));
  103. return Average(source, selector, CancellationToken.None);
  104. }
  105. public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector)
  106. {
  107. if (source == null)
  108. throw new ArgumentNullException(nameof(source));
  109. if (selector == null)
  110. throw new ArgumentNullException(nameof(selector));
  111. return Average(source, selector, CancellationToken.None);
  112. }
  113. public static Task<double?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector)
  114. {
  115. if (source == null)
  116. throw new ArgumentNullException(nameof(source));
  117. if (selector == null)
  118. throw new ArgumentNullException(nameof(selector));
  119. return Average(source, selector, CancellationToken.None);
  120. }
  121. public static Task<float> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector)
  122. {
  123. if (source == null)
  124. throw new ArgumentNullException(nameof(source));
  125. if (selector == null)
  126. throw new ArgumentNullException(nameof(selector));
  127. return Average(source, selector, CancellationToken.None);
  128. }
  129. public static Task<float?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector)
  130. {
  131. if (source == null)
  132. throw new ArgumentNullException(nameof(source));
  133. if (selector == null)
  134. throw new ArgumentNullException(nameof(selector));
  135. return Average(source, selector, CancellationToken.None);
  136. }
  137. public static Task<decimal> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector)
  138. {
  139. if (source == null)
  140. throw new ArgumentNullException(nameof(source));
  141. if (selector == null)
  142. throw new ArgumentNullException(nameof(selector));
  143. return Average(source, selector, CancellationToken.None);
  144. }
  145. public static Task<decimal?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector)
  146. {
  147. if (source == null)
  148. throw new ArgumentNullException(nameof(source));
  149. if (selector == null)
  150. throw new ArgumentNullException(nameof(selector));
  151. return Average(source, selector, CancellationToken.None);
  152. }
  153. public static Task<double> Average(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  154. {
  155. if (source == null)
  156. throw new ArgumentNullException(nameof(source));
  157. return Average_(source, cancellationToken);
  158. }
  159. public static Task<double?> Average(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  160. {
  161. if (source == null)
  162. throw new ArgumentNullException(nameof(source));
  163. return Average_(source, cancellationToken);
  164. }
  165. public static Task<double> Average(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  166. {
  167. if (source == null)
  168. throw new ArgumentNullException(nameof(source));
  169. return Average_(source, cancellationToken);
  170. }
  171. public static Task<double?> Average(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  172. {
  173. if (source == null)
  174. throw new ArgumentNullException(nameof(source));
  175. return Average_(source, cancellationToken);
  176. }
  177. public static Task<double> Average(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  178. {
  179. if (source == null)
  180. throw new ArgumentNullException(nameof(source));
  181. return Average_(source, cancellationToken);
  182. }
  183. public static Task<double?> Average(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  184. {
  185. if (source == null)
  186. throw new ArgumentNullException(nameof(source));
  187. return Average_(source, cancellationToken);
  188. }
  189. public static Task<float> Average(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  190. {
  191. if (source == null)
  192. throw new ArgumentNullException(nameof(source));
  193. return Average_(source, cancellationToken);
  194. }
  195. public static Task<float?> Average(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  196. {
  197. if (source == null)
  198. throw new ArgumentNullException(nameof(source));
  199. return Average_(source, cancellationToken);
  200. }
  201. public static Task<decimal> Average(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  202. {
  203. if (source == null)
  204. throw new ArgumentNullException(nameof(source));
  205. return Average_(source, cancellationToken);
  206. }
  207. public static Task<decimal?> Average(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  208. {
  209. if (source == null)
  210. throw new ArgumentNullException(nameof(source));
  211. return Average_(source, cancellationToken);
  212. }
  213. public static Task<double?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  214. {
  215. if (source == null)
  216. throw new ArgumentNullException(nameof(source));
  217. if (selector == null)
  218. throw new ArgumentNullException(nameof(selector));
  219. return source.Select(selector)
  220. .Average(cancellationToken);
  221. }
  222. public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  223. {
  224. if (source == null)
  225. throw new ArgumentNullException(nameof(source));
  226. if (selector == null)
  227. throw new ArgumentNullException(nameof(selector));
  228. return source.Select(selector)
  229. .Average(cancellationToken);
  230. }
  231. public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  232. {
  233. if (source == null)
  234. throw new ArgumentNullException(nameof(source));
  235. if (selector == null)
  236. throw new ArgumentNullException(nameof(selector));
  237. return source.Select(selector)
  238. .Average(cancellationToken);
  239. }
  240. public static Task<double?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  241. {
  242. if (source == null)
  243. throw new ArgumentNullException(nameof(source));
  244. if (selector == null)
  245. throw new ArgumentNullException(nameof(selector));
  246. return source.Select(selector)
  247. .Average(cancellationToken);
  248. }
  249. public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  250. {
  251. if (source == null)
  252. throw new ArgumentNullException(nameof(source));
  253. if (selector == null)
  254. throw new ArgumentNullException(nameof(selector));
  255. return source.Select(selector)
  256. .Average(cancellationToken);
  257. }
  258. public static Task<double?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  259. {
  260. if (source == null)
  261. throw new ArgumentNullException(nameof(source));
  262. if (selector == null)
  263. throw new ArgumentNullException(nameof(selector));
  264. return source.Select(selector)
  265. .Average(cancellationToken);
  266. }
  267. public static Task<float> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  268. {
  269. if (source == null)
  270. throw new ArgumentNullException(nameof(source));
  271. if (selector == null)
  272. throw new ArgumentNullException(nameof(selector));
  273. return source.Select(selector)
  274. .Average(cancellationToken);
  275. }
  276. public static Task<float?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  277. {
  278. if (source == null)
  279. throw new ArgumentNullException(nameof(source));
  280. if (selector == null)
  281. throw new ArgumentNullException(nameof(selector));
  282. return source.Select(selector)
  283. .Average(cancellationToken);
  284. }
  285. public static Task<decimal> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  286. {
  287. if (source == null)
  288. throw new ArgumentNullException(nameof(source));
  289. if (selector == null)
  290. throw new ArgumentNullException(nameof(selector));
  291. return source.Select(selector)
  292. .Average(cancellationToken);
  293. }
  294. public static Task<decimal?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  295. {
  296. if (source == null)
  297. throw new ArgumentNullException(nameof(source));
  298. if (selector == null)
  299. throw new ArgumentNullException(nameof(selector));
  300. return source.Select(selector)
  301. .Average(cancellationToken);
  302. }
  303. private static async Task<double> Average_(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  304. {
  305. var e = source.GetAsyncEnumerator();
  306. try
  307. {
  308. if (!await e.MoveNextAsync(cancellationToken)
  309. .ConfigureAwait(false))
  310. {
  311. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  312. }
  313. long sum = e.Current;
  314. long count = 1;
  315. checked
  316. {
  317. while (await e.MoveNextAsync(cancellationToken)
  318. .ConfigureAwait(false))
  319. {
  320. sum += e.Current;
  321. ++count;
  322. }
  323. }
  324. return (double)sum/count;
  325. }
  326. finally
  327. {
  328. await e.DisposeAsync().ConfigureAwait(false);
  329. }
  330. }
  331. private static async Task<double?> Average_(IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  332. {
  333. var e = source.GetAsyncEnumerator();
  334. try
  335. {
  336. while (await e.MoveNextAsync(cancellationToken)
  337. .ConfigureAwait(false))
  338. {
  339. var v = e.Current;
  340. if (v.HasValue)
  341. {
  342. long sum = v.GetValueOrDefault();
  343. long count = 1;
  344. checked
  345. {
  346. while (await e.MoveNextAsync(cancellationToken)
  347. .ConfigureAwait(false))
  348. {
  349. v = e.Current;
  350. if (v.HasValue)
  351. {
  352. sum += v.GetValueOrDefault();
  353. ++count;
  354. }
  355. }
  356. }
  357. return (double)sum/count;
  358. }
  359. }
  360. }
  361. finally
  362. {
  363. await e.DisposeAsync().ConfigureAwait(false);
  364. }
  365. return null;
  366. }
  367. private static async Task<double> Average_(IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  368. {
  369. var e = source.GetAsyncEnumerator();
  370. try
  371. {
  372. if (!await e.MoveNextAsync(cancellationToken)
  373. .ConfigureAwait(false))
  374. {
  375. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  376. }
  377. var sum = e.Current;
  378. long count = 1;
  379. checked
  380. {
  381. while (await e.MoveNextAsync(cancellationToken)
  382. .ConfigureAwait(false))
  383. {
  384. sum += e.Current;
  385. ++count;
  386. }
  387. }
  388. return (double)sum/count;
  389. }
  390. finally
  391. {
  392. await e.DisposeAsync().ConfigureAwait(false);
  393. }
  394. }
  395. private static async Task<double?> Average_(IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  396. {
  397. var e = source.GetAsyncEnumerator();
  398. try
  399. {
  400. while (await e.MoveNextAsync(cancellationToken)
  401. .ConfigureAwait(false))
  402. {
  403. var v = e.Current;
  404. if (v.HasValue)
  405. {
  406. var sum = v.GetValueOrDefault();
  407. long count = 1;
  408. checked
  409. {
  410. while (await e.MoveNextAsync(cancellationToken)
  411. .ConfigureAwait(false))
  412. {
  413. v = e.Current;
  414. if (v.HasValue)
  415. {
  416. sum += v.GetValueOrDefault();
  417. ++count;
  418. }
  419. }
  420. }
  421. return (double)sum/count;
  422. }
  423. }
  424. }
  425. finally
  426. {
  427. await e.DisposeAsync().ConfigureAwait(false);
  428. }
  429. return null;
  430. }
  431. private static async Task<double> Average_(IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  432. {
  433. var e = source.GetAsyncEnumerator();
  434. try
  435. {
  436. if (!await e.MoveNextAsync(cancellationToken)
  437. .ConfigureAwait(false))
  438. {
  439. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  440. }
  441. var sum = e.Current;
  442. long count = 1;
  443. while (await e.MoveNextAsync(cancellationToken)
  444. .ConfigureAwait(false))
  445. {
  446. // There is an opportunity to short-circuit here, in that if e.Current is
  447. // ever NaN then the result will always be NaN. Assuming that this case is
  448. // rare enough that not checking is the better approach generally.
  449. sum += e.Current;
  450. ++count;
  451. }
  452. return sum/count;
  453. }
  454. finally
  455. {
  456. await e.DisposeAsync().ConfigureAwait(false);
  457. }
  458. }
  459. private static async Task<double?> Average_(IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  460. {
  461. var e = source.GetAsyncEnumerator();
  462. try
  463. {
  464. while (await e.MoveNextAsync(cancellationToken)
  465. .ConfigureAwait(false))
  466. {
  467. var v = e.Current;
  468. if (v.HasValue)
  469. {
  470. var sum = v.GetValueOrDefault();
  471. long count = 1;
  472. checked
  473. {
  474. while (await e.MoveNextAsync(cancellationToken)
  475. .ConfigureAwait(false))
  476. {
  477. v = e.Current;
  478. if (v.HasValue)
  479. {
  480. sum += v.GetValueOrDefault();
  481. ++count;
  482. }
  483. }
  484. }
  485. return sum/count;
  486. }
  487. }
  488. }
  489. finally
  490. {
  491. await e.DisposeAsync().ConfigureAwait(false);
  492. }
  493. return null;
  494. }
  495. private static async Task<float> Average_(IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  496. {
  497. var e = source.GetAsyncEnumerator();
  498. try
  499. {
  500. if (!await e.MoveNextAsync(cancellationToken)
  501. .ConfigureAwait(false))
  502. {
  503. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  504. }
  505. double sum = e.Current;
  506. long count = 1;
  507. while (await e.MoveNextAsync(cancellationToken)
  508. .ConfigureAwait(false))
  509. {
  510. sum += e.Current;
  511. ++count;
  512. }
  513. return (float)(sum/count);
  514. }
  515. finally
  516. {
  517. await e.DisposeAsync().ConfigureAwait(false);
  518. }
  519. }
  520. private static async Task<float?> Average_(IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  521. {
  522. var e = source.GetAsyncEnumerator();
  523. try
  524. {
  525. while (await e.MoveNextAsync(cancellationToken)
  526. .ConfigureAwait(false))
  527. {
  528. var v = e.Current;
  529. if (v.HasValue)
  530. {
  531. double sum = v.GetValueOrDefault();
  532. long count = 1;
  533. checked
  534. {
  535. while (await e.MoveNextAsync(cancellationToken)
  536. .ConfigureAwait(false))
  537. {
  538. v = e.Current;
  539. if (v.HasValue)
  540. {
  541. sum += v.GetValueOrDefault();
  542. ++count;
  543. }
  544. }
  545. }
  546. return (float)(sum/count);
  547. }
  548. }
  549. }
  550. finally
  551. {
  552. await e.DisposeAsync().ConfigureAwait(false);
  553. }
  554. return null;
  555. }
  556. private static async Task<decimal> Average_(IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  557. {
  558. var e = source.GetAsyncEnumerator();
  559. try
  560. {
  561. if (!await e.MoveNextAsync(cancellationToken)
  562. .ConfigureAwait(false))
  563. {
  564. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  565. }
  566. var sum = e.Current;
  567. long count = 1;
  568. while (await e.MoveNextAsync(cancellationToken)
  569. .ConfigureAwait(false))
  570. {
  571. sum += e.Current;
  572. ++count;
  573. }
  574. return sum/count;
  575. }
  576. finally
  577. {
  578. await e.DisposeAsync().ConfigureAwait(false);
  579. }
  580. }
  581. private static async Task<decimal?> Average_(IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  582. {
  583. var e = source.GetAsyncEnumerator();
  584. try
  585. {
  586. while (await e.MoveNextAsync(cancellationToken)
  587. .ConfigureAwait(false))
  588. {
  589. var v = e.Current;
  590. if (v.HasValue)
  591. {
  592. var sum = v.GetValueOrDefault();
  593. long count = 1;
  594. while (await e.MoveNextAsync(cancellationToken)
  595. .ConfigureAwait(false))
  596. {
  597. v = e.Current;
  598. if (v.HasValue)
  599. {
  600. sum += v.GetValueOrDefault();
  601. ++count;
  602. }
  603. }
  604. return sum/count;
  605. }
  606. }
  607. }
  608. finally
  609. {
  610. await e.DisposeAsync().ConfigureAwait(false);
  611. }
  612. return null;
  613. }
  614. }
  615. }