MinMax.Generated.cs 209 KB

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