algorithm 197 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685
  1. // -*- C++ -*-
  2. //===-------------------------- algorithm ---------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_ALGORITHM
  10. #define _LIBCPP_ALGORITHM
  11. /*
  12. algorithm synopsis
  13. #include <initializer_list>
  14. namespace std
  15. {
  16. template <class InputIterator, class Predicate>
  17. constexpr bool // constexpr in C++20
  18. all_of(InputIterator first, InputIterator last, Predicate pred);
  19. template <class InputIterator, class Predicate>
  20. constexpr bool // constexpr in C++20
  21. any_of(InputIterator first, InputIterator last, Predicate pred);
  22. template <class InputIterator, class Predicate>
  23. constexpr bool // constexpr in C++20
  24. none_of(InputIterator first, InputIterator last, Predicate pred);
  25. template <class InputIterator, class Function>
  26. constexpr Function // constexpr in C++20
  27. for_each(InputIterator first, InputIterator last, Function f);
  28. template<class InputIterator, class Size, class Function>
  29. constexpr InputIterator // constexpr in C++20
  30. for_each_n(InputIterator first, Size n, Function f); // C++17
  31. template <class InputIterator, class T>
  32. constexpr InputIterator // constexpr in C++20
  33. find(InputIterator first, InputIterator last, const T& value);
  34. template <class InputIterator, class Predicate>
  35. constexpr InputIterator // constexpr in C++20
  36. find_if(InputIterator first, InputIterator last, Predicate pred);
  37. template<class InputIterator, class Predicate>
  38. InputIterator // constexpr in C++20
  39. find_if_not(InputIterator first, InputIterator last, Predicate pred);
  40. template <class ForwardIterator1, class ForwardIterator2>
  41. ForwardIterator1 // constexpr in C++20
  42. find_end(ForwardIterator1 first1, ForwardIterator1 last1,
  43. ForwardIterator2 first2, ForwardIterator2 last2);
  44. template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  45. ForwardIterator1 // constexpr in C++20
  46. find_end(ForwardIterator1 first1, ForwardIterator1 last1,
  47. ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
  48. template <class ForwardIterator1, class ForwardIterator2>
  49. constexpr ForwardIterator1 // constexpr in C++20
  50. find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
  51. ForwardIterator2 first2, ForwardIterator2 last2);
  52. template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  53. constexpr ForwardIterator1 // constexpr in C++20
  54. find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
  55. ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
  56. template <class ForwardIterator>
  57. constexpr ForwardIterator // constexpr in C++20
  58. adjacent_find(ForwardIterator first, ForwardIterator last);
  59. template <class ForwardIterator, class BinaryPredicate>
  60. constexpr ForwardIterator // constexpr in C++20
  61. adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
  62. template <class InputIterator, class T>
  63. constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
  64. count(InputIterator first, InputIterator last, const T& value);
  65. template <class InputIterator, class Predicate>
  66. constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
  67. count_if(InputIterator first, InputIterator last, Predicate pred);
  68. template <class InputIterator1, class InputIterator2>
  69. constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
  70. mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
  71. template <class InputIterator1, class InputIterator2>
  72. constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
  73. mismatch(InputIterator1 first1, InputIterator1 last1,
  74. InputIterator2 first2, InputIterator2 last2); // **C++14**
  75. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  76. constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
  77. mismatch(InputIterator1 first1, InputIterator1 last1,
  78. InputIterator2 first2, BinaryPredicate pred);
  79. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  80. constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
  81. mismatch(InputIterator1 first1, InputIterator1 last1,
  82. InputIterator2 first2, InputIterator2 last2,
  83. BinaryPredicate pred); // **C++14**
  84. template <class InputIterator1, class InputIterator2>
  85. constexpr bool // constexpr in C++20
  86. equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
  87. template <class InputIterator1, class InputIterator2>
  88. constexpr bool // constexpr in C++20
  89. equal(InputIterator1 first1, InputIterator1 last1,
  90. InputIterator2 first2, InputIterator2 last2); // **C++14**
  91. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  92. constexpr bool // constexpr in C++20
  93. equal(InputIterator1 first1, InputIterator1 last1,
  94. InputIterator2 first2, BinaryPredicate pred);
  95. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  96. constexpr bool // constexpr in C++20
  97. equal(InputIterator1 first1, InputIterator1 last1,
  98. InputIterator2 first2, InputIterator2 last2,
  99. BinaryPredicate pred); // **C++14**
  100. template<class ForwardIterator1, class ForwardIterator2>
  101. constexpr bool // constexpr in C++20
  102. is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
  103. ForwardIterator2 first2);
  104. template<class ForwardIterator1, class ForwardIterator2>
  105. constexpr bool // constexpr in C++20
  106. is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
  107. ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
  108. template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  109. constexpr bool // constexpr in C++20
  110. is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
  111. ForwardIterator2 first2, BinaryPredicate pred);
  112. template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  113. constexpr bool // constexpr in C++20
  114. is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
  115. ForwardIterator2 first2, ForwardIterator2 last2,
  116. BinaryPredicate pred); // **C++14**
  117. template <class ForwardIterator1, class ForwardIterator2>
  118. constexpr ForwardIterator1 // constexpr in C++20
  119. search(ForwardIterator1 first1, ForwardIterator1 last1,
  120. ForwardIterator2 first2, ForwardIterator2 last2);
  121. template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  122. constexpr ForwardIterator1 // constexpr in C++20
  123. search(ForwardIterator1 first1, ForwardIterator1 last1,
  124. ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
  125. template <class ForwardIterator, class Size, class T>
  126. constexpr ForwardIterator // constexpr in C++20
  127. search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
  128. template <class ForwardIterator, class Size, class T, class BinaryPredicate>
  129. constexpr ForwardIterator // constexpr in C++20
  130. search_n(ForwardIterator first, ForwardIterator last,
  131. Size count, const T& value, BinaryPredicate pred);
  132. template <class InputIterator, class OutputIterator>
  133. OutputIterator
  134. copy(InputIterator first, InputIterator last, OutputIterator result);
  135. template<class InputIterator, class OutputIterator, class Predicate>
  136. OutputIterator
  137. copy_if(InputIterator first, InputIterator last,
  138. OutputIterator result, Predicate pred);
  139. template<class InputIterator, class Size, class OutputIterator>
  140. OutputIterator
  141. copy_n(InputIterator first, Size n, OutputIterator result);
  142. template <class BidirectionalIterator1, class BidirectionalIterator2>
  143. BidirectionalIterator2
  144. copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
  145. BidirectionalIterator2 result);
  146. template <class ForwardIterator1, class ForwardIterator2>
  147. ForwardIterator2
  148. swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
  149. template <class ForwardIterator1, class ForwardIterator2>
  150. void
  151. iter_swap(ForwardIterator1 a, ForwardIterator2 b);
  152. template <class InputIterator, class OutputIterator, class UnaryOperation>
  153. constexpr OutputIterator // constexpr in C++20
  154. transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
  155. template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
  156. constexpr OutputIterator // constexpr in C++20
  157. transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
  158. OutputIterator result, BinaryOperation binary_op);
  159. template <class ForwardIterator, class T>
  160. constexpr void // constexpr in C++20
  161. replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
  162. template <class ForwardIterator, class Predicate, class T>
  163. constexpr void // constexpr in C++20
  164. replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
  165. template <class InputIterator, class OutputIterator, class T>
  166. constexpr OutputIterator // constexpr in C++20
  167. replace_copy(InputIterator first, InputIterator last, OutputIterator result,
  168. const T& old_value, const T& new_value);
  169. template <class InputIterator, class OutputIterator, class Predicate, class T>
  170. constexpr OutputIterator // constexpr in C++20
  171. replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
  172. template <class ForwardIterator, class T>
  173. constexpr void // constexpr in C++20
  174. fill(ForwardIterator first, ForwardIterator last, const T& value);
  175. template <class OutputIterator, class Size, class T>
  176. constexpr OutputIterator // constexpr in C++20
  177. fill_n(OutputIterator first, Size n, const T& value);
  178. template <class ForwardIterator, class Generator>
  179. constexpr void // constexpr in C++20
  180. generate(ForwardIterator first, ForwardIterator last, Generator gen);
  181. template <class OutputIterator, class Size, class Generator>
  182. constexpr OutputIterator // constexpr in C++20
  183. generate_n(OutputIterator first, Size n, Generator gen);
  184. template <class ForwardIterator, class T>
  185. constexpr ForwardIterator // constexpr in C++20
  186. remove(ForwardIterator first, ForwardIterator last, const T& value);
  187. template <class ForwardIterator, class Predicate>
  188. constexpr ForwardIterator // constexpr in C++20
  189. remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
  190. template <class InputIterator, class OutputIterator, class T>
  191. constexpr OutputIterator // constexpr in C++20
  192. remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
  193. template <class InputIterator, class OutputIterator, class Predicate>
  194. constexpr OutputIterator // constexpr in C++20
  195. remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
  196. template <class ForwardIterator>
  197. ForwardIterator
  198. unique(ForwardIterator first, ForwardIterator last);
  199. template <class ForwardIterator, class BinaryPredicate>
  200. ForwardIterator
  201. unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
  202. template <class InputIterator, class OutputIterator>
  203. OutputIterator
  204. unique_copy(InputIterator first, InputIterator last, OutputIterator result);
  205. template <class InputIterator, class OutputIterator, class BinaryPredicate>
  206. OutputIterator
  207. unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
  208. template <class BidirectionalIterator>
  209. void
  210. reverse(BidirectionalIterator first, BidirectionalIterator last);
  211. template <class BidirectionalIterator, class OutputIterator>
  212. constexpr OutputIterator // constexpr in C++20
  213. reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
  214. template <class ForwardIterator>
  215. ForwardIterator
  216. rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
  217. template <class ForwardIterator, class OutputIterator>
  218. OutputIterator
  219. rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
  220. template <class RandomAccessIterator>
  221. void
  222. random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
  223. template <class RandomAccessIterator, class RandomNumberGenerator>
  224. void
  225. random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
  226. RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
  227. template<class PopulationIterator, class SampleIterator,
  228. class Distance, class UniformRandomBitGenerator>
  229. SampleIterator sample(PopulationIterator first, PopulationIterator last,
  230. SampleIterator out, Distance n,
  231. UniformRandomBitGenerator&& g); // C++17
  232. template<class RandomAccessIterator, class UniformRandomNumberGenerator>
  233. void shuffle(RandomAccessIterator first, RandomAccessIterator last,
  234. UniformRandomNumberGenerator&& g);
  235. template <class InputIterator, class Predicate>
  236. constexpr bool // constexpr in C++20
  237. is_partitioned(InputIterator first, InputIterator last, Predicate pred);
  238. template <class ForwardIterator, class Predicate>
  239. ForwardIterator
  240. partition(ForwardIterator first, ForwardIterator last, Predicate pred);
  241. template <class InputIterator, class OutputIterator1,
  242. class OutputIterator2, class Predicate>
  243. constexpr pair<OutputIterator1, OutputIterator2> // constexpr in C++20
  244. partition_copy(InputIterator first, InputIterator last,
  245. OutputIterator1 out_true, OutputIterator2 out_false,
  246. Predicate pred);
  247. template <class ForwardIterator, class Predicate>
  248. ForwardIterator
  249. stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
  250. template<class ForwardIterator, class Predicate>
  251. constexpr ForwardIterator // constexpr in C++20
  252. partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
  253. template <class ForwardIterator>
  254. constexpr bool // constexpr in C++20
  255. is_sorted(ForwardIterator first, ForwardIterator last);
  256. template <class ForwardIterator, class Compare>
  257. bool
  258. is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
  259. template<class ForwardIterator>
  260. constexpr ForwardIterator // constexpr in C++20
  261. is_sorted_until(ForwardIterator first, ForwardIterator last);
  262. template <class ForwardIterator, class Compare>
  263. constexpr ForwardIterator // constexpr in C++20
  264. is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
  265. template <class RandomAccessIterator>
  266. void
  267. sort(RandomAccessIterator first, RandomAccessIterator last);
  268. template <class RandomAccessIterator, class Compare>
  269. void
  270. sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  271. template <class RandomAccessIterator>
  272. void
  273. stable_sort(RandomAccessIterator first, RandomAccessIterator last);
  274. template <class RandomAccessIterator, class Compare>
  275. void
  276. stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  277. template <class RandomAccessIterator>
  278. void
  279. partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
  280. template <class RandomAccessIterator, class Compare>
  281. void
  282. partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
  283. template <class InputIterator, class RandomAccessIterator>
  284. RandomAccessIterator
  285. partial_sort_copy(InputIterator first, InputIterator last,
  286. RandomAccessIterator result_first, RandomAccessIterator result_last);
  287. template <class InputIterator, class RandomAccessIterator, class Compare>
  288. RandomAccessIterator
  289. partial_sort_copy(InputIterator first, InputIterator last,
  290. RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
  291. template <class RandomAccessIterator>
  292. void
  293. nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
  294. template <class RandomAccessIterator, class Compare>
  295. void
  296. nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
  297. template <class ForwardIterator, class T>
  298. constexpr ForwardIterator // constexpr in C++20
  299. lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
  300. template <class ForwardIterator, class T, class Compare>
  301. constexpr ForwardIterator // constexpr in C++20
  302. lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
  303. template <class ForwardIterator, class T>
  304. constexpr ForwardIterator // constexpr in C++20
  305. upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
  306. template <class ForwardIterator, class T, class Compare>
  307. constexpr ForwardIterator // constexpr in C++20
  308. upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
  309. template <class ForwardIterator, class T>
  310. constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
  311. equal_range(ForwardIterator first, ForwardIterator last, const T& value);
  312. template <class ForwardIterator, class T, class Compare>
  313. constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
  314. equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
  315. template <class ForwardIterator, class T>
  316. constexpr bool // constexpr in C++20
  317. binary_search(ForwardIterator first, ForwardIterator last, const T& value);
  318. template <class ForwardIterator, class T, class Compare>
  319. constexpr bool // constexpr in C++20
  320. binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
  321. template <class InputIterator1, class InputIterator2, class OutputIterator>
  322. OutputIterator
  323. merge(InputIterator1 first1, InputIterator1 last1,
  324. InputIterator2 first2, InputIterator2 last2, OutputIterator result);
  325. template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
  326. OutputIterator
  327. merge(InputIterator1 first1, InputIterator1 last1,
  328. InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
  329. template <class BidirectionalIterator>
  330. void
  331. inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
  332. template <class BidirectionalIterator, class Compare>
  333. void
  334. inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
  335. template <class InputIterator1, class InputIterator2>
  336. constexpr bool // constexpr in C++20
  337. includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
  338. template <class InputIterator1, class InputIterator2, class Compare>
  339. constexpr bool // constexpr in C++20
  340. includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
  341. template <class InputIterator1, class InputIterator2, class OutputIterator>
  342. OutputIterator
  343. set_union(InputIterator1 first1, InputIterator1 last1,
  344. InputIterator2 first2, InputIterator2 last2, OutputIterator result);
  345. template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
  346. OutputIterator
  347. set_union(InputIterator1 first1, InputIterator1 last1,
  348. InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
  349. template <class InputIterator1, class InputIterator2, class OutputIterator>
  350. constexpr OutputIterator // constexpr in C++20
  351. set_intersection(InputIterator1 first1, InputIterator1 last1,
  352. InputIterator2 first2, InputIterator2 last2, OutputIterator result);
  353. template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
  354. constexpr OutputIterator // constexpr in C++20
  355. set_intersection(InputIterator1 first1, InputIterator1 last1,
  356. InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
  357. template <class InputIterator1, class InputIterator2, class OutputIterator>
  358. OutputIterator
  359. set_difference(InputIterator1 first1, InputIterator1 last1,
  360. InputIterator2 first2, InputIterator2 last2, OutputIterator result);
  361. template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
  362. OutputIterator
  363. set_difference(InputIterator1 first1, InputIterator1 last1,
  364. InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
  365. template <class InputIterator1, class InputIterator2, class OutputIterator>
  366. OutputIterator
  367. set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
  368. InputIterator2 first2, InputIterator2 last2, OutputIterator result);
  369. template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
  370. OutputIterator
  371. set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
  372. InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
  373. template <class RandomAccessIterator>
  374. void
  375. push_heap(RandomAccessIterator first, RandomAccessIterator last);
  376. template <class RandomAccessIterator, class Compare>
  377. void
  378. push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  379. template <class RandomAccessIterator>
  380. void
  381. pop_heap(RandomAccessIterator first, RandomAccessIterator last);
  382. template <class RandomAccessIterator, class Compare>
  383. void
  384. pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  385. template <class RandomAccessIterator>
  386. void
  387. make_heap(RandomAccessIterator first, RandomAccessIterator last);
  388. template <class RandomAccessIterator, class Compare>
  389. void
  390. make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  391. template <class RandomAccessIterator>
  392. void
  393. sort_heap(RandomAccessIterator first, RandomAccessIterator last);
  394. template <class RandomAccessIterator, class Compare>
  395. void
  396. sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  397. template <class RandomAccessIterator>
  398. constexpr bool // constexpr in C++20
  399. is_heap(RandomAccessIterator first, RandomAccessiterator last);
  400. template <class RandomAccessIterator, class Compare>
  401. constexpr bool // constexpr in C++20
  402. is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
  403. template <class RandomAccessIterator>
  404. constexpr RandomAccessIterator // constexpr in C++20
  405. is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
  406. template <class RandomAccessIterator, class Compare>
  407. constexpr RandomAccessIterator // constexpr in C++20
  408. is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
  409. template <class ForwardIterator>
  410. ForwardIterator
  411. min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
  412. template <class ForwardIterator, class Compare>
  413. ForwardIterator
  414. min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
  415. template <class T>
  416. const T&
  417. min(const T& a, const T& b); // constexpr in C++14
  418. template <class T, class Compare>
  419. const T&
  420. min(const T& a, const T& b, Compare comp); // constexpr in C++14
  421. template<class T>
  422. T
  423. min(initializer_list<T> t); // constexpr in C++14
  424. template<class T, class Compare>
  425. T
  426. min(initializer_list<T> t, Compare comp); // constexpr in C++14
  427. template<class T>
  428. constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
  429. template<class T, class Compare>
  430. constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
  431. template <class ForwardIterator>
  432. ForwardIterator
  433. max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
  434. template <class ForwardIterator, class Compare>
  435. ForwardIterator
  436. max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
  437. template <class T>
  438. const T&
  439. max(const T& a, const T& b); // constexpr in C++14
  440. template <class T, class Compare>
  441. const T&
  442. max(const T& a, const T& b, Compare comp); // constexpr in C++14
  443. template<class T>
  444. T
  445. max(initializer_list<T> t); // constexpr in C++14
  446. template<class T, class Compare>
  447. T
  448. max(initializer_list<T> t, Compare comp); // constexpr in C++14
  449. template<class ForwardIterator>
  450. pair<ForwardIterator, ForwardIterator>
  451. minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
  452. template<class ForwardIterator, class Compare>
  453. pair<ForwardIterator, ForwardIterator>
  454. minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
  455. template<class T>
  456. pair<const T&, const T&>
  457. minmax(const T& a, const T& b); // constexpr in C++14
  458. template<class T, class Compare>
  459. pair<const T&, const T&>
  460. minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
  461. template<class T>
  462. pair<T, T>
  463. minmax(initializer_list<T> t); // constexpr in C++14
  464. template<class T, class Compare>
  465. pair<T, T>
  466. minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
  467. template <class InputIterator1, class InputIterator2>
  468. constexpr bool // constexpr in C++20
  469. lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
  470. template <class InputIterator1, class InputIterator2, class Compare>
  471. constexpr bool // constexpr in C++20
  472. lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
  473. InputIterator2 first2, InputIterator2 last2, Compare comp);
  474. template <class BidirectionalIterator>
  475. bool
  476. next_permutation(BidirectionalIterator first, BidirectionalIterator last);
  477. template <class BidirectionalIterator, class Compare>
  478. bool
  479. next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
  480. template <class BidirectionalIterator>
  481. bool
  482. prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
  483. template <class BidirectionalIterator, class Compare>
  484. bool
  485. prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
  486. } // std
  487. */
  488. #include <__config>
  489. #include <initializer_list>
  490. #include <type_traits>
  491. #include <cstring>
  492. #include <utility> // needed to provide swap_ranges.
  493. #include <memory>
  494. #include <functional>
  495. #include <iterator>
  496. #include <cstddef>
  497. #include <bit>
  498. #include <version>
  499. #include <__debug>
  500. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  501. #pragma GCC system_header
  502. #endif
  503. _LIBCPP_PUSH_MACROS
  504. #include <__undef_macros>
  505. _LIBCPP_BEGIN_NAMESPACE_STD
  506. // I'd like to replace these with _VSTD::equal_to<void>, but can't because:
  507. // * That only works with C++14 and later, and
  508. // * We haven't included <functional> here.
  509. template <class _T1, class _T2 = _T1>
  510. struct __equal_to
  511. {
  512. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
  513. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
  514. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
  515. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
  516. };
  517. template <class _T1>
  518. struct __equal_to<_T1, _T1>
  519. {
  520. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  521. bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
  522. };
  523. template <class _T1>
  524. struct __equal_to<const _T1, _T1>
  525. {
  526. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  527. bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
  528. };
  529. template <class _T1>
  530. struct __equal_to<_T1, const _T1>
  531. {
  532. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  533. bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
  534. };
  535. template <class _T1, class _T2 = _T1>
  536. struct __less
  537. {
  538. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  539. bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
  540. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  541. bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
  542. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  543. bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
  544. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  545. bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
  546. };
  547. template <class _T1>
  548. struct __less<_T1, _T1>
  549. {
  550. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  551. bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
  552. };
  553. template <class _T1>
  554. struct __less<const _T1, _T1>
  555. {
  556. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  557. bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
  558. };
  559. template <class _T1>
  560. struct __less<_T1, const _T1>
  561. {
  562. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  563. bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
  564. };
  565. template <class _Predicate>
  566. class __invert // invert the sense of a comparison
  567. {
  568. private:
  569. _Predicate __p_;
  570. public:
  571. _LIBCPP_INLINE_VISIBILITY __invert() {}
  572. _LIBCPP_INLINE_VISIBILITY
  573. explicit __invert(_Predicate __p) : __p_(__p) {}
  574. template <class _T1>
  575. _LIBCPP_INLINE_VISIBILITY
  576. bool operator()(const _T1& __x) {return !__p_(__x);}
  577. template <class _T1, class _T2>
  578. _LIBCPP_INLINE_VISIBILITY
  579. bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
  580. };
  581. // Perform division by two quickly for positive integers (llvm.org/PR39129)
  582. template <typename _Integral>
  583. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  584. typename enable_if
  585. <
  586. is_integral<_Integral>::value,
  587. _Integral
  588. >::type
  589. __half_positive(_Integral __value)
  590. {
  591. return static_cast<_Integral>(static_cast<typename make_unsigned<_Integral>::type>(__value) / 2);
  592. }
  593. template <typename _Tp>
  594. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  595. typename enable_if
  596. <
  597. !is_integral<_Tp>::value,
  598. _Tp
  599. >::type
  600. __half_positive(_Tp __value)
  601. {
  602. return __value / 2;
  603. }
  604. #ifdef _LIBCPP_DEBUG
  605. template <class _Compare>
  606. struct __debug_less
  607. {
  608. _Compare &__comp_;
  609. _LIBCPP_CONSTEXPR_AFTER_CXX17
  610. __debug_less(_Compare& __c) : __comp_(__c) {}
  611. template <class _Tp, class _Up>
  612. _LIBCPP_CONSTEXPR_AFTER_CXX17
  613. bool operator()(const _Tp& __x, const _Up& __y)
  614. {
  615. bool __r = __comp_(__x, __y);
  616. if (__r)
  617. __do_compare_assert(0, __y, __x);
  618. return __r;
  619. }
  620. template <class _Tp, class _Up>
  621. _LIBCPP_CONSTEXPR_AFTER_CXX17
  622. bool operator()(_Tp& __x, _Up& __y)
  623. {
  624. bool __r = __comp_(__x, __y);
  625. if (__r)
  626. __do_compare_assert(0, __y, __x);
  627. return __r;
  628. }
  629. template <class _LHS, class _RHS>
  630. _LIBCPP_CONSTEXPR_AFTER_CXX17
  631. inline _LIBCPP_INLINE_VISIBILITY
  632. decltype((void)_VSTD::declval<_Compare&>()(
  633. _VSTD::declval<_LHS &>(), _VSTD::declval<_RHS &>()))
  634. __do_compare_assert(int, _LHS & __l, _RHS & __r) {
  635. _LIBCPP_ASSERT(!__comp_(__l, __r),
  636. "Comparator does not induce a strict weak ordering");
  637. }
  638. template <class _LHS, class _RHS>
  639. _LIBCPP_CONSTEXPR_AFTER_CXX17
  640. inline _LIBCPP_INLINE_VISIBILITY
  641. void __do_compare_assert(long, _LHS &, _RHS &) {}
  642. };
  643. #endif // _LIBCPP_DEBUG
  644. template <class _Comp>
  645. struct __comp_ref_type {
  646. // Pass the comparator by lvalue reference. Or in debug mode, using a
  647. // debugging wrapper that stores a reference.
  648. #ifndef _LIBCPP_DEBUG
  649. typedef typename add_lvalue_reference<_Comp>::type type;
  650. #else
  651. typedef __debug_less<_Comp> type;
  652. #endif
  653. };
  654. // all_of
  655. template <class _InputIterator, class _Predicate>
  656. _LIBCPP_NODISCARD_EXT inline
  657. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  658. bool
  659. all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  660. {
  661. for (; __first != __last; ++__first)
  662. if (!__pred(*__first))
  663. return false;
  664. return true;
  665. }
  666. // any_of
  667. template <class _InputIterator, class _Predicate>
  668. _LIBCPP_NODISCARD_EXT inline
  669. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  670. bool
  671. any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  672. {
  673. for (; __first != __last; ++__first)
  674. if (__pred(*__first))
  675. return true;
  676. return false;
  677. }
  678. // none_of
  679. template <class _InputIterator, class _Predicate>
  680. _LIBCPP_NODISCARD_EXT inline
  681. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  682. bool
  683. none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  684. {
  685. for (; __first != __last; ++__first)
  686. if (__pred(*__first))
  687. return false;
  688. return true;
  689. }
  690. // for_each
  691. template <class _InputIterator, class _Function>
  692. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  693. _Function
  694. for_each(_InputIterator __first, _InputIterator __last, _Function __f)
  695. {
  696. for (; __first != __last; ++__first)
  697. __f(*__first);
  698. return __f;
  699. }
  700. #if _LIBCPP_STD_VER > 14
  701. // for_each_n
  702. template <class _InputIterator, class _Size, class _Function>
  703. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  704. _InputIterator
  705. for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
  706. {
  707. typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
  708. _IntegralSize __n = __orig_n;
  709. while (__n > 0)
  710. {
  711. __f(*__first);
  712. ++__first;
  713. --__n;
  714. }
  715. return __first;
  716. }
  717. #endif
  718. // find
  719. template <class _InputIterator, class _Tp>
  720. _LIBCPP_NODISCARD_EXT inline
  721. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  722. _InputIterator
  723. find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
  724. {
  725. for (; __first != __last; ++__first)
  726. if (*__first == __value_)
  727. break;
  728. return __first;
  729. }
  730. // find_if
  731. template <class _InputIterator, class _Predicate>
  732. _LIBCPP_NODISCARD_EXT inline
  733. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  734. _InputIterator
  735. find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  736. {
  737. for (; __first != __last; ++__first)
  738. if (__pred(*__first))
  739. break;
  740. return __first;
  741. }
  742. // find_if_not
  743. template<class _InputIterator, class _Predicate>
  744. _LIBCPP_NODISCARD_EXT inline
  745. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  746. _InputIterator
  747. find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  748. {
  749. for (; __first != __last; ++__first)
  750. if (!__pred(*__first))
  751. break;
  752. return __first;
  753. }
  754. // find_end
  755. template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
  756. _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
  757. __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  758. _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
  759. forward_iterator_tag, forward_iterator_tag)
  760. {
  761. // modeled after search algorithm
  762. _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
  763. if (__first2 == __last2)
  764. return __r;
  765. while (true)
  766. {
  767. while (true)
  768. {
  769. if (__first1 == __last1) // if source exhausted return last correct answer
  770. return __r; // (or __last1 if never found)
  771. if (__pred(*__first1, *__first2))
  772. break;
  773. ++__first1;
  774. }
  775. // *__first1 matches *__first2, now match elements after here
  776. _ForwardIterator1 __m1 = __first1;
  777. _ForwardIterator2 __m2 = __first2;
  778. while (true)
  779. {
  780. if (++__m2 == __last2)
  781. { // Pattern exhaused, record answer and search for another one
  782. __r = __first1;
  783. ++__first1;
  784. break;
  785. }
  786. if (++__m1 == __last1) // Source exhausted, return last answer
  787. return __r;
  788. if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
  789. {
  790. ++__first1;
  791. break;
  792. } // else there is a match, check next elements
  793. }
  794. }
  795. }
  796. template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
  797. _LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
  798. __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
  799. _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
  800. bidirectional_iterator_tag, bidirectional_iterator_tag)
  801. {
  802. // modeled after search algorithm (in reverse)
  803. if (__first2 == __last2)
  804. return __last1; // Everything matches an empty sequence
  805. _BidirectionalIterator1 __l1 = __last1;
  806. _BidirectionalIterator2 __l2 = __last2;
  807. --__l2;
  808. while (true)
  809. {
  810. // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
  811. while (true)
  812. {
  813. if (__first1 == __l1) // return __last1 if no element matches *__first2
  814. return __last1;
  815. if (__pred(*--__l1, *__l2))
  816. break;
  817. }
  818. // *__l1 matches *__l2, now match elements before here
  819. _BidirectionalIterator1 __m1 = __l1;
  820. _BidirectionalIterator2 __m2 = __l2;
  821. while (true)
  822. {
  823. if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
  824. return __m1;
  825. if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
  826. return __last1;
  827. if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
  828. {
  829. break;
  830. } // else there is a match, check next elements
  831. }
  832. }
  833. }
  834. template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
  835. _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
  836. __find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
  837. _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
  838. random_access_iterator_tag, random_access_iterator_tag)
  839. {
  840. // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
  841. typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
  842. if (__len2 == 0)
  843. return __last1;
  844. typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
  845. if (__len1 < __len2)
  846. return __last1;
  847. const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
  848. _RandomAccessIterator1 __l1 = __last1;
  849. _RandomAccessIterator2 __l2 = __last2;
  850. --__l2;
  851. while (true)
  852. {
  853. while (true)
  854. {
  855. if (__s == __l1)
  856. return __last1;
  857. if (__pred(*--__l1, *__l2))
  858. break;
  859. }
  860. _RandomAccessIterator1 __m1 = __l1;
  861. _RandomAccessIterator2 __m2 = __l2;
  862. while (true)
  863. {
  864. if (__m2 == __first2)
  865. return __m1;
  866. // no need to check range on __m1 because __s guarantees we have enough source
  867. if (!__pred(*--__m1, *--__m2))
  868. {
  869. break;
  870. }
  871. }
  872. }
  873. }
  874. template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
  875. _LIBCPP_NODISCARD_EXT inline
  876. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  877. _ForwardIterator1
  878. find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  879. _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
  880. {
  881. return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
  882. (__first1, __last1, __first2, __last2, __pred,
  883. typename iterator_traits<_ForwardIterator1>::iterator_category(),
  884. typename iterator_traits<_ForwardIterator2>::iterator_category());
  885. }
  886. template <class _ForwardIterator1, class _ForwardIterator2>
  887. _LIBCPP_NODISCARD_EXT inline
  888. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  889. _ForwardIterator1
  890. find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  891. _ForwardIterator2 __first2, _ForwardIterator2 __last2)
  892. {
  893. typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
  894. typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
  895. return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
  896. }
  897. // find_first_of
  898. template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
  899. _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
  900. __find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  901. _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
  902. {
  903. for (; __first1 != __last1; ++__first1)
  904. for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
  905. if (__pred(*__first1, *__j))
  906. return __first1;
  907. return __last1;
  908. }
  909. template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
  910. _LIBCPP_NODISCARD_EXT inline
  911. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  912. _ForwardIterator1
  913. find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  914. _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
  915. {
  916. return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
  917. }
  918. template <class _ForwardIterator1, class _ForwardIterator2>
  919. _LIBCPP_NODISCARD_EXT inline
  920. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  921. _ForwardIterator1
  922. find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  923. _ForwardIterator2 __first2, _ForwardIterator2 __last2)
  924. {
  925. typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
  926. typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
  927. return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
  928. }
  929. // adjacent_find
  930. template <class _ForwardIterator, class _BinaryPredicate>
  931. _LIBCPP_NODISCARD_EXT inline
  932. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  933. _ForwardIterator
  934. adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
  935. {
  936. if (__first != __last)
  937. {
  938. _ForwardIterator __i = __first;
  939. while (++__i != __last)
  940. {
  941. if (__pred(*__first, *__i))
  942. return __first;
  943. __first = __i;
  944. }
  945. }
  946. return __last;
  947. }
  948. template <class _ForwardIterator>
  949. _LIBCPP_NODISCARD_EXT inline
  950. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  951. _ForwardIterator
  952. adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
  953. {
  954. typedef typename iterator_traits<_ForwardIterator>::value_type __v;
  955. return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
  956. }
  957. // count
  958. template <class _InputIterator, class _Tp>
  959. _LIBCPP_NODISCARD_EXT inline
  960. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  961. typename iterator_traits<_InputIterator>::difference_type
  962. count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
  963. {
  964. typename iterator_traits<_InputIterator>::difference_type __r(0);
  965. for (; __first != __last; ++__first)
  966. if (*__first == __value_)
  967. ++__r;
  968. return __r;
  969. }
  970. // count_if
  971. template <class _InputIterator, class _Predicate>
  972. _LIBCPP_NODISCARD_EXT inline
  973. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  974. typename iterator_traits<_InputIterator>::difference_type
  975. count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  976. {
  977. typename iterator_traits<_InputIterator>::difference_type __r(0);
  978. for (; __first != __last; ++__first)
  979. if (__pred(*__first))
  980. ++__r;
  981. return __r;
  982. }
  983. // mismatch
  984. template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
  985. _LIBCPP_NODISCARD_EXT inline
  986. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  987. pair<_InputIterator1, _InputIterator2>
  988. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  989. _InputIterator2 __first2, _BinaryPredicate __pred)
  990. {
  991. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  992. if (!__pred(*__first1, *__first2))
  993. break;
  994. return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
  995. }
  996. template <class _InputIterator1, class _InputIterator2>
  997. _LIBCPP_NODISCARD_EXT inline
  998. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  999. pair<_InputIterator1, _InputIterator2>
  1000. mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
  1001. {
  1002. typedef typename iterator_traits<_InputIterator1>::value_type __v1;
  1003. typedef typename iterator_traits<_InputIterator2>::value_type __v2;
  1004. return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
  1005. }
  1006. #if _LIBCPP_STD_VER > 11
  1007. template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
  1008. _LIBCPP_NODISCARD_EXT inline
  1009. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1010. pair<_InputIterator1, _InputIterator2>
  1011. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1012. _InputIterator2 __first2, _InputIterator2 __last2,
  1013. _BinaryPredicate __pred)
  1014. {
  1015. for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
  1016. if (!__pred(*__first1, *__first2))
  1017. break;
  1018. return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
  1019. }
  1020. template <class _InputIterator1, class _InputIterator2>
  1021. _LIBCPP_NODISCARD_EXT inline
  1022. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1023. pair<_InputIterator1, _InputIterator2>
  1024. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1025. _InputIterator2 __first2, _InputIterator2 __last2)
  1026. {
  1027. typedef typename iterator_traits<_InputIterator1>::value_type __v1;
  1028. typedef typename iterator_traits<_InputIterator2>::value_type __v2;
  1029. return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
  1030. }
  1031. #endif
  1032. // equal
  1033. template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
  1034. _LIBCPP_NODISCARD_EXT inline
  1035. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1036. bool
  1037. equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
  1038. {
  1039. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  1040. if (!__pred(*__first1, *__first2))
  1041. return false;
  1042. return true;
  1043. }
  1044. template <class _InputIterator1, class _InputIterator2>
  1045. _LIBCPP_NODISCARD_EXT inline
  1046. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1047. bool
  1048. equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
  1049. {
  1050. typedef typename iterator_traits<_InputIterator1>::value_type __v1;
  1051. typedef typename iterator_traits<_InputIterator2>::value_type __v2;
  1052. return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
  1053. }
  1054. #if _LIBCPP_STD_VER > 11
  1055. template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
  1056. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1057. bool
  1058. __equal(_InputIterator1 __first1, _InputIterator1 __last1,
  1059. _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
  1060. input_iterator_tag, input_iterator_tag )
  1061. {
  1062. for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
  1063. if (!__pred(*__first1, *__first2))
  1064. return false;
  1065. return __first1 == __last1 && __first2 == __last2;
  1066. }
  1067. template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
  1068. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1069. bool
  1070. __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
  1071. _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
  1072. random_access_iterator_tag, random_access_iterator_tag )
  1073. {
  1074. if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
  1075. return false;
  1076. return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
  1077. typename add_lvalue_reference<_BinaryPredicate>::type>
  1078. (__first1, __last1, __first2, __pred );
  1079. }
  1080. template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
  1081. _LIBCPP_NODISCARD_EXT inline
  1082. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1083. bool
  1084. equal(_InputIterator1 __first1, _InputIterator1 __last1,
  1085. _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
  1086. {
  1087. return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
  1088. (__first1, __last1, __first2, __last2, __pred,
  1089. typename iterator_traits<_InputIterator1>::iterator_category(),
  1090. typename iterator_traits<_InputIterator2>::iterator_category());
  1091. }
  1092. template <class _InputIterator1, class _InputIterator2>
  1093. _LIBCPP_NODISCARD_EXT inline
  1094. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1095. bool
  1096. equal(_InputIterator1 __first1, _InputIterator1 __last1,
  1097. _InputIterator2 __first2, _InputIterator2 __last2)
  1098. {
  1099. typedef typename iterator_traits<_InputIterator1>::value_type __v1;
  1100. typedef typename iterator_traits<_InputIterator2>::value_type __v2;
  1101. return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
  1102. typename iterator_traits<_InputIterator1>::iterator_category(),
  1103. typename iterator_traits<_InputIterator2>::iterator_category());
  1104. }
  1105. #endif
  1106. // is_permutation
  1107. template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
  1108. _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  1109. is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  1110. _ForwardIterator2 __first2, _BinaryPredicate __pred)
  1111. {
  1112. // shorten sequences as much as possible by lopping of any equal prefix
  1113. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  1114. if (!__pred(*__first1, *__first2))
  1115. break;
  1116. if (__first1 == __last1)
  1117. return true;
  1118. // __first1 != __last1 && *__first1 != *__first2
  1119. typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
  1120. _D1 __l1 = _VSTD::distance(__first1, __last1);
  1121. if (__l1 == _D1(1))
  1122. return false;
  1123. _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
  1124. // For each element in [f1, l1) see if there are the same number of
  1125. // equal elements in [f2, l2)
  1126. for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
  1127. {
  1128. // Have we already counted the number of *__i in [f1, l1)?
  1129. _ForwardIterator1 __match = __first1;
  1130. for (; __match != __i; ++__match)
  1131. if (__pred(*__match, *__i))
  1132. break;
  1133. if (__match == __i) {
  1134. // Count number of *__i in [f2, l2)
  1135. _D1 __c2 = 0;
  1136. for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
  1137. if (__pred(*__i, *__j))
  1138. ++__c2;
  1139. if (__c2 == 0)
  1140. return false;
  1141. // Count number of *__i in [__i, l1) (we can start with 1)
  1142. _D1 __c1 = 1;
  1143. for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
  1144. if (__pred(*__i, *__j))
  1145. ++__c1;
  1146. if (__c1 != __c2)
  1147. return false;
  1148. }
  1149. }
  1150. return true;
  1151. }
  1152. template<class _ForwardIterator1, class _ForwardIterator2>
  1153. _LIBCPP_NODISCARD_EXT inline
  1154. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1155. bool
  1156. is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  1157. _ForwardIterator2 __first2)
  1158. {
  1159. typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
  1160. typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
  1161. return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
  1162. }
  1163. #if _LIBCPP_STD_VER > 11
  1164. template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
  1165. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  1166. __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  1167. _ForwardIterator2 __first2, _ForwardIterator2 __last2,
  1168. _BinaryPredicate __pred,
  1169. forward_iterator_tag, forward_iterator_tag )
  1170. {
  1171. // shorten sequences as much as possible by lopping of any equal prefix
  1172. for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
  1173. if (!__pred(*__first1, *__first2))
  1174. break;
  1175. if (__first1 == __last1)
  1176. return __first2 == __last2;
  1177. else if (__first2 == __last2)
  1178. return false;
  1179. typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
  1180. _D1 __l1 = _VSTD::distance(__first1, __last1);
  1181. typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
  1182. _D2 __l2 = _VSTD::distance(__first2, __last2);
  1183. if (__l1 != __l2)
  1184. return false;
  1185. // For each element in [f1, l1) see if there are the same number of
  1186. // equal elements in [f2, l2)
  1187. for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
  1188. {
  1189. // Have we already counted the number of *__i in [f1, l1)?
  1190. _ForwardIterator1 __match = __first1;
  1191. for (; __match != __i; ++__match)
  1192. if (__pred(*__match, *__i))
  1193. break;
  1194. if (__match == __i) {
  1195. // Count number of *__i in [f2, l2)
  1196. _D1 __c2 = 0;
  1197. for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
  1198. if (__pred(*__i, *__j))
  1199. ++__c2;
  1200. if (__c2 == 0)
  1201. return false;
  1202. // Count number of *__i in [__i, l1) (we can start with 1)
  1203. _D1 __c1 = 1;
  1204. for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
  1205. if (__pred(*__i, *__j))
  1206. ++__c1;
  1207. if (__c1 != __c2)
  1208. return false;
  1209. }
  1210. }
  1211. return true;
  1212. }
  1213. template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
  1214. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  1215. __is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
  1216. _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
  1217. _BinaryPredicate __pred,
  1218. random_access_iterator_tag, random_access_iterator_tag )
  1219. {
  1220. if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
  1221. return false;
  1222. return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
  1223. typename add_lvalue_reference<_BinaryPredicate>::type>
  1224. (__first1, __last1, __first2, __pred );
  1225. }
  1226. template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
  1227. _LIBCPP_NODISCARD_EXT inline
  1228. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1229. bool
  1230. is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  1231. _ForwardIterator2 __first2, _ForwardIterator2 __last2,
  1232. _BinaryPredicate __pred )
  1233. {
  1234. return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
  1235. (__first1, __last1, __first2, __last2, __pred,
  1236. typename iterator_traits<_ForwardIterator1>::iterator_category(),
  1237. typename iterator_traits<_ForwardIterator2>::iterator_category());
  1238. }
  1239. template<class _ForwardIterator1, class _ForwardIterator2>
  1240. _LIBCPP_NODISCARD_EXT inline
  1241. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1242. bool
  1243. is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  1244. _ForwardIterator2 __first2, _ForwardIterator2 __last2)
  1245. {
  1246. typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
  1247. typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
  1248. return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
  1249. __equal_to<__v1, __v2>(),
  1250. typename iterator_traits<_ForwardIterator1>::iterator_category(),
  1251. typename iterator_traits<_ForwardIterator2>::iterator_category());
  1252. }
  1253. #endif
  1254. // search
  1255. // __search is in <functional>
  1256. template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
  1257. _LIBCPP_NODISCARD_EXT inline
  1258. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1259. _ForwardIterator1
  1260. search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  1261. _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
  1262. {
  1263. return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
  1264. (__first1, __last1, __first2, __last2, __pred,
  1265. typename iterator_traits<_ForwardIterator1>::iterator_category(),
  1266. typename iterator_traits<_ForwardIterator2>::iterator_category())
  1267. .first;
  1268. }
  1269. template <class _ForwardIterator1, class _ForwardIterator2>
  1270. _LIBCPP_NODISCARD_EXT inline
  1271. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1272. _ForwardIterator1
  1273. search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  1274. _ForwardIterator2 __first2, _ForwardIterator2 __last2)
  1275. {
  1276. typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
  1277. typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
  1278. return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
  1279. }
  1280. #if _LIBCPP_STD_VER > 14
  1281. template <class _ForwardIterator, class _Searcher>
  1282. _LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1283. _ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
  1284. { return __s(__f, __l).first; }
  1285. #endif
  1286. // search_n
  1287. template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
  1288. _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
  1289. __search_n(_ForwardIterator __first, _ForwardIterator __last,
  1290. _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
  1291. {
  1292. if (__count <= 0)
  1293. return __first;
  1294. while (true)
  1295. {
  1296. // Find first element in sequence that matchs __value_, with a mininum of loop checks
  1297. while (true)
  1298. {
  1299. if (__first == __last) // return __last if no element matches __value_
  1300. return __last;
  1301. if (__pred(*__first, __value_))
  1302. break;
  1303. ++__first;
  1304. }
  1305. // *__first matches __value_, now match elements after here
  1306. _ForwardIterator __m = __first;
  1307. _Size __c(0);
  1308. while (true)
  1309. {
  1310. if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
  1311. return __first;
  1312. if (++__m == __last) // Otherwise if source exhaused, pattern not found
  1313. return __last;
  1314. if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
  1315. {
  1316. __first = __m;
  1317. ++__first;
  1318. break;
  1319. } // else there is a match, check next elements
  1320. }
  1321. }
  1322. }
  1323. template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
  1324. _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
  1325. __search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
  1326. _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
  1327. {
  1328. if (__count <= 0)
  1329. return __first;
  1330. _Size __len = static_cast<_Size>(__last - __first);
  1331. if (__len < __count)
  1332. return __last;
  1333. const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
  1334. while (true)
  1335. {
  1336. // Find first element in sequence that matchs __value_, with a mininum of loop checks
  1337. while (true)
  1338. {
  1339. if (__first >= __s) // return __last if no element matches __value_
  1340. return __last;
  1341. if (__pred(*__first, __value_))
  1342. break;
  1343. ++__first;
  1344. }
  1345. // *__first matches __value_, now match elements after here
  1346. _RandomAccessIterator __m = __first;
  1347. _Size __c(0);
  1348. while (true)
  1349. {
  1350. if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
  1351. return __first;
  1352. ++__m; // no need to check range on __m because __s guarantees we have enough source
  1353. if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
  1354. {
  1355. __first = __m;
  1356. ++__first;
  1357. break;
  1358. } // else there is a match, check next elements
  1359. }
  1360. }
  1361. }
  1362. template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
  1363. _LIBCPP_NODISCARD_EXT inline
  1364. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1365. _ForwardIterator
  1366. search_n(_ForwardIterator __first, _ForwardIterator __last,
  1367. _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
  1368. {
  1369. return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
  1370. (__first, __last, __convert_to_integral(__count), __value_, __pred,
  1371. typename iterator_traits<_ForwardIterator>::iterator_category());
  1372. }
  1373. template <class _ForwardIterator, class _Size, class _Tp>
  1374. _LIBCPP_NODISCARD_EXT inline
  1375. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1376. _ForwardIterator
  1377. search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
  1378. {
  1379. typedef typename iterator_traits<_ForwardIterator>::value_type __v;
  1380. return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
  1381. __value_, __equal_to<__v, _Tp>());
  1382. }
  1383. // copy
  1384. template <class _Iter>
  1385. inline _LIBCPP_INLINE_VISIBILITY
  1386. _Iter
  1387. __unwrap_iter(_Iter __i)
  1388. {
  1389. return __i;
  1390. }
  1391. template <class _Tp>
  1392. inline _LIBCPP_INLINE_VISIBILITY
  1393. typename enable_if
  1394. <
  1395. is_trivially_copy_assignable<_Tp>::value,
  1396. _Tp*
  1397. >::type
  1398. __unwrap_iter(move_iterator<_Tp*> __i)
  1399. {
  1400. return __i.base();
  1401. }
  1402. #if _LIBCPP_DEBUG_LEVEL < 2
  1403. template <class _Tp>
  1404. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
  1405. typename enable_if
  1406. <
  1407. is_trivially_copy_assignable<_Tp>::value,
  1408. _Tp*
  1409. >::type
  1410. __unwrap_iter(__wrap_iter<_Tp*> __i)
  1411. {
  1412. return __i.base();
  1413. }
  1414. template <class _Tp>
  1415. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
  1416. typename enable_if
  1417. <
  1418. is_trivially_copy_assignable<_Tp>::value,
  1419. const _Tp*
  1420. >::type
  1421. __unwrap_iter(__wrap_iter<const _Tp*> __i)
  1422. {
  1423. return __i.base();
  1424. }
  1425. #else
  1426. template <class _Tp>
  1427. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
  1428. typename enable_if
  1429. <
  1430. is_trivially_copy_assignable<_Tp>::value,
  1431. __wrap_iter<_Tp*>
  1432. >::type
  1433. __unwrap_iter(__wrap_iter<_Tp*> __i)
  1434. {
  1435. return __i;
  1436. }
  1437. #endif // _LIBCPP_DEBUG_LEVEL < 2
  1438. template <class _InputIterator, class _OutputIterator>
  1439. inline _LIBCPP_INLINE_VISIBILITY
  1440. _OutputIterator
  1441. __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  1442. {
  1443. for (; __first != __last; ++__first, (void) ++__result)
  1444. *__result = *__first;
  1445. return __result;
  1446. }
  1447. template <class _Tp, class _Up>
  1448. inline _LIBCPP_INLINE_VISIBILITY
  1449. typename enable_if
  1450. <
  1451. is_same<typename remove_const<_Tp>::type, _Up>::value &&
  1452. is_trivially_copy_assignable<_Up>::value,
  1453. _Up*
  1454. >::type
  1455. __copy(_Tp* __first, _Tp* __last, _Up* __result)
  1456. {
  1457. const size_t __n = static_cast<size_t>(__last - __first);
  1458. if (__n > 0)
  1459. _VSTD::memmove(__result, __first, __n * sizeof(_Up));
  1460. return __result + __n;
  1461. }
  1462. template <class _InputIterator, class _OutputIterator>
  1463. inline _LIBCPP_INLINE_VISIBILITY
  1464. _OutputIterator
  1465. copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  1466. {
  1467. return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
  1468. }
  1469. // copy_backward
  1470. template <class _BidirectionalIterator, class _OutputIterator>
  1471. inline _LIBCPP_INLINE_VISIBILITY
  1472. _OutputIterator
  1473. __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
  1474. {
  1475. while (__first != __last)
  1476. *--__result = *--__last;
  1477. return __result;
  1478. }
  1479. template <class _Tp, class _Up>
  1480. inline _LIBCPP_INLINE_VISIBILITY
  1481. typename enable_if
  1482. <
  1483. is_same<typename remove_const<_Tp>::type, _Up>::value &&
  1484. is_trivially_copy_assignable<_Up>::value,
  1485. _Up*
  1486. >::type
  1487. __copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
  1488. {
  1489. const size_t __n = static_cast<size_t>(__last - __first);
  1490. if (__n > 0)
  1491. {
  1492. __result -= __n;
  1493. _VSTD::memmove(__result, __first, __n * sizeof(_Up));
  1494. }
  1495. return __result;
  1496. }
  1497. template <class _BidirectionalIterator1, class _BidirectionalIterator2>
  1498. inline _LIBCPP_INLINE_VISIBILITY
  1499. _BidirectionalIterator2
  1500. copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
  1501. _BidirectionalIterator2 __result)
  1502. {
  1503. return _VSTD::__copy_backward(__unwrap_iter(__first),
  1504. __unwrap_iter(__last),
  1505. __unwrap_iter(__result));
  1506. }
  1507. // copy_if
  1508. template<class _InputIterator, class _OutputIterator, class _Predicate>
  1509. inline _LIBCPP_INLINE_VISIBILITY
  1510. _OutputIterator
  1511. copy_if(_InputIterator __first, _InputIterator __last,
  1512. _OutputIterator __result, _Predicate __pred)
  1513. {
  1514. for (; __first != __last; ++__first)
  1515. {
  1516. if (__pred(*__first))
  1517. {
  1518. *__result = *__first;
  1519. ++__result;
  1520. }
  1521. }
  1522. return __result;
  1523. }
  1524. // copy_n
  1525. template<class _InputIterator, class _Size, class _OutputIterator>
  1526. inline _LIBCPP_INLINE_VISIBILITY
  1527. typename enable_if
  1528. <
  1529. __is_input_iterator<_InputIterator>::value &&
  1530. !__is_random_access_iterator<_InputIterator>::value,
  1531. _OutputIterator
  1532. >::type
  1533. copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
  1534. {
  1535. typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
  1536. _IntegralSize __n = __orig_n;
  1537. if (__n > 0)
  1538. {
  1539. *__result = *__first;
  1540. ++__result;
  1541. for (--__n; __n > 0; --__n)
  1542. {
  1543. ++__first;
  1544. *__result = *__first;
  1545. ++__result;
  1546. }
  1547. }
  1548. return __result;
  1549. }
  1550. template<class _InputIterator, class _Size, class _OutputIterator>
  1551. inline _LIBCPP_INLINE_VISIBILITY
  1552. typename enable_if
  1553. <
  1554. __is_random_access_iterator<_InputIterator>::value,
  1555. _OutputIterator
  1556. >::type
  1557. copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
  1558. {
  1559. typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
  1560. _IntegralSize __n = __orig_n;
  1561. return _VSTD::copy(__first, __first + __n, __result);
  1562. }
  1563. // move
  1564. template <class _InputIterator, class _OutputIterator>
  1565. inline _LIBCPP_INLINE_VISIBILITY
  1566. _OutputIterator
  1567. __move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  1568. {
  1569. for (; __first != __last; ++__first, (void) ++__result)
  1570. *__result = _VSTD::move(*__first);
  1571. return __result;
  1572. }
  1573. template <class _Tp, class _Up>
  1574. inline _LIBCPP_INLINE_VISIBILITY
  1575. typename enable_if
  1576. <
  1577. is_same<typename remove_const<_Tp>::type, _Up>::value &&
  1578. is_trivially_copy_assignable<_Up>::value,
  1579. _Up*
  1580. >::type
  1581. __move(_Tp* __first, _Tp* __last, _Up* __result)
  1582. {
  1583. const size_t __n = static_cast<size_t>(__last - __first);
  1584. if (__n > 0)
  1585. _VSTD::memmove(__result, __first, __n * sizeof(_Up));
  1586. return __result + __n;
  1587. }
  1588. template <class _InputIterator, class _OutputIterator>
  1589. inline _LIBCPP_INLINE_VISIBILITY
  1590. _OutputIterator
  1591. move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  1592. {
  1593. return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
  1594. }
  1595. // move_backward
  1596. template <class _InputIterator, class _OutputIterator>
  1597. inline _LIBCPP_INLINE_VISIBILITY
  1598. _OutputIterator
  1599. __move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  1600. {
  1601. while (__first != __last)
  1602. *--__result = _VSTD::move(*--__last);
  1603. return __result;
  1604. }
  1605. template <class _Tp, class _Up>
  1606. inline _LIBCPP_INLINE_VISIBILITY
  1607. typename enable_if
  1608. <
  1609. is_same<typename remove_const<_Tp>::type, _Up>::value &&
  1610. is_trivially_copy_assignable<_Up>::value,
  1611. _Up*
  1612. >::type
  1613. __move_backward(_Tp* __first, _Tp* __last, _Up* __result)
  1614. {
  1615. const size_t __n = static_cast<size_t>(__last - __first);
  1616. if (__n > 0)
  1617. {
  1618. __result -= __n;
  1619. _VSTD::memmove(__result, __first, __n * sizeof(_Up));
  1620. }
  1621. return __result;
  1622. }
  1623. template <class _BidirectionalIterator1, class _BidirectionalIterator2>
  1624. inline _LIBCPP_INLINE_VISIBILITY
  1625. _BidirectionalIterator2
  1626. move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
  1627. _BidirectionalIterator2 __result)
  1628. {
  1629. return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
  1630. }
  1631. // iter_swap
  1632. // moved to <type_traits> for better swap / noexcept support
  1633. // transform
  1634. template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
  1635. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1636. _OutputIterator
  1637. transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
  1638. {
  1639. for (; __first != __last; ++__first, (void) ++__result)
  1640. *__result = __op(*__first);
  1641. return __result;
  1642. }
  1643. template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
  1644. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1645. _OutputIterator
  1646. transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
  1647. _OutputIterator __result, _BinaryOperation __binary_op)
  1648. {
  1649. for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
  1650. *__result = __binary_op(*__first1, *__first2);
  1651. return __result;
  1652. }
  1653. // replace
  1654. template <class _ForwardIterator, class _Tp>
  1655. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1656. void
  1657. replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
  1658. {
  1659. for (; __first != __last; ++__first)
  1660. if (*__first == __old_value)
  1661. *__first = __new_value;
  1662. }
  1663. // replace_if
  1664. template <class _ForwardIterator, class _Predicate, class _Tp>
  1665. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1666. void
  1667. replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
  1668. {
  1669. for (; __first != __last; ++__first)
  1670. if (__pred(*__first))
  1671. *__first = __new_value;
  1672. }
  1673. // replace_copy
  1674. template <class _InputIterator, class _OutputIterator, class _Tp>
  1675. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1676. _OutputIterator
  1677. replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
  1678. const _Tp& __old_value, const _Tp& __new_value)
  1679. {
  1680. for (; __first != __last; ++__first, (void) ++__result)
  1681. if (*__first == __old_value)
  1682. *__result = __new_value;
  1683. else
  1684. *__result = *__first;
  1685. return __result;
  1686. }
  1687. // replace_copy_if
  1688. template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
  1689. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1690. _OutputIterator
  1691. replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
  1692. _Predicate __pred, const _Tp& __new_value)
  1693. {
  1694. for (; __first != __last; ++__first, (void) ++__result)
  1695. if (__pred(*__first))
  1696. *__result = __new_value;
  1697. else
  1698. *__result = *__first;
  1699. return __result;
  1700. }
  1701. // fill_n
  1702. template <class _OutputIterator, class _Size, class _Tp>
  1703. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1704. _OutputIterator
  1705. __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
  1706. {
  1707. for (; __n > 0; ++__first, (void) --__n)
  1708. *__first = __value_;
  1709. return __first;
  1710. }
  1711. template <class _OutputIterator, class _Size, class _Tp>
  1712. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1713. _OutputIterator
  1714. fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
  1715. {
  1716. return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
  1717. }
  1718. // fill
  1719. template <class _ForwardIterator, class _Tp>
  1720. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1721. void
  1722. __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
  1723. {
  1724. for (; __first != __last; ++__first)
  1725. *__first = __value_;
  1726. }
  1727. template <class _RandomAccessIterator, class _Tp>
  1728. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1729. void
  1730. __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
  1731. {
  1732. _VSTD::fill_n(__first, __last - __first, __value_);
  1733. }
  1734. template <class _ForwardIterator, class _Tp>
  1735. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1736. void
  1737. fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
  1738. {
  1739. _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
  1740. }
  1741. // generate
  1742. template <class _ForwardIterator, class _Generator>
  1743. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1744. void
  1745. generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
  1746. {
  1747. for (; __first != __last; ++__first)
  1748. *__first = __gen();
  1749. }
  1750. // generate_n
  1751. template <class _OutputIterator, class _Size, class _Generator>
  1752. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1753. _OutputIterator
  1754. generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
  1755. {
  1756. typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
  1757. _IntegralSize __n = __orig_n;
  1758. for (; __n > 0; ++__first, (void) --__n)
  1759. *__first = __gen();
  1760. return __first;
  1761. }
  1762. // remove
  1763. template <class _ForwardIterator, class _Tp>
  1764. _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
  1765. remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
  1766. {
  1767. __first = _VSTD::find(__first, __last, __value_);
  1768. if (__first != __last)
  1769. {
  1770. _ForwardIterator __i = __first;
  1771. while (++__i != __last)
  1772. {
  1773. if (!(*__i == __value_))
  1774. {
  1775. *__first = _VSTD::move(*__i);
  1776. ++__first;
  1777. }
  1778. }
  1779. }
  1780. return __first;
  1781. }
  1782. // remove_if
  1783. template <class _ForwardIterator, class _Predicate>
  1784. _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
  1785. remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
  1786. {
  1787. __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
  1788. (__first, __last, __pred);
  1789. if (__first != __last)
  1790. {
  1791. _ForwardIterator __i = __first;
  1792. while (++__i != __last)
  1793. {
  1794. if (!__pred(*__i))
  1795. {
  1796. *__first = _VSTD::move(*__i);
  1797. ++__first;
  1798. }
  1799. }
  1800. }
  1801. return __first;
  1802. }
  1803. // remove_copy
  1804. template <class _InputIterator, class _OutputIterator, class _Tp>
  1805. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1806. _OutputIterator
  1807. remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
  1808. {
  1809. for (; __first != __last; ++__first)
  1810. {
  1811. if (!(*__first == __value_))
  1812. {
  1813. *__result = *__first;
  1814. ++__result;
  1815. }
  1816. }
  1817. return __result;
  1818. }
  1819. // remove_copy_if
  1820. template <class _InputIterator, class _OutputIterator, class _Predicate>
  1821. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1822. _OutputIterator
  1823. remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
  1824. {
  1825. for (; __first != __last; ++__first)
  1826. {
  1827. if (!__pred(*__first))
  1828. {
  1829. *__result = *__first;
  1830. ++__result;
  1831. }
  1832. }
  1833. return __result;
  1834. }
  1835. // unique
  1836. template <class _ForwardIterator, class _BinaryPredicate>
  1837. _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
  1838. unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
  1839. {
  1840. __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
  1841. (__first, __last, __pred);
  1842. if (__first != __last)
  1843. {
  1844. // ... a a ? ...
  1845. // f i
  1846. _ForwardIterator __i = __first;
  1847. for (++__i; ++__i != __last;)
  1848. if (!__pred(*__first, *__i))
  1849. *++__first = _VSTD::move(*__i);
  1850. ++__first;
  1851. }
  1852. return __first;
  1853. }
  1854. template <class _ForwardIterator>
  1855. _LIBCPP_NODISCARD_EXT inline
  1856. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1857. _ForwardIterator
  1858. unique(_ForwardIterator __first, _ForwardIterator __last)
  1859. {
  1860. typedef typename iterator_traits<_ForwardIterator>::value_type __v;
  1861. return _VSTD::unique(__first, __last, __equal_to<__v>());
  1862. }
  1863. // unique_copy
  1864. template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
  1865. _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
  1866. __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
  1867. input_iterator_tag, output_iterator_tag)
  1868. {
  1869. if (__first != __last)
  1870. {
  1871. typename iterator_traits<_InputIterator>::value_type __t(*__first);
  1872. *__result = __t;
  1873. ++__result;
  1874. while (++__first != __last)
  1875. {
  1876. if (!__pred(__t, *__first))
  1877. {
  1878. __t = *__first;
  1879. *__result = __t;
  1880. ++__result;
  1881. }
  1882. }
  1883. }
  1884. return __result;
  1885. }
  1886. template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
  1887. _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
  1888. __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
  1889. forward_iterator_tag, output_iterator_tag)
  1890. {
  1891. if (__first != __last)
  1892. {
  1893. _ForwardIterator __i = __first;
  1894. *__result = *__i;
  1895. ++__result;
  1896. while (++__first != __last)
  1897. {
  1898. if (!__pred(*__i, *__first))
  1899. {
  1900. *__result = *__first;
  1901. ++__result;
  1902. __i = __first;
  1903. }
  1904. }
  1905. }
  1906. return __result;
  1907. }
  1908. template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
  1909. _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
  1910. __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
  1911. input_iterator_tag, forward_iterator_tag)
  1912. {
  1913. if (__first != __last)
  1914. {
  1915. *__result = *__first;
  1916. while (++__first != __last)
  1917. if (!__pred(*__result, *__first))
  1918. *++__result = *__first;
  1919. ++__result;
  1920. }
  1921. return __result;
  1922. }
  1923. template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
  1924. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1925. _OutputIterator
  1926. unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
  1927. {
  1928. return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
  1929. (__first, __last, __result, __pred,
  1930. typename iterator_traits<_InputIterator>::iterator_category(),
  1931. typename iterator_traits<_OutputIterator>::iterator_category());
  1932. }
  1933. template <class _InputIterator, class _OutputIterator>
  1934. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1935. _OutputIterator
  1936. unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  1937. {
  1938. typedef typename iterator_traits<_InputIterator>::value_type __v;
  1939. return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
  1940. }
  1941. // reverse
  1942. template <class _BidirectionalIterator>
  1943. inline _LIBCPP_INLINE_VISIBILITY
  1944. void
  1945. __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
  1946. {
  1947. while (__first != __last)
  1948. {
  1949. if (__first == --__last)
  1950. break;
  1951. _VSTD::iter_swap(__first, __last);
  1952. ++__first;
  1953. }
  1954. }
  1955. template <class _RandomAccessIterator>
  1956. inline _LIBCPP_INLINE_VISIBILITY
  1957. void
  1958. __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
  1959. {
  1960. if (__first != __last)
  1961. for (; __first < --__last; ++__first)
  1962. _VSTD::iter_swap(__first, __last);
  1963. }
  1964. template <class _BidirectionalIterator>
  1965. inline _LIBCPP_INLINE_VISIBILITY
  1966. void
  1967. reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
  1968. {
  1969. _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
  1970. }
  1971. // reverse_copy
  1972. template <class _BidirectionalIterator, class _OutputIterator>
  1973. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1974. _OutputIterator
  1975. reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
  1976. {
  1977. for (; __first != __last; ++__result)
  1978. *__result = *--__last;
  1979. return __result;
  1980. }
  1981. // rotate
  1982. template <class _ForwardIterator>
  1983. _ForwardIterator
  1984. __rotate_left(_ForwardIterator __first, _ForwardIterator __last)
  1985. {
  1986. typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
  1987. value_type __tmp = _VSTD::move(*__first);
  1988. _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
  1989. *__lm1 = _VSTD::move(__tmp);
  1990. return __lm1;
  1991. }
  1992. template <class _BidirectionalIterator>
  1993. _BidirectionalIterator
  1994. __rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
  1995. {
  1996. typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
  1997. _BidirectionalIterator __lm1 = _VSTD::prev(__last);
  1998. value_type __tmp = _VSTD::move(*__lm1);
  1999. _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
  2000. *__first = _VSTD::move(__tmp);
  2001. return __fp1;
  2002. }
  2003. template <class _ForwardIterator>
  2004. _ForwardIterator
  2005. __rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
  2006. {
  2007. _ForwardIterator __i = __middle;
  2008. while (true)
  2009. {
  2010. swap(*__first, *__i);
  2011. ++__first;
  2012. if (++__i == __last)
  2013. break;
  2014. if (__first == __middle)
  2015. __middle = __i;
  2016. }
  2017. _ForwardIterator __r = __first;
  2018. if (__first != __middle)
  2019. {
  2020. __i = __middle;
  2021. while (true)
  2022. {
  2023. swap(*__first, *__i);
  2024. ++__first;
  2025. if (++__i == __last)
  2026. {
  2027. if (__first == __middle)
  2028. break;
  2029. __i = __middle;
  2030. }
  2031. else if (__first == __middle)
  2032. __middle = __i;
  2033. }
  2034. }
  2035. return __r;
  2036. }
  2037. template<typename _Integral>
  2038. inline _LIBCPP_INLINE_VISIBILITY
  2039. _Integral
  2040. __algo_gcd(_Integral __x, _Integral __y)
  2041. {
  2042. do
  2043. {
  2044. _Integral __t = __x % __y;
  2045. __x = __y;
  2046. __y = __t;
  2047. } while (__y);
  2048. return __x;
  2049. }
  2050. template<typename _RandomAccessIterator>
  2051. _RandomAccessIterator
  2052. __rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
  2053. {
  2054. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  2055. typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
  2056. const difference_type __m1 = __middle - __first;
  2057. const difference_type __m2 = __last - __middle;
  2058. if (__m1 == __m2)
  2059. {
  2060. _VSTD::swap_ranges(__first, __middle, __middle);
  2061. return __middle;
  2062. }
  2063. const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
  2064. for (_RandomAccessIterator __p = __first + __g; __p != __first;)
  2065. {
  2066. value_type __t(_VSTD::move(*--__p));
  2067. _RandomAccessIterator __p1 = __p;
  2068. _RandomAccessIterator __p2 = __p1 + __m1;
  2069. do
  2070. {
  2071. *__p1 = _VSTD::move(*__p2);
  2072. __p1 = __p2;
  2073. const difference_type __d = __last - __p2;
  2074. if (__m1 < __d)
  2075. __p2 += __m1;
  2076. else
  2077. __p2 = __first + (__m1 - __d);
  2078. } while (__p2 != __p);
  2079. *__p1 = _VSTD::move(__t);
  2080. }
  2081. return __first + __m2;
  2082. }
  2083. template <class _ForwardIterator>
  2084. inline _LIBCPP_INLINE_VISIBILITY
  2085. _ForwardIterator
  2086. __rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
  2087. _VSTD::forward_iterator_tag)
  2088. {
  2089. typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
  2090. if (_VSTD::is_trivially_move_assignable<value_type>::value)
  2091. {
  2092. if (_VSTD::next(__first) == __middle)
  2093. return _VSTD::__rotate_left(__first, __last);
  2094. }
  2095. return _VSTD::__rotate_forward(__first, __middle, __last);
  2096. }
  2097. template <class _BidirectionalIterator>
  2098. inline _LIBCPP_INLINE_VISIBILITY
  2099. _BidirectionalIterator
  2100. __rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
  2101. _VSTD::bidirectional_iterator_tag)
  2102. {
  2103. typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
  2104. if (_VSTD::is_trivially_move_assignable<value_type>::value)
  2105. {
  2106. if (_VSTD::next(__first) == __middle)
  2107. return _VSTD::__rotate_left(__first, __last);
  2108. if (_VSTD::next(__middle) == __last)
  2109. return _VSTD::__rotate_right(__first, __last);
  2110. }
  2111. return _VSTD::__rotate_forward(__first, __middle, __last);
  2112. }
  2113. template <class _RandomAccessIterator>
  2114. inline _LIBCPP_INLINE_VISIBILITY
  2115. _RandomAccessIterator
  2116. __rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
  2117. _VSTD::random_access_iterator_tag)
  2118. {
  2119. typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
  2120. if (_VSTD::is_trivially_move_assignable<value_type>::value)
  2121. {
  2122. if (_VSTD::next(__first) == __middle)
  2123. return _VSTD::__rotate_left(__first, __last);
  2124. if (_VSTD::next(__middle) == __last)
  2125. return _VSTD::__rotate_right(__first, __last);
  2126. return _VSTD::__rotate_gcd(__first, __middle, __last);
  2127. }
  2128. return _VSTD::__rotate_forward(__first, __middle, __last);
  2129. }
  2130. template <class _ForwardIterator>
  2131. inline _LIBCPP_INLINE_VISIBILITY
  2132. _ForwardIterator
  2133. rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
  2134. {
  2135. if (__first == __middle)
  2136. return __last;
  2137. if (__middle == __last)
  2138. return __first;
  2139. return _VSTD::__rotate(__first, __middle, __last,
  2140. typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
  2141. }
  2142. // rotate_copy
  2143. template <class _ForwardIterator, class _OutputIterator>
  2144. inline _LIBCPP_INLINE_VISIBILITY
  2145. _OutputIterator
  2146. rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
  2147. {
  2148. return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
  2149. }
  2150. // min_element
  2151. template <class _ForwardIterator, class _Compare>
  2152. _LIBCPP_NODISCARD_EXT inline
  2153. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2154. _ForwardIterator
  2155. min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
  2156. {
  2157. static_assert(__is_forward_iterator<_ForwardIterator>::value,
  2158. "std::min_element requires a ForwardIterator");
  2159. if (__first != __last)
  2160. {
  2161. _ForwardIterator __i = __first;
  2162. while (++__i != __last)
  2163. if (__comp(*__i, *__first))
  2164. __first = __i;
  2165. }
  2166. return __first;
  2167. }
  2168. template <class _ForwardIterator>
  2169. _LIBCPP_NODISCARD_EXT inline
  2170. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2171. _ForwardIterator
  2172. min_element(_ForwardIterator __first, _ForwardIterator __last)
  2173. {
  2174. return _VSTD::min_element(__first, __last,
  2175. __less<typename iterator_traits<_ForwardIterator>::value_type>());
  2176. }
  2177. // min
  2178. template <class _Tp, class _Compare>
  2179. _LIBCPP_NODISCARD_EXT inline
  2180. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2181. const _Tp&
  2182. min(const _Tp& __a, const _Tp& __b, _Compare __comp)
  2183. {
  2184. return __comp(__b, __a) ? __b : __a;
  2185. }
  2186. template <class _Tp>
  2187. _LIBCPP_NODISCARD_EXT inline
  2188. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2189. const _Tp&
  2190. min(const _Tp& __a, const _Tp& __b)
  2191. {
  2192. return _VSTD::min(__a, __b, __less<_Tp>());
  2193. }
  2194. #ifndef _LIBCPP_CXX03_LANG
  2195. template<class _Tp, class _Compare>
  2196. _LIBCPP_NODISCARD_EXT inline
  2197. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2198. _Tp
  2199. min(initializer_list<_Tp> __t, _Compare __comp)
  2200. {
  2201. return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
  2202. }
  2203. template<class _Tp>
  2204. _LIBCPP_NODISCARD_EXT inline
  2205. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2206. _Tp
  2207. min(initializer_list<_Tp> __t)
  2208. {
  2209. return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
  2210. }
  2211. #endif // _LIBCPP_CXX03_LANG
  2212. // max_element
  2213. template <class _ForwardIterator, class _Compare>
  2214. _LIBCPP_NODISCARD_EXT inline
  2215. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2216. _ForwardIterator
  2217. max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
  2218. {
  2219. static_assert(__is_forward_iterator<_ForwardIterator>::value,
  2220. "std::max_element requires a ForwardIterator");
  2221. if (__first != __last)
  2222. {
  2223. _ForwardIterator __i = __first;
  2224. while (++__i != __last)
  2225. if (__comp(*__first, *__i))
  2226. __first = __i;
  2227. }
  2228. return __first;
  2229. }
  2230. template <class _ForwardIterator>
  2231. _LIBCPP_NODISCARD_EXT inline
  2232. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2233. _ForwardIterator
  2234. max_element(_ForwardIterator __first, _ForwardIterator __last)
  2235. {
  2236. return _VSTD::max_element(__first, __last,
  2237. __less<typename iterator_traits<_ForwardIterator>::value_type>());
  2238. }
  2239. // max
  2240. template <class _Tp, class _Compare>
  2241. _LIBCPP_NODISCARD_EXT inline
  2242. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2243. const _Tp&
  2244. max(const _Tp& __a, const _Tp& __b, _Compare __comp)
  2245. {
  2246. return __comp(__a, __b) ? __b : __a;
  2247. }
  2248. template <class _Tp>
  2249. _LIBCPP_NODISCARD_EXT inline
  2250. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2251. const _Tp&
  2252. max(const _Tp& __a, const _Tp& __b)
  2253. {
  2254. return _VSTD::max(__a, __b, __less<_Tp>());
  2255. }
  2256. #ifndef _LIBCPP_CXX03_LANG
  2257. template<class _Tp, class _Compare>
  2258. _LIBCPP_NODISCARD_EXT inline
  2259. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2260. _Tp
  2261. max(initializer_list<_Tp> __t, _Compare __comp)
  2262. {
  2263. return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
  2264. }
  2265. template<class _Tp>
  2266. _LIBCPP_NODISCARD_EXT inline
  2267. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2268. _Tp
  2269. max(initializer_list<_Tp> __t)
  2270. {
  2271. return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
  2272. }
  2273. #endif // _LIBCPP_CXX03_LANG
  2274. #if _LIBCPP_STD_VER > 14
  2275. // clamp
  2276. template<class _Tp, class _Compare>
  2277. _LIBCPP_NODISCARD_EXT inline
  2278. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  2279. const _Tp&
  2280. clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
  2281. {
  2282. _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
  2283. return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
  2284. }
  2285. template<class _Tp>
  2286. _LIBCPP_NODISCARD_EXT inline
  2287. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  2288. const _Tp&
  2289. clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
  2290. {
  2291. return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
  2292. }
  2293. #endif
  2294. // minmax_element
  2295. template <class _ForwardIterator, class _Compare>
  2296. _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX11
  2297. std::pair<_ForwardIterator, _ForwardIterator>
  2298. minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
  2299. {
  2300. static_assert(__is_forward_iterator<_ForwardIterator>::value,
  2301. "std::minmax_element requires a ForwardIterator");
  2302. std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
  2303. if (__first != __last)
  2304. {
  2305. if (++__first != __last)
  2306. {
  2307. if (__comp(*__first, *__result.first))
  2308. __result.first = __first;
  2309. else
  2310. __result.second = __first;
  2311. while (++__first != __last)
  2312. {
  2313. _ForwardIterator __i = __first;
  2314. if (++__first == __last)
  2315. {
  2316. if (__comp(*__i, *__result.first))
  2317. __result.first = __i;
  2318. else if (!__comp(*__i, *__result.second))
  2319. __result.second = __i;
  2320. break;
  2321. }
  2322. else
  2323. {
  2324. if (__comp(*__first, *__i))
  2325. {
  2326. if (__comp(*__first, *__result.first))
  2327. __result.first = __first;
  2328. if (!__comp(*__i, *__result.second))
  2329. __result.second = __i;
  2330. }
  2331. else
  2332. {
  2333. if (__comp(*__i, *__result.first))
  2334. __result.first = __i;
  2335. if (!__comp(*__first, *__result.second))
  2336. __result.second = __first;
  2337. }
  2338. }
  2339. }
  2340. }
  2341. }
  2342. return __result;
  2343. }
  2344. template <class _ForwardIterator>
  2345. _LIBCPP_NODISCARD_EXT inline
  2346. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2347. std::pair<_ForwardIterator, _ForwardIterator>
  2348. minmax_element(_ForwardIterator __first, _ForwardIterator __last)
  2349. {
  2350. return _VSTD::minmax_element(__first, __last,
  2351. __less<typename iterator_traits<_ForwardIterator>::value_type>());
  2352. }
  2353. // minmax
  2354. template<class _Tp, class _Compare>
  2355. _LIBCPP_NODISCARD_EXT inline
  2356. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2357. pair<const _Tp&, const _Tp&>
  2358. minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
  2359. {
  2360. return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
  2361. pair<const _Tp&, const _Tp&>(__a, __b);
  2362. }
  2363. template<class _Tp>
  2364. _LIBCPP_NODISCARD_EXT inline
  2365. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2366. pair<const _Tp&, const _Tp&>
  2367. minmax(const _Tp& __a, const _Tp& __b)
  2368. {
  2369. return _VSTD::minmax(__a, __b, __less<_Tp>());
  2370. }
  2371. #ifndef _LIBCPP_CXX03_LANG
  2372. template<class _Tp, class _Compare>
  2373. _LIBCPP_NODISCARD_EXT inline
  2374. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2375. pair<_Tp, _Tp>
  2376. minmax(initializer_list<_Tp> __t, _Compare __comp)
  2377. {
  2378. typedef typename initializer_list<_Tp>::const_iterator _Iter;
  2379. _Iter __first = __t.begin();
  2380. _Iter __last = __t.end();
  2381. std::pair<_Tp, _Tp> __result(*__first, *__first);
  2382. ++__first;
  2383. if (__t.size() % 2 == 0)
  2384. {
  2385. if (__comp(*__first, __result.first))
  2386. __result.first = *__first;
  2387. else
  2388. __result.second = *__first;
  2389. ++__first;
  2390. }
  2391. while (__first != __last)
  2392. {
  2393. _Tp __prev = *__first++;
  2394. if (__comp(*__first, __prev)) {
  2395. if ( __comp(*__first, __result.first)) __result.first = *__first;
  2396. if (!__comp(__prev, __result.second)) __result.second = __prev;
  2397. }
  2398. else {
  2399. if ( __comp(__prev, __result.first)) __result.first = __prev;
  2400. if (!__comp(*__first, __result.second)) __result.second = *__first;
  2401. }
  2402. __first++;
  2403. }
  2404. return __result;
  2405. }
  2406. template<class _Tp>
  2407. _LIBCPP_NODISCARD_EXT inline
  2408. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  2409. pair<_Tp, _Tp>
  2410. minmax(initializer_list<_Tp> __t)
  2411. {
  2412. return _VSTD::minmax(__t, __less<_Tp>());
  2413. }
  2414. #endif // _LIBCPP_CXX03_LANG
  2415. // random_shuffle
  2416. // __independent_bits_engine
  2417. template <unsigned long long _Xp, size_t _Rp>
  2418. struct __log2_imp
  2419. {
  2420. static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
  2421. : __log2_imp<_Xp, _Rp - 1>::value;
  2422. };
  2423. template <unsigned long long _Xp>
  2424. struct __log2_imp<_Xp, 0>
  2425. {
  2426. static const size_t value = 0;
  2427. };
  2428. template <size_t _Rp>
  2429. struct __log2_imp<0, _Rp>
  2430. {
  2431. static const size_t value = _Rp + 1;
  2432. };
  2433. template <class _UIntType, _UIntType _Xp>
  2434. struct __log2
  2435. {
  2436. static const size_t value = __log2_imp<_Xp,
  2437. sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
  2438. };
  2439. template<class _Engine, class _UIntType>
  2440. class __independent_bits_engine
  2441. {
  2442. public:
  2443. // types
  2444. typedef _UIntType result_type;
  2445. private:
  2446. typedef typename _Engine::result_type _Engine_result_type;
  2447. typedef typename conditional
  2448. <
  2449. sizeof(_Engine_result_type) <= sizeof(result_type),
  2450. result_type,
  2451. _Engine_result_type
  2452. >::type _Working_result_type;
  2453. _Engine& __e_;
  2454. size_t __w_;
  2455. size_t __w0_;
  2456. size_t __n_;
  2457. size_t __n0_;
  2458. _Working_result_type __y0_;
  2459. _Working_result_type __y1_;
  2460. _Engine_result_type __mask0_;
  2461. _Engine_result_type __mask1_;
  2462. #ifdef _LIBCPP_CXX03_LANG
  2463. static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
  2464. + _Working_result_type(1);
  2465. #else
  2466. static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
  2467. + _Working_result_type(1);
  2468. #endif
  2469. static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
  2470. static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
  2471. static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
  2472. public:
  2473. // constructors and seeding functions
  2474. __independent_bits_engine(_Engine& __e, size_t __w);
  2475. // generating functions
  2476. result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
  2477. private:
  2478. result_type __eval(false_type);
  2479. result_type __eval(true_type);
  2480. };
  2481. template<class _Engine, class _UIntType>
  2482. __independent_bits_engine<_Engine, _UIntType>
  2483. ::__independent_bits_engine(_Engine& __e, size_t __w)
  2484. : __e_(__e),
  2485. __w_(__w)
  2486. {
  2487. __n_ = __w_ / __m + (__w_ % __m != 0);
  2488. __w0_ = __w_ / __n_;
  2489. if (_Rp == 0)
  2490. __y0_ = _Rp;
  2491. else if (__w0_ < _WDt)
  2492. __y0_ = (_Rp >> __w0_) << __w0_;
  2493. else
  2494. __y0_ = 0;
  2495. if (_Rp - __y0_ > __y0_ / __n_)
  2496. {
  2497. ++__n_;
  2498. __w0_ = __w_ / __n_;
  2499. if (__w0_ < _WDt)
  2500. __y0_ = (_Rp >> __w0_) << __w0_;
  2501. else
  2502. __y0_ = 0;
  2503. }
  2504. __n0_ = __n_ - __w_ % __n_;
  2505. if (__w0_ < _WDt - 1)
  2506. __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
  2507. else
  2508. __y1_ = 0;
  2509. __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
  2510. _Engine_result_type(0);
  2511. __mask1_ = __w0_ < _EDt - 1 ?
  2512. _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
  2513. _Engine_result_type(~0);
  2514. }
  2515. template<class _Engine, class _UIntType>
  2516. inline
  2517. _UIntType
  2518. __independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
  2519. {
  2520. return static_cast<result_type>(__e_() & __mask0_);
  2521. }
  2522. template<class _Engine, class _UIntType>
  2523. _UIntType
  2524. __independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
  2525. {
  2526. const size_t _WRt = numeric_limits<result_type>::digits;
  2527. result_type _Sp = 0;
  2528. for (size_t __k = 0; __k < __n0_; ++__k)
  2529. {
  2530. _Engine_result_type __u;
  2531. do
  2532. {
  2533. __u = __e_() - _Engine::min();
  2534. } while (__u >= __y0_);
  2535. if (__w0_ < _WRt)
  2536. _Sp <<= __w0_;
  2537. else
  2538. _Sp = 0;
  2539. _Sp += __u & __mask0_;
  2540. }
  2541. for (size_t __k = __n0_; __k < __n_; ++__k)
  2542. {
  2543. _Engine_result_type __u;
  2544. do
  2545. {
  2546. __u = __e_() - _Engine::min();
  2547. } while (__u >= __y1_);
  2548. if (__w0_ < _WRt - 1)
  2549. _Sp <<= __w0_ + 1;
  2550. else
  2551. _Sp = 0;
  2552. _Sp += __u & __mask1_;
  2553. }
  2554. return _Sp;
  2555. }
  2556. // uniform_int_distribution
  2557. template<class _IntType = int>
  2558. class uniform_int_distribution
  2559. {
  2560. public:
  2561. // types
  2562. typedef _IntType result_type;
  2563. class param_type
  2564. {
  2565. result_type __a_;
  2566. result_type __b_;
  2567. public:
  2568. typedef uniform_int_distribution distribution_type;
  2569. explicit param_type(result_type __a = 0,
  2570. result_type __b = numeric_limits<result_type>::max())
  2571. : __a_(__a), __b_(__b) {}
  2572. result_type a() const {return __a_;}
  2573. result_type b() const {return __b_;}
  2574. friend bool operator==(const param_type& __x, const param_type& __y)
  2575. {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
  2576. friend bool operator!=(const param_type& __x, const param_type& __y)
  2577. {return !(__x == __y);}
  2578. };
  2579. private:
  2580. param_type __p_;
  2581. public:
  2582. // constructors and reset functions
  2583. explicit uniform_int_distribution(result_type __a = 0,
  2584. result_type __b = numeric_limits<result_type>::max())
  2585. : __p_(param_type(__a, __b)) {}
  2586. explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
  2587. void reset() {}
  2588. // generating functions
  2589. template<class _URNG> result_type operator()(_URNG& __g)
  2590. {return (*this)(__g, __p_);}
  2591. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  2592. // property functions
  2593. result_type a() const {return __p_.a();}
  2594. result_type b() const {return __p_.b();}
  2595. param_type param() const {return __p_;}
  2596. void param(const param_type& __p) {__p_ = __p;}
  2597. result_type min() const {return a();}
  2598. result_type max() const {return b();}
  2599. friend bool operator==(const uniform_int_distribution& __x,
  2600. const uniform_int_distribution& __y)
  2601. {return __x.__p_ == __y.__p_;}
  2602. friend bool operator!=(const uniform_int_distribution& __x,
  2603. const uniform_int_distribution& __y)
  2604. {return !(__x == __y);}
  2605. };
  2606. template<class _IntType>
  2607. template<class _URNG>
  2608. typename uniform_int_distribution<_IntType>::result_type
  2609. uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
  2610. _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
  2611. {
  2612. typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
  2613. uint32_t, uint64_t>::type _UIntType;
  2614. const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
  2615. if (_Rp == 1)
  2616. return __p.a();
  2617. const size_t _Dt = numeric_limits<_UIntType>::digits;
  2618. typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
  2619. if (_Rp == 0)
  2620. return static_cast<result_type>(_Eng(__g, _Dt)());
  2621. size_t __w = _Dt - __libcpp_clz(_Rp) - 1;
  2622. if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
  2623. ++__w;
  2624. _Eng __e(__g, __w);
  2625. _UIntType __u;
  2626. do
  2627. {
  2628. __u = __e();
  2629. } while (__u >= _Rp);
  2630. return static_cast<result_type>(__u + __p.a());
  2631. }
  2632. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
  2633. || defined(_LIBCPP_BUILDING_LIBRARY)
  2634. class _LIBCPP_TYPE_VIS __rs_default;
  2635. _LIBCPP_FUNC_VIS __rs_default __rs_get();
  2636. class _LIBCPP_TYPE_VIS __rs_default
  2637. {
  2638. static unsigned __c_;
  2639. __rs_default();
  2640. public:
  2641. typedef uint_fast32_t result_type;
  2642. static const result_type _Min = 0;
  2643. static const result_type _Max = 0xFFFFFFFF;
  2644. __rs_default(const __rs_default&);
  2645. ~__rs_default();
  2646. result_type operator()();
  2647. static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
  2648. static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
  2649. friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
  2650. };
  2651. _LIBCPP_FUNC_VIS __rs_default __rs_get();
  2652. template <class _RandomAccessIterator>
  2653. _LIBCPP_DEPRECATED_IN_CXX14 void
  2654. random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
  2655. {
  2656. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  2657. typedef uniform_int_distribution<ptrdiff_t> _Dp;
  2658. typedef typename _Dp::param_type _Pp;
  2659. difference_type __d = __last - __first;
  2660. if (__d > 1)
  2661. {
  2662. _Dp __uid;
  2663. __rs_default __g = __rs_get();
  2664. for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
  2665. {
  2666. difference_type __i = __uid(__g, _Pp(0, __d));
  2667. if (__i != difference_type(0))
  2668. swap(*__first, *(__first + __i));
  2669. }
  2670. }
  2671. }
  2672. template <class _RandomAccessIterator, class _RandomNumberGenerator>
  2673. _LIBCPP_DEPRECATED_IN_CXX14 void
  2674. random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
  2675. #ifndef _LIBCPP_CXX03_LANG
  2676. _RandomNumberGenerator&& __rand)
  2677. #else
  2678. _RandomNumberGenerator& __rand)
  2679. #endif
  2680. {
  2681. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  2682. difference_type __d = __last - __first;
  2683. if (__d > 1)
  2684. {
  2685. for (--__last; __first < __last; ++__first, (void) --__d)
  2686. {
  2687. difference_type __i = __rand(__d);
  2688. if (__i != difference_type(0))
  2689. swap(*__first, *(__first + __i));
  2690. }
  2691. }
  2692. }
  2693. #endif
  2694. template <class _PopulationIterator, class _SampleIterator, class _Distance,
  2695. class _UniformRandomNumberGenerator>
  2696. _LIBCPP_INLINE_VISIBILITY
  2697. _SampleIterator __sample(_PopulationIterator __first,
  2698. _PopulationIterator __last, _SampleIterator __output_iter,
  2699. _Distance __n,
  2700. _UniformRandomNumberGenerator & __g,
  2701. input_iterator_tag) {
  2702. _Distance __k = 0;
  2703. for (; __first != __last && __k < __n; ++__first, (void)++__k)
  2704. __output_iter[__k] = *__first;
  2705. _Distance __sz = __k;
  2706. for (; __first != __last; ++__first, (void)++__k) {
  2707. _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
  2708. if (__r < __sz)
  2709. __output_iter[__r] = *__first;
  2710. }
  2711. return __output_iter + _VSTD::min(__n, __k);
  2712. }
  2713. template <class _PopulationIterator, class _SampleIterator, class _Distance,
  2714. class _UniformRandomNumberGenerator>
  2715. _LIBCPP_INLINE_VISIBILITY
  2716. _SampleIterator __sample(_PopulationIterator __first,
  2717. _PopulationIterator __last, _SampleIterator __output_iter,
  2718. _Distance __n,
  2719. _UniformRandomNumberGenerator& __g,
  2720. forward_iterator_tag) {
  2721. _Distance __unsampled_sz = _VSTD::distance(__first, __last);
  2722. for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
  2723. _Distance __r =
  2724. _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
  2725. if (__r < __n) {
  2726. *__output_iter++ = *__first;
  2727. --__n;
  2728. }
  2729. }
  2730. return __output_iter;
  2731. }
  2732. template <class _PopulationIterator, class _SampleIterator, class _Distance,
  2733. class _UniformRandomNumberGenerator>
  2734. _LIBCPP_INLINE_VISIBILITY
  2735. _SampleIterator __sample(_PopulationIterator __first,
  2736. _PopulationIterator __last, _SampleIterator __output_iter,
  2737. _Distance __n, _UniformRandomNumberGenerator& __g) {
  2738. typedef typename iterator_traits<_PopulationIterator>::iterator_category
  2739. _PopCategory;
  2740. typedef typename iterator_traits<_PopulationIterator>::difference_type
  2741. _Difference;
  2742. static_assert(__is_forward_iterator<_PopulationIterator>::value ||
  2743. __is_random_access_iterator<_SampleIterator>::value,
  2744. "SampleIterator must meet the requirements of RandomAccessIterator");
  2745. typedef typename common_type<_Distance, _Difference>::type _CommonType;
  2746. _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
  2747. return _VSTD::__sample(
  2748. __first, __last, __output_iter, _CommonType(__n),
  2749. __g, _PopCategory());
  2750. }
  2751. #if _LIBCPP_STD_VER > 14
  2752. template <class _PopulationIterator, class _SampleIterator, class _Distance,
  2753. class _UniformRandomNumberGenerator>
  2754. inline _LIBCPP_INLINE_VISIBILITY
  2755. _SampleIterator sample(_PopulationIterator __first,
  2756. _PopulationIterator __last, _SampleIterator __output_iter,
  2757. _Distance __n, _UniformRandomNumberGenerator&& __g) {
  2758. return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
  2759. }
  2760. #endif // _LIBCPP_STD_VER > 14
  2761. template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
  2762. void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
  2763. _UniformRandomNumberGenerator&& __g)
  2764. {
  2765. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  2766. typedef uniform_int_distribution<ptrdiff_t> _Dp;
  2767. typedef typename _Dp::param_type _Pp;
  2768. difference_type __d = __last - __first;
  2769. if (__d > 1)
  2770. {
  2771. _Dp __uid;
  2772. for (--__last, --__d; __first < __last; ++__first, --__d)
  2773. {
  2774. difference_type __i = __uid(__g, _Pp(0, __d));
  2775. if (__i != difference_type(0))
  2776. swap(*__first, *(__first + __i));
  2777. }
  2778. }
  2779. }
  2780. template <class _InputIterator, class _Predicate>
  2781. _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  2782. is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  2783. {
  2784. for (; __first != __last; ++__first)
  2785. if (!__pred(*__first))
  2786. break;
  2787. if ( __first == __last )
  2788. return true;
  2789. ++__first;
  2790. for (; __first != __last; ++__first)
  2791. if (__pred(*__first))
  2792. return false;
  2793. return true;
  2794. }
  2795. // partition
  2796. template <class _Predicate, class _ForwardIterator>
  2797. _ForwardIterator
  2798. __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
  2799. {
  2800. while (true)
  2801. {
  2802. if (__first == __last)
  2803. return __first;
  2804. if (!__pred(*__first))
  2805. break;
  2806. ++__first;
  2807. }
  2808. for (_ForwardIterator __p = __first; ++__p != __last;)
  2809. {
  2810. if (__pred(*__p))
  2811. {
  2812. swap(*__first, *__p);
  2813. ++__first;
  2814. }
  2815. }
  2816. return __first;
  2817. }
  2818. template <class _Predicate, class _BidirectionalIterator>
  2819. _BidirectionalIterator
  2820. __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
  2821. bidirectional_iterator_tag)
  2822. {
  2823. while (true)
  2824. {
  2825. while (true)
  2826. {
  2827. if (__first == __last)
  2828. return __first;
  2829. if (!__pred(*__first))
  2830. break;
  2831. ++__first;
  2832. }
  2833. do
  2834. {
  2835. if (__first == --__last)
  2836. return __first;
  2837. } while (!__pred(*__last));
  2838. swap(*__first, *__last);
  2839. ++__first;
  2840. }
  2841. }
  2842. template <class _ForwardIterator, class _Predicate>
  2843. inline _LIBCPP_INLINE_VISIBILITY
  2844. _ForwardIterator
  2845. partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
  2846. {
  2847. return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
  2848. (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
  2849. }
  2850. // partition_copy
  2851. template <class _InputIterator, class _OutputIterator1,
  2852. class _OutputIterator2, class _Predicate>
  2853. _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
  2854. partition_copy(_InputIterator __first, _InputIterator __last,
  2855. _OutputIterator1 __out_true, _OutputIterator2 __out_false,
  2856. _Predicate __pred)
  2857. {
  2858. for (; __first != __last; ++__first)
  2859. {
  2860. if (__pred(*__first))
  2861. {
  2862. *__out_true = *__first;
  2863. ++__out_true;
  2864. }
  2865. else
  2866. {
  2867. *__out_false = *__first;
  2868. ++__out_false;
  2869. }
  2870. }
  2871. return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
  2872. }
  2873. // partition_point
  2874. template<class _ForwardIterator, class _Predicate>
  2875. _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
  2876. partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
  2877. {
  2878. typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
  2879. difference_type __len = _VSTD::distance(__first, __last);
  2880. while (__len != 0)
  2881. {
  2882. difference_type __l2 = _VSTD::__half_positive(__len);
  2883. _ForwardIterator __m = __first;
  2884. _VSTD::advance(__m, __l2);
  2885. if (__pred(*__m))
  2886. {
  2887. __first = ++__m;
  2888. __len -= __l2 + 1;
  2889. }
  2890. else
  2891. __len = __l2;
  2892. }
  2893. return __first;
  2894. }
  2895. // stable_partition
  2896. template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
  2897. _ForwardIterator
  2898. __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
  2899. _Distance __len, _Pair __p, forward_iterator_tag __fit)
  2900. {
  2901. // *__first is known to be false
  2902. // __len >= 1
  2903. if (__len == 1)
  2904. return __first;
  2905. if (__len == 2)
  2906. {
  2907. _ForwardIterator __m = __first;
  2908. if (__pred(*++__m))
  2909. {
  2910. swap(*__first, *__m);
  2911. return __m;
  2912. }
  2913. return __first;
  2914. }
  2915. if (__len <= __p.second)
  2916. { // The buffer is big enough to use
  2917. typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
  2918. __destruct_n __d(0);
  2919. unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
  2920. // Move the falses into the temporary buffer, and the trues to the front of the line
  2921. // Update __first to always point to the end of the trues
  2922. value_type* __t = __p.first;
  2923. ::new(__t) value_type(_VSTD::move(*__first));
  2924. __d.__incr((value_type*)0);
  2925. ++__t;
  2926. _ForwardIterator __i = __first;
  2927. while (++__i != __last)
  2928. {
  2929. if (__pred(*__i))
  2930. {
  2931. *__first = _VSTD::move(*__i);
  2932. ++__first;
  2933. }
  2934. else
  2935. {
  2936. ::new(__t) value_type(_VSTD::move(*__i));
  2937. __d.__incr((value_type*)0);
  2938. ++__t;
  2939. }
  2940. }
  2941. // All trues now at start of range, all falses in buffer
  2942. // Move falses back into range, but don't mess up __first which points to first false
  2943. __i = __first;
  2944. for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
  2945. *__i = _VSTD::move(*__t2);
  2946. // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
  2947. return __first;
  2948. }
  2949. // Else not enough buffer, do in place
  2950. // __len >= 3
  2951. _ForwardIterator __m = __first;
  2952. _Distance __len2 = __len / 2; // __len2 >= 2
  2953. _VSTD::advance(__m, __len2);
  2954. // recurse on [__first, __m), *__first know to be false
  2955. // F?????????????????
  2956. // f m l
  2957. typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
  2958. _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
  2959. // TTTFFFFF??????????
  2960. // f ff m l
  2961. // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
  2962. _ForwardIterator __m1 = __m;
  2963. _ForwardIterator __second_false = __last;
  2964. _Distance __len_half = __len - __len2;
  2965. while (__pred(*__m1))
  2966. {
  2967. if (++__m1 == __last)
  2968. goto __second_half_done;
  2969. --__len_half;
  2970. }
  2971. // TTTFFFFFTTTF??????
  2972. // f ff m m1 l
  2973. __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
  2974. __second_half_done:
  2975. // TTTFFFFFTTTTTFFFFF
  2976. // f ff m sf l
  2977. return _VSTD::rotate(__first_false, __m, __second_false);
  2978. // TTTTTTTTFFFFFFFFFF
  2979. // |
  2980. }
  2981. struct __return_temporary_buffer
  2982. {
  2983. template <class _Tp>
  2984. _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
  2985. };
  2986. template <class _Predicate, class _ForwardIterator>
  2987. _ForwardIterator
  2988. __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
  2989. forward_iterator_tag)
  2990. {
  2991. const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
  2992. // Either prove all true and return __first or point to first false
  2993. while (true)
  2994. {
  2995. if (__first == __last)
  2996. return __first;
  2997. if (!__pred(*__first))
  2998. break;
  2999. ++__first;
  3000. }
  3001. // We now have a reduced range [__first, __last)
  3002. // *__first is known to be false
  3003. typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
  3004. typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
  3005. difference_type __len = _VSTD::distance(__first, __last);
  3006. pair<value_type*, ptrdiff_t> __p(0, 0);
  3007. unique_ptr<value_type, __return_temporary_buffer> __h;
  3008. if (__len >= __alloc_limit)
  3009. {
  3010. __p = _VSTD::get_temporary_buffer<value_type>(__len);
  3011. __h.reset(__p.first);
  3012. }
  3013. return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
  3014. (__first, __last, __pred, __len, __p, forward_iterator_tag());
  3015. }
  3016. template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
  3017. _BidirectionalIterator
  3018. __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
  3019. _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
  3020. {
  3021. // *__first is known to be false
  3022. // *__last is known to be true
  3023. // __len >= 2
  3024. if (__len == 2)
  3025. {
  3026. swap(*__first, *__last);
  3027. return __last;
  3028. }
  3029. if (__len == 3)
  3030. {
  3031. _BidirectionalIterator __m = __first;
  3032. if (__pred(*++__m))
  3033. {
  3034. swap(*__first, *__m);
  3035. swap(*__m, *__last);
  3036. return __last;
  3037. }
  3038. swap(*__m, *__last);
  3039. swap(*__first, *__m);
  3040. return __m;
  3041. }
  3042. if (__len <= __p.second)
  3043. { // The buffer is big enough to use
  3044. typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
  3045. __destruct_n __d(0);
  3046. unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
  3047. // Move the falses into the temporary buffer, and the trues to the front of the line
  3048. // Update __first to always point to the end of the trues
  3049. value_type* __t = __p.first;
  3050. ::new(__t) value_type(_VSTD::move(*__first));
  3051. __d.__incr((value_type*)0);
  3052. ++__t;
  3053. _BidirectionalIterator __i = __first;
  3054. while (++__i != __last)
  3055. {
  3056. if (__pred(*__i))
  3057. {
  3058. *__first = _VSTD::move(*__i);
  3059. ++__first;
  3060. }
  3061. else
  3062. {
  3063. ::new(__t) value_type(_VSTD::move(*__i));
  3064. __d.__incr((value_type*)0);
  3065. ++__t;
  3066. }
  3067. }
  3068. // move *__last, known to be true
  3069. *__first = _VSTD::move(*__i);
  3070. __i = ++__first;
  3071. // All trues now at start of range, all falses in buffer
  3072. // Move falses back into range, but don't mess up __first which points to first false
  3073. for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
  3074. *__i = _VSTD::move(*__t2);
  3075. // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
  3076. return __first;
  3077. }
  3078. // Else not enough buffer, do in place
  3079. // __len >= 4
  3080. _BidirectionalIterator __m = __first;
  3081. _Distance __len2 = __len / 2; // __len2 >= 2
  3082. _VSTD::advance(__m, __len2);
  3083. // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
  3084. // F????????????????T
  3085. // f m l
  3086. _BidirectionalIterator __m1 = __m;
  3087. _BidirectionalIterator __first_false = __first;
  3088. _Distance __len_half = __len2;
  3089. while (!__pred(*--__m1))
  3090. {
  3091. if (__m1 == __first)
  3092. goto __first_half_done;
  3093. --__len_half;
  3094. }
  3095. // F???TFFF?????????T
  3096. // f m1 m l
  3097. typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
  3098. __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
  3099. __first_half_done:
  3100. // TTTFFFFF?????????T
  3101. // f ff m l
  3102. // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
  3103. __m1 = __m;
  3104. _BidirectionalIterator __second_false = __last;
  3105. ++__second_false;
  3106. __len_half = __len - __len2;
  3107. while (__pred(*__m1))
  3108. {
  3109. if (++__m1 == __last)
  3110. goto __second_half_done;
  3111. --__len_half;
  3112. }
  3113. // TTTFFFFFTTTF?????T
  3114. // f ff m m1 l
  3115. __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
  3116. __second_half_done:
  3117. // TTTFFFFFTTTTTFFFFF
  3118. // f ff m sf l
  3119. return _VSTD::rotate(__first_false, __m, __second_false);
  3120. // TTTTTTTTFFFFFFFFFF
  3121. // |
  3122. }
  3123. template <class _Predicate, class _BidirectionalIterator>
  3124. _BidirectionalIterator
  3125. __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
  3126. bidirectional_iterator_tag)
  3127. {
  3128. typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
  3129. typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
  3130. const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
  3131. // Either prove all true and return __first or point to first false
  3132. while (true)
  3133. {
  3134. if (__first == __last)
  3135. return __first;
  3136. if (!__pred(*__first))
  3137. break;
  3138. ++__first;
  3139. }
  3140. // __first points to first false, everything prior to __first is already set.
  3141. // Either prove [__first, __last) is all false and return __first, or point __last to last true
  3142. do
  3143. {
  3144. if (__first == --__last)
  3145. return __first;
  3146. } while (!__pred(*__last));
  3147. // We now have a reduced range [__first, __last]
  3148. // *__first is known to be false
  3149. // *__last is known to be true
  3150. // __len >= 2
  3151. difference_type __len = _VSTD::distance(__first, __last) + 1;
  3152. pair<value_type*, ptrdiff_t> __p(0, 0);
  3153. unique_ptr<value_type, __return_temporary_buffer> __h;
  3154. if (__len >= __alloc_limit)
  3155. {
  3156. __p = _VSTD::get_temporary_buffer<value_type>(__len);
  3157. __h.reset(__p.first);
  3158. }
  3159. return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
  3160. (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
  3161. }
  3162. template <class _ForwardIterator, class _Predicate>
  3163. inline _LIBCPP_INLINE_VISIBILITY
  3164. _ForwardIterator
  3165. stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
  3166. {
  3167. return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
  3168. (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
  3169. }
  3170. // is_sorted_until
  3171. template <class _ForwardIterator, class _Compare>
  3172. _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
  3173. is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
  3174. {
  3175. if (__first != __last)
  3176. {
  3177. _ForwardIterator __i = __first;
  3178. while (++__i != __last)
  3179. {
  3180. if (__comp(*__i, *__first))
  3181. return __i;
  3182. __first = __i;
  3183. }
  3184. }
  3185. return __last;
  3186. }
  3187. template<class _ForwardIterator>
  3188. _LIBCPP_NODISCARD_EXT inline
  3189. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3190. _ForwardIterator
  3191. is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
  3192. {
  3193. return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
  3194. }
  3195. // is_sorted
  3196. template <class _ForwardIterator, class _Compare>
  3197. _LIBCPP_NODISCARD_EXT inline
  3198. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3199. bool
  3200. is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
  3201. {
  3202. return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
  3203. }
  3204. template<class _ForwardIterator>
  3205. _LIBCPP_NODISCARD_EXT inline
  3206. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3207. bool
  3208. is_sorted(_ForwardIterator __first, _ForwardIterator __last)
  3209. {
  3210. return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
  3211. }
  3212. // sort
  3213. // stable, 2-3 compares, 0-2 swaps
  3214. template <class _Compare, class _ForwardIterator>
  3215. unsigned
  3216. __sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
  3217. {
  3218. unsigned __r = 0;
  3219. if (!__c(*__y, *__x)) // if x <= y
  3220. {
  3221. if (!__c(*__z, *__y)) // if y <= z
  3222. return __r; // x <= y && y <= z
  3223. // x <= y && y > z
  3224. swap(*__y, *__z); // x <= z && y < z
  3225. __r = 1;
  3226. if (__c(*__y, *__x)) // if x > y
  3227. {
  3228. swap(*__x, *__y); // x < y && y <= z
  3229. __r = 2;
  3230. }
  3231. return __r; // x <= y && y < z
  3232. }
  3233. if (__c(*__z, *__y)) // x > y, if y > z
  3234. {
  3235. swap(*__x, *__z); // x < y && y < z
  3236. __r = 1;
  3237. return __r;
  3238. }
  3239. swap(*__x, *__y); // x > y && y <= z
  3240. __r = 1; // x < y && x <= z
  3241. if (__c(*__z, *__y)) // if y > z
  3242. {
  3243. swap(*__y, *__z); // x <= y && y < z
  3244. __r = 2;
  3245. }
  3246. return __r;
  3247. } // x <= y && y <= z
  3248. // stable, 3-6 compares, 0-5 swaps
  3249. template <class _Compare, class _ForwardIterator>
  3250. unsigned
  3251. __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
  3252. _ForwardIterator __x4, _Compare __c)
  3253. {
  3254. unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
  3255. if (__c(*__x4, *__x3))
  3256. {
  3257. swap(*__x3, *__x4);
  3258. ++__r;
  3259. if (__c(*__x3, *__x2))
  3260. {
  3261. swap(*__x2, *__x3);
  3262. ++__r;
  3263. if (__c(*__x2, *__x1))
  3264. {
  3265. swap(*__x1, *__x2);
  3266. ++__r;
  3267. }
  3268. }
  3269. }
  3270. return __r;
  3271. }
  3272. // stable, 4-10 compares, 0-9 swaps
  3273. template <class _Compare, class _ForwardIterator>
  3274. _LIBCPP_HIDDEN
  3275. unsigned
  3276. __sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
  3277. _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
  3278. {
  3279. unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
  3280. if (__c(*__x5, *__x4))
  3281. {
  3282. swap(*__x4, *__x5);
  3283. ++__r;
  3284. if (__c(*__x4, *__x3))
  3285. {
  3286. swap(*__x3, *__x4);
  3287. ++__r;
  3288. if (__c(*__x3, *__x2))
  3289. {
  3290. swap(*__x2, *__x3);
  3291. ++__r;
  3292. if (__c(*__x2, *__x1))
  3293. {
  3294. swap(*__x1, *__x2);
  3295. ++__r;
  3296. }
  3297. }
  3298. }
  3299. }
  3300. return __r;
  3301. }
  3302. // Assumes size > 0
  3303. template <class _Compare, class _BirdirectionalIterator>
  3304. void
  3305. __selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
  3306. {
  3307. _BirdirectionalIterator __lm1 = __last;
  3308. for (--__lm1; __first != __lm1; ++__first)
  3309. {
  3310. _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
  3311. typename add_lvalue_reference<_Compare>::type>
  3312. (__first, __last, __comp);
  3313. if (__i != __first)
  3314. swap(*__first, *__i);
  3315. }
  3316. }
  3317. template <class _Compare, class _BirdirectionalIterator>
  3318. void
  3319. __insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
  3320. {
  3321. typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
  3322. if (__first != __last)
  3323. {
  3324. _BirdirectionalIterator __i = __first;
  3325. for (++__i; __i != __last; ++__i)
  3326. {
  3327. _BirdirectionalIterator __j = __i;
  3328. value_type __t(_VSTD::move(*__j));
  3329. for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
  3330. *__j = _VSTD::move(*__k);
  3331. *__j = _VSTD::move(__t);
  3332. }
  3333. }
  3334. }
  3335. template <class _Compare, class _RandomAccessIterator>
  3336. void
  3337. __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  3338. {
  3339. typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
  3340. _RandomAccessIterator __j = __first+2;
  3341. __sort3<_Compare>(__first, __first+1, __j, __comp);
  3342. for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
  3343. {
  3344. if (__comp(*__i, *__j))
  3345. {
  3346. value_type __t(_VSTD::move(*__i));
  3347. _RandomAccessIterator __k = __j;
  3348. __j = __i;
  3349. do
  3350. {
  3351. *__j = _VSTD::move(*__k);
  3352. __j = __k;
  3353. } while (__j != __first && __comp(__t, *--__k));
  3354. *__j = _VSTD::move(__t);
  3355. }
  3356. __j = __i;
  3357. }
  3358. }
  3359. template <class _Compare, class _RandomAccessIterator>
  3360. bool
  3361. __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  3362. {
  3363. switch (__last - __first)
  3364. {
  3365. case 0:
  3366. case 1:
  3367. return true;
  3368. case 2:
  3369. if (__comp(*--__last, *__first))
  3370. swap(*__first, *__last);
  3371. return true;
  3372. case 3:
  3373. _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
  3374. return true;
  3375. case 4:
  3376. _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
  3377. return true;
  3378. case 5:
  3379. _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
  3380. return true;
  3381. }
  3382. typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
  3383. _RandomAccessIterator __j = __first+2;
  3384. __sort3<_Compare>(__first, __first+1, __j, __comp);
  3385. const unsigned __limit = 8;
  3386. unsigned __count = 0;
  3387. for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
  3388. {
  3389. if (__comp(*__i, *__j))
  3390. {
  3391. value_type __t(_VSTD::move(*__i));
  3392. _RandomAccessIterator __k = __j;
  3393. __j = __i;
  3394. do
  3395. {
  3396. *__j = _VSTD::move(*__k);
  3397. __j = __k;
  3398. } while (__j != __first && __comp(__t, *--__k));
  3399. *__j = _VSTD::move(__t);
  3400. if (++__count == __limit)
  3401. return ++__i == __last;
  3402. }
  3403. __j = __i;
  3404. }
  3405. return true;
  3406. }
  3407. template <class _Compare, class _BirdirectionalIterator>
  3408. void
  3409. __insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
  3410. typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
  3411. {
  3412. typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
  3413. if (__first1 != __last1)
  3414. {
  3415. __destruct_n __d(0);
  3416. unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
  3417. value_type* __last2 = __first2;
  3418. ::new(__last2) value_type(_VSTD::move(*__first1));
  3419. __d.__incr((value_type*)0);
  3420. for (++__last2; ++__first1 != __last1; ++__last2)
  3421. {
  3422. value_type* __j2 = __last2;
  3423. value_type* __i2 = __j2;
  3424. if (__comp(*__first1, *--__i2))
  3425. {
  3426. ::new(__j2) value_type(_VSTD::move(*__i2));
  3427. __d.__incr((value_type*)0);
  3428. for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
  3429. *__j2 = _VSTD::move(*__i2);
  3430. *__j2 = _VSTD::move(*__first1);
  3431. }
  3432. else
  3433. {
  3434. ::new(__j2) value_type(_VSTD::move(*__first1));
  3435. __d.__incr((value_type*)0);
  3436. }
  3437. }
  3438. __h.release();
  3439. }
  3440. }
  3441. template <class _Compare, class _RandomAccessIterator>
  3442. void
  3443. __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  3444. {
  3445. // _Compare is known to be a reference type
  3446. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  3447. typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
  3448. const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
  3449. is_trivially_copy_assignable<value_type>::value ? 30 : 6;
  3450. while (true)
  3451. {
  3452. __restart:
  3453. difference_type __len = __last - __first;
  3454. switch (__len)
  3455. {
  3456. case 0:
  3457. case 1:
  3458. return;
  3459. case 2:
  3460. if (__comp(*--__last, *__first))
  3461. swap(*__first, *__last);
  3462. return;
  3463. case 3:
  3464. _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
  3465. return;
  3466. case 4:
  3467. _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
  3468. return;
  3469. case 5:
  3470. _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
  3471. return;
  3472. }
  3473. if (__len <= __limit)
  3474. {
  3475. _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
  3476. return;
  3477. }
  3478. // __len > 5
  3479. _RandomAccessIterator __m = __first;
  3480. _RandomAccessIterator __lm1 = __last;
  3481. --__lm1;
  3482. unsigned __n_swaps;
  3483. {
  3484. difference_type __delta;
  3485. if (__len >= 1000)
  3486. {
  3487. __delta = __len/2;
  3488. __m += __delta;
  3489. __delta /= 2;
  3490. __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
  3491. }
  3492. else
  3493. {
  3494. __delta = __len/2;
  3495. __m += __delta;
  3496. __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
  3497. }
  3498. }
  3499. // *__m is median
  3500. // partition [__first, __m) < *__m and *__m <= [__m, __last)
  3501. // (this inhibits tossing elements equivalent to __m around unnecessarily)
  3502. _RandomAccessIterator __i = __first;
  3503. _RandomAccessIterator __j = __lm1;
  3504. // j points beyond range to be tested, *__m is known to be <= *__lm1
  3505. // The search going up is known to be guarded but the search coming down isn't.
  3506. // Prime the downward search with a guard.
  3507. if (!__comp(*__i, *__m)) // if *__first == *__m
  3508. {
  3509. // *__first == *__m, *__first doesn't go in first part
  3510. // manually guard downward moving __j against __i
  3511. while (true)
  3512. {
  3513. if (__i == --__j)
  3514. {
  3515. // *__first == *__m, *__m <= all other elements
  3516. // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
  3517. ++__i; // __first + 1
  3518. __j = __last;
  3519. if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
  3520. {
  3521. while (true)
  3522. {
  3523. if (__i == __j)
  3524. return; // [__first, __last) all equivalent elements
  3525. if (__comp(*__first, *__i))
  3526. {
  3527. swap(*__i, *__j);
  3528. ++__n_swaps;
  3529. ++__i;
  3530. break;
  3531. }
  3532. ++__i;
  3533. }
  3534. }
  3535. // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
  3536. if (__i == __j)
  3537. return;
  3538. while (true)
  3539. {
  3540. while (!__comp(*__first, *__i))
  3541. ++__i;
  3542. while (__comp(*__first, *--__j))
  3543. ;
  3544. if (__i >= __j)
  3545. break;
  3546. swap(*__i, *__j);
  3547. ++__n_swaps;
  3548. ++__i;
  3549. }
  3550. // [__first, __i) == *__first and *__first < [__i, __last)
  3551. // The first part is sorted, sort the secod part
  3552. // _VSTD::__sort<_Compare>(__i, __last, __comp);
  3553. __first = __i;
  3554. goto __restart;
  3555. }
  3556. if (__comp(*__j, *__m))
  3557. {
  3558. swap(*__i, *__j);
  3559. ++__n_swaps;
  3560. break; // found guard for downward moving __j, now use unguarded partition
  3561. }
  3562. }
  3563. }
  3564. // It is known that *__i < *__m
  3565. ++__i;
  3566. // j points beyond range to be tested, *__m is known to be <= *__lm1
  3567. // if not yet partitioned...
  3568. if (__i < __j)
  3569. {
  3570. // known that *(__i - 1) < *__m
  3571. // known that __i <= __m
  3572. while (true)
  3573. {
  3574. // __m still guards upward moving __i
  3575. while (__comp(*__i, *__m))
  3576. ++__i;
  3577. // It is now known that a guard exists for downward moving __j
  3578. while (!__comp(*--__j, *__m))
  3579. ;
  3580. if (__i > __j)
  3581. break;
  3582. swap(*__i, *__j);
  3583. ++__n_swaps;
  3584. // It is known that __m != __j
  3585. // If __m just moved, follow it
  3586. if (__m == __i)
  3587. __m = __j;
  3588. ++__i;
  3589. }
  3590. }
  3591. // [__first, __i) < *__m and *__m <= [__i, __last)
  3592. if (__i != __m && __comp(*__m, *__i))
  3593. {
  3594. swap(*__i, *__m);
  3595. ++__n_swaps;
  3596. }
  3597. // [__first, __i) < *__i and *__i <= [__i+1, __last)
  3598. // If we were given a perfect partition, see if insertion sort is quick...
  3599. if (__n_swaps == 0)
  3600. {
  3601. bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
  3602. if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
  3603. {
  3604. if (__fs)
  3605. return;
  3606. __last = __i;
  3607. continue;
  3608. }
  3609. else
  3610. {
  3611. if (__fs)
  3612. {
  3613. __first = ++__i;
  3614. continue;
  3615. }
  3616. }
  3617. }
  3618. // sort smaller range with recursive call and larger with tail recursion elimination
  3619. if (__i - __first < __last - __i)
  3620. {
  3621. _VSTD::__sort<_Compare>(__first, __i, __comp);
  3622. // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
  3623. __first = ++__i;
  3624. }
  3625. else
  3626. {
  3627. _VSTD::__sort<_Compare>(__i+1, __last, __comp);
  3628. // _VSTD::__sort<_Compare>(__first, __i, __comp);
  3629. __last = __i;
  3630. }
  3631. }
  3632. }
  3633. // This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
  3634. template <class _RandomAccessIterator, class _Compare>
  3635. inline _LIBCPP_INLINE_VISIBILITY
  3636. void
  3637. sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  3638. {
  3639. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  3640. _VSTD::__sort<_Comp_ref>(__first, __last, _Comp_ref(__comp));
  3641. }
  3642. template <class _RandomAccessIterator>
  3643. inline _LIBCPP_INLINE_VISIBILITY
  3644. void
  3645. sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
  3646. {
  3647. _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  3648. }
  3649. template <class _Tp>
  3650. inline _LIBCPP_INLINE_VISIBILITY
  3651. void
  3652. sort(_Tp** __first, _Tp** __last)
  3653. {
  3654. _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
  3655. }
  3656. template <class _Tp>
  3657. inline _LIBCPP_INLINE_VISIBILITY
  3658. void
  3659. sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
  3660. {
  3661. _VSTD::sort(__first.base(), __last.base());
  3662. }
  3663. template <class _Tp, class _Compare>
  3664. inline _LIBCPP_INLINE_VISIBILITY
  3665. void
  3666. sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
  3667. {
  3668. typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
  3669. _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
  3670. }
  3671. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
  3672. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
  3673. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
  3674. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
  3675. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
  3676. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
  3677. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
  3678. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
  3679. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
  3680. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
  3681. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
  3682. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
  3683. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
  3684. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
  3685. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
  3686. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
  3687. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
  3688. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
  3689. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
  3690. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
  3691. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
  3692. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
  3693. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
  3694. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
  3695. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
  3696. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
  3697. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
  3698. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
  3699. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
  3700. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
  3701. _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&))
  3702. // lower_bound
  3703. template <class _Compare, class _ForwardIterator, class _Tp>
  3704. _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
  3705. __lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
  3706. {
  3707. typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
  3708. difference_type __len = _VSTD::distance(__first, __last);
  3709. while (__len != 0)
  3710. {
  3711. difference_type __l2 = _VSTD::__half_positive(__len);
  3712. _ForwardIterator __m = __first;
  3713. _VSTD::advance(__m, __l2);
  3714. if (__comp(*__m, __value_))
  3715. {
  3716. __first = ++__m;
  3717. __len -= __l2 + 1;
  3718. }
  3719. else
  3720. __len = __l2;
  3721. }
  3722. return __first;
  3723. }
  3724. template <class _ForwardIterator, class _Tp, class _Compare>
  3725. _LIBCPP_NODISCARD_EXT inline
  3726. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3727. _ForwardIterator
  3728. lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
  3729. {
  3730. typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
  3731. return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
  3732. }
  3733. template <class _ForwardIterator, class _Tp>
  3734. _LIBCPP_NODISCARD_EXT inline
  3735. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3736. _ForwardIterator
  3737. lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
  3738. {
  3739. return _VSTD::lower_bound(__first, __last, __value_,
  3740. __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
  3741. }
  3742. // upper_bound
  3743. template <class _Compare, class _ForwardIterator, class _Tp>
  3744. _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
  3745. __upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
  3746. {
  3747. typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
  3748. difference_type __len = _VSTD::distance(__first, __last);
  3749. while (__len != 0)
  3750. {
  3751. difference_type __l2 = _VSTD::__half_positive(__len);
  3752. _ForwardIterator __m = __first;
  3753. _VSTD::advance(__m, __l2);
  3754. if (__comp(__value_, *__m))
  3755. __len = __l2;
  3756. else
  3757. {
  3758. __first = ++__m;
  3759. __len -= __l2 + 1;
  3760. }
  3761. }
  3762. return __first;
  3763. }
  3764. template <class _ForwardIterator, class _Tp, class _Compare>
  3765. _LIBCPP_NODISCARD_EXT inline
  3766. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3767. _ForwardIterator
  3768. upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
  3769. {
  3770. typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
  3771. return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
  3772. }
  3773. template <class _ForwardIterator, class _Tp>
  3774. _LIBCPP_NODISCARD_EXT inline
  3775. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3776. _ForwardIterator
  3777. upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
  3778. {
  3779. return _VSTD::upper_bound(__first, __last, __value_,
  3780. __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
  3781. }
  3782. // equal_range
  3783. template <class _Compare, class _ForwardIterator, class _Tp>
  3784. _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
  3785. __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
  3786. {
  3787. typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
  3788. difference_type __len = _VSTD::distance(__first, __last);
  3789. while (__len != 0)
  3790. {
  3791. difference_type __l2 = _VSTD::__half_positive(__len);
  3792. _ForwardIterator __m = __first;
  3793. _VSTD::advance(__m, __l2);
  3794. if (__comp(*__m, __value_))
  3795. {
  3796. __first = ++__m;
  3797. __len -= __l2 + 1;
  3798. }
  3799. else if (__comp(__value_, *__m))
  3800. {
  3801. __last = __m;
  3802. __len = __l2;
  3803. }
  3804. else
  3805. {
  3806. _ForwardIterator __mp1 = __m;
  3807. return pair<_ForwardIterator, _ForwardIterator>
  3808. (
  3809. __lower_bound<_Compare>(__first, __m, __value_, __comp),
  3810. __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
  3811. );
  3812. }
  3813. }
  3814. return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
  3815. }
  3816. template <class _ForwardIterator, class _Tp, class _Compare>
  3817. _LIBCPP_NODISCARD_EXT inline
  3818. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3819. pair<_ForwardIterator, _ForwardIterator>
  3820. equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
  3821. {
  3822. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  3823. return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
  3824. }
  3825. template <class _ForwardIterator, class _Tp>
  3826. _LIBCPP_NODISCARD_EXT inline
  3827. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3828. pair<_ForwardIterator, _ForwardIterator>
  3829. equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
  3830. {
  3831. return _VSTD::equal_range(__first, __last, __value_,
  3832. __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
  3833. }
  3834. // binary_search
  3835. template <class _Compare, class _ForwardIterator, class _Tp>
  3836. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3837. bool
  3838. __binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
  3839. {
  3840. __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
  3841. return __first != __last && !__comp(__value_, *__first);
  3842. }
  3843. template <class _ForwardIterator, class _Tp, class _Compare>
  3844. _LIBCPP_NODISCARD_EXT inline
  3845. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3846. bool
  3847. binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
  3848. {
  3849. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  3850. return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
  3851. }
  3852. template <class _ForwardIterator, class _Tp>
  3853. _LIBCPP_NODISCARD_EXT inline
  3854. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  3855. bool
  3856. binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
  3857. {
  3858. return _VSTD::binary_search(__first, __last, __value_,
  3859. __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
  3860. }
  3861. // merge
  3862. template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
  3863. _OutputIterator
  3864. __merge(_InputIterator1 __first1, _InputIterator1 __last1,
  3865. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
  3866. {
  3867. for (; __first1 != __last1; ++__result)
  3868. {
  3869. if (__first2 == __last2)
  3870. return _VSTD::copy(__first1, __last1, __result);
  3871. if (__comp(*__first2, *__first1))
  3872. {
  3873. *__result = *__first2;
  3874. ++__first2;
  3875. }
  3876. else
  3877. {
  3878. *__result = *__first1;
  3879. ++__first1;
  3880. }
  3881. }
  3882. return _VSTD::copy(__first2, __last2, __result);
  3883. }
  3884. template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
  3885. inline _LIBCPP_INLINE_VISIBILITY
  3886. _OutputIterator
  3887. merge(_InputIterator1 __first1, _InputIterator1 __last1,
  3888. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
  3889. {
  3890. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  3891. return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
  3892. }
  3893. template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
  3894. inline _LIBCPP_INLINE_VISIBILITY
  3895. _OutputIterator
  3896. merge(_InputIterator1 __first1, _InputIterator1 __last1,
  3897. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
  3898. {
  3899. typedef typename iterator_traits<_InputIterator1>::value_type __v1;
  3900. typedef typename iterator_traits<_InputIterator2>::value_type __v2;
  3901. return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
  3902. }
  3903. // inplace_merge
  3904. template <class _Compare, class _InputIterator1, class _InputIterator2,
  3905. class _OutputIterator>
  3906. void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
  3907. _InputIterator2 __first2, _InputIterator2 __last2,
  3908. _OutputIterator __result, _Compare __comp)
  3909. {
  3910. for (; __first1 != __last1; ++__result)
  3911. {
  3912. if (__first2 == __last2)
  3913. {
  3914. _VSTD::move(__first1, __last1, __result);
  3915. return;
  3916. }
  3917. if (__comp(*__first2, *__first1))
  3918. {
  3919. *__result = _VSTD::move(*__first2);
  3920. ++__first2;
  3921. }
  3922. else
  3923. {
  3924. *__result = _VSTD::move(*__first1);
  3925. ++__first1;
  3926. }
  3927. }
  3928. // __first2 through __last2 are already in the right spot.
  3929. }
  3930. template <class _Compare, class _BidirectionalIterator>
  3931. void
  3932. __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
  3933. _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
  3934. typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
  3935. typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
  3936. {
  3937. typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
  3938. __destruct_n __d(0);
  3939. unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
  3940. if (__len1 <= __len2)
  3941. {
  3942. value_type* __p = __buff;
  3943. for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
  3944. ::new(__p) value_type(_VSTD::move(*__i));
  3945. __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
  3946. }
  3947. else
  3948. {
  3949. value_type* __p = __buff;
  3950. for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
  3951. ::new(__p) value_type(_VSTD::move(*__i));
  3952. typedef reverse_iterator<_BidirectionalIterator> _RBi;
  3953. typedef reverse_iterator<value_type*> _Rv;
  3954. __half_inplace_merge(_Rv(__p), _Rv(__buff),
  3955. _RBi(__middle), _RBi(__first),
  3956. _RBi(__last), __invert<_Compare>(__comp));
  3957. }
  3958. }
  3959. template <class _Compare, class _BidirectionalIterator>
  3960. void
  3961. __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
  3962. _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
  3963. typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
  3964. typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
  3965. {
  3966. typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
  3967. while (true)
  3968. {
  3969. // if __middle == __last, we're done
  3970. if (__len2 == 0)
  3971. return;
  3972. if (__len1 <= __buff_size || __len2 <= __buff_size)
  3973. return __buffered_inplace_merge<_Compare>
  3974. (__first, __middle, __last, __comp, __len1, __len2, __buff);
  3975. // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
  3976. for (; true; ++__first, (void) --__len1)
  3977. {
  3978. if (__len1 == 0)
  3979. return;
  3980. if (__comp(*__middle, *__first))
  3981. break;
  3982. }
  3983. // __first < __middle < __last
  3984. // *__first > *__middle
  3985. // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
  3986. // all elements in:
  3987. // [__first, __m1) <= [__middle, __m2)
  3988. // [__middle, __m2) < [__m1, __middle)
  3989. // [__m1, __middle) <= [__m2, __last)
  3990. // and __m1 or __m2 is in the middle of its range
  3991. _BidirectionalIterator __m1; // "median" of [__first, __middle)
  3992. _BidirectionalIterator __m2; // "median" of [__middle, __last)
  3993. difference_type __len11; // distance(__first, __m1)
  3994. difference_type __len21; // distance(__middle, __m2)
  3995. // binary search smaller range
  3996. if (__len1 < __len2)
  3997. { // __len >= 1, __len2 >= 2
  3998. __len21 = __len2 / 2;
  3999. __m2 = __middle;
  4000. _VSTD::advance(__m2, __len21);
  4001. __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
  4002. __len11 = _VSTD::distance(__first, __m1);
  4003. }
  4004. else
  4005. {
  4006. if (__len1 == 1)
  4007. { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
  4008. // It is known *__first > *__middle
  4009. swap(*__first, *__middle);
  4010. return;
  4011. }
  4012. // __len1 >= 2, __len2 >= 1
  4013. __len11 = __len1 / 2;
  4014. __m1 = __first;
  4015. _VSTD::advance(__m1, __len11);
  4016. __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
  4017. __len21 = _VSTD::distance(__middle, __m2);
  4018. }
  4019. difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
  4020. difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
  4021. // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
  4022. // swap middle two partitions
  4023. __middle = _VSTD::rotate(__m1, __middle, __m2);
  4024. // __len12 and __len21 now have swapped meanings
  4025. // merge smaller range with recurisve call and larger with tail recursion elimination
  4026. if (__len11 + __len21 < __len12 + __len22)
  4027. {
  4028. __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
  4029. // __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
  4030. __first = __middle;
  4031. __middle = __m2;
  4032. __len1 = __len12;
  4033. __len2 = __len22;
  4034. }
  4035. else
  4036. {
  4037. __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
  4038. // __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
  4039. __last = __middle;
  4040. __middle = __m1;
  4041. __len1 = __len11;
  4042. __len2 = __len21;
  4043. }
  4044. }
  4045. }
  4046. template <class _BidirectionalIterator, class _Compare>
  4047. inline _LIBCPP_INLINE_VISIBILITY
  4048. void
  4049. inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
  4050. _Compare __comp)
  4051. {
  4052. typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
  4053. typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
  4054. difference_type __len1 = _VSTD::distance(__first, __middle);
  4055. difference_type __len2 = _VSTD::distance(__middle, __last);
  4056. difference_type __buf_size = _VSTD::min(__len1, __len2);
  4057. pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
  4058. unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
  4059. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4060. return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
  4061. __buf.first, __buf.second);
  4062. }
  4063. template <class _BidirectionalIterator>
  4064. inline _LIBCPP_INLINE_VISIBILITY
  4065. void
  4066. inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
  4067. {
  4068. _VSTD::inplace_merge(__first, __middle, __last,
  4069. __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
  4070. }
  4071. // stable_sort
  4072. template <class _Compare, class _InputIterator1, class _InputIterator2>
  4073. void
  4074. __merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
  4075. _InputIterator2 __first2, _InputIterator2 __last2,
  4076. typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
  4077. {
  4078. typedef typename iterator_traits<_InputIterator1>::value_type value_type;
  4079. __destruct_n __d(0);
  4080. unique_ptr<value_type, __destruct_n&> __h(__result, __d);
  4081. for (; true; ++__result)
  4082. {
  4083. if (__first1 == __last1)
  4084. {
  4085. for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
  4086. ::new (__result) value_type(_VSTD::move(*__first2));
  4087. __h.release();
  4088. return;
  4089. }
  4090. if (__first2 == __last2)
  4091. {
  4092. for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
  4093. ::new (__result) value_type(_VSTD::move(*__first1));
  4094. __h.release();
  4095. return;
  4096. }
  4097. if (__comp(*__first2, *__first1))
  4098. {
  4099. ::new (__result) value_type(_VSTD::move(*__first2));
  4100. __d.__incr((value_type*)0);
  4101. ++__first2;
  4102. }
  4103. else
  4104. {
  4105. ::new (__result) value_type(_VSTD::move(*__first1));
  4106. __d.__incr((value_type*)0);
  4107. ++__first1;
  4108. }
  4109. }
  4110. }
  4111. template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
  4112. void
  4113. __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
  4114. _InputIterator2 __first2, _InputIterator2 __last2,
  4115. _OutputIterator __result, _Compare __comp)
  4116. {
  4117. for (; __first1 != __last1; ++__result)
  4118. {
  4119. if (__first2 == __last2)
  4120. {
  4121. for (; __first1 != __last1; ++__first1, ++__result)
  4122. *__result = _VSTD::move(*__first1);
  4123. return;
  4124. }
  4125. if (__comp(*__first2, *__first1))
  4126. {
  4127. *__result = _VSTD::move(*__first2);
  4128. ++__first2;
  4129. }
  4130. else
  4131. {
  4132. *__result = _VSTD::move(*__first1);
  4133. ++__first1;
  4134. }
  4135. }
  4136. for (; __first2 != __last2; ++__first2, ++__result)
  4137. *__result = _VSTD::move(*__first2);
  4138. }
  4139. template <class _Compare, class _RandomAccessIterator>
  4140. void
  4141. __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
  4142. typename iterator_traits<_RandomAccessIterator>::difference_type __len,
  4143. typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
  4144. template <class _Compare, class _RandomAccessIterator>
  4145. void
  4146. __stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
  4147. typename iterator_traits<_RandomAccessIterator>::difference_type __len,
  4148. typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
  4149. {
  4150. typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
  4151. switch (__len)
  4152. {
  4153. case 0:
  4154. return;
  4155. case 1:
  4156. ::new(__first2) value_type(_VSTD::move(*__first1));
  4157. return;
  4158. case 2:
  4159. __destruct_n __d(0);
  4160. unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
  4161. if (__comp(*--__last1, *__first1))
  4162. {
  4163. ::new(__first2) value_type(_VSTD::move(*__last1));
  4164. __d.__incr((value_type*)0);
  4165. ++__first2;
  4166. ::new(__first2) value_type(_VSTD::move(*__first1));
  4167. }
  4168. else
  4169. {
  4170. ::new(__first2) value_type(_VSTD::move(*__first1));
  4171. __d.__incr((value_type*)0);
  4172. ++__first2;
  4173. ::new(__first2) value_type(_VSTD::move(*__last1));
  4174. }
  4175. __h2.release();
  4176. return;
  4177. }
  4178. if (__len <= 8)
  4179. {
  4180. __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
  4181. return;
  4182. }
  4183. typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
  4184. _RandomAccessIterator __m = __first1 + __l2;
  4185. __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
  4186. __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
  4187. __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
  4188. }
  4189. template <class _Tp>
  4190. struct __stable_sort_switch
  4191. {
  4192. static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
  4193. };
  4194. template <class _Compare, class _RandomAccessIterator>
  4195. void
  4196. __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
  4197. typename iterator_traits<_RandomAccessIterator>::difference_type __len,
  4198. typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
  4199. {
  4200. typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
  4201. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  4202. switch (__len)
  4203. {
  4204. case 0:
  4205. case 1:
  4206. return;
  4207. case 2:
  4208. if (__comp(*--__last, *__first))
  4209. swap(*__first, *__last);
  4210. return;
  4211. }
  4212. if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
  4213. {
  4214. __insertion_sort<_Compare>(__first, __last, __comp);
  4215. return;
  4216. }
  4217. typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
  4218. _RandomAccessIterator __m = __first + __l2;
  4219. if (__len <= __buff_size)
  4220. {
  4221. __destruct_n __d(0);
  4222. unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
  4223. __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
  4224. __d.__set(__l2, (value_type*)0);
  4225. __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
  4226. __d.__set(__len, (value_type*)0);
  4227. __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
  4228. // __merge<_Compare>(move_iterator<value_type*>(__buff),
  4229. // move_iterator<value_type*>(__buff + __l2),
  4230. // move_iterator<_RandomAccessIterator>(__buff + __l2),
  4231. // move_iterator<_RandomAccessIterator>(__buff + __len),
  4232. // __first, __comp);
  4233. return;
  4234. }
  4235. __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
  4236. __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
  4237. __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
  4238. }
  4239. template <class _RandomAccessIterator, class _Compare>
  4240. inline _LIBCPP_INLINE_VISIBILITY
  4241. void
  4242. stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  4243. {
  4244. typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
  4245. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  4246. difference_type __len = __last - __first;
  4247. pair<value_type*, ptrdiff_t> __buf(0, 0);
  4248. unique_ptr<value_type, __return_temporary_buffer> __h;
  4249. if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
  4250. {
  4251. __buf = _VSTD::get_temporary_buffer<value_type>(__len);
  4252. __h.reset(__buf.first);
  4253. }
  4254. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4255. __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
  4256. }
  4257. template <class _RandomAccessIterator>
  4258. inline _LIBCPP_INLINE_VISIBILITY
  4259. void
  4260. stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
  4261. {
  4262. _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  4263. }
  4264. // is_heap_until
  4265. template <class _RandomAccessIterator, class _Compare>
  4266. _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
  4267. is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  4268. {
  4269. typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  4270. difference_type __len = __last - __first;
  4271. difference_type __p = 0;
  4272. difference_type __c = 1;
  4273. _RandomAccessIterator __pp = __first;
  4274. while (__c < __len)
  4275. {
  4276. _RandomAccessIterator __cp = __first + __c;
  4277. if (__comp(*__pp, *__cp))
  4278. return __cp;
  4279. ++__c;
  4280. ++__cp;
  4281. if (__c == __len)
  4282. return __last;
  4283. if (__comp(*__pp, *__cp))
  4284. return __cp;
  4285. ++__p;
  4286. ++__pp;
  4287. __c = 2 * __p + 1;
  4288. }
  4289. return __last;
  4290. }
  4291. template<class _RandomAccessIterator>
  4292. _LIBCPP_NODISCARD_EXT inline
  4293. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  4294. _RandomAccessIterator
  4295. is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
  4296. {
  4297. return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  4298. }
  4299. // is_heap
  4300. template <class _RandomAccessIterator, class _Compare>
  4301. _LIBCPP_NODISCARD_EXT inline
  4302. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  4303. bool
  4304. is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  4305. {
  4306. return _VSTD::is_heap_until(__first, __last, __comp) == __last;
  4307. }
  4308. template<class _RandomAccessIterator>
  4309. _LIBCPP_NODISCARD_EXT inline
  4310. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  4311. bool
  4312. is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
  4313. {
  4314. return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  4315. }
  4316. // push_heap
  4317. template <class _Compare, class _RandomAccessIterator>
  4318. void
  4319. __sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
  4320. typename iterator_traits<_RandomAccessIterator>::difference_type __len)
  4321. {
  4322. typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
  4323. if (__len > 1)
  4324. {
  4325. __len = (__len - 2) / 2;
  4326. _RandomAccessIterator __ptr = __first + __len;
  4327. if (__comp(*__ptr, *--__last))
  4328. {
  4329. value_type __t(_VSTD::move(*__last));
  4330. do
  4331. {
  4332. *__last = _VSTD::move(*__ptr);
  4333. __last = __ptr;
  4334. if (__len == 0)
  4335. break;
  4336. __len = (__len - 1) / 2;
  4337. __ptr = __first + __len;
  4338. } while (__comp(*__ptr, __t));
  4339. *__last = _VSTD::move(__t);
  4340. }
  4341. }
  4342. }
  4343. template <class _RandomAccessIterator, class _Compare>
  4344. inline _LIBCPP_INLINE_VISIBILITY
  4345. void
  4346. push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  4347. {
  4348. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4349. __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
  4350. }
  4351. template <class _RandomAccessIterator>
  4352. inline _LIBCPP_INLINE_VISIBILITY
  4353. void
  4354. push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
  4355. {
  4356. _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  4357. }
  4358. // pop_heap
  4359. template <class _Compare, class _RandomAccessIterator>
  4360. void
  4361. __sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
  4362. _Compare __comp,
  4363. typename iterator_traits<_RandomAccessIterator>::difference_type __len,
  4364. _RandomAccessIterator __start)
  4365. {
  4366. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  4367. typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
  4368. // left-child of __start is at 2 * __start + 1
  4369. // right-child of __start is at 2 * __start + 2
  4370. difference_type __child = __start - __first;
  4371. if (__len < 2 || (__len - 2) / 2 < __child)
  4372. return;
  4373. __child = 2 * __child + 1;
  4374. _RandomAccessIterator __child_i = __first + __child;
  4375. if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
  4376. // right-child exists and is greater than left-child
  4377. ++__child_i;
  4378. ++__child;
  4379. }
  4380. // check if we are in heap-order
  4381. if (__comp(*__child_i, *__start))
  4382. // we are, __start is larger than it's largest child
  4383. return;
  4384. value_type __top(_VSTD::move(*__start));
  4385. do
  4386. {
  4387. // we are not in heap-order, swap the parent with it's largest child
  4388. *__start = _VSTD::move(*__child_i);
  4389. __start = __child_i;
  4390. if ((__len - 2) / 2 < __child)
  4391. break;
  4392. // recompute the child based off of the updated parent
  4393. __child = 2 * __child + 1;
  4394. __child_i = __first + __child;
  4395. if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
  4396. // right-child exists and is greater than left-child
  4397. ++__child_i;
  4398. ++__child;
  4399. }
  4400. // check if we are in heap-order
  4401. } while (!__comp(*__child_i, __top));
  4402. *__start = _VSTD::move(__top);
  4403. }
  4404. template <class _Compare, class _RandomAccessIterator>
  4405. inline _LIBCPP_INLINE_VISIBILITY
  4406. void
  4407. __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
  4408. typename iterator_traits<_RandomAccessIterator>::difference_type __len)
  4409. {
  4410. if (__len > 1)
  4411. {
  4412. swap(*__first, *--__last);
  4413. __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
  4414. }
  4415. }
  4416. template <class _RandomAccessIterator, class _Compare>
  4417. inline _LIBCPP_INLINE_VISIBILITY
  4418. void
  4419. pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  4420. {
  4421. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4422. __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
  4423. }
  4424. template <class _RandomAccessIterator>
  4425. inline _LIBCPP_INLINE_VISIBILITY
  4426. void
  4427. pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
  4428. {
  4429. _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  4430. }
  4431. // make_heap
  4432. template <class _Compare, class _RandomAccessIterator>
  4433. void
  4434. __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  4435. {
  4436. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  4437. difference_type __n = __last - __first;
  4438. if (__n > 1)
  4439. {
  4440. // start from the first parent, there is no need to consider children
  4441. for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
  4442. {
  4443. __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
  4444. }
  4445. }
  4446. }
  4447. template <class _RandomAccessIterator, class _Compare>
  4448. inline _LIBCPP_INLINE_VISIBILITY
  4449. void
  4450. make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  4451. {
  4452. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4453. __make_heap<_Comp_ref>(__first, __last, __comp);
  4454. }
  4455. template <class _RandomAccessIterator>
  4456. inline _LIBCPP_INLINE_VISIBILITY
  4457. void
  4458. make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
  4459. {
  4460. _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  4461. }
  4462. // sort_heap
  4463. template <class _Compare, class _RandomAccessIterator>
  4464. void
  4465. __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  4466. {
  4467. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  4468. for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
  4469. __pop_heap<_Compare>(__first, __last, __comp, __n);
  4470. }
  4471. template <class _RandomAccessIterator, class _Compare>
  4472. inline _LIBCPP_INLINE_VISIBILITY
  4473. void
  4474. sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
  4475. {
  4476. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4477. __sort_heap<_Comp_ref>(__first, __last, __comp);
  4478. }
  4479. template <class _RandomAccessIterator>
  4480. inline _LIBCPP_INLINE_VISIBILITY
  4481. void
  4482. sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
  4483. {
  4484. _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  4485. }
  4486. // partial_sort
  4487. template <class _Compare, class _RandomAccessIterator>
  4488. void
  4489. __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
  4490. _Compare __comp)
  4491. {
  4492. __make_heap<_Compare>(__first, __middle, __comp);
  4493. typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
  4494. for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
  4495. {
  4496. if (__comp(*__i, *__first))
  4497. {
  4498. swap(*__i, *__first);
  4499. __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
  4500. }
  4501. }
  4502. __sort_heap<_Compare>(__first, __middle, __comp);
  4503. }
  4504. template <class _RandomAccessIterator, class _Compare>
  4505. inline _LIBCPP_INLINE_VISIBILITY
  4506. void
  4507. partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
  4508. _Compare __comp)
  4509. {
  4510. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4511. __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
  4512. }
  4513. template <class _RandomAccessIterator>
  4514. inline _LIBCPP_INLINE_VISIBILITY
  4515. void
  4516. partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
  4517. {
  4518. _VSTD::partial_sort(__first, __middle, __last,
  4519. __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  4520. }
  4521. // partial_sort_copy
  4522. template <class _Compare, class _InputIterator, class _RandomAccessIterator>
  4523. _RandomAccessIterator
  4524. __partial_sort_copy(_InputIterator __first, _InputIterator __last,
  4525. _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
  4526. {
  4527. _RandomAccessIterator __r = __result_first;
  4528. if (__r != __result_last)
  4529. {
  4530. for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
  4531. *__r = *__first;
  4532. __make_heap<_Compare>(__result_first, __r, __comp);
  4533. typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
  4534. for (; __first != __last; ++__first)
  4535. if (__comp(*__first, *__result_first))
  4536. {
  4537. *__result_first = *__first;
  4538. __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
  4539. }
  4540. __sort_heap<_Compare>(__result_first, __r, __comp);
  4541. }
  4542. return __r;
  4543. }
  4544. template <class _InputIterator, class _RandomAccessIterator, class _Compare>
  4545. inline _LIBCPP_INLINE_VISIBILITY
  4546. _RandomAccessIterator
  4547. partial_sort_copy(_InputIterator __first, _InputIterator __last,
  4548. _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
  4549. {
  4550. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4551. return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
  4552. }
  4553. template <class _InputIterator, class _RandomAccessIterator>
  4554. inline _LIBCPP_INLINE_VISIBILITY
  4555. _RandomAccessIterator
  4556. partial_sort_copy(_InputIterator __first, _InputIterator __last,
  4557. _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
  4558. {
  4559. return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
  4560. __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  4561. }
  4562. // nth_element
  4563. template <class _Compare, class _RandomAccessIterator>
  4564. void
  4565. __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
  4566. {
  4567. // _Compare is known to be a reference type
  4568. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  4569. const difference_type __limit = 7;
  4570. while (true)
  4571. {
  4572. __restart:
  4573. if (__nth == __last)
  4574. return;
  4575. difference_type __len = __last - __first;
  4576. switch (__len)
  4577. {
  4578. case 0:
  4579. case 1:
  4580. return;
  4581. case 2:
  4582. if (__comp(*--__last, *__first))
  4583. swap(*__first, *__last);
  4584. return;
  4585. case 3:
  4586. {
  4587. _RandomAccessIterator __m = __first;
  4588. _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
  4589. return;
  4590. }
  4591. }
  4592. if (__len <= __limit)
  4593. {
  4594. __selection_sort<_Compare>(__first, __last, __comp);
  4595. return;
  4596. }
  4597. // __len > __limit >= 3
  4598. _RandomAccessIterator __m = __first + __len/2;
  4599. _RandomAccessIterator __lm1 = __last;
  4600. unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
  4601. // *__m is median
  4602. // partition [__first, __m) < *__m and *__m <= [__m, __last)
  4603. // (this inhibits tossing elements equivalent to __m around unnecessarily)
  4604. _RandomAccessIterator __i = __first;
  4605. _RandomAccessIterator __j = __lm1;
  4606. // j points beyond range to be tested, *__lm1 is known to be <= *__m
  4607. // The search going up is known to be guarded but the search coming down isn't.
  4608. // Prime the downward search with a guard.
  4609. if (!__comp(*__i, *__m)) // if *__first == *__m
  4610. {
  4611. // *__first == *__m, *__first doesn't go in first part
  4612. // manually guard downward moving __j against __i
  4613. while (true)
  4614. {
  4615. if (__i == --__j)
  4616. {
  4617. // *__first == *__m, *__m <= all other elements
  4618. // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
  4619. ++__i; // __first + 1
  4620. __j = __last;
  4621. if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
  4622. {
  4623. while (true)
  4624. {
  4625. if (__i == __j)
  4626. return; // [__first, __last) all equivalent elements
  4627. if (__comp(*__first, *__i))
  4628. {
  4629. swap(*__i, *__j);
  4630. ++__n_swaps;
  4631. ++__i;
  4632. break;
  4633. }
  4634. ++__i;
  4635. }
  4636. }
  4637. // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
  4638. if (__i == __j)
  4639. return;
  4640. while (true)
  4641. {
  4642. while (!__comp(*__first, *__i))
  4643. ++__i;
  4644. while (__comp(*__first, *--__j))
  4645. ;
  4646. if (__i >= __j)
  4647. break;
  4648. swap(*__i, *__j);
  4649. ++__n_swaps;
  4650. ++__i;
  4651. }
  4652. // [__first, __i) == *__first and *__first < [__i, __last)
  4653. // The first part is sorted,
  4654. if (__nth < __i)
  4655. return;
  4656. // __nth_element the secod part
  4657. // __nth_element<_Compare>(__i, __nth, __last, __comp);
  4658. __first = __i;
  4659. goto __restart;
  4660. }
  4661. if (__comp(*__j, *__m))
  4662. {
  4663. swap(*__i, *__j);
  4664. ++__n_swaps;
  4665. break; // found guard for downward moving __j, now use unguarded partition
  4666. }
  4667. }
  4668. }
  4669. ++__i;
  4670. // j points beyond range to be tested, *__lm1 is known to be <= *__m
  4671. // if not yet partitioned...
  4672. if (__i < __j)
  4673. {
  4674. // known that *(__i - 1) < *__m
  4675. while (true)
  4676. {
  4677. // __m still guards upward moving __i
  4678. while (__comp(*__i, *__m))
  4679. ++__i;
  4680. // It is now known that a guard exists for downward moving __j
  4681. while (!__comp(*--__j, *__m))
  4682. ;
  4683. if (__i >= __j)
  4684. break;
  4685. swap(*__i, *__j);
  4686. ++__n_swaps;
  4687. // It is known that __m != __j
  4688. // If __m just moved, follow it
  4689. if (__m == __i)
  4690. __m = __j;
  4691. ++__i;
  4692. }
  4693. }
  4694. // [__first, __i) < *__m and *__m <= [__i, __last)
  4695. if (__i != __m && __comp(*__m, *__i))
  4696. {
  4697. swap(*__i, *__m);
  4698. ++__n_swaps;
  4699. }
  4700. // [__first, __i) < *__i and *__i <= [__i+1, __last)
  4701. if (__nth == __i)
  4702. return;
  4703. if (__n_swaps == 0)
  4704. {
  4705. // We were given a perfectly partitioned sequence. Coincidence?
  4706. if (__nth < __i)
  4707. {
  4708. // Check for [__first, __i) already sorted
  4709. __j = __m = __first;
  4710. while (++__j != __i)
  4711. {
  4712. if (__comp(*__j, *__m))
  4713. // not yet sorted, so sort
  4714. goto not_sorted;
  4715. __m = __j;
  4716. }
  4717. // [__first, __i) sorted
  4718. return;
  4719. }
  4720. else
  4721. {
  4722. // Check for [__i, __last) already sorted
  4723. __j = __m = __i;
  4724. while (++__j != __last)
  4725. {
  4726. if (__comp(*__j, *__m))
  4727. // not yet sorted, so sort
  4728. goto not_sorted;
  4729. __m = __j;
  4730. }
  4731. // [__i, __last) sorted
  4732. return;
  4733. }
  4734. }
  4735. not_sorted:
  4736. // __nth_element on range containing __nth
  4737. if (__nth < __i)
  4738. {
  4739. // __nth_element<_Compare>(__first, __nth, __i, __comp);
  4740. __last = __i;
  4741. }
  4742. else
  4743. {
  4744. // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
  4745. __first = ++__i;
  4746. }
  4747. }
  4748. }
  4749. template <class _RandomAccessIterator, class _Compare>
  4750. inline _LIBCPP_INLINE_VISIBILITY
  4751. void
  4752. nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
  4753. {
  4754. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4755. __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
  4756. }
  4757. template <class _RandomAccessIterator>
  4758. inline _LIBCPP_INLINE_VISIBILITY
  4759. void
  4760. nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
  4761. {
  4762. _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  4763. }
  4764. // includes
  4765. template <class _Compare, class _InputIterator1, class _InputIterator2>
  4766. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  4767. __includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
  4768. _Compare __comp)
  4769. {
  4770. for (; __first2 != __last2; ++__first1)
  4771. {
  4772. if (__first1 == __last1 || __comp(*__first2, *__first1))
  4773. return false;
  4774. if (!__comp(*__first1, *__first2))
  4775. ++__first2;
  4776. }
  4777. return true;
  4778. }
  4779. template <class _InputIterator1, class _InputIterator2, class _Compare>
  4780. _LIBCPP_NODISCARD_EXT inline
  4781. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  4782. bool
  4783. includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
  4784. _Compare __comp)
  4785. {
  4786. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4787. return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
  4788. }
  4789. template <class _InputIterator1, class _InputIterator2>
  4790. _LIBCPP_NODISCARD_EXT inline
  4791. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  4792. bool
  4793. includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
  4794. {
  4795. return _VSTD::includes(__first1, __last1, __first2, __last2,
  4796. __less<typename iterator_traits<_InputIterator1>::value_type,
  4797. typename iterator_traits<_InputIterator2>::value_type>());
  4798. }
  4799. // set_union
  4800. template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
  4801. _OutputIterator
  4802. __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
  4803. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
  4804. {
  4805. for (; __first1 != __last1; ++__result)
  4806. {
  4807. if (__first2 == __last2)
  4808. return _VSTD::copy(__first1, __last1, __result);
  4809. if (__comp(*__first2, *__first1))
  4810. {
  4811. *__result = *__first2;
  4812. ++__first2;
  4813. }
  4814. else
  4815. {
  4816. if (!__comp(*__first1, *__first2))
  4817. ++__first2;
  4818. *__result = *__first1;
  4819. ++__first1;
  4820. }
  4821. }
  4822. return _VSTD::copy(__first2, __last2, __result);
  4823. }
  4824. template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
  4825. inline _LIBCPP_INLINE_VISIBILITY
  4826. _OutputIterator
  4827. set_union(_InputIterator1 __first1, _InputIterator1 __last1,
  4828. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
  4829. {
  4830. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4831. return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
  4832. }
  4833. template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
  4834. inline _LIBCPP_INLINE_VISIBILITY
  4835. _OutputIterator
  4836. set_union(_InputIterator1 __first1, _InputIterator1 __last1,
  4837. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
  4838. {
  4839. return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
  4840. __less<typename iterator_traits<_InputIterator1>::value_type,
  4841. typename iterator_traits<_InputIterator2>::value_type>());
  4842. }
  4843. // set_intersection
  4844. template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
  4845. _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
  4846. __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
  4847. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
  4848. {
  4849. while (__first1 != __last1 && __first2 != __last2)
  4850. {
  4851. if (__comp(*__first1, *__first2))
  4852. ++__first1;
  4853. else
  4854. {
  4855. if (!__comp(*__first2, *__first1))
  4856. {
  4857. *__result = *__first1;
  4858. ++__result;
  4859. ++__first1;
  4860. }
  4861. ++__first2;
  4862. }
  4863. }
  4864. return __result;
  4865. }
  4866. template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
  4867. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  4868. _OutputIterator
  4869. set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
  4870. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
  4871. {
  4872. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4873. return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
  4874. }
  4875. template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
  4876. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  4877. _OutputIterator
  4878. set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
  4879. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
  4880. {
  4881. return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
  4882. __less<typename iterator_traits<_InputIterator1>::value_type,
  4883. typename iterator_traits<_InputIterator2>::value_type>());
  4884. }
  4885. // set_difference
  4886. template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
  4887. _OutputIterator
  4888. __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
  4889. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
  4890. {
  4891. while (__first1 != __last1)
  4892. {
  4893. if (__first2 == __last2)
  4894. return _VSTD::copy(__first1, __last1, __result);
  4895. if (__comp(*__first1, *__first2))
  4896. {
  4897. *__result = *__first1;
  4898. ++__result;
  4899. ++__first1;
  4900. }
  4901. else
  4902. {
  4903. if (!__comp(*__first2, *__first1))
  4904. ++__first1;
  4905. ++__first2;
  4906. }
  4907. }
  4908. return __result;
  4909. }
  4910. template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
  4911. inline _LIBCPP_INLINE_VISIBILITY
  4912. _OutputIterator
  4913. set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
  4914. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
  4915. {
  4916. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4917. return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
  4918. }
  4919. template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
  4920. inline _LIBCPP_INLINE_VISIBILITY
  4921. _OutputIterator
  4922. set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
  4923. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
  4924. {
  4925. return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
  4926. __less<typename iterator_traits<_InputIterator1>::value_type,
  4927. typename iterator_traits<_InputIterator2>::value_type>());
  4928. }
  4929. // set_symmetric_difference
  4930. template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
  4931. _OutputIterator
  4932. __set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
  4933. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
  4934. {
  4935. while (__first1 != __last1)
  4936. {
  4937. if (__first2 == __last2)
  4938. return _VSTD::copy(__first1, __last1, __result);
  4939. if (__comp(*__first1, *__first2))
  4940. {
  4941. *__result = *__first1;
  4942. ++__result;
  4943. ++__first1;
  4944. }
  4945. else
  4946. {
  4947. if (__comp(*__first2, *__first1))
  4948. {
  4949. *__result = *__first2;
  4950. ++__result;
  4951. }
  4952. else
  4953. ++__first1;
  4954. ++__first2;
  4955. }
  4956. }
  4957. return _VSTD::copy(__first2, __last2, __result);
  4958. }
  4959. template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
  4960. inline _LIBCPP_INLINE_VISIBILITY
  4961. _OutputIterator
  4962. set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
  4963. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
  4964. {
  4965. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  4966. return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
  4967. }
  4968. template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
  4969. inline _LIBCPP_INLINE_VISIBILITY
  4970. _OutputIterator
  4971. set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
  4972. _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
  4973. {
  4974. return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
  4975. __less<typename iterator_traits<_InputIterator1>::value_type,
  4976. typename iterator_traits<_InputIterator2>::value_type>());
  4977. }
  4978. // lexicographical_compare
  4979. template <class _Compare, class _InputIterator1, class _InputIterator2>
  4980. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  4981. __lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
  4982. _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
  4983. {
  4984. for (; __first2 != __last2; ++__first1, (void) ++__first2)
  4985. {
  4986. if (__first1 == __last1 || __comp(*__first1, *__first2))
  4987. return true;
  4988. if (__comp(*__first2, *__first1))
  4989. return false;
  4990. }
  4991. return false;
  4992. }
  4993. template <class _InputIterator1, class _InputIterator2, class _Compare>
  4994. _LIBCPP_NODISCARD_EXT inline
  4995. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  4996. bool
  4997. lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
  4998. _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
  4999. {
  5000. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  5001. return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
  5002. }
  5003. template <class _InputIterator1, class _InputIterator2>
  5004. _LIBCPP_NODISCARD_EXT inline
  5005. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  5006. bool
  5007. lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
  5008. _InputIterator2 __first2, _InputIterator2 __last2)
  5009. {
  5010. return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
  5011. __less<typename iterator_traits<_InputIterator1>::value_type,
  5012. typename iterator_traits<_InputIterator2>::value_type>());
  5013. }
  5014. // next_permutation
  5015. template <class _Compare, class _BidirectionalIterator>
  5016. bool
  5017. __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
  5018. {
  5019. _BidirectionalIterator __i = __last;
  5020. if (__first == __last || __first == --__i)
  5021. return false;
  5022. while (true)
  5023. {
  5024. _BidirectionalIterator __ip1 = __i;
  5025. if (__comp(*--__i, *__ip1))
  5026. {
  5027. _BidirectionalIterator __j = __last;
  5028. while (!__comp(*__i, *--__j))
  5029. ;
  5030. swap(*__i, *__j);
  5031. _VSTD::reverse(__ip1, __last);
  5032. return true;
  5033. }
  5034. if (__i == __first)
  5035. {
  5036. _VSTD::reverse(__first, __last);
  5037. return false;
  5038. }
  5039. }
  5040. }
  5041. template <class _BidirectionalIterator, class _Compare>
  5042. inline _LIBCPP_INLINE_VISIBILITY
  5043. bool
  5044. next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
  5045. {
  5046. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  5047. return __next_permutation<_Comp_ref>(__first, __last, __comp);
  5048. }
  5049. template <class _BidirectionalIterator>
  5050. inline _LIBCPP_INLINE_VISIBILITY
  5051. bool
  5052. next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
  5053. {
  5054. return _VSTD::next_permutation(__first, __last,
  5055. __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
  5056. }
  5057. // prev_permutation
  5058. template <class _Compare, class _BidirectionalIterator>
  5059. bool
  5060. __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
  5061. {
  5062. _BidirectionalIterator __i = __last;
  5063. if (__first == __last || __first == --__i)
  5064. return false;
  5065. while (true)
  5066. {
  5067. _BidirectionalIterator __ip1 = __i;
  5068. if (__comp(*__ip1, *--__i))
  5069. {
  5070. _BidirectionalIterator __j = __last;
  5071. while (!__comp(*--__j, *__i))
  5072. ;
  5073. swap(*__i, *__j);
  5074. _VSTD::reverse(__ip1, __last);
  5075. return true;
  5076. }
  5077. if (__i == __first)
  5078. {
  5079. _VSTD::reverse(__first, __last);
  5080. return false;
  5081. }
  5082. }
  5083. }
  5084. template <class _BidirectionalIterator, class _Compare>
  5085. inline _LIBCPP_INLINE_VISIBILITY
  5086. bool
  5087. prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
  5088. {
  5089. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  5090. return __prev_permutation<_Comp_ref>(__first, __last, __comp);
  5091. }
  5092. template <class _BidirectionalIterator>
  5093. inline _LIBCPP_INLINE_VISIBILITY
  5094. bool
  5095. prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
  5096. {
  5097. return _VSTD::prev_permutation(__first, __last,
  5098. __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
  5099. }
  5100. _LIBCPP_END_NAMESPACE_STD
  5101. _LIBCPP_POP_MACROS
  5102. #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
  5103. # include <pstl/internal/glue_algorithm_impl.h>
  5104. #endif
  5105. #endif // _LIBCPP_ALGORITHM