MinMax.Generated.cs 210 KB

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