Average.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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("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("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("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("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("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("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("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("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("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("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("source");
  77. if (selector == null)
  78. throw new ArgumentNullException("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("source");
  85. if (selector == null)
  86. throw new ArgumentNullException("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("source");
  93. if (selector == null)
  94. throw new ArgumentNullException("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("source");
  101. if (selector == null)
  102. throw new ArgumentNullException("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("source");
  109. if (selector == null)
  110. throw new ArgumentNullException("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("source");
  117. if (selector == null)
  118. throw new ArgumentNullException("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("source");
  125. if (selector == null)
  126. throw new ArgumentNullException("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("source");
  133. if (selector == null)
  134. throw new ArgumentNullException("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("source");
  141. if (selector == null)
  142. throw new ArgumentNullException("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("source");
  149. if (selector == null)
  150. throw new ArgumentNullException("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. using (var e = source.GetEnumerator())
  306. {
  307. if (!await e.MoveNext(cancellationToken)
  308. .ConfigureAwait(false))
  309. {
  310. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  311. }
  312. long sum = e.Current;
  313. long count = 1;
  314. checked
  315. {
  316. while (await e.MoveNext(cancellationToken)
  317. .ConfigureAwait(false))
  318. {
  319. sum += e.Current;
  320. ++count;
  321. }
  322. }
  323. return (double)sum/count;
  324. }
  325. }
  326. private static async Task<double?> Average_(IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  327. {
  328. using (var e = source.GetEnumerator())
  329. {
  330. while (await e.MoveNext(cancellationToken)
  331. .ConfigureAwait(false))
  332. {
  333. var v = e.Current;
  334. if (v.HasValue)
  335. {
  336. long sum = v.GetValueOrDefault();
  337. long count = 1;
  338. checked
  339. {
  340. while (await e.MoveNext(cancellationToken)
  341. .ConfigureAwait(false))
  342. {
  343. v = e.Current;
  344. if (v.HasValue)
  345. {
  346. sum += v.GetValueOrDefault();
  347. ++count;
  348. }
  349. }
  350. }
  351. return (double)sum/count;
  352. }
  353. }
  354. }
  355. return null;
  356. }
  357. private static async Task<double> Average_(IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  358. {
  359. using (var e = source.GetEnumerator())
  360. {
  361. if (!await e.MoveNext(cancellationToken)
  362. .ConfigureAwait(false))
  363. {
  364. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  365. }
  366. var sum = e.Current;
  367. long count = 1;
  368. checked
  369. {
  370. while (await e.MoveNext(cancellationToken)
  371. .ConfigureAwait(false))
  372. {
  373. sum += e.Current;
  374. ++count;
  375. }
  376. }
  377. return (double)sum/count;
  378. }
  379. }
  380. private static async Task<double?> Average_(IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  381. {
  382. using (var e = source.GetEnumerator())
  383. {
  384. while (await e.MoveNext(cancellationToken)
  385. .ConfigureAwait(false))
  386. {
  387. var v = e.Current;
  388. if (v.HasValue)
  389. {
  390. var sum = v.GetValueOrDefault();
  391. long count = 1;
  392. checked
  393. {
  394. while (await e.MoveNext(cancellationToken)
  395. .ConfigureAwait(false))
  396. {
  397. v = e.Current;
  398. if (v.HasValue)
  399. {
  400. sum += v.GetValueOrDefault();
  401. ++count;
  402. }
  403. }
  404. }
  405. return (double)sum/count;
  406. }
  407. }
  408. }
  409. return null;
  410. }
  411. private static async Task<double> Average_(IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  412. {
  413. using (var e = source.GetEnumerator())
  414. {
  415. if (!await e.MoveNext(cancellationToken)
  416. .ConfigureAwait(false))
  417. {
  418. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  419. }
  420. var sum = e.Current;
  421. long count = 1;
  422. while (await e.MoveNext(cancellationToken)
  423. .ConfigureAwait(false))
  424. {
  425. // There is an opportunity to short-circuit here, in that if e.Current is
  426. // ever NaN then the result will always be NaN. Assuming that this case is
  427. // rare enough that not checking is the better approach generally.
  428. sum += e.Current;
  429. ++count;
  430. }
  431. return sum/count;
  432. }
  433. }
  434. private static async Task<double?> Average_(IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  435. {
  436. using (var e = source.GetEnumerator())
  437. {
  438. while (await e.MoveNext(cancellationToken)
  439. .ConfigureAwait(false))
  440. {
  441. var v = e.Current;
  442. if (v.HasValue)
  443. {
  444. var sum = v.GetValueOrDefault();
  445. long count = 1;
  446. checked
  447. {
  448. while (await e.MoveNext(cancellationToken)
  449. .ConfigureAwait(false))
  450. {
  451. v = e.Current;
  452. if (v.HasValue)
  453. {
  454. sum += v.GetValueOrDefault();
  455. ++count;
  456. }
  457. }
  458. }
  459. return sum/count;
  460. }
  461. }
  462. }
  463. return null;
  464. }
  465. private static async Task<float> Average_(IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  466. {
  467. using (var e = source.GetEnumerator())
  468. {
  469. if (!await e.MoveNext(cancellationToken)
  470. .ConfigureAwait(false))
  471. {
  472. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  473. }
  474. double sum = e.Current;
  475. long count = 1;
  476. while (await e.MoveNext(cancellationToken)
  477. .ConfigureAwait(false))
  478. {
  479. sum += e.Current;
  480. ++count;
  481. }
  482. return (float)(sum/count);
  483. }
  484. }
  485. private static async Task<float?> Average_(IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  486. {
  487. using (var e = source.GetEnumerator())
  488. {
  489. while (await e.MoveNext(cancellationToken)
  490. .ConfigureAwait(false))
  491. {
  492. var v = e.Current;
  493. if (v.HasValue)
  494. {
  495. double sum = v.GetValueOrDefault();
  496. long count = 1;
  497. checked
  498. {
  499. while (await e.MoveNext(cancellationToken)
  500. .ConfigureAwait(false))
  501. {
  502. v = e.Current;
  503. if (v.HasValue)
  504. {
  505. sum += v.GetValueOrDefault();
  506. ++count;
  507. }
  508. }
  509. }
  510. return (float)(sum/count);
  511. }
  512. }
  513. }
  514. return null;
  515. }
  516. private static async Task<decimal> Average_(IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  517. {
  518. using (var e = source.GetEnumerator())
  519. {
  520. if (!await e.MoveNext(cancellationToken)
  521. .ConfigureAwait(false))
  522. {
  523. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  524. }
  525. var sum = e.Current;
  526. long count = 1;
  527. while (await e.MoveNext(cancellationToken)
  528. .ConfigureAwait(false))
  529. {
  530. sum += e.Current;
  531. ++count;
  532. }
  533. return sum/count;
  534. }
  535. }
  536. private static async Task<decimal?> Average_(IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  537. {
  538. using (var e = source.GetEnumerator())
  539. {
  540. while (await e.MoveNext(cancellationToken)
  541. .ConfigureAwait(false))
  542. {
  543. var v = e.Current;
  544. if (v.HasValue)
  545. {
  546. var sum = v.GetValueOrDefault();
  547. long count = 1;
  548. while (await e.MoveNext(cancellationToken)
  549. .ConfigureAwait(false))
  550. {
  551. v = e.Current;
  552. if (v.HasValue)
  553. {
  554. sum += v.GetValueOrDefault();
  555. ++count;
  556. }
  557. }
  558. return sum/count;
  559. }
  560. }
  561. }
  562. return null;
  563. }
  564. }
  565. }