1
0

Average.cs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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. private static async Task<double> AverageCore(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  12. {
  13. var e = source.GetAsyncEnumerator(cancellationToken);
  14. try
  15. {
  16. if (!await e.MoveNextAsync().ConfigureAwait(false))
  17. {
  18. throw Error.NoElements();
  19. }
  20. long sum = e.Current;
  21. long count = 1;
  22. checked
  23. {
  24. while (await e.MoveNextAsync().ConfigureAwait(false))
  25. {
  26. sum += e.Current;
  27. ++count;
  28. }
  29. }
  30. return (double)sum / count;
  31. }
  32. finally
  33. {
  34. await e.DisposeAsync().ConfigureAwait(false);
  35. }
  36. }
  37. private static async Task<double> AverageCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  38. {
  39. var e = source.GetAsyncEnumerator(cancellationToken);
  40. try
  41. {
  42. if (!await e.MoveNextAsync().ConfigureAwait(false))
  43. {
  44. throw Error.NoElements();
  45. }
  46. long sum = selector(e.Current);
  47. long count = 1;
  48. checked
  49. {
  50. while (await e.MoveNextAsync().ConfigureAwait(false))
  51. {
  52. sum += selector(e.Current);
  53. ++count;
  54. }
  55. }
  56. return (double)sum / count;
  57. }
  58. finally
  59. {
  60. await e.DisposeAsync().ConfigureAwait(false);
  61. }
  62. }
  63. private static async Task<double> AverageCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken)
  64. {
  65. var e = source.GetAsyncEnumerator(cancellationToken);
  66. try
  67. {
  68. if (!await e.MoveNextAsync().ConfigureAwait(false))
  69. {
  70. throw Error.NoElements();
  71. }
  72. long sum = await selector(e.Current).ConfigureAwait(false);
  73. long count = 1;
  74. checked
  75. {
  76. while (await e.MoveNextAsync().ConfigureAwait(false))
  77. {
  78. sum += await selector(e.Current).ConfigureAwait(false);
  79. ++count;
  80. }
  81. }
  82. return (double)sum / count;
  83. }
  84. finally
  85. {
  86. await e.DisposeAsync().ConfigureAwait(false);
  87. }
  88. }
  89. #if !NO_DEEP_CANCELLATION
  90. private static async Task<double> AverageCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken)
  91. {
  92. var e = source.GetAsyncEnumerator(cancellationToken);
  93. try
  94. {
  95. if (!await e.MoveNextAsync().ConfigureAwait(false))
  96. {
  97. throw Error.NoElements();
  98. }
  99. long sum = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  100. long count = 1;
  101. checked
  102. {
  103. while (await e.MoveNextAsync().ConfigureAwait(false))
  104. {
  105. sum += await selector(e.Current, cancellationToken).ConfigureAwait(false);
  106. ++count;
  107. }
  108. }
  109. return (double)sum / count;
  110. }
  111. finally
  112. {
  113. await e.DisposeAsync().ConfigureAwait(false);
  114. }
  115. }
  116. #endif
  117. private static async Task<double?> AverageCore(IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  118. {
  119. var e = source.GetAsyncEnumerator(cancellationToken);
  120. try
  121. {
  122. while (await e.MoveNextAsync().ConfigureAwait(false))
  123. {
  124. var v = e.Current;
  125. if (v.HasValue)
  126. {
  127. long sum = v.GetValueOrDefault();
  128. long count = 1;
  129. checked
  130. {
  131. while (await e.MoveNextAsync().ConfigureAwait(false))
  132. {
  133. v = e.Current;
  134. if (v.HasValue)
  135. {
  136. sum += v.GetValueOrDefault();
  137. ++count;
  138. }
  139. }
  140. }
  141. return (double)sum / count;
  142. }
  143. }
  144. }
  145. finally
  146. {
  147. await e.DisposeAsync().ConfigureAwait(false);
  148. }
  149. return null;
  150. }
  151. private static async Task<double?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  152. {
  153. var e = source.GetAsyncEnumerator(cancellationToken);
  154. try
  155. {
  156. while (await e.MoveNextAsync().ConfigureAwait(false))
  157. {
  158. var v = selector(e.Current);
  159. if (v.HasValue)
  160. {
  161. long sum = v.GetValueOrDefault();
  162. long count = 1;
  163. checked
  164. {
  165. while (await e.MoveNextAsync().ConfigureAwait(false))
  166. {
  167. v = selector(e.Current);
  168. if (v.HasValue)
  169. {
  170. sum += v.GetValueOrDefault();
  171. ++count;
  172. }
  173. }
  174. }
  175. return (double)sum / count;
  176. }
  177. }
  178. }
  179. finally
  180. {
  181. await e.DisposeAsync().ConfigureAwait(false);
  182. }
  183. return null;
  184. }
  185. private static async Task<double?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken)
  186. {
  187. var e = source.GetAsyncEnumerator(cancellationToken);
  188. try
  189. {
  190. while (await e.MoveNextAsync().ConfigureAwait(false))
  191. {
  192. var v = await selector(e.Current).ConfigureAwait(false);
  193. if (v.HasValue)
  194. {
  195. long sum = v.GetValueOrDefault();
  196. long count = 1;
  197. checked
  198. {
  199. while (await e.MoveNextAsync().ConfigureAwait(false))
  200. {
  201. v = await selector(e.Current).ConfigureAwait(false);
  202. if (v.HasValue)
  203. {
  204. sum += v.GetValueOrDefault();
  205. ++count;
  206. }
  207. }
  208. }
  209. return (double)sum / count;
  210. }
  211. }
  212. }
  213. finally
  214. {
  215. await e.DisposeAsync().ConfigureAwait(false);
  216. }
  217. return null;
  218. }
  219. #if !NO_DEEP_CANCELLATION
  220. private static async Task<double?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken)
  221. {
  222. var e = source.GetAsyncEnumerator(cancellationToken);
  223. try
  224. {
  225. while (await e.MoveNextAsync().ConfigureAwait(false))
  226. {
  227. var v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  228. if (v.HasValue)
  229. {
  230. long sum = v.GetValueOrDefault();
  231. long count = 1;
  232. checked
  233. {
  234. while (await e.MoveNextAsync().ConfigureAwait(false))
  235. {
  236. v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  237. if (v.HasValue)
  238. {
  239. sum += v.GetValueOrDefault();
  240. ++count;
  241. }
  242. }
  243. }
  244. return (double)sum / count;
  245. }
  246. }
  247. }
  248. finally
  249. {
  250. await e.DisposeAsync().ConfigureAwait(false);
  251. }
  252. return null;
  253. }
  254. #endif
  255. private static async Task<double> AverageCore(IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  256. {
  257. var e = source.GetAsyncEnumerator(cancellationToken);
  258. try
  259. {
  260. if (!await e.MoveNextAsync().ConfigureAwait(false))
  261. {
  262. throw Error.NoElements();
  263. }
  264. var sum = e.Current;
  265. long count = 1;
  266. checked
  267. {
  268. while (await e.MoveNextAsync().ConfigureAwait(false))
  269. {
  270. sum += e.Current;
  271. ++count;
  272. }
  273. }
  274. return (double)sum / count;
  275. }
  276. finally
  277. {
  278. await e.DisposeAsync().ConfigureAwait(false);
  279. }
  280. }
  281. private static async Task<double> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  282. {
  283. var e = source.GetAsyncEnumerator(cancellationToken);
  284. try
  285. {
  286. if (!await e.MoveNextAsync().ConfigureAwait(false))
  287. {
  288. throw Error.NoElements();
  289. }
  290. var sum = selector(e.Current);
  291. long count = 1;
  292. checked
  293. {
  294. while (await e.MoveNextAsync().ConfigureAwait(false))
  295. {
  296. sum += selector(e.Current);
  297. ++count;
  298. }
  299. }
  300. return (double)sum / count;
  301. }
  302. finally
  303. {
  304. await e.DisposeAsync().ConfigureAwait(false);
  305. }
  306. }
  307. private static async Task<double> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken)
  308. {
  309. var e = source.GetAsyncEnumerator(cancellationToken);
  310. try
  311. {
  312. if (!await e.MoveNextAsync().ConfigureAwait(false))
  313. {
  314. throw Error.NoElements();
  315. }
  316. var sum = await selector(e.Current).ConfigureAwait(false);
  317. long count = 1;
  318. checked
  319. {
  320. while (await e.MoveNextAsync().ConfigureAwait(false))
  321. {
  322. sum += await selector(e.Current).ConfigureAwait(false);
  323. ++count;
  324. }
  325. }
  326. return (double)sum / count;
  327. }
  328. finally
  329. {
  330. await e.DisposeAsync().ConfigureAwait(false);
  331. }
  332. }
  333. #if !NO_DEEP_CANCELLATION
  334. private static async Task<double> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken)
  335. {
  336. var e = source.GetAsyncEnumerator(cancellationToken);
  337. try
  338. {
  339. if (!await e.MoveNextAsync().ConfigureAwait(false))
  340. {
  341. throw Error.NoElements();
  342. }
  343. var sum = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  344. long count = 1;
  345. checked
  346. {
  347. while (await e.MoveNextAsync().ConfigureAwait(false))
  348. {
  349. sum += await selector(e.Current, cancellationToken).ConfigureAwait(false);
  350. ++count;
  351. }
  352. }
  353. return (double)sum / count;
  354. }
  355. finally
  356. {
  357. await e.DisposeAsync().ConfigureAwait(false);
  358. }
  359. }
  360. #endif
  361. private static async Task<double?> AverageCore(IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  362. {
  363. var e = source.GetAsyncEnumerator(cancellationToken);
  364. try
  365. {
  366. while (await e.MoveNextAsync().ConfigureAwait(false))
  367. {
  368. var v = e.Current;
  369. if (v.HasValue)
  370. {
  371. var sum = v.GetValueOrDefault();
  372. long count = 1;
  373. checked
  374. {
  375. while (await e.MoveNextAsync().ConfigureAwait(false))
  376. {
  377. v = e.Current;
  378. if (v.HasValue)
  379. {
  380. sum += v.GetValueOrDefault();
  381. ++count;
  382. }
  383. }
  384. }
  385. return (double)sum / count;
  386. }
  387. }
  388. }
  389. finally
  390. {
  391. await e.DisposeAsync().ConfigureAwait(false);
  392. }
  393. return null;
  394. }
  395. private static async Task<double?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  396. {
  397. var e = source.GetAsyncEnumerator(cancellationToken);
  398. try
  399. {
  400. while (await e.MoveNextAsync().ConfigureAwait(false))
  401. {
  402. var v = selector(e.Current);
  403. if (v.HasValue)
  404. {
  405. var sum = v.GetValueOrDefault();
  406. long count = 1;
  407. checked
  408. {
  409. while (await e.MoveNextAsync().ConfigureAwait(false))
  410. {
  411. v = selector(e.Current);
  412. if (v.HasValue)
  413. {
  414. sum += v.GetValueOrDefault();
  415. ++count;
  416. }
  417. }
  418. }
  419. return (double)sum / count;
  420. }
  421. }
  422. }
  423. finally
  424. {
  425. await e.DisposeAsync().ConfigureAwait(false);
  426. }
  427. return null;
  428. }
  429. private static async Task<double?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken)
  430. {
  431. var e = source.GetAsyncEnumerator(cancellationToken);
  432. try
  433. {
  434. while (await e.MoveNextAsync().ConfigureAwait(false))
  435. {
  436. var v = await selector(e.Current).ConfigureAwait(false);
  437. if (v.HasValue)
  438. {
  439. var sum = v.GetValueOrDefault();
  440. long count = 1;
  441. checked
  442. {
  443. while (await e.MoveNextAsync().ConfigureAwait(false))
  444. {
  445. v = await selector(e.Current).ConfigureAwait(false);
  446. if (v.HasValue)
  447. {
  448. sum += v.GetValueOrDefault();
  449. ++count;
  450. }
  451. }
  452. }
  453. return (double)sum / count;
  454. }
  455. }
  456. }
  457. finally
  458. {
  459. await e.DisposeAsync().ConfigureAwait(false);
  460. }
  461. return null;
  462. }
  463. #if !NO_DEEP_CANCELLATION
  464. private static async Task<double?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken)
  465. {
  466. var e = source.GetAsyncEnumerator(cancellationToken);
  467. try
  468. {
  469. while (await e.MoveNextAsync().ConfigureAwait(false))
  470. {
  471. var v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  472. if (v.HasValue)
  473. {
  474. var sum = v.GetValueOrDefault();
  475. long count = 1;
  476. checked
  477. {
  478. while (await e.MoveNextAsync().ConfigureAwait(false))
  479. {
  480. v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  481. if (v.HasValue)
  482. {
  483. sum += v.GetValueOrDefault();
  484. ++count;
  485. }
  486. }
  487. }
  488. return (double)sum / count;
  489. }
  490. }
  491. }
  492. finally
  493. {
  494. await e.DisposeAsync().ConfigureAwait(false);
  495. }
  496. return null;
  497. }
  498. #endif
  499. private static async Task<double> AverageCore(IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  500. {
  501. var e = source.GetAsyncEnumerator(cancellationToken);
  502. try
  503. {
  504. if (!await e.MoveNextAsync().ConfigureAwait(false))
  505. {
  506. throw Error.NoElements();
  507. }
  508. var sum = e.Current;
  509. long count = 1;
  510. while (await e.MoveNextAsync().ConfigureAwait(false))
  511. {
  512. // There is an opportunity to short-circuit here, in that if e.Current is
  513. // ever NaN then the result will always be NaN. Assuming that this case is
  514. // rare enough that not checking is the better approach generally.
  515. sum += e.Current;
  516. ++count;
  517. }
  518. return sum / count;
  519. }
  520. finally
  521. {
  522. await e.DisposeAsync().ConfigureAwait(false);
  523. }
  524. }
  525. private static async Task<double> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  526. {
  527. var e = source.GetAsyncEnumerator(cancellationToken);
  528. try
  529. {
  530. if (!await e.MoveNextAsync().ConfigureAwait(false))
  531. {
  532. throw Error.NoElements();
  533. }
  534. var sum = selector(e.Current);
  535. long count = 1;
  536. while (await e.MoveNextAsync().ConfigureAwait(false))
  537. {
  538. // There is an opportunity to short-circuit here, in that if e.Current is
  539. // ever NaN then the result will always be NaN. Assuming that this case is
  540. // rare enough that not checking is the better approach generally.
  541. sum += selector(e.Current);
  542. ++count;
  543. }
  544. return sum / count;
  545. }
  546. finally
  547. {
  548. await e.DisposeAsync().ConfigureAwait(false);
  549. }
  550. }
  551. private static async Task<double> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken)
  552. {
  553. var e = source.GetAsyncEnumerator(cancellationToken);
  554. try
  555. {
  556. if (!await e.MoveNextAsync().ConfigureAwait(false))
  557. {
  558. throw Error.NoElements();
  559. }
  560. var sum = await selector(e.Current).ConfigureAwait(false);
  561. long count = 1;
  562. while (await e.MoveNextAsync().ConfigureAwait(false))
  563. {
  564. // There is an opportunity to short-circuit here, in that if e.Current is
  565. // ever NaN then the result will always be NaN. Assuming that this case is
  566. // rare enough that not checking is the better approach generally.
  567. sum += await selector(e.Current).ConfigureAwait(false);
  568. ++count;
  569. }
  570. return sum / count;
  571. }
  572. finally
  573. {
  574. await e.DisposeAsync().ConfigureAwait(false);
  575. }
  576. }
  577. #if !NO_DEEP_CANCELLATION
  578. private static async Task<double> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken)
  579. {
  580. var e = source.GetAsyncEnumerator(cancellationToken);
  581. try
  582. {
  583. if (!await e.MoveNextAsync().ConfigureAwait(false))
  584. {
  585. throw Error.NoElements();
  586. }
  587. var sum = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  588. long count = 1;
  589. while (await e.MoveNextAsync().ConfigureAwait(false))
  590. {
  591. // There is an opportunity to short-circuit here, in that if e.Current is
  592. // ever NaN then the result will always be NaN. Assuming that this case is
  593. // rare enough that not checking is the better approach generally.
  594. sum += await selector(e.Current, cancellationToken).ConfigureAwait(false);
  595. ++count;
  596. }
  597. return sum / count;
  598. }
  599. finally
  600. {
  601. await e.DisposeAsync().ConfigureAwait(false);
  602. }
  603. }
  604. #endif
  605. private static async Task<double?> AverageCore(IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  606. {
  607. var e = source.GetAsyncEnumerator(cancellationToken);
  608. try
  609. {
  610. while (await e.MoveNextAsync().ConfigureAwait(false))
  611. {
  612. var v = e.Current;
  613. if (v.HasValue)
  614. {
  615. var sum = v.GetValueOrDefault();
  616. long count = 1;
  617. checked
  618. {
  619. while (await e.MoveNextAsync().ConfigureAwait(false))
  620. {
  621. v = e.Current;
  622. if (v.HasValue)
  623. {
  624. sum += v.GetValueOrDefault();
  625. ++count;
  626. }
  627. }
  628. }
  629. return sum / count;
  630. }
  631. }
  632. }
  633. finally
  634. {
  635. await e.DisposeAsync().ConfigureAwait(false);
  636. }
  637. return null;
  638. }
  639. private static async Task<double?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  640. {
  641. var e = source.GetAsyncEnumerator(cancellationToken);
  642. try
  643. {
  644. while (await e.MoveNextAsync().ConfigureAwait(false))
  645. {
  646. var v = selector(e.Current);
  647. if (v.HasValue)
  648. {
  649. var sum = v.GetValueOrDefault();
  650. long count = 1;
  651. checked
  652. {
  653. while (await e.MoveNextAsync().ConfigureAwait(false))
  654. {
  655. v = selector(e.Current);
  656. if (v.HasValue)
  657. {
  658. sum += v.GetValueOrDefault();
  659. ++count;
  660. }
  661. }
  662. }
  663. return sum / count;
  664. }
  665. }
  666. }
  667. finally
  668. {
  669. await e.DisposeAsync().ConfigureAwait(false);
  670. }
  671. return null;
  672. }
  673. private static async Task<double?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken)
  674. {
  675. var e = source.GetAsyncEnumerator(cancellationToken);
  676. try
  677. {
  678. while (await e.MoveNextAsync().ConfigureAwait(false))
  679. {
  680. var v = await selector(e.Current).ConfigureAwait(false);
  681. if (v.HasValue)
  682. {
  683. var sum = v.GetValueOrDefault();
  684. long count = 1;
  685. checked
  686. {
  687. while (await e.MoveNextAsync().ConfigureAwait(false))
  688. {
  689. v = await selector(e.Current).ConfigureAwait(false);
  690. if (v.HasValue)
  691. {
  692. sum += v.GetValueOrDefault();
  693. ++count;
  694. }
  695. }
  696. }
  697. return sum / count;
  698. }
  699. }
  700. }
  701. finally
  702. {
  703. await e.DisposeAsync().ConfigureAwait(false);
  704. }
  705. return null;
  706. }
  707. #if !NO_DEEP_CANCELLATION
  708. private static async Task<double?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken)
  709. {
  710. var e = source.GetAsyncEnumerator(cancellationToken);
  711. try
  712. {
  713. while (await e.MoveNextAsync().ConfigureAwait(false))
  714. {
  715. var v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  716. if (v.HasValue)
  717. {
  718. var sum = v.GetValueOrDefault();
  719. long count = 1;
  720. checked
  721. {
  722. while (await e.MoveNextAsync().ConfigureAwait(false))
  723. {
  724. v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  725. if (v.HasValue)
  726. {
  727. sum += v.GetValueOrDefault();
  728. ++count;
  729. }
  730. }
  731. }
  732. return sum / count;
  733. }
  734. }
  735. }
  736. finally
  737. {
  738. await e.DisposeAsync().ConfigureAwait(false);
  739. }
  740. return null;
  741. }
  742. #endif
  743. private static async Task<float> AverageCore(IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  744. {
  745. var e = source.GetAsyncEnumerator(cancellationToken);
  746. try
  747. {
  748. if (!await e.MoveNextAsync().ConfigureAwait(false))
  749. {
  750. throw Error.NoElements();
  751. }
  752. double sum = e.Current;
  753. long count = 1;
  754. while (await e.MoveNextAsync().ConfigureAwait(false))
  755. {
  756. sum += e.Current;
  757. ++count;
  758. }
  759. return (float)(sum / count);
  760. }
  761. finally
  762. {
  763. await e.DisposeAsync().ConfigureAwait(false);
  764. }
  765. }
  766. private static async Task<float> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  767. {
  768. var e = source.GetAsyncEnumerator(cancellationToken);
  769. try
  770. {
  771. if (!await e.MoveNextAsync().ConfigureAwait(false))
  772. {
  773. throw Error.NoElements();
  774. }
  775. double sum = selector(e.Current);
  776. long count = 1;
  777. while (await e.MoveNextAsync().ConfigureAwait(false))
  778. {
  779. sum += selector(e.Current);
  780. ++count;
  781. }
  782. return (float)(sum / count);
  783. }
  784. finally
  785. {
  786. await e.DisposeAsync().ConfigureAwait(false);
  787. }
  788. }
  789. private static async Task<float> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken)
  790. {
  791. var e = source.GetAsyncEnumerator(cancellationToken);
  792. try
  793. {
  794. if (!await e.MoveNextAsync().ConfigureAwait(false))
  795. {
  796. throw Error.NoElements();
  797. }
  798. double sum = await selector(e.Current).ConfigureAwait(false);
  799. long count = 1;
  800. while (await e.MoveNextAsync().ConfigureAwait(false))
  801. {
  802. sum += await selector(e.Current).ConfigureAwait(false);
  803. ++count;
  804. }
  805. return (float)(sum / count);
  806. }
  807. finally
  808. {
  809. await e.DisposeAsync().ConfigureAwait(false);
  810. }
  811. }
  812. #if !NO_DEEP_CANCELLATION
  813. private static async Task<float> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken)
  814. {
  815. var e = source.GetAsyncEnumerator(cancellationToken);
  816. try
  817. {
  818. if (!await e.MoveNextAsync().ConfigureAwait(false))
  819. {
  820. throw Error.NoElements();
  821. }
  822. double sum = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  823. long count = 1;
  824. while (await e.MoveNextAsync().ConfigureAwait(false))
  825. {
  826. sum += await selector(e.Current, cancellationToken).ConfigureAwait(false);
  827. ++count;
  828. }
  829. return (float)(sum / count);
  830. }
  831. finally
  832. {
  833. await e.DisposeAsync().ConfigureAwait(false);
  834. }
  835. }
  836. #endif
  837. private static async Task<float?> AverageCore(IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  838. {
  839. var e = source.GetAsyncEnumerator(cancellationToken);
  840. try
  841. {
  842. while (await e.MoveNextAsync().ConfigureAwait(false))
  843. {
  844. var v = e.Current;
  845. if (v.HasValue)
  846. {
  847. double sum = v.GetValueOrDefault();
  848. long count = 1;
  849. checked
  850. {
  851. while (await e.MoveNextAsync().ConfigureAwait(false))
  852. {
  853. v = e.Current;
  854. if (v.HasValue)
  855. {
  856. sum += v.GetValueOrDefault();
  857. ++count;
  858. }
  859. }
  860. }
  861. return (float)(sum / count);
  862. }
  863. }
  864. }
  865. finally
  866. {
  867. await e.DisposeAsync().ConfigureAwait(false);
  868. }
  869. return null;
  870. }
  871. private static async Task<float?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  872. {
  873. var e = source.GetAsyncEnumerator(cancellationToken);
  874. try
  875. {
  876. while (await e.MoveNextAsync().ConfigureAwait(false))
  877. {
  878. var v = selector(e.Current);
  879. if (v.HasValue)
  880. {
  881. double sum = v.GetValueOrDefault();
  882. long count = 1;
  883. checked
  884. {
  885. while (await e.MoveNextAsync().ConfigureAwait(false))
  886. {
  887. v = selector(e.Current);
  888. if (v.HasValue)
  889. {
  890. sum += v.GetValueOrDefault();
  891. ++count;
  892. }
  893. }
  894. }
  895. return (float)(sum / count);
  896. }
  897. }
  898. }
  899. finally
  900. {
  901. await e.DisposeAsync().ConfigureAwait(false);
  902. }
  903. return null;
  904. }
  905. private static async Task<float?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken)
  906. {
  907. var e = source.GetAsyncEnumerator(cancellationToken);
  908. try
  909. {
  910. while (await e.MoveNextAsync().ConfigureAwait(false))
  911. {
  912. var v = await selector(e.Current).ConfigureAwait(false);
  913. if (v.HasValue)
  914. {
  915. double sum = v.GetValueOrDefault();
  916. long count = 1;
  917. checked
  918. {
  919. while (await e.MoveNextAsync().ConfigureAwait(false))
  920. {
  921. v = await selector(e.Current).ConfigureAwait(false);
  922. if (v.HasValue)
  923. {
  924. sum += v.GetValueOrDefault();
  925. ++count;
  926. }
  927. }
  928. }
  929. return (float)(sum / count);
  930. }
  931. }
  932. }
  933. finally
  934. {
  935. await e.DisposeAsync().ConfigureAwait(false);
  936. }
  937. return null;
  938. }
  939. #if !NO_DEEP_CANCELLATION
  940. private static async Task<float?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken)
  941. {
  942. var e = source.GetAsyncEnumerator(cancellationToken);
  943. try
  944. {
  945. while (await e.MoveNextAsync().ConfigureAwait(false))
  946. {
  947. var v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  948. if (v.HasValue)
  949. {
  950. double sum = v.GetValueOrDefault();
  951. long count = 1;
  952. checked
  953. {
  954. while (await e.MoveNextAsync().ConfigureAwait(false))
  955. {
  956. v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  957. if (v.HasValue)
  958. {
  959. sum += v.GetValueOrDefault();
  960. ++count;
  961. }
  962. }
  963. }
  964. return (float)(sum / count);
  965. }
  966. }
  967. }
  968. finally
  969. {
  970. await e.DisposeAsync().ConfigureAwait(false);
  971. }
  972. return null;
  973. }
  974. #endif
  975. private static async Task<decimal> AverageCore(IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  976. {
  977. var e = source.GetAsyncEnumerator(cancellationToken);
  978. try
  979. {
  980. if (!await e.MoveNextAsync().ConfigureAwait(false))
  981. {
  982. throw Error.NoElements();
  983. }
  984. var sum = e.Current;
  985. long count = 1;
  986. while (await e.MoveNextAsync().ConfigureAwait(false))
  987. {
  988. sum += e.Current;
  989. ++count;
  990. }
  991. return sum / count;
  992. }
  993. finally
  994. {
  995. await e.DisposeAsync().ConfigureAwait(false);
  996. }
  997. }
  998. private static async Task<decimal> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  999. {
  1000. var e = source.GetAsyncEnumerator(cancellationToken);
  1001. try
  1002. {
  1003. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1004. {
  1005. throw Error.NoElements();
  1006. }
  1007. var sum = selector(e.Current);
  1008. long count = 1;
  1009. while (await e.MoveNextAsync().ConfigureAwait(false))
  1010. {
  1011. sum += selector(e.Current);
  1012. ++count;
  1013. }
  1014. return sum / count;
  1015. }
  1016. finally
  1017. {
  1018. await e.DisposeAsync().ConfigureAwait(false);
  1019. }
  1020. }
  1021. private static async Task<decimal> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  1022. {
  1023. var e = source.GetAsyncEnumerator(cancellationToken);
  1024. try
  1025. {
  1026. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1027. {
  1028. throw Error.NoElements();
  1029. }
  1030. var sum = await selector(e.Current).ConfigureAwait(false);
  1031. long count = 1;
  1032. while (await e.MoveNextAsync().ConfigureAwait(false))
  1033. {
  1034. sum += await selector(e.Current).ConfigureAwait(false);
  1035. ++count;
  1036. }
  1037. return sum / count;
  1038. }
  1039. finally
  1040. {
  1041. await e.DisposeAsync().ConfigureAwait(false);
  1042. }
  1043. }
  1044. #if !NO_DEEP_CANCELLATION
  1045. private static async Task<decimal> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  1046. {
  1047. var e = source.GetAsyncEnumerator(cancellationToken);
  1048. try
  1049. {
  1050. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1051. {
  1052. throw Error.NoElements();
  1053. }
  1054. var sum = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1055. long count = 1;
  1056. while (await e.MoveNextAsync().ConfigureAwait(false))
  1057. {
  1058. sum += await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1059. ++count;
  1060. }
  1061. return sum / count;
  1062. }
  1063. finally
  1064. {
  1065. await e.DisposeAsync().ConfigureAwait(false);
  1066. }
  1067. }
  1068. #endif
  1069. private static async Task<decimal?> AverageCore(IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  1070. {
  1071. var e = source.GetAsyncEnumerator(cancellationToken);
  1072. try
  1073. {
  1074. while (await e.MoveNextAsync().ConfigureAwait(false))
  1075. {
  1076. var v = e.Current;
  1077. if (v.HasValue)
  1078. {
  1079. var sum = v.GetValueOrDefault();
  1080. long count = 1;
  1081. while (await e.MoveNextAsync().ConfigureAwait(false))
  1082. {
  1083. v = e.Current;
  1084. if (v.HasValue)
  1085. {
  1086. sum += v.GetValueOrDefault();
  1087. ++count;
  1088. }
  1089. }
  1090. return sum / count;
  1091. }
  1092. }
  1093. }
  1094. finally
  1095. {
  1096. await e.DisposeAsync().ConfigureAwait(false);
  1097. }
  1098. return null;
  1099. }
  1100. private static async Task<decimal?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  1101. {
  1102. var e = source.GetAsyncEnumerator(cancellationToken);
  1103. try
  1104. {
  1105. while (await e.MoveNextAsync().ConfigureAwait(false))
  1106. {
  1107. var v = selector(e.Current);
  1108. if (v.HasValue)
  1109. {
  1110. var sum = v.GetValueOrDefault();
  1111. long count = 1;
  1112. while (await e.MoveNextAsync().ConfigureAwait(false))
  1113. {
  1114. v = selector(e.Current);
  1115. if (v.HasValue)
  1116. {
  1117. sum += v.GetValueOrDefault();
  1118. ++count;
  1119. }
  1120. }
  1121. return sum / count;
  1122. }
  1123. }
  1124. }
  1125. finally
  1126. {
  1127. await e.DisposeAsync().ConfigureAwait(false);
  1128. }
  1129. return null;
  1130. }
  1131. private static async Task<decimal?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  1132. {
  1133. var e = source.GetAsyncEnumerator(cancellationToken);
  1134. try
  1135. {
  1136. while (await e.MoveNextAsync().ConfigureAwait(false))
  1137. {
  1138. var v = await selector(e.Current).ConfigureAwait(false);
  1139. if (v.HasValue)
  1140. {
  1141. var sum = v.GetValueOrDefault();
  1142. long count = 1;
  1143. while (await e.MoveNextAsync().ConfigureAwait(false))
  1144. {
  1145. v = await selector(e.Current).ConfigureAwait(false);
  1146. if (v.HasValue)
  1147. {
  1148. sum += v.GetValueOrDefault();
  1149. ++count;
  1150. }
  1151. }
  1152. return sum / count;
  1153. }
  1154. }
  1155. }
  1156. finally
  1157. {
  1158. await e.DisposeAsync().ConfigureAwait(false);
  1159. }
  1160. return null;
  1161. }
  1162. #if !NO_DEEP_CANCELLATION
  1163. private static async Task<decimal?> AverageCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  1164. {
  1165. var e = source.GetAsyncEnumerator(cancellationToken);
  1166. try
  1167. {
  1168. while (await e.MoveNextAsync().ConfigureAwait(false))
  1169. {
  1170. var v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1171. if (v.HasValue)
  1172. {
  1173. var sum = v.GetValueOrDefault();
  1174. long count = 1;
  1175. while (await e.MoveNextAsync().ConfigureAwait(false))
  1176. {
  1177. v = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1178. if (v.HasValue)
  1179. {
  1180. sum += v.GetValueOrDefault();
  1181. ++count;
  1182. }
  1183. }
  1184. return sum / count;
  1185. }
  1186. }
  1187. }
  1188. finally
  1189. {
  1190. await e.DisposeAsync().ConfigureAwait(false);
  1191. }
  1192. return null;
  1193. }
  1194. #endif
  1195. }
  1196. }