Max.Primitive.cs 44 KB

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