MinMax.Generated.cs 200 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576
  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. /// <summary>
  2067. /// Invokes and awaits a transform function on each element of a sequence and returns the minimum <see cref="int"/> value.
  2068. /// </summary>
  2069. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  2070. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  2071. /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
  2072. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  2073. /// <returns>A ValueTask sequence containing a single element with the value that corresponds to the minimum element in the source sequence.</returns>
  2074. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  2075. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  2076. [GenerateAsyncOverload]
  2077. private static ValueTask<int> MinAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  2078. {
  2079. if (source == null)
  2080. throw Error.ArgumentNull(nameof(source));
  2081. if (selector == null)
  2082. throw Error.ArgumentNull(nameof(selector));
  2083. return Core(source, selector, cancellationToken);
  2084. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken)
  2085. {
  2086. int value;
  2087. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2088. {
  2089. if (!await e.MoveNextAsync())
  2090. {
  2091. throw Error.NoElements();
  2092. }
  2093. value = await selector(e.Current).ConfigureAwait(false);
  2094. while (await e.MoveNextAsync())
  2095. {
  2096. var x = await selector(e.Current).ConfigureAwait(false);
  2097. if (x < value)
  2098. {
  2099. value = x;
  2100. }
  2101. }
  2102. }
  2103. return value;
  2104. }
  2105. }
  2106. #if !NO_DEEP_CANCELLATION
  2107. [GenerateAsyncOverload]
  2108. private static ValueTask<int> MinAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  2109. {
  2110. if (source == null)
  2111. throw Error.ArgumentNull(nameof(source));
  2112. if (selector == null)
  2113. throw Error.ArgumentNull(nameof(selector));
  2114. return Core(source, selector, cancellationToken);
  2115. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken)
  2116. {
  2117. int value;
  2118. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2119. {
  2120. if (!await e.MoveNextAsync())
  2121. {
  2122. throw Error.NoElements();
  2123. }
  2124. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  2125. while (await e.MoveNextAsync())
  2126. {
  2127. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  2128. if (x < value)
  2129. {
  2130. value = x;
  2131. }
  2132. }
  2133. }
  2134. return value;
  2135. }
  2136. }
  2137. #endif
  2138. /// <summary>
  2139. /// Returns the minimum value in an async-enumerable sequence of <see cref="Nullable{Int}" /> values.
  2140. /// </summary>
  2141. /// <param name="source">A sequence of <see cref="Nullable{Int}" /> values to determine the minimum value of.</param>
  2142. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  2143. /// <returns>A ValueTask containing a single element with the minimum value in the source sequence.</returns>
  2144. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  2145. public static ValueTask<int?> MinAsync(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken = default)
  2146. {
  2147. if (source == null)
  2148. throw Error.ArgumentNull(nameof(source));
  2149. return Core(source, cancellationToken);
  2150. static async ValueTask<int?> Core(IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  2151. {
  2152. int? value = null;
  2153. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2154. {
  2155. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2156. // so we don't have to keep testing for nullity.
  2157. do
  2158. {
  2159. if (!await e.MoveNextAsync())
  2160. {
  2161. return value;
  2162. }
  2163. value = e.Current;
  2164. }
  2165. while (!value.HasValue);
  2166. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2167. // using the lifted operation each time.
  2168. var valueVal = value.GetValueOrDefault();
  2169. while (await e.MoveNextAsync())
  2170. {
  2171. var cur = e.Current;
  2172. var x = cur.GetValueOrDefault();
  2173. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2174. // unless nulls either never happen or always happen.
  2175. if (cur.HasValue & x < valueVal)
  2176. {
  2177. valueVal = x;
  2178. value = cur;
  2179. }
  2180. }
  2181. }
  2182. return value;
  2183. }
  2184. }
  2185. public static ValueTask<int?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken = default)
  2186. {
  2187. if (source == null)
  2188. throw Error.ArgumentNull(nameof(source));
  2189. if (selector == null)
  2190. throw Error.ArgumentNull(nameof(selector));
  2191. return Core(source, selector, cancellationToken);
  2192. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  2193. {
  2194. int? value = null;
  2195. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2196. {
  2197. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2198. // so we don't have to keep testing for nullity.
  2199. do
  2200. {
  2201. if (!await e.MoveNextAsync())
  2202. {
  2203. return value;
  2204. }
  2205. value = selector(e.Current);
  2206. }
  2207. while (!value.HasValue);
  2208. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2209. // using the lifted operation each time.
  2210. var valueVal = value.GetValueOrDefault();
  2211. while (await e.MoveNextAsync())
  2212. {
  2213. var cur = selector(e.Current);
  2214. var x = cur.GetValueOrDefault();
  2215. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2216. // unless nulls either never happen or always happen.
  2217. if (cur.HasValue & x < valueVal)
  2218. {
  2219. valueVal = x;
  2220. value = cur;
  2221. }
  2222. }
  2223. }
  2224. return value;
  2225. }
  2226. }
  2227. /// <summary>
  2228. /// Invokes and awaits a transform function on each element of a sequence and returns the minimum <see cref="Nullable{Int}"/> value.
  2229. /// </summary>
  2230. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  2231. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  2232. /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
  2233. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  2234. /// <returns>A ValueTask sequence containing a single element with the value that corresponds to the minimum element in the source sequence.</returns>
  2235. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  2236. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  2237. [GenerateAsyncOverload]
  2238. private static ValueTask<int?> MinAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  2239. {
  2240. if (source == null)
  2241. throw Error.ArgumentNull(nameof(source));
  2242. if (selector == null)
  2243. throw Error.ArgumentNull(nameof(selector));
  2244. return Core(source, selector, cancellationToken);
  2245. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken)
  2246. {
  2247. int? value = null;
  2248. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2249. {
  2250. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2251. // so we don't have to keep testing for nullity.
  2252. do
  2253. {
  2254. if (!await e.MoveNextAsync())
  2255. {
  2256. return value;
  2257. }
  2258. value = await selector(e.Current).ConfigureAwait(false);
  2259. }
  2260. while (!value.HasValue);
  2261. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2262. // using the lifted operation each time.
  2263. var valueVal = value.GetValueOrDefault();
  2264. while (await e.MoveNextAsync())
  2265. {
  2266. var cur = await selector(e.Current).ConfigureAwait(false);
  2267. var x = cur.GetValueOrDefault();
  2268. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2269. // unless nulls either never happen or always happen.
  2270. if (cur.HasValue & x < valueVal)
  2271. {
  2272. valueVal = x;
  2273. value = cur;
  2274. }
  2275. }
  2276. }
  2277. return value;
  2278. }
  2279. }
  2280. #if !NO_DEEP_CANCELLATION
  2281. [GenerateAsyncOverload]
  2282. private static ValueTask<int?> MinAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  2283. {
  2284. if (source == null)
  2285. throw Error.ArgumentNull(nameof(source));
  2286. if (selector == null)
  2287. throw Error.ArgumentNull(nameof(selector));
  2288. return Core(source, selector, cancellationToken);
  2289. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken)
  2290. {
  2291. int? value = null;
  2292. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2293. {
  2294. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2295. // so we don't have to keep testing for nullity.
  2296. do
  2297. {
  2298. if (!await e.MoveNextAsync())
  2299. {
  2300. return value;
  2301. }
  2302. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  2303. }
  2304. while (!value.HasValue);
  2305. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2306. // using the lifted operation each time.
  2307. var valueVal = value.GetValueOrDefault();
  2308. while (await e.MoveNextAsync())
  2309. {
  2310. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  2311. var x = cur.GetValueOrDefault();
  2312. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2313. // unless nulls either never happen or always happen.
  2314. if (cur.HasValue & x < valueVal)
  2315. {
  2316. valueVal = x;
  2317. value = cur;
  2318. }
  2319. }
  2320. }
  2321. return value;
  2322. }
  2323. }
  2324. #endif
  2325. /// <summary>
  2326. /// Returns the minimum value in an async-enumerable sequence of <see cref="long" /> values.
  2327. /// </summary>
  2328. /// <param name="source">A sequence of <see cref="long" /> values to determine the minimum value of.</param>
  2329. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  2330. /// <returns>A ValueTask containing a single element with the minimum value in the source sequence.</returns>
  2331. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  2332. public static ValueTask<long> MinAsync(this IAsyncEnumerable<long> source, CancellationToken cancellationToken = default)
  2333. {
  2334. if (source == null)
  2335. throw Error.ArgumentNull(nameof(source));
  2336. return Core(source, cancellationToken);
  2337. static async ValueTask<long> Core(IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  2338. {
  2339. long value;
  2340. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2341. {
  2342. if (!await e.MoveNextAsync())
  2343. {
  2344. throw Error.NoElements();
  2345. }
  2346. value = e.Current;
  2347. while (await e.MoveNextAsync())
  2348. {
  2349. var x = e.Current;
  2350. if (x < value)
  2351. {
  2352. value = x;
  2353. }
  2354. }
  2355. }
  2356. return value;
  2357. }
  2358. }
  2359. public static ValueTask<long> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken = default)
  2360. {
  2361. if (source == null)
  2362. throw Error.ArgumentNull(nameof(source));
  2363. if (selector == null)
  2364. throw Error.ArgumentNull(nameof(selector));
  2365. return Core(source, selector, cancellationToken);
  2366. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  2367. {
  2368. long value;
  2369. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2370. {
  2371. if (!await e.MoveNextAsync())
  2372. {
  2373. throw Error.NoElements();
  2374. }
  2375. value = selector(e.Current);
  2376. while (await e.MoveNextAsync())
  2377. {
  2378. var x = selector(e.Current);
  2379. if (x < value)
  2380. {
  2381. value = x;
  2382. }
  2383. }
  2384. }
  2385. return value;
  2386. }
  2387. }
  2388. /// <summary>
  2389. /// Invokes and awaits a transform function on each element of a sequence and returns the minimum <see cref="long"/> value.
  2390. /// </summary>
  2391. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  2392. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  2393. /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
  2394. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  2395. /// <returns>A ValueTask sequence containing a single element with the value that corresponds to the minimum element in the source sequence.</returns>
  2396. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  2397. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  2398. [GenerateAsyncOverload]
  2399. private static ValueTask<long> MinAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  2400. {
  2401. if (source == null)
  2402. throw Error.ArgumentNull(nameof(source));
  2403. if (selector == null)
  2404. throw Error.ArgumentNull(nameof(selector));
  2405. return Core(source, selector, cancellationToken);
  2406. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken)
  2407. {
  2408. long value;
  2409. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2410. {
  2411. if (!await e.MoveNextAsync())
  2412. {
  2413. throw Error.NoElements();
  2414. }
  2415. value = await selector(e.Current).ConfigureAwait(false);
  2416. while (await e.MoveNextAsync())
  2417. {
  2418. var x = await selector(e.Current).ConfigureAwait(false);
  2419. if (x < value)
  2420. {
  2421. value = x;
  2422. }
  2423. }
  2424. }
  2425. return value;
  2426. }
  2427. }
  2428. #if !NO_DEEP_CANCELLATION
  2429. [GenerateAsyncOverload]
  2430. private static ValueTask<long> MinAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  2431. {
  2432. if (source == null)
  2433. throw Error.ArgumentNull(nameof(source));
  2434. if (selector == null)
  2435. throw Error.ArgumentNull(nameof(selector));
  2436. return Core(source, selector, cancellationToken);
  2437. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken)
  2438. {
  2439. long value;
  2440. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2441. {
  2442. if (!await e.MoveNextAsync())
  2443. {
  2444. throw Error.NoElements();
  2445. }
  2446. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  2447. while (await e.MoveNextAsync())
  2448. {
  2449. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  2450. if (x < value)
  2451. {
  2452. value = x;
  2453. }
  2454. }
  2455. }
  2456. return value;
  2457. }
  2458. }
  2459. #endif
  2460. /// <summary>
  2461. /// Returns the minimum value in an async-enumerable sequence of <see cref="Nullable{Long}" /> values.
  2462. /// </summary>
  2463. /// <param name="source">A sequence of <see cref="Nullable{Long}" /> values to determine the minimum value of.</param>
  2464. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  2465. /// <returns>A ValueTask containing a single element with the minimum value in the source sequence.</returns>
  2466. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  2467. public static ValueTask<long?> MinAsync(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken = default)
  2468. {
  2469. if (source == null)
  2470. throw Error.ArgumentNull(nameof(source));
  2471. return Core(source, cancellationToken);
  2472. static async ValueTask<long?> Core(IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  2473. {
  2474. long? value = null;
  2475. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2476. {
  2477. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2478. // so we don't have to keep testing for nullity.
  2479. do
  2480. {
  2481. if (!await e.MoveNextAsync())
  2482. {
  2483. return value;
  2484. }
  2485. value = e.Current;
  2486. }
  2487. while (!value.HasValue);
  2488. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2489. // using the lifted operation each time.
  2490. var valueVal = value.GetValueOrDefault();
  2491. while (await e.MoveNextAsync())
  2492. {
  2493. var cur = e.Current;
  2494. var x = cur.GetValueOrDefault();
  2495. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2496. // unless nulls either never happen or always happen.
  2497. if (cur.HasValue & x < valueVal)
  2498. {
  2499. valueVal = x;
  2500. value = cur;
  2501. }
  2502. }
  2503. }
  2504. return value;
  2505. }
  2506. }
  2507. public static ValueTask<long?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken = default)
  2508. {
  2509. if (source == null)
  2510. throw Error.ArgumentNull(nameof(source));
  2511. if (selector == null)
  2512. throw Error.ArgumentNull(nameof(selector));
  2513. return Core(source, selector, cancellationToken);
  2514. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  2515. {
  2516. long? value = null;
  2517. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2518. {
  2519. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2520. // so we don't have to keep testing for nullity.
  2521. do
  2522. {
  2523. if (!await e.MoveNextAsync())
  2524. {
  2525. return value;
  2526. }
  2527. value = selector(e.Current);
  2528. }
  2529. while (!value.HasValue);
  2530. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2531. // using the lifted operation each time.
  2532. var valueVal = value.GetValueOrDefault();
  2533. while (await e.MoveNextAsync())
  2534. {
  2535. var cur = selector(e.Current);
  2536. var x = cur.GetValueOrDefault();
  2537. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2538. // unless nulls either never happen or always happen.
  2539. if (cur.HasValue & x < valueVal)
  2540. {
  2541. valueVal = x;
  2542. value = cur;
  2543. }
  2544. }
  2545. }
  2546. return value;
  2547. }
  2548. }
  2549. /// <summary>
  2550. /// Invokes and awaits a transform function on each element of a sequence and returns the minimum <see cref="Nullable{Long}"/> value.
  2551. /// </summary>
  2552. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  2553. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  2554. /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
  2555. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  2556. /// <returns>A ValueTask sequence containing a single element with the value that corresponds to the minimum element in the source sequence.</returns>
  2557. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  2558. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  2559. [GenerateAsyncOverload]
  2560. private static ValueTask<long?> MinAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  2561. {
  2562. if (source == null)
  2563. throw Error.ArgumentNull(nameof(source));
  2564. if (selector == null)
  2565. throw Error.ArgumentNull(nameof(selector));
  2566. return Core(source, selector, cancellationToken);
  2567. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken)
  2568. {
  2569. long? value = null;
  2570. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2571. {
  2572. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2573. // so we don't have to keep testing for nullity.
  2574. do
  2575. {
  2576. if (!await e.MoveNextAsync())
  2577. {
  2578. return value;
  2579. }
  2580. value = await selector(e.Current).ConfigureAwait(false);
  2581. }
  2582. while (!value.HasValue);
  2583. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2584. // using the lifted operation each time.
  2585. var valueVal = value.GetValueOrDefault();
  2586. while (await e.MoveNextAsync())
  2587. {
  2588. var cur = await selector(e.Current).ConfigureAwait(false);
  2589. var x = cur.GetValueOrDefault();
  2590. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2591. // unless nulls either never happen or always happen.
  2592. if (cur.HasValue & x < valueVal)
  2593. {
  2594. valueVal = x;
  2595. value = cur;
  2596. }
  2597. }
  2598. }
  2599. return value;
  2600. }
  2601. }
  2602. #if !NO_DEEP_CANCELLATION
  2603. [GenerateAsyncOverload]
  2604. private static ValueTask<long?> MinAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  2605. {
  2606. if (source == null)
  2607. throw Error.ArgumentNull(nameof(source));
  2608. if (selector == null)
  2609. throw Error.ArgumentNull(nameof(selector));
  2610. return Core(source, selector, cancellationToken);
  2611. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken)
  2612. {
  2613. long? value = null;
  2614. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2615. {
  2616. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2617. // so we don't have to keep testing for nullity.
  2618. do
  2619. {
  2620. if (!await e.MoveNextAsync())
  2621. {
  2622. return value;
  2623. }
  2624. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  2625. }
  2626. while (!value.HasValue);
  2627. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2628. // using the lifted operation each time.
  2629. var valueVal = value.GetValueOrDefault();
  2630. while (await e.MoveNextAsync())
  2631. {
  2632. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  2633. var x = cur.GetValueOrDefault();
  2634. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2635. // unless nulls either never happen or always happen.
  2636. if (cur.HasValue & x < valueVal)
  2637. {
  2638. valueVal = x;
  2639. value = cur;
  2640. }
  2641. }
  2642. }
  2643. return value;
  2644. }
  2645. }
  2646. #endif
  2647. /// <summary>
  2648. /// Returns the minimum value in an async-enumerable sequence of <see cref="float" /> values.
  2649. /// </summary>
  2650. /// <param name="source">A sequence of <see cref="float" /> values to determine the minimum value of.</param>
  2651. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  2652. /// <returns>A ValueTask containing a single element with the minimum value in the source sequence.</returns>
  2653. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  2654. public static ValueTask<float> MinAsync(this IAsyncEnumerable<float> source, CancellationToken cancellationToken = default)
  2655. {
  2656. if (source == null)
  2657. throw Error.ArgumentNull(nameof(source));
  2658. return Core(source, cancellationToken);
  2659. static async ValueTask<float> Core(IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  2660. {
  2661. float value;
  2662. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2663. {
  2664. if (!await e.MoveNextAsync())
  2665. {
  2666. throw Error.NoElements();
  2667. }
  2668. value = e.Current;
  2669. while (await e.MoveNextAsync())
  2670. {
  2671. var x = e.Current;
  2672. if (x < value)
  2673. {
  2674. value = x;
  2675. }
  2676. else
  2677. {
  2678. // Normally NaN < anything is false, as is anything < NaN
  2679. // However, this leads to some irksome outcomes in Min and Max.
  2680. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2681. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2682. // ordering where NaN is smaller than every value, including
  2683. // negative infinity.
  2684. // Not testing for NaN therefore isn't an option, but since we
  2685. // can't find a smaller value, we can short-circuit.
  2686. if (float.IsNaN(x))
  2687. {
  2688. return x;
  2689. }
  2690. }
  2691. }
  2692. }
  2693. return value;
  2694. }
  2695. }
  2696. public static ValueTask<float> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken = default)
  2697. {
  2698. if (source == null)
  2699. throw Error.ArgumentNull(nameof(source));
  2700. if (selector == null)
  2701. throw Error.ArgumentNull(nameof(selector));
  2702. return Core(source, selector, cancellationToken);
  2703. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  2704. {
  2705. float value;
  2706. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2707. {
  2708. if (!await e.MoveNextAsync())
  2709. {
  2710. throw Error.NoElements();
  2711. }
  2712. value = selector(e.Current);
  2713. while (await e.MoveNextAsync())
  2714. {
  2715. var x = selector(e.Current);
  2716. if (x < value)
  2717. {
  2718. value = x;
  2719. }
  2720. else
  2721. {
  2722. // Normally NaN < anything is false, as is anything < NaN
  2723. // However, this leads to some irksome outcomes in Min and Max.
  2724. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2725. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2726. // ordering where NaN is smaller than every value, including
  2727. // negative infinity.
  2728. // Not testing for NaN therefore isn't an option, but since we
  2729. // can't find a smaller value, we can short-circuit.
  2730. if (float.IsNaN(x))
  2731. {
  2732. return x;
  2733. }
  2734. }
  2735. }
  2736. }
  2737. return value;
  2738. }
  2739. }
  2740. /// <summary>
  2741. /// Invokes and awaits a transform function on each element of a sequence and returns the minimum <see cref="float"/> value.
  2742. /// </summary>
  2743. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  2744. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  2745. /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
  2746. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  2747. /// <returns>A ValueTask sequence containing a single element with the value that corresponds to the minimum element in the source sequence.</returns>
  2748. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  2749. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  2750. [GenerateAsyncOverload]
  2751. private static ValueTask<float> MinAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  2752. {
  2753. if (source == null)
  2754. throw Error.ArgumentNull(nameof(source));
  2755. if (selector == null)
  2756. throw Error.ArgumentNull(nameof(selector));
  2757. return Core(source, selector, cancellationToken);
  2758. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken)
  2759. {
  2760. float value;
  2761. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2762. {
  2763. if (!await e.MoveNextAsync())
  2764. {
  2765. throw Error.NoElements();
  2766. }
  2767. value = await selector(e.Current).ConfigureAwait(false);
  2768. while (await e.MoveNextAsync())
  2769. {
  2770. var x = await selector(e.Current).ConfigureAwait(false);
  2771. if (x < value)
  2772. {
  2773. value = x;
  2774. }
  2775. else
  2776. {
  2777. // Normally NaN < anything is false, as is anything < NaN
  2778. // However, this leads to some irksome outcomes in Min and Max.
  2779. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2780. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2781. // ordering where NaN is smaller than every value, including
  2782. // negative infinity.
  2783. // Not testing for NaN therefore isn't an option, but since we
  2784. // can't find a smaller value, we can short-circuit.
  2785. if (float.IsNaN(x))
  2786. {
  2787. return x;
  2788. }
  2789. }
  2790. }
  2791. }
  2792. return value;
  2793. }
  2794. }
  2795. #if !NO_DEEP_CANCELLATION
  2796. [GenerateAsyncOverload]
  2797. private static ValueTask<float> MinAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  2798. {
  2799. if (source == null)
  2800. throw Error.ArgumentNull(nameof(source));
  2801. if (selector == null)
  2802. throw Error.ArgumentNull(nameof(selector));
  2803. return Core(source, selector, cancellationToken);
  2804. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken)
  2805. {
  2806. float value;
  2807. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2808. {
  2809. if (!await e.MoveNextAsync())
  2810. {
  2811. throw Error.NoElements();
  2812. }
  2813. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  2814. while (await e.MoveNextAsync())
  2815. {
  2816. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  2817. if (x < value)
  2818. {
  2819. value = x;
  2820. }
  2821. else
  2822. {
  2823. // Normally NaN < anything is false, as is anything < NaN
  2824. // However, this leads to some irksome outcomes in Min and Max.
  2825. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2826. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2827. // ordering where NaN is smaller than every value, including
  2828. // negative infinity.
  2829. // Not testing for NaN therefore isn't an option, but since we
  2830. // can't find a smaller value, we can short-circuit.
  2831. if (float.IsNaN(x))
  2832. {
  2833. return x;
  2834. }
  2835. }
  2836. }
  2837. }
  2838. return value;
  2839. }
  2840. }
  2841. #endif
  2842. /// <summary>
  2843. /// Returns the minimum value in an async-enumerable sequence of <see cref="Nullable{Float}" /> values.
  2844. /// </summary>
  2845. /// <param name="source">A sequence of <see cref="Nullable{Float}" /> values to determine the minimum value of.</param>
  2846. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  2847. /// <returns>A ValueTask containing a single element with the minimum value in the source sequence.</returns>
  2848. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  2849. public static ValueTask<float?> MinAsync(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken = default)
  2850. {
  2851. if (source == null)
  2852. throw Error.ArgumentNull(nameof(source));
  2853. return Core(source, cancellationToken);
  2854. static async ValueTask<float?> Core(IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  2855. {
  2856. float? value = null;
  2857. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2858. {
  2859. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2860. // so we don't have to keep testing for nullity.
  2861. do
  2862. {
  2863. if (!await e.MoveNextAsync())
  2864. {
  2865. return value;
  2866. }
  2867. value = e.Current;
  2868. }
  2869. while (!value.HasValue);
  2870. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2871. // using the lifted operation each time.
  2872. var valueVal = value.GetValueOrDefault();
  2873. while (await e.MoveNextAsync())
  2874. {
  2875. var cur = e.Current;
  2876. if (cur.HasValue)
  2877. {
  2878. var x = cur.GetValueOrDefault();
  2879. if (x < valueVal)
  2880. {
  2881. valueVal = x;
  2882. value = cur;
  2883. }
  2884. else
  2885. {
  2886. // Normally NaN < anything is false, as is anything < NaN
  2887. // However, this leads to some irksome outcomes in Min and Max.
  2888. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2889. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2890. // ordering where NaN is smaller than every value, including
  2891. // negative infinity.
  2892. // Not testing for NaN therefore isn't an option, but since we
  2893. // can't find a smaller value, we can short-circuit.
  2894. if (float.IsNaN(x))
  2895. {
  2896. return cur;
  2897. }
  2898. }
  2899. }
  2900. }
  2901. }
  2902. return value;
  2903. }
  2904. }
  2905. public static ValueTask<float?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken = default)
  2906. {
  2907. if (source == null)
  2908. throw Error.ArgumentNull(nameof(source));
  2909. if (selector == null)
  2910. throw Error.ArgumentNull(nameof(selector));
  2911. return Core(source, selector, cancellationToken);
  2912. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  2913. {
  2914. float? value = null;
  2915. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2916. {
  2917. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2918. // so we don't have to keep testing for nullity.
  2919. do
  2920. {
  2921. if (!await e.MoveNextAsync())
  2922. {
  2923. return value;
  2924. }
  2925. value = selector(e.Current);
  2926. }
  2927. while (!value.HasValue);
  2928. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2929. // using the lifted operation each time.
  2930. var valueVal = value.GetValueOrDefault();
  2931. while (await e.MoveNextAsync())
  2932. {
  2933. var cur = selector(e.Current);
  2934. if (cur.HasValue)
  2935. {
  2936. var x = cur.GetValueOrDefault();
  2937. if (x < valueVal)
  2938. {
  2939. valueVal = x;
  2940. value = cur;
  2941. }
  2942. else
  2943. {
  2944. // Normally NaN < anything is false, as is anything < NaN
  2945. // However, this leads to some irksome outcomes in Min and Max.
  2946. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  2947. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  2948. // ordering where NaN is smaller than every value, including
  2949. // negative infinity.
  2950. // Not testing for NaN therefore isn't an option, but since we
  2951. // can't find a smaller value, we can short-circuit.
  2952. if (float.IsNaN(x))
  2953. {
  2954. return cur;
  2955. }
  2956. }
  2957. }
  2958. }
  2959. }
  2960. return value;
  2961. }
  2962. }
  2963. /// <summary>
  2964. /// Invokes and awaits a transform function on each element of a sequence and returns the minimum <see cref="Nullable{Float}"/> value.
  2965. /// </summary>
  2966. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  2967. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  2968. /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
  2969. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  2970. /// <returns>A ValueTask sequence containing a single element with the value that corresponds to the minimum element in the source sequence.</returns>
  2971. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  2972. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  2973. [GenerateAsyncOverload]
  2974. private static ValueTask<float?> MinAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  2975. {
  2976. if (source == null)
  2977. throw Error.ArgumentNull(nameof(source));
  2978. if (selector == null)
  2979. throw Error.ArgumentNull(nameof(selector));
  2980. return Core(source, selector, cancellationToken);
  2981. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken)
  2982. {
  2983. float? value = null;
  2984. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  2985. {
  2986. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2987. // so we don't have to keep testing for nullity.
  2988. do
  2989. {
  2990. if (!await e.MoveNextAsync())
  2991. {
  2992. return value;
  2993. }
  2994. value = await selector(e.Current).ConfigureAwait(false);
  2995. }
  2996. while (!value.HasValue);
  2997. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2998. // using the lifted operation each time.
  2999. var valueVal = value.GetValueOrDefault();
  3000. while (await e.MoveNextAsync())
  3001. {
  3002. var cur = await selector(e.Current).ConfigureAwait(false);
  3003. if (cur.HasValue)
  3004. {
  3005. var x = cur.GetValueOrDefault();
  3006. if (x < valueVal)
  3007. {
  3008. valueVal = x;
  3009. value = cur;
  3010. }
  3011. else
  3012. {
  3013. // Normally NaN < anything is false, as is anything < NaN
  3014. // However, this leads to some irksome outcomes in Min and Max.
  3015. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3016. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3017. // ordering where NaN is smaller than every value, including
  3018. // negative infinity.
  3019. // Not testing for NaN therefore isn't an option, but since we
  3020. // can't find a smaller value, we can short-circuit.
  3021. if (float.IsNaN(x))
  3022. {
  3023. return cur;
  3024. }
  3025. }
  3026. }
  3027. }
  3028. }
  3029. return value;
  3030. }
  3031. }
  3032. #if !NO_DEEP_CANCELLATION
  3033. [GenerateAsyncOverload]
  3034. private static ValueTask<float?> MinAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  3035. {
  3036. if (source == null)
  3037. throw Error.ArgumentNull(nameof(source));
  3038. if (selector == null)
  3039. throw Error.ArgumentNull(nameof(selector));
  3040. return Core(source, selector, cancellationToken);
  3041. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken)
  3042. {
  3043. float? value = null;
  3044. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3045. {
  3046. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3047. // so we don't have to keep testing for nullity.
  3048. do
  3049. {
  3050. if (!await e.MoveNextAsync())
  3051. {
  3052. return value;
  3053. }
  3054. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  3055. }
  3056. while (!value.HasValue);
  3057. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3058. // using the lifted operation each time.
  3059. var valueVal = value.GetValueOrDefault();
  3060. while (await e.MoveNextAsync())
  3061. {
  3062. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  3063. if (cur.HasValue)
  3064. {
  3065. var x = cur.GetValueOrDefault();
  3066. if (x < valueVal)
  3067. {
  3068. valueVal = x;
  3069. value = cur;
  3070. }
  3071. else
  3072. {
  3073. // Normally NaN < anything is false, as is anything < NaN
  3074. // However, this leads to some irksome outcomes in Min and Max.
  3075. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3076. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3077. // ordering where NaN is smaller than every value, including
  3078. // negative infinity.
  3079. // Not testing for NaN therefore isn't an option, but since we
  3080. // can't find a smaller value, we can short-circuit.
  3081. if (float.IsNaN(x))
  3082. {
  3083. return cur;
  3084. }
  3085. }
  3086. }
  3087. }
  3088. }
  3089. return value;
  3090. }
  3091. }
  3092. #endif
  3093. /// <summary>
  3094. /// Returns the minimum value in an async-enumerable sequence of <see cref="double" /> values.
  3095. /// </summary>
  3096. /// <param name="source">A sequence of <see cref="double" /> values to determine the minimum value of.</param>
  3097. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  3098. /// <returns>A ValueTask containing a single element with the minimum value in the source sequence.</returns>
  3099. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  3100. public static ValueTask<double> MinAsync(this IAsyncEnumerable<double> source, CancellationToken cancellationToken = default)
  3101. {
  3102. if (source == null)
  3103. throw Error.ArgumentNull(nameof(source));
  3104. return Core(source, cancellationToken);
  3105. static async ValueTask<double> Core(IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  3106. {
  3107. double value;
  3108. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3109. {
  3110. if (!await e.MoveNextAsync())
  3111. {
  3112. throw Error.NoElements();
  3113. }
  3114. value = e.Current;
  3115. while (await e.MoveNextAsync())
  3116. {
  3117. var x = e.Current;
  3118. if (x < value)
  3119. {
  3120. value = x;
  3121. }
  3122. else
  3123. {
  3124. // Normally NaN < anything is false, as is anything < NaN
  3125. // However, this leads to some irksome outcomes in Min and Max.
  3126. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3127. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3128. // ordering where NaN is smaller than every value, including
  3129. // negative infinity.
  3130. // Not testing for NaN therefore isn't an option, but since we
  3131. // can't find a smaller value, we can short-circuit.
  3132. if (double.IsNaN(x))
  3133. {
  3134. return x;
  3135. }
  3136. }
  3137. }
  3138. }
  3139. return value;
  3140. }
  3141. }
  3142. public static ValueTask<double> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken = default)
  3143. {
  3144. if (source == null)
  3145. throw Error.ArgumentNull(nameof(source));
  3146. if (selector == null)
  3147. throw Error.ArgumentNull(nameof(selector));
  3148. return Core(source, selector, cancellationToken);
  3149. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  3150. {
  3151. double value;
  3152. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3153. {
  3154. if (!await e.MoveNextAsync())
  3155. {
  3156. throw Error.NoElements();
  3157. }
  3158. value = selector(e.Current);
  3159. while (await e.MoveNextAsync())
  3160. {
  3161. var x = selector(e.Current);
  3162. if (x < value)
  3163. {
  3164. value = x;
  3165. }
  3166. else
  3167. {
  3168. // Normally NaN < anything is false, as is anything < NaN
  3169. // However, this leads to some irksome outcomes in Min and Max.
  3170. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3171. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3172. // ordering where NaN is smaller than every value, including
  3173. // negative infinity.
  3174. // Not testing for NaN therefore isn't an option, but since we
  3175. // can't find a smaller value, we can short-circuit.
  3176. if (double.IsNaN(x))
  3177. {
  3178. return x;
  3179. }
  3180. }
  3181. }
  3182. }
  3183. return value;
  3184. }
  3185. }
  3186. /// <summary>
  3187. /// Invokes and awaits a transform function on each element of a sequence and returns the minimum <see cref="double"/> value.
  3188. /// </summary>
  3189. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  3190. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  3191. /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
  3192. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  3193. /// <returns>A ValueTask sequence containing a single element with the value that corresponds to the minimum element in the source sequence.</returns>
  3194. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  3195. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  3196. [GenerateAsyncOverload]
  3197. private static ValueTask<double> MinAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  3198. {
  3199. if (source == null)
  3200. throw Error.ArgumentNull(nameof(source));
  3201. if (selector == null)
  3202. throw Error.ArgumentNull(nameof(selector));
  3203. return Core(source, selector, cancellationToken);
  3204. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken)
  3205. {
  3206. double value;
  3207. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3208. {
  3209. if (!await e.MoveNextAsync())
  3210. {
  3211. throw Error.NoElements();
  3212. }
  3213. value = await selector(e.Current).ConfigureAwait(false);
  3214. while (await e.MoveNextAsync())
  3215. {
  3216. var x = await selector(e.Current).ConfigureAwait(false);
  3217. if (x < value)
  3218. {
  3219. value = x;
  3220. }
  3221. else
  3222. {
  3223. // Normally NaN < anything is false, as is anything < NaN
  3224. // However, this leads to some irksome outcomes in Min and Max.
  3225. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3226. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3227. // ordering where NaN is smaller than every value, including
  3228. // negative infinity.
  3229. // Not testing for NaN therefore isn't an option, but since we
  3230. // can't find a smaller value, we can short-circuit.
  3231. if (double.IsNaN(x))
  3232. {
  3233. return x;
  3234. }
  3235. }
  3236. }
  3237. }
  3238. return value;
  3239. }
  3240. }
  3241. #if !NO_DEEP_CANCELLATION
  3242. [GenerateAsyncOverload]
  3243. private static ValueTask<double> MinAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  3244. {
  3245. if (source == null)
  3246. throw Error.ArgumentNull(nameof(source));
  3247. if (selector == null)
  3248. throw Error.ArgumentNull(nameof(selector));
  3249. return Core(source, selector, cancellationToken);
  3250. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken)
  3251. {
  3252. double value;
  3253. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3254. {
  3255. if (!await e.MoveNextAsync())
  3256. {
  3257. throw Error.NoElements();
  3258. }
  3259. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  3260. while (await e.MoveNextAsync())
  3261. {
  3262. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  3263. if (x < value)
  3264. {
  3265. value = x;
  3266. }
  3267. else
  3268. {
  3269. // Normally NaN < anything is false, as is anything < NaN
  3270. // However, this leads to some irksome outcomes in Min and Max.
  3271. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3272. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3273. // ordering where NaN is smaller than every value, including
  3274. // negative infinity.
  3275. // Not testing for NaN therefore isn't an option, but since we
  3276. // can't find a smaller value, we can short-circuit.
  3277. if (double.IsNaN(x))
  3278. {
  3279. return x;
  3280. }
  3281. }
  3282. }
  3283. }
  3284. return value;
  3285. }
  3286. }
  3287. #endif
  3288. /// <summary>
  3289. /// Returns the minimum value in an async-enumerable sequence of <see cref="Nullable{Double}" /> values.
  3290. /// </summary>
  3291. /// <param name="source">A sequence of <see cref="Nullable{Double}" /> values to determine the minimum value of.</param>
  3292. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  3293. /// <returns>A ValueTask containing a single element with the minimum value in the source sequence.</returns>
  3294. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  3295. public static ValueTask<double?> MinAsync(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken = default)
  3296. {
  3297. if (source == null)
  3298. throw Error.ArgumentNull(nameof(source));
  3299. return Core(source, cancellationToken);
  3300. static async ValueTask<double?> Core(IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  3301. {
  3302. double? value = null;
  3303. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3304. {
  3305. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3306. // so we don't have to keep testing for nullity.
  3307. do
  3308. {
  3309. if (!await e.MoveNextAsync())
  3310. {
  3311. return value;
  3312. }
  3313. value = e.Current;
  3314. }
  3315. while (!value.HasValue);
  3316. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3317. // using the lifted operation each time.
  3318. var valueVal = value.GetValueOrDefault();
  3319. while (await e.MoveNextAsync())
  3320. {
  3321. var cur = e.Current;
  3322. if (cur.HasValue)
  3323. {
  3324. var x = cur.GetValueOrDefault();
  3325. if (x < valueVal)
  3326. {
  3327. valueVal = x;
  3328. value = cur;
  3329. }
  3330. else
  3331. {
  3332. // Normally NaN < anything is false, as is anything < NaN
  3333. // However, this leads to some irksome outcomes in Min and Max.
  3334. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3335. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3336. // ordering where NaN is smaller than every value, including
  3337. // negative infinity.
  3338. // Not testing for NaN therefore isn't an option, but since we
  3339. // can't find a smaller value, we can short-circuit.
  3340. if (double.IsNaN(x))
  3341. {
  3342. return cur;
  3343. }
  3344. }
  3345. }
  3346. }
  3347. }
  3348. return value;
  3349. }
  3350. }
  3351. public static ValueTask<double?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
  3352. {
  3353. if (source == null)
  3354. throw Error.ArgumentNull(nameof(source));
  3355. if (selector == null)
  3356. throw Error.ArgumentNull(nameof(selector));
  3357. return Core(source, selector, cancellationToken);
  3358. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  3359. {
  3360. double? value = null;
  3361. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3362. {
  3363. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3364. // so we don't have to keep testing for nullity.
  3365. do
  3366. {
  3367. if (!await e.MoveNextAsync())
  3368. {
  3369. return value;
  3370. }
  3371. value = selector(e.Current);
  3372. }
  3373. while (!value.HasValue);
  3374. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3375. // using the lifted operation each time.
  3376. var valueVal = value.GetValueOrDefault();
  3377. while (await e.MoveNextAsync())
  3378. {
  3379. var cur = selector(e.Current);
  3380. if (cur.HasValue)
  3381. {
  3382. var x = cur.GetValueOrDefault();
  3383. if (x < valueVal)
  3384. {
  3385. valueVal = x;
  3386. value = cur;
  3387. }
  3388. else
  3389. {
  3390. // Normally NaN < anything is false, as is anything < NaN
  3391. // However, this leads to some irksome outcomes in Min and Max.
  3392. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3393. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3394. // ordering where NaN is smaller than every value, including
  3395. // negative infinity.
  3396. // Not testing for NaN therefore isn't an option, but since we
  3397. // can't find a smaller value, we can short-circuit.
  3398. if (double.IsNaN(x))
  3399. {
  3400. return cur;
  3401. }
  3402. }
  3403. }
  3404. }
  3405. }
  3406. return value;
  3407. }
  3408. }
  3409. /// <summary>
  3410. /// Invokes and awaits a transform function on each element of a sequence and returns the minimum <see cref="Nullable{Double}"/> value.
  3411. /// </summary>
  3412. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  3413. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  3414. /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
  3415. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  3416. /// <returns>A ValueTask sequence containing a single element with the value that corresponds to the minimum element in the source sequence.</returns>
  3417. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  3418. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  3419. [GenerateAsyncOverload]
  3420. private static ValueTask<double?> MinAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  3421. {
  3422. if (source == null)
  3423. throw Error.ArgumentNull(nameof(source));
  3424. if (selector == null)
  3425. throw Error.ArgumentNull(nameof(selector));
  3426. return Core(source, selector, cancellationToken);
  3427. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken)
  3428. {
  3429. double? value = null;
  3430. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3431. {
  3432. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3433. // so we don't have to keep testing for nullity.
  3434. do
  3435. {
  3436. if (!await e.MoveNextAsync())
  3437. {
  3438. return value;
  3439. }
  3440. value = await selector(e.Current).ConfigureAwait(false);
  3441. }
  3442. while (!value.HasValue);
  3443. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3444. // using the lifted operation each time.
  3445. var valueVal = value.GetValueOrDefault();
  3446. while (await e.MoveNextAsync())
  3447. {
  3448. var cur = await selector(e.Current).ConfigureAwait(false);
  3449. if (cur.HasValue)
  3450. {
  3451. var x = cur.GetValueOrDefault();
  3452. if (x < valueVal)
  3453. {
  3454. valueVal = x;
  3455. value = cur;
  3456. }
  3457. else
  3458. {
  3459. // Normally NaN < anything is false, as is anything < NaN
  3460. // However, this leads to some irksome outcomes in Min and Max.
  3461. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3462. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3463. // ordering where NaN is smaller than every value, including
  3464. // negative infinity.
  3465. // Not testing for NaN therefore isn't an option, but since we
  3466. // can't find a smaller value, we can short-circuit.
  3467. if (double.IsNaN(x))
  3468. {
  3469. return cur;
  3470. }
  3471. }
  3472. }
  3473. }
  3474. }
  3475. return value;
  3476. }
  3477. }
  3478. #if !NO_DEEP_CANCELLATION
  3479. [GenerateAsyncOverload]
  3480. private static ValueTask<double?> MinAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  3481. {
  3482. if (source == null)
  3483. throw Error.ArgumentNull(nameof(source));
  3484. if (selector == null)
  3485. throw Error.ArgumentNull(nameof(selector));
  3486. return Core(source, selector, cancellationToken);
  3487. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken)
  3488. {
  3489. double? value = null;
  3490. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3491. {
  3492. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3493. // so we don't have to keep testing for nullity.
  3494. do
  3495. {
  3496. if (!await e.MoveNextAsync())
  3497. {
  3498. return value;
  3499. }
  3500. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  3501. }
  3502. while (!value.HasValue);
  3503. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3504. // using the lifted operation each time.
  3505. var valueVal = value.GetValueOrDefault();
  3506. while (await e.MoveNextAsync())
  3507. {
  3508. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  3509. if (cur.HasValue)
  3510. {
  3511. var x = cur.GetValueOrDefault();
  3512. if (x < valueVal)
  3513. {
  3514. valueVal = x;
  3515. value = cur;
  3516. }
  3517. else
  3518. {
  3519. // Normally NaN < anything is false, as is anything < NaN
  3520. // However, this leads to some irksome outcomes in Min and Max.
  3521. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  3522. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  3523. // ordering where NaN is smaller than every value, including
  3524. // negative infinity.
  3525. // Not testing for NaN therefore isn't an option, but since we
  3526. // can't find a smaller value, we can short-circuit.
  3527. if (double.IsNaN(x))
  3528. {
  3529. return cur;
  3530. }
  3531. }
  3532. }
  3533. }
  3534. }
  3535. return value;
  3536. }
  3537. }
  3538. #endif
  3539. /// <summary>
  3540. /// Returns the minimum value in an async-enumerable sequence of <see cref="decimal" /> values.
  3541. /// </summary>
  3542. /// <param name="source">A sequence of <see cref="decimal" /> values to determine the minimum value of.</param>
  3543. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  3544. /// <returns>A ValueTask containing a single element with the minimum value in the source sequence.</returns>
  3545. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  3546. public static ValueTask<decimal> MinAsync(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken = default)
  3547. {
  3548. if (source == null)
  3549. throw Error.ArgumentNull(nameof(source));
  3550. return Core(source, cancellationToken);
  3551. static async ValueTask<decimal> Core(IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  3552. {
  3553. decimal value;
  3554. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3555. {
  3556. if (!await e.MoveNextAsync())
  3557. {
  3558. throw Error.NoElements();
  3559. }
  3560. value = e.Current;
  3561. while (await e.MoveNextAsync())
  3562. {
  3563. var x = e.Current;
  3564. if (x < value)
  3565. {
  3566. value = x;
  3567. }
  3568. }
  3569. }
  3570. return value;
  3571. }
  3572. }
  3573. public static ValueTask<decimal> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken = default)
  3574. {
  3575. if (source == null)
  3576. throw Error.ArgumentNull(nameof(source));
  3577. if (selector == null)
  3578. throw Error.ArgumentNull(nameof(selector));
  3579. return Core(source, selector, cancellationToken);
  3580. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  3581. {
  3582. decimal value;
  3583. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3584. {
  3585. if (!await e.MoveNextAsync())
  3586. {
  3587. throw Error.NoElements();
  3588. }
  3589. value = selector(e.Current);
  3590. while (await e.MoveNextAsync())
  3591. {
  3592. var x = selector(e.Current);
  3593. if (x < value)
  3594. {
  3595. value = x;
  3596. }
  3597. }
  3598. }
  3599. return value;
  3600. }
  3601. }
  3602. /// <summary>
  3603. /// Invokes and awaits a transform function on each element of a sequence and returns the minimum <see cref="decimal"/> value.
  3604. /// </summary>
  3605. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  3606. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  3607. /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
  3608. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  3609. /// <returns>A ValueTask sequence containing a single element with the value that corresponds to the minimum element in the source sequence.</returns>
  3610. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  3611. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  3612. [GenerateAsyncOverload]
  3613. private static ValueTask<decimal> MinAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  3614. {
  3615. if (source == null)
  3616. throw Error.ArgumentNull(nameof(source));
  3617. if (selector == null)
  3618. throw Error.ArgumentNull(nameof(selector));
  3619. return Core(source, selector, cancellationToken);
  3620. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  3621. {
  3622. decimal value;
  3623. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3624. {
  3625. if (!await e.MoveNextAsync())
  3626. {
  3627. throw Error.NoElements();
  3628. }
  3629. value = await selector(e.Current).ConfigureAwait(false);
  3630. while (await e.MoveNextAsync())
  3631. {
  3632. var x = await selector(e.Current).ConfigureAwait(false);
  3633. if (x < value)
  3634. {
  3635. value = x;
  3636. }
  3637. }
  3638. }
  3639. return value;
  3640. }
  3641. }
  3642. #if !NO_DEEP_CANCELLATION
  3643. [GenerateAsyncOverload]
  3644. private static ValueTask<decimal> MinAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  3645. {
  3646. if (source == null)
  3647. throw Error.ArgumentNull(nameof(source));
  3648. if (selector == null)
  3649. throw Error.ArgumentNull(nameof(selector));
  3650. return Core(source, selector, cancellationToken);
  3651. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  3652. {
  3653. decimal value;
  3654. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3655. {
  3656. if (!await e.MoveNextAsync())
  3657. {
  3658. throw Error.NoElements();
  3659. }
  3660. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  3661. while (await e.MoveNextAsync())
  3662. {
  3663. var x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  3664. if (x < value)
  3665. {
  3666. value = x;
  3667. }
  3668. }
  3669. }
  3670. return value;
  3671. }
  3672. }
  3673. #endif
  3674. /// <summary>
  3675. /// Returns the minimum value in an async-enumerable sequence of <see cref="Nullable{Decimal}" /> values.
  3676. /// </summary>
  3677. /// <param name="source">A sequence of <see cref="Nullable{Decimal}" /> values to determine the minimum value of.</param>
  3678. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  3679. /// <returns>A ValueTask containing a single element with the minimum value in the source sequence.</returns>
  3680. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  3681. public static ValueTask<decimal?> MinAsync(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken = default)
  3682. {
  3683. if (source == null)
  3684. throw Error.ArgumentNull(nameof(source));
  3685. return Core(source, cancellationToken);
  3686. static async ValueTask<decimal?> Core(IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  3687. {
  3688. decimal? value = null;
  3689. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3690. {
  3691. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3692. // so we don't have to keep testing for nullity.
  3693. do
  3694. {
  3695. if (!await e.MoveNextAsync())
  3696. {
  3697. return value;
  3698. }
  3699. value = e.Current;
  3700. }
  3701. while (!value.HasValue);
  3702. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3703. // using the lifted operation each time.
  3704. var valueVal = value.GetValueOrDefault();
  3705. while (await e.MoveNextAsync())
  3706. {
  3707. var cur = e.Current;
  3708. var x = cur.GetValueOrDefault();
  3709. if (cur.HasValue && x < valueVal)
  3710. {
  3711. valueVal = x;
  3712. value = cur;
  3713. }
  3714. }
  3715. }
  3716. return value;
  3717. }
  3718. }
  3719. public static ValueTask<decimal?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken = default)
  3720. {
  3721. if (source == null)
  3722. throw Error.ArgumentNull(nameof(source));
  3723. if (selector == null)
  3724. throw Error.ArgumentNull(nameof(selector));
  3725. return Core(source, selector, cancellationToken);
  3726. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  3727. {
  3728. decimal? value = null;
  3729. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3730. {
  3731. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3732. // so we don't have to keep testing for nullity.
  3733. do
  3734. {
  3735. if (!await e.MoveNextAsync())
  3736. {
  3737. return value;
  3738. }
  3739. value = selector(e.Current);
  3740. }
  3741. while (!value.HasValue);
  3742. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3743. // using the lifted operation each time.
  3744. var valueVal = value.GetValueOrDefault();
  3745. while (await e.MoveNextAsync())
  3746. {
  3747. var cur = selector(e.Current);
  3748. var x = cur.GetValueOrDefault();
  3749. if (cur.HasValue && x < valueVal)
  3750. {
  3751. valueVal = x;
  3752. value = cur;
  3753. }
  3754. }
  3755. }
  3756. return value;
  3757. }
  3758. }
  3759. /// <summary>
  3760. /// Invokes and awaits a transform function on each element of a sequence and returns the minimum <see cref="Nullable{Decimal}"/> value.
  3761. /// </summary>
  3762. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  3763. /// <param name="source">An async-enumerable sequence to determine the minimum element of.</param>
  3764. /// <param name="selector">An asynchronous transform function to invoke and await on each element.</param>
  3765. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  3766. /// <returns>A ValueTask sequence containing a single element with the value that corresponds to the minimum element in the source sequence.</returns>
  3767. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  3768. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  3769. [GenerateAsyncOverload]
  3770. private static ValueTask<decimal?> MinAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  3771. {
  3772. if (source == null)
  3773. throw Error.ArgumentNull(nameof(source));
  3774. if (selector == null)
  3775. throw Error.ArgumentNull(nameof(selector));
  3776. return Core(source, selector, cancellationToken);
  3777. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  3778. {
  3779. decimal? value = null;
  3780. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3781. {
  3782. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3783. // so we don't have to keep testing for nullity.
  3784. do
  3785. {
  3786. if (!await e.MoveNextAsync())
  3787. {
  3788. return value;
  3789. }
  3790. value = await selector(e.Current).ConfigureAwait(false);
  3791. }
  3792. while (!value.HasValue);
  3793. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3794. // using the lifted operation each time.
  3795. var valueVal = value.GetValueOrDefault();
  3796. while (await e.MoveNextAsync())
  3797. {
  3798. var cur = await selector(e.Current).ConfigureAwait(false);
  3799. var x = cur.GetValueOrDefault();
  3800. if (cur.HasValue && x < valueVal)
  3801. {
  3802. valueVal = x;
  3803. value = cur;
  3804. }
  3805. }
  3806. }
  3807. return value;
  3808. }
  3809. }
  3810. #if !NO_DEEP_CANCELLATION
  3811. [GenerateAsyncOverload]
  3812. private static ValueTask<decimal?> MinAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  3813. {
  3814. if (source == null)
  3815. throw Error.ArgumentNull(nameof(source));
  3816. if (selector == null)
  3817. throw Error.ArgumentNull(nameof(selector));
  3818. return Core(source, selector, cancellationToken);
  3819. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  3820. {
  3821. decimal? value = null;
  3822. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  3823. {
  3824. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3825. // so we don't have to keep testing for nullity.
  3826. do
  3827. {
  3828. if (!await e.MoveNextAsync())
  3829. {
  3830. return value;
  3831. }
  3832. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  3833. }
  3834. while (!value.HasValue);
  3835. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3836. // using the lifted operation each time.
  3837. var valueVal = value.GetValueOrDefault();
  3838. while (await e.MoveNextAsync())
  3839. {
  3840. var cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  3841. var x = cur.GetValueOrDefault();
  3842. if (cur.HasValue && x < valueVal)
  3843. {
  3844. valueVal = x;
  3845. value = cur;
  3846. }
  3847. }
  3848. }
  3849. return value;
  3850. }
  3851. }
  3852. #endif
  3853. }
  3854. }