MinMax.Generated.cs 169 KB

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