algorithm 200 KB

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