1
0

Max.Primitive.cs 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  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, ValueTask<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, ValueTask<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, ValueTask<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, ValueTask<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, ValueTask<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, ValueTask<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, ValueTask<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, ValueTask<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, ValueTask<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, ValueTask<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. #if !NO_DEEP_CANCELLATION
  1167. private static async Task<int> MaxCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken)
  1168. {
  1169. int value;
  1170. var e = source.GetAsyncEnumerator(cancellationToken);
  1171. try
  1172. {
  1173. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1174. {
  1175. throw Error.NoElements();
  1176. }
  1177. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1178. while (await e.MoveNextAsync().ConfigureAwait(false))
  1179. {
  1180. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1181. if (x > value)
  1182. {
  1183. value = x;
  1184. }
  1185. }
  1186. }
  1187. finally
  1188. {
  1189. await e.DisposeAsync().ConfigureAwait(false);
  1190. }
  1191. return value;
  1192. }
  1193. private static async Task<int?> MaxCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken)
  1194. {
  1195. int? value = null;
  1196. var e = source.GetAsyncEnumerator(cancellationToken);
  1197. try
  1198. {
  1199. do
  1200. {
  1201. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1202. {
  1203. return value;
  1204. }
  1205. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1206. }
  1207. while (!value.HasValue);
  1208. var valueVal = value.GetValueOrDefault();
  1209. if (valueVal >= 0)
  1210. {
  1211. // We can fast-path this case where we know HasValue will
  1212. // never affect the outcome, without constantly checking
  1213. // if we're in such a state. Similar fast-paths could
  1214. // be done for other cases, but as all-positive
  1215. // or mostly-positive integer values are quite common in real-world
  1216. // uses, it's only been done in this direction for int? and long?.
  1217. while (await e.MoveNextAsync().ConfigureAwait(false))
  1218. {
  1219. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1220. var x = cur.GetValueOrDefault();
  1221. if (x > valueVal)
  1222. {
  1223. valueVal = x;
  1224. value = cur;
  1225. }
  1226. }
  1227. }
  1228. else
  1229. {
  1230. while (await e.MoveNextAsync().ConfigureAwait(false))
  1231. {
  1232. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1233. var x = cur.GetValueOrDefault();
  1234. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1235. // unless nulls either never happen or always happen.
  1236. if (cur.HasValue & x > valueVal)
  1237. {
  1238. valueVal = x;
  1239. value = cur;
  1240. }
  1241. }
  1242. }
  1243. }
  1244. finally
  1245. {
  1246. await e.DisposeAsync().ConfigureAwait(false);
  1247. }
  1248. return value;
  1249. }
  1250. private static async Task<long> MaxCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken)
  1251. {
  1252. long value;
  1253. var e = source.GetAsyncEnumerator(cancellationToken);
  1254. try
  1255. {
  1256. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1257. {
  1258. throw Error.NoElements();
  1259. }
  1260. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1261. while (await e.MoveNextAsync().ConfigureAwait(false))
  1262. {
  1263. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1264. if (x > value)
  1265. {
  1266. value = x;
  1267. }
  1268. }
  1269. }
  1270. finally
  1271. {
  1272. await e.DisposeAsync().ConfigureAwait(false);
  1273. }
  1274. return value;
  1275. }
  1276. private static async Task<long?> MaxCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken)
  1277. {
  1278. long? value = null;
  1279. var e = source.GetAsyncEnumerator(cancellationToken);
  1280. try
  1281. {
  1282. do
  1283. {
  1284. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1285. {
  1286. return value;
  1287. }
  1288. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1289. }
  1290. while (!value.HasValue);
  1291. var valueVal = value.GetValueOrDefault();
  1292. if (valueVal >= 0)
  1293. {
  1294. while (await e.MoveNextAsync().ConfigureAwait(false))
  1295. {
  1296. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1297. var x = cur.GetValueOrDefault();
  1298. if (x > valueVal)
  1299. {
  1300. valueVal = x;
  1301. value = cur;
  1302. }
  1303. }
  1304. }
  1305. else
  1306. {
  1307. while (await e.MoveNextAsync().ConfigureAwait(false))
  1308. {
  1309. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1310. var x = cur.GetValueOrDefault();
  1311. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1312. // unless nulls either never happen or always happen.
  1313. if (cur.HasValue & x > valueVal)
  1314. {
  1315. valueVal = x;
  1316. value = cur;
  1317. }
  1318. }
  1319. }
  1320. }
  1321. finally
  1322. {
  1323. await e.DisposeAsync().ConfigureAwait(false);
  1324. }
  1325. return value;
  1326. }
  1327. private static async Task<float> MaxCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken)
  1328. {
  1329. float value;
  1330. var e = source.GetAsyncEnumerator(cancellationToken);
  1331. try
  1332. {
  1333. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1334. {
  1335. throw Error.NoElements();
  1336. }
  1337. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1338. while (float.IsNaN(value))
  1339. {
  1340. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1341. {
  1342. return value;
  1343. }
  1344. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1345. }
  1346. while (await e.MoveNextAsync().ConfigureAwait(false))
  1347. {
  1348. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1349. if (x > value)
  1350. {
  1351. value = x;
  1352. }
  1353. }
  1354. }
  1355. finally
  1356. {
  1357. await e.DisposeAsync().ConfigureAwait(false);
  1358. }
  1359. return value;
  1360. }
  1361. private static async Task<float?> MaxCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken)
  1362. {
  1363. float? value = null;
  1364. var e = source.GetAsyncEnumerator(cancellationToken);
  1365. try
  1366. {
  1367. do
  1368. {
  1369. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1370. {
  1371. return value;
  1372. }
  1373. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1374. }
  1375. while (!value.HasValue);
  1376. var valueVal = value.GetValueOrDefault();
  1377. while (float.IsNaN(valueVal))
  1378. {
  1379. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1380. {
  1381. return value;
  1382. }
  1383. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1384. if (cur.HasValue)
  1385. {
  1386. valueVal = (value = cur).GetValueOrDefault();
  1387. }
  1388. }
  1389. while (await e.MoveNextAsync().ConfigureAwait(false))
  1390. {
  1391. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1392. var x = cur.GetValueOrDefault();
  1393. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1394. // unless nulls either never happen or always happen.
  1395. if (cur.HasValue & x > valueVal)
  1396. {
  1397. valueVal = x;
  1398. value = cur;
  1399. }
  1400. }
  1401. }
  1402. finally
  1403. {
  1404. await e.DisposeAsync().ConfigureAwait(false);
  1405. }
  1406. return value;
  1407. }
  1408. private static async Task<double> MaxCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken)
  1409. {
  1410. double value;
  1411. var e = source.GetAsyncEnumerator(cancellationToken);
  1412. try
  1413. {
  1414. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1415. {
  1416. throw Error.NoElements();
  1417. }
  1418. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1419. // As described in a comment on Min(IAsyncEnumerable<double>) NaN is ordered
  1420. // less than all other values. We need to do explicit checks to ensure this, but
  1421. // once we've found a value that is not NaN we need no longer worry about it,
  1422. // so first loop until such a value is found (or not, as the case may be).
  1423. while (double.IsNaN(value))
  1424. {
  1425. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1426. {
  1427. return value;
  1428. }
  1429. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1430. }
  1431. while (await e.MoveNextAsync().ConfigureAwait(false))
  1432. {
  1433. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1434. if (x > value)
  1435. {
  1436. value = x;
  1437. }
  1438. }
  1439. }
  1440. finally
  1441. {
  1442. await e.DisposeAsync().ConfigureAwait(false);
  1443. }
  1444. return value;
  1445. }
  1446. private static async Task<double?> MaxCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken)
  1447. {
  1448. double? value = null;
  1449. var e = source.GetAsyncEnumerator(cancellationToken);
  1450. try
  1451. {
  1452. do
  1453. {
  1454. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1455. {
  1456. return value;
  1457. }
  1458. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1459. }
  1460. while (!value.HasValue);
  1461. var valueVal = value.GetValueOrDefault();
  1462. while (double.IsNaN(valueVal))
  1463. {
  1464. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1465. {
  1466. return value;
  1467. }
  1468. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1469. if (cur.HasValue)
  1470. {
  1471. valueVal = (value = cur).GetValueOrDefault();
  1472. }
  1473. }
  1474. while (await e.MoveNextAsync().ConfigureAwait(false))
  1475. {
  1476. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1477. var x = cur.GetValueOrDefault();
  1478. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1479. // unless nulls either never happen or always happen.
  1480. if (cur.HasValue & x > valueVal)
  1481. {
  1482. valueVal = x;
  1483. value = cur;
  1484. }
  1485. }
  1486. }
  1487. finally
  1488. {
  1489. await e.DisposeAsync().ConfigureAwait(false);
  1490. }
  1491. return value;
  1492. }
  1493. private static async Task<decimal> MaxCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  1494. {
  1495. decimal value;
  1496. var e = source.GetAsyncEnumerator(cancellationToken);
  1497. try
  1498. {
  1499. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1500. {
  1501. throw Error.NoElements();
  1502. }
  1503. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1504. while (await e.MoveNextAsync().ConfigureAwait(false))
  1505. {
  1506. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1507. if (x > value)
  1508. {
  1509. value = x;
  1510. }
  1511. }
  1512. }
  1513. finally
  1514. {
  1515. await e.DisposeAsync().ConfigureAwait(false);
  1516. }
  1517. return value;
  1518. }
  1519. private static async Task<decimal?> MaxCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  1520. {
  1521. decimal? value = null;
  1522. var e = source.GetAsyncEnumerator(cancellationToken);
  1523. try
  1524. {
  1525. do
  1526. {
  1527. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1528. {
  1529. return value;
  1530. }
  1531. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1532. }
  1533. while (!value.HasValue);
  1534. var valueVal = value.GetValueOrDefault();
  1535. while (await e.MoveNextAsync().ConfigureAwait(false))
  1536. {
  1537. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1538. var x = cur.GetValueOrDefault();
  1539. if (cur.HasValue && x > valueVal)
  1540. {
  1541. valueVal = x;
  1542. value = cur;
  1543. }
  1544. }
  1545. }
  1546. finally
  1547. {
  1548. await e.DisposeAsync().ConfigureAwait(false);
  1549. }
  1550. return value;
  1551. }
  1552. #endif
  1553. }
  1554. }