Average.cs 26 KB

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