MinMax.Generated.cs 189 KB

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