MinMax.Generated.cs 298 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678
  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. public static Task<int> MaxAsync(this IAsyncEnumerable<int> source, CancellationToken cancellationToken = default)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. return Core(source, cancellationToken);
  16. static async Task<int> Core(IAsyncEnumerable<int> _source, CancellationToken _cancellationToken)
  17. {
  18. int value;
  19. #if CSHARP8
  20. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  21. {
  22. if (!await e.MoveNextAsync())
  23. {
  24. throw Error.NoElements();
  25. }
  26. value = e.Current;
  27. while (await e.MoveNextAsync())
  28. {
  29. var x = e.Current;
  30. if (x > value)
  31. {
  32. value = x;
  33. }
  34. }
  35. }
  36. #else
  37. var e = _source.GetAsyncEnumerator(_cancellationToken);
  38. try
  39. {
  40. if (!await e.MoveNextAsync().ConfigureAwait(false))
  41. {
  42. throw Error.NoElements();
  43. }
  44. value = e.Current;
  45. while (await e.MoveNextAsync().ConfigureAwait(false))
  46. {
  47. var x = e.Current;
  48. if (x > value)
  49. {
  50. value = x;
  51. }
  52. }
  53. }
  54. finally
  55. {
  56. await e.DisposeAsync().ConfigureAwait(false);
  57. }
  58. #endif
  59. return value;
  60. }
  61. }
  62. public static Task<int> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken = default)
  63. {
  64. if (source == null)
  65. throw Error.ArgumentNull(nameof(source));
  66. if (selector == null)
  67. throw Error.ArgumentNull(nameof(selector));
  68. return Core(source, selector, cancellationToken);
  69. static async Task<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, int> _selector, CancellationToken _cancellationToken)
  70. {
  71. int value;
  72. #if CSHARP8
  73. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  74. {
  75. if (!await e.MoveNextAsync())
  76. {
  77. throw Error.NoElements();
  78. }
  79. value = _selector(e.Current);
  80. while (await e.MoveNextAsync())
  81. {
  82. var x = _selector(e.Current);
  83. if (x > value)
  84. {
  85. value = x;
  86. }
  87. }
  88. }
  89. #else
  90. var e = _source.GetAsyncEnumerator(_cancellationToken);
  91. try
  92. {
  93. if (!await e.MoveNextAsync().ConfigureAwait(false))
  94. {
  95. throw Error.NoElements();
  96. }
  97. value = _selector(e.Current);
  98. while (await e.MoveNextAsync().ConfigureAwait(false))
  99. {
  100. var x = _selector(e.Current);
  101. if (x > value)
  102. {
  103. value = x;
  104. }
  105. }
  106. }
  107. finally
  108. {
  109. await e.DisposeAsync().ConfigureAwait(false);
  110. }
  111. #endif
  112. return value;
  113. }
  114. }
  115. public static Task<int> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  116. {
  117. if (source == null)
  118. throw Error.ArgumentNull(nameof(source));
  119. if (selector == null)
  120. throw Error.ArgumentNull(nameof(selector));
  121. return Core(source, selector, cancellationToken);
  122. static async Task<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<int>> _selector, CancellationToken _cancellationToken)
  123. {
  124. int value;
  125. #if CSHARP8
  126. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  127. {
  128. if (!await e.MoveNextAsync())
  129. {
  130. throw Error.NoElements();
  131. }
  132. value = await _selector(e.Current).ConfigureAwait(false);
  133. while (await e.MoveNextAsync())
  134. {
  135. var x = await _selector(e.Current).ConfigureAwait(false);
  136. if (x > value)
  137. {
  138. value = x;
  139. }
  140. }
  141. }
  142. #else
  143. var e = _source.GetAsyncEnumerator(_cancellationToken);
  144. try
  145. {
  146. if (!await e.MoveNextAsync().ConfigureAwait(false))
  147. {
  148. throw Error.NoElements();
  149. }
  150. value = await _selector(e.Current).ConfigureAwait(false);
  151. while (await e.MoveNextAsync().ConfigureAwait(false))
  152. {
  153. var x = await _selector(e.Current).ConfigureAwait(false);
  154. if (x > value)
  155. {
  156. value = x;
  157. }
  158. }
  159. }
  160. finally
  161. {
  162. await e.DisposeAsync().ConfigureAwait(false);
  163. }
  164. #endif
  165. return value;
  166. }
  167. }
  168. #if !NO_DEEP_CANCELLATION
  169. public static Task<int> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  170. {
  171. if (source == null)
  172. throw Error.ArgumentNull(nameof(source));
  173. if (selector == null)
  174. throw Error.ArgumentNull(nameof(selector));
  175. return Core(source, selector, cancellationToken);
  176. static async Task<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<int>> _selector, CancellationToken _cancellationToken)
  177. {
  178. int value;
  179. #if CSHARP8
  180. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  181. {
  182. if (!await e.MoveNextAsync())
  183. {
  184. throw Error.NoElements();
  185. }
  186. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  187. while (await e.MoveNextAsync())
  188. {
  189. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  190. if (x > value)
  191. {
  192. value = x;
  193. }
  194. }
  195. }
  196. #else
  197. var e = _source.GetAsyncEnumerator(_cancellationToken);
  198. try
  199. {
  200. if (!await e.MoveNextAsync().ConfigureAwait(false))
  201. {
  202. throw Error.NoElements();
  203. }
  204. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  205. while (await e.MoveNextAsync().ConfigureAwait(false))
  206. {
  207. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  208. if (x > value)
  209. {
  210. value = x;
  211. }
  212. }
  213. }
  214. finally
  215. {
  216. await e.DisposeAsync().ConfigureAwait(false);
  217. }
  218. #endif
  219. return value;
  220. }
  221. }
  222. #endif
  223. public static Task<int?> MaxAsync(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken = default)
  224. {
  225. if (source == null)
  226. throw Error.ArgumentNull(nameof(source));
  227. return Core(source, cancellationToken);
  228. static async Task<int?> Core(IAsyncEnumerable<int?> _source, CancellationToken _cancellationToken)
  229. {
  230. int? value = null;
  231. #if CSHARP8
  232. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  233. {
  234. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  235. // so we don't have to keep testing for nullity.
  236. do
  237. {
  238. if (!await e.MoveNextAsync())
  239. {
  240. return value;
  241. }
  242. value = e.Current;
  243. }
  244. while (!value.HasValue);
  245. // Keep hold of the wrapped value, and do comparisons on that, rather than
  246. // using the lifted operation each time.
  247. var valueVal = value.GetValueOrDefault();
  248. if (valueVal >= 0)
  249. {
  250. // We can fast-path this case where we know HasValue will
  251. // never affect the outcome, without constantly checking
  252. // if we're in such a state. Similar fast-paths could
  253. // be done for other cases, but as all-positive or mostly-
  254. // positive integer values are quite common in real-world
  255. // uses, it's only been done for int? and long?.
  256. while (await e.MoveNextAsync())
  257. {
  258. var cur = e.Current;
  259. var x = cur.GetValueOrDefault();
  260. if (x > valueVal)
  261. {
  262. valueVal = x;
  263. value = cur;
  264. }
  265. }
  266. }
  267. else
  268. {
  269. while (await e.MoveNextAsync())
  270. {
  271. var cur = e.Current;
  272. var x = cur.GetValueOrDefault();
  273. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  274. // unless nulls either never happen or always happen.
  275. if (cur.HasValue & x > valueVal)
  276. {
  277. valueVal = x;
  278. value = cur;
  279. }
  280. }
  281. }
  282. }
  283. #else
  284. var e = _source.GetAsyncEnumerator(_cancellationToken);
  285. try
  286. {
  287. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  288. // so we don't have to keep testing for nullity.
  289. do
  290. {
  291. if (!await e.MoveNextAsync().ConfigureAwait(false))
  292. {
  293. return value;
  294. }
  295. value = e.Current;
  296. }
  297. while (!value.HasValue);
  298. // Keep hold of the wrapped value, and do comparisons on that, rather than
  299. // using the lifted operation each time.
  300. var valueVal = value.GetValueOrDefault();
  301. if (valueVal >= 0)
  302. {
  303. // We can fast-path this case where we know HasValue will
  304. // never affect the outcome, without constantly checking
  305. // if we're in such a state. Similar fast-paths could
  306. // be done for other cases, but as all-positive or mostly-
  307. // positive integer values are quite common in real-world
  308. // uses, it's only been done for int? and long?.
  309. while (await e.MoveNextAsync().ConfigureAwait(false))
  310. {
  311. var cur = e.Current;
  312. var x = cur.GetValueOrDefault();
  313. if (x > valueVal)
  314. {
  315. valueVal = x;
  316. value = cur;
  317. }
  318. }
  319. }
  320. else
  321. {
  322. while (await e.MoveNextAsync().ConfigureAwait(false))
  323. {
  324. var cur = e.Current;
  325. var x = cur.GetValueOrDefault();
  326. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  327. // unless nulls either never happen or always happen.
  328. if (cur.HasValue & x > valueVal)
  329. {
  330. valueVal = x;
  331. value = cur;
  332. }
  333. }
  334. }
  335. }
  336. finally
  337. {
  338. await e.DisposeAsync().ConfigureAwait(false);
  339. }
  340. #endif
  341. return value;
  342. }
  343. }
  344. public static Task<int?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken = default)
  345. {
  346. if (source == null)
  347. throw Error.ArgumentNull(nameof(source));
  348. if (selector == null)
  349. throw Error.ArgumentNull(nameof(selector));
  350. return Core(source, selector, cancellationToken);
  351. static async Task<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, int?> _selector, CancellationToken _cancellationToken)
  352. {
  353. int? value = null;
  354. #if CSHARP8
  355. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  356. {
  357. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  358. // so we don't have to keep testing for nullity.
  359. do
  360. {
  361. if (!await e.MoveNextAsync())
  362. {
  363. return value;
  364. }
  365. value = _selector(e.Current);
  366. }
  367. while (!value.HasValue);
  368. // Keep hold of the wrapped value, and do comparisons on that, rather than
  369. // using the lifted operation each time.
  370. var valueVal = value.GetValueOrDefault();
  371. if (valueVal >= 0)
  372. {
  373. // We can fast-path this case where we know HasValue will
  374. // never affect the outcome, without constantly checking
  375. // if we're in such a state. Similar fast-paths could
  376. // be done for other cases, but as all-positive or mostly-
  377. // positive integer values are quite common in real-world
  378. // uses, it's only been done for int? and long?.
  379. while (await e.MoveNextAsync())
  380. {
  381. var cur = _selector(e.Current);
  382. var x = cur.GetValueOrDefault();
  383. if (x > valueVal)
  384. {
  385. valueVal = x;
  386. value = cur;
  387. }
  388. }
  389. }
  390. else
  391. {
  392. while (await e.MoveNextAsync())
  393. {
  394. var cur = _selector(e.Current);
  395. var x = cur.GetValueOrDefault();
  396. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  397. // unless nulls either never happen or always happen.
  398. if (cur.HasValue & x > valueVal)
  399. {
  400. valueVal = x;
  401. value = cur;
  402. }
  403. }
  404. }
  405. }
  406. #else
  407. var e = _source.GetAsyncEnumerator(_cancellationToken);
  408. try
  409. {
  410. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  411. // so we don't have to keep testing for nullity.
  412. do
  413. {
  414. if (!await e.MoveNextAsync().ConfigureAwait(false))
  415. {
  416. return value;
  417. }
  418. value = _selector(e.Current);
  419. }
  420. while (!value.HasValue);
  421. // Keep hold of the wrapped value, and do comparisons on that, rather than
  422. // using the lifted operation each time.
  423. var valueVal = value.GetValueOrDefault();
  424. if (valueVal >= 0)
  425. {
  426. // We can fast-path this case where we know HasValue will
  427. // never affect the outcome, without constantly checking
  428. // if we're in such a state. Similar fast-paths could
  429. // be done for other cases, but as all-positive or mostly-
  430. // positive integer values are quite common in real-world
  431. // uses, it's only been done for int? and long?.
  432. while (await e.MoveNextAsync().ConfigureAwait(false))
  433. {
  434. var cur = _selector(e.Current);
  435. var x = cur.GetValueOrDefault();
  436. if (x > valueVal)
  437. {
  438. valueVal = x;
  439. value = cur;
  440. }
  441. }
  442. }
  443. else
  444. {
  445. while (await e.MoveNextAsync().ConfigureAwait(false))
  446. {
  447. var cur = _selector(e.Current);
  448. var x = cur.GetValueOrDefault();
  449. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  450. // unless nulls either never happen or always happen.
  451. if (cur.HasValue & x > valueVal)
  452. {
  453. valueVal = x;
  454. value = cur;
  455. }
  456. }
  457. }
  458. }
  459. finally
  460. {
  461. await e.DisposeAsync().ConfigureAwait(false);
  462. }
  463. #endif
  464. return value;
  465. }
  466. }
  467. public static Task<int?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  468. {
  469. if (source == null)
  470. throw Error.ArgumentNull(nameof(source));
  471. if (selector == null)
  472. throw Error.ArgumentNull(nameof(selector));
  473. return Core(source, selector, cancellationToken);
  474. static async Task<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<int?>> _selector, CancellationToken _cancellationToken)
  475. {
  476. int? value = null;
  477. #if CSHARP8
  478. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  479. {
  480. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  481. // so we don't have to keep testing for nullity.
  482. do
  483. {
  484. if (!await e.MoveNextAsync())
  485. {
  486. return value;
  487. }
  488. value = await _selector(e.Current).ConfigureAwait(false);
  489. }
  490. while (!value.HasValue);
  491. // Keep hold of the wrapped value, and do comparisons on that, rather than
  492. // using the lifted operation each time.
  493. var valueVal = value.GetValueOrDefault();
  494. if (valueVal >= 0)
  495. {
  496. // We can fast-path this case where we know HasValue will
  497. // never affect the outcome, without constantly checking
  498. // if we're in such a state. Similar fast-paths could
  499. // be done for other cases, but as all-positive or mostly-
  500. // positive integer values are quite common in real-world
  501. // uses, it's only been done for int? and long?.
  502. while (await e.MoveNextAsync())
  503. {
  504. var cur = await _selector(e.Current).ConfigureAwait(false);
  505. var x = cur.GetValueOrDefault();
  506. if (x > valueVal)
  507. {
  508. valueVal = x;
  509. value = cur;
  510. }
  511. }
  512. }
  513. else
  514. {
  515. while (await e.MoveNextAsync())
  516. {
  517. var cur = await _selector(e.Current).ConfigureAwait(false);
  518. var x = cur.GetValueOrDefault();
  519. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  520. // unless nulls either never happen or always happen.
  521. if (cur.HasValue & x > valueVal)
  522. {
  523. valueVal = x;
  524. value = cur;
  525. }
  526. }
  527. }
  528. }
  529. #else
  530. var e = _source.GetAsyncEnumerator(_cancellationToken);
  531. try
  532. {
  533. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  534. // so we don't have to keep testing for nullity.
  535. do
  536. {
  537. if (!await e.MoveNextAsync().ConfigureAwait(false))
  538. {
  539. return value;
  540. }
  541. value = await _selector(e.Current).ConfigureAwait(false);
  542. }
  543. while (!value.HasValue);
  544. // Keep hold of the wrapped value, and do comparisons on that, rather than
  545. // using the lifted operation each time.
  546. var valueVal = value.GetValueOrDefault();
  547. if (valueVal >= 0)
  548. {
  549. // We can fast-path this case where we know HasValue will
  550. // never affect the outcome, without constantly checking
  551. // if we're in such a state. Similar fast-paths could
  552. // be done for other cases, but as all-positive or mostly-
  553. // positive integer values are quite common in real-world
  554. // uses, it's only been done for int? and long?.
  555. while (await e.MoveNextAsync().ConfigureAwait(false))
  556. {
  557. var cur = await _selector(e.Current).ConfigureAwait(false);
  558. var x = cur.GetValueOrDefault();
  559. if (x > valueVal)
  560. {
  561. valueVal = x;
  562. value = cur;
  563. }
  564. }
  565. }
  566. else
  567. {
  568. while (await e.MoveNextAsync().ConfigureAwait(false))
  569. {
  570. var cur = await _selector(e.Current).ConfigureAwait(false);
  571. var x = cur.GetValueOrDefault();
  572. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  573. // unless nulls either never happen or always happen.
  574. if (cur.HasValue & x > valueVal)
  575. {
  576. valueVal = x;
  577. value = cur;
  578. }
  579. }
  580. }
  581. }
  582. finally
  583. {
  584. await e.DisposeAsync().ConfigureAwait(false);
  585. }
  586. #endif
  587. return value;
  588. }
  589. }
  590. #if !NO_DEEP_CANCELLATION
  591. public static Task<int?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  592. {
  593. if (source == null)
  594. throw Error.ArgumentNull(nameof(source));
  595. if (selector == null)
  596. throw Error.ArgumentNull(nameof(selector));
  597. return Core(source, selector, cancellationToken);
  598. static async Task<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<int?>> _selector, CancellationToken _cancellationToken)
  599. {
  600. int? value = null;
  601. #if CSHARP8
  602. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  603. {
  604. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  605. // so we don't have to keep testing for nullity.
  606. do
  607. {
  608. if (!await e.MoveNextAsync())
  609. {
  610. return value;
  611. }
  612. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  613. }
  614. while (!value.HasValue);
  615. // Keep hold of the wrapped value, and do comparisons on that, rather than
  616. // using the lifted operation each time.
  617. var valueVal = value.GetValueOrDefault();
  618. if (valueVal >= 0)
  619. {
  620. // We can fast-path this case where we know HasValue will
  621. // never affect the outcome, without constantly checking
  622. // if we're in such a state. Similar fast-paths could
  623. // be done for other cases, but as all-positive or mostly-
  624. // positive integer values are quite common in real-world
  625. // uses, it's only been done for int? and long?.
  626. while (await e.MoveNextAsync())
  627. {
  628. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  629. var x = cur.GetValueOrDefault();
  630. if (x > valueVal)
  631. {
  632. valueVal = x;
  633. value = cur;
  634. }
  635. }
  636. }
  637. else
  638. {
  639. while (await e.MoveNextAsync())
  640. {
  641. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  642. var x = cur.GetValueOrDefault();
  643. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  644. // unless nulls either never happen or always happen.
  645. if (cur.HasValue & x > valueVal)
  646. {
  647. valueVal = x;
  648. value = cur;
  649. }
  650. }
  651. }
  652. }
  653. #else
  654. var e = _source.GetAsyncEnumerator(_cancellationToken);
  655. try
  656. {
  657. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  658. // so we don't have to keep testing for nullity.
  659. do
  660. {
  661. if (!await e.MoveNextAsync().ConfigureAwait(false))
  662. {
  663. return value;
  664. }
  665. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  666. }
  667. while (!value.HasValue);
  668. // Keep hold of the wrapped value, and do comparisons on that, rather than
  669. // using the lifted operation each time.
  670. var valueVal = value.GetValueOrDefault();
  671. if (valueVal >= 0)
  672. {
  673. // We can fast-path this case where we know HasValue will
  674. // never affect the outcome, without constantly checking
  675. // if we're in such a state. Similar fast-paths could
  676. // be done for other cases, but as all-positive or mostly-
  677. // positive integer values are quite common in real-world
  678. // uses, it's only been done for int? and long?.
  679. while (await e.MoveNextAsync().ConfigureAwait(false))
  680. {
  681. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  682. var x = cur.GetValueOrDefault();
  683. if (x > valueVal)
  684. {
  685. valueVal = x;
  686. value = cur;
  687. }
  688. }
  689. }
  690. else
  691. {
  692. while (await e.MoveNextAsync().ConfigureAwait(false))
  693. {
  694. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  695. var x = cur.GetValueOrDefault();
  696. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  697. // unless nulls either never happen or always happen.
  698. if (cur.HasValue & x > valueVal)
  699. {
  700. valueVal = x;
  701. value = cur;
  702. }
  703. }
  704. }
  705. }
  706. finally
  707. {
  708. await e.DisposeAsync().ConfigureAwait(false);
  709. }
  710. #endif
  711. return value;
  712. }
  713. }
  714. #endif
  715. public static Task<long> MaxAsync(this IAsyncEnumerable<long> source, CancellationToken cancellationToken = default)
  716. {
  717. if (source == null)
  718. throw Error.ArgumentNull(nameof(source));
  719. return Core(source, cancellationToken);
  720. static async Task<long> Core(IAsyncEnumerable<long> _source, CancellationToken _cancellationToken)
  721. {
  722. long value;
  723. #if CSHARP8
  724. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  725. {
  726. if (!await e.MoveNextAsync())
  727. {
  728. throw Error.NoElements();
  729. }
  730. value = e.Current;
  731. while (await e.MoveNextAsync())
  732. {
  733. var x = e.Current;
  734. if (x > value)
  735. {
  736. value = x;
  737. }
  738. }
  739. }
  740. #else
  741. var e = _source.GetAsyncEnumerator(_cancellationToken);
  742. try
  743. {
  744. if (!await e.MoveNextAsync().ConfigureAwait(false))
  745. {
  746. throw Error.NoElements();
  747. }
  748. value = e.Current;
  749. while (await e.MoveNextAsync().ConfigureAwait(false))
  750. {
  751. var x = e.Current;
  752. if (x > value)
  753. {
  754. value = x;
  755. }
  756. }
  757. }
  758. finally
  759. {
  760. await e.DisposeAsync().ConfigureAwait(false);
  761. }
  762. #endif
  763. return value;
  764. }
  765. }
  766. public static Task<long> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken = default)
  767. {
  768. if (source == null)
  769. throw Error.ArgumentNull(nameof(source));
  770. if (selector == null)
  771. throw Error.ArgumentNull(nameof(selector));
  772. return Core(source, selector, cancellationToken);
  773. static async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, long> _selector, CancellationToken _cancellationToken)
  774. {
  775. long value;
  776. #if CSHARP8
  777. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  778. {
  779. if (!await e.MoveNextAsync())
  780. {
  781. throw Error.NoElements();
  782. }
  783. value = _selector(e.Current);
  784. while (await e.MoveNextAsync())
  785. {
  786. var x = _selector(e.Current);
  787. if (x > value)
  788. {
  789. value = x;
  790. }
  791. }
  792. }
  793. #else
  794. var e = _source.GetAsyncEnumerator(_cancellationToken);
  795. try
  796. {
  797. if (!await e.MoveNextAsync().ConfigureAwait(false))
  798. {
  799. throw Error.NoElements();
  800. }
  801. value = _selector(e.Current);
  802. while (await e.MoveNextAsync().ConfigureAwait(false))
  803. {
  804. var x = _selector(e.Current);
  805. if (x > value)
  806. {
  807. value = x;
  808. }
  809. }
  810. }
  811. finally
  812. {
  813. await e.DisposeAsync().ConfigureAwait(false);
  814. }
  815. #endif
  816. return value;
  817. }
  818. }
  819. public static Task<long> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  820. {
  821. if (source == null)
  822. throw Error.ArgumentNull(nameof(source));
  823. if (selector == null)
  824. throw Error.ArgumentNull(nameof(selector));
  825. return Core(source, selector, cancellationToken);
  826. static async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<long>> _selector, CancellationToken _cancellationToken)
  827. {
  828. long value;
  829. #if CSHARP8
  830. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  831. {
  832. if (!await e.MoveNextAsync())
  833. {
  834. throw Error.NoElements();
  835. }
  836. value = await _selector(e.Current).ConfigureAwait(false);
  837. while (await e.MoveNextAsync())
  838. {
  839. var x = await _selector(e.Current).ConfigureAwait(false);
  840. if (x > value)
  841. {
  842. value = x;
  843. }
  844. }
  845. }
  846. #else
  847. var e = _source.GetAsyncEnumerator(_cancellationToken);
  848. try
  849. {
  850. if (!await e.MoveNextAsync().ConfigureAwait(false))
  851. {
  852. throw Error.NoElements();
  853. }
  854. value = await _selector(e.Current).ConfigureAwait(false);
  855. while (await e.MoveNextAsync().ConfigureAwait(false))
  856. {
  857. var x = await _selector(e.Current).ConfigureAwait(false);
  858. if (x > value)
  859. {
  860. value = x;
  861. }
  862. }
  863. }
  864. finally
  865. {
  866. await e.DisposeAsync().ConfigureAwait(false);
  867. }
  868. #endif
  869. return value;
  870. }
  871. }
  872. #if !NO_DEEP_CANCELLATION
  873. public static Task<long> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  874. {
  875. if (source == null)
  876. throw Error.ArgumentNull(nameof(source));
  877. if (selector == null)
  878. throw Error.ArgumentNull(nameof(selector));
  879. return Core(source, selector, cancellationToken);
  880. static async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<long>> _selector, CancellationToken _cancellationToken)
  881. {
  882. long value;
  883. #if CSHARP8
  884. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  885. {
  886. if (!await e.MoveNextAsync())
  887. {
  888. throw Error.NoElements();
  889. }
  890. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  891. while (await e.MoveNextAsync())
  892. {
  893. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  894. if (x > value)
  895. {
  896. value = x;
  897. }
  898. }
  899. }
  900. #else
  901. var e = _source.GetAsyncEnumerator(_cancellationToken);
  902. try
  903. {
  904. if (!await e.MoveNextAsync().ConfigureAwait(false))
  905. {
  906. throw Error.NoElements();
  907. }
  908. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  909. while (await e.MoveNextAsync().ConfigureAwait(false))
  910. {
  911. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  912. if (x > value)
  913. {
  914. value = x;
  915. }
  916. }
  917. }
  918. finally
  919. {
  920. await e.DisposeAsync().ConfigureAwait(false);
  921. }
  922. #endif
  923. return value;
  924. }
  925. }
  926. #endif
  927. public static Task<long?> MaxAsync(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken = default)
  928. {
  929. if (source == null)
  930. throw Error.ArgumentNull(nameof(source));
  931. return Core(source, cancellationToken);
  932. static async Task<long?> Core(IAsyncEnumerable<long?> _source, CancellationToken _cancellationToken)
  933. {
  934. long? value = null;
  935. #if CSHARP8
  936. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  937. {
  938. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  939. // so we don't have to keep testing for nullity.
  940. do
  941. {
  942. if (!await e.MoveNextAsync())
  943. {
  944. return value;
  945. }
  946. value = e.Current;
  947. }
  948. while (!value.HasValue);
  949. // Keep hold of the wrapped value, and do comparisons on that, rather than
  950. // using the lifted operation each time.
  951. var valueVal = value.GetValueOrDefault();
  952. if (valueVal >= 0)
  953. {
  954. // We can fast-path this case where we know HasValue will
  955. // never affect the outcome, without constantly checking
  956. // if we're in such a state. Similar fast-paths could
  957. // be done for other cases, but as all-positive or mostly-
  958. // positive integer values are quite common in real-world
  959. // uses, it's only been done for int? and long?.
  960. while (await e.MoveNextAsync())
  961. {
  962. var cur = e.Current;
  963. var x = cur.GetValueOrDefault();
  964. if (x > valueVal)
  965. {
  966. valueVal = x;
  967. value = cur;
  968. }
  969. }
  970. }
  971. else
  972. {
  973. while (await e.MoveNextAsync())
  974. {
  975. var cur = e.Current;
  976. var x = cur.GetValueOrDefault();
  977. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  978. // unless nulls either never happen or always happen.
  979. if (cur.HasValue & x > valueVal)
  980. {
  981. valueVal = x;
  982. value = cur;
  983. }
  984. }
  985. }
  986. }
  987. #else
  988. var e = _source.GetAsyncEnumerator(_cancellationToken);
  989. try
  990. {
  991. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  992. // so we don't have to keep testing for nullity.
  993. do
  994. {
  995. if (!await e.MoveNextAsync().ConfigureAwait(false))
  996. {
  997. return value;
  998. }
  999. value = e.Current;
  1000. }
  1001. while (!value.HasValue);
  1002. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1003. // using the lifted operation each time.
  1004. var valueVal = value.GetValueOrDefault();
  1005. if (valueVal >= 0)
  1006. {
  1007. // We can fast-path this case where we know HasValue will
  1008. // never affect the outcome, without constantly checking
  1009. // if we're in such a state. Similar fast-paths could
  1010. // be done for other cases, but as all-positive or mostly-
  1011. // positive integer values are quite common in real-world
  1012. // uses, it's only been done for int? and long?.
  1013. while (await e.MoveNextAsync().ConfigureAwait(false))
  1014. {
  1015. var cur = e.Current;
  1016. var x = cur.GetValueOrDefault();
  1017. if (x > valueVal)
  1018. {
  1019. valueVal = x;
  1020. value = cur;
  1021. }
  1022. }
  1023. }
  1024. else
  1025. {
  1026. while (await e.MoveNextAsync().ConfigureAwait(false))
  1027. {
  1028. var cur = e.Current;
  1029. var x = cur.GetValueOrDefault();
  1030. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1031. // unless nulls either never happen or always happen.
  1032. if (cur.HasValue & x > valueVal)
  1033. {
  1034. valueVal = x;
  1035. value = cur;
  1036. }
  1037. }
  1038. }
  1039. }
  1040. finally
  1041. {
  1042. await e.DisposeAsync().ConfigureAwait(false);
  1043. }
  1044. #endif
  1045. return value;
  1046. }
  1047. }
  1048. public static Task<long?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken = default)
  1049. {
  1050. if (source == null)
  1051. throw Error.ArgumentNull(nameof(source));
  1052. if (selector == null)
  1053. throw Error.ArgumentNull(nameof(selector));
  1054. return Core(source, selector, cancellationToken);
  1055. static async Task<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, long?> _selector, CancellationToken _cancellationToken)
  1056. {
  1057. long? value = null;
  1058. #if CSHARP8
  1059. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  1060. {
  1061. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1062. // so we don't have to keep testing for nullity.
  1063. do
  1064. {
  1065. if (!await e.MoveNextAsync())
  1066. {
  1067. return value;
  1068. }
  1069. value = _selector(e.Current);
  1070. }
  1071. while (!value.HasValue);
  1072. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1073. // using the lifted operation each time.
  1074. var valueVal = value.GetValueOrDefault();
  1075. if (valueVal >= 0)
  1076. {
  1077. // We can fast-path this case where we know HasValue will
  1078. // never affect the outcome, without constantly checking
  1079. // if we're in such a state. Similar fast-paths could
  1080. // be done for other cases, but as all-positive or mostly-
  1081. // positive integer values are quite common in real-world
  1082. // uses, it's only been done for int? and long?.
  1083. while (await e.MoveNextAsync())
  1084. {
  1085. var cur = _selector(e.Current);
  1086. var x = cur.GetValueOrDefault();
  1087. if (x > valueVal)
  1088. {
  1089. valueVal = x;
  1090. value = cur;
  1091. }
  1092. }
  1093. }
  1094. else
  1095. {
  1096. while (await e.MoveNextAsync())
  1097. {
  1098. var cur = _selector(e.Current);
  1099. var x = cur.GetValueOrDefault();
  1100. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1101. // unless nulls either never happen or always happen.
  1102. if (cur.HasValue & x > valueVal)
  1103. {
  1104. valueVal = x;
  1105. value = cur;
  1106. }
  1107. }
  1108. }
  1109. }
  1110. #else
  1111. var e = _source.GetAsyncEnumerator(_cancellationToken);
  1112. try
  1113. {
  1114. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1115. // so we don't have to keep testing for nullity.
  1116. do
  1117. {
  1118. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1119. {
  1120. return value;
  1121. }
  1122. value = _selector(e.Current);
  1123. }
  1124. while (!value.HasValue);
  1125. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1126. // using the lifted operation each time.
  1127. var valueVal = value.GetValueOrDefault();
  1128. if (valueVal >= 0)
  1129. {
  1130. // We can fast-path this case where we know HasValue will
  1131. // never affect the outcome, without constantly checking
  1132. // if we're in such a state. Similar fast-paths could
  1133. // be done for other cases, but as all-positive or mostly-
  1134. // positive integer values are quite common in real-world
  1135. // uses, it's only been done for int? and long?.
  1136. while (await e.MoveNextAsync().ConfigureAwait(false))
  1137. {
  1138. var cur = _selector(e.Current);
  1139. var x = cur.GetValueOrDefault();
  1140. if (x > valueVal)
  1141. {
  1142. valueVal = x;
  1143. value = cur;
  1144. }
  1145. }
  1146. }
  1147. else
  1148. {
  1149. while (await e.MoveNextAsync().ConfigureAwait(false))
  1150. {
  1151. var cur = _selector(e.Current);
  1152. var x = cur.GetValueOrDefault();
  1153. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1154. // unless nulls either never happen or always happen.
  1155. if (cur.HasValue & x > valueVal)
  1156. {
  1157. valueVal = x;
  1158. value = cur;
  1159. }
  1160. }
  1161. }
  1162. }
  1163. finally
  1164. {
  1165. await e.DisposeAsync().ConfigureAwait(false);
  1166. }
  1167. #endif
  1168. return value;
  1169. }
  1170. }
  1171. public static Task<long?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  1172. {
  1173. if (source == null)
  1174. throw Error.ArgumentNull(nameof(source));
  1175. if (selector == null)
  1176. throw Error.ArgumentNull(nameof(selector));
  1177. return Core(source, selector, cancellationToken);
  1178. static async Task<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<long?>> _selector, CancellationToken _cancellationToken)
  1179. {
  1180. long? value = null;
  1181. #if CSHARP8
  1182. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  1183. {
  1184. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1185. // so we don't have to keep testing for nullity.
  1186. do
  1187. {
  1188. if (!await e.MoveNextAsync())
  1189. {
  1190. return value;
  1191. }
  1192. value = await _selector(e.Current).ConfigureAwait(false);
  1193. }
  1194. while (!value.HasValue);
  1195. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1196. // using the lifted operation each time.
  1197. var valueVal = value.GetValueOrDefault();
  1198. if (valueVal >= 0)
  1199. {
  1200. // We can fast-path this case where we know HasValue will
  1201. // never affect the outcome, without constantly checking
  1202. // if we're in such a state. Similar fast-paths could
  1203. // be done for other cases, but as all-positive or mostly-
  1204. // positive integer values are quite common in real-world
  1205. // uses, it's only been done for int? and long?.
  1206. while (await e.MoveNextAsync())
  1207. {
  1208. var cur = await _selector(e.Current).ConfigureAwait(false);
  1209. var x = cur.GetValueOrDefault();
  1210. if (x > valueVal)
  1211. {
  1212. valueVal = x;
  1213. value = cur;
  1214. }
  1215. }
  1216. }
  1217. else
  1218. {
  1219. while (await e.MoveNextAsync())
  1220. {
  1221. var cur = await _selector(e.Current).ConfigureAwait(false);
  1222. var x = cur.GetValueOrDefault();
  1223. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1224. // unless nulls either never happen or always happen.
  1225. if (cur.HasValue & x > valueVal)
  1226. {
  1227. valueVal = x;
  1228. value = cur;
  1229. }
  1230. }
  1231. }
  1232. }
  1233. #else
  1234. var e = _source.GetAsyncEnumerator(_cancellationToken);
  1235. try
  1236. {
  1237. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1238. // so we don't have to keep testing for nullity.
  1239. do
  1240. {
  1241. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1242. {
  1243. return value;
  1244. }
  1245. value = await _selector(e.Current).ConfigureAwait(false);
  1246. }
  1247. while (!value.HasValue);
  1248. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1249. // using the lifted operation each time.
  1250. var valueVal = value.GetValueOrDefault();
  1251. if (valueVal >= 0)
  1252. {
  1253. // We can fast-path this case where we know HasValue will
  1254. // never affect the outcome, without constantly checking
  1255. // if we're in such a state. Similar fast-paths could
  1256. // be done for other cases, but as all-positive or mostly-
  1257. // positive integer values are quite common in real-world
  1258. // uses, it's only been done for int? and long?.
  1259. while (await e.MoveNextAsync().ConfigureAwait(false))
  1260. {
  1261. var cur = await _selector(e.Current).ConfigureAwait(false);
  1262. var x = cur.GetValueOrDefault();
  1263. if (x > valueVal)
  1264. {
  1265. valueVal = x;
  1266. value = cur;
  1267. }
  1268. }
  1269. }
  1270. else
  1271. {
  1272. while (await e.MoveNextAsync().ConfigureAwait(false))
  1273. {
  1274. var cur = await _selector(e.Current).ConfigureAwait(false);
  1275. var x = cur.GetValueOrDefault();
  1276. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1277. // unless nulls either never happen or always happen.
  1278. if (cur.HasValue & x > valueVal)
  1279. {
  1280. valueVal = x;
  1281. value = cur;
  1282. }
  1283. }
  1284. }
  1285. }
  1286. finally
  1287. {
  1288. await e.DisposeAsync().ConfigureAwait(false);
  1289. }
  1290. #endif
  1291. return value;
  1292. }
  1293. }
  1294. #if !NO_DEEP_CANCELLATION
  1295. public static Task<long?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  1296. {
  1297. if (source == null)
  1298. throw Error.ArgumentNull(nameof(source));
  1299. if (selector == null)
  1300. throw Error.ArgumentNull(nameof(selector));
  1301. return Core(source, selector, cancellationToken);
  1302. static async Task<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<long?>> _selector, CancellationToken _cancellationToken)
  1303. {
  1304. long? value = null;
  1305. #if CSHARP8
  1306. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  1307. {
  1308. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1309. // so we don't have to keep testing for nullity.
  1310. do
  1311. {
  1312. if (!await e.MoveNextAsync())
  1313. {
  1314. return value;
  1315. }
  1316. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1317. }
  1318. while (!value.HasValue);
  1319. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1320. // using the lifted operation each time.
  1321. var valueVal = value.GetValueOrDefault();
  1322. if (valueVal >= 0)
  1323. {
  1324. // We can fast-path this case where we know HasValue will
  1325. // never affect the outcome, without constantly checking
  1326. // if we're in such a state. Similar fast-paths could
  1327. // be done for other cases, but as all-positive or mostly-
  1328. // positive integer values are quite common in real-world
  1329. // uses, it's only been done for int? and long?.
  1330. while (await e.MoveNextAsync())
  1331. {
  1332. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1333. var x = cur.GetValueOrDefault();
  1334. if (x > valueVal)
  1335. {
  1336. valueVal = x;
  1337. value = cur;
  1338. }
  1339. }
  1340. }
  1341. else
  1342. {
  1343. while (await e.MoveNextAsync())
  1344. {
  1345. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1346. var x = cur.GetValueOrDefault();
  1347. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1348. // unless nulls either never happen or always happen.
  1349. if (cur.HasValue & x > valueVal)
  1350. {
  1351. valueVal = x;
  1352. value = cur;
  1353. }
  1354. }
  1355. }
  1356. }
  1357. #else
  1358. var e = _source.GetAsyncEnumerator(_cancellationToken);
  1359. try
  1360. {
  1361. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1362. // so we don't have to keep testing for nullity.
  1363. do
  1364. {
  1365. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1366. {
  1367. return value;
  1368. }
  1369. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1370. }
  1371. while (!value.HasValue);
  1372. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1373. // using the lifted operation each time.
  1374. var valueVal = value.GetValueOrDefault();
  1375. if (valueVal >= 0)
  1376. {
  1377. // We can fast-path this case where we know HasValue will
  1378. // never affect the outcome, without constantly checking
  1379. // if we're in such a state. Similar fast-paths could
  1380. // be done for other cases, but as all-positive or mostly-
  1381. // positive integer values are quite common in real-world
  1382. // uses, it's only been done for int? and long?.
  1383. while (await e.MoveNextAsync().ConfigureAwait(false))
  1384. {
  1385. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1386. var x = cur.GetValueOrDefault();
  1387. if (x > valueVal)
  1388. {
  1389. valueVal = x;
  1390. value = cur;
  1391. }
  1392. }
  1393. }
  1394. else
  1395. {
  1396. while (await e.MoveNextAsync().ConfigureAwait(false))
  1397. {
  1398. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1399. var x = cur.GetValueOrDefault();
  1400. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1401. // unless nulls either never happen or always happen.
  1402. if (cur.HasValue & x > valueVal)
  1403. {
  1404. valueVal = x;
  1405. value = cur;
  1406. }
  1407. }
  1408. }
  1409. }
  1410. finally
  1411. {
  1412. await e.DisposeAsync().ConfigureAwait(false);
  1413. }
  1414. #endif
  1415. return value;
  1416. }
  1417. }
  1418. #endif
  1419. public static Task<float> MaxAsync(this IAsyncEnumerable<float> source, CancellationToken cancellationToken = default)
  1420. {
  1421. if (source == null)
  1422. throw Error.ArgumentNull(nameof(source));
  1423. return Core(source, cancellationToken);
  1424. static async Task<float> Core(IAsyncEnumerable<float> _source, CancellationToken _cancellationToken)
  1425. {
  1426. float value;
  1427. #if CSHARP8
  1428. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  1429. {
  1430. if (!await e.MoveNextAsync())
  1431. {
  1432. throw Error.NoElements();
  1433. }
  1434. value = e.Current;
  1435. // NaN is ordered less than all other values. We need to do explicit checks
  1436. // to ensure this, but once we've found a value that is not NaN we need no
  1437. // longer worry about it, so first loop until such a value is found (or not,
  1438. // as the case may be).
  1439. while (float.IsNaN(value))
  1440. {
  1441. if (!await e.MoveNextAsync())
  1442. {
  1443. return value;
  1444. }
  1445. value = e.Current;
  1446. }
  1447. while (await e.MoveNextAsync())
  1448. {
  1449. var x = e.Current;
  1450. if (x > value)
  1451. {
  1452. value = x;
  1453. }
  1454. }
  1455. }
  1456. #else
  1457. var e = _source.GetAsyncEnumerator(_cancellationToken);
  1458. try
  1459. {
  1460. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1461. {
  1462. throw Error.NoElements();
  1463. }
  1464. value = e.Current;
  1465. // NaN is ordered less than all other values. We need to do explicit checks
  1466. // to ensure this, but once we've found a value that is not NaN we need no
  1467. // longer worry about it, so first loop until such a value is found (or not,
  1468. // as the case may be).
  1469. while (float.IsNaN(value))
  1470. {
  1471. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1472. {
  1473. return value;
  1474. }
  1475. value = e.Current;
  1476. }
  1477. while (await e.MoveNextAsync().ConfigureAwait(false))
  1478. {
  1479. var x = e.Current;
  1480. if (x > value)
  1481. {
  1482. value = x;
  1483. }
  1484. }
  1485. }
  1486. finally
  1487. {
  1488. await e.DisposeAsync().ConfigureAwait(false);
  1489. }
  1490. #endif
  1491. return value;
  1492. }
  1493. }
  1494. public static Task<float> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken = default)
  1495. {
  1496. if (source == null)
  1497. throw Error.ArgumentNull(nameof(source));
  1498. if (selector == null)
  1499. throw Error.ArgumentNull(nameof(selector));
  1500. return Core(source, selector, cancellationToken);
  1501. static async Task<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, float> _selector, CancellationToken _cancellationToken)
  1502. {
  1503. float value;
  1504. #if CSHARP8
  1505. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  1506. {
  1507. if (!await e.MoveNextAsync())
  1508. {
  1509. throw Error.NoElements();
  1510. }
  1511. value = _selector(e.Current);
  1512. // NaN is ordered less than all other values. We need to do explicit checks
  1513. // to ensure this, but once we've found a value that is not NaN we need no
  1514. // longer worry about it, so first loop until such a value is found (or not,
  1515. // as the case may be).
  1516. while (float.IsNaN(value))
  1517. {
  1518. if (!await e.MoveNextAsync())
  1519. {
  1520. return value;
  1521. }
  1522. value = _selector(e.Current);
  1523. }
  1524. while (await e.MoveNextAsync())
  1525. {
  1526. var x = _selector(e.Current);
  1527. if (x > value)
  1528. {
  1529. value = x;
  1530. }
  1531. }
  1532. }
  1533. #else
  1534. var e = _source.GetAsyncEnumerator(_cancellationToken);
  1535. try
  1536. {
  1537. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1538. {
  1539. throw Error.NoElements();
  1540. }
  1541. value = _selector(e.Current);
  1542. // NaN is ordered less than all other values. We need to do explicit checks
  1543. // to ensure this, but once we've found a value that is not NaN we need no
  1544. // longer worry about it, so first loop until such a value is found (or not,
  1545. // as the case may be).
  1546. while (float.IsNaN(value))
  1547. {
  1548. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1549. {
  1550. return value;
  1551. }
  1552. value = _selector(e.Current);
  1553. }
  1554. while (await e.MoveNextAsync().ConfigureAwait(false))
  1555. {
  1556. var x = _selector(e.Current);
  1557. if (x > value)
  1558. {
  1559. value = x;
  1560. }
  1561. }
  1562. }
  1563. finally
  1564. {
  1565. await e.DisposeAsync().ConfigureAwait(false);
  1566. }
  1567. #endif
  1568. return value;
  1569. }
  1570. }
  1571. public static Task<float> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  1572. {
  1573. if (source == null)
  1574. throw Error.ArgumentNull(nameof(source));
  1575. if (selector == null)
  1576. throw Error.ArgumentNull(nameof(selector));
  1577. return Core(source, selector, cancellationToken);
  1578. static async Task<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<float>> _selector, CancellationToken _cancellationToken)
  1579. {
  1580. float value;
  1581. #if CSHARP8
  1582. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  1583. {
  1584. if (!await e.MoveNextAsync())
  1585. {
  1586. throw Error.NoElements();
  1587. }
  1588. value = await _selector(e.Current).ConfigureAwait(false);
  1589. // NaN is ordered less than all other values. We need to do explicit checks
  1590. // to ensure this, but once we've found a value that is not NaN we need no
  1591. // longer worry about it, so first loop until such a value is found (or not,
  1592. // as the case may be).
  1593. while (float.IsNaN(value))
  1594. {
  1595. if (!await e.MoveNextAsync())
  1596. {
  1597. return value;
  1598. }
  1599. value = await _selector(e.Current).ConfigureAwait(false);
  1600. }
  1601. while (await e.MoveNextAsync())
  1602. {
  1603. var x = await _selector(e.Current).ConfigureAwait(false);
  1604. if (x > value)
  1605. {
  1606. value = x;
  1607. }
  1608. }
  1609. }
  1610. #else
  1611. var e = _source.GetAsyncEnumerator(_cancellationToken);
  1612. try
  1613. {
  1614. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1615. {
  1616. throw Error.NoElements();
  1617. }
  1618. value = await _selector(e.Current).ConfigureAwait(false);
  1619. // NaN is ordered less than all other values. We need to do explicit checks
  1620. // to ensure this, but once we've found a value that is not NaN we need no
  1621. // longer worry about it, so first loop until such a value is found (or not,
  1622. // as the case may be).
  1623. while (float.IsNaN(value))
  1624. {
  1625. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1626. {
  1627. return value;
  1628. }
  1629. value = await _selector(e.Current).ConfigureAwait(false);
  1630. }
  1631. while (await e.MoveNextAsync().ConfigureAwait(false))
  1632. {
  1633. var x = await _selector(e.Current).ConfigureAwait(false);
  1634. if (x > value)
  1635. {
  1636. value = x;
  1637. }
  1638. }
  1639. }
  1640. finally
  1641. {
  1642. await e.DisposeAsync().ConfigureAwait(false);
  1643. }
  1644. #endif
  1645. return value;
  1646. }
  1647. }
  1648. #if !NO_DEEP_CANCELLATION
  1649. public static Task<float> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  1650. {
  1651. if (source == null)
  1652. throw Error.ArgumentNull(nameof(source));
  1653. if (selector == null)
  1654. throw Error.ArgumentNull(nameof(selector));
  1655. return Core(source, selector, cancellationToken);
  1656. static async Task<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<float>> _selector, CancellationToken _cancellationToken)
  1657. {
  1658. float value;
  1659. #if CSHARP8
  1660. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  1661. {
  1662. if (!await e.MoveNextAsync())
  1663. {
  1664. throw Error.NoElements();
  1665. }
  1666. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1667. // NaN is ordered less than all other values. We need to do explicit checks
  1668. // to ensure this, but once we've found a value that is not NaN we need no
  1669. // longer worry about it, so first loop until such a value is found (or not,
  1670. // as the case may be).
  1671. while (float.IsNaN(value))
  1672. {
  1673. if (!await e.MoveNextAsync())
  1674. {
  1675. return value;
  1676. }
  1677. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1678. }
  1679. while (await e.MoveNextAsync())
  1680. {
  1681. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1682. if (x > value)
  1683. {
  1684. value = x;
  1685. }
  1686. }
  1687. }
  1688. #else
  1689. var e = _source.GetAsyncEnumerator(_cancellationToken);
  1690. try
  1691. {
  1692. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1693. {
  1694. throw Error.NoElements();
  1695. }
  1696. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1697. // NaN is ordered less than all other values. We need to do explicit checks
  1698. // to ensure this, but once we've found a value that is not NaN we need no
  1699. // longer worry about it, so first loop until such a value is found (or not,
  1700. // as the case may be).
  1701. while (float.IsNaN(value))
  1702. {
  1703. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1704. {
  1705. return value;
  1706. }
  1707. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1708. }
  1709. while (await e.MoveNextAsync().ConfigureAwait(false))
  1710. {
  1711. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  1712. if (x > value)
  1713. {
  1714. value = x;
  1715. }
  1716. }
  1717. }
  1718. finally
  1719. {
  1720. await e.DisposeAsync().ConfigureAwait(false);
  1721. }
  1722. #endif
  1723. return value;
  1724. }
  1725. }
  1726. #endif
  1727. public static Task<float?> MaxAsync(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken = default)
  1728. {
  1729. if (source == null)
  1730. throw Error.ArgumentNull(nameof(source));
  1731. return Core(source, cancellationToken);
  1732. static async Task<float?> Core(IAsyncEnumerable<float?> _source, CancellationToken _cancellationToken)
  1733. {
  1734. float? value = null;
  1735. #if CSHARP8
  1736. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  1737. {
  1738. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1739. // so we don't have to keep testing for nullity.
  1740. do
  1741. {
  1742. if (!await e.MoveNextAsync())
  1743. {
  1744. return value;
  1745. }
  1746. value = e.Current;
  1747. }
  1748. while (!value.HasValue);
  1749. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1750. // using the lifted operation each time.
  1751. var valueVal = value.GetValueOrDefault();
  1752. // NaN is ordered less than all other values. We need to do explicit checks
  1753. // to ensure this, but once we've found a value that is not NaN we need no
  1754. // longer worry about it, so first loop until such a value is found (or not,
  1755. // as the case may be).
  1756. while (float.IsNaN(valueVal))
  1757. {
  1758. if (!await e.MoveNextAsync())
  1759. {
  1760. return value;
  1761. }
  1762. var cur = e.Current;
  1763. if (cur.HasValue)
  1764. {
  1765. valueVal = (value = cur).GetValueOrDefault();
  1766. }
  1767. }
  1768. while (await e.MoveNextAsync())
  1769. {
  1770. var cur = e.Current;
  1771. var x = cur.GetValueOrDefault();
  1772. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1773. // unless nulls either never happen or always happen.
  1774. if (cur.HasValue & x > valueVal)
  1775. {
  1776. valueVal = x;
  1777. value = cur;
  1778. }
  1779. }
  1780. }
  1781. #else
  1782. var e = _source.GetAsyncEnumerator(_cancellationToken);
  1783. try
  1784. {
  1785. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1786. // so we don't have to keep testing for nullity.
  1787. do
  1788. {
  1789. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1790. {
  1791. return value;
  1792. }
  1793. value = e.Current;
  1794. }
  1795. while (!value.HasValue);
  1796. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1797. // using the lifted operation each time.
  1798. var valueVal = value.GetValueOrDefault();
  1799. // NaN is ordered less than all other values. We need to do explicit checks
  1800. // to ensure this, but once we've found a value that is not NaN we need no
  1801. // longer worry about it, so first loop until such a value is found (or not,
  1802. // as the case may be).
  1803. while (float.IsNaN(valueVal))
  1804. {
  1805. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1806. {
  1807. return value;
  1808. }
  1809. var cur = e.Current;
  1810. if (cur.HasValue)
  1811. {
  1812. valueVal = (value = cur).GetValueOrDefault();
  1813. }
  1814. }
  1815. while (await e.MoveNextAsync().ConfigureAwait(false))
  1816. {
  1817. var cur = e.Current;
  1818. var x = cur.GetValueOrDefault();
  1819. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1820. // unless nulls either never happen or always happen.
  1821. if (cur.HasValue & x > valueVal)
  1822. {
  1823. valueVal = x;
  1824. value = cur;
  1825. }
  1826. }
  1827. }
  1828. finally
  1829. {
  1830. await e.DisposeAsync().ConfigureAwait(false);
  1831. }
  1832. #endif
  1833. return value;
  1834. }
  1835. }
  1836. public static Task<float?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken = default)
  1837. {
  1838. if (source == null)
  1839. throw Error.ArgumentNull(nameof(source));
  1840. if (selector == null)
  1841. throw Error.ArgumentNull(nameof(selector));
  1842. return Core(source, selector, cancellationToken);
  1843. static async Task<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, float?> _selector, CancellationToken _cancellationToken)
  1844. {
  1845. float? value = null;
  1846. #if CSHARP8
  1847. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  1848. {
  1849. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1850. // so we don't have to keep testing for nullity.
  1851. do
  1852. {
  1853. if (!await e.MoveNextAsync())
  1854. {
  1855. return value;
  1856. }
  1857. value = _selector(e.Current);
  1858. }
  1859. while (!value.HasValue);
  1860. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1861. // using the lifted operation each time.
  1862. var valueVal = value.GetValueOrDefault();
  1863. // NaN is ordered less than all other values. We need to do explicit checks
  1864. // to ensure this, but once we've found a value that is not NaN we need no
  1865. // longer worry about it, so first loop until such a value is found (or not,
  1866. // as the case may be).
  1867. while (float.IsNaN(valueVal))
  1868. {
  1869. if (!await e.MoveNextAsync())
  1870. {
  1871. return value;
  1872. }
  1873. var cur = _selector(e.Current);
  1874. if (cur.HasValue)
  1875. {
  1876. valueVal = (value = cur).GetValueOrDefault();
  1877. }
  1878. }
  1879. while (await e.MoveNextAsync())
  1880. {
  1881. var cur = _selector(e.Current);
  1882. var x = cur.GetValueOrDefault();
  1883. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1884. // unless nulls either never happen or always happen.
  1885. if (cur.HasValue & x > valueVal)
  1886. {
  1887. valueVal = x;
  1888. value = cur;
  1889. }
  1890. }
  1891. }
  1892. #else
  1893. var e = _source.GetAsyncEnumerator(_cancellationToken);
  1894. try
  1895. {
  1896. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1897. // so we don't have to keep testing for nullity.
  1898. do
  1899. {
  1900. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1901. {
  1902. return value;
  1903. }
  1904. value = _selector(e.Current);
  1905. }
  1906. while (!value.HasValue);
  1907. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1908. // using the lifted operation each time.
  1909. var valueVal = value.GetValueOrDefault();
  1910. // NaN is ordered less than all other values. We need to do explicit checks
  1911. // to ensure this, but once we've found a value that is not NaN we need no
  1912. // longer worry about it, so first loop until such a value is found (or not,
  1913. // as the case may be).
  1914. while (float.IsNaN(valueVal))
  1915. {
  1916. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1917. {
  1918. return value;
  1919. }
  1920. var cur = _selector(e.Current);
  1921. if (cur.HasValue)
  1922. {
  1923. valueVal = (value = cur).GetValueOrDefault();
  1924. }
  1925. }
  1926. while (await e.MoveNextAsync().ConfigureAwait(false))
  1927. {
  1928. var cur = _selector(e.Current);
  1929. var x = cur.GetValueOrDefault();
  1930. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1931. // unless nulls either never happen or always happen.
  1932. if (cur.HasValue & x > valueVal)
  1933. {
  1934. valueVal = x;
  1935. value = cur;
  1936. }
  1937. }
  1938. }
  1939. finally
  1940. {
  1941. await e.DisposeAsync().ConfigureAwait(false);
  1942. }
  1943. #endif
  1944. return value;
  1945. }
  1946. }
  1947. public static Task<float?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  1948. {
  1949. if (source == null)
  1950. throw Error.ArgumentNull(nameof(source));
  1951. if (selector == null)
  1952. throw Error.ArgumentNull(nameof(selector));
  1953. return Core(source, selector, cancellationToken);
  1954. static async Task<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<float?>> _selector, CancellationToken _cancellationToken)
  1955. {
  1956. float? value = null;
  1957. #if CSHARP8
  1958. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  1959. {
  1960. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1961. // so we don't have to keep testing for nullity.
  1962. do
  1963. {
  1964. if (!await e.MoveNextAsync())
  1965. {
  1966. return value;
  1967. }
  1968. value = await _selector(e.Current).ConfigureAwait(false);
  1969. }
  1970. while (!value.HasValue);
  1971. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1972. // using the lifted operation each time.
  1973. var valueVal = value.GetValueOrDefault();
  1974. // NaN is ordered less than all other values. We need to do explicit checks
  1975. // to ensure this, but once we've found a value that is not NaN we need no
  1976. // longer worry about it, so first loop until such a value is found (or not,
  1977. // as the case may be).
  1978. while (float.IsNaN(valueVal))
  1979. {
  1980. if (!await e.MoveNextAsync())
  1981. {
  1982. return value;
  1983. }
  1984. var cur = await _selector(e.Current).ConfigureAwait(false);
  1985. if (cur.HasValue)
  1986. {
  1987. valueVal = (value = cur).GetValueOrDefault();
  1988. }
  1989. }
  1990. while (await e.MoveNextAsync())
  1991. {
  1992. var cur = await _selector(e.Current).ConfigureAwait(false);
  1993. var x = cur.GetValueOrDefault();
  1994. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1995. // unless nulls either never happen or always happen.
  1996. if (cur.HasValue & x > valueVal)
  1997. {
  1998. valueVal = x;
  1999. value = cur;
  2000. }
  2001. }
  2002. }
  2003. #else
  2004. var e = _source.GetAsyncEnumerator(_cancellationToken);
  2005. try
  2006. {
  2007. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2008. // so we don't have to keep testing for nullity.
  2009. do
  2010. {
  2011. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2012. {
  2013. return value;
  2014. }
  2015. value = await _selector(e.Current).ConfigureAwait(false);
  2016. }
  2017. while (!value.HasValue);
  2018. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2019. // using the lifted operation each time.
  2020. var valueVal = value.GetValueOrDefault();
  2021. // NaN is ordered less than all other values. We need to do explicit checks
  2022. // to ensure this, but once we've found a value that is not NaN we need no
  2023. // longer worry about it, so first loop until such a value is found (or not,
  2024. // as the case may be).
  2025. while (float.IsNaN(valueVal))
  2026. {
  2027. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2028. {
  2029. return value;
  2030. }
  2031. var cur = await _selector(e.Current).ConfigureAwait(false);
  2032. if (cur.HasValue)
  2033. {
  2034. valueVal = (value = cur).GetValueOrDefault();
  2035. }
  2036. }
  2037. while (await e.MoveNextAsync().ConfigureAwait(false))
  2038. {
  2039. var cur = await _selector(e.Current).ConfigureAwait(false);
  2040. var x = cur.GetValueOrDefault();
  2041. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2042. // unless nulls either never happen or always happen.
  2043. if (cur.HasValue & x > valueVal)
  2044. {
  2045. valueVal = x;
  2046. value = cur;
  2047. }
  2048. }
  2049. }
  2050. finally
  2051. {
  2052. await e.DisposeAsync().ConfigureAwait(false);
  2053. }
  2054. #endif
  2055. return value;
  2056. }
  2057. }
  2058. #if !NO_DEEP_CANCELLATION
  2059. public static Task<float?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  2060. {
  2061. if (source == null)
  2062. throw Error.ArgumentNull(nameof(source));
  2063. if (selector == null)
  2064. throw Error.ArgumentNull(nameof(selector));
  2065. return Core(source, selector, cancellationToken);
  2066. static async Task<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<float?>> _selector, CancellationToken _cancellationToken)
  2067. {
  2068. float? value = null;
  2069. #if CSHARP8
  2070. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  2071. {
  2072. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2073. // so we don't have to keep testing for nullity.
  2074. do
  2075. {
  2076. if (!await e.MoveNextAsync())
  2077. {
  2078. return value;
  2079. }
  2080. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2081. }
  2082. while (!value.HasValue);
  2083. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2084. // using the lifted operation each time.
  2085. var valueVal = value.GetValueOrDefault();
  2086. // NaN is ordered less than all other values. We need to do explicit checks
  2087. // to ensure this, but once we've found a value that is not NaN we need no
  2088. // longer worry about it, so first loop until such a value is found (or not,
  2089. // as the case may be).
  2090. while (float.IsNaN(valueVal))
  2091. {
  2092. if (!await e.MoveNextAsync())
  2093. {
  2094. return value;
  2095. }
  2096. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2097. if (cur.HasValue)
  2098. {
  2099. valueVal = (value = cur).GetValueOrDefault();
  2100. }
  2101. }
  2102. while (await e.MoveNextAsync())
  2103. {
  2104. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2105. var x = cur.GetValueOrDefault();
  2106. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2107. // unless nulls either never happen or always happen.
  2108. if (cur.HasValue & x > valueVal)
  2109. {
  2110. valueVal = x;
  2111. value = cur;
  2112. }
  2113. }
  2114. }
  2115. #else
  2116. var e = _source.GetAsyncEnumerator(_cancellationToken);
  2117. try
  2118. {
  2119. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2120. // so we don't have to keep testing for nullity.
  2121. do
  2122. {
  2123. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2124. {
  2125. return value;
  2126. }
  2127. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2128. }
  2129. while (!value.HasValue);
  2130. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2131. // using the lifted operation each time.
  2132. var valueVal = value.GetValueOrDefault();
  2133. // NaN is ordered less than all other values. We need to do explicit checks
  2134. // to ensure this, but once we've found a value that is not NaN we need no
  2135. // longer worry about it, so first loop until such a value is found (or not,
  2136. // as the case may be).
  2137. while (float.IsNaN(valueVal))
  2138. {
  2139. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2140. {
  2141. return value;
  2142. }
  2143. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2144. if (cur.HasValue)
  2145. {
  2146. valueVal = (value = cur).GetValueOrDefault();
  2147. }
  2148. }
  2149. while (await e.MoveNextAsync().ConfigureAwait(false))
  2150. {
  2151. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2152. var x = cur.GetValueOrDefault();
  2153. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2154. // unless nulls either never happen or always happen.
  2155. if (cur.HasValue & x > valueVal)
  2156. {
  2157. valueVal = x;
  2158. value = cur;
  2159. }
  2160. }
  2161. }
  2162. finally
  2163. {
  2164. await e.DisposeAsync().ConfigureAwait(false);
  2165. }
  2166. #endif
  2167. return value;
  2168. }
  2169. }
  2170. #endif
  2171. public static Task<double> MaxAsync(this IAsyncEnumerable<double> source, CancellationToken cancellationToken = default)
  2172. {
  2173. if (source == null)
  2174. throw Error.ArgumentNull(nameof(source));
  2175. return Core(source, cancellationToken);
  2176. static async Task<double> Core(IAsyncEnumerable<double> _source, CancellationToken _cancellationToken)
  2177. {
  2178. double value;
  2179. #if CSHARP8
  2180. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  2181. {
  2182. if (!await e.MoveNextAsync())
  2183. {
  2184. throw Error.NoElements();
  2185. }
  2186. value = e.Current;
  2187. // NaN is ordered less than all other values. We need to do explicit checks
  2188. // to ensure this, but once we've found a value that is not NaN we need no
  2189. // longer worry about it, so first loop until such a value is found (or not,
  2190. // as the case may be).
  2191. while (double.IsNaN(value))
  2192. {
  2193. if (!await e.MoveNextAsync())
  2194. {
  2195. return value;
  2196. }
  2197. value = e.Current;
  2198. }
  2199. while (await e.MoveNextAsync())
  2200. {
  2201. var x = e.Current;
  2202. if (x > value)
  2203. {
  2204. value = x;
  2205. }
  2206. }
  2207. }
  2208. #else
  2209. var e = _source.GetAsyncEnumerator(_cancellationToken);
  2210. try
  2211. {
  2212. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2213. {
  2214. throw Error.NoElements();
  2215. }
  2216. value = e.Current;
  2217. // NaN is ordered less than all other values. We need to do explicit checks
  2218. // to ensure this, but once we've found a value that is not NaN we need no
  2219. // longer worry about it, so first loop until such a value is found (or not,
  2220. // as the case may be).
  2221. while (double.IsNaN(value))
  2222. {
  2223. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2224. {
  2225. return value;
  2226. }
  2227. value = e.Current;
  2228. }
  2229. while (await e.MoveNextAsync().ConfigureAwait(false))
  2230. {
  2231. var x = e.Current;
  2232. if (x > value)
  2233. {
  2234. value = x;
  2235. }
  2236. }
  2237. }
  2238. finally
  2239. {
  2240. await e.DisposeAsync().ConfigureAwait(false);
  2241. }
  2242. #endif
  2243. return value;
  2244. }
  2245. }
  2246. public static Task<double> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken = default)
  2247. {
  2248. if (source == null)
  2249. throw Error.ArgumentNull(nameof(source));
  2250. if (selector == null)
  2251. throw Error.ArgumentNull(nameof(selector));
  2252. return Core(source, selector, cancellationToken);
  2253. static async Task<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, double> _selector, CancellationToken _cancellationToken)
  2254. {
  2255. double value;
  2256. #if CSHARP8
  2257. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  2258. {
  2259. if (!await e.MoveNextAsync())
  2260. {
  2261. throw Error.NoElements();
  2262. }
  2263. value = _selector(e.Current);
  2264. // NaN is ordered less than all other values. We need to do explicit checks
  2265. // to ensure this, but once we've found a value that is not NaN we need no
  2266. // longer worry about it, so first loop until such a value is found (or not,
  2267. // as the case may be).
  2268. while (double.IsNaN(value))
  2269. {
  2270. if (!await e.MoveNextAsync())
  2271. {
  2272. return value;
  2273. }
  2274. value = _selector(e.Current);
  2275. }
  2276. while (await e.MoveNextAsync())
  2277. {
  2278. var x = _selector(e.Current);
  2279. if (x > value)
  2280. {
  2281. value = x;
  2282. }
  2283. }
  2284. }
  2285. #else
  2286. var e = _source.GetAsyncEnumerator(_cancellationToken);
  2287. try
  2288. {
  2289. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2290. {
  2291. throw Error.NoElements();
  2292. }
  2293. value = _selector(e.Current);
  2294. // NaN is ordered less than all other values. We need to do explicit checks
  2295. // to ensure this, but once we've found a value that is not NaN we need no
  2296. // longer worry about it, so first loop until such a value is found (or not,
  2297. // as the case may be).
  2298. while (double.IsNaN(value))
  2299. {
  2300. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2301. {
  2302. return value;
  2303. }
  2304. value = _selector(e.Current);
  2305. }
  2306. while (await e.MoveNextAsync().ConfigureAwait(false))
  2307. {
  2308. var x = _selector(e.Current);
  2309. if (x > value)
  2310. {
  2311. value = x;
  2312. }
  2313. }
  2314. }
  2315. finally
  2316. {
  2317. await e.DisposeAsync().ConfigureAwait(false);
  2318. }
  2319. #endif
  2320. return value;
  2321. }
  2322. }
  2323. public static Task<double> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  2324. {
  2325. if (source == null)
  2326. throw Error.ArgumentNull(nameof(source));
  2327. if (selector == null)
  2328. throw Error.ArgumentNull(nameof(selector));
  2329. return Core(source, selector, cancellationToken);
  2330. static async Task<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<double>> _selector, CancellationToken _cancellationToken)
  2331. {
  2332. double value;
  2333. #if CSHARP8
  2334. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  2335. {
  2336. if (!await e.MoveNextAsync())
  2337. {
  2338. throw Error.NoElements();
  2339. }
  2340. value = await _selector(e.Current).ConfigureAwait(false);
  2341. // NaN is ordered less than all other values. We need to do explicit checks
  2342. // to ensure this, but once we've found a value that is not NaN we need no
  2343. // longer worry about it, so first loop until such a value is found (or not,
  2344. // as the case may be).
  2345. while (double.IsNaN(value))
  2346. {
  2347. if (!await e.MoveNextAsync())
  2348. {
  2349. return value;
  2350. }
  2351. value = await _selector(e.Current).ConfigureAwait(false);
  2352. }
  2353. while (await e.MoveNextAsync())
  2354. {
  2355. var x = await _selector(e.Current).ConfigureAwait(false);
  2356. if (x > value)
  2357. {
  2358. value = x;
  2359. }
  2360. }
  2361. }
  2362. #else
  2363. var e = _source.GetAsyncEnumerator(_cancellationToken);
  2364. try
  2365. {
  2366. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2367. {
  2368. throw Error.NoElements();
  2369. }
  2370. value = await _selector(e.Current).ConfigureAwait(false);
  2371. // NaN is ordered less than all other values. We need to do explicit checks
  2372. // to ensure this, but once we've found a value that is not NaN we need no
  2373. // longer worry about it, so first loop until such a value is found (or not,
  2374. // as the case may be).
  2375. while (double.IsNaN(value))
  2376. {
  2377. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2378. {
  2379. return value;
  2380. }
  2381. value = await _selector(e.Current).ConfigureAwait(false);
  2382. }
  2383. while (await e.MoveNextAsync().ConfigureAwait(false))
  2384. {
  2385. var x = await _selector(e.Current).ConfigureAwait(false);
  2386. if (x > value)
  2387. {
  2388. value = x;
  2389. }
  2390. }
  2391. }
  2392. finally
  2393. {
  2394. await e.DisposeAsync().ConfigureAwait(false);
  2395. }
  2396. #endif
  2397. return value;
  2398. }
  2399. }
  2400. #if !NO_DEEP_CANCELLATION
  2401. public static Task<double> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  2402. {
  2403. if (source == null)
  2404. throw Error.ArgumentNull(nameof(source));
  2405. if (selector == null)
  2406. throw Error.ArgumentNull(nameof(selector));
  2407. return Core(source, selector, cancellationToken);
  2408. static async Task<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<double>> _selector, CancellationToken _cancellationToken)
  2409. {
  2410. double value;
  2411. #if CSHARP8
  2412. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  2413. {
  2414. if (!await e.MoveNextAsync())
  2415. {
  2416. throw Error.NoElements();
  2417. }
  2418. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2419. // NaN is ordered less than all other values. We need to do explicit checks
  2420. // to ensure this, but once we've found a value that is not NaN we need no
  2421. // longer worry about it, so first loop until such a value is found (or not,
  2422. // as the case may be).
  2423. while (double.IsNaN(value))
  2424. {
  2425. if (!await e.MoveNextAsync())
  2426. {
  2427. return value;
  2428. }
  2429. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2430. }
  2431. while (await e.MoveNextAsync())
  2432. {
  2433. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2434. if (x > value)
  2435. {
  2436. value = x;
  2437. }
  2438. }
  2439. }
  2440. #else
  2441. var e = _source.GetAsyncEnumerator(_cancellationToken);
  2442. try
  2443. {
  2444. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2445. {
  2446. throw Error.NoElements();
  2447. }
  2448. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2449. // NaN is ordered less than all other values. We need to do explicit checks
  2450. // to ensure this, but once we've found a value that is not NaN we need no
  2451. // longer worry about it, so first loop until such a value is found (or not,
  2452. // as the case may be).
  2453. while (double.IsNaN(value))
  2454. {
  2455. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2456. {
  2457. return value;
  2458. }
  2459. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2460. }
  2461. while (await e.MoveNextAsync().ConfigureAwait(false))
  2462. {
  2463. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2464. if (x > value)
  2465. {
  2466. value = x;
  2467. }
  2468. }
  2469. }
  2470. finally
  2471. {
  2472. await e.DisposeAsync().ConfigureAwait(false);
  2473. }
  2474. #endif
  2475. return value;
  2476. }
  2477. }
  2478. #endif
  2479. public static Task<double?> MaxAsync(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken = default)
  2480. {
  2481. if (source == null)
  2482. throw Error.ArgumentNull(nameof(source));
  2483. return Core(source, cancellationToken);
  2484. static async Task<double?> Core(IAsyncEnumerable<double?> _source, CancellationToken _cancellationToken)
  2485. {
  2486. double? value = null;
  2487. #if CSHARP8
  2488. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  2489. {
  2490. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2491. // so we don't have to keep testing for nullity.
  2492. do
  2493. {
  2494. if (!await e.MoveNextAsync())
  2495. {
  2496. return value;
  2497. }
  2498. value = e.Current;
  2499. }
  2500. while (!value.HasValue);
  2501. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2502. // using the lifted operation each time.
  2503. var valueVal = value.GetValueOrDefault();
  2504. // NaN is ordered less than all other values. We need to do explicit checks
  2505. // to ensure this, but once we've found a value that is not NaN we need no
  2506. // longer worry about it, so first loop until such a value is found (or not,
  2507. // as the case may be).
  2508. while (double.IsNaN(valueVal))
  2509. {
  2510. if (!await e.MoveNextAsync())
  2511. {
  2512. return value;
  2513. }
  2514. var cur = e.Current;
  2515. if (cur.HasValue)
  2516. {
  2517. valueVal = (value = cur).GetValueOrDefault();
  2518. }
  2519. }
  2520. while (await e.MoveNextAsync())
  2521. {
  2522. var cur = e.Current;
  2523. var x = cur.GetValueOrDefault();
  2524. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2525. // unless nulls either never happen or always happen.
  2526. if (cur.HasValue & x > valueVal)
  2527. {
  2528. valueVal = x;
  2529. value = cur;
  2530. }
  2531. }
  2532. }
  2533. #else
  2534. var e = _source.GetAsyncEnumerator(_cancellationToken);
  2535. try
  2536. {
  2537. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2538. // so we don't have to keep testing for nullity.
  2539. do
  2540. {
  2541. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2542. {
  2543. return value;
  2544. }
  2545. value = e.Current;
  2546. }
  2547. while (!value.HasValue);
  2548. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2549. // using the lifted operation each time.
  2550. var valueVal = value.GetValueOrDefault();
  2551. // NaN is ordered less than all other values. We need to do explicit checks
  2552. // to ensure this, but once we've found a value that is not NaN we need no
  2553. // longer worry about it, so first loop until such a value is found (or not,
  2554. // as the case may be).
  2555. while (double.IsNaN(valueVal))
  2556. {
  2557. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2558. {
  2559. return value;
  2560. }
  2561. var cur = e.Current;
  2562. if (cur.HasValue)
  2563. {
  2564. valueVal = (value = cur).GetValueOrDefault();
  2565. }
  2566. }
  2567. while (await e.MoveNextAsync().ConfigureAwait(false))
  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. finally
  2581. {
  2582. await e.DisposeAsync().ConfigureAwait(false);
  2583. }
  2584. #endif
  2585. return value;
  2586. }
  2587. }
  2588. public static Task<double?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
  2589. {
  2590. if (source == null)
  2591. throw Error.ArgumentNull(nameof(source));
  2592. if (selector == null)
  2593. throw Error.ArgumentNull(nameof(selector));
  2594. return Core(source, selector, cancellationToken);
  2595. static async Task<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, double?> _selector, CancellationToken _cancellationToken)
  2596. {
  2597. double? value = null;
  2598. #if CSHARP8
  2599. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  2600. {
  2601. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2602. // so we don't have to keep testing for nullity.
  2603. do
  2604. {
  2605. if (!await e.MoveNextAsync())
  2606. {
  2607. return value;
  2608. }
  2609. value = _selector(e.Current);
  2610. }
  2611. while (!value.HasValue);
  2612. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2613. // using the lifted operation each time.
  2614. var valueVal = value.GetValueOrDefault();
  2615. // NaN is ordered less than all other values. We need to do explicit checks
  2616. // to ensure this, but once we've found a value that is not NaN we need no
  2617. // longer worry about it, so first loop until such a value is found (or not,
  2618. // as the case may be).
  2619. while (double.IsNaN(valueVal))
  2620. {
  2621. if (!await e.MoveNextAsync())
  2622. {
  2623. return value;
  2624. }
  2625. var cur = _selector(e.Current);
  2626. if (cur.HasValue)
  2627. {
  2628. valueVal = (value = cur).GetValueOrDefault();
  2629. }
  2630. }
  2631. while (await e.MoveNextAsync())
  2632. {
  2633. var cur = _selector(e.Current);
  2634. var x = cur.GetValueOrDefault();
  2635. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2636. // unless nulls either never happen or always happen.
  2637. if (cur.HasValue & x > valueVal)
  2638. {
  2639. valueVal = x;
  2640. value = cur;
  2641. }
  2642. }
  2643. }
  2644. #else
  2645. var e = _source.GetAsyncEnumerator(_cancellationToken);
  2646. try
  2647. {
  2648. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2649. // so we don't have to keep testing for nullity.
  2650. do
  2651. {
  2652. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2653. {
  2654. return value;
  2655. }
  2656. value = _selector(e.Current);
  2657. }
  2658. while (!value.HasValue);
  2659. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2660. // using the lifted operation each time.
  2661. var valueVal = value.GetValueOrDefault();
  2662. // NaN is ordered less than all other values. We need to do explicit checks
  2663. // to ensure this, but once we've found a value that is not NaN we need no
  2664. // longer worry about it, so first loop until such a value is found (or not,
  2665. // as the case may be).
  2666. while (double.IsNaN(valueVal))
  2667. {
  2668. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2669. {
  2670. return value;
  2671. }
  2672. var cur = _selector(e.Current);
  2673. if (cur.HasValue)
  2674. {
  2675. valueVal = (value = cur).GetValueOrDefault();
  2676. }
  2677. }
  2678. while (await e.MoveNextAsync().ConfigureAwait(false))
  2679. {
  2680. var cur = _selector(e.Current);
  2681. var x = cur.GetValueOrDefault();
  2682. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2683. // unless nulls either never happen or always happen.
  2684. if (cur.HasValue & x > valueVal)
  2685. {
  2686. valueVal = x;
  2687. value = cur;
  2688. }
  2689. }
  2690. }
  2691. finally
  2692. {
  2693. await e.DisposeAsync().ConfigureAwait(false);
  2694. }
  2695. #endif
  2696. return value;
  2697. }
  2698. }
  2699. public static Task<double?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  2700. {
  2701. if (source == null)
  2702. throw Error.ArgumentNull(nameof(source));
  2703. if (selector == null)
  2704. throw Error.ArgumentNull(nameof(selector));
  2705. return Core(source, selector, cancellationToken);
  2706. static async Task<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<double?>> _selector, CancellationToken _cancellationToken)
  2707. {
  2708. double? value = null;
  2709. #if CSHARP8
  2710. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  2711. {
  2712. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2713. // so we don't have to keep testing for nullity.
  2714. do
  2715. {
  2716. if (!await e.MoveNextAsync())
  2717. {
  2718. return value;
  2719. }
  2720. value = await _selector(e.Current).ConfigureAwait(false);
  2721. }
  2722. while (!value.HasValue);
  2723. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2724. // using the lifted operation each time.
  2725. var valueVal = value.GetValueOrDefault();
  2726. // NaN is ordered less than all other values. We need to do explicit checks
  2727. // to ensure this, but once we've found a value that is not NaN we need no
  2728. // longer worry about it, so first loop until such a value is found (or not,
  2729. // as the case may be).
  2730. while (double.IsNaN(valueVal))
  2731. {
  2732. if (!await e.MoveNextAsync())
  2733. {
  2734. return value;
  2735. }
  2736. var cur = await _selector(e.Current).ConfigureAwait(false);
  2737. if (cur.HasValue)
  2738. {
  2739. valueVal = (value = cur).GetValueOrDefault();
  2740. }
  2741. }
  2742. while (await e.MoveNextAsync())
  2743. {
  2744. var cur = await _selector(e.Current).ConfigureAwait(false);
  2745. var x = cur.GetValueOrDefault();
  2746. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2747. // unless nulls either never happen or always happen.
  2748. if (cur.HasValue & x > valueVal)
  2749. {
  2750. valueVal = x;
  2751. value = cur;
  2752. }
  2753. }
  2754. }
  2755. #else
  2756. var e = _source.GetAsyncEnumerator(_cancellationToken);
  2757. try
  2758. {
  2759. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2760. // so we don't have to keep testing for nullity.
  2761. do
  2762. {
  2763. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2764. {
  2765. return value;
  2766. }
  2767. value = await _selector(e.Current).ConfigureAwait(false);
  2768. }
  2769. while (!value.HasValue);
  2770. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2771. // using the lifted operation each time.
  2772. var valueVal = value.GetValueOrDefault();
  2773. // NaN is ordered less than all other values. We need to do explicit checks
  2774. // to ensure this, but once we've found a value that is not NaN we need no
  2775. // longer worry about it, so first loop until such a value is found (or not,
  2776. // as the case may be).
  2777. while (double.IsNaN(valueVal))
  2778. {
  2779. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2780. {
  2781. return value;
  2782. }
  2783. var cur = await _selector(e.Current).ConfigureAwait(false);
  2784. if (cur.HasValue)
  2785. {
  2786. valueVal = (value = cur).GetValueOrDefault();
  2787. }
  2788. }
  2789. while (await e.MoveNextAsync().ConfigureAwait(false))
  2790. {
  2791. var cur = await _selector(e.Current).ConfigureAwait(false);
  2792. var x = cur.GetValueOrDefault();
  2793. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2794. // unless nulls either never happen or always happen.
  2795. if (cur.HasValue & x > valueVal)
  2796. {
  2797. valueVal = x;
  2798. value = cur;
  2799. }
  2800. }
  2801. }
  2802. finally
  2803. {
  2804. await e.DisposeAsync().ConfigureAwait(false);
  2805. }
  2806. #endif
  2807. return value;
  2808. }
  2809. }
  2810. #if !NO_DEEP_CANCELLATION
  2811. public static Task<double?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  2812. {
  2813. if (source == null)
  2814. throw Error.ArgumentNull(nameof(source));
  2815. if (selector == null)
  2816. throw Error.ArgumentNull(nameof(selector));
  2817. return Core(source, selector, cancellationToken);
  2818. static async Task<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<double?>> _selector, CancellationToken _cancellationToken)
  2819. {
  2820. double? value = null;
  2821. #if CSHARP8
  2822. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  2823. {
  2824. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2825. // so we don't have to keep testing for nullity.
  2826. do
  2827. {
  2828. if (!await e.MoveNextAsync())
  2829. {
  2830. return value;
  2831. }
  2832. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2833. }
  2834. while (!value.HasValue);
  2835. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2836. // using the lifted operation each time.
  2837. var valueVal = value.GetValueOrDefault();
  2838. // NaN is ordered less than all other values. We need to do explicit checks
  2839. // to ensure this, but once we've found a value that is not NaN we need no
  2840. // longer worry about it, so first loop until such a value is found (or not,
  2841. // as the case may be).
  2842. while (double.IsNaN(valueVal))
  2843. {
  2844. if (!await e.MoveNextAsync())
  2845. {
  2846. return value;
  2847. }
  2848. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2849. if (cur.HasValue)
  2850. {
  2851. valueVal = (value = cur).GetValueOrDefault();
  2852. }
  2853. }
  2854. while (await e.MoveNextAsync())
  2855. {
  2856. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2857. var x = cur.GetValueOrDefault();
  2858. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2859. // unless nulls either never happen or always happen.
  2860. if (cur.HasValue & x > valueVal)
  2861. {
  2862. valueVal = x;
  2863. value = cur;
  2864. }
  2865. }
  2866. }
  2867. #else
  2868. var e = _source.GetAsyncEnumerator(_cancellationToken);
  2869. try
  2870. {
  2871. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  2872. // so we don't have to keep testing for nullity.
  2873. do
  2874. {
  2875. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2876. {
  2877. return value;
  2878. }
  2879. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2880. }
  2881. while (!value.HasValue);
  2882. // Keep hold of the wrapped value, and do comparisons on that, rather than
  2883. // using the lifted operation each time.
  2884. var valueVal = value.GetValueOrDefault();
  2885. // NaN is ordered less than all other values. We need to do explicit checks
  2886. // to ensure this, but once we've found a value that is not NaN we need no
  2887. // longer worry about it, so first loop until such a value is found (or not,
  2888. // as the case may be).
  2889. while (double.IsNaN(valueVal))
  2890. {
  2891. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2892. {
  2893. return value;
  2894. }
  2895. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2896. if (cur.HasValue)
  2897. {
  2898. valueVal = (value = cur).GetValueOrDefault();
  2899. }
  2900. }
  2901. while (await e.MoveNextAsync().ConfigureAwait(false))
  2902. {
  2903. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  2904. var x = cur.GetValueOrDefault();
  2905. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  2906. // unless nulls either never happen or always happen.
  2907. if (cur.HasValue & x > valueVal)
  2908. {
  2909. valueVal = x;
  2910. value = cur;
  2911. }
  2912. }
  2913. }
  2914. finally
  2915. {
  2916. await e.DisposeAsync().ConfigureAwait(false);
  2917. }
  2918. #endif
  2919. return value;
  2920. }
  2921. }
  2922. #endif
  2923. public static Task<decimal> MaxAsync(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken = default)
  2924. {
  2925. if (source == null)
  2926. throw Error.ArgumentNull(nameof(source));
  2927. return Core(source, cancellationToken);
  2928. static async Task<decimal> Core(IAsyncEnumerable<decimal> _source, CancellationToken _cancellationToken)
  2929. {
  2930. decimal value;
  2931. #if CSHARP8
  2932. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  2933. {
  2934. if (!await e.MoveNextAsync())
  2935. {
  2936. throw Error.NoElements();
  2937. }
  2938. value = e.Current;
  2939. while (await e.MoveNextAsync())
  2940. {
  2941. var x = e.Current;
  2942. if (x > value)
  2943. {
  2944. value = x;
  2945. }
  2946. }
  2947. }
  2948. #else
  2949. var e = _source.GetAsyncEnumerator(_cancellationToken);
  2950. try
  2951. {
  2952. if (!await e.MoveNextAsync().ConfigureAwait(false))
  2953. {
  2954. throw Error.NoElements();
  2955. }
  2956. value = e.Current;
  2957. while (await e.MoveNextAsync().ConfigureAwait(false))
  2958. {
  2959. var x = e.Current;
  2960. if (x > value)
  2961. {
  2962. value = x;
  2963. }
  2964. }
  2965. }
  2966. finally
  2967. {
  2968. await e.DisposeAsync().ConfigureAwait(false);
  2969. }
  2970. #endif
  2971. return value;
  2972. }
  2973. }
  2974. public static Task<decimal> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken = default)
  2975. {
  2976. if (source == null)
  2977. throw Error.ArgumentNull(nameof(source));
  2978. if (selector == null)
  2979. throw Error.ArgumentNull(nameof(selector));
  2980. return Core(source, selector, cancellationToken);
  2981. static async Task<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, decimal> _selector, CancellationToken _cancellationToken)
  2982. {
  2983. decimal value;
  2984. #if CSHARP8
  2985. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  2986. {
  2987. if (!await e.MoveNextAsync())
  2988. {
  2989. throw Error.NoElements();
  2990. }
  2991. value = _selector(e.Current);
  2992. while (await e.MoveNextAsync())
  2993. {
  2994. var x = _selector(e.Current);
  2995. if (x > value)
  2996. {
  2997. value = x;
  2998. }
  2999. }
  3000. }
  3001. #else
  3002. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3003. try
  3004. {
  3005. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3006. {
  3007. throw Error.NoElements();
  3008. }
  3009. value = _selector(e.Current);
  3010. while (await e.MoveNextAsync().ConfigureAwait(false))
  3011. {
  3012. var x = _selector(e.Current);
  3013. if (x > value)
  3014. {
  3015. value = x;
  3016. }
  3017. }
  3018. }
  3019. finally
  3020. {
  3021. await e.DisposeAsync().ConfigureAwait(false);
  3022. }
  3023. #endif
  3024. return value;
  3025. }
  3026. }
  3027. public static Task<decimal> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  3028. {
  3029. if (source == null)
  3030. throw Error.ArgumentNull(nameof(source));
  3031. if (selector == null)
  3032. throw Error.ArgumentNull(nameof(selector));
  3033. return Core(source, selector, cancellationToken);
  3034. static async Task<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<decimal>> _selector, CancellationToken _cancellationToken)
  3035. {
  3036. decimal value;
  3037. #if CSHARP8
  3038. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3039. {
  3040. if (!await e.MoveNextAsync())
  3041. {
  3042. throw Error.NoElements();
  3043. }
  3044. value = await _selector(e.Current).ConfigureAwait(false);
  3045. while (await e.MoveNextAsync())
  3046. {
  3047. var x = await _selector(e.Current).ConfigureAwait(false);
  3048. if (x > value)
  3049. {
  3050. value = x;
  3051. }
  3052. }
  3053. }
  3054. #else
  3055. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3056. try
  3057. {
  3058. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3059. {
  3060. throw Error.NoElements();
  3061. }
  3062. value = await _selector(e.Current).ConfigureAwait(false);
  3063. while (await e.MoveNextAsync().ConfigureAwait(false))
  3064. {
  3065. var x = await _selector(e.Current).ConfigureAwait(false);
  3066. if (x > value)
  3067. {
  3068. value = x;
  3069. }
  3070. }
  3071. }
  3072. finally
  3073. {
  3074. await e.DisposeAsync().ConfigureAwait(false);
  3075. }
  3076. #endif
  3077. return value;
  3078. }
  3079. }
  3080. #if !NO_DEEP_CANCELLATION
  3081. public static Task<decimal> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  3082. {
  3083. if (source == null)
  3084. throw Error.ArgumentNull(nameof(source));
  3085. if (selector == null)
  3086. throw Error.ArgumentNull(nameof(selector));
  3087. return Core(source, selector, cancellationToken);
  3088. static async Task<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<decimal>> _selector, CancellationToken _cancellationToken)
  3089. {
  3090. decimal value;
  3091. #if CSHARP8
  3092. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3093. {
  3094. if (!await e.MoveNextAsync())
  3095. {
  3096. throw Error.NoElements();
  3097. }
  3098. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3099. while (await e.MoveNextAsync())
  3100. {
  3101. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3102. if (x > value)
  3103. {
  3104. value = x;
  3105. }
  3106. }
  3107. }
  3108. #else
  3109. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3110. try
  3111. {
  3112. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3113. {
  3114. throw Error.NoElements();
  3115. }
  3116. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3117. while (await e.MoveNextAsync().ConfigureAwait(false))
  3118. {
  3119. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3120. if (x > value)
  3121. {
  3122. value = x;
  3123. }
  3124. }
  3125. }
  3126. finally
  3127. {
  3128. await e.DisposeAsync().ConfigureAwait(false);
  3129. }
  3130. #endif
  3131. return value;
  3132. }
  3133. }
  3134. #endif
  3135. public static Task<decimal?> MaxAsync(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken = default)
  3136. {
  3137. if (source == null)
  3138. throw Error.ArgumentNull(nameof(source));
  3139. return Core(source, cancellationToken);
  3140. static async Task<decimal?> Core(IAsyncEnumerable<decimal?> _source, CancellationToken _cancellationToken)
  3141. {
  3142. decimal? value = null;
  3143. #if CSHARP8
  3144. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3145. {
  3146. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3147. // so we don't have to keep testing for nullity.
  3148. do
  3149. {
  3150. if (!await e.MoveNextAsync())
  3151. {
  3152. return value;
  3153. }
  3154. value = e.Current;
  3155. }
  3156. while (!value.HasValue);
  3157. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3158. // using the lifted operation each time.
  3159. var valueVal = value.GetValueOrDefault();
  3160. while (await e.MoveNextAsync())
  3161. {
  3162. var cur = e.Current;
  3163. var x = cur.GetValueOrDefault();
  3164. if (cur.HasValue && x > valueVal)
  3165. {
  3166. valueVal = x;
  3167. value = cur;
  3168. }
  3169. }
  3170. }
  3171. #else
  3172. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3173. try
  3174. {
  3175. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3176. // so we don't have to keep testing for nullity.
  3177. do
  3178. {
  3179. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3180. {
  3181. return value;
  3182. }
  3183. value = e.Current;
  3184. }
  3185. while (!value.HasValue);
  3186. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3187. // using the lifted operation each time.
  3188. var valueVal = value.GetValueOrDefault();
  3189. while (await e.MoveNextAsync().ConfigureAwait(false))
  3190. {
  3191. var cur = e.Current;
  3192. var x = cur.GetValueOrDefault();
  3193. if (cur.HasValue && x > valueVal)
  3194. {
  3195. valueVal = x;
  3196. value = cur;
  3197. }
  3198. }
  3199. }
  3200. finally
  3201. {
  3202. await e.DisposeAsync().ConfigureAwait(false);
  3203. }
  3204. #endif
  3205. return value;
  3206. }
  3207. }
  3208. public static Task<decimal?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken = default)
  3209. {
  3210. if (source == null)
  3211. throw Error.ArgumentNull(nameof(source));
  3212. if (selector == null)
  3213. throw Error.ArgumentNull(nameof(selector));
  3214. return Core(source, selector, cancellationToken);
  3215. static async Task<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, decimal?> _selector, CancellationToken _cancellationToken)
  3216. {
  3217. decimal? value = null;
  3218. #if CSHARP8
  3219. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3220. {
  3221. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3222. // so we don't have to keep testing for nullity.
  3223. do
  3224. {
  3225. if (!await e.MoveNextAsync())
  3226. {
  3227. return value;
  3228. }
  3229. value = _selector(e.Current);
  3230. }
  3231. while (!value.HasValue);
  3232. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3233. // using the lifted operation each time.
  3234. var valueVal = value.GetValueOrDefault();
  3235. while (await e.MoveNextAsync())
  3236. {
  3237. var cur = _selector(e.Current);
  3238. var x = cur.GetValueOrDefault();
  3239. if (cur.HasValue && x > valueVal)
  3240. {
  3241. valueVal = x;
  3242. value = cur;
  3243. }
  3244. }
  3245. }
  3246. #else
  3247. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3248. try
  3249. {
  3250. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3251. // so we don't have to keep testing for nullity.
  3252. do
  3253. {
  3254. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3255. {
  3256. return value;
  3257. }
  3258. value = _selector(e.Current);
  3259. }
  3260. while (!value.HasValue);
  3261. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3262. // using the lifted operation each time.
  3263. var valueVal = value.GetValueOrDefault();
  3264. while (await e.MoveNextAsync().ConfigureAwait(false))
  3265. {
  3266. var cur = _selector(e.Current);
  3267. var x = cur.GetValueOrDefault();
  3268. if (cur.HasValue && x > valueVal)
  3269. {
  3270. valueVal = x;
  3271. value = cur;
  3272. }
  3273. }
  3274. }
  3275. finally
  3276. {
  3277. await e.DisposeAsync().ConfigureAwait(false);
  3278. }
  3279. #endif
  3280. return value;
  3281. }
  3282. }
  3283. public static Task<decimal?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  3284. {
  3285. if (source == null)
  3286. throw Error.ArgumentNull(nameof(source));
  3287. if (selector == null)
  3288. throw Error.ArgumentNull(nameof(selector));
  3289. return Core(source, selector, cancellationToken);
  3290. static async Task<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<decimal?>> _selector, CancellationToken _cancellationToken)
  3291. {
  3292. decimal? value = null;
  3293. #if CSHARP8
  3294. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3295. {
  3296. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3297. // so we don't have to keep testing for nullity.
  3298. do
  3299. {
  3300. if (!await e.MoveNextAsync())
  3301. {
  3302. return value;
  3303. }
  3304. value = await _selector(e.Current).ConfigureAwait(false);
  3305. }
  3306. while (!value.HasValue);
  3307. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3308. // using the lifted operation each time.
  3309. var valueVal = value.GetValueOrDefault();
  3310. while (await e.MoveNextAsync())
  3311. {
  3312. var cur = await _selector(e.Current).ConfigureAwait(false);
  3313. var x = cur.GetValueOrDefault();
  3314. if (cur.HasValue && x > valueVal)
  3315. {
  3316. valueVal = x;
  3317. value = cur;
  3318. }
  3319. }
  3320. }
  3321. #else
  3322. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3323. try
  3324. {
  3325. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3326. // so we don't have to keep testing for nullity.
  3327. do
  3328. {
  3329. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3330. {
  3331. return value;
  3332. }
  3333. value = await _selector(e.Current).ConfigureAwait(false);
  3334. }
  3335. while (!value.HasValue);
  3336. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3337. // using the lifted operation each time.
  3338. var valueVal = value.GetValueOrDefault();
  3339. while (await e.MoveNextAsync().ConfigureAwait(false))
  3340. {
  3341. var cur = await _selector(e.Current).ConfigureAwait(false);
  3342. var x = cur.GetValueOrDefault();
  3343. if (cur.HasValue && x > valueVal)
  3344. {
  3345. valueVal = x;
  3346. value = cur;
  3347. }
  3348. }
  3349. }
  3350. finally
  3351. {
  3352. await e.DisposeAsync().ConfigureAwait(false);
  3353. }
  3354. #endif
  3355. return value;
  3356. }
  3357. }
  3358. #if !NO_DEEP_CANCELLATION
  3359. public static Task<decimal?> MaxAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  3360. {
  3361. if (source == null)
  3362. throw Error.ArgumentNull(nameof(source));
  3363. if (selector == null)
  3364. throw Error.ArgumentNull(nameof(selector));
  3365. return Core(source, selector, cancellationToken);
  3366. static async Task<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<decimal?>> _selector, CancellationToken _cancellationToken)
  3367. {
  3368. decimal? value = null;
  3369. #if CSHARP8
  3370. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3371. {
  3372. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3373. // so we don't have to keep testing for nullity.
  3374. do
  3375. {
  3376. if (!await e.MoveNextAsync())
  3377. {
  3378. return value;
  3379. }
  3380. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3381. }
  3382. while (!value.HasValue);
  3383. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3384. // using the lifted operation each time.
  3385. var valueVal = value.GetValueOrDefault();
  3386. while (await e.MoveNextAsync())
  3387. {
  3388. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3389. var x = cur.GetValueOrDefault();
  3390. if (cur.HasValue && x > valueVal)
  3391. {
  3392. valueVal = x;
  3393. value = cur;
  3394. }
  3395. }
  3396. }
  3397. #else
  3398. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3399. try
  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().ConfigureAwait(false))
  3406. {
  3407. return value;
  3408. }
  3409. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  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().ConfigureAwait(false))
  3416. {
  3417. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3418. var x = cur.GetValueOrDefault();
  3419. if (cur.HasValue && x > valueVal)
  3420. {
  3421. valueVal = x;
  3422. value = cur;
  3423. }
  3424. }
  3425. }
  3426. finally
  3427. {
  3428. await e.DisposeAsync().ConfigureAwait(false);
  3429. }
  3430. #endif
  3431. return value;
  3432. }
  3433. }
  3434. #endif
  3435. public static Task<int> MinAsync(this IAsyncEnumerable<int> source, CancellationToken cancellationToken = default)
  3436. {
  3437. if (source == null)
  3438. throw Error.ArgumentNull(nameof(source));
  3439. return Core(source, cancellationToken);
  3440. static async Task<int> Core(IAsyncEnumerable<int> _source, CancellationToken _cancellationToken)
  3441. {
  3442. int value;
  3443. #if CSHARP8
  3444. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3445. {
  3446. if (!await e.MoveNextAsync())
  3447. {
  3448. throw Error.NoElements();
  3449. }
  3450. value = e.Current;
  3451. while (await e.MoveNextAsync())
  3452. {
  3453. var x = e.Current;
  3454. if (x < value)
  3455. {
  3456. value = x;
  3457. }
  3458. }
  3459. }
  3460. #else
  3461. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3462. try
  3463. {
  3464. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3465. {
  3466. throw Error.NoElements();
  3467. }
  3468. value = e.Current;
  3469. while (await e.MoveNextAsync().ConfigureAwait(false))
  3470. {
  3471. var x = e.Current;
  3472. if (x < value)
  3473. {
  3474. value = x;
  3475. }
  3476. }
  3477. }
  3478. finally
  3479. {
  3480. await e.DisposeAsync().ConfigureAwait(false);
  3481. }
  3482. #endif
  3483. return value;
  3484. }
  3485. }
  3486. public static Task<int> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken = default)
  3487. {
  3488. if (source == null)
  3489. throw Error.ArgumentNull(nameof(source));
  3490. if (selector == null)
  3491. throw Error.ArgumentNull(nameof(selector));
  3492. return Core(source, selector, cancellationToken);
  3493. static async Task<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, int> _selector, CancellationToken _cancellationToken)
  3494. {
  3495. int value;
  3496. #if CSHARP8
  3497. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3498. {
  3499. if (!await e.MoveNextAsync())
  3500. {
  3501. throw Error.NoElements();
  3502. }
  3503. value = _selector(e.Current);
  3504. while (await e.MoveNextAsync())
  3505. {
  3506. var x = _selector(e.Current);
  3507. if (x < value)
  3508. {
  3509. value = x;
  3510. }
  3511. }
  3512. }
  3513. #else
  3514. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3515. try
  3516. {
  3517. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3518. {
  3519. throw Error.NoElements();
  3520. }
  3521. value = _selector(e.Current);
  3522. while (await e.MoveNextAsync().ConfigureAwait(false))
  3523. {
  3524. var x = _selector(e.Current);
  3525. if (x < value)
  3526. {
  3527. value = x;
  3528. }
  3529. }
  3530. }
  3531. finally
  3532. {
  3533. await e.DisposeAsync().ConfigureAwait(false);
  3534. }
  3535. #endif
  3536. return value;
  3537. }
  3538. }
  3539. public static Task<int> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  3540. {
  3541. if (source == null)
  3542. throw Error.ArgumentNull(nameof(source));
  3543. if (selector == null)
  3544. throw Error.ArgumentNull(nameof(selector));
  3545. return Core(source, selector, cancellationToken);
  3546. static async Task<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<int>> _selector, CancellationToken _cancellationToken)
  3547. {
  3548. int value;
  3549. #if CSHARP8
  3550. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3551. {
  3552. if (!await e.MoveNextAsync())
  3553. {
  3554. throw Error.NoElements();
  3555. }
  3556. value = await _selector(e.Current).ConfigureAwait(false);
  3557. while (await e.MoveNextAsync())
  3558. {
  3559. var x = await _selector(e.Current).ConfigureAwait(false);
  3560. if (x < value)
  3561. {
  3562. value = x;
  3563. }
  3564. }
  3565. }
  3566. #else
  3567. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3568. try
  3569. {
  3570. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3571. {
  3572. throw Error.NoElements();
  3573. }
  3574. value = await _selector(e.Current).ConfigureAwait(false);
  3575. while (await e.MoveNextAsync().ConfigureAwait(false))
  3576. {
  3577. var x = await _selector(e.Current).ConfigureAwait(false);
  3578. if (x < value)
  3579. {
  3580. value = x;
  3581. }
  3582. }
  3583. }
  3584. finally
  3585. {
  3586. await e.DisposeAsync().ConfigureAwait(false);
  3587. }
  3588. #endif
  3589. return value;
  3590. }
  3591. }
  3592. #if !NO_DEEP_CANCELLATION
  3593. public static Task<int> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  3594. {
  3595. if (source == null)
  3596. throw Error.ArgumentNull(nameof(source));
  3597. if (selector == null)
  3598. throw Error.ArgumentNull(nameof(selector));
  3599. return Core(source, selector, cancellationToken);
  3600. static async Task<int> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<int>> _selector, CancellationToken _cancellationToken)
  3601. {
  3602. int value;
  3603. #if CSHARP8
  3604. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3605. {
  3606. if (!await e.MoveNextAsync())
  3607. {
  3608. throw Error.NoElements();
  3609. }
  3610. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3611. while (await e.MoveNextAsync())
  3612. {
  3613. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3614. if (x < value)
  3615. {
  3616. value = x;
  3617. }
  3618. }
  3619. }
  3620. #else
  3621. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3622. try
  3623. {
  3624. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3625. {
  3626. throw Error.NoElements();
  3627. }
  3628. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3629. while (await e.MoveNextAsync().ConfigureAwait(false))
  3630. {
  3631. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3632. if (x < value)
  3633. {
  3634. value = x;
  3635. }
  3636. }
  3637. }
  3638. finally
  3639. {
  3640. await e.DisposeAsync().ConfigureAwait(false);
  3641. }
  3642. #endif
  3643. return value;
  3644. }
  3645. }
  3646. #endif
  3647. public static Task<int?> MinAsync(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken = default)
  3648. {
  3649. if (source == null)
  3650. throw Error.ArgumentNull(nameof(source));
  3651. return Core(source, cancellationToken);
  3652. static async Task<int?> Core(IAsyncEnumerable<int?> _source, CancellationToken _cancellationToken)
  3653. {
  3654. int? value = null;
  3655. #if CSHARP8
  3656. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3657. {
  3658. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3659. // so we don't have to keep testing for nullity.
  3660. do
  3661. {
  3662. if (!await e.MoveNextAsync())
  3663. {
  3664. return value;
  3665. }
  3666. value = e.Current;
  3667. }
  3668. while (!value.HasValue);
  3669. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3670. // using the lifted operation each time.
  3671. var valueVal = value.GetValueOrDefault();
  3672. while (await e.MoveNextAsync())
  3673. {
  3674. var cur = e.Current;
  3675. var x = cur.GetValueOrDefault();
  3676. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  3677. // unless nulls either never happen or always happen.
  3678. if (cur.HasValue & x < valueVal)
  3679. {
  3680. valueVal = x;
  3681. value = cur;
  3682. }
  3683. }
  3684. }
  3685. #else
  3686. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3687. try
  3688. {
  3689. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3690. // so we don't have to keep testing for nullity.
  3691. do
  3692. {
  3693. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3694. {
  3695. return value;
  3696. }
  3697. value = e.Current;
  3698. }
  3699. while (!value.HasValue);
  3700. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3701. // using the lifted operation each time.
  3702. var valueVal = value.GetValueOrDefault();
  3703. while (await e.MoveNextAsync().ConfigureAwait(false))
  3704. {
  3705. var cur = e.Current;
  3706. var x = cur.GetValueOrDefault();
  3707. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  3708. // unless nulls either never happen or always happen.
  3709. if (cur.HasValue & x < valueVal)
  3710. {
  3711. valueVal = x;
  3712. value = cur;
  3713. }
  3714. }
  3715. }
  3716. finally
  3717. {
  3718. await e.DisposeAsync().ConfigureAwait(false);
  3719. }
  3720. #endif
  3721. return value;
  3722. }
  3723. }
  3724. public static Task<int?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken = default)
  3725. {
  3726. if (source == null)
  3727. throw Error.ArgumentNull(nameof(source));
  3728. if (selector == null)
  3729. throw Error.ArgumentNull(nameof(selector));
  3730. return Core(source, selector, cancellationToken);
  3731. static async Task<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, int?> _selector, CancellationToken _cancellationToken)
  3732. {
  3733. int? value = null;
  3734. #if CSHARP8
  3735. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3736. {
  3737. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3738. // so we don't have to keep testing for nullity.
  3739. do
  3740. {
  3741. if (!await e.MoveNextAsync())
  3742. {
  3743. return value;
  3744. }
  3745. value = _selector(e.Current);
  3746. }
  3747. while (!value.HasValue);
  3748. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3749. // using the lifted operation each time.
  3750. var valueVal = value.GetValueOrDefault();
  3751. while (await e.MoveNextAsync())
  3752. {
  3753. var cur = _selector(e.Current);
  3754. var x = cur.GetValueOrDefault();
  3755. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  3756. // unless nulls either never happen or always happen.
  3757. if (cur.HasValue & x < valueVal)
  3758. {
  3759. valueVal = x;
  3760. value = cur;
  3761. }
  3762. }
  3763. }
  3764. #else
  3765. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3766. try
  3767. {
  3768. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3769. // so we don't have to keep testing for nullity.
  3770. do
  3771. {
  3772. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3773. {
  3774. return value;
  3775. }
  3776. value = _selector(e.Current);
  3777. }
  3778. while (!value.HasValue);
  3779. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3780. // using the lifted operation each time.
  3781. var valueVal = value.GetValueOrDefault();
  3782. while (await e.MoveNextAsync().ConfigureAwait(false))
  3783. {
  3784. var cur = _selector(e.Current);
  3785. var x = cur.GetValueOrDefault();
  3786. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  3787. // unless nulls either never happen or always happen.
  3788. if (cur.HasValue & x < valueVal)
  3789. {
  3790. valueVal = x;
  3791. value = cur;
  3792. }
  3793. }
  3794. }
  3795. finally
  3796. {
  3797. await e.DisposeAsync().ConfigureAwait(false);
  3798. }
  3799. #endif
  3800. return value;
  3801. }
  3802. }
  3803. public static Task<int?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  3804. {
  3805. if (source == null)
  3806. throw Error.ArgumentNull(nameof(source));
  3807. if (selector == null)
  3808. throw Error.ArgumentNull(nameof(selector));
  3809. return Core(source, selector, cancellationToken);
  3810. static async Task<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<int?>> _selector, CancellationToken _cancellationToken)
  3811. {
  3812. int? value = null;
  3813. #if CSHARP8
  3814. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3815. {
  3816. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3817. // so we don't have to keep testing for nullity.
  3818. do
  3819. {
  3820. if (!await e.MoveNextAsync())
  3821. {
  3822. return value;
  3823. }
  3824. value = await _selector(e.Current).ConfigureAwait(false);
  3825. }
  3826. while (!value.HasValue);
  3827. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3828. // using the lifted operation each time.
  3829. var valueVal = value.GetValueOrDefault();
  3830. while (await e.MoveNextAsync())
  3831. {
  3832. var cur = await _selector(e.Current).ConfigureAwait(false);
  3833. var x = cur.GetValueOrDefault();
  3834. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  3835. // unless nulls either never happen or always happen.
  3836. if (cur.HasValue & x < valueVal)
  3837. {
  3838. valueVal = x;
  3839. value = cur;
  3840. }
  3841. }
  3842. }
  3843. #else
  3844. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3845. try
  3846. {
  3847. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3848. // so we don't have to keep testing for nullity.
  3849. do
  3850. {
  3851. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3852. {
  3853. return value;
  3854. }
  3855. value = await _selector(e.Current).ConfigureAwait(false);
  3856. }
  3857. while (!value.HasValue);
  3858. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3859. // using the lifted operation each time.
  3860. var valueVal = value.GetValueOrDefault();
  3861. while (await e.MoveNextAsync().ConfigureAwait(false))
  3862. {
  3863. var cur = await _selector(e.Current).ConfigureAwait(false);
  3864. var x = cur.GetValueOrDefault();
  3865. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  3866. // unless nulls either never happen or always happen.
  3867. if (cur.HasValue & x < valueVal)
  3868. {
  3869. valueVal = x;
  3870. value = cur;
  3871. }
  3872. }
  3873. }
  3874. finally
  3875. {
  3876. await e.DisposeAsync().ConfigureAwait(false);
  3877. }
  3878. #endif
  3879. return value;
  3880. }
  3881. }
  3882. #if !NO_DEEP_CANCELLATION
  3883. public static Task<int?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  3884. {
  3885. if (source == null)
  3886. throw Error.ArgumentNull(nameof(source));
  3887. if (selector == null)
  3888. throw Error.ArgumentNull(nameof(selector));
  3889. return Core(source, selector, cancellationToken);
  3890. static async Task<int?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<int?>> _selector, CancellationToken _cancellationToken)
  3891. {
  3892. int? value = null;
  3893. #if CSHARP8
  3894. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3895. {
  3896. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3897. // so we don't have to keep testing for nullity.
  3898. do
  3899. {
  3900. if (!await e.MoveNextAsync())
  3901. {
  3902. return value;
  3903. }
  3904. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3905. }
  3906. while (!value.HasValue);
  3907. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3908. // using the lifted operation each time.
  3909. var valueVal = value.GetValueOrDefault();
  3910. while (await e.MoveNextAsync())
  3911. {
  3912. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3913. var x = cur.GetValueOrDefault();
  3914. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  3915. // unless nulls either never happen or always happen.
  3916. if (cur.HasValue & x < valueVal)
  3917. {
  3918. valueVal = x;
  3919. value = cur;
  3920. }
  3921. }
  3922. }
  3923. #else
  3924. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3925. try
  3926. {
  3927. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  3928. // so we don't have to keep testing for nullity.
  3929. do
  3930. {
  3931. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3932. {
  3933. return value;
  3934. }
  3935. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3936. }
  3937. while (!value.HasValue);
  3938. // Keep hold of the wrapped value, and do comparisons on that, rather than
  3939. // using the lifted operation each time.
  3940. var valueVal = value.GetValueOrDefault();
  3941. while (await e.MoveNextAsync().ConfigureAwait(false))
  3942. {
  3943. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  3944. var x = cur.GetValueOrDefault();
  3945. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  3946. // unless nulls either never happen or always happen.
  3947. if (cur.HasValue & x < valueVal)
  3948. {
  3949. valueVal = x;
  3950. value = cur;
  3951. }
  3952. }
  3953. }
  3954. finally
  3955. {
  3956. await e.DisposeAsync().ConfigureAwait(false);
  3957. }
  3958. #endif
  3959. return value;
  3960. }
  3961. }
  3962. #endif
  3963. public static Task<long> MinAsync(this IAsyncEnumerable<long> source, CancellationToken cancellationToken = default)
  3964. {
  3965. if (source == null)
  3966. throw Error.ArgumentNull(nameof(source));
  3967. return Core(source, cancellationToken);
  3968. static async Task<long> Core(IAsyncEnumerable<long> _source, CancellationToken _cancellationToken)
  3969. {
  3970. long value;
  3971. #if CSHARP8
  3972. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  3973. {
  3974. if (!await e.MoveNextAsync())
  3975. {
  3976. throw Error.NoElements();
  3977. }
  3978. value = e.Current;
  3979. while (await e.MoveNextAsync())
  3980. {
  3981. var x = e.Current;
  3982. if (x < value)
  3983. {
  3984. value = x;
  3985. }
  3986. }
  3987. }
  3988. #else
  3989. var e = _source.GetAsyncEnumerator(_cancellationToken);
  3990. try
  3991. {
  3992. if (!await e.MoveNextAsync().ConfigureAwait(false))
  3993. {
  3994. throw Error.NoElements();
  3995. }
  3996. value = e.Current;
  3997. while (await e.MoveNextAsync().ConfigureAwait(false))
  3998. {
  3999. var x = e.Current;
  4000. if (x < value)
  4001. {
  4002. value = x;
  4003. }
  4004. }
  4005. }
  4006. finally
  4007. {
  4008. await e.DisposeAsync().ConfigureAwait(false);
  4009. }
  4010. #endif
  4011. return value;
  4012. }
  4013. }
  4014. public static Task<long> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken = default)
  4015. {
  4016. if (source == null)
  4017. throw Error.ArgumentNull(nameof(source));
  4018. if (selector == null)
  4019. throw Error.ArgumentNull(nameof(selector));
  4020. return Core(source, selector, cancellationToken);
  4021. static async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, long> _selector, CancellationToken _cancellationToken)
  4022. {
  4023. long value;
  4024. #if CSHARP8
  4025. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4026. {
  4027. if (!await e.MoveNextAsync())
  4028. {
  4029. throw Error.NoElements();
  4030. }
  4031. value = _selector(e.Current);
  4032. while (await e.MoveNextAsync())
  4033. {
  4034. var x = _selector(e.Current);
  4035. if (x < value)
  4036. {
  4037. value = x;
  4038. }
  4039. }
  4040. }
  4041. #else
  4042. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4043. try
  4044. {
  4045. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4046. {
  4047. throw Error.NoElements();
  4048. }
  4049. value = _selector(e.Current);
  4050. while (await e.MoveNextAsync().ConfigureAwait(false))
  4051. {
  4052. var x = _selector(e.Current);
  4053. if (x < value)
  4054. {
  4055. value = x;
  4056. }
  4057. }
  4058. }
  4059. finally
  4060. {
  4061. await e.DisposeAsync().ConfigureAwait(false);
  4062. }
  4063. #endif
  4064. return value;
  4065. }
  4066. }
  4067. public static Task<long> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  4068. {
  4069. if (source == null)
  4070. throw Error.ArgumentNull(nameof(source));
  4071. if (selector == null)
  4072. throw Error.ArgumentNull(nameof(selector));
  4073. return Core(source, selector, cancellationToken);
  4074. static async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<long>> _selector, CancellationToken _cancellationToken)
  4075. {
  4076. long value;
  4077. #if CSHARP8
  4078. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4079. {
  4080. if (!await e.MoveNextAsync())
  4081. {
  4082. throw Error.NoElements();
  4083. }
  4084. value = await _selector(e.Current).ConfigureAwait(false);
  4085. while (await e.MoveNextAsync())
  4086. {
  4087. var x = await _selector(e.Current).ConfigureAwait(false);
  4088. if (x < value)
  4089. {
  4090. value = x;
  4091. }
  4092. }
  4093. }
  4094. #else
  4095. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4096. try
  4097. {
  4098. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4099. {
  4100. throw Error.NoElements();
  4101. }
  4102. value = await _selector(e.Current).ConfigureAwait(false);
  4103. while (await e.MoveNextAsync().ConfigureAwait(false))
  4104. {
  4105. var x = await _selector(e.Current).ConfigureAwait(false);
  4106. if (x < value)
  4107. {
  4108. value = x;
  4109. }
  4110. }
  4111. }
  4112. finally
  4113. {
  4114. await e.DisposeAsync().ConfigureAwait(false);
  4115. }
  4116. #endif
  4117. return value;
  4118. }
  4119. }
  4120. #if !NO_DEEP_CANCELLATION
  4121. public static Task<long> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  4122. {
  4123. if (source == null)
  4124. throw Error.ArgumentNull(nameof(source));
  4125. if (selector == null)
  4126. throw Error.ArgumentNull(nameof(selector));
  4127. return Core(source, selector, cancellationToken);
  4128. static async Task<long> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<long>> _selector, CancellationToken _cancellationToken)
  4129. {
  4130. long value;
  4131. #if CSHARP8
  4132. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4133. {
  4134. if (!await e.MoveNextAsync())
  4135. {
  4136. throw Error.NoElements();
  4137. }
  4138. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4139. while (await e.MoveNextAsync())
  4140. {
  4141. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4142. if (x < value)
  4143. {
  4144. value = x;
  4145. }
  4146. }
  4147. }
  4148. #else
  4149. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4150. try
  4151. {
  4152. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4153. {
  4154. throw Error.NoElements();
  4155. }
  4156. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4157. while (await e.MoveNextAsync().ConfigureAwait(false))
  4158. {
  4159. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4160. if (x < value)
  4161. {
  4162. value = x;
  4163. }
  4164. }
  4165. }
  4166. finally
  4167. {
  4168. await e.DisposeAsync().ConfigureAwait(false);
  4169. }
  4170. #endif
  4171. return value;
  4172. }
  4173. }
  4174. #endif
  4175. public static Task<long?> MinAsync(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken = default)
  4176. {
  4177. if (source == null)
  4178. throw Error.ArgumentNull(nameof(source));
  4179. return Core(source, cancellationToken);
  4180. static async Task<long?> Core(IAsyncEnumerable<long?> _source, CancellationToken _cancellationToken)
  4181. {
  4182. long? value = null;
  4183. #if CSHARP8
  4184. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4185. {
  4186. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4187. // so we don't have to keep testing for nullity.
  4188. do
  4189. {
  4190. if (!await e.MoveNextAsync())
  4191. {
  4192. return value;
  4193. }
  4194. value = e.Current;
  4195. }
  4196. while (!value.HasValue);
  4197. // Keep hold of the wrapped value, and do comparisons on that, rather than
  4198. // using the lifted operation each time.
  4199. var valueVal = value.GetValueOrDefault();
  4200. while (await e.MoveNextAsync())
  4201. {
  4202. var cur = e.Current;
  4203. var x = cur.GetValueOrDefault();
  4204. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  4205. // unless nulls either never happen or always happen.
  4206. if (cur.HasValue & x < valueVal)
  4207. {
  4208. valueVal = x;
  4209. value = cur;
  4210. }
  4211. }
  4212. }
  4213. #else
  4214. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4215. try
  4216. {
  4217. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4218. // so we don't have to keep testing for nullity.
  4219. do
  4220. {
  4221. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4222. {
  4223. return value;
  4224. }
  4225. value = e.Current;
  4226. }
  4227. while (!value.HasValue);
  4228. // Keep hold of the wrapped value, and do comparisons on that, rather than
  4229. // using the lifted operation each time.
  4230. var valueVal = value.GetValueOrDefault();
  4231. while (await e.MoveNextAsync().ConfigureAwait(false))
  4232. {
  4233. var cur = e.Current;
  4234. var x = cur.GetValueOrDefault();
  4235. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  4236. // unless nulls either never happen or always happen.
  4237. if (cur.HasValue & x < valueVal)
  4238. {
  4239. valueVal = x;
  4240. value = cur;
  4241. }
  4242. }
  4243. }
  4244. finally
  4245. {
  4246. await e.DisposeAsync().ConfigureAwait(false);
  4247. }
  4248. #endif
  4249. return value;
  4250. }
  4251. }
  4252. public static Task<long?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken = default)
  4253. {
  4254. if (source == null)
  4255. throw Error.ArgumentNull(nameof(source));
  4256. if (selector == null)
  4257. throw Error.ArgumentNull(nameof(selector));
  4258. return Core(source, selector, cancellationToken);
  4259. static async Task<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, long?> _selector, CancellationToken _cancellationToken)
  4260. {
  4261. long? value = null;
  4262. #if CSHARP8
  4263. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4264. {
  4265. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4266. // so we don't have to keep testing for nullity.
  4267. do
  4268. {
  4269. if (!await e.MoveNextAsync())
  4270. {
  4271. return value;
  4272. }
  4273. value = _selector(e.Current);
  4274. }
  4275. while (!value.HasValue);
  4276. // Keep hold of the wrapped value, and do comparisons on that, rather than
  4277. // using the lifted operation each time.
  4278. var valueVal = value.GetValueOrDefault();
  4279. while (await e.MoveNextAsync())
  4280. {
  4281. var cur = _selector(e.Current);
  4282. var x = cur.GetValueOrDefault();
  4283. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  4284. // unless nulls either never happen or always happen.
  4285. if (cur.HasValue & x < valueVal)
  4286. {
  4287. valueVal = x;
  4288. value = cur;
  4289. }
  4290. }
  4291. }
  4292. #else
  4293. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4294. try
  4295. {
  4296. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4297. // so we don't have to keep testing for nullity.
  4298. do
  4299. {
  4300. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4301. {
  4302. return value;
  4303. }
  4304. value = _selector(e.Current);
  4305. }
  4306. while (!value.HasValue);
  4307. // Keep hold of the wrapped value, and do comparisons on that, rather than
  4308. // using the lifted operation each time.
  4309. var valueVal = value.GetValueOrDefault();
  4310. while (await e.MoveNextAsync().ConfigureAwait(false))
  4311. {
  4312. var cur = _selector(e.Current);
  4313. var x = cur.GetValueOrDefault();
  4314. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  4315. // unless nulls either never happen or always happen.
  4316. if (cur.HasValue & x < valueVal)
  4317. {
  4318. valueVal = x;
  4319. value = cur;
  4320. }
  4321. }
  4322. }
  4323. finally
  4324. {
  4325. await e.DisposeAsync().ConfigureAwait(false);
  4326. }
  4327. #endif
  4328. return value;
  4329. }
  4330. }
  4331. public static Task<long?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  4332. {
  4333. if (source == null)
  4334. throw Error.ArgumentNull(nameof(source));
  4335. if (selector == null)
  4336. throw Error.ArgumentNull(nameof(selector));
  4337. return Core(source, selector, cancellationToken);
  4338. static async Task<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<long?>> _selector, CancellationToken _cancellationToken)
  4339. {
  4340. long? value = null;
  4341. #if CSHARP8
  4342. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4343. {
  4344. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4345. // so we don't have to keep testing for nullity.
  4346. do
  4347. {
  4348. if (!await e.MoveNextAsync())
  4349. {
  4350. return value;
  4351. }
  4352. value = await _selector(e.Current).ConfigureAwait(false);
  4353. }
  4354. while (!value.HasValue);
  4355. // Keep hold of the wrapped value, and do comparisons on that, rather than
  4356. // using the lifted operation each time.
  4357. var valueVal = value.GetValueOrDefault();
  4358. while (await e.MoveNextAsync())
  4359. {
  4360. var cur = await _selector(e.Current).ConfigureAwait(false);
  4361. var x = cur.GetValueOrDefault();
  4362. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  4363. // unless nulls either never happen or always happen.
  4364. if (cur.HasValue & x < valueVal)
  4365. {
  4366. valueVal = x;
  4367. value = cur;
  4368. }
  4369. }
  4370. }
  4371. #else
  4372. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4373. try
  4374. {
  4375. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4376. // so we don't have to keep testing for nullity.
  4377. do
  4378. {
  4379. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4380. {
  4381. return value;
  4382. }
  4383. value = await _selector(e.Current).ConfigureAwait(false);
  4384. }
  4385. while (!value.HasValue);
  4386. // Keep hold of the wrapped value, and do comparisons on that, rather than
  4387. // using the lifted operation each time.
  4388. var valueVal = value.GetValueOrDefault();
  4389. while (await e.MoveNextAsync().ConfigureAwait(false))
  4390. {
  4391. var cur = await _selector(e.Current).ConfigureAwait(false);
  4392. var x = cur.GetValueOrDefault();
  4393. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  4394. // unless nulls either never happen or always happen.
  4395. if (cur.HasValue & x < valueVal)
  4396. {
  4397. valueVal = x;
  4398. value = cur;
  4399. }
  4400. }
  4401. }
  4402. finally
  4403. {
  4404. await e.DisposeAsync().ConfigureAwait(false);
  4405. }
  4406. #endif
  4407. return value;
  4408. }
  4409. }
  4410. #if !NO_DEEP_CANCELLATION
  4411. public static Task<long?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  4412. {
  4413. if (source == null)
  4414. throw Error.ArgumentNull(nameof(source));
  4415. if (selector == null)
  4416. throw Error.ArgumentNull(nameof(selector));
  4417. return Core(source, selector, cancellationToken);
  4418. static async Task<long?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<long?>> _selector, CancellationToken _cancellationToken)
  4419. {
  4420. long? value = null;
  4421. #if CSHARP8
  4422. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4423. {
  4424. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4425. // so we don't have to keep testing for nullity.
  4426. do
  4427. {
  4428. if (!await e.MoveNextAsync())
  4429. {
  4430. return value;
  4431. }
  4432. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4433. }
  4434. while (!value.HasValue);
  4435. // Keep hold of the wrapped value, and do comparisons on that, rather than
  4436. // using the lifted operation each time.
  4437. var valueVal = value.GetValueOrDefault();
  4438. while (await e.MoveNextAsync())
  4439. {
  4440. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4441. var x = cur.GetValueOrDefault();
  4442. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  4443. // unless nulls either never happen or always happen.
  4444. if (cur.HasValue & x < valueVal)
  4445. {
  4446. valueVal = x;
  4447. value = cur;
  4448. }
  4449. }
  4450. }
  4451. #else
  4452. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4453. try
  4454. {
  4455. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4456. // so we don't have to keep testing for nullity.
  4457. do
  4458. {
  4459. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4460. {
  4461. return value;
  4462. }
  4463. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4464. }
  4465. while (!value.HasValue);
  4466. // Keep hold of the wrapped value, and do comparisons on that, rather than
  4467. // using the lifted operation each time.
  4468. var valueVal = value.GetValueOrDefault();
  4469. while (await e.MoveNextAsync().ConfigureAwait(false))
  4470. {
  4471. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4472. var x = cur.GetValueOrDefault();
  4473. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  4474. // unless nulls either never happen or always happen.
  4475. if (cur.HasValue & x < valueVal)
  4476. {
  4477. valueVal = x;
  4478. value = cur;
  4479. }
  4480. }
  4481. }
  4482. finally
  4483. {
  4484. await e.DisposeAsync().ConfigureAwait(false);
  4485. }
  4486. #endif
  4487. return value;
  4488. }
  4489. }
  4490. #endif
  4491. public static Task<float> MinAsync(this IAsyncEnumerable<float> source, CancellationToken cancellationToken = default)
  4492. {
  4493. if (source == null)
  4494. throw Error.ArgumentNull(nameof(source));
  4495. return Core(source, cancellationToken);
  4496. static async Task<float> Core(IAsyncEnumerable<float> _source, CancellationToken _cancellationToken)
  4497. {
  4498. float value;
  4499. #if CSHARP8
  4500. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4501. {
  4502. if (!await e.MoveNextAsync())
  4503. {
  4504. throw Error.NoElements();
  4505. }
  4506. value = e.Current;
  4507. while (await e.MoveNextAsync())
  4508. {
  4509. var x = e.Current;
  4510. if (x < value)
  4511. {
  4512. value = x;
  4513. }
  4514. else
  4515. {
  4516. // Normally NaN < anything is false, as is anything < NaN
  4517. // However, this leads to some irksome outcomes in Min and Max.
  4518. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  4519. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  4520. // ordering where NaN is smaller than every value, including
  4521. // negative infinity.
  4522. // Not testing for NaN therefore isn't an option, but since we
  4523. // can't find a smaller value, we can short-circuit.
  4524. if (float.IsNaN(x))
  4525. {
  4526. return x;
  4527. }
  4528. }
  4529. }
  4530. }
  4531. #else
  4532. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4533. try
  4534. {
  4535. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4536. {
  4537. throw Error.NoElements();
  4538. }
  4539. value = e.Current;
  4540. while (await e.MoveNextAsync().ConfigureAwait(false))
  4541. {
  4542. var x = e.Current;
  4543. if (x < value)
  4544. {
  4545. value = x;
  4546. }
  4547. else
  4548. {
  4549. // Normally NaN < anything is false, as is anything < NaN
  4550. // However, this leads to some irksome outcomes in Min and Max.
  4551. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  4552. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  4553. // ordering where NaN is smaller than every value, including
  4554. // negative infinity.
  4555. // Not testing for NaN therefore isn't an option, but since we
  4556. // can't find a smaller value, we can short-circuit.
  4557. if (float.IsNaN(x))
  4558. {
  4559. return x;
  4560. }
  4561. }
  4562. }
  4563. }
  4564. finally
  4565. {
  4566. await e.DisposeAsync().ConfigureAwait(false);
  4567. }
  4568. #endif
  4569. return value;
  4570. }
  4571. }
  4572. public static Task<float> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken = default)
  4573. {
  4574. if (source == null)
  4575. throw Error.ArgumentNull(nameof(source));
  4576. if (selector == null)
  4577. throw Error.ArgumentNull(nameof(selector));
  4578. return Core(source, selector, cancellationToken);
  4579. static async Task<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, float> _selector, CancellationToken _cancellationToken)
  4580. {
  4581. float value;
  4582. #if CSHARP8
  4583. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4584. {
  4585. if (!await e.MoveNextAsync())
  4586. {
  4587. throw Error.NoElements();
  4588. }
  4589. value = _selector(e.Current);
  4590. while (await e.MoveNextAsync())
  4591. {
  4592. var x = _selector(e.Current);
  4593. if (x < value)
  4594. {
  4595. value = x;
  4596. }
  4597. else
  4598. {
  4599. // Normally NaN < anything is false, as is anything < NaN
  4600. // However, this leads to some irksome outcomes in Min and Max.
  4601. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  4602. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  4603. // ordering where NaN is smaller than every value, including
  4604. // negative infinity.
  4605. // Not testing for NaN therefore isn't an option, but since we
  4606. // can't find a smaller value, we can short-circuit.
  4607. if (float.IsNaN(x))
  4608. {
  4609. return x;
  4610. }
  4611. }
  4612. }
  4613. }
  4614. #else
  4615. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4616. try
  4617. {
  4618. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4619. {
  4620. throw Error.NoElements();
  4621. }
  4622. value = _selector(e.Current);
  4623. while (await e.MoveNextAsync().ConfigureAwait(false))
  4624. {
  4625. var x = _selector(e.Current);
  4626. if (x < value)
  4627. {
  4628. value = x;
  4629. }
  4630. else
  4631. {
  4632. // Normally NaN < anything is false, as is anything < NaN
  4633. // However, this leads to some irksome outcomes in Min and Max.
  4634. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  4635. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  4636. // ordering where NaN is smaller than every value, including
  4637. // negative infinity.
  4638. // Not testing for NaN therefore isn't an option, but since we
  4639. // can't find a smaller value, we can short-circuit.
  4640. if (float.IsNaN(x))
  4641. {
  4642. return x;
  4643. }
  4644. }
  4645. }
  4646. }
  4647. finally
  4648. {
  4649. await e.DisposeAsync().ConfigureAwait(false);
  4650. }
  4651. #endif
  4652. return value;
  4653. }
  4654. }
  4655. public static Task<float> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  4656. {
  4657. if (source == null)
  4658. throw Error.ArgumentNull(nameof(source));
  4659. if (selector == null)
  4660. throw Error.ArgumentNull(nameof(selector));
  4661. return Core(source, selector, cancellationToken);
  4662. static async Task<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<float>> _selector, CancellationToken _cancellationToken)
  4663. {
  4664. float value;
  4665. #if CSHARP8
  4666. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4667. {
  4668. if (!await e.MoveNextAsync())
  4669. {
  4670. throw Error.NoElements();
  4671. }
  4672. value = await _selector(e.Current).ConfigureAwait(false);
  4673. while (await e.MoveNextAsync())
  4674. {
  4675. var x = await _selector(e.Current).ConfigureAwait(false);
  4676. if (x < value)
  4677. {
  4678. value = x;
  4679. }
  4680. else
  4681. {
  4682. // Normally NaN < anything is false, as is anything < NaN
  4683. // However, this leads to some irksome outcomes in Min and Max.
  4684. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  4685. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  4686. // ordering where NaN is smaller than every value, including
  4687. // negative infinity.
  4688. // Not testing for NaN therefore isn't an option, but since we
  4689. // can't find a smaller value, we can short-circuit.
  4690. if (float.IsNaN(x))
  4691. {
  4692. return x;
  4693. }
  4694. }
  4695. }
  4696. }
  4697. #else
  4698. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4699. try
  4700. {
  4701. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4702. {
  4703. throw Error.NoElements();
  4704. }
  4705. value = await _selector(e.Current).ConfigureAwait(false);
  4706. while (await e.MoveNextAsync().ConfigureAwait(false))
  4707. {
  4708. var x = await _selector(e.Current).ConfigureAwait(false);
  4709. if (x < value)
  4710. {
  4711. value = x;
  4712. }
  4713. else
  4714. {
  4715. // Normally NaN < anything is false, as is anything < NaN
  4716. // However, this leads to some irksome outcomes in Min and Max.
  4717. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  4718. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  4719. // ordering where NaN is smaller than every value, including
  4720. // negative infinity.
  4721. // Not testing for NaN therefore isn't an option, but since we
  4722. // can't find a smaller value, we can short-circuit.
  4723. if (float.IsNaN(x))
  4724. {
  4725. return x;
  4726. }
  4727. }
  4728. }
  4729. }
  4730. finally
  4731. {
  4732. await e.DisposeAsync().ConfigureAwait(false);
  4733. }
  4734. #endif
  4735. return value;
  4736. }
  4737. }
  4738. #if !NO_DEEP_CANCELLATION
  4739. public static Task<float> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  4740. {
  4741. if (source == null)
  4742. throw Error.ArgumentNull(nameof(source));
  4743. if (selector == null)
  4744. throw Error.ArgumentNull(nameof(selector));
  4745. return Core(source, selector, cancellationToken);
  4746. static async Task<float> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<float>> _selector, CancellationToken _cancellationToken)
  4747. {
  4748. float value;
  4749. #if CSHARP8
  4750. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4751. {
  4752. if (!await e.MoveNextAsync())
  4753. {
  4754. throw Error.NoElements();
  4755. }
  4756. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4757. while (await e.MoveNextAsync())
  4758. {
  4759. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4760. if (x < value)
  4761. {
  4762. value = x;
  4763. }
  4764. else
  4765. {
  4766. // Normally NaN < anything is false, as is anything < NaN
  4767. // However, this leads to some irksome outcomes in Min and Max.
  4768. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  4769. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  4770. // ordering where NaN is smaller than every value, including
  4771. // negative infinity.
  4772. // Not testing for NaN therefore isn't an option, but since we
  4773. // can't find a smaller value, we can short-circuit.
  4774. if (float.IsNaN(x))
  4775. {
  4776. return x;
  4777. }
  4778. }
  4779. }
  4780. }
  4781. #else
  4782. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4783. try
  4784. {
  4785. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4786. {
  4787. throw Error.NoElements();
  4788. }
  4789. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4790. while (await e.MoveNextAsync().ConfigureAwait(false))
  4791. {
  4792. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  4793. if (x < value)
  4794. {
  4795. value = x;
  4796. }
  4797. else
  4798. {
  4799. // Normally NaN < anything is false, as is anything < NaN
  4800. // However, this leads to some irksome outcomes in Min and Max.
  4801. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  4802. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  4803. // ordering where NaN is smaller than every value, including
  4804. // negative infinity.
  4805. // Not testing for NaN therefore isn't an option, but since we
  4806. // can't find a smaller value, we can short-circuit.
  4807. if (float.IsNaN(x))
  4808. {
  4809. return x;
  4810. }
  4811. }
  4812. }
  4813. }
  4814. finally
  4815. {
  4816. await e.DisposeAsync().ConfigureAwait(false);
  4817. }
  4818. #endif
  4819. return value;
  4820. }
  4821. }
  4822. #endif
  4823. public static Task<float?> MinAsync(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken = default)
  4824. {
  4825. if (source == null)
  4826. throw Error.ArgumentNull(nameof(source));
  4827. return Core(source, cancellationToken);
  4828. static async Task<float?> Core(IAsyncEnumerable<float?> _source, CancellationToken _cancellationToken)
  4829. {
  4830. float? value = null;
  4831. #if CSHARP8
  4832. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4833. {
  4834. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4835. // so we don't have to keep testing for nullity.
  4836. do
  4837. {
  4838. if (!await e.MoveNextAsync())
  4839. {
  4840. return value;
  4841. }
  4842. value = e.Current;
  4843. }
  4844. while (!value.HasValue);
  4845. // Keep hold of the wrapped value, and do comparisons on that, rather than
  4846. // using the lifted operation each time.
  4847. var valueVal = value.GetValueOrDefault();
  4848. while (await e.MoveNextAsync())
  4849. {
  4850. var cur = e.Current;
  4851. if (cur.HasValue)
  4852. {
  4853. var x = cur.GetValueOrDefault();
  4854. if (x < valueVal)
  4855. {
  4856. valueVal = x;
  4857. value = cur;
  4858. }
  4859. else
  4860. {
  4861. // Normally NaN < anything is false, as is anything < NaN
  4862. // However, this leads to some irksome outcomes in Min and Max.
  4863. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  4864. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  4865. // ordering where NaN is smaller than every value, including
  4866. // negative infinity.
  4867. // Not testing for NaN therefore isn't an option, but since we
  4868. // can't find a smaller value, we can short-circuit.
  4869. if (float.IsNaN(x))
  4870. {
  4871. return cur;
  4872. }
  4873. }
  4874. }
  4875. }
  4876. }
  4877. #else
  4878. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4879. try
  4880. {
  4881. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4882. // so we don't have to keep testing for nullity.
  4883. do
  4884. {
  4885. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4886. {
  4887. return value;
  4888. }
  4889. value = e.Current;
  4890. }
  4891. while (!value.HasValue);
  4892. // Keep hold of the wrapped value, and do comparisons on that, rather than
  4893. // using the lifted operation each time.
  4894. var valueVal = value.GetValueOrDefault();
  4895. while (await e.MoveNextAsync().ConfigureAwait(false))
  4896. {
  4897. var cur = e.Current;
  4898. if (cur.HasValue)
  4899. {
  4900. var x = cur.GetValueOrDefault();
  4901. if (x < valueVal)
  4902. {
  4903. valueVal = x;
  4904. value = cur;
  4905. }
  4906. else
  4907. {
  4908. // Normally NaN < anything is false, as is anything < NaN
  4909. // However, this leads to some irksome outcomes in Min and Max.
  4910. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  4911. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  4912. // ordering where NaN is smaller than every value, including
  4913. // negative infinity.
  4914. // Not testing for NaN therefore isn't an option, but since we
  4915. // can't find a smaller value, we can short-circuit.
  4916. if (float.IsNaN(x))
  4917. {
  4918. return cur;
  4919. }
  4920. }
  4921. }
  4922. }
  4923. }
  4924. finally
  4925. {
  4926. await e.DisposeAsync().ConfigureAwait(false);
  4927. }
  4928. #endif
  4929. return value;
  4930. }
  4931. }
  4932. public static Task<float?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken = default)
  4933. {
  4934. if (source == null)
  4935. throw Error.ArgumentNull(nameof(source));
  4936. if (selector == null)
  4937. throw Error.ArgumentNull(nameof(selector));
  4938. return Core(source, selector, cancellationToken);
  4939. static async Task<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, float?> _selector, CancellationToken _cancellationToken)
  4940. {
  4941. float? value = null;
  4942. #if CSHARP8
  4943. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  4944. {
  4945. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4946. // so we don't have to keep testing for nullity.
  4947. do
  4948. {
  4949. if (!await e.MoveNextAsync())
  4950. {
  4951. return value;
  4952. }
  4953. value = _selector(e.Current);
  4954. }
  4955. while (!value.HasValue);
  4956. // Keep hold of the wrapped value, and do comparisons on that, rather than
  4957. // using the lifted operation each time.
  4958. var valueVal = value.GetValueOrDefault();
  4959. while (await e.MoveNextAsync())
  4960. {
  4961. var cur = _selector(e.Current);
  4962. if (cur.HasValue)
  4963. {
  4964. var x = cur.GetValueOrDefault();
  4965. if (x < valueVal)
  4966. {
  4967. valueVal = x;
  4968. value = cur;
  4969. }
  4970. else
  4971. {
  4972. // Normally NaN < anything is false, as is anything < NaN
  4973. // However, this leads to some irksome outcomes in Min and Max.
  4974. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  4975. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  4976. // ordering where NaN is smaller than every value, including
  4977. // negative infinity.
  4978. // Not testing for NaN therefore isn't an option, but since we
  4979. // can't find a smaller value, we can short-circuit.
  4980. if (float.IsNaN(x))
  4981. {
  4982. return cur;
  4983. }
  4984. }
  4985. }
  4986. }
  4987. }
  4988. #else
  4989. var e = _source.GetAsyncEnumerator(_cancellationToken);
  4990. try
  4991. {
  4992. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  4993. // so we don't have to keep testing for nullity.
  4994. do
  4995. {
  4996. if (!await e.MoveNextAsync().ConfigureAwait(false))
  4997. {
  4998. return value;
  4999. }
  5000. value = _selector(e.Current);
  5001. }
  5002. while (!value.HasValue);
  5003. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5004. // using the lifted operation each time.
  5005. var valueVal = value.GetValueOrDefault();
  5006. while (await e.MoveNextAsync().ConfigureAwait(false))
  5007. {
  5008. var cur = _selector(e.Current);
  5009. if (cur.HasValue)
  5010. {
  5011. var x = cur.GetValueOrDefault();
  5012. if (x < valueVal)
  5013. {
  5014. valueVal = x;
  5015. value = cur;
  5016. }
  5017. else
  5018. {
  5019. // Normally NaN < anything is false, as is anything < NaN
  5020. // However, this leads to some irksome outcomes in Min and Max.
  5021. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5022. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5023. // ordering where NaN is smaller than every value, including
  5024. // negative infinity.
  5025. // Not testing for NaN therefore isn't an option, but since we
  5026. // can't find a smaller value, we can short-circuit.
  5027. if (float.IsNaN(x))
  5028. {
  5029. return cur;
  5030. }
  5031. }
  5032. }
  5033. }
  5034. }
  5035. finally
  5036. {
  5037. await e.DisposeAsync().ConfigureAwait(false);
  5038. }
  5039. #endif
  5040. return value;
  5041. }
  5042. }
  5043. public static Task<float?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  5044. {
  5045. if (source == null)
  5046. throw Error.ArgumentNull(nameof(source));
  5047. if (selector == null)
  5048. throw Error.ArgumentNull(nameof(selector));
  5049. return Core(source, selector, cancellationToken);
  5050. static async Task<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<float?>> _selector, CancellationToken _cancellationToken)
  5051. {
  5052. float? value = null;
  5053. #if CSHARP8
  5054. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  5055. {
  5056. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5057. // so we don't have to keep testing for nullity.
  5058. do
  5059. {
  5060. if (!await e.MoveNextAsync())
  5061. {
  5062. return value;
  5063. }
  5064. value = await _selector(e.Current).ConfigureAwait(false);
  5065. }
  5066. while (!value.HasValue);
  5067. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5068. // using the lifted operation each time.
  5069. var valueVal = value.GetValueOrDefault();
  5070. while (await e.MoveNextAsync())
  5071. {
  5072. var cur = await _selector(e.Current).ConfigureAwait(false);
  5073. if (cur.HasValue)
  5074. {
  5075. var x = cur.GetValueOrDefault();
  5076. if (x < valueVal)
  5077. {
  5078. valueVal = x;
  5079. value = cur;
  5080. }
  5081. else
  5082. {
  5083. // Normally NaN < anything is false, as is anything < NaN
  5084. // However, this leads to some irksome outcomes in Min and Max.
  5085. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5086. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5087. // ordering where NaN is smaller than every value, including
  5088. // negative infinity.
  5089. // Not testing for NaN therefore isn't an option, but since we
  5090. // can't find a smaller value, we can short-circuit.
  5091. if (float.IsNaN(x))
  5092. {
  5093. return cur;
  5094. }
  5095. }
  5096. }
  5097. }
  5098. }
  5099. #else
  5100. var e = _source.GetAsyncEnumerator(_cancellationToken);
  5101. try
  5102. {
  5103. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5104. // so we don't have to keep testing for nullity.
  5105. do
  5106. {
  5107. if (!await e.MoveNextAsync().ConfigureAwait(false))
  5108. {
  5109. return value;
  5110. }
  5111. value = await _selector(e.Current).ConfigureAwait(false);
  5112. }
  5113. while (!value.HasValue);
  5114. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5115. // using the lifted operation each time.
  5116. var valueVal = value.GetValueOrDefault();
  5117. while (await e.MoveNextAsync().ConfigureAwait(false))
  5118. {
  5119. var cur = await _selector(e.Current).ConfigureAwait(false);
  5120. if (cur.HasValue)
  5121. {
  5122. var x = cur.GetValueOrDefault();
  5123. if (x < valueVal)
  5124. {
  5125. valueVal = x;
  5126. value = cur;
  5127. }
  5128. else
  5129. {
  5130. // Normally NaN < anything is false, as is anything < NaN
  5131. // However, this leads to some irksome outcomes in Min and Max.
  5132. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5133. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5134. // ordering where NaN is smaller than every value, including
  5135. // negative infinity.
  5136. // Not testing for NaN therefore isn't an option, but since we
  5137. // can't find a smaller value, we can short-circuit.
  5138. if (float.IsNaN(x))
  5139. {
  5140. return cur;
  5141. }
  5142. }
  5143. }
  5144. }
  5145. }
  5146. finally
  5147. {
  5148. await e.DisposeAsync().ConfigureAwait(false);
  5149. }
  5150. #endif
  5151. return value;
  5152. }
  5153. }
  5154. #if !NO_DEEP_CANCELLATION
  5155. public static Task<float?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  5156. {
  5157. if (source == null)
  5158. throw Error.ArgumentNull(nameof(source));
  5159. if (selector == null)
  5160. throw Error.ArgumentNull(nameof(selector));
  5161. return Core(source, selector, cancellationToken);
  5162. static async Task<float?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<float?>> _selector, CancellationToken _cancellationToken)
  5163. {
  5164. float? value = null;
  5165. #if CSHARP8
  5166. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  5167. {
  5168. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5169. // so we don't have to keep testing for nullity.
  5170. do
  5171. {
  5172. if (!await e.MoveNextAsync())
  5173. {
  5174. return value;
  5175. }
  5176. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  5177. }
  5178. while (!value.HasValue);
  5179. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5180. // using the lifted operation each time.
  5181. var valueVal = value.GetValueOrDefault();
  5182. while (await e.MoveNextAsync())
  5183. {
  5184. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  5185. if (cur.HasValue)
  5186. {
  5187. var x = cur.GetValueOrDefault();
  5188. if (x < valueVal)
  5189. {
  5190. valueVal = x;
  5191. value = cur;
  5192. }
  5193. else
  5194. {
  5195. // Normally NaN < anything is false, as is anything < NaN
  5196. // However, this leads to some irksome outcomes in Min and Max.
  5197. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5198. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5199. // ordering where NaN is smaller than every value, including
  5200. // negative infinity.
  5201. // Not testing for NaN therefore isn't an option, but since we
  5202. // can't find a smaller value, we can short-circuit.
  5203. if (float.IsNaN(x))
  5204. {
  5205. return cur;
  5206. }
  5207. }
  5208. }
  5209. }
  5210. }
  5211. #else
  5212. var e = _source.GetAsyncEnumerator(_cancellationToken);
  5213. try
  5214. {
  5215. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5216. // so we don't have to keep testing for nullity.
  5217. do
  5218. {
  5219. if (!await e.MoveNextAsync().ConfigureAwait(false))
  5220. {
  5221. return value;
  5222. }
  5223. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  5224. }
  5225. while (!value.HasValue);
  5226. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5227. // using the lifted operation each time.
  5228. var valueVal = value.GetValueOrDefault();
  5229. while (await e.MoveNextAsync().ConfigureAwait(false))
  5230. {
  5231. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  5232. if (cur.HasValue)
  5233. {
  5234. var x = cur.GetValueOrDefault();
  5235. if (x < valueVal)
  5236. {
  5237. valueVal = x;
  5238. value = cur;
  5239. }
  5240. else
  5241. {
  5242. // Normally NaN < anything is false, as is anything < NaN
  5243. // However, this leads to some irksome outcomes in Min and Max.
  5244. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5245. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5246. // ordering where NaN is smaller than every value, including
  5247. // negative infinity.
  5248. // Not testing for NaN therefore isn't an option, but since we
  5249. // can't find a smaller value, we can short-circuit.
  5250. if (float.IsNaN(x))
  5251. {
  5252. return cur;
  5253. }
  5254. }
  5255. }
  5256. }
  5257. }
  5258. finally
  5259. {
  5260. await e.DisposeAsync().ConfigureAwait(false);
  5261. }
  5262. #endif
  5263. return value;
  5264. }
  5265. }
  5266. #endif
  5267. public static Task<double> MinAsync(this IAsyncEnumerable<double> source, CancellationToken cancellationToken = default)
  5268. {
  5269. if (source == null)
  5270. throw Error.ArgumentNull(nameof(source));
  5271. return Core(source, cancellationToken);
  5272. static async Task<double> Core(IAsyncEnumerable<double> _source, CancellationToken _cancellationToken)
  5273. {
  5274. double value;
  5275. #if CSHARP8
  5276. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  5277. {
  5278. if (!await e.MoveNextAsync())
  5279. {
  5280. throw Error.NoElements();
  5281. }
  5282. value = e.Current;
  5283. while (await e.MoveNextAsync())
  5284. {
  5285. var x = e.Current;
  5286. if (x < value)
  5287. {
  5288. value = x;
  5289. }
  5290. else
  5291. {
  5292. // Normally NaN < anything is false, as is anything < NaN
  5293. // However, this leads to some irksome outcomes in Min and Max.
  5294. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5295. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5296. // ordering where NaN is smaller than every value, including
  5297. // negative infinity.
  5298. // Not testing for NaN therefore isn't an option, but since we
  5299. // can't find a smaller value, we can short-circuit.
  5300. if (double.IsNaN(x))
  5301. {
  5302. return x;
  5303. }
  5304. }
  5305. }
  5306. }
  5307. #else
  5308. var e = _source.GetAsyncEnumerator(_cancellationToken);
  5309. try
  5310. {
  5311. if (!await e.MoveNextAsync().ConfigureAwait(false))
  5312. {
  5313. throw Error.NoElements();
  5314. }
  5315. value = e.Current;
  5316. while (await e.MoveNextAsync().ConfigureAwait(false))
  5317. {
  5318. var x = e.Current;
  5319. if (x < value)
  5320. {
  5321. value = x;
  5322. }
  5323. else
  5324. {
  5325. // Normally NaN < anything is false, as is anything < NaN
  5326. // However, this leads to some irksome outcomes in Min and Max.
  5327. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5328. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5329. // ordering where NaN is smaller than every value, including
  5330. // negative infinity.
  5331. // Not testing for NaN therefore isn't an option, but since we
  5332. // can't find a smaller value, we can short-circuit.
  5333. if (double.IsNaN(x))
  5334. {
  5335. return x;
  5336. }
  5337. }
  5338. }
  5339. }
  5340. finally
  5341. {
  5342. await e.DisposeAsync().ConfigureAwait(false);
  5343. }
  5344. #endif
  5345. return value;
  5346. }
  5347. }
  5348. public static Task<double> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken = default)
  5349. {
  5350. if (source == null)
  5351. throw Error.ArgumentNull(nameof(source));
  5352. if (selector == null)
  5353. throw Error.ArgumentNull(nameof(selector));
  5354. return Core(source, selector, cancellationToken);
  5355. static async Task<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, double> _selector, CancellationToken _cancellationToken)
  5356. {
  5357. double value;
  5358. #if CSHARP8
  5359. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  5360. {
  5361. if (!await e.MoveNextAsync())
  5362. {
  5363. throw Error.NoElements();
  5364. }
  5365. value = _selector(e.Current);
  5366. while (await e.MoveNextAsync())
  5367. {
  5368. var x = _selector(e.Current);
  5369. if (x < value)
  5370. {
  5371. value = x;
  5372. }
  5373. else
  5374. {
  5375. // Normally NaN < anything is false, as is anything < NaN
  5376. // However, this leads to some irksome outcomes in Min and Max.
  5377. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5378. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5379. // ordering where NaN is smaller than every value, including
  5380. // negative infinity.
  5381. // Not testing for NaN therefore isn't an option, but since we
  5382. // can't find a smaller value, we can short-circuit.
  5383. if (double.IsNaN(x))
  5384. {
  5385. return x;
  5386. }
  5387. }
  5388. }
  5389. }
  5390. #else
  5391. var e = _source.GetAsyncEnumerator(_cancellationToken);
  5392. try
  5393. {
  5394. if (!await e.MoveNextAsync().ConfigureAwait(false))
  5395. {
  5396. throw Error.NoElements();
  5397. }
  5398. value = _selector(e.Current);
  5399. while (await e.MoveNextAsync().ConfigureAwait(false))
  5400. {
  5401. var x = _selector(e.Current);
  5402. if (x < value)
  5403. {
  5404. value = x;
  5405. }
  5406. else
  5407. {
  5408. // Normally NaN < anything is false, as is anything < NaN
  5409. // However, this leads to some irksome outcomes in Min and Max.
  5410. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5411. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5412. // ordering where NaN is smaller than every value, including
  5413. // negative infinity.
  5414. // Not testing for NaN therefore isn't an option, but since we
  5415. // can't find a smaller value, we can short-circuit.
  5416. if (double.IsNaN(x))
  5417. {
  5418. return x;
  5419. }
  5420. }
  5421. }
  5422. }
  5423. finally
  5424. {
  5425. await e.DisposeAsync().ConfigureAwait(false);
  5426. }
  5427. #endif
  5428. return value;
  5429. }
  5430. }
  5431. public static Task<double> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  5432. {
  5433. if (source == null)
  5434. throw Error.ArgumentNull(nameof(source));
  5435. if (selector == null)
  5436. throw Error.ArgumentNull(nameof(selector));
  5437. return Core(source, selector, cancellationToken);
  5438. static async Task<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<double>> _selector, CancellationToken _cancellationToken)
  5439. {
  5440. double value;
  5441. #if CSHARP8
  5442. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  5443. {
  5444. if (!await e.MoveNextAsync())
  5445. {
  5446. throw Error.NoElements();
  5447. }
  5448. value = await _selector(e.Current).ConfigureAwait(false);
  5449. while (await e.MoveNextAsync())
  5450. {
  5451. var x = await _selector(e.Current).ConfigureAwait(false);
  5452. if (x < value)
  5453. {
  5454. value = x;
  5455. }
  5456. else
  5457. {
  5458. // Normally NaN < anything is false, as is anything < NaN
  5459. // However, this leads to some irksome outcomes in Min and Max.
  5460. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5461. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5462. // ordering where NaN is smaller than every value, including
  5463. // negative infinity.
  5464. // Not testing for NaN therefore isn't an option, but since we
  5465. // can't find a smaller value, we can short-circuit.
  5466. if (double.IsNaN(x))
  5467. {
  5468. return x;
  5469. }
  5470. }
  5471. }
  5472. }
  5473. #else
  5474. var e = _source.GetAsyncEnumerator(_cancellationToken);
  5475. try
  5476. {
  5477. if (!await e.MoveNextAsync().ConfigureAwait(false))
  5478. {
  5479. throw Error.NoElements();
  5480. }
  5481. value = await _selector(e.Current).ConfigureAwait(false);
  5482. while (await e.MoveNextAsync().ConfigureAwait(false))
  5483. {
  5484. var x = await _selector(e.Current).ConfigureAwait(false);
  5485. if (x < value)
  5486. {
  5487. value = x;
  5488. }
  5489. else
  5490. {
  5491. // Normally NaN < anything is false, as is anything < NaN
  5492. // However, this leads to some irksome outcomes in Min and Max.
  5493. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5494. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5495. // ordering where NaN is smaller than every value, including
  5496. // negative infinity.
  5497. // Not testing for NaN therefore isn't an option, but since we
  5498. // can't find a smaller value, we can short-circuit.
  5499. if (double.IsNaN(x))
  5500. {
  5501. return x;
  5502. }
  5503. }
  5504. }
  5505. }
  5506. finally
  5507. {
  5508. await e.DisposeAsync().ConfigureAwait(false);
  5509. }
  5510. #endif
  5511. return value;
  5512. }
  5513. }
  5514. #if !NO_DEEP_CANCELLATION
  5515. public static Task<double> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  5516. {
  5517. if (source == null)
  5518. throw Error.ArgumentNull(nameof(source));
  5519. if (selector == null)
  5520. throw Error.ArgumentNull(nameof(selector));
  5521. return Core(source, selector, cancellationToken);
  5522. static async Task<double> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<double>> _selector, CancellationToken _cancellationToken)
  5523. {
  5524. double value;
  5525. #if CSHARP8
  5526. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  5527. {
  5528. if (!await e.MoveNextAsync())
  5529. {
  5530. throw Error.NoElements();
  5531. }
  5532. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  5533. while (await e.MoveNextAsync())
  5534. {
  5535. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  5536. if (x < value)
  5537. {
  5538. value = x;
  5539. }
  5540. else
  5541. {
  5542. // Normally NaN < anything is false, as is anything < NaN
  5543. // However, this leads to some irksome outcomes in Min and Max.
  5544. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5545. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5546. // ordering where NaN is smaller than every value, including
  5547. // negative infinity.
  5548. // Not testing for NaN therefore isn't an option, but since we
  5549. // can't find a smaller value, we can short-circuit.
  5550. if (double.IsNaN(x))
  5551. {
  5552. return x;
  5553. }
  5554. }
  5555. }
  5556. }
  5557. #else
  5558. var e = _source.GetAsyncEnumerator(_cancellationToken);
  5559. try
  5560. {
  5561. if (!await e.MoveNextAsync().ConfigureAwait(false))
  5562. {
  5563. throw Error.NoElements();
  5564. }
  5565. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  5566. while (await e.MoveNextAsync().ConfigureAwait(false))
  5567. {
  5568. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  5569. if (x < value)
  5570. {
  5571. value = x;
  5572. }
  5573. else
  5574. {
  5575. // Normally NaN < anything is false, as is anything < NaN
  5576. // However, this leads to some irksome outcomes in Min and Max.
  5577. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5578. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5579. // ordering where NaN is smaller than every value, including
  5580. // negative infinity.
  5581. // Not testing for NaN therefore isn't an option, but since we
  5582. // can't find a smaller value, we can short-circuit.
  5583. if (double.IsNaN(x))
  5584. {
  5585. return x;
  5586. }
  5587. }
  5588. }
  5589. }
  5590. finally
  5591. {
  5592. await e.DisposeAsync().ConfigureAwait(false);
  5593. }
  5594. #endif
  5595. return value;
  5596. }
  5597. }
  5598. #endif
  5599. public static Task<double?> MinAsync(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken = default)
  5600. {
  5601. if (source == null)
  5602. throw Error.ArgumentNull(nameof(source));
  5603. return Core(source, cancellationToken);
  5604. static async Task<double?> Core(IAsyncEnumerable<double?> _source, CancellationToken _cancellationToken)
  5605. {
  5606. double? value = null;
  5607. #if CSHARP8
  5608. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  5609. {
  5610. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5611. // so we don't have to keep testing for nullity.
  5612. do
  5613. {
  5614. if (!await e.MoveNextAsync())
  5615. {
  5616. return value;
  5617. }
  5618. value = e.Current;
  5619. }
  5620. while (!value.HasValue);
  5621. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5622. // using the lifted operation each time.
  5623. var valueVal = value.GetValueOrDefault();
  5624. while (await e.MoveNextAsync())
  5625. {
  5626. var cur = e.Current;
  5627. if (cur.HasValue)
  5628. {
  5629. var x = cur.GetValueOrDefault();
  5630. if (x < valueVal)
  5631. {
  5632. valueVal = x;
  5633. value = cur;
  5634. }
  5635. else
  5636. {
  5637. // Normally NaN < anything is false, as is anything < NaN
  5638. // However, this leads to some irksome outcomes in Min and Max.
  5639. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5640. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5641. // ordering where NaN is smaller than every value, including
  5642. // negative infinity.
  5643. // Not testing for NaN therefore isn't an option, but since we
  5644. // can't find a smaller value, we can short-circuit.
  5645. if (double.IsNaN(x))
  5646. {
  5647. return cur;
  5648. }
  5649. }
  5650. }
  5651. }
  5652. }
  5653. #else
  5654. var e = _source.GetAsyncEnumerator(_cancellationToken);
  5655. try
  5656. {
  5657. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5658. // so we don't have to keep testing for nullity.
  5659. do
  5660. {
  5661. if (!await e.MoveNextAsync().ConfigureAwait(false))
  5662. {
  5663. return value;
  5664. }
  5665. value = e.Current;
  5666. }
  5667. while (!value.HasValue);
  5668. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5669. // using the lifted operation each time.
  5670. var valueVal = value.GetValueOrDefault();
  5671. while (await e.MoveNextAsync().ConfigureAwait(false))
  5672. {
  5673. var cur = e.Current;
  5674. if (cur.HasValue)
  5675. {
  5676. var x = cur.GetValueOrDefault();
  5677. if (x < valueVal)
  5678. {
  5679. valueVal = x;
  5680. value = cur;
  5681. }
  5682. else
  5683. {
  5684. // Normally NaN < anything is false, as is anything < NaN
  5685. // However, this leads to some irksome outcomes in Min and Max.
  5686. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5687. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5688. // ordering where NaN is smaller than every value, including
  5689. // negative infinity.
  5690. // Not testing for NaN therefore isn't an option, but since we
  5691. // can't find a smaller value, we can short-circuit.
  5692. if (double.IsNaN(x))
  5693. {
  5694. return cur;
  5695. }
  5696. }
  5697. }
  5698. }
  5699. }
  5700. finally
  5701. {
  5702. await e.DisposeAsync().ConfigureAwait(false);
  5703. }
  5704. #endif
  5705. return value;
  5706. }
  5707. }
  5708. public static Task<double?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
  5709. {
  5710. if (source == null)
  5711. throw Error.ArgumentNull(nameof(source));
  5712. if (selector == null)
  5713. throw Error.ArgumentNull(nameof(selector));
  5714. return Core(source, selector, cancellationToken);
  5715. static async Task<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, double?> _selector, CancellationToken _cancellationToken)
  5716. {
  5717. double? value = null;
  5718. #if CSHARP8
  5719. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  5720. {
  5721. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5722. // so we don't have to keep testing for nullity.
  5723. do
  5724. {
  5725. if (!await e.MoveNextAsync())
  5726. {
  5727. return value;
  5728. }
  5729. value = _selector(e.Current);
  5730. }
  5731. while (!value.HasValue);
  5732. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5733. // using the lifted operation each time.
  5734. var valueVal = value.GetValueOrDefault();
  5735. while (await e.MoveNextAsync())
  5736. {
  5737. var cur = _selector(e.Current);
  5738. if (cur.HasValue)
  5739. {
  5740. var x = cur.GetValueOrDefault();
  5741. if (x < valueVal)
  5742. {
  5743. valueVal = x;
  5744. value = cur;
  5745. }
  5746. else
  5747. {
  5748. // Normally NaN < anything is false, as is anything < NaN
  5749. // However, this leads to some irksome outcomes in Min and Max.
  5750. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5751. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5752. // ordering where NaN is smaller than every value, including
  5753. // negative infinity.
  5754. // Not testing for NaN therefore isn't an option, but since we
  5755. // can't find a smaller value, we can short-circuit.
  5756. if (double.IsNaN(x))
  5757. {
  5758. return cur;
  5759. }
  5760. }
  5761. }
  5762. }
  5763. }
  5764. #else
  5765. var e = _source.GetAsyncEnumerator(_cancellationToken);
  5766. try
  5767. {
  5768. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5769. // so we don't have to keep testing for nullity.
  5770. do
  5771. {
  5772. if (!await e.MoveNextAsync().ConfigureAwait(false))
  5773. {
  5774. return value;
  5775. }
  5776. value = _selector(e.Current);
  5777. }
  5778. while (!value.HasValue);
  5779. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5780. // using the lifted operation each time.
  5781. var valueVal = value.GetValueOrDefault();
  5782. while (await e.MoveNextAsync().ConfigureAwait(false))
  5783. {
  5784. var cur = _selector(e.Current);
  5785. if (cur.HasValue)
  5786. {
  5787. var x = cur.GetValueOrDefault();
  5788. if (x < valueVal)
  5789. {
  5790. valueVal = x;
  5791. value = cur;
  5792. }
  5793. else
  5794. {
  5795. // Normally NaN < anything is false, as is anything < NaN
  5796. // However, this leads to some irksome outcomes in Min and Max.
  5797. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5798. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5799. // ordering where NaN is smaller than every value, including
  5800. // negative infinity.
  5801. // Not testing for NaN therefore isn't an option, but since we
  5802. // can't find a smaller value, we can short-circuit.
  5803. if (double.IsNaN(x))
  5804. {
  5805. return cur;
  5806. }
  5807. }
  5808. }
  5809. }
  5810. }
  5811. finally
  5812. {
  5813. await e.DisposeAsync().ConfigureAwait(false);
  5814. }
  5815. #endif
  5816. return value;
  5817. }
  5818. }
  5819. public static Task<double?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  5820. {
  5821. if (source == null)
  5822. throw Error.ArgumentNull(nameof(source));
  5823. if (selector == null)
  5824. throw Error.ArgumentNull(nameof(selector));
  5825. return Core(source, selector, cancellationToken);
  5826. static async Task<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<double?>> _selector, CancellationToken _cancellationToken)
  5827. {
  5828. double? value = null;
  5829. #if CSHARP8
  5830. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  5831. {
  5832. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5833. // so we don't have to keep testing for nullity.
  5834. do
  5835. {
  5836. if (!await e.MoveNextAsync())
  5837. {
  5838. return value;
  5839. }
  5840. value = await _selector(e.Current).ConfigureAwait(false);
  5841. }
  5842. while (!value.HasValue);
  5843. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5844. // using the lifted operation each time.
  5845. var valueVal = value.GetValueOrDefault();
  5846. while (await e.MoveNextAsync())
  5847. {
  5848. var cur = await _selector(e.Current).ConfigureAwait(false);
  5849. if (cur.HasValue)
  5850. {
  5851. var x = cur.GetValueOrDefault();
  5852. if (x < valueVal)
  5853. {
  5854. valueVal = x;
  5855. value = cur;
  5856. }
  5857. else
  5858. {
  5859. // Normally NaN < anything is false, as is anything < NaN
  5860. // However, this leads to some irksome outcomes in Min and Max.
  5861. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5862. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5863. // ordering where NaN is smaller than every value, including
  5864. // negative infinity.
  5865. // Not testing for NaN therefore isn't an option, but since we
  5866. // can't find a smaller value, we can short-circuit.
  5867. if (double.IsNaN(x))
  5868. {
  5869. return cur;
  5870. }
  5871. }
  5872. }
  5873. }
  5874. }
  5875. #else
  5876. var e = _source.GetAsyncEnumerator(_cancellationToken);
  5877. try
  5878. {
  5879. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5880. // so we don't have to keep testing for nullity.
  5881. do
  5882. {
  5883. if (!await e.MoveNextAsync().ConfigureAwait(false))
  5884. {
  5885. return value;
  5886. }
  5887. value = await _selector(e.Current).ConfigureAwait(false);
  5888. }
  5889. while (!value.HasValue);
  5890. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5891. // using the lifted operation each time.
  5892. var valueVal = value.GetValueOrDefault();
  5893. while (await e.MoveNextAsync().ConfigureAwait(false))
  5894. {
  5895. var cur = await _selector(e.Current).ConfigureAwait(false);
  5896. if (cur.HasValue)
  5897. {
  5898. var x = cur.GetValueOrDefault();
  5899. if (x < valueVal)
  5900. {
  5901. valueVal = x;
  5902. value = cur;
  5903. }
  5904. else
  5905. {
  5906. // Normally NaN < anything is false, as is anything < NaN
  5907. // However, this leads to some irksome outcomes in Min and Max.
  5908. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5909. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5910. // ordering where NaN is smaller than every value, including
  5911. // negative infinity.
  5912. // Not testing for NaN therefore isn't an option, but since we
  5913. // can't find a smaller value, we can short-circuit.
  5914. if (double.IsNaN(x))
  5915. {
  5916. return cur;
  5917. }
  5918. }
  5919. }
  5920. }
  5921. }
  5922. finally
  5923. {
  5924. await e.DisposeAsync().ConfigureAwait(false);
  5925. }
  5926. #endif
  5927. return value;
  5928. }
  5929. }
  5930. #if !NO_DEEP_CANCELLATION
  5931. public static Task<double?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  5932. {
  5933. if (source == null)
  5934. throw Error.ArgumentNull(nameof(source));
  5935. if (selector == null)
  5936. throw Error.ArgumentNull(nameof(selector));
  5937. return Core(source, selector, cancellationToken);
  5938. static async Task<double?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<double?>> _selector, CancellationToken _cancellationToken)
  5939. {
  5940. double? value = null;
  5941. #if CSHARP8
  5942. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  5943. {
  5944. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5945. // so we don't have to keep testing for nullity.
  5946. do
  5947. {
  5948. if (!await e.MoveNextAsync())
  5949. {
  5950. return value;
  5951. }
  5952. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  5953. }
  5954. while (!value.HasValue);
  5955. // Keep hold of the wrapped value, and do comparisons on that, rather than
  5956. // using the lifted operation each time.
  5957. var valueVal = value.GetValueOrDefault();
  5958. while (await e.MoveNextAsync())
  5959. {
  5960. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  5961. if (cur.HasValue)
  5962. {
  5963. var x = cur.GetValueOrDefault();
  5964. if (x < valueVal)
  5965. {
  5966. valueVal = x;
  5967. value = cur;
  5968. }
  5969. else
  5970. {
  5971. // Normally NaN < anything is false, as is anything < NaN
  5972. // However, this leads to some irksome outcomes in Min and Max.
  5973. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  5974. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  5975. // ordering where NaN is smaller than every value, including
  5976. // negative infinity.
  5977. // Not testing for NaN therefore isn't an option, but since we
  5978. // can't find a smaller value, we can short-circuit.
  5979. if (double.IsNaN(x))
  5980. {
  5981. return cur;
  5982. }
  5983. }
  5984. }
  5985. }
  5986. }
  5987. #else
  5988. var e = _source.GetAsyncEnumerator(_cancellationToken);
  5989. try
  5990. {
  5991. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  5992. // so we don't have to keep testing for nullity.
  5993. do
  5994. {
  5995. if (!await e.MoveNextAsync().ConfigureAwait(false))
  5996. {
  5997. return value;
  5998. }
  5999. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  6000. }
  6001. while (!value.HasValue);
  6002. // Keep hold of the wrapped value, and do comparisons on that, rather than
  6003. // using the lifted operation each time.
  6004. var valueVal = value.GetValueOrDefault();
  6005. while (await e.MoveNextAsync().ConfigureAwait(false))
  6006. {
  6007. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  6008. if (cur.HasValue)
  6009. {
  6010. var x = cur.GetValueOrDefault();
  6011. if (x < valueVal)
  6012. {
  6013. valueVal = x;
  6014. value = cur;
  6015. }
  6016. else
  6017. {
  6018. // Normally NaN < anything is false, as is anything < NaN
  6019. // However, this leads to some irksome outcomes in Min and Max.
  6020. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  6021. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  6022. // ordering where NaN is smaller than every value, including
  6023. // negative infinity.
  6024. // Not testing for NaN therefore isn't an option, but since we
  6025. // can't find a smaller value, we can short-circuit.
  6026. if (double.IsNaN(x))
  6027. {
  6028. return cur;
  6029. }
  6030. }
  6031. }
  6032. }
  6033. }
  6034. finally
  6035. {
  6036. await e.DisposeAsync().ConfigureAwait(false);
  6037. }
  6038. #endif
  6039. return value;
  6040. }
  6041. }
  6042. #endif
  6043. public static Task<decimal> MinAsync(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken = default)
  6044. {
  6045. if (source == null)
  6046. throw Error.ArgumentNull(nameof(source));
  6047. return Core(source, cancellationToken);
  6048. static async Task<decimal> Core(IAsyncEnumerable<decimal> _source, CancellationToken _cancellationToken)
  6049. {
  6050. decimal value;
  6051. #if CSHARP8
  6052. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  6053. {
  6054. if (!await e.MoveNextAsync())
  6055. {
  6056. throw Error.NoElements();
  6057. }
  6058. value = e.Current;
  6059. while (await e.MoveNextAsync())
  6060. {
  6061. var x = e.Current;
  6062. if (x < value)
  6063. {
  6064. value = x;
  6065. }
  6066. }
  6067. }
  6068. #else
  6069. var e = _source.GetAsyncEnumerator(_cancellationToken);
  6070. try
  6071. {
  6072. if (!await e.MoveNextAsync().ConfigureAwait(false))
  6073. {
  6074. throw Error.NoElements();
  6075. }
  6076. value = e.Current;
  6077. while (await e.MoveNextAsync().ConfigureAwait(false))
  6078. {
  6079. var x = e.Current;
  6080. if (x < value)
  6081. {
  6082. value = x;
  6083. }
  6084. }
  6085. }
  6086. finally
  6087. {
  6088. await e.DisposeAsync().ConfigureAwait(false);
  6089. }
  6090. #endif
  6091. return value;
  6092. }
  6093. }
  6094. public static Task<decimal> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken = default)
  6095. {
  6096. if (source == null)
  6097. throw Error.ArgumentNull(nameof(source));
  6098. if (selector == null)
  6099. throw Error.ArgumentNull(nameof(selector));
  6100. return Core(source, selector, cancellationToken);
  6101. static async Task<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, decimal> _selector, CancellationToken _cancellationToken)
  6102. {
  6103. decimal value;
  6104. #if CSHARP8
  6105. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  6106. {
  6107. if (!await e.MoveNextAsync())
  6108. {
  6109. throw Error.NoElements();
  6110. }
  6111. value = _selector(e.Current);
  6112. while (await e.MoveNextAsync())
  6113. {
  6114. var x = _selector(e.Current);
  6115. if (x < value)
  6116. {
  6117. value = x;
  6118. }
  6119. }
  6120. }
  6121. #else
  6122. var e = _source.GetAsyncEnumerator(_cancellationToken);
  6123. try
  6124. {
  6125. if (!await e.MoveNextAsync().ConfigureAwait(false))
  6126. {
  6127. throw Error.NoElements();
  6128. }
  6129. value = _selector(e.Current);
  6130. while (await e.MoveNextAsync().ConfigureAwait(false))
  6131. {
  6132. var x = _selector(e.Current);
  6133. if (x < value)
  6134. {
  6135. value = x;
  6136. }
  6137. }
  6138. }
  6139. finally
  6140. {
  6141. await e.DisposeAsync().ConfigureAwait(false);
  6142. }
  6143. #endif
  6144. return value;
  6145. }
  6146. }
  6147. public static Task<decimal> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  6148. {
  6149. if (source == null)
  6150. throw Error.ArgumentNull(nameof(source));
  6151. if (selector == null)
  6152. throw Error.ArgumentNull(nameof(selector));
  6153. return Core(source, selector, cancellationToken);
  6154. static async Task<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<decimal>> _selector, CancellationToken _cancellationToken)
  6155. {
  6156. decimal value;
  6157. #if CSHARP8
  6158. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  6159. {
  6160. if (!await e.MoveNextAsync())
  6161. {
  6162. throw Error.NoElements();
  6163. }
  6164. value = await _selector(e.Current).ConfigureAwait(false);
  6165. while (await e.MoveNextAsync())
  6166. {
  6167. var x = await _selector(e.Current).ConfigureAwait(false);
  6168. if (x < value)
  6169. {
  6170. value = x;
  6171. }
  6172. }
  6173. }
  6174. #else
  6175. var e = _source.GetAsyncEnumerator(_cancellationToken);
  6176. try
  6177. {
  6178. if (!await e.MoveNextAsync().ConfigureAwait(false))
  6179. {
  6180. throw Error.NoElements();
  6181. }
  6182. value = await _selector(e.Current).ConfigureAwait(false);
  6183. while (await e.MoveNextAsync().ConfigureAwait(false))
  6184. {
  6185. var x = await _selector(e.Current).ConfigureAwait(false);
  6186. if (x < value)
  6187. {
  6188. value = x;
  6189. }
  6190. }
  6191. }
  6192. finally
  6193. {
  6194. await e.DisposeAsync().ConfigureAwait(false);
  6195. }
  6196. #endif
  6197. return value;
  6198. }
  6199. }
  6200. #if !NO_DEEP_CANCELLATION
  6201. public static Task<decimal> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  6202. {
  6203. if (source == null)
  6204. throw Error.ArgumentNull(nameof(source));
  6205. if (selector == null)
  6206. throw Error.ArgumentNull(nameof(selector));
  6207. return Core(source, selector, cancellationToken);
  6208. static async Task<decimal> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<decimal>> _selector, CancellationToken _cancellationToken)
  6209. {
  6210. decimal value;
  6211. #if CSHARP8
  6212. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  6213. {
  6214. if (!await e.MoveNextAsync())
  6215. {
  6216. throw Error.NoElements();
  6217. }
  6218. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  6219. while (await e.MoveNextAsync())
  6220. {
  6221. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  6222. if (x < value)
  6223. {
  6224. value = x;
  6225. }
  6226. }
  6227. }
  6228. #else
  6229. var e = _source.GetAsyncEnumerator(_cancellationToken);
  6230. try
  6231. {
  6232. if (!await e.MoveNextAsync().ConfigureAwait(false))
  6233. {
  6234. throw Error.NoElements();
  6235. }
  6236. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  6237. while (await e.MoveNextAsync().ConfigureAwait(false))
  6238. {
  6239. var x = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  6240. if (x < value)
  6241. {
  6242. value = x;
  6243. }
  6244. }
  6245. }
  6246. finally
  6247. {
  6248. await e.DisposeAsync().ConfigureAwait(false);
  6249. }
  6250. #endif
  6251. return value;
  6252. }
  6253. }
  6254. #endif
  6255. public static Task<decimal?> MinAsync(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken = default)
  6256. {
  6257. if (source == null)
  6258. throw Error.ArgumentNull(nameof(source));
  6259. return Core(source, cancellationToken);
  6260. static async Task<decimal?> Core(IAsyncEnumerable<decimal?> _source, CancellationToken _cancellationToken)
  6261. {
  6262. decimal? value = null;
  6263. #if CSHARP8
  6264. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  6265. {
  6266. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  6267. // so we don't have to keep testing for nullity.
  6268. do
  6269. {
  6270. if (!await e.MoveNextAsync())
  6271. {
  6272. return value;
  6273. }
  6274. value = e.Current;
  6275. }
  6276. while (!value.HasValue);
  6277. // Keep hold of the wrapped value, and do comparisons on that, rather than
  6278. // using the lifted operation each time.
  6279. var valueVal = value.GetValueOrDefault();
  6280. while (await e.MoveNextAsync())
  6281. {
  6282. var cur = e.Current;
  6283. var x = cur.GetValueOrDefault();
  6284. if (cur.HasValue && x < valueVal)
  6285. {
  6286. valueVal = x;
  6287. value = cur;
  6288. }
  6289. }
  6290. }
  6291. #else
  6292. var e = _source.GetAsyncEnumerator(_cancellationToken);
  6293. try
  6294. {
  6295. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  6296. // so we don't have to keep testing for nullity.
  6297. do
  6298. {
  6299. if (!await e.MoveNextAsync().ConfigureAwait(false))
  6300. {
  6301. return value;
  6302. }
  6303. value = e.Current;
  6304. }
  6305. while (!value.HasValue);
  6306. // Keep hold of the wrapped value, and do comparisons on that, rather than
  6307. // using the lifted operation each time.
  6308. var valueVal = value.GetValueOrDefault();
  6309. while (await e.MoveNextAsync().ConfigureAwait(false))
  6310. {
  6311. var cur = e.Current;
  6312. var x = cur.GetValueOrDefault();
  6313. if (cur.HasValue && x < valueVal)
  6314. {
  6315. valueVal = x;
  6316. value = cur;
  6317. }
  6318. }
  6319. }
  6320. finally
  6321. {
  6322. await e.DisposeAsync().ConfigureAwait(false);
  6323. }
  6324. #endif
  6325. return value;
  6326. }
  6327. }
  6328. public static Task<decimal?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken = default)
  6329. {
  6330. if (source == null)
  6331. throw Error.ArgumentNull(nameof(source));
  6332. if (selector == null)
  6333. throw Error.ArgumentNull(nameof(selector));
  6334. return Core(source, selector, cancellationToken);
  6335. static async Task<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, decimal?> _selector, CancellationToken _cancellationToken)
  6336. {
  6337. decimal? value = null;
  6338. #if CSHARP8
  6339. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  6340. {
  6341. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  6342. // so we don't have to keep testing for nullity.
  6343. do
  6344. {
  6345. if (!await e.MoveNextAsync())
  6346. {
  6347. return value;
  6348. }
  6349. value = _selector(e.Current);
  6350. }
  6351. while (!value.HasValue);
  6352. // Keep hold of the wrapped value, and do comparisons on that, rather than
  6353. // using the lifted operation each time.
  6354. var valueVal = value.GetValueOrDefault();
  6355. while (await e.MoveNextAsync())
  6356. {
  6357. var cur = _selector(e.Current);
  6358. var x = cur.GetValueOrDefault();
  6359. if (cur.HasValue && x < valueVal)
  6360. {
  6361. valueVal = x;
  6362. value = cur;
  6363. }
  6364. }
  6365. }
  6366. #else
  6367. var e = _source.GetAsyncEnumerator(_cancellationToken);
  6368. try
  6369. {
  6370. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  6371. // so we don't have to keep testing for nullity.
  6372. do
  6373. {
  6374. if (!await e.MoveNextAsync().ConfigureAwait(false))
  6375. {
  6376. return value;
  6377. }
  6378. value = _selector(e.Current);
  6379. }
  6380. while (!value.HasValue);
  6381. // Keep hold of the wrapped value, and do comparisons on that, rather than
  6382. // using the lifted operation each time.
  6383. var valueVal = value.GetValueOrDefault();
  6384. while (await e.MoveNextAsync().ConfigureAwait(false))
  6385. {
  6386. var cur = _selector(e.Current);
  6387. var x = cur.GetValueOrDefault();
  6388. if (cur.HasValue && x < valueVal)
  6389. {
  6390. valueVal = x;
  6391. value = cur;
  6392. }
  6393. }
  6394. }
  6395. finally
  6396. {
  6397. await e.DisposeAsync().ConfigureAwait(false);
  6398. }
  6399. #endif
  6400. return value;
  6401. }
  6402. }
  6403. public static Task<decimal?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  6404. {
  6405. if (source == null)
  6406. throw Error.ArgumentNull(nameof(source));
  6407. if (selector == null)
  6408. throw Error.ArgumentNull(nameof(selector));
  6409. return Core(source, selector, cancellationToken);
  6410. static async Task<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<decimal?>> _selector, CancellationToken _cancellationToken)
  6411. {
  6412. decimal? value = null;
  6413. #if CSHARP8
  6414. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  6415. {
  6416. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  6417. // so we don't have to keep testing for nullity.
  6418. do
  6419. {
  6420. if (!await e.MoveNextAsync())
  6421. {
  6422. return value;
  6423. }
  6424. value = await _selector(e.Current).ConfigureAwait(false);
  6425. }
  6426. while (!value.HasValue);
  6427. // Keep hold of the wrapped value, and do comparisons on that, rather than
  6428. // using the lifted operation each time.
  6429. var valueVal = value.GetValueOrDefault();
  6430. while (await e.MoveNextAsync())
  6431. {
  6432. var cur = await _selector(e.Current).ConfigureAwait(false);
  6433. var x = cur.GetValueOrDefault();
  6434. if (cur.HasValue && x < valueVal)
  6435. {
  6436. valueVal = x;
  6437. value = cur;
  6438. }
  6439. }
  6440. }
  6441. #else
  6442. var e = _source.GetAsyncEnumerator(_cancellationToken);
  6443. try
  6444. {
  6445. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  6446. // so we don't have to keep testing for nullity.
  6447. do
  6448. {
  6449. if (!await e.MoveNextAsync().ConfigureAwait(false))
  6450. {
  6451. return value;
  6452. }
  6453. value = await _selector(e.Current).ConfigureAwait(false);
  6454. }
  6455. while (!value.HasValue);
  6456. // Keep hold of the wrapped value, and do comparisons on that, rather than
  6457. // using the lifted operation each time.
  6458. var valueVal = value.GetValueOrDefault();
  6459. while (await e.MoveNextAsync().ConfigureAwait(false))
  6460. {
  6461. var cur = await _selector(e.Current).ConfigureAwait(false);
  6462. var x = cur.GetValueOrDefault();
  6463. if (cur.HasValue && x < valueVal)
  6464. {
  6465. valueVal = x;
  6466. value = cur;
  6467. }
  6468. }
  6469. }
  6470. finally
  6471. {
  6472. await e.DisposeAsync().ConfigureAwait(false);
  6473. }
  6474. #endif
  6475. return value;
  6476. }
  6477. }
  6478. #if !NO_DEEP_CANCELLATION
  6479. public static Task<decimal?> MinAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  6480. {
  6481. if (source == null)
  6482. throw Error.ArgumentNull(nameof(source));
  6483. if (selector == null)
  6484. throw Error.ArgumentNull(nameof(selector));
  6485. return Core(source, selector, cancellationToken);
  6486. static async Task<decimal?> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<decimal?>> _selector, CancellationToken _cancellationToken)
  6487. {
  6488. decimal? value = null;
  6489. #if CSHARP8
  6490. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  6491. {
  6492. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  6493. // so we don't have to keep testing for nullity.
  6494. do
  6495. {
  6496. if (!await e.MoveNextAsync())
  6497. {
  6498. return value;
  6499. }
  6500. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  6501. }
  6502. while (!value.HasValue);
  6503. // Keep hold of the wrapped value, and do comparisons on that, rather than
  6504. // using the lifted operation each time.
  6505. var valueVal = value.GetValueOrDefault();
  6506. while (await e.MoveNextAsync())
  6507. {
  6508. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  6509. var x = cur.GetValueOrDefault();
  6510. if (cur.HasValue && x < valueVal)
  6511. {
  6512. valueVal = x;
  6513. value = cur;
  6514. }
  6515. }
  6516. }
  6517. #else
  6518. var e = _source.GetAsyncEnumerator(_cancellationToken);
  6519. try
  6520. {
  6521. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  6522. // so we don't have to keep testing for nullity.
  6523. do
  6524. {
  6525. if (!await e.MoveNextAsync().ConfigureAwait(false))
  6526. {
  6527. return value;
  6528. }
  6529. value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  6530. }
  6531. while (!value.HasValue);
  6532. // Keep hold of the wrapped value, and do comparisons on that, rather than
  6533. // using the lifted operation each time.
  6534. var valueVal = value.GetValueOrDefault();
  6535. while (await e.MoveNextAsync().ConfigureAwait(false))
  6536. {
  6537. var cur = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  6538. var x = cur.GetValueOrDefault();
  6539. if (cur.HasValue && x < valueVal)
  6540. {
  6541. valueVal = x;
  6542. value = cur;
  6543. }
  6544. }
  6545. }
  6546. finally
  6547. {
  6548. await e.DisposeAsync().ConfigureAwait(false);
  6549. }
  6550. #endif
  6551. return value;
  6552. }
  6553. }
  6554. #endif
  6555. }
  6556. }