MinMax.Generated.cs 184 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686
  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. public static ValueTask<int> MaxAsync(this IAsyncEnumerable<int> source, CancellationToken cancellationToken = default)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. return Core(source, cancellationToken);
  16. static async ValueTask<int> Core(IAsyncEnumerable<int> _source, CancellationToken _cancellationToken)
  17. {
  18. int value;
  19. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  20. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  21. {
  22. if (!await e.MoveNextAsync())
  23. {
  24. throw Error.NoElements();
  25. }
  26. value = e.Current;
  27. while (await e.MoveNextAsync())
  28. {
  29. var x = e.Current;
  30. if (x > value)
  31. {
  32. value = x;
  33. }
  34. }
  35. }
  36. finally
  37. {
  38. await e.DisposeAsync();
  39. }
  40. return value;
  41. }
  42. }
  43. public static ValueTask<int> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken = default)
  44. {
  45. if (source == null)
  46. throw Error.ArgumentNull(nameof(source));
  47. if (selector == null)
  48. throw Error.ArgumentNull(nameof(selector));
  49. return Core(source, selector, cancellationToken);
  50. static async ValueTask<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, int> _selector, CancellationToken _cancellationToken)
  51. {
  52. int value;
  53. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  54. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  55. {
  56. if (!await e.MoveNextAsync())
  57. {
  58. throw Error.NoElements();
  59. }
  60. value = _selector(e.Current);
  61. while (await e.MoveNextAsync())
  62. {
  63. var x = _selector(e.Current);
  64. if (x > value)
  65. {
  66. value = x;
  67. }
  68. }
  69. }
  70. finally
  71. {
  72. await e.DisposeAsync();
  73. }
  74. return value;
  75. }
  76. }
  77. public static ValueTask<int> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  78. {
  79. if (source == null)
  80. throw Error.ArgumentNull(nameof(source));
  81. if (selector == null)
  82. throw Error.ArgumentNull(nameof(selector));
  83. return Core(source, selector, cancellationToken);
  84. static async ValueTask<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<int>> _selector, CancellationToken _cancellationToken)
  85. {
  86. int value;
  87. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  88. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  89. {
  90. if (!await e.MoveNextAsync())
  91. {
  92. throw Error.NoElements();
  93. }
  94. value = await _selector(e.Current).ConfigureAwait(false);
  95. while (await e.MoveNextAsync())
  96. {
  97. var x = await _selector(e.Current).ConfigureAwait(false);
  98. if (x > value)
  99. {
  100. value = x;
  101. }
  102. }
  103. }
  104. finally
  105. {
  106. await e.DisposeAsync();
  107. }
  108. return value;
  109. }
  110. }
  111. #if !NO_DEEP_CANCELLATION
  112. public static ValueTask<int> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  113. {
  114. if (source == null)
  115. throw Error.ArgumentNull(nameof(source));
  116. if (selector == null)
  117. throw Error.ArgumentNull(nameof(selector));
  118. return Core(source, selector, cancellationToken);
  119. static async ValueTask<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<int>> _selector, CancellationToken _cancellationToken)
  120. {
  121. int value;
  122. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  123. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  124. {
  125. if (!await e.MoveNextAsync())
  126. {
  127. throw Error.NoElements();
  128. }
  129. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  130. while (await e.MoveNextAsync())
  131. {
  132. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  133. if (x > value)
  134. {
  135. value = x;
  136. }
  137. }
  138. }
  139. finally
  140. {
  141. await e.DisposeAsync();
  142. }
  143. return value;
  144. }
  145. }
  146. #endif
  147. public static ValueTask<int?> MaxAsync(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken = default)
  148. {
  149. if (source == null)
  150. throw Error.ArgumentNull(nameof(source));
  151. return Core(source, cancellationToken);
  152. static async ValueTask<int?> Core(IAsyncEnumerable<int?> _source, CancellationToken _cancellationToken)
  153. {
  154. int? value = null;
  155. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  156. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  157. {
  158. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  159. // so we don't have to keep testing for nullity.
  160. do
  161. {
  162. if (!await e.MoveNextAsync())
  163. {
  164. return value;
  165. }
  166. value = e.Current;
  167. }
  168. while (!value.HasValue);
  169. // Keep hold of the wrapped value, and do comparisons on that, rather than
  170. // using the lifted operation each time.
  171. var valueVal = value.GetValueOrDefault();
  172. if (valueVal >= 0)
  173. {
  174. // We can fast-path this case where we know HasValue will
  175. // never affect the outcome, without constantly checking
  176. // if we're in such a state. Similar fast-paths could
  177. // be done for other cases, but as all-positive or mostly-
  178. // positive integer values are quite common in real-world
  179. // uses, it's only been done for int? and long?.
  180. while (await e.MoveNextAsync())
  181. {
  182. var cur = e.Current;
  183. var x = cur.GetValueOrDefault();
  184. if (x > valueVal)
  185. {
  186. valueVal = x;
  187. value = cur;
  188. }
  189. }
  190. }
  191. else
  192. {
  193. while (await e.MoveNextAsync())
  194. {
  195. var cur = e.Current;
  196. var x = cur.GetValueOrDefault();
  197. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  198. // unless nulls either never happen or always happen.
  199. if (cur.HasValue & x > valueVal)
  200. {
  201. valueVal = x;
  202. value = cur;
  203. }
  204. }
  205. }
  206. }
  207. finally
  208. {
  209. await e.DisposeAsync();
  210. }
  211. return value;
  212. }
  213. }
  214. public static ValueTask<int?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken = default)
  215. {
  216. if (source == null)
  217. throw Error.ArgumentNull(nameof(source));
  218. if (selector == null)
  219. throw Error.ArgumentNull(nameof(selector));
  220. return Core(source, selector, cancellationToken);
  221. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, int?> _selector, CancellationToken _cancellationToken)
  222. {
  223. int? value = null;
  224. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  225. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  226. {
  227. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  228. // so we don't have to keep testing for nullity.
  229. do
  230. {
  231. if (!await e.MoveNextAsync())
  232. {
  233. return value;
  234. }
  235. value = _selector(e.Current);
  236. }
  237. while (!value.HasValue);
  238. // Keep hold of the wrapped value, and do comparisons on that, rather than
  239. // using the lifted operation each time.
  240. var valueVal = value.GetValueOrDefault();
  241. if (valueVal >= 0)
  242. {
  243. // We can fast-path this case where we know HasValue will
  244. // never affect the outcome, without constantly checking
  245. // if we're in such a state. Similar fast-paths could
  246. // be done for other cases, but as all-positive or mostly-
  247. // positive integer values are quite common in real-world
  248. // uses, it's only been done for int? and long?.
  249. while (await e.MoveNextAsync())
  250. {
  251. var cur = _selector(e.Current);
  252. var x = cur.GetValueOrDefault();
  253. if (x > valueVal)
  254. {
  255. valueVal = x;
  256. value = cur;
  257. }
  258. }
  259. }
  260. else
  261. {
  262. while (await e.MoveNextAsync())
  263. {
  264. var cur = _selector(e.Current);
  265. var x = cur.GetValueOrDefault();
  266. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  267. // unless nulls either never happen or always happen.
  268. if (cur.HasValue & x > valueVal)
  269. {
  270. valueVal = x;
  271. value = cur;
  272. }
  273. }
  274. }
  275. }
  276. finally
  277. {
  278. await e.DisposeAsync();
  279. }
  280. return value;
  281. }
  282. }
  283. public static ValueTask<int?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  284. {
  285. if (source == null)
  286. throw Error.ArgumentNull(nameof(source));
  287. if (selector == null)
  288. throw Error.ArgumentNull(nameof(selector));
  289. return Core(source, selector, cancellationToken);
  290. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<int?>> _selector, CancellationToken _cancellationToken)
  291. {
  292. int? value = null;
  293. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  294. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  295. {
  296. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  297. // so we don't have to keep testing for nullity.
  298. do
  299. {
  300. if (!await e.MoveNextAsync())
  301. {
  302. return value;
  303. }
  304. value = await _selector(e.Current).ConfigureAwait(false);
  305. }
  306. while (!value.HasValue);
  307. // Keep hold of the wrapped value, and do comparisons on that, rather than
  308. // using the lifted operation each time.
  309. var valueVal = value.GetValueOrDefault();
  310. if (valueVal >= 0)
  311. {
  312. // We can fast-path this case where we know HasValue will
  313. // never affect the outcome, without constantly checking
  314. // if we're in such a state. Similar fast-paths could
  315. // be done for other cases, but as all-positive or mostly-
  316. // positive integer values are quite common in real-world
  317. // uses, it's only been done for int? and long?.
  318. while (await e.MoveNextAsync())
  319. {
  320. var cur = await _selector(e.Current).ConfigureAwait(false);
  321. var x = cur.GetValueOrDefault();
  322. if (x > valueVal)
  323. {
  324. valueVal = x;
  325. value = cur;
  326. }
  327. }
  328. }
  329. else
  330. {
  331. while (await e.MoveNextAsync())
  332. {
  333. var cur = await _selector(e.Current).ConfigureAwait(false);
  334. var x = cur.GetValueOrDefault();
  335. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  336. // unless nulls either never happen or always happen.
  337. if (cur.HasValue & x > valueVal)
  338. {
  339. valueVal = x;
  340. value = cur;
  341. }
  342. }
  343. }
  344. }
  345. finally
  346. {
  347. await e.DisposeAsync();
  348. }
  349. return value;
  350. }
  351. }
  352. #if !NO_DEEP_CANCELLATION
  353. public static ValueTask<int?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  354. {
  355. if (source == null)
  356. throw Error.ArgumentNull(nameof(source));
  357. if (selector == null)
  358. throw Error.ArgumentNull(nameof(selector));
  359. return Core(source, selector, cancellationToken);
  360. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<int?>> _selector, CancellationToken _cancellationToken)
  361. {
  362. int? value = null;
  363. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  364. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  365. {
  366. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  367. // so we don't have to keep testing for nullity.
  368. do
  369. {
  370. if (!await e.MoveNextAsync())
  371. {
  372. return value;
  373. }
  374. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  375. }
  376. while (!value.HasValue);
  377. // Keep hold of the wrapped value, and do comparisons on that, rather than
  378. // using the lifted operation each time.
  379. var valueVal = value.GetValueOrDefault();
  380. if (valueVal >= 0)
  381. {
  382. // We can fast-path this case where we know HasValue will
  383. // never affect the outcome, without constantly checking
  384. // if we're in such a state. Similar fast-paths could
  385. // be done for other cases, but as all-positive or mostly-
  386. // positive integer values are quite common in real-world
  387. // uses, it's only been done for int? and long?.
  388. while (await e.MoveNextAsync())
  389. {
  390. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  391. var x = cur.GetValueOrDefault();
  392. if (x > valueVal)
  393. {
  394. valueVal = x;
  395. value = cur;
  396. }
  397. }
  398. }
  399. else
  400. {
  401. while (await e.MoveNextAsync())
  402. {
  403. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  404. var x = cur.GetValueOrDefault();
  405. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  406. // unless nulls either never happen or always happen.
  407. if (cur.HasValue & x > valueVal)
  408. {
  409. valueVal = x;
  410. value = cur;
  411. }
  412. }
  413. }
  414. }
  415. finally
  416. {
  417. await e.DisposeAsync();
  418. }
  419. return value;
  420. }
  421. }
  422. #endif
  423. public static ValueTask<long> MaxAsync(this IAsyncEnumerable<long> source, CancellationToken cancellationToken = default)
  424. {
  425. if (source == null)
  426. throw Error.ArgumentNull(nameof(source));
  427. return Core(source, cancellationToken);
  428. static async ValueTask<long> Core(IAsyncEnumerable<long> _source, CancellationToken _cancellationToken)
  429. {
  430. long value;
  431. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  432. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  433. {
  434. if (!await e.MoveNextAsync())
  435. {
  436. throw Error.NoElements();
  437. }
  438. value = e.Current;
  439. while (await e.MoveNextAsync())
  440. {
  441. var x = e.Current;
  442. if (x > value)
  443. {
  444. value = x;
  445. }
  446. }
  447. }
  448. finally
  449. {
  450. await e.DisposeAsync();
  451. }
  452. return value;
  453. }
  454. }
  455. public static ValueTask<long> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken = default)
  456. {
  457. if (source == null)
  458. throw Error.ArgumentNull(nameof(source));
  459. if (selector == null)
  460. throw Error.ArgumentNull(nameof(selector));
  461. return Core(source, selector, cancellationToken);
  462. static async ValueTask<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, long> _selector, CancellationToken _cancellationToken)
  463. {
  464. long value;
  465. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  466. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  467. {
  468. if (!await e.MoveNextAsync())
  469. {
  470. throw Error.NoElements();
  471. }
  472. value = _selector(e.Current);
  473. while (await e.MoveNextAsync())
  474. {
  475. var x = _selector(e.Current);
  476. if (x > value)
  477. {
  478. value = x;
  479. }
  480. }
  481. }
  482. finally
  483. {
  484. await e.DisposeAsync();
  485. }
  486. return value;
  487. }
  488. }
  489. public static ValueTask<long> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  490. {
  491. if (source == null)
  492. throw Error.ArgumentNull(nameof(source));
  493. if (selector == null)
  494. throw Error.ArgumentNull(nameof(selector));
  495. return Core(source, selector, cancellationToken);
  496. static async ValueTask<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<long>> _selector, CancellationToken _cancellationToken)
  497. {
  498. long value;
  499. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  500. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  501. {
  502. if (!await e.MoveNextAsync())
  503. {
  504. throw Error.NoElements();
  505. }
  506. value = await _selector(e.Current).ConfigureAwait(false);
  507. while (await e.MoveNextAsync())
  508. {
  509. var x = await _selector(e.Current).ConfigureAwait(false);
  510. if (x > value)
  511. {
  512. value = x;
  513. }
  514. }
  515. }
  516. finally
  517. {
  518. await e.DisposeAsync();
  519. }
  520. return value;
  521. }
  522. }
  523. #if !NO_DEEP_CANCELLATION
  524. public static ValueTask<long> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  525. {
  526. if (source == null)
  527. throw Error.ArgumentNull(nameof(source));
  528. if (selector == null)
  529. throw Error.ArgumentNull(nameof(selector));
  530. return Core(source, selector, cancellationToken);
  531. static async ValueTask<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<long>> _selector, CancellationToken _cancellationToken)
  532. {
  533. long value;
  534. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  535. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  536. {
  537. if (!await e.MoveNextAsync())
  538. {
  539. throw Error.NoElements();
  540. }
  541. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  542. while (await e.MoveNextAsync())
  543. {
  544. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  545. if (x > value)
  546. {
  547. value = x;
  548. }
  549. }
  550. }
  551. finally
  552. {
  553. await e.DisposeAsync();
  554. }
  555. return value;
  556. }
  557. }
  558. #endif
  559. public static ValueTask<long?> MaxAsync(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken = default)
  560. {
  561. if (source == null)
  562. throw Error.ArgumentNull(nameof(source));
  563. return Core(source, cancellationToken);
  564. static async ValueTask<long?> Core(IAsyncEnumerable<long?> _source, CancellationToken _cancellationToken)
  565. {
  566. long? value = null;
  567. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  568. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  569. {
  570. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  571. // so we don't have to keep testing for nullity.
  572. do
  573. {
  574. if (!await e.MoveNextAsync())
  575. {
  576. return value;
  577. }
  578. value = e.Current;
  579. }
  580. while (!value.HasValue);
  581. // Keep hold of the wrapped value, and do comparisons on that, rather than
  582. // using the lifted operation each time.
  583. var valueVal = value.GetValueOrDefault();
  584. if (valueVal >= 0)
  585. {
  586. // We can fast-path this case where we know HasValue will
  587. // never affect the outcome, without constantly checking
  588. // if we're in such a state. Similar fast-paths could
  589. // be done for other cases, but as all-positive or mostly-
  590. // positive integer values are quite common in real-world
  591. // uses, it's only been done for int? and long?.
  592. while (await e.MoveNextAsync())
  593. {
  594. var cur = e.Current;
  595. var x = cur.GetValueOrDefault();
  596. if (x > valueVal)
  597. {
  598. valueVal = x;
  599. value = cur;
  600. }
  601. }
  602. }
  603. else
  604. {
  605. while (await e.MoveNextAsync())
  606. {
  607. var cur = e.Current;
  608. var x = cur.GetValueOrDefault();
  609. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  610. // unless nulls either never happen or always happen.
  611. if (cur.HasValue & x > valueVal)
  612. {
  613. valueVal = x;
  614. value = cur;
  615. }
  616. }
  617. }
  618. }
  619. finally
  620. {
  621. await e.DisposeAsync();
  622. }
  623. return value;
  624. }
  625. }
  626. public static ValueTask<long?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken = default)
  627. {
  628. if (source == null)
  629. throw Error.ArgumentNull(nameof(source));
  630. if (selector == null)
  631. throw Error.ArgumentNull(nameof(selector));
  632. return Core(source, selector, cancellationToken);
  633. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, long?> _selector, CancellationToken _cancellationToken)
  634. {
  635. long? value = null;
  636. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  637. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  638. {
  639. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  640. // so we don't have to keep testing for nullity.
  641. do
  642. {
  643. if (!await e.MoveNextAsync())
  644. {
  645. return value;
  646. }
  647. value = _selector(e.Current);
  648. }
  649. while (!value.HasValue);
  650. // Keep hold of the wrapped value, and do comparisons on that, rather than
  651. // using the lifted operation each time.
  652. var valueVal = value.GetValueOrDefault();
  653. if (valueVal >= 0)
  654. {
  655. // We can fast-path this case where we know HasValue will
  656. // never affect the outcome, without constantly checking
  657. // if we're in such a state. Similar fast-paths could
  658. // be done for other cases, but as all-positive or mostly-
  659. // positive integer values are quite common in real-world
  660. // uses, it's only been done for int? and long?.
  661. while (await e.MoveNextAsync())
  662. {
  663. var cur = _selector(e.Current);
  664. var x = cur.GetValueOrDefault();
  665. if (x > valueVal)
  666. {
  667. valueVal = x;
  668. value = cur;
  669. }
  670. }
  671. }
  672. else
  673. {
  674. while (await e.MoveNextAsync())
  675. {
  676. var cur = _selector(e.Current);
  677. var x = cur.GetValueOrDefault();
  678. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  679. // unless nulls either never happen or always happen.
  680. if (cur.HasValue & x > valueVal)
  681. {
  682. valueVal = x;
  683. value = cur;
  684. }
  685. }
  686. }
  687. }
  688. finally
  689. {
  690. await e.DisposeAsync();
  691. }
  692. return value;
  693. }
  694. }
  695. public static ValueTask<long?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  696. {
  697. if (source == null)
  698. throw Error.ArgumentNull(nameof(source));
  699. if (selector == null)
  700. throw Error.ArgumentNull(nameof(selector));
  701. return Core(source, selector, cancellationToken);
  702. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<long?>> _selector, CancellationToken _cancellationToken)
  703. {
  704. long? value = null;
  705. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  706. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  707. {
  708. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  709. // so we don't have to keep testing for nullity.
  710. do
  711. {
  712. if (!await e.MoveNextAsync())
  713. {
  714. return value;
  715. }
  716. value = await _selector(e.Current).ConfigureAwait(false);
  717. }
  718. while (!value.HasValue);
  719. // Keep hold of the wrapped value, and do comparisons on that, rather than
  720. // using the lifted operation each time.
  721. var valueVal = value.GetValueOrDefault();
  722. if (valueVal >= 0)
  723. {
  724. // We can fast-path this case where we know HasValue will
  725. // never affect the outcome, without constantly checking
  726. // if we're in such a state. Similar fast-paths could
  727. // be done for other cases, but as all-positive or mostly-
  728. // positive integer values are quite common in real-world
  729. // uses, it's only been done for int? and long?.
  730. while (await e.MoveNextAsync())
  731. {
  732. var cur = await _selector(e.Current).ConfigureAwait(false);
  733. var x = cur.GetValueOrDefault();
  734. if (x > valueVal)
  735. {
  736. valueVal = x;
  737. value = cur;
  738. }
  739. }
  740. }
  741. else
  742. {
  743. while (await e.MoveNextAsync())
  744. {
  745. var cur = await _selector(e.Current).ConfigureAwait(false);
  746. var x = cur.GetValueOrDefault();
  747. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  748. // unless nulls either never happen or always happen.
  749. if (cur.HasValue & x > valueVal)
  750. {
  751. valueVal = x;
  752. value = cur;
  753. }
  754. }
  755. }
  756. }
  757. finally
  758. {
  759. await e.DisposeAsync();
  760. }
  761. return value;
  762. }
  763. }
  764. #if !NO_DEEP_CANCELLATION
  765. public static ValueTask<long?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  766. {
  767. if (source == null)
  768. throw Error.ArgumentNull(nameof(source));
  769. if (selector == null)
  770. throw Error.ArgumentNull(nameof(selector));
  771. return Core(source, selector, cancellationToken);
  772. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<long?>> _selector, CancellationToken _cancellationToken)
  773. {
  774. long? value = null;
  775. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  776. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  777. {
  778. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  779. // so we don't have to keep testing for nullity.
  780. do
  781. {
  782. if (!await e.MoveNextAsync())
  783. {
  784. return value;
  785. }
  786. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  787. }
  788. while (!value.HasValue);
  789. // Keep hold of the wrapped value, and do comparisons on that, rather than
  790. // using the lifted operation each time.
  791. var valueVal = value.GetValueOrDefault();
  792. if (valueVal >= 0)
  793. {
  794. // We can fast-path this case where we know HasValue will
  795. // never affect the outcome, without constantly checking
  796. // if we're in such a state. Similar fast-paths could
  797. // be done for other cases, but as all-positive or mostly-
  798. // positive integer values are quite common in real-world
  799. // uses, it's only been done for int? and long?.
  800. while (await e.MoveNextAsync())
  801. {
  802. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  803. var x = cur.GetValueOrDefault();
  804. if (x > valueVal)
  805. {
  806. valueVal = x;
  807. value = cur;
  808. }
  809. }
  810. }
  811. else
  812. {
  813. while (await e.MoveNextAsync())
  814. {
  815. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  816. var x = cur.GetValueOrDefault();
  817. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  818. // unless nulls either never happen or always happen.
  819. if (cur.HasValue & x > valueVal)
  820. {
  821. valueVal = x;
  822. value = cur;
  823. }
  824. }
  825. }
  826. }
  827. finally
  828. {
  829. await e.DisposeAsync();
  830. }
  831. return value;
  832. }
  833. }
  834. #endif
  835. public static ValueTask<float> MaxAsync(this IAsyncEnumerable<float> source, CancellationToken cancellationToken = default)
  836. {
  837. if (source == null)
  838. throw Error.ArgumentNull(nameof(source));
  839. return Core(source, cancellationToken);
  840. static async ValueTask<float> Core(IAsyncEnumerable<float> _source, CancellationToken _cancellationToken)
  841. {
  842. float value;
  843. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  844. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  845. {
  846. if (!await e.MoveNextAsync())
  847. {
  848. throw Error.NoElements();
  849. }
  850. value = e.Current;
  851. // NaN is ordered less than all other values. We need to do explicit checks
  852. // to ensure this, but once we've found a value that is not NaN we need no
  853. // longer worry about it, so first loop until such a value is found (or not,
  854. // as the case may be).
  855. while (float.IsNaN(value))
  856. {
  857. if (!await e.MoveNextAsync())
  858. {
  859. return value;
  860. }
  861. value = e.Current;
  862. }
  863. while (await e.MoveNextAsync())
  864. {
  865. var x = e.Current;
  866. if (x > value)
  867. {
  868. value = x;
  869. }
  870. }
  871. }
  872. finally
  873. {
  874. await e.DisposeAsync();
  875. }
  876. return value;
  877. }
  878. }
  879. public static ValueTask<float> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken = default)
  880. {
  881. if (source == null)
  882. throw Error.ArgumentNull(nameof(source));
  883. if (selector == null)
  884. throw Error.ArgumentNull(nameof(selector));
  885. return Core(source, selector, cancellationToken);
  886. static async ValueTask<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, float> _selector, CancellationToken _cancellationToken)
  887. {
  888. float value;
  889. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  890. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  891. {
  892. if (!await e.MoveNextAsync())
  893. {
  894. throw Error.NoElements();
  895. }
  896. value = _selector(e.Current);
  897. // NaN is ordered less than all other values. We need to do explicit checks
  898. // to ensure this, but once we've found a value that is not NaN we need no
  899. // longer worry about it, so first loop until such a value is found (or not,
  900. // as the case may be).
  901. while (float.IsNaN(value))
  902. {
  903. if (!await e.MoveNextAsync())
  904. {
  905. return value;
  906. }
  907. value = _selector(e.Current);
  908. }
  909. while (await e.MoveNextAsync())
  910. {
  911. var x = _selector(e.Current);
  912. if (x > value)
  913. {
  914. value = x;
  915. }
  916. }
  917. }
  918. finally
  919. {
  920. await e.DisposeAsync();
  921. }
  922. return value;
  923. }
  924. }
  925. public static ValueTask<float> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  926. {
  927. if (source == null)
  928. throw Error.ArgumentNull(nameof(source));
  929. if (selector == null)
  930. throw Error.ArgumentNull(nameof(selector));
  931. return Core(source, selector, cancellationToken);
  932. static async ValueTask<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<float>> _selector, CancellationToken _cancellationToken)
  933. {
  934. float value;
  935. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  936. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  937. {
  938. if (!await e.MoveNextAsync())
  939. {
  940. throw Error.NoElements();
  941. }
  942. value = await _selector(e.Current).ConfigureAwait(false);
  943. // NaN is ordered less than all other values. We need to do explicit checks
  944. // to ensure this, but once we've found a value that is not NaN we need no
  945. // longer worry about it, so first loop until such a value is found (or not,
  946. // as the case may be).
  947. while (float.IsNaN(value))
  948. {
  949. if (!await e.MoveNextAsync())
  950. {
  951. return value;
  952. }
  953. value = await _selector(e.Current).ConfigureAwait(false);
  954. }
  955. while (await e.MoveNextAsync())
  956. {
  957. var x = await _selector(e.Current).ConfigureAwait(false);
  958. if (x > value)
  959. {
  960. value = x;
  961. }
  962. }
  963. }
  964. finally
  965. {
  966. await e.DisposeAsync();
  967. }
  968. return value;
  969. }
  970. }
  971. #if !NO_DEEP_CANCELLATION
  972. public static ValueTask<float> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  973. {
  974. if (source == null)
  975. throw Error.ArgumentNull(nameof(source));
  976. if (selector == null)
  977. throw Error.ArgumentNull(nameof(selector));
  978. return Core(source, selector, cancellationToken);
  979. static async ValueTask<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<float>> _selector, CancellationToken _cancellationToken)
  980. {
  981. float value;
  982. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  983. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  984. {
  985. if (!await e.MoveNextAsync())
  986. {
  987. throw Error.NoElements();
  988. }
  989. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  990. // NaN is ordered less than all other values. We need to do explicit checks
  991. // to ensure this, but once we've found a value that is not NaN we need no
  992. // longer worry about it, so first loop until such a value is found (or not,
  993. // as the case may be).
  994. while (float.IsNaN(value))
  995. {
  996. if (!await e.MoveNextAsync())
  997. {
  998. return value;
  999. }
  1000. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1001. }
  1002. while (await e.MoveNextAsync())
  1003. {
  1004. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1005. if (x > value)
  1006. {
  1007. value = x;
  1008. }
  1009. }
  1010. }
  1011. finally
  1012. {
  1013. await e.DisposeAsync();
  1014. }
  1015. return value;
  1016. }
  1017. }
  1018. #endif
  1019. public static ValueTask<float?> MaxAsync(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken = default)
  1020. {
  1021. if (source == null)
  1022. throw Error.ArgumentNull(nameof(source));
  1023. return Core(source, cancellationToken);
  1024. static async ValueTask<float?> Core(IAsyncEnumerable<float?> _source, CancellationToken _cancellationToken)
  1025. {
  1026. float? value = null;
  1027. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1028. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1029. {
  1030. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1031. // so we don't have to keep testing for nullity.
  1032. do
  1033. {
  1034. if (!await e.MoveNextAsync())
  1035. {
  1036. return value;
  1037. }
  1038. value = e.Current;
  1039. }
  1040. while (!value.HasValue);
  1041. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1042. // using the lifted operation each time.
  1043. var valueVal = value.GetValueOrDefault();
  1044. // NaN is ordered less than all other values. We need to do explicit checks
  1045. // to ensure this, but once we've found a value that is not NaN we need no
  1046. // longer worry about it, so first loop until such a value is found (or not,
  1047. // as the case may be).
  1048. while (float.IsNaN(valueVal))
  1049. {
  1050. if (!await e.MoveNextAsync())
  1051. {
  1052. return value;
  1053. }
  1054. var cur = e.Current;
  1055. if (cur.HasValue)
  1056. {
  1057. valueVal = (value = cur).GetValueOrDefault();
  1058. }
  1059. }
  1060. while (await e.MoveNextAsync())
  1061. {
  1062. var cur = e.Current;
  1063. var x = cur.GetValueOrDefault();
  1064. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1065. // unless nulls either never happen or always happen.
  1066. if (cur.HasValue & x > valueVal)
  1067. {
  1068. valueVal = x;
  1069. value = cur;
  1070. }
  1071. }
  1072. }
  1073. finally
  1074. {
  1075. await e.DisposeAsync();
  1076. }
  1077. return value;
  1078. }
  1079. }
  1080. public static ValueTask<float?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken = default)
  1081. {
  1082. if (source == null)
  1083. throw Error.ArgumentNull(nameof(source));
  1084. if (selector == null)
  1085. throw Error.ArgumentNull(nameof(selector));
  1086. return Core(source, selector, cancellationToken);
  1087. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, float?> _selector, CancellationToken _cancellationToken)
  1088. {
  1089. float? value = null;
  1090. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1091. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1092. {
  1093. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1094. // so we don't have to keep testing for nullity.
  1095. do
  1096. {
  1097. if (!await e.MoveNextAsync())
  1098. {
  1099. return value;
  1100. }
  1101. value = _selector(e.Current);
  1102. }
  1103. while (!value.HasValue);
  1104. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1105. // using the lifted operation each time.
  1106. var valueVal = value.GetValueOrDefault();
  1107. // NaN is ordered less than all other values. We need to do explicit checks
  1108. // to ensure this, but once we've found a value that is not NaN we need no
  1109. // longer worry about it, so first loop until such a value is found (or not,
  1110. // as the case may be).
  1111. while (float.IsNaN(valueVal))
  1112. {
  1113. if (!await e.MoveNextAsync())
  1114. {
  1115. return value;
  1116. }
  1117. var cur = _selector(e.Current);
  1118. if (cur.HasValue)
  1119. {
  1120. valueVal = (value = cur).GetValueOrDefault();
  1121. }
  1122. }
  1123. while (await e.MoveNextAsync())
  1124. {
  1125. var cur = _selector(e.Current);
  1126. var x = cur.GetValueOrDefault();
  1127. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1128. // unless nulls either never happen or always happen.
  1129. if (cur.HasValue & x > valueVal)
  1130. {
  1131. valueVal = x;
  1132. value = cur;
  1133. }
  1134. }
  1135. }
  1136. finally
  1137. {
  1138. await e.DisposeAsync();
  1139. }
  1140. return value;
  1141. }
  1142. }
  1143. public static ValueTask<float?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  1144. {
  1145. if (source == null)
  1146. throw Error.ArgumentNull(nameof(source));
  1147. if (selector == null)
  1148. throw Error.ArgumentNull(nameof(selector));
  1149. return Core(source, selector, cancellationToken);
  1150. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<float?>> _selector, CancellationToken _cancellationToken)
  1151. {
  1152. float? value = null;
  1153. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1154. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1155. {
  1156. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1157. // so we don't have to keep testing for nullity.
  1158. do
  1159. {
  1160. if (!await e.MoveNextAsync())
  1161. {
  1162. return value;
  1163. }
  1164. value = await _selector(e.Current).ConfigureAwait(false);
  1165. }
  1166. while (!value.HasValue);
  1167. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1168. // using the lifted operation each time.
  1169. var valueVal = value.GetValueOrDefault();
  1170. // NaN is ordered less than all other values. We need to do explicit checks
  1171. // to ensure this, but once we've found a value that is not NaN we need no
  1172. // longer worry about it, so first loop until such a value is found (or not,
  1173. // as the case may be).
  1174. while (float.IsNaN(valueVal))
  1175. {
  1176. if (!await e.MoveNextAsync())
  1177. {
  1178. return value;
  1179. }
  1180. var cur = await _selector(e.Current).ConfigureAwait(false);
  1181. if (cur.HasValue)
  1182. {
  1183. valueVal = (value = cur).GetValueOrDefault();
  1184. }
  1185. }
  1186. while (await e.MoveNextAsync())
  1187. {
  1188. var cur = await _selector(e.Current).ConfigureAwait(false);
  1189. var x = cur.GetValueOrDefault();
  1190. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1191. // unless nulls either never happen or always happen.
  1192. if (cur.HasValue & x > valueVal)
  1193. {
  1194. valueVal = x;
  1195. value = cur;
  1196. }
  1197. }
  1198. }
  1199. finally
  1200. {
  1201. await e.DisposeAsync();
  1202. }
  1203. return value;
  1204. }
  1205. }
  1206. #if !NO_DEEP_CANCELLATION
  1207. public static ValueTask<float?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  1208. {
  1209. if (source == null)
  1210. throw Error.ArgumentNull(nameof(source));
  1211. if (selector == null)
  1212. throw Error.ArgumentNull(nameof(selector));
  1213. return Core(source, selector, cancellationToken);
  1214. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<float?>> _selector, CancellationToken _cancellationToken)
  1215. {
  1216. float? value = null;
  1217. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1218. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1219. {
  1220. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1221. // so we don't have to keep testing for nullity.
  1222. do
  1223. {
  1224. if (!await e.MoveNextAsync())
  1225. {
  1226. return value;
  1227. }
  1228. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1229. }
  1230. while (!value.HasValue);
  1231. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1232. // using the lifted operation each time.
  1233. var valueVal = value.GetValueOrDefault();
  1234. // NaN is ordered less than all other values. We need to do explicit checks
  1235. // to ensure this, but once we've found a value that is not NaN we need no
  1236. // longer worry about it, so first loop until such a value is found (or not,
  1237. // as the case may be).
  1238. while (float.IsNaN(valueVal))
  1239. {
  1240. if (!await e.MoveNextAsync())
  1241. {
  1242. return value;
  1243. }
  1244. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1245. if (cur.HasValue)
  1246. {
  1247. valueVal = (value = cur).GetValueOrDefault();
  1248. }
  1249. }
  1250. while (await e.MoveNextAsync())
  1251. {
  1252. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1253. var x = cur.GetValueOrDefault();
  1254. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1255. // unless nulls either never happen or always happen.
  1256. if (cur.HasValue & x > valueVal)
  1257. {
  1258. valueVal = x;
  1259. value = cur;
  1260. }
  1261. }
  1262. }
  1263. finally
  1264. {
  1265. await e.DisposeAsync();
  1266. }
  1267. return value;
  1268. }
  1269. }
  1270. #endif
  1271. public static ValueTask<double> MaxAsync(this IAsyncEnumerable<double> source, CancellationToken cancellationToken = default)
  1272. {
  1273. if (source == null)
  1274. throw Error.ArgumentNull(nameof(source));
  1275. return Core(source, cancellationToken);
  1276. static async ValueTask<double> Core(IAsyncEnumerable<double> _source, CancellationToken _cancellationToken)
  1277. {
  1278. double value;
  1279. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1280. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1281. {
  1282. if (!await e.MoveNextAsync())
  1283. {
  1284. throw Error.NoElements();
  1285. }
  1286. value = e.Current;
  1287. // NaN is ordered less than all other values. We need to do explicit checks
  1288. // to ensure this, but once we've found a value that is not NaN we need no
  1289. // longer worry about it, so first loop until such a value is found (or not,
  1290. // as the case may be).
  1291. while (double.IsNaN(value))
  1292. {
  1293. if (!await e.MoveNextAsync())
  1294. {
  1295. return value;
  1296. }
  1297. value = e.Current;
  1298. }
  1299. while (await e.MoveNextAsync())
  1300. {
  1301. var x = e.Current;
  1302. if (x > value)
  1303. {
  1304. value = x;
  1305. }
  1306. }
  1307. }
  1308. finally
  1309. {
  1310. await e.DisposeAsync();
  1311. }
  1312. return value;
  1313. }
  1314. }
  1315. public static ValueTask<double> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken = default)
  1316. {
  1317. if (source == null)
  1318. throw Error.ArgumentNull(nameof(source));
  1319. if (selector == null)
  1320. throw Error.ArgumentNull(nameof(selector));
  1321. return Core(source, selector, cancellationToken);
  1322. static async ValueTask<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, double> _selector, CancellationToken _cancellationToken)
  1323. {
  1324. double value;
  1325. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1326. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1327. {
  1328. if (!await e.MoveNextAsync())
  1329. {
  1330. throw Error.NoElements();
  1331. }
  1332. value = _selector(e.Current);
  1333. // NaN is ordered less than all other values. We need to do explicit checks
  1334. // to ensure this, but once we've found a value that is not NaN we need no
  1335. // longer worry about it, so first loop until such a value is found (or not,
  1336. // as the case may be).
  1337. while (double.IsNaN(value))
  1338. {
  1339. if (!await e.MoveNextAsync())
  1340. {
  1341. return value;
  1342. }
  1343. value = _selector(e.Current);
  1344. }
  1345. while (await e.MoveNextAsync())
  1346. {
  1347. var x = _selector(e.Current);
  1348. if (x > value)
  1349. {
  1350. value = x;
  1351. }
  1352. }
  1353. }
  1354. finally
  1355. {
  1356. await e.DisposeAsync();
  1357. }
  1358. return value;
  1359. }
  1360. }
  1361. public static ValueTask<double> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  1362. {
  1363. if (source == null)
  1364. throw Error.ArgumentNull(nameof(source));
  1365. if (selector == null)
  1366. throw Error.ArgumentNull(nameof(selector));
  1367. return Core(source, selector, cancellationToken);
  1368. static async ValueTask<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<double>> _selector, CancellationToken _cancellationToken)
  1369. {
  1370. double value;
  1371. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1372. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1373. {
  1374. if (!await e.MoveNextAsync())
  1375. {
  1376. throw Error.NoElements();
  1377. }
  1378. value = await _selector(e.Current).ConfigureAwait(false);
  1379. // NaN is ordered less than all other values. We need to do explicit checks
  1380. // to ensure this, but once we've found a value that is not NaN we need no
  1381. // longer worry about it, so first loop until such a value is found (or not,
  1382. // as the case may be).
  1383. while (double.IsNaN(value))
  1384. {
  1385. if (!await e.MoveNextAsync())
  1386. {
  1387. return value;
  1388. }
  1389. value = await _selector(e.Current).ConfigureAwait(false);
  1390. }
  1391. while (await e.MoveNextAsync())
  1392. {
  1393. var x = await _selector(e.Current).ConfigureAwait(false);
  1394. if (x > value)
  1395. {
  1396. value = x;
  1397. }
  1398. }
  1399. }
  1400. finally
  1401. {
  1402. await e.DisposeAsync();
  1403. }
  1404. return value;
  1405. }
  1406. }
  1407. #if !NO_DEEP_CANCELLATION
  1408. public static ValueTask<double> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  1409. {
  1410. if (source == null)
  1411. throw Error.ArgumentNull(nameof(source));
  1412. if (selector == null)
  1413. throw Error.ArgumentNull(nameof(selector));
  1414. return Core(source, selector, cancellationToken);
  1415. static async ValueTask<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<double>> _selector, CancellationToken _cancellationToken)
  1416. {
  1417. double value;
  1418. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1419. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1420. {
  1421. if (!await e.MoveNextAsync())
  1422. {
  1423. throw Error.NoElements();
  1424. }
  1425. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1426. // NaN is ordered less than all other values. We need to do explicit checks
  1427. // to ensure this, but once we've found a value that is not NaN we need no
  1428. // longer worry about it, so first loop until such a value is found (or not,
  1429. // as the case may be).
  1430. while (double.IsNaN(value))
  1431. {
  1432. if (!await e.MoveNextAsync())
  1433. {
  1434. return value;
  1435. }
  1436. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1437. }
  1438. while (await e.MoveNextAsync())
  1439. {
  1440. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1441. if (x > value)
  1442. {
  1443. value = x;
  1444. }
  1445. }
  1446. }
  1447. finally
  1448. {
  1449. await e.DisposeAsync();
  1450. }
  1451. return value;
  1452. }
  1453. }
  1454. #endif
  1455. public static ValueTask<double?> MaxAsync(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken = default)
  1456. {
  1457. if (source == null)
  1458. throw Error.ArgumentNull(nameof(source));
  1459. return Core(source, cancellationToken);
  1460. static async ValueTask<double?> Core(IAsyncEnumerable<double?> _source, CancellationToken _cancellationToken)
  1461. {
  1462. double? value = null;
  1463. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1464. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1465. {
  1466. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1467. // so we don't have to keep testing for nullity.
  1468. do
  1469. {
  1470. if (!await e.MoveNextAsync())
  1471. {
  1472. return value;
  1473. }
  1474. value = e.Current;
  1475. }
  1476. while (!value.HasValue);
  1477. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1478. // using the lifted operation each time.
  1479. var valueVal = value.GetValueOrDefault();
  1480. // NaN is ordered less than all other values. We need to do explicit checks
  1481. // to ensure this, but once we've found a value that is not NaN we need no
  1482. // longer worry about it, so first loop until such a value is found (or not,
  1483. // as the case may be).
  1484. while (double.IsNaN(valueVal))
  1485. {
  1486. if (!await e.MoveNextAsync())
  1487. {
  1488. return value;
  1489. }
  1490. var cur = e.Current;
  1491. if (cur.HasValue)
  1492. {
  1493. valueVal = (value = cur).GetValueOrDefault();
  1494. }
  1495. }
  1496. while (await e.MoveNextAsync())
  1497. {
  1498. var cur = e.Current;
  1499. var x = cur.GetValueOrDefault();
  1500. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1501. // unless nulls either never happen or always happen.
  1502. if (cur.HasValue & x > valueVal)
  1503. {
  1504. valueVal = x;
  1505. value = cur;
  1506. }
  1507. }
  1508. }
  1509. finally
  1510. {
  1511. await e.DisposeAsync();
  1512. }
  1513. return value;
  1514. }
  1515. }
  1516. public static ValueTask<double?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
  1517. {
  1518. if (source == null)
  1519. throw Error.ArgumentNull(nameof(source));
  1520. if (selector == null)
  1521. throw Error.ArgumentNull(nameof(selector));
  1522. return Core(source, selector, cancellationToken);
  1523. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, double?> _selector, CancellationToken _cancellationToken)
  1524. {
  1525. double? value = null;
  1526. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1527. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1528. {
  1529. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1530. // so we don't have to keep testing for nullity.
  1531. do
  1532. {
  1533. if (!await e.MoveNextAsync())
  1534. {
  1535. return value;
  1536. }
  1537. value = _selector(e.Current);
  1538. }
  1539. while (!value.HasValue);
  1540. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1541. // using the lifted operation each time.
  1542. var valueVal = value.GetValueOrDefault();
  1543. // NaN is ordered less than all other values. We need to do explicit checks
  1544. // to ensure this, but once we've found a value that is not NaN we need no
  1545. // longer worry about it, so first loop until such a value is found (or not,
  1546. // as the case may be).
  1547. while (double.IsNaN(valueVal))
  1548. {
  1549. if (!await e.MoveNextAsync())
  1550. {
  1551. return value;
  1552. }
  1553. var cur = _selector(e.Current);
  1554. if (cur.HasValue)
  1555. {
  1556. valueVal = (value = cur).GetValueOrDefault();
  1557. }
  1558. }
  1559. while (await e.MoveNextAsync())
  1560. {
  1561. var cur = _selector(e.Current);
  1562. var x = cur.GetValueOrDefault();
  1563. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1564. // unless nulls either never happen or always happen.
  1565. if (cur.HasValue & x > valueVal)
  1566. {
  1567. valueVal = x;
  1568. value = cur;
  1569. }
  1570. }
  1571. }
  1572. finally
  1573. {
  1574. await e.DisposeAsync();
  1575. }
  1576. return value;
  1577. }
  1578. }
  1579. public static ValueTask<double?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  1580. {
  1581. if (source == null)
  1582. throw Error.ArgumentNull(nameof(source));
  1583. if (selector == null)
  1584. throw Error.ArgumentNull(nameof(selector));
  1585. return Core(source, selector, cancellationToken);
  1586. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<double?>> _selector, CancellationToken _cancellationToken)
  1587. {
  1588. double? value = null;
  1589. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1590. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1591. {
  1592. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1593. // so we don't have to keep testing for nullity.
  1594. do
  1595. {
  1596. if (!await e.MoveNextAsync())
  1597. {
  1598. return value;
  1599. }
  1600. value = await _selector(e.Current).ConfigureAwait(false);
  1601. }
  1602. while (!value.HasValue);
  1603. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1604. // using the lifted operation each time.
  1605. var valueVal = value.GetValueOrDefault();
  1606. // NaN is ordered less than all other values. We need to do explicit checks
  1607. // to ensure this, but once we've found a value that is not NaN we need no
  1608. // longer worry about it, so first loop until such a value is found (or not,
  1609. // as the case may be).
  1610. while (double.IsNaN(valueVal))
  1611. {
  1612. if (!await e.MoveNextAsync())
  1613. {
  1614. return value;
  1615. }
  1616. var cur = await _selector(e.Current).ConfigureAwait(false);
  1617. if (cur.HasValue)
  1618. {
  1619. valueVal = (value = cur).GetValueOrDefault();
  1620. }
  1621. }
  1622. while (await e.MoveNextAsync())
  1623. {
  1624. var cur = await _selector(e.Current).ConfigureAwait(false);
  1625. var x = cur.GetValueOrDefault();
  1626. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1627. // unless nulls either never happen or always happen.
  1628. if (cur.HasValue & x > valueVal)
  1629. {
  1630. valueVal = x;
  1631. value = cur;
  1632. }
  1633. }
  1634. }
  1635. finally
  1636. {
  1637. await e.DisposeAsync();
  1638. }
  1639. return value;
  1640. }
  1641. }
  1642. #if !NO_DEEP_CANCELLATION
  1643. public static ValueTask<double?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  1644. {
  1645. if (source == null)
  1646. throw Error.ArgumentNull(nameof(source));
  1647. if (selector == null)
  1648. throw Error.ArgumentNull(nameof(selector));
  1649. return Core(source, selector, cancellationToken);
  1650. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<double?>> _selector, CancellationToken _cancellationToken)
  1651. {
  1652. double? value = null;
  1653. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1654. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1655. {
  1656. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1657. // so we don't have to keep testing for nullity.
  1658. do
  1659. {
  1660. if (!await e.MoveNextAsync())
  1661. {
  1662. return value;
  1663. }
  1664. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1665. }
  1666. while (!value.HasValue);
  1667. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1668. // using the lifted operation each time.
  1669. var valueVal = value.GetValueOrDefault();
  1670. // NaN is ordered less than all other values. We need to do explicit checks
  1671. // to ensure this, but once we've found a value that is not NaN we need no
  1672. // longer worry about it, so first loop until such a value is found (or not,
  1673. // as the case may be).
  1674. while (double.IsNaN(valueVal))
  1675. {
  1676. if (!await e.MoveNextAsync())
  1677. {
  1678. return value;
  1679. }
  1680. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1681. if (cur.HasValue)
  1682. {
  1683. valueVal = (value = cur).GetValueOrDefault();
  1684. }
  1685. }
  1686. while (await e.MoveNextAsync())
  1687. {
  1688. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1689. var x = cur.GetValueOrDefault();
  1690. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1691. // unless nulls either never happen or always happen.
  1692. if (cur.HasValue & x > valueVal)
  1693. {
  1694. valueVal = x;
  1695. value = cur;
  1696. }
  1697. }
  1698. }
  1699. finally
  1700. {
  1701. await e.DisposeAsync();
  1702. }
  1703. return value;
  1704. }
  1705. }
  1706. #endif
  1707. public static ValueTask<decimal> MaxAsync(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken = default)
  1708. {
  1709. if (source == null)
  1710. throw Error.ArgumentNull(nameof(source));
  1711. return Core(source, cancellationToken);
  1712. static async ValueTask<decimal> Core(IAsyncEnumerable<decimal> _source, CancellationToken _cancellationToken)
  1713. {
  1714. decimal value;
  1715. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1716. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1717. {
  1718. if (!await e.MoveNextAsync())
  1719. {
  1720. throw Error.NoElements();
  1721. }
  1722. value = e.Current;
  1723. while (await e.MoveNextAsync())
  1724. {
  1725. var x = e.Current;
  1726. if (x > value)
  1727. {
  1728. value = x;
  1729. }
  1730. }
  1731. }
  1732. finally
  1733. {
  1734. await e.DisposeAsync();
  1735. }
  1736. return value;
  1737. }
  1738. }
  1739. public static ValueTask<decimal> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken = default)
  1740. {
  1741. if (source == null)
  1742. throw Error.ArgumentNull(nameof(source));
  1743. if (selector == null)
  1744. throw Error.ArgumentNull(nameof(selector));
  1745. return Core(source, selector, cancellationToken);
  1746. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, decimal> _selector, CancellationToken _cancellationToken)
  1747. {
  1748. decimal value;
  1749. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1750. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1751. {
  1752. if (!await e.MoveNextAsync())
  1753. {
  1754. throw Error.NoElements();
  1755. }
  1756. value = _selector(e.Current);
  1757. while (await e.MoveNextAsync())
  1758. {
  1759. var x = _selector(e.Current);
  1760. if (x > value)
  1761. {
  1762. value = x;
  1763. }
  1764. }
  1765. }
  1766. finally
  1767. {
  1768. await e.DisposeAsync();
  1769. }
  1770. return value;
  1771. }
  1772. }
  1773. public static ValueTask<decimal> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  1774. {
  1775. if (source == null)
  1776. throw Error.ArgumentNull(nameof(source));
  1777. if (selector == null)
  1778. throw Error.ArgumentNull(nameof(selector));
  1779. return Core(source, selector, cancellationToken);
  1780. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<decimal>> _selector, CancellationToken _cancellationToken)
  1781. {
  1782. decimal value;
  1783. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1784. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1785. {
  1786. if (!await e.MoveNextAsync())
  1787. {
  1788. throw Error.NoElements();
  1789. }
  1790. value = await _selector(e.Current).ConfigureAwait(false);
  1791. while (await e.MoveNextAsync())
  1792. {
  1793. var x = await _selector(e.Current).ConfigureAwait(false);
  1794. if (x > value)
  1795. {
  1796. value = x;
  1797. }
  1798. }
  1799. }
  1800. finally
  1801. {
  1802. await e.DisposeAsync();
  1803. }
  1804. return value;
  1805. }
  1806. }
  1807. #if !NO_DEEP_CANCELLATION
  1808. public static ValueTask<decimal> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  1809. {
  1810. if (source == null)
  1811. throw Error.ArgumentNull(nameof(source));
  1812. if (selector == null)
  1813. throw Error.ArgumentNull(nameof(selector));
  1814. return Core(source, selector, cancellationToken);
  1815. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<decimal>> _selector, CancellationToken _cancellationToken)
  1816. {
  1817. decimal value;
  1818. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1819. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1820. {
  1821. if (!await e.MoveNextAsync())
  1822. {
  1823. throw Error.NoElements();
  1824. }
  1825. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1826. while (await e.MoveNextAsync())
  1827. {
  1828. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1829. if (x > value)
  1830. {
  1831. value = x;
  1832. }
  1833. }
  1834. }
  1835. finally
  1836. {
  1837. await e.DisposeAsync();
  1838. }
  1839. return value;
  1840. }
  1841. }
  1842. #endif
  1843. public static ValueTask<decimal?> MaxAsync(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken = default)
  1844. {
  1845. if (source == null)
  1846. throw Error.ArgumentNull(nameof(source));
  1847. return Core(source, cancellationToken);
  1848. static async ValueTask<decimal?> Core(IAsyncEnumerable<decimal?> _source, CancellationToken _cancellationToken)
  1849. {
  1850. decimal? value = null;
  1851. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1852. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1853. {
  1854. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1855. // so we don't have to keep testing for nullity.
  1856. do
  1857. {
  1858. if (!await e.MoveNextAsync())
  1859. {
  1860. return value;
  1861. }
  1862. value = e.Current;
  1863. }
  1864. while (!value.HasValue);
  1865. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1866. // using the lifted operation each time.
  1867. var valueVal = value.GetValueOrDefault();
  1868. while (await e.MoveNextAsync())
  1869. {
  1870. var cur = e.Current;
  1871. var x = cur.GetValueOrDefault();
  1872. if (cur.HasValue && x > valueVal)
  1873. {
  1874. valueVal = x;
  1875. value = cur;
  1876. }
  1877. }
  1878. }
  1879. finally
  1880. {
  1881. await e.DisposeAsync();
  1882. }
  1883. return value;
  1884. }
  1885. }
  1886. public static ValueTask<decimal?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken = default)
  1887. {
  1888. if (source == null)
  1889. throw Error.ArgumentNull(nameof(source));
  1890. if (selector == null)
  1891. throw Error.ArgumentNull(nameof(selector));
  1892. return Core(source, selector, cancellationToken);
  1893. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, decimal?> _selector, CancellationToken _cancellationToken)
  1894. {
  1895. decimal? value = null;
  1896. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1897. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1898. {
  1899. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1900. // so we don't have to keep testing for nullity.
  1901. do
  1902. {
  1903. if (!await e.MoveNextAsync())
  1904. {
  1905. return value;
  1906. }
  1907. value = _selector(e.Current);
  1908. }
  1909. while (!value.HasValue);
  1910. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1911. // using the lifted operation each time.
  1912. var valueVal = value.GetValueOrDefault();
  1913. while (await e.MoveNextAsync())
  1914. {
  1915. var cur = _selector(e.Current);
  1916. var x = cur.GetValueOrDefault();
  1917. if (cur.HasValue && x > valueVal)
  1918. {
  1919. valueVal = x;
  1920. value = cur;
  1921. }
  1922. }
  1923. }
  1924. finally
  1925. {
  1926. await e.DisposeAsync();
  1927. }
  1928. return value;
  1929. }
  1930. }
  1931. public static ValueTask<decimal?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  1932. {
  1933. if (source == null)
  1934. throw Error.ArgumentNull(nameof(source));
  1935. if (selector == null)
  1936. throw Error.ArgumentNull(nameof(selector));
  1937. return Core(source, selector, cancellationToken);
  1938. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<decimal?>> _selector, CancellationToken _cancellationToken)
  1939. {
  1940. decimal? value = null;
  1941. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1942. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1943. {
  1944. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1945. // so we don't have to keep testing for nullity.
  1946. do
  1947. {
  1948. if (!await e.MoveNextAsync())
  1949. {
  1950. return value;
  1951. }
  1952. value = await _selector(e.Current).ConfigureAwait(false);
  1953. }
  1954. while (!value.HasValue);
  1955. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1956. // using the lifted operation each time.
  1957. var valueVal = value.GetValueOrDefault();
  1958. while (await e.MoveNextAsync())
  1959. {
  1960. var cur = await _selector(e.Current).ConfigureAwait(false);
  1961. var x = cur.GetValueOrDefault();
  1962. if (cur.HasValue && x > valueVal)
  1963. {
  1964. valueVal = x;
  1965. value = cur;
  1966. }
  1967. }
  1968. }
  1969. finally
  1970. {
  1971. await e.DisposeAsync();
  1972. }
  1973. return value;
  1974. }
  1975. }
  1976. #if !NO_DEEP_CANCELLATION
  1977. public static ValueTask<decimal?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  1978. {
  1979. if (source == null)
  1980. throw Error.ArgumentNull(nameof(source));
  1981. if (selector == null)
  1982. throw Error.ArgumentNull(nameof(selector));
  1983. return Core(source, selector, cancellationToken);
  1984. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<decimal?>> _selector, CancellationToken _cancellationToken)
  1985. {
  1986. decimal? value = null;
  1987. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  1988. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  1989. {
  1990. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1991. // so we don't have to keep testing for nullity.
  1992. do
  1993. {
  1994. if (!await e.MoveNextAsync())
  1995. {
  1996. return value;
  1997. }
  1998. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1999. }
  2000. while (!value.HasValue);
  2001. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2002. // using the lifted operation each time.
  2003. var valueVal = value.GetValueOrDefault();
  2004. while (await e.MoveNextAsync())
  2005. {
  2006. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2007. var x = cur.GetValueOrDefault();
  2008. if (cur.HasValue && x > valueVal)
  2009. {
  2010. valueVal = x;
  2011. value = cur;
  2012. }
  2013. }
  2014. }
  2015. finally
  2016. {
  2017. await e.DisposeAsync();
  2018. }
  2019. return value;
  2020. }
  2021. }
  2022. #endif
  2023. public static ValueTask<int> MinAsync(this IAsyncEnumerable<int> source, CancellationToken cancellationToken = default)
  2024. {
  2025. if (source == null)
  2026. throw Error.ArgumentNull(nameof(source));
  2027. return Core(source, cancellationToken);
  2028. static async ValueTask<int> Core(IAsyncEnumerable<int> _source, CancellationToken _cancellationToken)
  2029. {
  2030. int value;
  2031. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2032. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2033. {
  2034. if (!await e.MoveNextAsync())
  2035. {
  2036. throw Error.NoElements();
  2037. }
  2038. value = e.Current;
  2039. while (await e.MoveNextAsync())
  2040. {
  2041. var x = e.Current;
  2042. if (x < value)
  2043. {
  2044. value = x;
  2045. }
  2046. }
  2047. }
  2048. finally
  2049. {
  2050. await e.DisposeAsync();
  2051. }
  2052. return value;
  2053. }
  2054. }
  2055. public static ValueTask<int> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken = default)
  2056. {
  2057. if (source == null)
  2058. throw Error.ArgumentNull(nameof(source));
  2059. if (selector == null)
  2060. throw Error.ArgumentNull(nameof(selector));
  2061. return Core(source, selector, cancellationToken);
  2062. static async ValueTask<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, int> _selector, CancellationToken _cancellationToken)
  2063. {
  2064. int value;
  2065. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2066. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2067. {
  2068. if (!await e.MoveNextAsync())
  2069. {
  2070. throw Error.NoElements();
  2071. }
  2072. value = _selector(e.Current);
  2073. while (await e.MoveNextAsync())
  2074. {
  2075. var x = _selector(e.Current);
  2076. if (x < value)
  2077. {
  2078. value = x;
  2079. }
  2080. }
  2081. }
  2082. finally
  2083. {
  2084. await e.DisposeAsync();
  2085. }
  2086. return value;
  2087. }
  2088. }
  2089. public static ValueTask<int> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  2090. {
  2091. if (source == null)
  2092. throw Error.ArgumentNull(nameof(source));
  2093. if (selector == null)
  2094. throw Error.ArgumentNull(nameof(selector));
  2095. return Core(source, selector, cancellationToken);
  2096. static async ValueTask<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<int>> _selector, CancellationToken _cancellationToken)
  2097. {
  2098. int value;
  2099. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2100. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2101. {
  2102. if (!await e.MoveNextAsync())
  2103. {
  2104. throw Error.NoElements();
  2105. }
  2106. value = await _selector(e.Current).ConfigureAwait(false);
  2107. while (await e.MoveNextAsync())
  2108. {
  2109. var x = await _selector(e.Current).ConfigureAwait(false);
  2110. if (x < value)
  2111. {
  2112. value = x;
  2113. }
  2114. }
  2115. }
  2116. finally
  2117. {
  2118. await e.DisposeAsync();
  2119. }
  2120. return value;
  2121. }
  2122. }
  2123. #if !NO_DEEP_CANCELLATION
  2124. public static ValueTask<int> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  2125. {
  2126. if (source == null)
  2127. throw Error.ArgumentNull(nameof(source));
  2128. if (selector == null)
  2129. throw Error.ArgumentNull(nameof(selector));
  2130. return Core(source, selector, cancellationToken);
  2131. static async ValueTask<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<int>> _selector, CancellationToken _cancellationToken)
  2132. {
  2133. int value;
  2134. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2135. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2136. {
  2137. if (!await e.MoveNextAsync())
  2138. {
  2139. throw Error.NoElements();
  2140. }
  2141. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2142. while (await e.MoveNextAsync())
  2143. {
  2144. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2145. if (x < value)
  2146. {
  2147. value = x;
  2148. }
  2149. }
  2150. }
  2151. finally
  2152. {
  2153. await e.DisposeAsync();
  2154. }
  2155. return value;
  2156. }
  2157. }
  2158. #endif
  2159. public static ValueTask<int?> MinAsync(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken = default)
  2160. {
  2161. if (source == null)
  2162. throw Error.ArgumentNull(nameof(source));
  2163. return Core(source, cancellationToken);
  2164. static async ValueTask<int?> Core(IAsyncEnumerable<int?> _source, CancellationToken _cancellationToken)
  2165. {
  2166. int? value = null;
  2167. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2168. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2169. {
  2170. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2171. // so we don't have to keep testing for nullity.
  2172. do
  2173. {
  2174. if (!await e.MoveNextAsync())
  2175. {
  2176. return value;
  2177. }
  2178. value = e.Current;
  2179. }
  2180. while (!value.HasValue);
  2181. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2182. // using the lifted operation each time.
  2183. var valueVal = value.GetValueOrDefault();
  2184. while (await e.MoveNextAsync())
  2185. {
  2186. var cur = e.Current;
  2187. var x = cur.GetValueOrDefault();
  2188. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2189. // unless nulls either never happen or always happen.
  2190. if (cur.HasValue & x < valueVal)
  2191. {
  2192. valueVal = x;
  2193. value = cur;
  2194. }
  2195. }
  2196. }
  2197. finally
  2198. {
  2199. await e.DisposeAsync();
  2200. }
  2201. return value;
  2202. }
  2203. }
  2204. public static ValueTask<int?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken = default)
  2205. {
  2206. if (source == null)
  2207. throw Error.ArgumentNull(nameof(source));
  2208. if (selector == null)
  2209. throw Error.ArgumentNull(nameof(selector));
  2210. return Core(source, selector, cancellationToken);
  2211. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, int?> _selector, CancellationToken _cancellationToken)
  2212. {
  2213. int? value = null;
  2214. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2215. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2216. {
  2217. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2218. // so we don't have to keep testing for nullity.
  2219. do
  2220. {
  2221. if (!await e.MoveNextAsync())
  2222. {
  2223. return value;
  2224. }
  2225. value = _selector(e.Current);
  2226. }
  2227. while (!value.HasValue);
  2228. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2229. // using the lifted operation each time.
  2230. var valueVal = value.GetValueOrDefault();
  2231. while (await e.MoveNextAsync())
  2232. {
  2233. var cur = _selector(e.Current);
  2234. var x = cur.GetValueOrDefault();
  2235. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2236. // unless nulls either never happen or always happen.
  2237. if (cur.HasValue & x < valueVal)
  2238. {
  2239. valueVal = x;
  2240. value = cur;
  2241. }
  2242. }
  2243. }
  2244. finally
  2245. {
  2246. await e.DisposeAsync();
  2247. }
  2248. return value;
  2249. }
  2250. }
  2251. public static ValueTask<int?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  2252. {
  2253. if (source == null)
  2254. throw Error.ArgumentNull(nameof(source));
  2255. if (selector == null)
  2256. throw Error.ArgumentNull(nameof(selector));
  2257. return Core(source, selector, cancellationToken);
  2258. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<int?>> _selector, CancellationToken _cancellationToken)
  2259. {
  2260. int? value = null;
  2261. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2262. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2263. {
  2264. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2265. // so we don't have to keep testing for nullity.
  2266. do
  2267. {
  2268. if (!await e.MoveNextAsync())
  2269. {
  2270. return value;
  2271. }
  2272. value = await _selector(e.Current).ConfigureAwait(false);
  2273. }
  2274. while (!value.HasValue);
  2275. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2276. // using the lifted operation each time.
  2277. var valueVal = value.GetValueOrDefault();
  2278. while (await e.MoveNextAsync())
  2279. {
  2280. var cur = await _selector(e.Current).ConfigureAwait(false);
  2281. var x = cur.GetValueOrDefault();
  2282. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2283. // unless nulls either never happen or always happen.
  2284. if (cur.HasValue & x < valueVal)
  2285. {
  2286. valueVal = x;
  2287. value = cur;
  2288. }
  2289. }
  2290. }
  2291. finally
  2292. {
  2293. await e.DisposeAsync();
  2294. }
  2295. return value;
  2296. }
  2297. }
  2298. #if !NO_DEEP_CANCELLATION
  2299. public static ValueTask<int?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  2300. {
  2301. if (source == null)
  2302. throw Error.ArgumentNull(nameof(source));
  2303. if (selector == null)
  2304. throw Error.ArgumentNull(nameof(selector));
  2305. return Core(source, selector, cancellationToken);
  2306. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<int?>> _selector, CancellationToken _cancellationToken)
  2307. {
  2308. int? value = null;
  2309. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2310. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2311. {
  2312. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2313. // so we don't have to keep testing for nullity.
  2314. do
  2315. {
  2316. if (!await e.MoveNextAsync())
  2317. {
  2318. return value;
  2319. }
  2320. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2321. }
  2322. while (!value.HasValue);
  2323. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2324. // using the lifted operation each time.
  2325. var valueVal = value.GetValueOrDefault();
  2326. while (await e.MoveNextAsync())
  2327. {
  2328. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2329. var x = cur.GetValueOrDefault();
  2330. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2331. // unless nulls either never happen or always happen.
  2332. if (cur.HasValue & x < valueVal)
  2333. {
  2334. valueVal = x;
  2335. value = cur;
  2336. }
  2337. }
  2338. }
  2339. finally
  2340. {
  2341. await e.DisposeAsync();
  2342. }
  2343. return value;
  2344. }
  2345. }
  2346. #endif
  2347. public static ValueTask<long> MinAsync(this IAsyncEnumerable<long> source, CancellationToken cancellationToken = default)
  2348. {
  2349. if (source == null)
  2350. throw Error.ArgumentNull(nameof(source));
  2351. return Core(source, cancellationToken);
  2352. static async ValueTask<long> Core(IAsyncEnumerable<long> _source, CancellationToken _cancellationToken)
  2353. {
  2354. long value;
  2355. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2356. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2357. {
  2358. if (!await e.MoveNextAsync())
  2359. {
  2360. throw Error.NoElements();
  2361. }
  2362. value = e.Current;
  2363. while (await e.MoveNextAsync())
  2364. {
  2365. var x = e.Current;
  2366. if (x < value)
  2367. {
  2368. value = x;
  2369. }
  2370. }
  2371. }
  2372. finally
  2373. {
  2374. await e.DisposeAsync();
  2375. }
  2376. return value;
  2377. }
  2378. }
  2379. public static ValueTask<long> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken = default)
  2380. {
  2381. if (source == null)
  2382. throw Error.ArgumentNull(nameof(source));
  2383. if (selector == null)
  2384. throw Error.ArgumentNull(nameof(selector));
  2385. return Core(source, selector, cancellationToken);
  2386. static async ValueTask<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, long> _selector, CancellationToken _cancellationToken)
  2387. {
  2388. long value;
  2389. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2390. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2391. {
  2392. if (!await e.MoveNextAsync())
  2393. {
  2394. throw Error.NoElements();
  2395. }
  2396. value = _selector(e.Current);
  2397. while (await e.MoveNextAsync())
  2398. {
  2399. var x = _selector(e.Current);
  2400. if (x < value)
  2401. {
  2402. value = x;
  2403. }
  2404. }
  2405. }
  2406. finally
  2407. {
  2408. await e.DisposeAsync();
  2409. }
  2410. return value;
  2411. }
  2412. }
  2413. public static ValueTask<long> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  2414. {
  2415. if (source == null)
  2416. throw Error.ArgumentNull(nameof(source));
  2417. if (selector == null)
  2418. throw Error.ArgumentNull(nameof(selector));
  2419. return Core(source, selector, cancellationToken);
  2420. static async ValueTask<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<long>> _selector, CancellationToken _cancellationToken)
  2421. {
  2422. long value;
  2423. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2424. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2425. {
  2426. if (!await e.MoveNextAsync())
  2427. {
  2428. throw Error.NoElements();
  2429. }
  2430. value = await _selector(e.Current).ConfigureAwait(false);
  2431. while (await e.MoveNextAsync())
  2432. {
  2433. var x = await _selector(e.Current).ConfigureAwait(false);
  2434. if (x < value)
  2435. {
  2436. value = x;
  2437. }
  2438. }
  2439. }
  2440. finally
  2441. {
  2442. await e.DisposeAsync();
  2443. }
  2444. return value;
  2445. }
  2446. }
  2447. #if !NO_DEEP_CANCELLATION
  2448. public static ValueTask<long> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  2449. {
  2450. if (source == null)
  2451. throw Error.ArgumentNull(nameof(source));
  2452. if (selector == null)
  2453. throw Error.ArgumentNull(nameof(selector));
  2454. return Core(source, selector, cancellationToken);
  2455. static async ValueTask<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<long>> _selector, CancellationToken _cancellationToken)
  2456. {
  2457. long value;
  2458. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2459. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2460. {
  2461. if (!await e.MoveNextAsync())
  2462. {
  2463. throw Error.NoElements();
  2464. }
  2465. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2466. while (await e.MoveNextAsync())
  2467. {
  2468. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2469. if (x < value)
  2470. {
  2471. value = x;
  2472. }
  2473. }
  2474. }
  2475. finally
  2476. {
  2477. await e.DisposeAsync();
  2478. }
  2479. return value;
  2480. }
  2481. }
  2482. #endif
  2483. public static ValueTask<long?> MinAsync(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken = default)
  2484. {
  2485. if (source == null)
  2486. throw Error.ArgumentNull(nameof(source));
  2487. return Core(source, cancellationToken);
  2488. static async ValueTask<long?> Core(IAsyncEnumerable<long?> _source, CancellationToken _cancellationToken)
  2489. {
  2490. long? value = null;
  2491. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2492. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2493. {
  2494. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2495. // so we don't have to keep testing for nullity.
  2496. do
  2497. {
  2498. if (!await e.MoveNextAsync())
  2499. {
  2500. return value;
  2501. }
  2502. value = e.Current;
  2503. }
  2504. while (!value.HasValue);
  2505. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2506. // using the lifted operation each time.
  2507. var valueVal = value.GetValueOrDefault();
  2508. while (await e.MoveNextAsync())
  2509. {
  2510. var cur = e.Current;
  2511. var x = cur.GetValueOrDefault();
  2512. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2513. // unless nulls either never happen or always happen.
  2514. if (cur.HasValue & x < valueVal)
  2515. {
  2516. valueVal = x;
  2517. value = cur;
  2518. }
  2519. }
  2520. }
  2521. finally
  2522. {
  2523. await e.DisposeAsync();
  2524. }
  2525. return value;
  2526. }
  2527. }
  2528. public static ValueTask<long?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken = default)
  2529. {
  2530. if (source == null)
  2531. throw Error.ArgumentNull(nameof(source));
  2532. if (selector == null)
  2533. throw Error.ArgumentNull(nameof(selector));
  2534. return Core(source, selector, cancellationToken);
  2535. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, long?> _selector, CancellationToken _cancellationToken)
  2536. {
  2537. long? value = null;
  2538. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2539. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2540. {
  2541. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2542. // so we don't have to keep testing for nullity.
  2543. do
  2544. {
  2545. if (!await e.MoveNextAsync())
  2546. {
  2547. return value;
  2548. }
  2549. value = _selector(e.Current);
  2550. }
  2551. while (!value.HasValue);
  2552. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2553. // using the lifted operation each time.
  2554. var valueVal = value.GetValueOrDefault();
  2555. while (await e.MoveNextAsync())
  2556. {
  2557. var cur = _selector(e.Current);
  2558. var x = cur.GetValueOrDefault();
  2559. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2560. // unless nulls either never happen or always happen.
  2561. if (cur.HasValue & x < valueVal)
  2562. {
  2563. valueVal = x;
  2564. value = cur;
  2565. }
  2566. }
  2567. }
  2568. finally
  2569. {
  2570. await e.DisposeAsync();
  2571. }
  2572. return value;
  2573. }
  2574. }
  2575. public static ValueTask<long?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  2576. {
  2577. if (source == null)
  2578. throw Error.ArgumentNull(nameof(source));
  2579. if (selector == null)
  2580. throw Error.ArgumentNull(nameof(selector));
  2581. return Core(source, selector, cancellationToken);
  2582. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<long?>> _selector, CancellationToken _cancellationToken)
  2583. {
  2584. long? value = null;
  2585. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2586. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2587. {
  2588. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2589. // so we don't have to keep testing for nullity.
  2590. do
  2591. {
  2592. if (!await e.MoveNextAsync())
  2593. {
  2594. return value;
  2595. }
  2596. value = await _selector(e.Current).ConfigureAwait(false);
  2597. }
  2598. while (!value.HasValue);
  2599. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2600. // using the lifted operation each time.
  2601. var valueVal = value.GetValueOrDefault();
  2602. while (await e.MoveNextAsync())
  2603. {
  2604. var cur = await _selector(e.Current).ConfigureAwait(false);
  2605. var x = cur.GetValueOrDefault();
  2606. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2607. // unless nulls either never happen or always happen.
  2608. if (cur.HasValue & x < valueVal)
  2609. {
  2610. valueVal = x;
  2611. value = cur;
  2612. }
  2613. }
  2614. }
  2615. finally
  2616. {
  2617. await e.DisposeAsync();
  2618. }
  2619. return value;
  2620. }
  2621. }
  2622. #if !NO_DEEP_CANCELLATION
  2623. public static ValueTask<long?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  2624. {
  2625. if (source == null)
  2626. throw Error.ArgumentNull(nameof(source));
  2627. if (selector == null)
  2628. throw Error.ArgumentNull(nameof(selector));
  2629. return Core(source, selector, cancellationToken);
  2630. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<long?>> _selector, CancellationToken _cancellationToken)
  2631. {
  2632. long? value = null;
  2633. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2634. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2635. {
  2636. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2637. // so we don't have to keep testing for nullity.
  2638. do
  2639. {
  2640. if (!await e.MoveNextAsync())
  2641. {
  2642. return value;
  2643. }
  2644. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2645. }
  2646. while (!value.HasValue);
  2647. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2648. // using the lifted operation each time.
  2649. var valueVal = value.GetValueOrDefault();
  2650. while (await e.MoveNextAsync())
  2651. {
  2652. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2653. var x = cur.GetValueOrDefault();
  2654. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2655. // unless nulls either never happen or always happen.
  2656. if (cur.HasValue & x < valueVal)
  2657. {
  2658. valueVal = x;
  2659. value = cur;
  2660. }
  2661. }
  2662. }
  2663. finally
  2664. {
  2665. await e.DisposeAsync();
  2666. }
  2667. return value;
  2668. }
  2669. }
  2670. #endif
  2671. public static ValueTask<float> MinAsync(this IAsyncEnumerable<float> source, CancellationToken cancellationToken = default)
  2672. {
  2673. if (source == null)
  2674. throw Error.ArgumentNull(nameof(source));
  2675. return Core(source, cancellationToken);
  2676. static async ValueTask<float> Core(IAsyncEnumerable<float> _source, CancellationToken _cancellationToken)
  2677. {
  2678. float value;
  2679. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2680. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2681. {
  2682. if (!await e.MoveNextAsync())
  2683. {
  2684. throw Error.NoElements();
  2685. }
  2686. value = e.Current;
  2687. while (await e.MoveNextAsync())
  2688. {
  2689. var x = e.Current;
  2690. if (x < value)
  2691. {
  2692. value = x;
  2693. }
  2694. else
  2695. {
  2696. // Normally NaN < anything is false, as is anything < NaN
  2697. // However, this leads to some irksome outcomes in Min and Max.
  2698. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2699. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2700. // ordering where NaN is smaller than every value, including
  2701. // negative infinity.
  2702. // Not testing for NaN therefore isn't an option, but since we
  2703. // can't find a smaller value, we can short-circuit.
  2704. if (float.IsNaN(x))
  2705. {
  2706. return x;
  2707. }
  2708. }
  2709. }
  2710. }
  2711. finally
  2712. {
  2713. await e.DisposeAsync();
  2714. }
  2715. return value;
  2716. }
  2717. }
  2718. public static ValueTask<float> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken = default)
  2719. {
  2720. if (source == null)
  2721. throw Error.ArgumentNull(nameof(source));
  2722. if (selector == null)
  2723. throw Error.ArgumentNull(nameof(selector));
  2724. return Core(source, selector, cancellationToken);
  2725. static async ValueTask<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, float> _selector, CancellationToken _cancellationToken)
  2726. {
  2727. float value;
  2728. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2729. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2730. {
  2731. if (!await e.MoveNextAsync())
  2732. {
  2733. throw Error.NoElements();
  2734. }
  2735. value = _selector(e.Current);
  2736. while (await e.MoveNextAsync())
  2737. {
  2738. var x = _selector(e.Current);
  2739. if (x < value)
  2740. {
  2741. value = x;
  2742. }
  2743. else
  2744. {
  2745. // Normally NaN < anything is false, as is anything < NaN
  2746. // However, this leads to some irksome outcomes in Min and Max.
  2747. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2748. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2749. // ordering where NaN is smaller than every value, including
  2750. // negative infinity.
  2751. // Not testing for NaN therefore isn't an option, but since we
  2752. // can't find a smaller value, we can short-circuit.
  2753. if (float.IsNaN(x))
  2754. {
  2755. return x;
  2756. }
  2757. }
  2758. }
  2759. }
  2760. finally
  2761. {
  2762. await e.DisposeAsync();
  2763. }
  2764. return value;
  2765. }
  2766. }
  2767. public static ValueTask<float> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  2768. {
  2769. if (source == null)
  2770. throw Error.ArgumentNull(nameof(source));
  2771. if (selector == null)
  2772. throw Error.ArgumentNull(nameof(selector));
  2773. return Core(source, selector, cancellationToken);
  2774. static async ValueTask<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<float>> _selector, CancellationToken _cancellationToken)
  2775. {
  2776. float value;
  2777. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2778. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2779. {
  2780. if (!await e.MoveNextAsync())
  2781. {
  2782. throw Error.NoElements();
  2783. }
  2784. value = await _selector(e.Current).ConfigureAwait(false);
  2785. while (await e.MoveNextAsync())
  2786. {
  2787. var x = await _selector(e.Current).ConfigureAwait(false);
  2788. if (x < value)
  2789. {
  2790. value = x;
  2791. }
  2792. else
  2793. {
  2794. // Normally NaN < anything is false, as is anything < NaN
  2795. // However, this leads to some irksome outcomes in Min and Max.
  2796. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2797. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2798. // ordering where NaN is smaller than every value, including
  2799. // negative infinity.
  2800. // Not testing for NaN therefore isn't an option, but since we
  2801. // can't find a smaller value, we can short-circuit.
  2802. if (float.IsNaN(x))
  2803. {
  2804. return x;
  2805. }
  2806. }
  2807. }
  2808. }
  2809. finally
  2810. {
  2811. await e.DisposeAsync();
  2812. }
  2813. return value;
  2814. }
  2815. }
  2816. #if !NO_DEEP_CANCELLATION
  2817. public static ValueTask<float> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  2818. {
  2819. if (source == null)
  2820. throw Error.ArgumentNull(nameof(source));
  2821. if (selector == null)
  2822. throw Error.ArgumentNull(nameof(selector));
  2823. return Core(source, selector, cancellationToken);
  2824. static async ValueTask<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<float>> _selector, CancellationToken _cancellationToken)
  2825. {
  2826. float value;
  2827. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2828. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2829. {
  2830. if (!await e.MoveNextAsync())
  2831. {
  2832. throw Error.NoElements();
  2833. }
  2834. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2835. while (await e.MoveNextAsync())
  2836. {
  2837. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2838. if (x < value)
  2839. {
  2840. value = x;
  2841. }
  2842. else
  2843. {
  2844. // Normally NaN < anything is false, as is anything < NaN
  2845. // However, this leads to some irksome outcomes in Min and Max.
  2846. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2847. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2848. // ordering where NaN is smaller than every value, including
  2849. // negative infinity.
  2850. // Not testing for NaN therefore isn't an option, but since we
  2851. // can't find a smaller value, we can short-circuit.
  2852. if (float.IsNaN(x))
  2853. {
  2854. return x;
  2855. }
  2856. }
  2857. }
  2858. }
  2859. finally
  2860. {
  2861. await e.DisposeAsync();
  2862. }
  2863. return value;
  2864. }
  2865. }
  2866. #endif
  2867. public static ValueTask<float?> MinAsync(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken = default)
  2868. {
  2869. if (source == null)
  2870. throw Error.ArgumentNull(nameof(source));
  2871. return Core(source, cancellationToken);
  2872. static async ValueTask<float?> Core(IAsyncEnumerable<float?> _source, CancellationToken _cancellationToken)
  2873. {
  2874. float? value = null;
  2875. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2876. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2877. {
  2878. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2879. // so we don't have to keep testing for nullity.
  2880. do
  2881. {
  2882. if (!await e.MoveNextAsync())
  2883. {
  2884. return value;
  2885. }
  2886. value = e.Current;
  2887. }
  2888. while (!value.HasValue);
  2889. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2890. // using the lifted operation each time.
  2891. var valueVal = value.GetValueOrDefault();
  2892. while (await e.MoveNextAsync())
  2893. {
  2894. var cur = e.Current;
  2895. if (cur.HasValue)
  2896. {
  2897. var x = cur.GetValueOrDefault();
  2898. if (x < valueVal)
  2899. {
  2900. valueVal = x;
  2901. value = cur;
  2902. }
  2903. else
  2904. {
  2905. // Normally NaN < anything is false, as is anything < NaN
  2906. // However, this leads to some irksome outcomes in Min and Max.
  2907. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2908. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2909. // ordering where NaN is smaller than every value, including
  2910. // negative infinity.
  2911. // Not testing for NaN therefore isn't an option, but since we
  2912. // can't find a smaller value, we can short-circuit.
  2913. if (float.IsNaN(x))
  2914. {
  2915. return cur;
  2916. }
  2917. }
  2918. }
  2919. }
  2920. }
  2921. finally
  2922. {
  2923. await e.DisposeAsync();
  2924. }
  2925. return value;
  2926. }
  2927. }
  2928. public static ValueTask<float?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken = default)
  2929. {
  2930. if (source == null)
  2931. throw Error.ArgumentNull(nameof(source));
  2932. if (selector == null)
  2933. throw Error.ArgumentNull(nameof(selector));
  2934. return Core(source, selector, cancellationToken);
  2935. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, float?> _selector, CancellationToken _cancellationToken)
  2936. {
  2937. float? value = null;
  2938. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  2939. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  2940. {
  2941. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2942. // so we don't have to keep testing for nullity.
  2943. do
  2944. {
  2945. if (!await e.MoveNextAsync())
  2946. {
  2947. return value;
  2948. }
  2949. value = _selector(e.Current);
  2950. }
  2951. while (!value.HasValue);
  2952. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2953. // using the lifted operation each time.
  2954. var valueVal = value.GetValueOrDefault();
  2955. while (await e.MoveNextAsync())
  2956. {
  2957. var cur = _selector(e.Current);
  2958. if (cur.HasValue)
  2959. {
  2960. var x = cur.GetValueOrDefault();
  2961. if (x < valueVal)
  2962. {
  2963. valueVal = x;
  2964. value = cur;
  2965. }
  2966. else
  2967. {
  2968. // Normally NaN < anything is false, as is anything < NaN
  2969. // However, this leads to some irksome outcomes in Min and Max.
  2970. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2971. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2972. // ordering where NaN is smaller than every value, including
  2973. // negative infinity.
  2974. // Not testing for NaN therefore isn't an option, but since we
  2975. // can't find a smaller value, we can short-circuit.
  2976. if (float.IsNaN(x))
  2977. {
  2978. return cur;
  2979. }
  2980. }
  2981. }
  2982. }
  2983. }
  2984. finally
  2985. {
  2986. await e.DisposeAsync();
  2987. }
  2988. return value;
  2989. }
  2990. }
  2991. public static ValueTask<float?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  2992. {
  2993. if (source == null)
  2994. throw Error.ArgumentNull(nameof(source));
  2995. if (selector == null)
  2996. throw Error.ArgumentNull(nameof(selector));
  2997. return Core(source, selector, cancellationToken);
  2998. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<float?>> _selector, CancellationToken _cancellationToken)
  2999. {
  3000. float? value = null;
  3001. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3002. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3003. {
  3004. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3005. // so we don't have to keep testing for nullity.
  3006. do
  3007. {
  3008. if (!await e.MoveNextAsync())
  3009. {
  3010. return value;
  3011. }
  3012. value = await _selector(e.Current).ConfigureAwait(false);
  3013. }
  3014. while (!value.HasValue);
  3015. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3016. // using the lifted operation each time.
  3017. var valueVal = value.GetValueOrDefault();
  3018. while (await e.MoveNextAsync())
  3019. {
  3020. var cur = await _selector(e.Current).ConfigureAwait(false);
  3021. if (cur.HasValue)
  3022. {
  3023. var x = cur.GetValueOrDefault();
  3024. if (x < valueVal)
  3025. {
  3026. valueVal = x;
  3027. value = cur;
  3028. }
  3029. else
  3030. {
  3031. // Normally NaN < anything is false, as is anything < NaN
  3032. // However, this leads to some irksome outcomes in Min and Max.
  3033. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3034. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3035. // ordering where NaN is smaller than every value, including
  3036. // negative infinity.
  3037. // Not testing for NaN therefore isn't an option, but since we
  3038. // can't find a smaller value, we can short-circuit.
  3039. if (float.IsNaN(x))
  3040. {
  3041. return cur;
  3042. }
  3043. }
  3044. }
  3045. }
  3046. }
  3047. finally
  3048. {
  3049. await e.DisposeAsync();
  3050. }
  3051. return value;
  3052. }
  3053. }
  3054. #if !NO_DEEP_CANCELLATION
  3055. public static ValueTask<float?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  3056. {
  3057. if (source == null)
  3058. throw Error.ArgumentNull(nameof(source));
  3059. if (selector == null)
  3060. throw Error.ArgumentNull(nameof(selector));
  3061. return Core(source, selector, cancellationToken);
  3062. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<float?>> _selector, CancellationToken _cancellationToken)
  3063. {
  3064. float? value = null;
  3065. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3066. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3067. {
  3068. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3069. // so we don't have to keep testing for nullity.
  3070. do
  3071. {
  3072. if (!await e.MoveNextAsync())
  3073. {
  3074. return value;
  3075. }
  3076. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3077. }
  3078. while (!value.HasValue);
  3079. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3080. // using the lifted operation each time.
  3081. var valueVal = value.GetValueOrDefault();
  3082. while (await e.MoveNextAsync())
  3083. {
  3084. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3085. if (cur.HasValue)
  3086. {
  3087. var x = cur.GetValueOrDefault();
  3088. if (x < valueVal)
  3089. {
  3090. valueVal = x;
  3091. value = cur;
  3092. }
  3093. else
  3094. {
  3095. // Normally NaN < anything is false, as is anything < NaN
  3096. // However, this leads to some irksome outcomes in Min and Max.
  3097. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3098. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3099. // ordering where NaN is smaller than every value, including
  3100. // negative infinity.
  3101. // Not testing for NaN therefore isn't an option, but since we
  3102. // can't find a smaller value, we can short-circuit.
  3103. if (float.IsNaN(x))
  3104. {
  3105. return cur;
  3106. }
  3107. }
  3108. }
  3109. }
  3110. }
  3111. finally
  3112. {
  3113. await e.DisposeAsync();
  3114. }
  3115. return value;
  3116. }
  3117. }
  3118. #endif
  3119. public static ValueTask<double> MinAsync(this IAsyncEnumerable<double> source, CancellationToken cancellationToken = default)
  3120. {
  3121. if (source == null)
  3122. throw Error.ArgumentNull(nameof(source));
  3123. return Core(source, cancellationToken);
  3124. static async ValueTask<double> Core(IAsyncEnumerable<double> _source, CancellationToken _cancellationToken)
  3125. {
  3126. double value;
  3127. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3128. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3129. {
  3130. if (!await e.MoveNextAsync())
  3131. {
  3132. throw Error.NoElements();
  3133. }
  3134. value = e.Current;
  3135. while (await e.MoveNextAsync())
  3136. {
  3137. var x = e.Current;
  3138. if (x < value)
  3139. {
  3140. value = x;
  3141. }
  3142. else
  3143. {
  3144. // Normally NaN < anything is false, as is anything < NaN
  3145. // However, this leads to some irksome outcomes in Min and Max.
  3146. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3147. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3148. // ordering where NaN is smaller than every value, including
  3149. // negative infinity.
  3150. // Not testing for NaN therefore isn't an option, but since we
  3151. // can't find a smaller value, we can short-circuit.
  3152. if (double.IsNaN(x))
  3153. {
  3154. return x;
  3155. }
  3156. }
  3157. }
  3158. }
  3159. finally
  3160. {
  3161. await e.DisposeAsync();
  3162. }
  3163. return value;
  3164. }
  3165. }
  3166. public static ValueTask<double> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken = default)
  3167. {
  3168. if (source == null)
  3169. throw Error.ArgumentNull(nameof(source));
  3170. if (selector == null)
  3171. throw Error.ArgumentNull(nameof(selector));
  3172. return Core(source, selector, cancellationToken);
  3173. static async ValueTask<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, double> _selector, CancellationToken _cancellationToken)
  3174. {
  3175. double value;
  3176. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3177. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3178. {
  3179. if (!await e.MoveNextAsync())
  3180. {
  3181. throw Error.NoElements();
  3182. }
  3183. value = _selector(e.Current);
  3184. while (await e.MoveNextAsync())
  3185. {
  3186. var x = _selector(e.Current);
  3187. if (x < value)
  3188. {
  3189. value = x;
  3190. }
  3191. else
  3192. {
  3193. // Normally NaN < anything is false, as is anything < NaN
  3194. // However, this leads to some irksome outcomes in Min and Max.
  3195. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3196. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3197. // ordering where NaN is smaller than every value, including
  3198. // negative infinity.
  3199. // Not testing for NaN therefore isn't an option, but since we
  3200. // can't find a smaller value, we can short-circuit.
  3201. if (double.IsNaN(x))
  3202. {
  3203. return x;
  3204. }
  3205. }
  3206. }
  3207. }
  3208. finally
  3209. {
  3210. await e.DisposeAsync();
  3211. }
  3212. return value;
  3213. }
  3214. }
  3215. public static ValueTask<double> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  3216. {
  3217. if (source == null)
  3218. throw Error.ArgumentNull(nameof(source));
  3219. if (selector == null)
  3220. throw Error.ArgumentNull(nameof(selector));
  3221. return Core(source, selector, cancellationToken);
  3222. static async ValueTask<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<double>> _selector, CancellationToken _cancellationToken)
  3223. {
  3224. double value;
  3225. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3226. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3227. {
  3228. if (!await e.MoveNextAsync())
  3229. {
  3230. throw Error.NoElements();
  3231. }
  3232. value = await _selector(e.Current).ConfigureAwait(false);
  3233. while (await e.MoveNextAsync())
  3234. {
  3235. var x = await _selector(e.Current).ConfigureAwait(false);
  3236. if (x < value)
  3237. {
  3238. value = x;
  3239. }
  3240. else
  3241. {
  3242. // Normally NaN < anything is false, as is anything < NaN
  3243. // However, this leads to some irksome outcomes in Min and Max.
  3244. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3245. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3246. // ordering where NaN is smaller than every value, including
  3247. // negative infinity.
  3248. // Not testing for NaN therefore isn't an option, but since we
  3249. // can't find a smaller value, we can short-circuit.
  3250. if (double.IsNaN(x))
  3251. {
  3252. return x;
  3253. }
  3254. }
  3255. }
  3256. }
  3257. finally
  3258. {
  3259. await e.DisposeAsync();
  3260. }
  3261. return value;
  3262. }
  3263. }
  3264. #if !NO_DEEP_CANCELLATION
  3265. public static ValueTask<double> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  3266. {
  3267. if (source == null)
  3268. throw Error.ArgumentNull(nameof(source));
  3269. if (selector == null)
  3270. throw Error.ArgumentNull(nameof(selector));
  3271. return Core(source, selector, cancellationToken);
  3272. static async ValueTask<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<double>> _selector, CancellationToken _cancellationToken)
  3273. {
  3274. double value;
  3275. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3276. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3277. {
  3278. if (!await e.MoveNextAsync())
  3279. {
  3280. throw Error.NoElements();
  3281. }
  3282. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3283. while (await e.MoveNextAsync())
  3284. {
  3285. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3286. if (x < value)
  3287. {
  3288. value = x;
  3289. }
  3290. else
  3291. {
  3292. // Normally NaN < anything is false, as is anything < NaN
  3293. // However, this leads to some irksome outcomes in Min and Max.
  3294. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3295. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3296. // ordering where NaN is smaller than every value, including
  3297. // negative infinity.
  3298. // Not testing for NaN therefore isn't an option, but since we
  3299. // can't find a smaller value, we can short-circuit.
  3300. if (double.IsNaN(x))
  3301. {
  3302. return x;
  3303. }
  3304. }
  3305. }
  3306. }
  3307. finally
  3308. {
  3309. await e.DisposeAsync();
  3310. }
  3311. return value;
  3312. }
  3313. }
  3314. #endif
  3315. public static ValueTask<double?> MinAsync(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken = default)
  3316. {
  3317. if (source == null)
  3318. throw Error.ArgumentNull(nameof(source));
  3319. return Core(source, cancellationToken);
  3320. static async ValueTask<double?> Core(IAsyncEnumerable<double?> _source, CancellationToken _cancellationToken)
  3321. {
  3322. double? value = null;
  3323. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3324. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3325. {
  3326. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3327. // so we don't have to keep testing for nullity.
  3328. do
  3329. {
  3330. if (!await e.MoveNextAsync())
  3331. {
  3332. return value;
  3333. }
  3334. value = e.Current;
  3335. }
  3336. while (!value.HasValue);
  3337. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3338. // using the lifted operation each time.
  3339. var valueVal = value.GetValueOrDefault();
  3340. while (await e.MoveNextAsync())
  3341. {
  3342. var cur = e.Current;
  3343. if (cur.HasValue)
  3344. {
  3345. var x = cur.GetValueOrDefault();
  3346. if (x < valueVal)
  3347. {
  3348. valueVal = x;
  3349. value = cur;
  3350. }
  3351. else
  3352. {
  3353. // Normally NaN < anything is false, as is anything < NaN
  3354. // However, this leads to some irksome outcomes in Min and Max.
  3355. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3356. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3357. // ordering where NaN is smaller than every value, including
  3358. // negative infinity.
  3359. // Not testing for NaN therefore isn't an option, but since we
  3360. // can't find a smaller value, we can short-circuit.
  3361. if (double.IsNaN(x))
  3362. {
  3363. return cur;
  3364. }
  3365. }
  3366. }
  3367. }
  3368. }
  3369. finally
  3370. {
  3371. await e.DisposeAsync();
  3372. }
  3373. return value;
  3374. }
  3375. }
  3376. public static ValueTask<double?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
  3377. {
  3378. if (source == null)
  3379. throw Error.ArgumentNull(nameof(source));
  3380. if (selector == null)
  3381. throw Error.ArgumentNull(nameof(selector));
  3382. return Core(source, selector, cancellationToken);
  3383. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, double?> _selector, CancellationToken _cancellationToken)
  3384. {
  3385. double? value = null;
  3386. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3387. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3388. {
  3389. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3390. // so we don't have to keep testing for nullity.
  3391. do
  3392. {
  3393. if (!await e.MoveNextAsync())
  3394. {
  3395. return value;
  3396. }
  3397. value = _selector(e.Current);
  3398. }
  3399. while (!value.HasValue);
  3400. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3401. // using the lifted operation each time.
  3402. var valueVal = value.GetValueOrDefault();
  3403. while (await e.MoveNextAsync())
  3404. {
  3405. var cur = _selector(e.Current);
  3406. if (cur.HasValue)
  3407. {
  3408. var x = cur.GetValueOrDefault();
  3409. if (x < valueVal)
  3410. {
  3411. valueVal = x;
  3412. value = cur;
  3413. }
  3414. else
  3415. {
  3416. // Normally NaN < anything is false, as is anything < NaN
  3417. // However, this leads to some irksome outcomes in Min and Max.
  3418. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3419. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3420. // ordering where NaN is smaller than every value, including
  3421. // negative infinity.
  3422. // Not testing for NaN therefore isn't an option, but since we
  3423. // can't find a smaller value, we can short-circuit.
  3424. if (double.IsNaN(x))
  3425. {
  3426. return cur;
  3427. }
  3428. }
  3429. }
  3430. }
  3431. }
  3432. finally
  3433. {
  3434. await e.DisposeAsync();
  3435. }
  3436. return value;
  3437. }
  3438. }
  3439. public static ValueTask<double?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  3440. {
  3441. if (source == null)
  3442. throw Error.ArgumentNull(nameof(source));
  3443. if (selector == null)
  3444. throw Error.ArgumentNull(nameof(selector));
  3445. return Core(source, selector, cancellationToken);
  3446. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<double?>> _selector, CancellationToken _cancellationToken)
  3447. {
  3448. double? value = null;
  3449. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3450. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3451. {
  3452. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3453. // so we don't have to keep testing for nullity.
  3454. do
  3455. {
  3456. if (!await e.MoveNextAsync())
  3457. {
  3458. return value;
  3459. }
  3460. value = await _selector(e.Current).ConfigureAwait(false);
  3461. }
  3462. while (!value.HasValue);
  3463. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3464. // using the lifted operation each time.
  3465. var valueVal = value.GetValueOrDefault();
  3466. while (await e.MoveNextAsync())
  3467. {
  3468. var cur = await _selector(e.Current).ConfigureAwait(false);
  3469. if (cur.HasValue)
  3470. {
  3471. var x = cur.GetValueOrDefault();
  3472. if (x < valueVal)
  3473. {
  3474. valueVal = x;
  3475. value = cur;
  3476. }
  3477. else
  3478. {
  3479. // Normally NaN < anything is false, as is anything < NaN
  3480. // However, this leads to some irksome outcomes in Min and Max.
  3481. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3482. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3483. // ordering where NaN is smaller than every value, including
  3484. // negative infinity.
  3485. // Not testing for NaN therefore isn't an option, but since we
  3486. // can't find a smaller value, we can short-circuit.
  3487. if (double.IsNaN(x))
  3488. {
  3489. return cur;
  3490. }
  3491. }
  3492. }
  3493. }
  3494. }
  3495. finally
  3496. {
  3497. await e.DisposeAsync();
  3498. }
  3499. return value;
  3500. }
  3501. }
  3502. #if !NO_DEEP_CANCELLATION
  3503. public static ValueTask<double?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  3504. {
  3505. if (source == null)
  3506. throw Error.ArgumentNull(nameof(source));
  3507. if (selector == null)
  3508. throw Error.ArgumentNull(nameof(selector));
  3509. return Core(source, selector, cancellationToken);
  3510. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<double?>> _selector, CancellationToken _cancellationToken)
  3511. {
  3512. double? value = null;
  3513. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3514. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3515. {
  3516. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3517. // so we don't have to keep testing for nullity.
  3518. do
  3519. {
  3520. if (!await e.MoveNextAsync())
  3521. {
  3522. return value;
  3523. }
  3524. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3525. }
  3526. while (!value.HasValue);
  3527. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3528. // using the lifted operation each time.
  3529. var valueVal = value.GetValueOrDefault();
  3530. while (await e.MoveNextAsync())
  3531. {
  3532. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3533. if (cur.HasValue)
  3534. {
  3535. var x = cur.GetValueOrDefault();
  3536. if (x < valueVal)
  3537. {
  3538. valueVal = x;
  3539. value = cur;
  3540. }
  3541. else
  3542. {
  3543. // Normally NaN < anything is false, as is anything < NaN
  3544. // However, this leads to some irksome outcomes in Min and Max.
  3545. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3546. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3547. // ordering where NaN is smaller than every value, including
  3548. // negative infinity.
  3549. // Not testing for NaN therefore isn't an option, but since we
  3550. // can't find a smaller value, we can short-circuit.
  3551. if (double.IsNaN(x))
  3552. {
  3553. return cur;
  3554. }
  3555. }
  3556. }
  3557. }
  3558. }
  3559. finally
  3560. {
  3561. await e.DisposeAsync();
  3562. }
  3563. return value;
  3564. }
  3565. }
  3566. #endif
  3567. public static ValueTask<decimal> MinAsync(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken = default)
  3568. {
  3569. if (source == null)
  3570. throw Error.ArgumentNull(nameof(source));
  3571. return Core(source, cancellationToken);
  3572. static async ValueTask<decimal> Core(IAsyncEnumerable<decimal> _source, CancellationToken _cancellationToken)
  3573. {
  3574. decimal value;
  3575. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3576. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3577. {
  3578. if (!await e.MoveNextAsync())
  3579. {
  3580. throw Error.NoElements();
  3581. }
  3582. value = e.Current;
  3583. while (await e.MoveNextAsync())
  3584. {
  3585. var x = e.Current;
  3586. if (x < value)
  3587. {
  3588. value = x;
  3589. }
  3590. }
  3591. }
  3592. finally
  3593. {
  3594. await e.DisposeAsync();
  3595. }
  3596. return value;
  3597. }
  3598. }
  3599. public static ValueTask<decimal> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken = default)
  3600. {
  3601. if (source == null)
  3602. throw Error.ArgumentNull(nameof(source));
  3603. if (selector == null)
  3604. throw Error.ArgumentNull(nameof(selector));
  3605. return Core(source, selector, cancellationToken);
  3606. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, decimal> _selector, CancellationToken _cancellationToken)
  3607. {
  3608. decimal value;
  3609. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3610. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3611. {
  3612. if (!await e.MoveNextAsync())
  3613. {
  3614. throw Error.NoElements();
  3615. }
  3616. value = _selector(e.Current);
  3617. while (await e.MoveNextAsync())
  3618. {
  3619. var x = _selector(e.Current);
  3620. if (x < value)
  3621. {
  3622. value = x;
  3623. }
  3624. }
  3625. }
  3626. finally
  3627. {
  3628. await e.DisposeAsync();
  3629. }
  3630. return value;
  3631. }
  3632. }
  3633. public static ValueTask<decimal> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  3634. {
  3635. if (source == null)
  3636. throw Error.ArgumentNull(nameof(source));
  3637. if (selector == null)
  3638. throw Error.ArgumentNull(nameof(selector));
  3639. return Core(source, selector, cancellationToken);
  3640. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<decimal>> _selector, CancellationToken _cancellationToken)
  3641. {
  3642. decimal value;
  3643. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3644. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3645. {
  3646. if (!await e.MoveNextAsync())
  3647. {
  3648. throw Error.NoElements();
  3649. }
  3650. value = await _selector(e.Current).ConfigureAwait(false);
  3651. while (await e.MoveNextAsync())
  3652. {
  3653. var x = await _selector(e.Current).ConfigureAwait(false);
  3654. if (x < value)
  3655. {
  3656. value = x;
  3657. }
  3658. }
  3659. }
  3660. finally
  3661. {
  3662. await e.DisposeAsync();
  3663. }
  3664. return value;
  3665. }
  3666. }
  3667. #if !NO_DEEP_CANCELLATION
  3668. public static ValueTask<decimal> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  3669. {
  3670. if (source == null)
  3671. throw Error.ArgumentNull(nameof(source));
  3672. if (selector == null)
  3673. throw Error.ArgumentNull(nameof(selector));
  3674. return Core(source, selector, cancellationToken);
  3675. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<decimal>> _selector, CancellationToken _cancellationToken)
  3676. {
  3677. decimal value;
  3678. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3679. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3680. {
  3681. if (!await e.MoveNextAsync())
  3682. {
  3683. throw Error.NoElements();
  3684. }
  3685. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3686. while (await e.MoveNextAsync())
  3687. {
  3688. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3689. if (x < value)
  3690. {
  3691. value = x;
  3692. }
  3693. }
  3694. }
  3695. finally
  3696. {
  3697. await e.DisposeAsync();
  3698. }
  3699. return value;
  3700. }
  3701. }
  3702. #endif
  3703. public static ValueTask<decimal?> MinAsync(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken = default)
  3704. {
  3705. if (source == null)
  3706. throw Error.ArgumentNull(nameof(source));
  3707. return Core(source, cancellationToken);
  3708. static async ValueTask<decimal?> Core(IAsyncEnumerable<decimal?> _source, CancellationToken _cancellationToken)
  3709. {
  3710. decimal? value = null;
  3711. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3712. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3713. {
  3714. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3715. // so we don't have to keep testing for nullity.
  3716. do
  3717. {
  3718. if (!await e.MoveNextAsync())
  3719. {
  3720. return value;
  3721. }
  3722. value = e.Current;
  3723. }
  3724. while (!value.HasValue);
  3725. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3726. // using the lifted operation each time.
  3727. var valueVal = value.GetValueOrDefault();
  3728. while (await e.MoveNextAsync())
  3729. {
  3730. var cur = e.Current;
  3731. var x = cur.GetValueOrDefault();
  3732. if (cur.HasValue && x < valueVal)
  3733. {
  3734. valueVal = x;
  3735. value = cur;
  3736. }
  3737. }
  3738. }
  3739. finally
  3740. {
  3741. await e.DisposeAsync();
  3742. }
  3743. return value;
  3744. }
  3745. }
  3746. public static ValueTask<decimal?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken = default)
  3747. {
  3748. if (source == null)
  3749. throw Error.ArgumentNull(nameof(source));
  3750. if (selector == null)
  3751. throw Error.ArgumentNull(nameof(selector));
  3752. return Core(source, selector, cancellationToken);
  3753. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, decimal?> _selector, CancellationToken _cancellationToken)
  3754. {
  3755. decimal? value = null;
  3756. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3757. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3758. {
  3759. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3760. // so we don't have to keep testing for nullity.
  3761. do
  3762. {
  3763. if (!await e.MoveNextAsync())
  3764. {
  3765. return value;
  3766. }
  3767. value = _selector(e.Current);
  3768. }
  3769. while (!value.HasValue);
  3770. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3771. // using the lifted operation each time.
  3772. var valueVal = value.GetValueOrDefault();
  3773. while (await e.MoveNextAsync())
  3774. {
  3775. var cur = _selector(e.Current);
  3776. var x = cur.GetValueOrDefault();
  3777. if (cur.HasValue && x < valueVal)
  3778. {
  3779. valueVal = x;
  3780. value = cur;
  3781. }
  3782. }
  3783. }
  3784. finally
  3785. {
  3786. await e.DisposeAsync();
  3787. }
  3788. return value;
  3789. }
  3790. }
  3791. public static ValueTask<decimal?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  3792. {
  3793. if (source == null)
  3794. throw Error.ArgumentNull(nameof(source));
  3795. if (selector == null)
  3796. throw Error.ArgumentNull(nameof(selector));
  3797. return Core(source, selector, cancellationToken);
  3798. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<decimal?>> _selector, CancellationToken _cancellationToken)
  3799. {
  3800. decimal? value = null;
  3801. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3802. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3803. {
  3804. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3805. // so we don't have to keep testing for nullity.
  3806. do
  3807. {
  3808. if (!await e.MoveNextAsync())
  3809. {
  3810. return value;
  3811. }
  3812. value = await _selector(e.Current).ConfigureAwait(false);
  3813. }
  3814. while (!value.HasValue);
  3815. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3816. // using the lifted operation each time.
  3817. var valueVal = value.GetValueOrDefault();
  3818. while (await e.MoveNextAsync())
  3819. {
  3820. var cur = await _selector(e.Current).ConfigureAwait(false);
  3821. var x = cur.GetValueOrDefault();
  3822. if (cur.HasValue && x < valueVal)
  3823. {
  3824. valueVal = x;
  3825. value = cur;
  3826. }
  3827. }
  3828. }
  3829. finally
  3830. {
  3831. await e.DisposeAsync();
  3832. }
  3833. return value;
  3834. }
  3835. }
  3836. #if !NO_DEEP_CANCELLATION
  3837. public static ValueTask<decimal?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  3838. {
  3839. if (source == null)
  3840. throw Error.ArgumentNull(nameof(source));
  3841. if (selector == null)
  3842. throw Error.ArgumentNull(nameof(selector));
  3843. return Core(source, selector, cancellationToken);
  3844. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<decimal?>> _selector, CancellationToken _cancellationToken)
  3845. {
  3846. decimal? value = null;
  3847. var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false);
  3848. try // TODO: Switch to `await using` in preview 3 (https://github.com/dotnet/roslyn/pull/32731)
  3849. {
  3850. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3851. // so we don't have to keep testing for nullity.
  3852. do
  3853. {
  3854. if (!await e.MoveNextAsync())
  3855. {
  3856. return value;
  3857. }
  3858. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3859. }
  3860. while (!value.HasValue);
  3861. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3862. // using the lifted operation each time.
  3863. var valueVal = value.GetValueOrDefault();
  3864. while (await e.MoveNextAsync())
  3865. {
  3866. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3867. var x = cur.GetValueOrDefault();
  3868. if (cur.HasValue && x < valueVal)
  3869. {
  3870. valueVal = x;
  3871. value = cur;
  3872. }
  3873. }
  3874. }
  3875. finally
  3876. {
  3877. await e.DisposeAsync();
  3878. }
  3879. return value;
  3880. }
  3881. }
  3882. #endif
  3883. }
  3884. }