Average.Generated.tt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. <#@ template debug="false" hostspecific="false" language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ import namespace="System.Linq" #>
  4. <#@ import namespace="System.Text" #>
  5. <#@ import namespace="System.Collections.Generic" #>
  6. <#@ output extension=".cs" #>
  7. // Licensed to the .NET Foundation under one or more agreements.
  8. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  9. // See the LICENSE file in the project root for more information.
  10. using System.Collections.Generic;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace System.Linq
  14. {
  15. public static partial class AsyncEnumerable
  16. {
  17. <#
  18. var os = new[]
  19. {
  20. new { type = "int", res = "double", sum = "long" },
  21. new { type = "long", res = "double", sum = "long" },
  22. new { type = "float", res = "float", sum = "double" },
  23. new { type = "double", res = "double", sum = "double" },
  24. new { type = "decimal", res = "decimal", sum = "decimal" },
  25. new { type = "int?", res = "double?", sum = "long" },
  26. new { type = "long?", res = "double?", sum = "long" },
  27. new { type = "float?", res = "float?", sum = "double" },
  28. new { type = "double?", res = "double?", sum = "double" },
  29. new { type = "decimal?", res = "decimal?", sum = "decimal" },
  30. };
  31. foreach (var o in os)
  32. {
  33. var isNullable = o.type.EndsWith("?");
  34. var t = o.type.TrimEnd('?');
  35. string res = "";
  36. if (t == "int" || t == "long")
  37. res = "(double)sum / count";
  38. else if (t == "double" || t == "decimal")
  39. res = "sum / count";
  40. else if (t == "float")
  41. res = "(float)(sum / count)";
  42. #>
  43. public static Task<<#=o.res#>> AverageAsync(this IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken = default)
  44. {
  45. if (source == null)
  46. throw Error.ArgumentNull(nameof(source));
  47. return Core(source, cancellationToken);
  48. async Task<<#=o.res#>> Core(IAsyncEnumerable<<#=o.type#>> _source, CancellationToken _cancellationToken)
  49. {
  50. <#
  51. if (isNullable)
  52. {
  53. #>
  54. #if CSHARP8
  55. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  56. {
  57. while (await e.MoveNextAsync())
  58. {
  59. var v = e.Current;
  60. if (v.HasValue)
  61. {
  62. <#=o.sum#> sum = v.GetValueOrDefault();
  63. long count = 1;
  64. checked
  65. {
  66. while (await e.MoveNextAsync())
  67. {
  68. v = e.Current;
  69. if (v.HasValue)
  70. {
  71. sum += v.GetValueOrDefault();
  72. ++count;
  73. }
  74. }
  75. }
  76. return <#=res#>;
  77. }
  78. }
  79. }
  80. #else
  81. var e = _source.GetAsyncEnumerator(_cancellationToken);
  82. try
  83. {
  84. while (await e.MoveNextAsync().ConfigureAwait(false))
  85. {
  86. var v = e.Current;
  87. if (v.HasValue)
  88. {
  89. <#=o.sum#> sum = v.GetValueOrDefault();
  90. long count = 1;
  91. checked
  92. {
  93. while (await e.MoveNextAsync().ConfigureAwait(false))
  94. {
  95. v = e.Current;
  96. if (v.HasValue)
  97. {
  98. sum += v.GetValueOrDefault();
  99. ++count;
  100. }
  101. }
  102. }
  103. return <#=res#>;
  104. }
  105. }
  106. }
  107. finally
  108. {
  109. await e.DisposeAsync().ConfigureAwait(false);
  110. }
  111. #endif
  112. return null;
  113. <#
  114. }
  115. else
  116. {
  117. #>
  118. #if CSHARP8
  119. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  120. {
  121. if (!await e.MoveNextAsync())
  122. {
  123. throw Error.NoElements();
  124. }
  125. <#=o.sum#> sum = e.Current;
  126. long count = 1;
  127. checked
  128. {
  129. while (await e.MoveNextAsync())
  130. {
  131. sum += e.Current;
  132. ++count;
  133. }
  134. }
  135. return <#=res#>;
  136. }
  137. #else
  138. var e = _source.GetAsyncEnumerator(_cancellationToken);
  139. try
  140. {
  141. if (!await e.MoveNextAsync().ConfigureAwait(false))
  142. {
  143. throw Error.NoElements();
  144. }
  145. <#=o.sum#> sum = e.Current;
  146. long count = 1;
  147. checked
  148. {
  149. while (await e.MoveNextAsync().ConfigureAwait(false))
  150. {
  151. sum += e.Current;
  152. ++count;
  153. }
  154. }
  155. return <#=res#>;
  156. }
  157. finally
  158. {
  159. await e.DisposeAsync().ConfigureAwait(false);
  160. }
  161. #endif
  162. <#
  163. }
  164. #>
  165. }
  166. }
  167. public static Task<<#=o.res#>> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken = default)
  168. {
  169. if (source == null)
  170. throw Error.ArgumentNull(nameof(source));
  171. if (selector == null)
  172. throw Error.ArgumentNull(nameof(selector));
  173. return Core(source, selector, cancellationToken);
  174. async Task<<#=o.res#>> Core(IAsyncEnumerable<TSource> _source, Func<TSource, <#=o.type#>> _selector, CancellationToken _cancellationToken)
  175. {
  176. <#
  177. if (isNullable)
  178. {
  179. #>
  180. #if CSHARP8
  181. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  182. {
  183. while (await e.MoveNextAsync())
  184. {
  185. var v = _selector(e.Current);
  186. if (v.HasValue)
  187. {
  188. <#=o.sum#> sum = v.GetValueOrDefault();
  189. long count = 1;
  190. checked
  191. {
  192. while (await e.MoveNextAsync())
  193. {
  194. v = _selector(e.Current);
  195. if (v.HasValue)
  196. {
  197. sum += v.GetValueOrDefault();
  198. ++count;
  199. }
  200. }
  201. }
  202. return <#=res#>;
  203. }
  204. }
  205. }
  206. #else
  207. var e = _source.GetAsyncEnumerator(_cancellationToken);
  208. try
  209. {
  210. while (await e.MoveNextAsync().ConfigureAwait(false))
  211. {
  212. var v = _selector(e.Current);
  213. if (v.HasValue)
  214. {
  215. <#=o.sum#> sum = v.GetValueOrDefault();
  216. long count = 1;
  217. checked
  218. {
  219. while (await e.MoveNextAsync().ConfigureAwait(false))
  220. {
  221. v = _selector(e.Current);
  222. if (v.HasValue)
  223. {
  224. sum += v.GetValueOrDefault();
  225. ++count;
  226. }
  227. }
  228. }
  229. return <#=res#>;
  230. }
  231. }
  232. }
  233. finally
  234. {
  235. await e.DisposeAsync().ConfigureAwait(false);
  236. }
  237. #endif
  238. return null;
  239. <#
  240. }
  241. else
  242. {
  243. #>
  244. #if CSHARP8
  245. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  246. {
  247. if (!await e.MoveNextAsync())
  248. {
  249. throw Error.NoElements();
  250. }
  251. <#=o.sum#> sum = _selector(e.Current);
  252. long count = 1;
  253. checked
  254. {
  255. while (await e.MoveNextAsync())
  256. {
  257. sum += _selector(e.Current);
  258. ++count;
  259. }
  260. }
  261. return <#=res#>;
  262. }
  263. #else
  264. var e = _source.GetAsyncEnumerator(_cancellationToken);
  265. try
  266. {
  267. if (!await e.MoveNextAsync().ConfigureAwait(false))
  268. {
  269. throw Error.NoElements();
  270. }
  271. <#=o.sum#> sum = _selector(e.Current);
  272. long count = 1;
  273. checked
  274. {
  275. while (await e.MoveNextAsync().ConfigureAwait(false))
  276. {
  277. sum += _selector(e.Current);
  278. ++count;
  279. }
  280. }
  281. return <#=res#>;
  282. }
  283. finally
  284. {
  285. await e.DisposeAsync().ConfigureAwait(false);
  286. }
  287. #endif
  288. <#
  289. }
  290. #>
  291. }
  292. }
  293. public static Task<<#=o.res#>> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  294. {
  295. if (source == null)
  296. throw Error.ArgumentNull(nameof(source));
  297. if (selector == null)
  298. throw Error.ArgumentNull(nameof(selector));
  299. return Core(source, selector, cancellationToken);
  300. async Task<<#=o.res#>> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<<#=o.type#>>> _selector, CancellationToken _cancellationToken)
  301. {
  302. <#
  303. if (isNullable)
  304. {
  305. #>
  306. #if CSHARP8
  307. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  308. {
  309. while (await e.MoveNextAsync())
  310. {
  311. var v = await _selector(e.Current).ConfigureAwait(false);
  312. if (v.HasValue)
  313. {
  314. <#=o.sum#> sum = v.GetValueOrDefault();
  315. long count = 1;
  316. checked
  317. {
  318. while (await e.MoveNextAsync())
  319. {
  320. v = await _selector(e.Current).ConfigureAwait(false);
  321. if (v.HasValue)
  322. {
  323. sum += v.GetValueOrDefault();
  324. ++count;
  325. }
  326. }
  327. }
  328. return <#=res#>;
  329. }
  330. }
  331. }
  332. #else
  333. var e = _source.GetAsyncEnumerator(_cancellationToken);
  334. try
  335. {
  336. while (await e.MoveNextAsync().ConfigureAwait(false))
  337. {
  338. var v = await _selector(e.Current).ConfigureAwait(false);
  339. if (v.HasValue)
  340. {
  341. <#=o.sum#> sum = v.GetValueOrDefault();
  342. long count = 1;
  343. checked
  344. {
  345. while (await e.MoveNextAsync().ConfigureAwait(false))
  346. {
  347. v = await _selector(e.Current).ConfigureAwait(false);
  348. if (v.HasValue)
  349. {
  350. sum += v.GetValueOrDefault();
  351. ++count;
  352. }
  353. }
  354. }
  355. return <#=res#>;
  356. }
  357. }
  358. }
  359. finally
  360. {
  361. await e.DisposeAsync().ConfigureAwait(false);
  362. }
  363. #endif
  364. return null;
  365. <#
  366. }
  367. else
  368. {
  369. #>
  370. #if CSHARP8
  371. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  372. {
  373. if (!await e.MoveNextAsync())
  374. {
  375. throw Error.NoElements();
  376. }
  377. <#=o.sum#> sum = await _selector(e.Current).ConfigureAwait(false);
  378. long count = 1;
  379. checked
  380. {
  381. while (await e.MoveNextAsync())
  382. {
  383. sum += await _selector(e.Current).ConfigureAwait(false);
  384. ++count;
  385. }
  386. }
  387. return <#=res#>;
  388. }
  389. #else
  390. var e = _source.GetAsyncEnumerator(_cancellationToken);
  391. try
  392. {
  393. if (!await e.MoveNextAsync().ConfigureAwait(false))
  394. {
  395. throw Error.NoElements();
  396. }
  397. <#=o.sum#> sum = await _selector(e.Current).ConfigureAwait(false);
  398. long count = 1;
  399. checked
  400. {
  401. while (await e.MoveNextAsync().ConfigureAwait(false))
  402. {
  403. sum += await _selector(e.Current).ConfigureAwait(false);
  404. ++count;
  405. }
  406. }
  407. return <#=res#>;
  408. }
  409. finally
  410. {
  411. await e.DisposeAsync().ConfigureAwait(false);
  412. }
  413. #endif
  414. <#
  415. }
  416. #>
  417. }
  418. }
  419. #if !NO_DEEP_CANCELLATION
  420. public static Task<<#=o.res#>> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  421. {
  422. if (source == null)
  423. throw Error.ArgumentNull(nameof(source));
  424. if (selector == null)
  425. throw Error.ArgumentNull(nameof(selector));
  426. return Core(source, selector, cancellationToken);
  427. async Task<<#=o.res#>> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> _selector, CancellationToken _cancellationToken)
  428. {
  429. <#
  430. if (isNullable)
  431. {
  432. #>
  433. #if CSHARP8
  434. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  435. {
  436. while (await e.MoveNextAsync())
  437. {
  438. var v = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  439. if (v.HasValue)
  440. {
  441. <#=o.sum#> sum = v.GetValueOrDefault();
  442. long count = 1;
  443. checked
  444. {
  445. while (await e.MoveNextAsync())
  446. {
  447. v = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  448. if (v.HasValue)
  449. {
  450. sum += v.GetValueOrDefault();
  451. ++count;
  452. }
  453. }
  454. }
  455. return <#=res#>;
  456. }
  457. }
  458. }
  459. #else
  460. var e = _source.GetAsyncEnumerator(_cancellationToken);
  461. try
  462. {
  463. while (await e.MoveNextAsync().ConfigureAwait(false))
  464. {
  465. var v = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  466. if (v.HasValue)
  467. {
  468. <#=o.sum#> sum = v.GetValueOrDefault();
  469. long count = 1;
  470. checked
  471. {
  472. while (await e.MoveNextAsync().ConfigureAwait(false))
  473. {
  474. v = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  475. if (v.HasValue)
  476. {
  477. sum += v.GetValueOrDefault();
  478. ++count;
  479. }
  480. }
  481. }
  482. return <#=res#>;
  483. }
  484. }
  485. }
  486. finally
  487. {
  488. await e.DisposeAsync().ConfigureAwait(false);
  489. }
  490. #endif
  491. return null;
  492. <#
  493. }
  494. else
  495. {
  496. #>
  497. #if CSHARP8
  498. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  499. {
  500. if (!await e.MoveNextAsync())
  501. {
  502. throw Error.NoElements();
  503. }
  504. <#=o.sum#> sum = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  505. long count = 1;
  506. checked
  507. {
  508. while (await e.MoveNextAsync())
  509. {
  510. sum += await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  511. ++count;
  512. }
  513. }
  514. return <#=res#>;
  515. }
  516. #else
  517. var e = _source.GetAsyncEnumerator(_cancellationToken);
  518. try
  519. {
  520. if (!await e.MoveNextAsync().ConfigureAwait(false))
  521. {
  522. throw Error.NoElements();
  523. }
  524. <#=o.sum#> sum = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  525. long count = 1;
  526. checked
  527. {
  528. while (await e.MoveNextAsync().ConfigureAwait(false))
  529. {
  530. sum += await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  531. ++count;
  532. }
  533. }
  534. return <#=res#>;
  535. }
  536. finally
  537. {
  538. await e.DisposeAsync().ConfigureAwait(false);
  539. }
  540. #endif
  541. <#
  542. }
  543. #>
  544. }
  545. }
  546. #endif
  547. <#
  548. }
  549. #>
  550. }
  551. }