MinMax.Generated.cs 181 KB

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