Min.Primitive.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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> MinCore(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?> MinCore(IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  38. {
  39. int? value = null;
  40. var e = source.GetAsyncEnumerator(cancellationToken);
  41. try
  42. {
  43. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  44. // so we don't have to keep testing for nullity.
  45. do
  46. {
  47. if (!await e.MoveNextAsync().ConfigureAwait(false))
  48. {
  49. return value;
  50. }
  51. value = e.Current;
  52. }
  53. while (!value.HasValue);
  54. // Keep hold of the wrapped value, and do comparisons on that, rather than
  55. // using the lifted operation each time.
  56. var valueVal = value.GetValueOrDefault();
  57. while (await e.MoveNextAsync().ConfigureAwait(false))
  58. {
  59. var cur = e.Current;
  60. var x = cur.GetValueOrDefault();
  61. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  62. // unless nulls either never happen or always happen.
  63. if (cur.HasValue & x < valueVal)
  64. {
  65. valueVal = x;
  66. value = cur;
  67. }
  68. }
  69. }
  70. finally
  71. {
  72. await e.DisposeAsync().ConfigureAwait(false);
  73. }
  74. return value;
  75. }
  76. private static async Task<long> MinCore(IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  77. {
  78. long value;
  79. var e = source.GetAsyncEnumerator(cancellationToken);
  80. try
  81. {
  82. if (!await e.MoveNextAsync().ConfigureAwait(false))
  83. {
  84. throw Error.NoElements();
  85. }
  86. value = e.Current;
  87. while (await e.MoveNextAsync().ConfigureAwait(false))
  88. {
  89. var x = e.Current;
  90. if (x < value)
  91. {
  92. value = x;
  93. }
  94. }
  95. }
  96. finally
  97. {
  98. await e.DisposeAsync().ConfigureAwait(false);
  99. }
  100. return value;
  101. }
  102. private static async Task<long?> MinCore(IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  103. {
  104. long? value = null;
  105. var e = source.GetAsyncEnumerator(cancellationToken);
  106. try
  107. {
  108. do
  109. {
  110. if (!await e.MoveNextAsync().ConfigureAwait(false))
  111. {
  112. return value;
  113. }
  114. value = e.Current;
  115. }
  116. while (!value.HasValue);
  117. var valueVal = value.GetValueOrDefault();
  118. while (await e.MoveNextAsync().ConfigureAwait(false))
  119. {
  120. var cur = e.Current;
  121. var x = cur.GetValueOrDefault();
  122. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  123. // unless nulls either never happen or always happen.
  124. if (cur.HasValue & x < valueVal)
  125. {
  126. valueVal = x;
  127. value = cur;
  128. }
  129. }
  130. }
  131. finally
  132. {
  133. await e.DisposeAsync().ConfigureAwait(false);
  134. }
  135. return value;
  136. }
  137. private static async Task<float> MinCore(IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  138. {
  139. float value;
  140. var e = source.GetAsyncEnumerator(cancellationToken);
  141. try
  142. {
  143. if (!await e.MoveNextAsync().ConfigureAwait(false))
  144. {
  145. throw Error.NoElements();
  146. }
  147. value = e.Current;
  148. while (await e.MoveNextAsync().ConfigureAwait(false))
  149. {
  150. var x = e.Current;
  151. if (x < value)
  152. {
  153. value = x;
  154. }
  155. // Normally NaN < anything is false, as is anything < NaN
  156. // However, this leads to some irksome outcomes in Min and Max.
  157. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  158. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  159. // ordering where NaN is smaller than every value, including
  160. // negative infinity.
  161. // Not testing for NaN therefore isn't an option, but since we
  162. // can't find a smaller value, we can short-circuit.
  163. else if (float.IsNaN(x))
  164. {
  165. return x;
  166. }
  167. }
  168. }
  169. finally
  170. {
  171. await e.DisposeAsync().ConfigureAwait(false);
  172. }
  173. return value;
  174. }
  175. private static async Task<float?> MinCore(IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  176. {
  177. float? value = null;
  178. var e = source.GetAsyncEnumerator(cancellationToken);
  179. try
  180. {
  181. do
  182. {
  183. if (!await e.MoveNextAsync().ConfigureAwait(false))
  184. {
  185. return value;
  186. }
  187. value = e.Current;
  188. }
  189. while (!value.HasValue);
  190. var valueVal = value.GetValueOrDefault();
  191. while (await e.MoveNextAsync().ConfigureAwait(false))
  192. {
  193. var cur = e.Current;
  194. if (cur.HasValue)
  195. {
  196. var x = cur.GetValueOrDefault();
  197. if (x < valueVal)
  198. {
  199. valueVal = x;
  200. value = cur;
  201. }
  202. else if (float.IsNaN(x))
  203. {
  204. return cur;
  205. }
  206. }
  207. }
  208. }
  209. finally
  210. {
  211. await e.DisposeAsync().ConfigureAwait(false);
  212. }
  213. return value;
  214. }
  215. private static async Task<double> MinCore(IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  216. {
  217. double value;
  218. var e = source.GetAsyncEnumerator(cancellationToken);
  219. try
  220. {
  221. if (!await e.MoveNextAsync().ConfigureAwait(false))
  222. {
  223. throw Error.NoElements();
  224. }
  225. value = e.Current;
  226. while (await e.MoveNextAsync().ConfigureAwait(false))
  227. {
  228. var x = e.Current;
  229. if (x < value)
  230. {
  231. value = x;
  232. }
  233. else if (double.IsNaN(x))
  234. {
  235. return x;
  236. }
  237. }
  238. }
  239. finally
  240. {
  241. await e.DisposeAsync().ConfigureAwait(false);
  242. }
  243. return value;
  244. }
  245. private static async Task<double?> MinCore(IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  246. {
  247. double? value = null;
  248. var e = source.GetAsyncEnumerator(cancellationToken);
  249. try
  250. {
  251. do
  252. {
  253. if (!await e.MoveNextAsync().ConfigureAwait(false))
  254. {
  255. return value;
  256. }
  257. value = e.Current;
  258. }
  259. while (!value.HasValue);
  260. var valueVal = value.GetValueOrDefault();
  261. while (await e.MoveNextAsync().ConfigureAwait(false))
  262. {
  263. var cur = e.Current;
  264. if (cur.HasValue)
  265. {
  266. var x = cur.GetValueOrDefault();
  267. if (x < valueVal)
  268. {
  269. valueVal = x;
  270. value = cur;
  271. }
  272. else if (double.IsNaN(x))
  273. {
  274. return cur;
  275. }
  276. }
  277. }
  278. }
  279. finally
  280. {
  281. await e.DisposeAsync().ConfigureAwait(false);
  282. }
  283. return value;
  284. }
  285. private static async Task<decimal> MinCore(IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  286. {
  287. decimal value;
  288. var e = source.GetAsyncEnumerator(cancellationToken);
  289. try
  290. {
  291. if (!await e.MoveNextAsync().ConfigureAwait(false))
  292. {
  293. throw Error.NoElements();
  294. }
  295. value = e.Current;
  296. while (await e.MoveNextAsync().ConfigureAwait(false))
  297. {
  298. var x = e.Current;
  299. if (x < value)
  300. {
  301. value = x;
  302. }
  303. }
  304. }
  305. finally
  306. {
  307. await e.DisposeAsync().ConfigureAwait(false);
  308. }
  309. return value;
  310. }
  311. private static async Task<decimal?> MinCore(IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  312. {
  313. decimal? value = null;
  314. var e = source.GetAsyncEnumerator(cancellationToken);
  315. try
  316. {
  317. do
  318. {
  319. if (!await e.MoveNextAsync().ConfigureAwait(false))
  320. {
  321. return value;
  322. }
  323. value = e.Current;
  324. }
  325. while (!value.HasValue);
  326. var valueVal = value.GetValueOrDefault();
  327. while (await e.MoveNextAsync().ConfigureAwait(false))
  328. {
  329. var cur = e.Current;
  330. var x = cur.GetValueOrDefault();
  331. if (cur.HasValue && x < valueVal)
  332. {
  333. valueVal = x;
  334. value = cur;
  335. }
  336. }
  337. }
  338. finally
  339. {
  340. await e.DisposeAsync().ConfigureAwait(false);
  341. }
  342. return value;
  343. }
  344. private static async Task<int> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  345. {
  346. int value;
  347. var e = source.GetAsyncEnumerator(cancellationToken);
  348. try
  349. {
  350. if (!await e.MoveNextAsync().ConfigureAwait(false))
  351. {
  352. throw Error.NoElements();
  353. }
  354. value = selector(e.Current);
  355. while (await e.MoveNextAsync().ConfigureAwait(false))
  356. {
  357. var x = selector(e.Current);
  358. if (x < value)
  359. {
  360. value = x;
  361. }
  362. }
  363. }
  364. finally
  365. {
  366. await e.DisposeAsync().ConfigureAwait(false);
  367. }
  368. return value;
  369. }
  370. private static async Task<int?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  371. {
  372. int? value = null;
  373. var e = source.GetAsyncEnumerator(cancellationToken);
  374. try
  375. {
  376. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  377. // so we don't have to keep testing for nullity.
  378. do
  379. {
  380. if (!await e.MoveNextAsync().ConfigureAwait(false))
  381. {
  382. return value;
  383. }
  384. value = selector(e.Current);
  385. }
  386. while (!value.HasValue);
  387. // Keep hold of the wrapped value, and do comparisons on that, rather than
  388. // using the lifted operation each time.
  389. var valueVal = value.GetValueOrDefault();
  390. while (await e.MoveNextAsync().ConfigureAwait(false))
  391. {
  392. var cur = selector(e.Current);
  393. var x = cur.GetValueOrDefault();
  394. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  395. // unless nulls either never happen or always happen.
  396. if (cur.HasValue & x < valueVal)
  397. {
  398. valueVal = x;
  399. value = cur;
  400. }
  401. }
  402. }
  403. finally
  404. {
  405. await e.DisposeAsync().ConfigureAwait(false);
  406. }
  407. return value;
  408. }
  409. private static async Task<long> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  410. {
  411. long value;
  412. var e = source.GetAsyncEnumerator(cancellationToken);
  413. try
  414. {
  415. if (!await e.MoveNextAsync().ConfigureAwait(false))
  416. {
  417. throw Error.NoElements();
  418. }
  419. value = selector(e.Current);
  420. while (await e.MoveNextAsync().ConfigureAwait(false))
  421. {
  422. var x = selector(e.Current);
  423. if (x < value)
  424. {
  425. value = x;
  426. }
  427. }
  428. }
  429. finally
  430. {
  431. await e.DisposeAsync().ConfigureAwait(false);
  432. }
  433. return value;
  434. }
  435. private static async Task<long?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  436. {
  437. long? value = null;
  438. var e = source.GetAsyncEnumerator(cancellationToken);
  439. try
  440. {
  441. do
  442. {
  443. if (!await e.MoveNextAsync().ConfigureAwait(false))
  444. {
  445. return value;
  446. }
  447. value = selector(e.Current);
  448. }
  449. while (!value.HasValue);
  450. var valueVal = value.GetValueOrDefault();
  451. while (await e.MoveNextAsync().ConfigureAwait(false))
  452. {
  453. var cur = selector(e.Current);
  454. var x = cur.GetValueOrDefault();
  455. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  456. // unless nulls either never happen or always happen.
  457. if (cur.HasValue & x < valueVal)
  458. {
  459. valueVal = x;
  460. value = cur;
  461. }
  462. }
  463. }
  464. finally
  465. {
  466. await e.DisposeAsync().ConfigureAwait(false);
  467. }
  468. return value;
  469. }
  470. private static async Task<float> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  471. {
  472. float value;
  473. var e = source.GetAsyncEnumerator(cancellationToken);
  474. try
  475. {
  476. if (!await e.MoveNextAsync().ConfigureAwait(false))
  477. {
  478. throw Error.NoElements();
  479. }
  480. value = selector(e.Current);
  481. while (await e.MoveNextAsync().ConfigureAwait(false))
  482. {
  483. var x = selector(e.Current);
  484. if (x < value)
  485. {
  486. value = x;
  487. }
  488. // Normally NaN < anything is false, as is anything < NaN
  489. // However, this leads to some irksome outcomes in Min and Max.
  490. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  491. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  492. // ordering where NaN is smaller than every value, including
  493. // negative infinity.
  494. // Not testing for NaN therefore isn't an option, but since we
  495. // can't find a smaller value, we can short-circuit.
  496. else if (float.IsNaN(x))
  497. {
  498. return x;
  499. }
  500. }
  501. }
  502. finally
  503. {
  504. await e.DisposeAsync().ConfigureAwait(false);
  505. }
  506. return value;
  507. }
  508. private static async Task<float?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  509. {
  510. float? value = null;
  511. var e = source.GetAsyncEnumerator(cancellationToken);
  512. try
  513. {
  514. do
  515. {
  516. if (!await e.MoveNextAsync().ConfigureAwait(false))
  517. {
  518. return value;
  519. }
  520. value = selector(e.Current);
  521. }
  522. while (!value.HasValue);
  523. var valueVal = value.GetValueOrDefault();
  524. while (await e.MoveNextAsync().ConfigureAwait(false))
  525. {
  526. var cur = selector(e.Current);
  527. if (cur.HasValue)
  528. {
  529. var x = cur.GetValueOrDefault();
  530. if (x < valueVal)
  531. {
  532. valueVal = x;
  533. value = cur;
  534. }
  535. else if (float.IsNaN(x))
  536. {
  537. return cur;
  538. }
  539. }
  540. }
  541. }
  542. finally
  543. {
  544. await e.DisposeAsync().ConfigureAwait(false);
  545. }
  546. return value;
  547. }
  548. private static async Task<double> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  549. {
  550. double value;
  551. var e = source.GetAsyncEnumerator(cancellationToken);
  552. try
  553. {
  554. if (!await e.MoveNextAsync().ConfigureAwait(false))
  555. {
  556. throw Error.NoElements();
  557. }
  558. value = selector(e.Current);
  559. while (await e.MoveNextAsync().ConfigureAwait(false))
  560. {
  561. var x = selector(e.Current);
  562. if (x < value)
  563. {
  564. value = x;
  565. }
  566. else if (double.IsNaN(x))
  567. {
  568. return x;
  569. }
  570. }
  571. }
  572. finally
  573. {
  574. await e.DisposeAsync().ConfigureAwait(false);
  575. }
  576. return value;
  577. }
  578. private static async Task<double?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  579. {
  580. double? value = null;
  581. var e = source.GetAsyncEnumerator(cancellationToken);
  582. try
  583. {
  584. do
  585. {
  586. if (!await e.MoveNextAsync().ConfigureAwait(false))
  587. {
  588. return value;
  589. }
  590. value = selector(e.Current);
  591. }
  592. while (!value.HasValue);
  593. var valueVal = value.GetValueOrDefault();
  594. while (await e.MoveNextAsync().ConfigureAwait(false))
  595. {
  596. var cur = selector(e.Current);
  597. if (cur.HasValue)
  598. {
  599. var x = cur.GetValueOrDefault();
  600. if (x < valueVal)
  601. {
  602. valueVal = x;
  603. value = cur;
  604. }
  605. else if (double.IsNaN(x))
  606. {
  607. return cur;
  608. }
  609. }
  610. }
  611. }
  612. finally
  613. {
  614. await e.DisposeAsync().ConfigureAwait(false);
  615. }
  616. return value;
  617. }
  618. private static async Task<decimal> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  619. {
  620. decimal value;
  621. var e = source.GetAsyncEnumerator(cancellationToken);
  622. try
  623. {
  624. if (!await e.MoveNextAsync().ConfigureAwait(false))
  625. {
  626. throw Error.NoElements();
  627. }
  628. value = selector(e.Current);
  629. while (await e.MoveNextAsync().ConfigureAwait(false))
  630. {
  631. var x = selector(e.Current);
  632. if (x < value)
  633. {
  634. value = x;
  635. }
  636. }
  637. }
  638. finally
  639. {
  640. await e.DisposeAsync().ConfigureAwait(false);
  641. }
  642. return value;
  643. }
  644. private static async Task<decimal?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  645. {
  646. decimal? value = null;
  647. var e = source.GetAsyncEnumerator(cancellationToken);
  648. try
  649. {
  650. do
  651. {
  652. if (!await e.MoveNextAsync().ConfigureAwait(false))
  653. {
  654. return value;
  655. }
  656. value = selector(e.Current);
  657. }
  658. while (!value.HasValue);
  659. var valueVal = value.GetValueOrDefault();
  660. while (await e.MoveNextAsync().ConfigureAwait(false))
  661. {
  662. var cur = selector(e.Current);
  663. var x = cur.GetValueOrDefault();
  664. if (cur.HasValue && x < valueVal)
  665. {
  666. valueVal = x;
  667. value = cur;
  668. }
  669. }
  670. }
  671. finally
  672. {
  673. await e.DisposeAsync().ConfigureAwait(false);
  674. }
  675. return value;
  676. }
  677. private static async Task<int> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<int>> selector, CancellationToken cancellationToken)
  678. {
  679. int value;
  680. var e = source.GetAsyncEnumerator(cancellationToken);
  681. try
  682. {
  683. if (!await e.MoveNextAsync().ConfigureAwait(false))
  684. {
  685. throw Error.NoElements();
  686. }
  687. value = await selector(e.Current).ConfigureAwait(false);
  688. while (await e.MoveNextAsync().ConfigureAwait(false))
  689. {
  690. var x = await selector(e.Current).ConfigureAwait(false);
  691. if (x < value)
  692. {
  693. value = x;
  694. }
  695. }
  696. }
  697. finally
  698. {
  699. await e.DisposeAsync().ConfigureAwait(false);
  700. }
  701. return value;
  702. }
  703. private static async Task<int?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<int?>> selector, CancellationToken cancellationToken)
  704. {
  705. int? value = null;
  706. var e = source.GetAsyncEnumerator(cancellationToken);
  707. try
  708. {
  709. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  710. // so we don't have to keep testing for nullity.
  711. do
  712. {
  713. if (!await e.MoveNextAsync().ConfigureAwait(false))
  714. {
  715. return value;
  716. }
  717. value = await selector(e.Current).ConfigureAwait(false);
  718. }
  719. while (!value.HasValue);
  720. // Keep hold of the wrapped value, and do comparisons on that, rather than
  721. // using the lifted operation each time.
  722. var valueVal = value.GetValueOrDefault();
  723. while (await e.MoveNextAsync().ConfigureAwait(false))
  724. {
  725. var cur = await selector(e.Current).ConfigureAwait(false);
  726. var x = cur.GetValueOrDefault();
  727. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  728. // unless nulls either never happen or always happen.
  729. if (cur.HasValue & x < valueVal)
  730. {
  731. valueVal = x;
  732. value = cur;
  733. }
  734. }
  735. }
  736. finally
  737. {
  738. await e.DisposeAsync().ConfigureAwait(false);
  739. }
  740. return value;
  741. }
  742. private static async Task<long> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<long>> selector, CancellationToken cancellationToken)
  743. {
  744. long value;
  745. var e = source.GetAsyncEnumerator(cancellationToken);
  746. try
  747. {
  748. if (!await e.MoveNextAsync().ConfigureAwait(false))
  749. {
  750. throw Error.NoElements();
  751. }
  752. value = await selector(e.Current).ConfigureAwait(false);
  753. while (await e.MoveNextAsync().ConfigureAwait(false))
  754. {
  755. var x = await selector(e.Current).ConfigureAwait(false);
  756. if (x < value)
  757. {
  758. value = x;
  759. }
  760. }
  761. }
  762. finally
  763. {
  764. await e.DisposeAsync().ConfigureAwait(false);
  765. }
  766. return value;
  767. }
  768. private static async Task<long?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<long?>> selector, CancellationToken cancellationToken)
  769. {
  770. long? value = null;
  771. var e = source.GetAsyncEnumerator(cancellationToken);
  772. try
  773. {
  774. do
  775. {
  776. if (!await e.MoveNextAsync().ConfigureAwait(false))
  777. {
  778. return value;
  779. }
  780. value = await selector(e.Current).ConfigureAwait(false);
  781. }
  782. while (!value.HasValue);
  783. var valueVal = value.GetValueOrDefault();
  784. while (await e.MoveNextAsync().ConfigureAwait(false))
  785. {
  786. var cur = await selector(e.Current).ConfigureAwait(false);
  787. var x = cur.GetValueOrDefault();
  788. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  789. // unless nulls either never happen or always happen.
  790. if (cur.HasValue & x < valueVal)
  791. {
  792. valueVal = x;
  793. value = cur;
  794. }
  795. }
  796. }
  797. finally
  798. {
  799. await e.DisposeAsync().ConfigureAwait(false);
  800. }
  801. return value;
  802. }
  803. private static async Task<float> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<float>> selector, CancellationToken cancellationToken)
  804. {
  805. float value;
  806. var e = source.GetAsyncEnumerator(cancellationToken);
  807. try
  808. {
  809. if (!await e.MoveNextAsync().ConfigureAwait(false))
  810. {
  811. throw Error.NoElements();
  812. }
  813. value = await selector(e.Current).ConfigureAwait(false);
  814. while (await e.MoveNextAsync().ConfigureAwait(false))
  815. {
  816. var x = await selector(e.Current).ConfigureAwait(false);
  817. if (x < value)
  818. {
  819. value = x;
  820. }
  821. // Normally NaN < anything is false, as is anything < NaN
  822. // However, this leads to some irksome outcomes in Min and Max.
  823. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  824. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  825. // ordering where NaN is smaller than every value, including
  826. // negative infinity.
  827. // Not testing for NaN therefore isn't an option, but since we
  828. // can't find a smaller value, we can short-circuit.
  829. else if (float.IsNaN(x))
  830. {
  831. return x;
  832. }
  833. }
  834. }
  835. finally
  836. {
  837. await e.DisposeAsync().ConfigureAwait(false);
  838. }
  839. return value;
  840. }
  841. private static async Task<float?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<float?>> selector, CancellationToken cancellationToken)
  842. {
  843. float? value = null;
  844. var e = source.GetAsyncEnumerator(cancellationToken);
  845. try
  846. {
  847. do
  848. {
  849. if (!await e.MoveNextAsync().ConfigureAwait(false))
  850. {
  851. return value;
  852. }
  853. value = await selector(e.Current).ConfigureAwait(false);
  854. }
  855. while (!value.HasValue);
  856. var valueVal = value.GetValueOrDefault();
  857. while (await e.MoveNextAsync().ConfigureAwait(false))
  858. {
  859. var cur = await selector(e.Current).ConfigureAwait(false);
  860. if (cur.HasValue)
  861. {
  862. var x = cur.GetValueOrDefault();
  863. if (x < valueVal)
  864. {
  865. valueVal = x;
  866. value = cur;
  867. }
  868. else if (float.IsNaN(x))
  869. {
  870. return cur;
  871. }
  872. }
  873. }
  874. }
  875. finally
  876. {
  877. await e.DisposeAsync().ConfigureAwait(false);
  878. }
  879. return value;
  880. }
  881. private static async Task<double> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<double>> selector, CancellationToken cancellationToken)
  882. {
  883. double value;
  884. var e = source.GetAsyncEnumerator(cancellationToken);
  885. try
  886. {
  887. if (!await e.MoveNextAsync().ConfigureAwait(false))
  888. {
  889. throw Error.NoElements();
  890. }
  891. value = await selector(e.Current).ConfigureAwait(false);
  892. while (await e.MoveNextAsync().ConfigureAwait(false))
  893. {
  894. var x = await selector(e.Current).ConfigureAwait(false);
  895. if (x < value)
  896. {
  897. value = x;
  898. }
  899. else if (double.IsNaN(x))
  900. {
  901. return x;
  902. }
  903. }
  904. }
  905. finally
  906. {
  907. await e.DisposeAsync().ConfigureAwait(false);
  908. }
  909. return value;
  910. }
  911. private static async Task<double?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<double?>> selector, CancellationToken cancellationToken)
  912. {
  913. double? value = null;
  914. var e = source.GetAsyncEnumerator(cancellationToken);
  915. try
  916. {
  917. do
  918. {
  919. if (!await e.MoveNextAsync().ConfigureAwait(false))
  920. {
  921. return value;
  922. }
  923. value = await selector(e.Current).ConfigureAwait(false);
  924. }
  925. while (!value.HasValue);
  926. var valueVal = value.GetValueOrDefault();
  927. while (await e.MoveNextAsync().ConfigureAwait(false))
  928. {
  929. var cur = await selector(e.Current).ConfigureAwait(false);
  930. if (cur.HasValue)
  931. {
  932. var x = cur.GetValueOrDefault();
  933. if (x < valueVal)
  934. {
  935. valueVal = x;
  936. value = cur;
  937. }
  938. else if (double.IsNaN(x))
  939. {
  940. return cur;
  941. }
  942. }
  943. }
  944. }
  945. finally
  946. {
  947. await e.DisposeAsync().ConfigureAwait(false);
  948. }
  949. return value;
  950. }
  951. private static async Task<decimal> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<decimal>> selector, CancellationToken cancellationToken)
  952. {
  953. decimal value;
  954. var e = source.GetAsyncEnumerator(cancellationToken);
  955. try
  956. {
  957. if (!await e.MoveNextAsync().ConfigureAwait(false))
  958. {
  959. throw Error.NoElements();
  960. }
  961. value = await selector(e.Current).ConfigureAwait(false);
  962. while (await e.MoveNextAsync().ConfigureAwait(false))
  963. {
  964. var x = await selector(e.Current).ConfigureAwait(false);
  965. if (x < value)
  966. {
  967. value = x;
  968. }
  969. }
  970. }
  971. finally
  972. {
  973. await e.DisposeAsync().ConfigureAwait(false);
  974. }
  975. return value;
  976. }
  977. private static async Task<decimal?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<decimal?>> selector, CancellationToken cancellationToken)
  978. {
  979. decimal? value = null;
  980. var e = source.GetAsyncEnumerator(cancellationToken);
  981. try
  982. {
  983. do
  984. {
  985. if (!await e.MoveNextAsync().ConfigureAwait(false))
  986. {
  987. return value;
  988. }
  989. value = await selector(e.Current).ConfigureAwait(false);
  990. }
  991. while (!value.HasValue);
  992. var valueVal = value.GetValueOrDefault();
  993. while (await e.MoveNextAsync().ConfigureAwait(false))
  994. {
  995. var cur = await selector(e.Current).ConfigureAwait(false);
  996. var x = cur.GetValueOrDefault();
  997. if (cur.HasValue && x < valueVal)
  998. {
  999. valueVal = x;
  1000. value = cur;
  1001. }
  1002. }
  1003. }
  1004. finally
  1005. {
  1006. await e.DisposeAsync().ConfigureAwait(false);
  1007. }
  1008. return value;
  1009. }
  1010. }
  1011. }