algorithm.h 202 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_RANGES_ALGORITHM_H_
  5. #define BASE_RANGES_ALGORITHM_H_
  6. #include <algorithm>
  7. #include <initializer_list>
  8. #include <iterator>
  9. #include <type_traits>
  10. #include <utility>
  11. #include "base/ranges/functional.h"
  12. #include "base/ranges/ranges.h"
  13. #include "base/stl_util.h"
  14. #include "base/template_util.h"
  15. namespace base {
  16. namespace internal {
  17. // Returns a transformed version of the unary predicate `pred` applying `proj`
  18. // to its argument before invoking `pred` on it.
  19. // Ensures that the return type of `invoke(pred, ...)` is convertible to bool.
  20. template <typename Pred, typename Proj>
  21. constexpr auto ProjectedUnaryPredicate(Pred& pred, Proj& proj) noexcept {
  22. return [&pred, &proj](auto&& arg) -> bool {
  23. return base::invoke(pred,
  24. base::invoke(proj, std::forward<decltype(arg)>(arg)));
  25. };
  26. }
  27. // Returns a transformed version of the binary predicate `pred` applying `proj1`
  28. // and `proj2` to its arguments before invoking `pred` on them.
  29. //
  30. // Provides an opt-in to considers all four permutations of projections and
  31. // argument types. This is sometimes necessary to allow usage with legacy
  32. // non-ranges std:: algorithms that don't support projections.
  33. //
  34. // These permutations are assigned different priorities to break ambiguities in
  35. // case several permutations are possible, e.g. when Proj1 and Proj2 are the
  36. // same type.
  37. //
  38. // Note that even when opting in to using all permutations of projections,
  39. // calling code should still ensure that the canonical mapping of {Proj1, Proj2}
  40. // to {LHS, RHS} compiles for all members of the range. This can be done by
  41. // adding the following constraint:
  42. //
  43. // typename = indirect_result_t<Pred&,
  44. // projected<iterator_t<Range1>, Proj1>,
  45. // projected<iterator_t<Range2>, Proj2>>
  46. //
  47. // Ensures that the return type of `invoke(pred, ...)` is convertible to bool.
  48. template <typename Pred, typename Proj1, typename Proj2, bool kPermute = false>
  49. class BinaryPredicateProjector {
  50. public:
  51. constexpr BinaryPredicateProjector(Pred& pred, Proj1& proj1, Proj2& proj2)
  52. : pred_(pred), proj1_(proj1), proj2_(proj2) {}
  53. private:
  54. template <typename ProjT, typename ProjU, typename T, typename U>
  55. using InvokeResult = invoke_result_t<Pred&,
  56. invoke_result_t<ProjT&, T&&>,
  57. invoke_result_t<ProjU&, U&&>>;
  58. template <typename T, typename U, typename = InvokeResult<Proj1, Proj2, T, U>>
  59. constexpr std::pair<Proj1&, Proj2&> GetProjs(priority_tag<3>) const {
  60. return {proj1_, proj2_};
  61. }
  62. template <typename T,
  63. typename U,
  64. bool LazyPermute = kPermute,
  65. typename = std::enable_if_t<LazyPermute>,
  66. typename = InvokeResult<Proj2, Proj1, T, U>>
  67. constexpr std::pair<Proj2&, Proj1&> GetProjs(priority_tag<2>) const {
  68. return {proj2_, proj1_};
  69. }
  70. template <typename T,
  71. typename U,
  72. bool LazyPermute = kPermute,
  73. typename = std::enable_if_t<LazyPermute>,
  74. typename = InvokeResult<Proj1, Proj1, T, U>>
  75. constexpr std::pair<Proj1&, Proj1&> GetProjs(priority_tag<1>) const {
  76. return {proj1_, proj1_};
  77. }
  78. template <typename T,
  79. typename U,
  80. bool LazyPermute = kPermute,
  81. typename = std::enable_if_t<LazyPermute>,
  82. typename = InvokeResult<Proj2, Proj2, T, U>>
  83. constexpr std::pair<Proj2&, Proj2&> GetProjs(priority_tag<0>) const {
  84. return {proj2_, proj2_};
  85. }
  86. public:
  87. template <typename T, typename U>
  88. constexpr bool operator()(T&& lhs, U&& rhs) const {
  89. auto projs = GetProjs<T, U>(priority_tag<3>());
  90. return base::invoke(pred_, base::invoke(projs.first, std::forward<T>(lhs)),
  91. base::invoke(projs.second, std::forward<U>(rhs)));
  92. }
  93. private:
  94. Pred& pred_;
  95. Proj1& proj1_;
  96. Proj2& proj2_;
  97. };
  98. // Small wrappers around BinaryPredicateProjector to make the calling side more
  99. // readable.
  100. template <typename Pred, typename Proj1, typename Proj2>
  101. constexpr auto ProjectedBinaryPredicate(Pred& pred,
  102. Proj1& proj1,
  103. Proj2& proj2) noexcept {
  104. return BinaryPredicateProjector<Pred, Proj1, Proj2>(pred, proj1, proj2);
  105. }
  106. template <typename Pred, typename Proj1, typename Proj2>
  107. constexpr auto PermutedProjectedBinaryPredicate(Pred& pred,
  108. Proj1& proj1,
  109. Proj2& proj2) noexcept {
  110. return BinaryPredicateProjector<Pred, Proj1, Proj2, true>(pred, proj1, proj2);
  111. }
  112. // This alias is used below to restrict iterator based APIs to types for which
  113. // `iterator_category` and the pre-increment and post-increment operators are
  114. // defined. This is required in situations where otherwise an undesired overload
  115. // would be chosen, e.g. copy_if. In spirit this is similar to C++20's
  116. // std::input_or_output_iterator, a concept that each iterator should satisfy.
  117. template <typename Iter,
  118. typename = decltype(++std::declval<Iter&>()),
  119. typename = decltype(std::declval<Iter&>()++)>
  120. using iterator_category_t =
  121. typename std::iterator_traits<Iter>::iterator_category;
  122. // This alias is used below to restrict range based APIs to types for which
  123. // `iterator_category_t` is defined for the underlying iterator. This is
  124. // required in situations where otherwise an undesired overload would be chosen,
  125. // e.g. transform. In spirit this is similar to C++20's std::ranges::range, a
  126. // concept that each range should satisfy.
  127. template <typename Range>
  128. using range_category_t = iterator_category_t<ranges::iterator_t<Range>>;
  129. } // namespace internal
  130. namespace ranges {
  131. // C++14 implementation of std::ranges::in_fun_result. Note the because C++14
  132. // lacks the `no_unique_address` attribute it is commented out.
  133. //
  134. // Reference: https://wg21.link/algorithms.results#:~:text=in_fun_result
  135. template <typename I, typename F>
  136. struct in_fun_result {
  137. /* [[no_unique_address]] */ I in;
  138. /* [[no_unique_address]] */ F fun;
  139. template <typename I2,
  140. typename F2,
  141. std::enable_if_t<std::is_convertible<const I&, I2>{} &&
  142. std::is_convertible<const F&, F2>{}>>
  143. constexpr operator in_fun_result<I2, F2>() const& {
  144. return {in, fun};
  145. }
  146. template <typename I2,
  147. typename F2,
  148. std::enable_if_t<std::is_convertible<I, I2>{} &&
  149. std::is_convertible<F, F2>{}>>
  150. constexpr operator in_fun_result<I2, F2>() && {
  151. return {std::move(in), std::move(fun)};
  152. }
  153. };
  154. // TODO(crbug.com/1071094): Implement the other result types.
  155. // [alg.nonmodifying] Non-modifying sequence operations
  156. // Reference: https://wg21.link/alg.nonmodifying
  157. // [alg.all.of] All of
  158. // Reference: https://wg21.link/alg.all.of
  159. // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
  160. //
  161. // Returns: `false` if `E(i)` is `false` for some iterator `i` in the range
  162. // `[first, last)`, and `true` otherwise.
  163. //
  164. // Complexity: At most `last - first` applications of the predicate and any
  165. // projection.
  166. //
  167. // Reference: https://wg21.link/alg.all.of#:~:text=ranges::all_of(I
  168. template <typename InputIterator,
  169. typename Pred,
  170. typename Proj = identity,
  171. typename = internal::iterator_category_t<InputIterator>>
  172. constexpr bool all_of(InputIterator first,
  173. InputIterator last,
  174. Pred pred,
  175. Proj proj = {}) {
  176. for (; first != last; ++first) {
  177. if (!invoke(pred, invoke(proj, *first)))
  178. return false;
  179. }
  180. return true;
  181. }
  182. // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
  183. //
  184. // Returns: `false` if `E(i)` is `false` for some iterator `i` in `range`, and
  185. // `true` otherwise.
  186. //
  187. // Complexity: At most `size(range)` applications of the predicate and any
  188. // projection.
  189. //
  190. // Reference: https://wg21.link/alg.all.of#:~:text=ranges::all_of(R
  191. template <typename Range,
  192. typename Pred,
  193. typename Proj = identity,
  194. typename = internal::range_category_t<Range>>
  195. constexpr bool all_of(Range&& range, Pred pred, Proj proj = {}) {
  196. return ranges::all_of(ranges::begin(range), ranges::end(range),
  197. std::move(pred), std::move(proj));
  198. }
  199. // [alg.any.of] Any of
  200. // Reference: https://wg21.link/alg.any.of
  201. // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
  202. //
  203. // Returns: `true` if `E(i)` is `true` for some iterator `i` in the range
  204. // `[first, last)`, and `false` otherwise.
  205. //
  206. // Complexity: At most `last - first` applications of the predicate and any
  207. // projection.
  208. //
  209. // Reference: https://wg21.link/alg.any.of#:~:text=ranges::any_of(I
  210. template <typename InputIterator,
  211. typename Pred,
  212. typename Proj = identity,
  213. typename = internal::iterator_category_t<InputIterator>>
  214. constexpr bool any_of(InputIterator first,
  215. InputIterator last,
  216. Pred pred,
  217. Proj proj = {}) {
  218. for (; first != last; ++first) {
  219. if (invoke(pred, invoke(proj, *first)))
  220. return true;
  221. }
  222. return false;
  223. }
  224. // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
  225. //
  226. // Returns: `true` if `E(i)` is `true` for some iterator `i` in `range`, and
  227. // `false` otherwise.
  228. //
  229. // Complexity: At most `size(range)` applications of the predicate and any
  230. // projection.
  231. //
  232. // Reference: https://wg21.link/alg.any.of#:~:text=ranges::any_of(R
  233. template <typename Range,
  234. typename Pred,
  235. typename Proj = identity,
  236. typename = internal::range_category_t<Range>>
  237. constexpr bool any_of(Range&& range, Pred pred, Proj proj = {}) {
  238. return ranges::any_of(ranges::begin(range), ranges::end(range),
  239. std::move(pred), std::move(proj));
  240. }
  241. // [alg.none.of] None of
  242. // Reference: https://wg21.link/alg.none.of
  243. // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
  244. //
  245. // Returns: `false` if `E(i)` is `true` for some iterator `i` in the range
  246. // `[first, last)`, and `true` otherwise.
  247. //
  248. // Complexity: At most `last - first` applications of the predicate and any
  249. // projection.
  250. //
  251. // Reference: https://wg21.link/alg.none.of#:~:text=ranges::none_of(I
  252. template <typename InputIterator,
  253. typename Pred,
  254. typename Proj = identity,
  255. typename = internal::iterator_category_t<InputIterator>>
  256. constexpr bool none_of(InputIterator first,
  257. InputIterator last,
  258. Pred pred,
  259. Proj proj = {}) {
  260. for (; first != last; ++first) {
  261. if (invoke(pred, invoke(proj, *first)))
  262. return false;
  263. }
  264. return true;
  265. }
  266. // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
  267. //
  268. // Returns: `false` if `E(i)` is `true` for some iterator `i` in `range`, and
  269. // `true` otherwise.
  270. //
  271. // Complexity: At most `size(range)` applications of the predicate and any
  272. // projection.
  273. //
  274. // Reference: https://wg21.link/alg.none.of#:~:text=ranges::none_of(R
  275. template <typename Range,
  276. typename Pred,
  277. typename Proj = identity,
  278. typename = internal::range_category_t<Range>>
  279. constexpr bool none_of(Range&& range, Pred pred, Proj proj = {}) {
  280. return ranges::none_of(ranges::begin(range), ranges::end(range),
  281. std::move(pred), std::move(proj));
  282. }
  283. // [alg.foreach] For each
  284. // Reference: https://wg21.link/alg.foreach
  285. // Reference: https://wg21.link/algorithm.syn#:~:text=for_each_result
  286. template <typename I, typename F>
  287. using for_each_result = in_fun_result<I, F>;
  288. // Effects: Calls `invoke(f, invoke(proj, *i))` for every iterator `i` in the
  289. // range `[first, last)`, starting from `first` and proceeding to `last - 1`.
  290. //
  291. // Returns: `{last, std::move(f)}`.
  292. //
  293. // Complexity: Applies `f` and `proj` exactly `last - first` times.
  294. //
  295. // Remarks: If `f` returns a result, the result is ignored.
  296. //
  297. // Reference: https://wg21.link/alg.foreach#:~:text=ranges::for_each(I
  298. template <typename InputIterator,
  299. typename Fun,
  300. typename Proj = identity,
  301. typename = internal::iterator_category_t<InputIterator>>
  302. constexpr auto for_each(InputIterator first,
  303. InputIterator last,
  304. Fun f,
  305. Proj proj = {}) {
  306. for (; first != last; ++first)
  307. invoke(f, invoke(proj, *first));
  308. return for_each_result<InputIterator, Fun>{first, std::move(f)};
  309. }
  310. // Effects: Calls `invoke(f, invoke(proj, *i))` for every iterator `i` in the
  311. // range `range`, starting from `begin(range)` and proceeding to `end(range) -
  312. // 1`.
  313. //
  314. // Returns: `{last, std::move(f)}`.
  315. //
  316. // Complexity: Applies `f` and `proj` exactly `size(range)` times.
  317. //
  318. // Remarks: If `f` returns a result, the result is ignored.
  319. //
  320. // Reference: https://wg21.link/alg.foreach#:~:text=ranges::for_each(R
  321. template <typename Range,
  322. typename Fun,
  323. typename Proj = identity,
  324. typename = internal::range_category_t<Range>>
  325. constexpr auto for_each(Range&& range, Fun f, Proj proj = {}) {
  326. return ranges::for_each(ranges::begin(range), ranges::end(range),
  327. std::move(f), std::move(proj));
  328. }
  329. // Reference: https://wg21.link/algorithm.syn#:~:text=for_each_n_result
  330. template <typename I, typename F>
  331. using for_each_n_result = in_fun_result<I, F>;
  332. // Preconditions: `n >= 0` is `true`.
  333. //
  334. // Effects: Calls `invoke(f, invoke(proj, *i))` for every iterator `i` in the
  335. // range `[first, first + n)` in order.
  336. //
  337. // Returns: `{first + n, std::move(f)}`.
  338. //
  339. // Remarks: If `f` returns a result, the result is ignored.
  340. //
  341. // Reference: https://wg21.link/alg.foreach#:~:text=ranges::for_each_n
  342. template <typename InputIterator,
  343. typename Size,
  344. typename Fun,
  345. typename Proj = identity,
  346. typename = internal::iterator_category_t<InputIterator>>
  347. constexpr auto for_each_n(InputIterator first, Size n, Fun f, Proj proj = {}) {
  348. while (n > 0) {
  349. invoke(f, invoke(proj, *first));
  350. ++first;
  351. --n;
  352. }
  353. return for_each_n_result<InputIterator, Fun>{first, std::move(f)};
  354. }
  355. // [alg.find] Find
  356. // Reference: https://wg21.link/alg.find
  357. // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
  358. //
  359. // Returns: The first iterator `i` in the range `[first, last)` for which `E(i)`
  360. // is `true`. Returns `last` if no such iterator is found.
  361. //
  362. // Complexity: At most `last - first` applications of the corresponding
  363. // predicate and any projection.
  364. //
  365. // Reference: https://wg21.link/alg.find#:~:text=ranges::find(I
  366. template <typename InputIterator,
  367. typename T,
  368. typename Proj = identity,
  369. typename = internal::iterator_category_t<InputIterator>>
  370. constexpr auto find(InputIterator first,
  371. InputIterator last,
  372. const T& value,
  373. Proj proj = {}) {
  374. // Note: In order to be able to apply `proj` to each element in [first, last)
  375. // we are dispatching to std::find_if instead of std::find.
  376. return std::find_if(first, last, [&proj, &value](auto&& lhs) {
  377. return invoke(proj, std::forward<decltype(lhs)>(lhs)) == value;
  378. });
  379. }
  380. // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
  381. //
  382. // Returns: The first iterator `i` in `range` for which `E(i)` is `true`.
  383. // Returns `end(range)` if no such iterator is found.
  384. //
  385. // Complexity: At most `size(range)` applications of the corresponding predicate
  386. // and any projection.
  387. //
  388. // Reference: https://wg21.link/alg.find#:~:text=ranges::find(R
  389. template <typename Range,
  390. typename T,
  391. typename Proj = identity,
  392. typename = internal::range_category_t<Range>>
  393. constexpr auto find(Range&& range, const T& value, Proj proj = {}) {
  394. return ranges::find(ranges::begin(range), ranges::end(range), value,
  395. std::move(proj));
  396. }
  397. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
  398. //
  399. // Returns: The first iterator `i` in the range `[first, last)` for which `E(i)`
  400. // is `true`. Returns `last` if no such iterator is found.
  401. //
  402. // Complexity: At most `last - first` applications of the corresponding
  403. // predicate and any projection.
  404. //
  405. // Reference: https://wg21.link/alg.find#:~:text=ranges::find_if(I
  406. template <typename InputIterator,
  407. typename Pred,
  408. typename Proj = identity,
  409. typename = internal::iterator_category_t<InputIterator>>
  410. constexpr auto find_if(InputIterator first,
  411. InputIterator last,
  412. Pred pred,
  413. Proj proj = {}) {
  414. return std::find_if(first, last,
  415. internal::ProjectedUnaryPredicate(pred, proj));
  416. }
  417. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
  418. //
  419. // Returns: The first iterator `i` in `range` for which `E(i)` is `true`.
  420. // Returns `end(range)` if no such iterator is found.
  421. //
  422. // Complexity: At most `size(range)` applications of the corresponding predicate
  423. // and any projection.
  424. //
  425. // Reference: https://wg21.link/alg.find#:~:text=ranges::find_if(R
  426. template <typename Range,
  427. typename Pred,
  428. typename Proj = identity,
  429. typename = internal::range_category_t<Range>>
  430. constexpr auto find_if(Range&& range, Pred pred, Proj proj = {}) {
  431. return ranges::find_if(ranges::begin(range), ranges::end(range),
  432. std::move(pred), std::move(proj));
  433. }
  434. // Let `E(i)` be `bool(!invoke(pred, invoke(proj, *i)))`.
  435. //
  436. // Returns: The first iterator `i` in the range `[first, last)` for which `E(i)`
  437. // is `true`. Returns `last` if no such iterator is found.
  438. //
  439. // Complexity: At most `last - first` applications of the corresponding
  440. // predicate and any projection.
  441. //
  442. // Reference: https://wg21.link/alg.find#:~:text=ranges::find_if_not(I
  443. template <typename InputIterator,
  444. typename Pred,
  445. typename Proj = identity,
  446. typename = internal::iterator_category_t<InputIterator>>
  447. constexpr auto find_if_not(InputIterator first,
  448. InputIterator last,
  449. Pred pred,
  450. Proj proj = {}) {
  451. return std::find_if_not(first, last,
  452. internal::ProjectedUnaryPredicate(pred, proj));
  453. }
  454. // Let `E(i)` be `bool(!invoke(pred, invoke(proj, *i)))`.
  455. //
  456. // Returns: The first iterator `i` in `range` for which `E(i)` is `true`.
  457. // Returns `end(range)` if no such iterator is found.
  458. //
  459. // Complexity: At most `size(range)` applications of the corresponding predicate
  460. // and any projection.
  461. //
  462. // Reference: https://wg21.link/alg.find#:~:text=ranges::find_if_not(R
  463. template <typename Range,
  464. typename Pred,
  465. typename Proj = identity,
  466. typename = internal::range_category_t<Range>>
  467. constexpr auto find_if_not(Range&& range, Pred pred, Proj proj = {}) {
  468. return ranges::find_if_not(ranges::begin(range), ranges::end(range),
  469. std::move(pred), std::move(proj));
  470. }
  471. // [alg.find.end] Find end
  472. // Reference: https://wg21.link/alg.find.end
  473. // Let:
  474. // - `E(i,n)` be `invoke(pred, invoke(proj1, *(i + n)),
  475. // invoke(proj2, *(first2 + n)))`
  476. //
  477. // - `i` be `last1` if `[first2, last2)` is empty, or if
  478. // `(last2 - first2) > (last1 - first1)` is `true`, or if there is no iterator
  479. // in the range `[first1, last1 - (last2 - first2))` such that for every
  480. // non-negative integer `n < (last2 - first2)`, `E(i,n)` is `true`. Otherwise
  481. // `i` is the last such iterator in `[first1, last1 - (last2 - first2))`.
  482. //
  483. // Returns: `i`
  484. // Note: std::ranges::find_end(I1 first1,...) returns a range, rather than an
  485. // iterator. For simplicitly we match std::find_end's return type instead.
  486. //
  487. // Complexity:
  488. // At most `(last2 - first2) * (last1 - first1 - (last2 - first2) + 1)`
  489. // applications of the corresponding predicate and any projections.
  490. //
  491. // Reference: https://wg21.link/alg.find.end#:~:text=ranges::find_end(I1
  492. template <typename ForwardIterator1,
  493. typename ForwardIterator2,
  494. typename Pred = ranges::equal_to,
  495. typename Proj1 = identity,
  496. typename Proj2 = identity,
  497. typename = internal::iterator_category_t<ForwardIterator1>,
  498. typename = internal::iterator_category_t<ForwardIterator2>,
  499. typename = indirect_result_t<Pred&,
  500. projected<ForwardIterator1, Proj1>,
  501. projected<ForwardIterator2, Proj2>>>
  502. constexpr auto find_end(ForwardIterator1 first1,
  503. ForwardIterator1 last1,
  504. ForwardIterator2 first2,
  505. ForwardIterator2 last2,
  506. Pred pred = {},
  507. Proj1 proj1 = {},
  508. Proj2 proj2 = {}) {
  509. return std::find_end(first1, last1, first2, last2,
  510. internal::ProjectedBinaryPredicate(pred, proj1, proj2));
  511. }
  512. // Let:
  513. // - `E(i,n)` be `invoke(pred, invoke(proj1, *(i + n)),
  514. // invoke(proj2, *(first2 + n)))`
  515. //
  516. // - `i` be `end(range1)` if `range2` is empty, or if
  517. // `size(range2) > size(range1)` is `true`, or if there is no iterator in the
  518. // range `[begin(range1), end(range1) - size(range2))` such that for every
  519. // non-negative integer `n < size(range2)`, `E(i,n)` is `true`. Otherwise `i`
  520. // is the last such iterator in `[begin(range1), end(range1) - size(range2))`.
  521. //
  522. // Returns: `i`
  523. // Note: std::ranges::find_end(R1&& r1,...) returns a range, rather than an
  524. // iterator. For simplicitly we match std::find_end's return type instead.
  525. //
  526. // Complexity: At most `size(range2) * (size(range1) - size(range2) + 1)`
  527. // applications of the corresponding predicate and any projections.
  528. //
  529. // Reference: https://wg21.link/alg.find.end#:~:text=ranges::find_end(R1
  530. template <typename Range1,
  531. typename Range2,
  532. typename Pred = ranges::equal_to,
  533. typename Proj1 = identity,
  534. typename Proj2 = identity,
  535. typename = internal::range_category_t<Range1>,
  536. typename = internal::range_category_t<Range2>,
  537. typename = indirect_result_t<Pred&,
  538. projected<iterator_t<Range1>, Proj1>,
  539. projected<iterator_t<Range2>, Proj2>>>
  540. constexpr auto find_end(Range1&& range1,
  541. Range2&& range2,
  542. Pred pred = {},
  543. Proj1 proj1 = {},
  544. Proj2 proj2 = {}) {
  545. return ranges::find_end(ranges::begin(range1), ranges::end(range1),
  546. ranges::begin(range2), ranges::end(range2),
  547. std::move(pred), std::move(proj1), std::move(proj2));
  548. }
  549. // [alg.find.first.of] Find first
  550. // Reference: https://wg21.link/alg.find.first.of
  551. // Let `E(i,j)` be `bool(invoke(pred, invoke(proj1, *i), invoke(proj2, *j)))`.
  552. //
  553. // Effects: Finds an element that matches one of a set of values.
  554. //
  555. // Returns: The first iterator `i` in the range `[first1, last1)` such that for
  556. // some iterator `j` in the range `[first2, last2)` `E(i,j)` holds. Returns
  557. // `last1` if `[first2, last2)` is empty or if no such iterator is found.
  558. //
  559. // Complexity: At most `(last1 - first1) * (last2 - first2)` applications of the
  560. // corresponding predicate and any projections.
  561. //
  562. // Reference:
  563. // https://wg21.link/alg.find.first.of#:~:text=ranges::find_first_of(I1
  564. template <typename ForwardIterator1,
  565. typename ForwardIterator2,
  566. typename Pred = ranges::equal_to,
  567. typename Proj1 = identity,
  568. typename Proj2 = identity,
  569. typename = internal::iterator_category_t<ForwardIterator1>,
  570. typename = internal::iterator_category_t<ForwardIterator2>,
  571. typename = indirect_result_t<Pred&,
  572. projected<ForwardIterator1, Proj1>,
  573. projected<ForwardIterator2, Proj2>>>
  574. constexpr auto find_first_of(ForwardIterator1 first1,
  575. ForwardIterator1 last1,
  576. ForwardIterator2 first2,
  577. ForwardIterator2 last2,
  578. Pred pred = {},
  579. Proj1 proj1 = {},
  580. Proj2 proj2 = {}) {
  581. return std::find_first_of(
  582. first1, last1, first2, last2,
  583. internal::ProjectedBinaryPredicate(pred, proj1, proj2));
  584. }
  585. // Let `E(i,j)` be `bool(invoke(pred, invoke(proj1, *i), invoke(proj2, *j)))`.
  586. //
  587. // Effects: Finds an element that matches one of a set of values.
  588. //
  589. // Returns: The first iterator `i` in `range1` such that for some iterator `j`
  590. // in `range2` `E(i,j)` holds. Returns `end(range1)` if `range2` is empty or if
  591. // no such iterator is found.
  592. //
  593. // Complexity: At most `size(range1) * size(range2)` applications of the
  594. // corresponding predicate and any projections.
  595. //
  596. // Reference:
  597. // https://wg21.link/alg.find.first.of#:~:text=ranges::find_first_of(R1
  598. template <typename Range1,
  599. typename Range2,
  600. typename Pred = ranges::equal_to,
  601. typename Proj1 = identity,
  602. typename Proj2 = identity,
  603. typename = internal::range_category_t<Range1>,
  604. typename = internal::range_category_t<Range2>,
  605. typename = indirect_result_t<Pred&,
  606. projected<iterator_t<Range1>, Proj1>,
  607. projected<iterator_t<Range2>, Proj2>>>
  608. constexpr auto find_first_of(Range1&& range1,
  609. Range2&& range2,
  610. Pred pred = {},
  611. Proj1 proj1 = {},
  612. Proj2 proj2 = {}) {
  613. return ranges::find_first_of(
  614. ranges::begin(range1), ranges::end(range1), ranges::begin(range2),
  615. ranges::end(range2), std::move(pred), std::move(proj1), std::move(proj2));
  616. }
  617. // [alg.adjacent.find] Adjacent find
  618. // Reference: https://wg21.link/alg.adjacent.find
  619. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i), invoke(proj, *(i + 1))))`.
  620. //
  621. // Returns: The first iterator `i` such that both `i` and `i + 1` are in the
  622. // range `[first, last)` for which `E(i)` holds. Returns `last` if no such
  623. // iterator is found.
  624. //
  625. // Complexity: Exactly `min((i - first) + 1, (last - first) - 1)` applications
  626. // of the corresponding predicate, where `i` is `adjacent_find`'s return value.
  627. //
  628. // Reference:
  629. // https://wg21.link/alg.adjacent.find#:~:text=ranges::adjacent_find(I
  630. template <typename ForwardIterator,
  631. typename Pred = ranges::equal_to,
  632. typename Proj = identity,
  633. typename = internal::iterator_category_t<ForwardIterator>>
  634. constexpr auto adjacent_find(ForwardIterator first,
  635. ForwardIterator last,
  636. Pred pred = {},
  637. Proj proj = {}) {
  638. return std::adjacent_find(
  639. first, last, internal::ProjectedBinaryPredicate(pred, proj, proj));
  640. }
  641. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i), invoke(proj, *(i + 1))))`.
  642. //
  643. // Returns: The first iterator `i` such that both `i` and `i + 1` are in the
  644. // range `range` for which `E(i)` holds. Returns `end(range)` if no such
  645. // iterator is found.
  646. //
  647. // Complexity: Exactly `min((i - begin(range)) + 1, size(range) - 1)`
  648. // applications of the corresponding predicate, where `i` is `adjacent_find`'s
  649. // return value.
  650. //
  651. // Reference:
  652. // https://wg21.link/alg.adjacent.find#:~:text=ranges::adjacent_find(R
  653. template <typename Range,
  654. typename Pred = ranges::equal_to,
  655. typename Proj = identity,
  656. typename = internal::range_category_t<Range>>
  657. constexpr auto adjacent_find(Range&& range, Pred pred = {}, Proj proj = {}) {
  658. return ranges::adjacent_find(ranges::begin(range), ranges::end(range),
  659. std::move(pred), std::move(proj));
  660. }
  661. // [alg.count] Count
  662. // Reference: https://wg21.link/alg.count
  663. // Let `E(i)` be `invoke(proj, *i) == value`.
  664. //
  665. // Effects: Returns the number of iterators `i` in the range `[first, last)` for
  666. // which `E(i)` holds.
  667. //
  668. // Complexity: Exactly `last - first` applications of the corresponding
  669. // predicate and any projection.
  670. //
  671. // Reference: https://wg21.link/alg.count#:~:text=ranges::count(I
  672. template <typename InputIterator,
  673. typename T,
  674. typename Proj = identity,
  675. typename = internal::iterator_category_t<InputIterator>>
  676. constexpr auto count(InputIterator first,
  677. InputIterator last,
  678. const T& value,
  679. Proj proj = {}) {
  680. // Note: In order to be able to apply `proj` to each element in [first, last)
  681. // we are dispatching to std::count_if instead of std::count.
  682. return std::count_if(first, last, [&proj, &value](auto&& lhs) {
  683. return invoke(proj, std::forward<decltype(lhs)>(lhs)) == value;
  684. });
  685. }
  686. // Let `E(i)` be `invoke(proj, *i) == value`.
  687. //
  688. // Effects: Returns the number of iterators `i` in `range` for which `E(i)`
  689. // holds.
  690. //
  691. // Complexity: Exactly `size(range)` applications of the corresponding predicate
  692. // and any projection.
  693. //
  694. // Reference: https://wg21.link/alg.count#:~:text=ranges::count(R
  695. template <typename Range,
  696. typename T,
  697. typename Proj = identity,
  698. typename = internal::range_category_t<Range>>
  699. constexpr auto count(Range&& range, const T& value, Proj proj = {}) {
  700. return ranges::count(ranges::begin(range), ranges::end(range), value,
  701. std::move(proj));
  702. }
  703. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
  704. //
  705. // Effects: Returns the number of iterators `i` in the range `[first, last)` for
  706. // which `E(i)` holds.
  707. //
  708. // Complexity: Exactly `last - first` applications of the corresponding
  709. // predicate and any projection.
  710. //
  711. // Reference: https://wg21.link/alg.count#:~:text=ranges::count_if(I
  712. template <typename InputIterator,
  713. typename Pred,
  714. typename Proj = identity,
  715. typename = internal::iterator_category_t<InputIterator>>
  716. constexpr auto count_if(InputIterator first,
  717. InputIterator last,
  718. Pred pred,
  719. Proj proj = {}) {
  720. return std::count_if(first, last,
  721. internal::ProjectedUnaryPredicate(pred, proj));
  722. }
  723. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
  724. //
  725. // Effects: Returns the number of iterators `i` in `range` for which `E(i)`
  726. // holds.
  727. //
  728. // Complexity: Exactly `size(range)` applications of the corresponding predicate
  729. // and any projection.
  730. //
  731. // Reference: https://wg21.link/alg.count#:~:text=ranges::count_if(R
  732. template <typename Range,
  733. typename Pred,
  734. typename Proj = identity,
  735. typename = internal::range_category_t<Range>>
  736. constexpr auto count_if(Range&& range, Pred pred, Proj proj = {}) {
  737. return ranges::count_if(ranges::begin(range), ranges::end(range),
  738. std::move(pred), std::move(proj));
  739. }
  740. // [mismatch] Mismatch
  741. // Reference: https://wg21.link/mismatch
  742. // Let `E(n)` be `!invoke(pred, invoke(proj1, *(first1 + n)),
  743. // invoke(proj2, *(first2 + n)))`.
  744. //
  745. // Let `N` be `min(last1 - first1, last2 - first2)`.
  746. //
  747. // Returns: `{ first1 + n, first2 + n }`, where `n` is the smallest integer in
  748. // `[0, N)` such that `E(n)` holds, or `N` if no such integer exists.
  749. //
  750. // Complexity: At most `N` applications of the corresponding predicate and any
  751. // projections.
  752. //
  753. // Reference: https://wg21.link/mismatch#:~:text=ranges::mismatch(I1
  754. template <typename ForwardIterator1,
  755. typename ForwardIterator2,
  756. typename Pred = ranges::equal_to,
  757. typename Proj1 = identity,
  758. typename Proj2 = identity,
  759. typename = internal::iterator_category_t<ForwardIterator1>,
  760. typename = internal::iterator_category_t<ForwardIterator2>,
  761. typename = indirect_result_t<Pred&,
  762. projected<ForwardIterator1, Proj1>,
  763. projected<ForwardIterator2, Proj2>>>
  764. constexpr auto mismatch(ForwardIterator1 first1,
  765. ForwardIterator1 last1,
  766. ForwardIterator2 first2,
  767. ForwardIterator2 last2,
  768. Pred pred = {},
  769. Proj1 proj1 = {},
  770. Proj2 proj2 = {}) {
  771. return std::mismatch(first1, last1, first2, last2,
  772. internal::ProjectedBinaryPredicate(pred, proj1, proj2));
  773. }
  774. // Let `E(n)` be `!invoke(pred, invoke(proj1, *(begin(range1) + n)),
  775. // invoke(proj2, *(begin(range2) + n)))`.
  776. //
  777. // Let `N` be `min(size(range1), size(range2))`.
  778. //
  779. // Returns: `{ begin(range1) + n, begin(range2) + n }`, where `n` is the
  780. // smallest integer in `[0, N)` such that `E(n)` holds, or `N` if no such
  781. // integer exists.
  782. //
  783. // Complexity: At most `N` applications of the corresponding predicate and any
  784. // projections.
  785. //
  786. // Reference: https://wg21.link/mismatch#:~:text=ranges::mismatch(R1
  787. template <typename Range1,
  788. typename Range2,
  789. typename Pred = ranges::equal_to,
  790. typename Proj1 = identity,
  791. typename Proj2 = identity,
  792. typename = internal::range_category_t<Range1>,
  793. typename = internal::range_category_t<Range2>,
  794. typename = indirect_result_t<Pred&,
  795. projected<iterator_t<Range1>, Proj1>,
  796. projected<iterator_t<Range2>, Proj2>>>
  797. constexpr auto mismatch(Range1&& range1,
  798. Range2&& range2,
  799. Pred pred = {},
  800. Proj1 proj1 = {},
  801. Proj2 proj2 = {}) {
  802. return ranges::mismatch(ranges::begin(range1), ranges::end(range1),
  803. ranges::begin(range2), ranges::end(range2),
  804. std::move(pred), std::move(proj1), std::move(proj2));
  805. }
  806. // [alg.equal] Equal
  807. // Reference: https://wg21.link/alg.equal
  808. // Let `E(i)` be
  809. // `invoke(pred, invoke(proj1, *i), invoke(proj2, *(first2 + (i - first1))))`.
  810. //
  811. // Returns: If `last1 - first1 != last2 - first2`, return `false.` Otherwise
  812. // return `true` if `E(i)` holds for every iterator `i` in the range `[first1,
  813. // last1)`. Otherwise, returns `false`.
  814. //
  815. // Complexity: If the types of `first1`, `last1`, `first2`, and `last2` meet the
  816. // `RandomAccessIterator` requirements and `last1 - first1 != last2 - first2`,
  817. // then no applications of the corresponding predicate and each projection;
  818. // otherwise, at most `min(last1 - first1, last2 - first2)` applications of the
  819. // corresponding predicate and any projections.
  820. //
  821. // Reference: https://wg21.link/alg.equal#:~:text=ranges::equal(I1
  822. template <typename ForwardIterator1,
  823. typename ForwardIterator2,
  824. typename Pred = ranges::equal_to,
  825. typename Proj1 = identity,
  826. typename Proj2 = identity,
  827. typename = internal::iterator_category_t<ForwardIterator1>,
  828. typename = internal::iterator_category_t<ForwardIterator2>,
  829. typename = indirect_result_t<Pred&,
  830. projected<ForwardIterator1, Proj1>,
  831. projected<ForwardIterator2, Proj2>>>
  832. constexpr bool equal(ForwardIterator1 first1,
  833. ForwardIterator1 last1,
  834. ForwardIterator2 first2,
  835. ForwardIterator2 last2,
  836. Pred pred = {},
  837. Proj1 proj1 = {},
  838. Proj2 proj2 = {}) {
  839. return std::equal(first1, last1, first2, last2,
  840. internal::ProjectedBinaryPredicate(pred, proj1, proj2));
  841. }
  842. // Let `E(i)` be
  843. // `invoke(pred, invoke(proj1, *i),
  844. // invoke(proj2, *(begin(range2) + (i - begin(range1)))))`.
  845. //
  846. // Returns: If `size(range1) != size(range2)`, return `false.` Otherwise return
  847. // `true` if `E(i)` holds for every iterator `i` in `range1`. Otherwise, returns
  848. // `false`.
  849. //
  850. // Complexity: If the types of `begin(range1)`, `end(range1)`, `begin(range2)`,
  851. // and `end(range2)` meet the `RandomAccessIterator` requirements and
  852. // `size(range1) != size(range2)`, then no applications of the corresponding
  853. // predicate and each projection;
  854. // otherwise, at most `min(size(range1), size(range2))` applications of the
  855. // corresponding predicate and any projections.
  856. //
  857. // Reference: https://wg21.link/alg.equal#:~:text=ranges::equal(R1
  858. template <typename Range1,
  859. typename Range2,
  860. typename Pred = ranges::equal_to,
  861. typename Proj1 = identity,
  862. typename Proj2 = identity,
  863. typename = internal::range_category_t<Range1>,
  864. typename = internal::range_category_t<Range2>,
  865. typename = indirect_result_t<Pred&,
  866. projected<iterator_t<Range1>, Proj1>,
  867. projected<iterator_t<Range2>, Proj2>>>
  868. constexpr bool equal(Range1&& range1,
  869. Range2&& range2,
  870. Pred pred = {},
  871. Proj1 proj1 = {},
  872. Proj2 proj2 = {}) {
  873. return ranges::equal(ranges::begin(range1), ranges::end(range1),
  874. ranges::begin(range2), ranges::end(range2),
  875. std::move(pred), std::move(proj1), std::move(proj2));
  876. }
  877. // [alg.is.permutation] Is permutation
  878. // Reference: https://wg21.link/alg.is.permutation
  879. // Returns: If `last1 - first1 != last2 - first2`, return `false`. Otherwise
  880. // return `true` if there exists a permutation of the elements in the range
  881. // `[first2, last2)`, bounded by `[pfirst, plast)`, such that
  882. // `ranges::equal(first1, last1, pfirst, plast, pred, proj, proj)` returns
  883. // `true`; otherwise, returns `false`.
  884. //
  885. // Complexity: No applications of the corresponding predicate if
  886. // ForwardIterator1 and ForwardIterator2 meet the requirements of random access
  887. // iterators and `last1 - first1 != last2 - first2`. Otherwise, exactly
  888. // `last1 - first1` applications of the corresponding predicate and projections
  889. // if `ranges::equal(first1, last1, first2, last2, pred, proj, proj)` would
  890. // return true;
  891. // otherwise, at worst `O(N^2)`, where `N` has the value `last1 - first1`.
  892. //
  893. // Reference:
  894. // https://wg21.link/alg.is.permutation#:~:text=ranges::is_permutation(I1
  895. template <typename ForwardIterator1,
  896. typename ForwardIterator2,
  897. typename Pred = ranges::equal_to,
  898. typename Proj1 = identity,
  899. typename Proj2 = identity,
  900. typename = internal::iterator_category_t<ForwardIterator1>,
  901. typename = internal::iterator_category_t<ForwardIterator2>,
  902. typename = indirect_result_t<Pred&,
  903. projected<ForwardIterator1, Proj1>,
  904. projected<ForwardIterator2, Proj2>>>
  905. constexpr bool is_permutation(ForwardIterator1 first1,
  906. ForwardIterator1 last1,
  907. ForwardIterator2 first2,
  908. ForwardIterator2 last2,
  909. Pred pred = {},
  910. Proj1 proj1 = {},
  911. Proj2 proj2 = {}) {
  912. // Needs to opt-in to all permutations, since std::is_permutation expects
  913. // pred(proj1(lhs), proj1(rhs)) to compile.
  914. return std::is_permutation(
  915. first1, last1, first2, last2,
  916. internal::PermutedProjectedBinaryPredicate(pred, proj1, proj2));
  917. }
  918. // Returns: If `size(range1) != size(range2)`, return `false`. Otherwise return
  919. // `true` if there exists a permutation of the elements in `range2`, bounded by
  920. // `[pbegin, pend)`, such that
  921. // `ranges::equal(range1, [pbegin, pend), pred, proj, proj)` returns `true`;
  922. // otherwise, returns `false`.
  923. //
  924. // Complexity: No applications of the corresponding predicate if Range1 and
  925. // Range2 meet the requirements of random access ranges and
  926. // `size(range1) != size(range2)`. Otherwise, exactly `size(range1)`
  927. // applications of the corresponding predicate and projections if
  928. // `ranges::equal(range1, range2, pred, proj, proj)` would return true;
  929. // otherwise, at worst `O(N^2)`, where `N` has the value `size(range1)`.
  930. //
  931. // Reference:
  932. // https://wg21.link/alg.is.permutation#:~:text=ranges::is_permutation(R1
  933. template <typename Range1,
  934. typename Range2,
  935. typename Pred = ranges::equal_to,
  936. typename Proj1 = identity,
  937. typename Proj2 = identity,
  938. typename = internal::range_category_t<Range1>,
  939. typename = internal::range_category_t<Range2>,
  940. typename = indirect_result_t<Pred&,
  941. projected<iterator_t<Range1>, Proj1>,
  942. projected<iterator_t<Range2>, Proj2>>>
  943. constexpr bool is_permutation(Range1&& range1,
  944. Range2&& range2,
  945. Pred pred = {},
  946. Proj1 proj1 = {},
  947. Proj2 proj2 = {}) {
  948. return ranges::is_permutation(
  949. ranges::begin(range1), ranges::end(range1), ranges::begin(range2),
  950. ranges::end(range2), std::move(pred), std::move(proj1), std::move(proj2));
  951. }
  952. // [alg.search] Search
  953. // Reference: https://wg21.link/alg.search
  954. // Returns: `i`, where `i` is the first iterator in the range
  955. // `[first1, last1 - (last2 - first2))` such that for every non-negative integer
  956. // `n` less than `last2 - first2` the condition
  957. // `bool(invoke(pred, invoke(proj1, *(i + n)), invoke(proj2, *(first2 + n))))`
  958. // is `true`.
  959. // Returns `last1` if no such iterator exists.
  960. // Note: std::ranges::search(I1 first1,...) returns a range, rather than an
  961. // iterator. For simplicitly we match std::search's return type instead.
  962. //
  963. // Complexity: At most `(last1 - first1) * (last2 - first2)` applications of the
  964. // corresponding predicate and projections.
  965. //
  966. // Reference: https://wg21.link/alg.search#:~:text=ranges::search(I1
  967. template <typename ForwardIterator1,
  968. typename ForwardIterator2,
  969. typename Pred = ranges::equal_to,
  970. typename Proj1 = identity,
  971. typename Proj2 = identity,
  972. typename = internal::iterator_category_t<ForwardIterator1>,
  973. typename = internal::iterator_category_t<ForwardIterator2>,
  974. typename = indirect_result_t<Pred&,
  975. projected<ForwardIterator1, Proj1>,
  976. projected<ForwardIterator2, Proj2>>>
  977. constexpr auto search(ForwardIterator1 first1,
  978. ForwardIterator1 last1,
  979. ForwardIterator2 first2,
  980. ForwardIterator2 last2,
  981. Pred pred = {},
  982. Proj1 proj1 = {},
  983. Proj2 proj2 = {}) {
  984. return std::search(first1, last1, first2, last2,
  985. internal::ProjectedBinaryPredicate(pred, proj1, proj2));
  986. }
  987. // Returns: `i`, where `i` is the first iterator in the range
  988. // `[begin(range1), end(range1) - size(range2))` such that for every
  989. // non-negative integer `n` less than `size(range2)` the condition
  990. // `bool(invoke(pred, invoke(proj1, *(i + n)),
  991. // invoke(proj2, *(begin(range2) + n))))` is `true`.
  992. // Returns `end(range1)` if no such iterator exists.
  993. // Note: std::ranges::search(R1&& r1,...) returns a range, rather than an
  994. // iterator. For simplicitly we match std::search's return type instead.
  995. //
  996. // Complexity: At most `size(range1) * size(range2)` applications of the
  997. // corresponding predicate and projections.
  998. //
  999. // Reference: https://wg21.link/alg.search#:~:text=ranges::search(R1
  1000. template <typename Range1,
  1001. typename Range2,
  1002. typename Pred = ranges::equal_to,
  1003. typename Proj1 = identity,
  1004. typename Proj2 = identity,
  1005. typename = internal::range_category_t<Range1>,
  1006. typename = internal::range_category_t<Range2>,
  1007. typename = indirect_result_t<Pred&,
  1008. projected<iterator_t<Range1>, Proj1>,
  1009. projected<iterator_t<Range2>, Proj2>>>
  1010. constexpr auto search(Range1&& range1,
  1011. Range2&& range2,
  1012. Pred pred = {},
  1013. Proj1 proj1 = {},
  1014. Proj2 proj2 = {}) {
  1015. return ranges::search(ranges::begin(range1), ranges::end(range1),
  1016. ranges::begin(range2), ranges::end(range2),
  1017. std::move(pred), std::move(proj1), std::move(proj2));
  1018. }
  1019. // Mandates: The type `Size` is convertible to an integral type.
  1020. //
  1021. // Returns: `i` where `i` is the first iterator in the range
  1022. // `[first, last - count)` such that for every non-negative integer `n` less
  1023. // than `count`, the following condition holds:
  1024. // `invoke(pred, invoke(proj, *(i + n)), value)`.
  1025. // Returns `last` if no such iterator is found.
  1026. // Note: std::ranges::search_n(I1 first1,...) returns a range, rather than an
  1027. // iterator. For simplicitly we match std::search_n's return type instead.
  1028. //
  1029. // Complexity: At most `last - first` applications of the corresponding
  1030. // predicate and projection.
  1031. //
  1032. // Reference: https://wg21.link/alg.search#:~:text=ranges::search_n(I
  1033. template <typename ForwardIterator,
  1034. typename Size,
  1035. typename T,
  1036. typename Pred = ranges::equal_to,
  1037. typename Proj = identity,
  1038. typename = internal::iterator_category_t<ForwardIterator>>
  1039. constexpr auto search_n(ForwardIterator first,
  1040. ForwardIterator last,
  1041. Size count,
  1042. const T& value,
  1043. Pred pred = {},
  1044. Proj proj = {}) {
  1045. // The second arg is guaranteed to be `value`, so we'll simply apply the
  1046. // identity projection.
  1047. identity value_proj;
  1048. return std::search_n(
  1049. first, last, count, value,
  1050. internal::ProjectedBinaryPredicate(pred, proj, value_proj));
  1051. }
  1052. // Mandates: The type `Size` is convertible to an integral type.
  1053. //
  1054. // Returns: `i` where `i` is the first iterator in the range
  1055. // `[begin(range), end(range) - count)` such that for every non-negative integer
  1056. // `n` less than `count`, the following condition holds:
  1057. // `invoke(pred, invoke(proj, *(i + n)), value)`.
  1058. // Returns `end(arnge)` if no such iterator is found.
  1059. // Note: std::ranges::search_n(R1&& r1,...) returns a range, rather than an
  1060. // iterator. For simplicitly we match std::search_n's return type instead.
  1061. //
  1062. // Complexity: At most `size(range)` applications of the corresponding predicate
  1063. // and projection.
  1064. //
  1065. // Reference: https://wg21.link/alg.search#:~:text=ranges::search_n(R
  1066. template <typename Range,
  1067. typename Size,
  1068. typename T,
  1069. typename Pred = ranges::equal_to,
  1070. typename Proj = identity,
  1071. typename = internal::range_category_t<Range>>
  1072. constexpr auto search_n(Range&& range,
  1073. Size count,
  1074. const T& value,
  1075. Pred pred = {},
  1076. Proj proj = {}) {
  1077. return ranges::search_n(ranges::begin(range), ranges::end(range), count,
  1078. value, std::move(pred), std::move(proj));
  1079. }
  1080. // [alg.modifying.operations] Mutating sequence operations
  1081. // Reference: https://wg21.link/alg.modifying.operations
  1082. // [alg.copy] Copy
  1083. // Reference: https://wg21.link/alg.copy
  1084. // Let N be `last - first`.
  1085. //
  1086. // Preconditions: `result` is not in the range `[first, last)`.
  1087. //
  1088. // Effects: Copies elements in the range `[first, last)` into the range
  1089. // `[result, result + N)` starting from `first` and proceeding to `last`. For
  1090. // each non-negative integer `n < N` , performs `*(result + n) = *(first + n)`.
  1091. //
  1092. // Returns: `result + N`
  1093. //
  1094. // Complexity: Exactly `N` assignments.
  1095. //
  1096. // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy(I
  1097. template <typename InputIterator,
  1098. typename OutputIterator,
  1099. typename = internal::iterator_category_t<InputIterator>,
  1100. typename = internal::iterator_category_t<OutputIterator>>
  1101. constexpr auto copy(InputIterator first,
  1102. InputIterator last,
  1103. OutputIterator result) {
  1104. return std::copy(first, last, result);
  1105. }
  1106. // Let N be `size(range)`.
  1107. //
  1108. // Preconditions: `result` is not in `range`.
  1109. //
  1110. // Effects: Copies elements in `range` into the range `[result, result + N)`
  1111. // starting from `begin(range)` and proceeding to `end(range)`. For each
  1112. // non-negative integer `n < N` , performs
  1113. // *(result + n) = *(begin(range) + n)`.
  1114. //
  1115. // Returns: `result + N`
  1116. //
  1117. // Complexity: Exactly `N` assignments.
  1118. //
  1119. // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy(R
  1120. template <typename Range,
  1121. typename OutputIterator,
  1122. typename = internal::range_category_t<Range>,
  1123. typename = internal::iterator_category_t<OutputIterator>>
  1124. constexpr auto copy(Range&& range, OutputIterator result) {
  1125. return ranges::copy(ranges::begin(range), ranges::end(range), result);
  1126. }
  1127. // Let `N` be `max(0, n)`.
  1128. //
  1129. // Mandates: The type `Size` is convertible to an integral type.
  1130. //
  1131. // Effects: For each non-negative integer `i < N`, performs
  1132. // `*(result + i) = *(first + i)`.
  1133. //
  1134. // Returns: `result + N`
  1135. //
  1136. // Complexity: Exactly `N` assignments.
  1137. //
  1138. // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy_n
  1139. template <typename InputIterator,
  1140. typename Size,
  1141. typename OutputIterator,
  1142. typename = internal::iterator_category_t<InputIterator>,
  1143. typename = internal::iterator_category_t<OutputIterator>>
  1144. constexpr auto copy_n(InputIterator first, Size n, OutputIterator result) {
  1145. return std::copy_n(first, n, result);
  1146. }
  1147. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`, and `N` be the number
  1148. // of iterators `i` in the range `[first, last)` for which the condition `E(i)`
  1149. // holds.
  1150. //
  1151. // Preconditions: The ranges `[first, last)` and
  1152. // `[result, result + (last - first))` do not overlap.
  1153. //
  1154. // Effects: Copies all of the elements referred to by the iterator `i` in the
  1155. // range `[first, last)` for which `E(i)` is true.
  1156. //
  1157. // Returns: `result + N`
  1158. //
  1159. // Complexity: Exactly `last - first` applications of the corresponding
  1160. // predicate and any projection.
  1161. //
  1162. // Remarks: Stable.
  1163. //
  1164. // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy_if(I
  1165. template <typename InputIterator,
  1166. typename OutputIterator,
  1167. typename Pred,
  1168. typename Proj = identity,
  1169. typename = internal::iterator_category_t<InputIterator>,
  1170. typename = internal::iterator_category_t<OutputIterator>>
  1171. constexpr auto copy_if(InputIterator first,
  1172. InputIterator last,
  1173. OutputIterator result,
  1174. Pred pred,
  1175. Proj proj = {}) {
  1176. return std::copy_if(first, last, result,
  1177. internal::ProjectedUnaryPredicate(pred, proj));
  1178. }
  1179. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`, and `N` be the number
  1180. // of iterators `i` in `range` for which the condition `E(i)` holds.
  1181. //
  1182. // Preconditions: `range` and `[result, result + size(range))` do not overlap.
  1183. //
  1184. // Effects: Copies all of the elements referred to by the iterator `i` in
  1185. // `range` for which `E(i)` is true.
  1186. //
  1187. // Returns: `result + N`
  1188. //
  1189. // Complexity: Exactly `size(range)` applications of the corresponding predicate
  1190. // and any projection.
  1191. //
  1192. // Remarks: Stable.
  1193. //
  1194. // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy_if(R
  1195. template <typename Range,
  1196. typename OutputIterator,
  1197. typename Pred,
  1198. typename Proj = identity,
  1199. typename = internal::range_category_t<Range>,
  1200. typename = internal::iterator_category_t<OutputIterator>>
  1201. constexpr auto copy_if(Range&& range,
  1202. OutputIterator result,
  1203. Pred pred,
  1204. Proj proj = {}) {
  1205. return ranges::copy_if(ranges::begin(range), ranges::end(range), result,
  1206. std::move(pred), std::move(proj));
  1207. }
  1208. // Let `N` be `last - first`.
  1209. //
  1210. // Preconditions: `result` is not in the range `(first, last]`.
  1211. //
  1212. // Effects: Copies elements in the range `[first, last)` into the range
  1213. // `[result - N, result)` starting from `last - 1` and proceeding to `first`.
  1214. // For each positive integer `n ≤ N`, performs `*(result - n) = *(last - n)`.
  1215. //
  1216. // Returns: `result - N`
  1217. //
  1218. // Complexity: Exactly `N` assignments.
  1219. //
  1220. // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy_backward(I1
  1221. template <typename BidirectionalIterator1,
  1222. typename BidirectionalIterator2,
  1223. typename = internal::iterator_category_t<BidirectionalIterator1>,
  1224. typename = internal::iterator_category_t<BidirectionalIterator2>>
  1225. constexpr auto copy_backward(BidirectionalIterator1 first,
  1226. BidirectionalIterator1 last,
  1227. BidirectionalIterator2 result) {
  1228. return std::copy_backward(first, last, result);
  1229. }
  1230. // Let `N` be `size(range)`.
  1231. //
  1232. // Preconditions: `result` is not in the range `(begin(range), end(range)]`.
  1233. //
  1234. // Effects: Copies elements in `range` into the range `[result - N, result)`
  1235. // starting from `end(range) - 1` and proceeding to `begin(range)`. For each
  1236. // positive integer `n ≤ N`, performs `*(result - n) = *(end(range) - n)`.
  1237. //
  1238. // Returns: `result - N`
  1239. //
  1240. // Complexity: Exactly `N` assignments.
  1241. //
  1242. // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy_backward(R
  1243. template <typename Range,
  1244. typename BidirectionalIterator,
  1245. typename = internal::range_category_t<Range>,
  1246. typename = internal::iterator_category_t<BidirectionalIterator>>
  1247. constexpr auto copy_backward(Range&& range, BidirectionalIterator result) {
  1248. return ranges::copy_backward(ranges::begin(range), ranges::end(range),
  1249. result);
  1250. }
  1251. // [alg.move] Move
  1252. // Reference: https://wg21.link/alg.move
  1253. // Let `E(n)` be `std::move(*(first + n))`.
  1254. //
  1255. // Let `N` be `last - first`.
  1256. //
  1257. // Preconditions: `result` is not in the range `[first, last)`.
  1258. //
  1259. // Effects: Moves elements in the range `[first, last)` into the range `[result,
  1260. // result + N)` starting from `first` and proceeding to `last`. For each
  1261. // non-negative integer `n < N`, performs `*(result + n) = E(n)`.
  1262. //
  1263. // Returns: `result + N`
  1264. //
  1265. // Complexity: Exactly `N` assignments.
  1266. //
  1267. // Reference: https://wg21.link/alg.move#:~:text=ranges::move(I
  1268. template <typename InputIterator,
  1269. typename OutputIterator,
  1270. typename = internal::iterator_category_t<InputIterator>,
  1271. typename = internal::iterator_category_t<OutputIterator>>
  1272. constexpr auto move(InputIterator first,
  1273. InputIterator last,
  1274. OutputIterator result) {
  1275. return std::move(first, last, result);
  1276. }
  1277. // Let `E(n)` be `std::move(*(begin(range) + n))`.
  1278. //
  1279. // Let `N` be `size(range)`.
  1280. //
  1281. // Preconditions: `result` is not in `range`.
  1282. //
  1283. // Effects: Moves elements in `range` into the range `[result, result + N)`
  1284. // starting from `begin(range)` and proceeding to `end(range)`. For each
  1285. // non-negative integer `n < N`, performs `*(result + n) = E(n)`.
  1286. //
  1287. // Returns: `result + N`
  1288. //
  1289. // Complexity: Exactly `N` assignments.
  1290. //
  1291. // Reference: https://wg21.link/alg.move#:~:text=ranges::move(R
  1292. template <typename Range,
  1293. typename OutputIterator,
  1294. typename = internal::range_category_t<Range>,
  1295. typename = internal::iterator_category_t<OutputIterator>>
  1296. constexpr auto move(Range&& range, OutputIterator result) {
  1297. return ranges::move(ranges::begin(range), ranges::end(range), result);
  1298. }
  1299. // Let `E(n)` be `std::move(*(last - n))`.
  1300. //
  1301. // Let `N` be `last - first`.
  1302. //
  1303. // Preconditions: `result` is not in the range `(first, last]`.
  1304. //
  1305. // Effects: Moves elements in the range `[first, last)` into the range
  1306. // `[result - N, result)` starting from `last - 1` and proceeding to `first`.
  1307. // For each positive integer `n ≤ N`, performs `*(result - n) = E(n)`.
  1308. //
  1309. // Returns: `result - N`
  1310. //
  1311. // Complexity: Exactly `N` assignments.
  1312. //
  1313. // Reference: https://wg21.link/alg.move#:~:text=ranges::move_backward(I1
  1314. template <typename BidirectionalIterator1,
  1315. typename BidirectionalIterator2,
  1316. typename = internal::iterator_category_t<BidirectionalIterator1>,
  1317. typename = internal::iterator_category_t<BidirectionalIterator2>>
  1318. constexpr auto move_backward(BidirectionalIterator1 first,
  1319. BidirectionalIterator1 last,
  1320. BidirectionalIterator2 result) {
  1321. return std::move_backward(first, last, result);
  1322. }
  1323. // Let `E(n)` be `std::move(*(end(range) - n))`.
  1324. //
  1325. // Let `N` be `size(range)`.
  1326. //
  1327. // Preconditions: `result` is not in the range `(begin(range), end(range)]`.
  1328. //
  1329. // Effects: Moves elements in `range` into the range `[result - N, result)`
  1330. // starting from `end(range) - 1` and proceeding to `begin(range)`. For each
  1331. // positive integer `n ≤ N`, performs `*(result - n) = E(n)`.
  1332. //
  1333. // Returns: `result - N`
  1334. //
  1335. // Complexity: Exactly `N` assignments.
  1336. //
  1337. // Reference: https://wg21.link/alg.move#:~:text=ranges::move_backward(R
  1338. template <typename Range,
  1339. typename BidirectionalIterator,
  1340. typename = internal::range_category_t<Range>,
  1341. typename = internal::iterator_category_t<BidirectionalIterator>>
  1342. constexpr auto move_backward(Range&& range, BidirectionalIterator result) {
  1343. return ranges::move_backward(ranges::begin(range), ranges::end(range),
  1344. result);
  1345. }
  1346. // [alg.swap] Swap
  1347. // Reference: https://wg21.link/alg.swap
  1348. // Let `M` be `min(last1 - first1, last2 - first2)`.
  1349. //
  1350. // Preconditions: The two ranges `[first1, last1)` and `[first2, last2)` do not
  1351. // overlap. `*(first1 + n)` is swappable with `*(first2 + n)`.
  1352. //
  1353. // Effects: For each non-negative integer `n < M` performs
  1354. // `swap(*(first1 + n), *(first2 + n))`
  1355. //
  1356. // Returns: `first2 + M`
  1357. //
  1358. // Complexity: Exactly `M` swaps.
  1359. //
  1360. // Reference: https://wg21.link/alg.swap#:~:text=ranges::swap_ranges(I1
  1361. template <typename ForwardIterator1,
  1362. typename ForwardIterator2,
  1363. typename = internal::iterator_category_t<ForwardIterator1>,
  1364. typename = internal::iterator_category_t<ForwardIterator2>>
  1365. constexpr auto swap_ranges(ForwardIterator1 first1,
  1366. ForwardIterator1 last1,
  1367. ForwardIterator2 first2,
  1368. ForwardIterator2 last2) {
  1369. // std::swap_ranges does not have a `last2` overload. Thus we need to
  1370. // adjust `last1` to ensure to not read past `last2`.
  1371. last1 = std::next(first1, std::min(std::distance(first1, last1),
  1372. std::distance(first2, last2)));
  1373. return std::swap_ranges(first1, last1, first2);
  1374. }
  1375. // Let `M` be `min(size(range1), size(range2))`.
  1376. //
  1377. // Preconditions: The two ranges `range1` and `range2` do not overlap.
  1378. // `*(begin(range1) + n)` is swappable with `*(begin(range2) + n)`.
  1379. //
  1380. // Effects: For each non-negative integer `n < M` performs
  1381. // `swap(*(begin(range1) + n), *(begin(range2) + n))`
  1382. //
  1383. // Returns: `begin(range2) + M`
  1384. //
  1385. // Complexity: Exactly `M` swaps.
  1386. //
  1387. // Reference: https://wg21.link/alg.swap#:~:text=ranges::swap_ranges(R1
  1388. template <typename Range1,
  1389. typename Range2,
  1390. typename = internal::range_category_t<Range1>,
  1391. typename = internal::range_category_t<Range2>>
  1392. constexpr auto swap_ranges(Range1&& range1, Range2&& range2) {
  1393. return ranges::swap_ranges(ranges::begin(range1), ranges::end(range1),
  1394. ranges::begin(range2), ranges::end(range2));
  1395. }
  1396. // [alg.transform] Transform
  1397. // Reference: https://wg21.link/alg.transform
  1398. // Let `N` be `last1 - first1`,
  1399. // `E(i)` be `invoke(op, invoke(proj, *(first1 + (i - result))))`.
  1400. //
  1401. // Preconditions: `op` does not invalidate iterators or subranges, nor modify
  1402. // elements in the ranges `[first1, first1 + N]`, and `[result, result + N]`.
  1403. //
  1404. // Effects: Assigns through every iterator `i` in the range
  1405. // `[result, result + N)` a new corresponding value equal to `E(i)`.
  1406. //
  1407. // Returns: `result + N`
  1408. //
  1409. // Complexity: Exactly `N` applications of `op` and any projections.
  1410. //
  1411. // Remarks: result may be equal to `first1`.
  1412. //
  1413. // Reference: https://wg21.link/alg.transform#:~:text=ranges::transform(I
  1414. template <typename InputIterator,
  1415. typename OutputIterator,
  1416. typename UnaryOperation,
  1417. typename Proj = identity,
  1418. typename = internal::iterator_category_t<InputIterator>,
  1419. typename = internal::iterator_category_t<OutputIterator>,
  1420. typename = indirect_result_t<UnaryOperation&,
  1421. projected<InputIterator, Proj>>>
  1422. constexpr auto transform(InputIterator first1,
  1423. InputIterator last1,
  1424. OutputIterator result,
  1425. UnaryOperation op,
  1426. Proj proj = {}) {
  1427. return std::transform(first1, last1, result, [&op, &proj](auto&& arg) {
  1428. return invoke(op, invoke(proj, std::forward<decltype(arg)>(arg)));
  1429. });
  1430. }
  1431. // Let `N` be `size(range)`,
  1432. // `E(i)` be `invoke(op, invoke(proj, *(begin(range) + (i - result))))`.
  1433. //
  1434. // Preconditions: `op` does not invalidate iterators or subranges, nor modify
  1435. // elements in the ranges `[begin(range), end(range)]`, and
  1436. // `[result, result + N]`.
  1437. //
  1438. // Effects: Assigns through every iterator `i` in the range
  1439. // `[result, result + N)` a new corresponding value equal to `E(i)`.
  1440. //
  1441. // Returns: `result + N`
  1442. //
  1443. // Complexity: Exactly `N` applications of `op` and any projections.
  1444. //
  1445. // Remarks: result may be equal to `begin(range)`.
  1446. //
  1447. // Reference: https://wg21.link/alg.transform#:~:text=ranges::transform(R
  1448. template <typename Range,
  1449. typename OutputIterator,
  1450. typename UnaryOperation,
  1451. typename Proj = identity,
  1452. typename = internal::range_category_t<Range>,
  1453. typename = internal::iterator_category_t<OutputIterator>,
  1454. typename = indirect_result_t<UnaryOperation&,
  1455. projected<iterator_t<Range>, Proj>>>
  1456. constexpr auto transform(Range&& range,
  1457. OutputIterator result,
  1458. UnaryOperation op,
  1459. Proj proj = {}) {
  1460. return ranges::transform(ranges::begin(range), ranges::end(range), result,
  1461. std::move(op), std::move(proj));
  1462. }
  1463. // Let:
  1464. // `N` be `min(last1 - first1, last2 - first2)`,
  1465. // `E(i)` be `invoke(binary_op, invoke(proj1, *(first1 + (i - result))),
  1466. // invoke(proj2, *(first2 + (i - result))))`.
  1467. //
  1468. // Preconditions: `binary_op` does not invalidate iterators or subranges, nor
  1469. // modify elements in the ranges `[first1, first1 + N]`, `[first2, first2 + N]`,
  1470. // and `[result, result + N]`.
  1471. //
  1472. // Effects: Assigns through every iterator `i` in the range
  1473. // `[result, result + N)` a new corresponding value equal to `E(i)`.
  1474. //
  1475. // Returns: `result + N`
  1476. //
  1477. // Complexity: Exactly `N` applications of `binary_op`, and any projections.
  1478. //
  1479. // Remarks: `result` may be equal to `first1` or `first2`.
  1480. //
  1481. // Reference: https://wg21.link/alg.transform#:~:text=ranges::transform(I1
  1482. template <typename ForwardIterator1,
  1483. typename ForwardIterator2,
  1484. typename OutputIterator,
  1485. typename BinaryOperation,
  1486. typename Proj1 = identity,
  1487. typename Proj2 = identity,
  1488. typename = internal::iterator_category_t<ForwardIterator1>,
  1489. typename = internal::iterator_category_t<ForwardIterator2>,
  1490. typename = internal::iterator_category_t<OutputIterator>,
  1491. typename = indirect_result_t<BinaryOperation&,
  1492. projected<ForwardIterator1, Proj1>,
  1493. projected<ForwardIterator2, Proj2>>>
  1494. constexpr auto transform(ForwardIterator1 first1,
  1495. ForwardIterator1 last1,
  1496. ForwardIterator2 first2,
  1497. ForwardIterator2 last2,
  1498. OutputIterator result,
  1499. BinaryOperation binary_op,
  1500. Proj1 proj1 = {},
  1501. Proj2 proj2 = {}) {
  1502. // std::transform does not have a `last2` overload. Thus we need to adjust
  1503. // `last1` to ensure to not read past `last2`.
  1504. last1 = std::next(first1, std::min(std::distance(first1, last1),
  1505. std::distance(first2, last2)));
  1506. return std::transform(first1, last1, first2, result,
  1507. [&binary_op, &proj1, &proj2](auto&& lhs, auto&& rhs) {
  1508. return invoke(
  1509. binary_op,
  1510. invoke(proj1, std::forward<decltype(lhs)>(lhs)),
  1511. invoke(proj2, std::forward<decltype(rhs)>(rhs)));
  1512. });
  1513. }
  1514. // Let:
  1515. // `N` be `min(size(range1), size(range2)`,
  1516. // `E(i)` be `invoke(binary_op, invoke(proj1, *(begin(range1) + (i - result))),
  1517. // invoke(proj2, *(begin(range2) + (i - result))))`
  1518. //
  1519. // Preconditions: `binary_op` does not invalidate iterators or subranges, nor
  1520. // modify elements in the ranges `[begin(range1), end(range1)]`,
  1521. // `[begin(range2), end(range2)]`, and `[result, result + N]`.
  1522. //
  1523. // Effects: Assigns through every iterator `i` in the range
  1524. // `[result, result + N)` a new corresponding value equal to `E(i)`.
  1525. //
  1526. // Returns: `result + N`
  1527. //
  1528. // Complexity: Exactly `N` applications of `binary_op`, and any projections.
  1529. //
  1530. // Remarks: `result` may be equal to `begin(range1)` or `begin(range2)`.
  1531. //
  1532. // Reference: https://wg21.link/alg.transform#:~:text=ranges::transform(R1
  1533. template <typename Range1,
  1534. typename Range2,
  1535. typename OutputIterator,
  1536. typename BinaryOperation,
  1537. typename Proj1 = identity,
  1538. typename Proj2 = identity,
  1539. typename = internal::range_category_t<Range1>,
  1540. typename = internal::range_category_t<Range2>,
  1541. typename = internal::iterator_category_t<OutputIterator>,
  1542. typename = indirect_result_t<BinaryOperation&,
  1543. projected<iterator_t<Range1>, Proj1>,
  1544. projected<iterator_t<Range2>, Proj2>>>
  1545. constexpr auto transform(Range1&& range1,
  1546. Range2&& range2,
  1547. OutputIterator result,
  1548. BinaryOperation binary_op,
  1549. Proj1 proj1 = {},
  1550. Proj2 proj2 = {}) {
  1551. return ranges::transform(ranges::begin(range1), ranges::end(range1),
  1552. ranges::begin(range2), ranges::end(range2), result,
  1553. std::move(binary_op), std::move(proj1),
  1554. std::move(proj2));
  1555. }
  1556. // [alg.replace] Replace
  1557. // Reference: https://wg21.link/alg.replace
  1558. // Let `E(i)` be `bool(invoke(proj, *i) == old_value)`.
  1559. //
  1560. // Mandates: `new_value` is writable to `first`.
  1561. //
  1562. // Effects: Substitutes elements referred by the iterator `i` in the range
  1563. // `[first, last)` with `new_value`, when `E(i)` is true.
  1564. //
  1565. // Returns: `last`
  1566. //
  1567. // Complexity: Exactly `last - first` applications of the corresponding
  1568. // predicate and any projection.
  1569. //
  1570. // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace(I
  1571. template <typename ForwardIterator,
  1572. typename T,
  1573. typename Proj = identity,
  1574. typename = internal::iterator_category_t<ForwardIterator>>
  1575. constexpr auto replace(ForwardIterator first,
  1576. ForwardIterator last,
  1577. const T& old_value,
  1578. const T& new_value,
  1579. Proj proj = {}) {
  1580. // Note: In order to be able to apply `proj` to each element in [first, last)
  1581. // we are dispatching to std::replace_if instead of std::replace.
  1582. std::replace_if(
  1583. first, last,
  1584. [&proj, &old_value](auto&& lhs) {
  1585. return invoke(proj, std::forward<decltype(lhs)>(lhs)) == old_value;
  1586. },
  1587. new_value);
  1588. return last;
  1589. }
  1590. // Let `E(i)` be `bool(invoke(proj, *i) == old_value)`.
  1591. //
  1592. // Mandates: `new_value` is writable to `begin(range)`.
  1593. //
  1594. // Effects: Substitutes elements referred by the iterator `i` in `range` with
  1595. // `new_value`, when `E(i)` is true.
  1596. //
  1597. // Returns: `end(range)`
  1598. //
  1599. // Complexity: Exactly `size(range)` applications of the corresponding predicate
  1600. // and any projection.
  1601. //
  1602. // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace(R
  1603. template <typename Range,
  1604. typename T,
  1605. typename Proj = identity,
  1606. typename = internal::range_category_t<Range>>
  1607. constexpr auto replace(Range&& range,
  1608. const T& old_value,
  1609. const T& new_value,
  1610. Proj proj = {}) {
  1611. return ranges::replace(ranges::begin(range), ranges::end(range), old_value,
  1612. new_value, std::move(proj));
  1613. }
  1614. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
  1615. //
  1616. // Mandates: `new_value` is writable to `first`.
  1617. //
  1618. // Effects: Substitutes elements referred by the iterator `i` in the range
  1619. // `[first, last)` with `new_value`, when `E(i)` is true.
  1620. //
  1621. // Returns: `last`
  1622. //
  1623. // Complexity: Exactly `last - first` applications of the corresponding
  1624. // predicate and any projection.
  1625. //
  1626. // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_if(I
  1627. template <typename ForwardIterator,
  1628. typename Predicate,
  1629. typename T,
  1630. typename Proj = identity,
  1631. typename = internal::iterator_category_t<ForwardIterator>>
  1632. constexpr auto replace_if(ForwardIterator first,
  1633. ForwardIterator last,
  1634. Predicate pred,
  1635. const T& new_value,
  1636. Proj proj = {}) {
  1637. std::replace_if(first, last, internal::ProjectedUnaryPredicate(pred, proj),
  1638. new_value);
  1639. return last;
  1640. }
  1641. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
  1642. //
  1643. // Mandates: `new_value` is writable to `begin(range)`.
  1644. //
  1645. // Effects: Substitutes elements referred by the iterator `i` in `range` with
  1646. // `new_value`, when `E(i)` is true.
  1647. //
  1648. // Returns: `end(range)`
  1649. //
  1650. // Complexity: Exactly `size(range)` applications of the corresponding predicate
  1651. // and any projection.
  1652. //
  1653. // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_if(R
  1654. template <typename Range,
  1655. typename Predicate,
  1656. typename T,
  1657. typename Proj = identity,
  1658. typename = internal::range_category_t<Range>>
  1659. constexpr auto replace_if(Range&& range,
  1660. Predicate pred,
  1661. const T& new_value,
  1662. Proj proj = {}) {
  1663. return ranges::replace_if(ranges::begin(range), ranges::end(range),
  1664. std::move(pred), new_value, std::move(proj));
  1665. }
  1666. // Let `E(i)` be `bool(invoke(proj, *(first + (i - result))) == old_value)`.
  1667. //
  1668. // Mandates: The results of the expressions `*first` and `new_value` are
  1669. // writable to `result`.
  1670. //
  1671. // Preconditions: The ranges `[first, last)` and `[result, result + (last -
  1672. // first))` do not overlap.
  1673. //
  1674. // Effects: Assigns through every iterator `i` in the range `[result, result +
  1675. // (last - first))` a new corresponding value, `new_value` if `E(i)` is true, or
  1676. // `*(first + (i - result))` otherwise.
  1677. //
  1678. // Returns: `result + (last - first)`.
  1679. //
  1680. // Complexity: Exactly `last - first` applications of the corresponding
  1681. // predicate and any projection.
  1682. //
  1683. // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_copy(I
  1684. template <typename InputIterator,
  1685. typename OutputIterator,
  1686. typename T,
  1687. typename Proj = identity,
  1688. typename = internal::iterator_category_t<InputIterator>,
  1689. typename = internal::iterator_category_t<OutputIterator>>
  1690. constexpr auto replace_copy(InputIterator first,
  1691. InputIterator last,
  1692. OutputIterator result,
  1693. const T& old_value,
  1694. const T& new_value,
  1695. Proj proj = {}) {
  1696. // Note: In order to be able to apply `proj` to each element in [first, last)
  1697. // we are dispatching to std::replace_copy_if instead of std::replace_copy.
  1698. std::replace_copy_if(
  1699. first, last, result,
  1700. [&proj, &old_value](auto&& lhs) {
  1701. return invoke(proj, std::forward<decltype(lhs)>(lhs)) == old_value;
  1702. },
  1703. new_value);
  1704. return last;
  1705. }
  1706. // Let `E(i)` be
  1707. // `bool(invoke(proj, *(begin(range) + (i - result))) == old_value)`.
  1708. //
  1709. // Mandates: The results of the expressions `*begin(range)` and `new_value` are
  1710. // writable to `result`.
  1711. //
  1712. // Preconditions: The ranges `range` and `[result, result + size(range))` do not
  1713. // overlap.
  1714. //
  1715. // Effects: Assigns through every iterator `i` in the range `[result, result +
  1716. // size(range))` a new corresponding value, `new_value` if `E(i)` is true, or
  1717. // `*(begin(range) + (i - result))` otherwise.
  1718. //
  1719. // Returns: `result + size(range)`.
  1720. //
  1721. // Complexity: Exactly `size(range)` applications of the corresponding
  1722. // predicate and any projection.
  1723. //
  1724. // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_copy(R
  1725. template <typename Range,
  1726. typename OutputIterator,
  1727. typename T,
  1728. typename Proj = identity,
  1729. typename = internal::range_category_t<Range>,
  1730. typename = internal::iterator_category_t<OutputIterator>>
  1731. constexpr auto replace_copy(Range&& range,
  1732. OutputIterator result,
  1733. const T& old_value,
  1734. const T& new_value,
  1735. Proj proj = {}) {
  1736. return ranges::replace_copy(ranges::begin(range), ranges::end(range), result,
  1737. old_value, new_value, std::move(proj));
  1738. }
  1739. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *(first + (i - result)))))`.
  1740. //
  1741. // Mandates: The results of the expressions `*first` and `new_value` are
  1742. // writable to `result`.
  1743. //
  1744. // Preconditions: The ranges `[first, last)` and `[result, result + (last -
  1745. // first))` do not overlap.
  1746. //
  1747. // Effects: Assigns through every iterator `i` in the range `[result, result +
  1748. // (last - first))` a new corresponding value, `new_value` if `E(i)` is true, or
  1749. // `*(first + (i - result))` otherwise.
  1750. //
  1751. // Returns: `result + (last - first)`.
  1752. //
  1753. // Complexity: Exactly `last - first` applications of the corresponding
  1754. // predicate and any projection.
  1755. //
  1756. // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_copy_if(I
  1757. template <typename InputIterator,
  1758. typename OutputIterator,
  1759. typename Predicate,
  1760. typename T,
  1761. typename Proj = identity,
  1762. typename = internal::iterator_category_t<InputIterator>,
  1763. typename = internal::iterator_category_t<OutputIterator>>
  1764. constexpr auto replace_copy_if(InputIterator first,
  1765. InputIterator last,
  1766. OutputIterator result,
  1767. Predicate pred,
  1768. const T& new_value,
  1769. Proj proj = {}) {
  1770. return std::replace_copy_if(first, last, result,
  1771. internal::ProjectedUnaryPredicate(pred, proj),
  1772. new_value);
  1773. }
  1774. // Let `E(i)` be
  1775. // `bool(invoke(pred, invoke(proj, *(begin(range) + (i - result)))))`.
  1776. //
  1777. // Mandates: The results of the expressions `*begin(range)` and `new_value` are
  1778. // writable to `result`.
  1779. //
  1780. // Preconditions: The ranges `range` and `[result, result + size(range))` do not
  1781. // overlap.
  1782. //
  1783. // Effects: Assigns through every iterator `i` in the range `[result, result +
  1784. // size(range))` a new corresponding value, `new_value` if `E(i)` is true, or
  1785. // `*(begin(range) + (i - result))` otherwise.
  1786. //
  1787. // Returns: `result + size(range)`.
  1788. //
  1789. // Complexity: Exactly `size(range)` applications of the corresponding
  1790. // predicate and any projection.
  1791. //
  1792. // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_copy_if(R
  1793. template <typename Range,
  1794. typename OutputIterator,
  1795. typename Predicate,
  1796. typename T,
  1797. typename Proj = identity,
  1798. typename = internal::range_category_t<Range>,
  1799. typename = internal::iterator_category_t<OutputIterator>>
  1800. constexpr auto replace_copy_if(Range&& range,
  1801. OutputIterator result,
  1802. Predicate pred,
  1803. const T& new_value,
  1804. Proj proj = {}) {
  1805. return ranges::replace_copy_if(ranges::begin(range), ranges::end(range),
  1806. result, pred, new_value, std::move(proj));
  1807. }
  1808. // [alg.fill] Fill
  1809. // Reference: https://wg21.link/alg.fill
  1810. // Let `N` be `last - first`.
  1811. //
  1812. // Mandates: The expression `value` is writable to the output iterator.
  1813. //
  1814. // Effects: Assigns `value` through all the iterators in the range
  1815. // `[first, last)`.
  1816. //
  1817. // Returns: `last`.
  1818. //
  1819. // Complexity: Exactly `N` assignments.
  1820. //
  1821. // Reference: https://wg21.link/alg.fill#:~:text=ranges::fill(O
  1822. template <typename OutputIterator,
  1823. typename T,
  1824. typename = internal::iterator_category_t<OutputIterator>>
  1825. constexpr auto fill(OutputIterator first, OutputIterator last, const T& value) {
  1826. std::fill(first, last, value);
  1827. return last;
  1828. }
  1829. // Let `N` be `size(range)`.
  1830. //
  1831. // Mandates: The expression `value` is writable to the output iterator.
  1832. //
  1833. // Effects: Assigns `value` through all the iterators in `range`.
  1834. //
  1835. // Returns: `end(range)`.
  1836. //
  1837. // Complexity: Exactly `N` assignments.
  1838. //
  1839. // Reference: https://wg21.link/alg.fill#:~:text=ranges::fill(R
  1840. template <typename Range,
  1841. typename T,
  1842. typename = internal::range_category_t<Range>>
  1843. constexpr auto fill(Range&& range, const T& value) {
  1844. return ranges::fill(ranges::begin(range), ranges::end(range), value);
  1845. }
  1846. // Let `N` be `max(0, n)`.
  1847. //
  1848. // Mandates: The expression `value` is writable to the output iterator.
  1849. // The type `Size` is convertible to an integral type.
  1850. //
  1851. // Effects: Assigns `value` through all the iterators in `[first, first + N)`.
  1852. //
  1853. // Returns: `first + N`.
  1854. //
  1855. // Complexity: Exactly `N` assignments.
  1856. //
  1857. // Reference: https://wg21.link/alg.fill#:~:text=ranges::fill_n(O
  1858. template <typename OutputIterator,
  1859. typename Size,
  1860. typename T,
  1861. typename = internal::iterator_category_t<OutputIterator>>
  1862. constexpr auto fill_n(OutputIterator first, Size n, const T& value) {
  1863. return std::fill_n(first, n, value);
  1864. }
  1865. // [alg.generate] Generate
  1866. // Reference: https://wg21.link/alg.generate
  1867. // Let `N` be `last - first`.
  1868. //
  1869. // Effects: Assigns the result of successive evaluations of gen() through each
  1870. // iterator in the range `[first, last)`.
  1871. //
  1872. // Returns: `last`.
  1873. //
  1874. // Complexity: Exactly `N` evaluations of `gen()` and assignments.
  1875. //
  1876. // Reference: https://wg21.link/alg.generate#:~:text=ranges::generate(O
  1877. template <typename OutputIterator,
  1878. typename Generator,
  1879. typename = internal::iterator_category_t<OutputIterator>>
  1880. constexpr auto generate(OutputIterator first,
  1881. OutputIterator last,
  1882. Generator gen) {
  1883. std::generate(first, last, std::move(gen));
  1884. return last;
  1885. }
  1886. // Let `N` be `size(range)`.
  1887. //
  1888. // Effects: Assigns the result of successive evaluations of gen() through each
  1889. // iterator in `range`.
  1890. //
  1891. // Returns: `end(range)`.
  1892. //
  1893. // Complexity: Exactly `N` evaluations of `gen()` and assignments.
  1894. //
  1895. // Reference: https://wg21.link/alg.generate#:~:text=ranges::generate(R
  1896. template <typename Range,
  1897. typename Generator,
  1898. typename = internal::range_category_t<Range>>
  1899. constexpr auto generate(Range&& range, Generator gen) {
  1900. return ranges::generate(ranges::begin(range), ranges::end(range),
  1901. std::move(gen));
  1902. }
  1903. // Let `N` be `max(0, n)`.
  1904. //
  1905. // Mandates: `Size` is convertible to an integral type.
  1906. //
  1907. // Effects: Assigns the result of successive evaluations of gen() through each
  1908. // iterator in the range `[first, first + N)`.
  1909. //
  1910. // Returns: `first + N`.
  1911. //
  1912. // Complexity: Exactly `N` evaluations of `gen()` and assignments.
  1913. //
  1914. // Reference: https://wg21.link/alg.generate#:~:text=ranges::generate_n(O
  1915. template <typename OutputIterator,
  1916. typename Size,
  1917. typename Generator,
  1918. typename = internal::iterator_category_t<OutputIterator>>
  1919. constexpr auto generate_n(OutputIterator first, Size n, Generator gen) {
  1920. return std::generate_n(first, n, std::move(gen));
  1921. }
  1922. // [alg.remove] Remove
  1923. // Reference: https://wg21.link/alg.remove
  1924. // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
  1925. //
  1926. // Effects: Eliminates all the elements referred to by iterator `i` in the range
  1927. // `[first, last)` for which `E(i)` holds.
  1928. //
  1929. // Returns: The end of the resulting range.
  1930. //
  1931. // Remarks: Stable.
  1932. //
  1933. // Complexity: Exactly `last - first` applications of the corresponding
  1934. // predicate and any projection.
  1935. //
  1936. // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove(I
  1937. template <typename ForwardIterator,
  1938. typename T,
  1939. typename Proj = identity,
  1940. typename = internal::iterator_category_t<ForwardIterator>>
  1941. constexpr auto remove(ForwardIterator first,
  1942. ForwardIterator last,
  1943. const T& value,
  1944. Proj proj = {}) {
  1945. // Note: In order to be able to apply `proj` to each element in [first, last)
  1946. // we are dispatching to std::remove_if instead of std::remove.
  1947. return std::remove_if(first, last, [&proj, &value](auto&& lhs) {
  1948. return invoke(proj, std::forward<decltype(lhs)>(lhs)) == value;
  1949. });
  1950. }
  1951. // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
  1952. //
  1953. // Effects: Eliminates all the elements referred to by iterator `i` in `range`
  1954. // for which `E(i)` holds.
  1955. //
  1956. // Returns: The end of the resulting range.
  1957. //
  1958. // Remarks: Stable.
  1959. //
  1960. // Complexity: Exactly `size(range)` applications of the corresponding predicate
  1961. // and any projection.
  1962. //
  1963. // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove(R
  1964. template <typename Range,
  1965. typename T,
  1966. typename Proj = identity,
  1967. typename = internal::range_category_t<Range>>
  1968. constexpr auto remove(Range&& range, const T& value, Proj proj = {}) {
  1969. return ranges::remove(ranges::begin(range), ranges::end(range), value,
  1970. std::move(proj));
  1971. }
  1972. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
  1973. //
  1974. // Effects: Eliminates all the elements referred to by iterator `i` in the range
  1975. // `[first, last)` for which `E(i)` holds.
  1976. //
  1977. // Returns: The end of the resulting range.
  1978. //
  1979. // Remarks: Stable.
  1980. //
  1981. // Complexity: Exactly `last - first` applications of the corresponding
  1982. // predicate and any projection.
  1983. //
  1984. // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_if(I
  1985. template <typename ForwardIterator,
  1986. typename Predicate,
  1987. typename Proj = identity,
  1988. typename = internal::iterator_category_t<ForwardIterator>>
  1989. constexpr auto remove_if(ForwardIterator first,
  1990. ForwardIterator last,
  1991. Predicate pred,
  1992. Proj proj = {}) {
  1993. return std::remove_if(first, last,
  1994. internal::ProjectedUnaryPredicate(pred, proj));
  1995. }
  1996. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
  1997. //
  1998. // Effects: Eliminates all the elements referred to by iterator `i` in `range`.
  1999. //
  2000. // Returns: The end of the resulting range.
  2001. //
  2002. // Remarks: Stable.
  2003. //
  2004. // Complexity: Exactly `size(range)` applications of the corresponding predicate
  2005. // and any projection.
  2006. //
  2007. // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_if(R
  2008. template <typename Range,
  2009. typename Predicate,
  2010. typename Proj = identity,
  2011. typename = internal::range_category_t<Range>>
  2012. constexpr auto remove_if(Range&& range, Predicate pred, Proj proj = {}) {
  2013. return ranges::remove_if(ranges::begin(range), ranges::end(range),
  2014. std::move(pred), std::move(proj));
  2015. }
  2016. // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
  2017. //
  2018. // Let `N` be the number of elements in `[first, last)` for which `E(i)` is
  2019. // false.
  2020. //
  2021. // Mandates: `*first` is writable to `result`.
  2022. //
  2023. // Preconditions: The ranges `[first, last)` and `[result, result + (last -
  2024. // first))` do not overlap.
  2025. //
  2026. // Effects: Copies all the elements referred to by the iterator `i` in the range
  2027. // `[first, last)` for which `E(i)` is false.
  2028. //
  2029. // Returns: `result + N`.
  2030. //
  2031. // Complexity: Exactly `last - first` applications of the corresponding
  2032. // predicate and any projection.
  2033. //
  2034. // Remarks: Stable.
  2035. //
  2036. // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_copy(I
  2037. template <typename InputIterator,
  2038. typename OutputIterator,
  2039. typename T,
  2040. typename Proj = identity,
  2041. typename = internal::iterator_category_t<InputIterator>,
  2042. typename = internal::iterator_category_t<OutputIterator>>
  2043. constexpr auto remove_copy(InputIterator first,
  2044. InputIterator last,
  2045. OutputIterator result,
  2046. const T& value,
  2047. Proj proj = {}) {
  2048. // Note: In order to be able to apply `proj` to each element in [first, last)
  2049. // we are dispatching to std::remove_copy_if instead of std::remove_copy.
  2050. return std::remove_copy_if(first, last, result, [&proj, &value](auto&& lhs) {
  2051. return invoke(proj, std::forward<decltype(lhs)>(lhs)) == value;
  2052. });
  2053. }
  2054. // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
  2055. //
  2056. // Let `N` be the number of elements in `range` for which `E(i)` is false.
  2057. //
  2058. // Mandates: `*begin(range)` is writable to `result`.
  2059. //
  2060. // Preconditions: The ranges `range` and `[result, result + size(range))` do not
  2061. // overlap.
  2062. //
  2063. // Effects: Copies all the elements referred to by the iterator `i` in `range`
  2064. // for which `E(i)` is false.
  2065. //
  2066. // Returns: `result + N`.
  2067. //
  2068. // Complexity: Exactly `size(range)` applications of the corresponding
  2069. // predicate and any projection.
  2070. //
  2071. // Remarks: Stable.
  2072. //
  2073. // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_copy(R
  2074. template <typename Range,
  2075. typename OutputIterator,
  2076. typename T,
  2077. typename Proj = identity,
  2078. typename = internal::range_category_t<Range>,
  2079. typename = internal::iterator_category_t<OutputIterator>>
  2080. constexpr auto remove_copy(Range&& range,
  2081. OutputIterator result,
  2082. const T& value,
  2083. Proj proj = {}) {
  2084. return ranges::remove_copy(ranges::begin(range), ranges::end(range), result,
  2085. value, std::move(proj));
  2086. }
  2087. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
  2088. //
  2089. // Let `N` be the number of elements in `[first, last)` for which `E(i)` is
  2090. // false.
  2091. //
  2092. // Mandates: `*first` is writable to `result`.
  2093. //
  2094. // Preconditions: The ranges `[first, last)` and `[result, result + (last -
  2095. // first))` do not overlap.
  2096. //
  2097. // Effects: Copies all the elements referred to by the iterator `i` in the range
  2098. // `[first, last)` for which `E(i)` is false.
  2099. //
  2100. // Returns: `result + N`.
  2101. //
  2102. // Complexity: Exactly `last - first` applications of the corresponding
  2103. // predicate and any projection.
  2104. //
  2105. // Remarks: Stable.
  2106. //
  2107. // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_copy_if(I
  2108. template <typename InputIterator,
  2109. typename OutputIterator,
  2110. typename Pred,
  2111. typename Proj = identity,
  2112. typename = internal::iterator_category_t<InputIterator>,
  2113. typename = internal::iterator_category_t<OutputIterator>>
  2114. constexpr auto remove_copy_if(InputIterator first,
  2115. InputIterator last,
  2116. OutputIterator result,
  2117. Pred pred,
  2118. Proj proj = {}) {
  2119. return std::remove_copy_if(first, last, result,
  2120. internal::ProjectedUnaryPredicate(pred, proj));
  2121. }
  2122. // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
  2123. //
  2124. // Let `N` be the number of elements in `range` for which `E(i)` is false.
  2125. //
  2126. // Mandates: `*begin(range)` is writable to `result`.
  2127. //
  2128. // Preconditions: The ranges `range` and `[result, result + size(range))` do not
  2129. // overlap.
  2130. //
  2131. // Effects: Copies all the elements referred to by the iterator `i` in `range`
  2132. // for which `E(i)` is false.
  2133. //
  2134. // Returns: `result + N`.
  2135. //
  2136. // Complexity: Exactly `size(range)` applications of the corresponding
  2137. // predicate and any projection.
  2138. //
  2139. // Remarks: Stable.
  2140. //
  2141. // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_copy(R
  2142. template <typename Range,
  2143. typename OutputIterator,
  2144. typename Pred,
  2145. typename Proj = identity,
  2146. typename = internal::range_category_t<Range>,
  2147. typename = internal::iterator_category_t<OutputIterator>>
  2148. constexpr auto remove_copy_if(Range&& range,
  2149. OutputIterator result,
  2150. Pred pred,
  2151. Proj proj = {}) {
  2152. return ranges::remove_copy_if(ranges::begin(range), ranges::end(range),
  2153. result, std::move(pred), std::move(proj));
  2154. }
  2155. // [alg.unique] Unique
  2156. // Reference: https://wg21.link/alg.unique
  2157. // Let `E(i)` be `bool(invoke(comp, invoke(proj, *(i - 1)), invoke(proj, *i)))`.
  2158. //
  2159. // Effects: For a nonempty range, eliminates all but the first element from
  2160. // every consecutive group of equivalent elements referred to by the iterator
  2161. // `i` in the range `[first + 1, last)` for which `E(i)` is true.
  2162. //
  2163. // Returns: The end of the resulting range.
  2164. //
  2165. // Complexity: For nonempty ranges, exactly `(last - first) - 1` applications of
  2166. // the corresponding predicate and no more than twice as many applications of
  2167. // any projection.
  2168. //
  2169. // Reference: https://wg21.link/alg.unique#:~:text=ranges::unique(I
  2170. template <typename ForwardIterator,
  2171. typename Comp = ranges::equal_to,
  2172. typename Proj = identity,
  2173. typename = internal::iterator_category_t<ForwardIterator>,
  2174. typename = indirect_result_t<Comp&,
  2175. projected<ForwardIterator, Proj>,
  2176. projected<ForwardIterator, Proj>>>
  2177. constexpr auto unique(ForwardIterator first,
  2178. ForwardIterator last,
  2179. Comp comp = {},
  2180. Proj proj = {}) {
  2181. return std::unique(first, last,
  2182. internal::ProjectedBinaryPredicate(comp, proj, proj));
  2183. }
  2184. // Let `E(i)` be `bool(invoke(comp, invoke(proj, *(i - 1)), invoke(proj, *i)))`.
  2185. //
  2186. // Effects: For a nonempty range, eliminates all but the first element from
  2187. // every consecutive group of equivalent elements referred to by the iterator
  2188. // `i` in the range `[begin(range) + 1, end(range))` for which `E(i)` is true.
  2189. //
  2190. // Returns: The end of the resulting range.
  2191. //
  2192. // Complexity: For nonempty ranges, exactly `size(range) - 1` applications of
  2193. // the corresponding predicate and no more than twice as many applications of
  2194. // any projection.
  2195. //
  2196. // Reference: https://wg21.link/alg.unique#:~:text=ranges::unique(R
  2197. template <typename Range,
  2198. typename Comp = ranges::equal_to,
  2199. typename Proj = identity,
  2200. typename = internal::range_category_t<Range>,
  2201. typename = indirect_result_t<Comp&,
  2202. projected<iterator_t<Range>, Proj>,
  2203. projected<iterator_t<Range>, Proj>>>
  2204. constexpr auto unique(Range&& range, Comp comp = {}, Proj proj = {}) {
  2205. return ranges::unique(ranges::begin(range), ranges::end(range),
  2206. std::move(comp), std::move(proj));
  2207. }
  2208. // Let `E(i)` be `bool(invoke(comp, invoke(proj, *i), invoke(proj, *(i - 1))))`.
  2209. //
  2210. // Mandates: `*first` is writable to `result`.
  2211. //
  2212. // Preconditions: The ranges `[first, last)` and
  2213. // `[result, result + (last - first))` do not overlap.
  2214. //
  2215. // Effects: Copies only the first element from every consecutive group of equal
  2216. // elements referred to by the iterator `i` in the range `[first, last)` for
  2217. // which `E(i)` holds.
  2218. //
  2219. // Returns: `result + N`.
  2220. //
  2221. // Complexity: Exactly `last - first - 1` applications of the corresponding
  2222. // predicate and no more than twice as many applications of any projection.
  2223. //
  2224. // Reference: https://wg21.link/alg.unique#:~:text=ranges::unique_copy(I
  2225. template <typename ForwardIterator,
  2226. typename OutputIterator,
  2227. typename Comp = ranges::equal_to,
  2228. typename Proj = identity,
  2229. typename = internal::iterator_category_t<ForwardIterator>,
  2230. typename = internal::iterator_category_t<OutputIterator>>
  2231. constexpr auto unique_copy(ForwardIterator first,
  2232. ForwardIterator last,
  2233. OutputIterator result,
  2234. Comp comp = {},
  2235. Proj proj = {}) {
  2236. return std::unique_copy(first, last, result,
  2237. internal::ProjectedBinaryPredicate(comp, proj, proj));
  2238. }
  2239. // Let `E(i)` be `bool(invoke(comp, invoke(proj, *i), invoke(proj, *(i - 1))))`.
  2240. //
  2241. // Mandates: `*begin(range)` is writable to `result`.
  2242. //
  2243. // Preconditions: The ranges `range` and `[result, result + size(range))` do not
  2244. // overlap.
  2245. //
  2246. // Effects: Copies only the first element from every consecutive group of equal
  2247. // elements referred to by the iterator `i` in `range` for which `E(i)` holds.
  2248. //
  2249. // Returns: `result + N`.
  2250. //
  2251. // Complexity: Exactly `size(range) - 1` applications of the corresponding
  2252. // predicate and no more than twice as many applications of any projection.
  2253. //
  2254. // Reference: https://wg21.link/alg.unique#:~:text=ranges::unique_copy(R
  2255. template <typename Range,
  2256. typename OutputIterator,
  2257. typename Comp = ranges::equal_to,
  2258. typename Proj = identity,
  2259. typename = internal::range_category_t<Range>,
  2260. typename = internal::iterator_category_t<OutputIterator>>
  2261. constexpr auto unique_copy(Range&& range,
  2262. OutputIterator result,
  2263. Comp comp = {},
  2264. Proj proj = {}) {
  2265. return ranges::unique_copy(ranges::begin(range), ranges::end(range), result,
  2266. std::move(comp), std::move(proj));
  2267. }
  2268. // [alg.reverse] Reverse
  2269. // Reference: https://wg21.link/alg.reverse
  2270. // Effects: For each non-negative integer `i < (last - first) / 2`, applies
  2271. // `std::iter_swap` to all pairs of iterators `first + i, (last - i) - 1`.
  2272. //
  2273. // Returns: `last`.
  2274. //
  2275. // Complexity: Exactly `(last - first)/2` swaps.
  2276. //
  2277. // Reference: https://wg21.link/alg.reverse#:~:text=ranges::reverse(I
  2278. template <typename BidirectionalIterator,
  2279. typename = internal::iterator_category_t<BidirectionalIterator>>
  2280. constexpr auto reverse(BidirectionalIterator first,
  2281. BidirectionalIterator last) {
  2282. std::reverse(first, last);
  2283. return last;
  2284. }
  2285. // Effects: For each non-negative integer `i < size(range) / 2`, applies
  2286. // `std::iter_swap` to all pairs of iterators
  2287. // `begin(range) + i, (end(range) - i) - 1`.
  2288. //
  2289. // Returns: `end(range)`.
  2290. //
  2291. // Complexity: Exactly `size(range)/2` swaps.
  2292. //
  2293. // Reference: https://wg21.link/alg.reverse#:~:text=ranges::reverse(R
  2294. template <typename Range, typename = internal::range_category_t<Range>>
  2295. constexpr auto reverse(Range&& range) {
  2296. return ranges::reverse(ranges::begin(range), ranges::end(range));
  2297. }
  2298. // Let `N` be `last - first`.
  2299. //
  2300. // Preconditions: The ranges `[first, last)` and `[result, result + N)` do not
  2301. // overlap.
  2302. //
  2303. // Effects: Copies the range `[first, last)` to the range `[result, result + N)`
  2304. // such that for every non-negative integer `i < N` the following assignment
  2305. // takes place: `*(result + N - 1 - i) = *(first + i)`.
  2306. //
  2307. // Returns: `result + N`.
  2308. //
  2309. // Complexity: Exactly `N` assignments.
  2310. //
  2311. // Reference: https://wg21.link/alg.reverse#:~:text=ranges::reverse_copy(I
  2312. template <typename BidirectionalIterator,
  2313. typename OutputIterator,
  2314. typename = internal::iterator_category_t<BidirectionalIterator>,
  2315. typename = internal::iterator_category_t<OutputIterator>>
  2316. constexpr auto reverse_copy(BidirectionalIterator first,
  2317. BidirectionalIterator last,
  2318. OutputIterator result) {
  2319. return std::reverse_copy(first, last, result);
  2320. }
  2321. // Let `N` be `size(range)`.
  2322. //
  2323. // Preconditions: The ranges `range` and `[result, result + N)` do not
  2324. // overlap.
  2325. //
  2326. // Effects: Copies `range` to the range `[result, result + N)` such that for
  2327. // every non-negative integer `i < N` the following assignment takes place:
  2328. // `*(result + N - 1 - i) = *(begin(range) + i)`.
  2329. //
  2330. // Returns: `result + N`.
  2331. //
  2332. // Complexity: Exactly `N` assignments.
  2333. //
  2334. // Reference: https://wg21.link/alg.reverse#:~:text=ranges::reverse_copy(R
  2335. template <typename Range,
  2336. typename OutputIterator,
  2337. typename = internal::range_category_t<Range>,
  2338. typename = internal::iterator_category_t<OutputIterator>>
  2339. constexpr auto reverse_copy(Range&& range, OutputIterator result) {
  2340. return ranges::reverse_copy(ranges::begin(range), ranges::end(range), result);
  2341. }
  2342. // [alg.rotate] Rotate
  2343. // Reference: https://wg21.link/alg.rotate
  2344. // Preconditions: `[first, middle)` and `[middle, last)` are valid ranges.
  2345. //
  2346. // Effects: For each non-negative integer `i < (last - first)`, places the
  2347. // element from the position `first + i` into position
  2348. // `first + (i + (last - middle)) % (last - first)`.
  2349. //
  2350. // Returns: `first + (last - middle)`.
  2351. //
  2352. // Complexity: At most `last - first` swaps.
  2353. //
  2354. // Reference: https://wg21.link/alg.rotate#:~:text=ranges::rotate(I
  2355. template <typename ForwardIterator,
  2356. typename = internal::iterator_category_t<ForwardIterator>>
  2357. constexpr auto rotate(ForwardIterator first,
  2358. ForwardIterator middle,
  2359. ForwardIterator last) {
  2360. return std::rotate(first, middle, last);
  2361. }
  2362. // Preconditions: `[begin(range), middle)` and `[middle, end(range))` are valid
  2363. // ranges.
  2364. //
  2365. // Effects: For each non-negative integer `i < size(range)`, places the element
  2366. // from the position `begin(range) + i` into position
  2367. // `begin(range) + (i + (end(range) - middle)) % size(range)`.
  2368. //
  2369. // Returns: `begin(range) + (end(range) - middle)`.
  2370. //
  2371. // Complexity: At most `size(range)` swaps.
  2372. //
  2373. // Reference: https://wg21.link/alg.rotate#:~:text=ranges::rotate(R
  2374. template <typename Range, typename = internal::range_category_t<Range>>
  2375. constexpr auto rotate(Range&& range, iterator_t<Range> middle) {
  2376. return ranges::rotate(ranges::begin(range), middle, ranges::end(range));
  2377. }
  2378. // Let `N` be `last - first`.
  2379. //
  2380. // Preconditions: `[first, middle)` and `[middle, last)` are valid ranges. The
  2381. // ranges `[first, last)` and `[result, result + N)` do not overlap.
  2382. //
  2383. // Effects: Copies the range `[first, last)` to the range `[result, result + N)`
  2384. // such that for each non-negative integer `i < N` the following assignment
  2385. // takes place: `*(result + i) = *(first + (i + (middle - first)) % N)`.
  2386. //
  2387. // Returns: `result + N`.
  2388. //
  2389. // Complexity: Exactly `N` assignments.
  2390. //
  2391. // Reference: https://wg21.link/alg.rotate#:~:text=ranges::rotate_copy(I
  2392. template <typename ForwardIterator,
  2393. typename OutputIterator,
  2394. typename = internal::iterator_category_t<ForwardIterator>,
  2395. typename = internal::iterator_category_t<OutputIterator>>
  2396. constexpr auto rotate_copy(ForwardIterator first,
  2397. ForwardIterator middle,
  2398. ForwardIterator last,
  2399. OutputIterator result) {
  2400. return std::rotate_copy(first, middle, last, result);
  2401. }
  2402. // Let `N` be `size(range)`.
  2403. //
  2404. // Preconditions: `[begin(range), middle)` and `[middle, end(range))` are valid
  2405. // ranges. The ranges `range` and `[result, result + N)` do not overlap.
  2406. //
  2407. // Effects: Copies `range` to the range `[result, result + N)` such that for
  2408. // each non-negative integer `i < N` the following assignment takes place:
  2409. // `*(result + i) = *(begin(range) + (i + (middle - begin(range))) % N)`.
  2410. //
  2411. // Returns: `result + N`.
  2412. //
  2413. // Complexity: Exactly `N` assignments.
  2414. //
  2415. // Reference: https://wg21.link/alg.rotate#:~:text=ranges::rotate_copy(R
  2416. template <typename Range,
  2417. typename OutputIterator,
  2418. typename = internal::range_category_t<Range>,
  2419. typename = internal::iterator_category_t<OutputIterator>>
  2420. constexpr auto rotate_copy(Range&& range,
  2421. iterator_t<Range> middle,
  2422. OutputIterator result) {
  2423. return ranges::rotate_copy(ranges::begin(range), middle, ranges::end(range),
  2424. result);
  2425. }
  2426. // [alg.random.sample] Sample
  2427. // Reference: https://wg21.link/alg.random.sample
  2428. // Currently not implemented due to lack of std::sample in C++14.
  2429. // TODO(crbug.com/1071094): Consider implementing a hand-rolled version.
  2430. // [alg.random.shuffle] Shuffle
  2431. // Reference: https://wg21.link/alg.random.shuffle
  2432. // Preconditions: The type `std::remove_reference_t<UniformRandomBitGenerator>`
  2433. // meets the uniform random bit generator requirements.
  2434. //
  2435. // Effects: Permutes the elements in the range `[first, last)` such that each
  2436. // possible permutation of those elements has equal probability of appearance.
  2437. //
  2438. // Returns: `last`.
  2439. //
  2440. // Complexity: Exactly `(last - first) - 1` swaps.
  2441. //
  2442. // Remarks: To the extent that the implementation of this function makes use of
  2443. // random numbers, the object referenced by g shall serve as the
  2444. // implementation's source of randomness.
  2445. //
  2446. // Reference: https://wg21.link/alg.random.shuffle#:~:text=ranges::shuffle(I
  2447. template <typename RandomAccessIterator,
  2448. typename UniformRandomBitGenerator,
  2449. typename = internal::iterator_category_t<RandomAccessIterator>>
  2450. constexpr auto shuffle(RandomAccessIterator first,
  2451. RandomAccessIterator last,
  2452. UniformRandomBitGenerator&& g) {
  2453. std::shuffle(first, last, std::forward<UniformRandomBitGenerator>(g));
  2454. return last;
  2455. }
  2456. // Preconditions: The type `std::remove_reference_t<UniformRandomBitGenerator>`
  2457. // meets the uniform random bit generator requirements.
  2458. //
  2459. // Effects: Permutes the elements in `range` such that each possible permutation
  2460. // of those elements has equal probability of appearance.
  2461. //
  2462. // Returns: `end(range)`.
  2463. //
  2464. // Complexity: Exactly `size(range) - 1` swaps.
  2465. //
  2466. // Remarks: To the extent that the implementation of this function makes use of
  2467. // random numbers, the object referenced by g shall serve as the
  2468. // implementation's source of randomness.
  2469. //
  2470. // Reference: https://wg21.link/alg.random.shuffle#:~:text=ranges::shuffle(R
  2471. template <typename Range,
  2472. typename UniformRandomBitGenerator,
  2473. typename = internal::range_category_t<Range>>
  2474. constexpr auto shuffle(Range&& range, UniformRandomBitGenerator&& g) {
  2475. return ranges::shuffle(ranges::begin(range), ranges::end(range),
  2476. std::forward<UniformRandomBitGenerator>(g));
  2477. }
  2478. // [alg.nonmodifying] Sorting and related operations
  2479. // Reference: https://wg21.link/alg.sorting
  2480. // [alg.sort] Sorting
  2481. // Reference: https://wg21.link/alg.sort
  2482. // [sort] sort
  2483. // Reference: https://wg21.link/sort
  2484. // Effects: Sorts the elements in the range `[first, last)` with respect to
  2485. // `comp` and `proj`.
  2486. //
  2487. // Returns: `last`.
  2488. //
  2489. // Complexity: Let `N` be `last - first`. `O(N log N)` comparisons and
  2490. // projections.
  2491. //
  2492. // Reference: https://wg21.link/sort#:~:text=ranges::sort(I
  2493. template <typename RandomAccessIterator,
  2494. typename Comp = ranges::less,
  2495. typename Proj = identity,
  2496. typename = internal::iterator_category_t<RandomAccessIterator>,
  2497. typename = indirect_result_t<Comp&,
  2498. projected<RandomAccessIterator, Proj>,
  2499. projected<RandomAccessIterator, Proj>>>
  2500. constexpr auto sort(RandomAccessIterator first,
  2501. RandomAccessIterator last,
  2502. Comp comp = {},
  2503. Proj proj = {}) {
  2504. std::sort(first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
  2505. return last;
  2506. }
  2507. // Effects: Sorts the elements in `range` with respect to `comp` and `proj`.
  2508. //
  2509. // Returns: `end(range)`.
  2510. //
  2511. // Complexity: Let `N` be `size(range)`. `O(N log N)` comparisons and
  2512. // projections.
  2513. //
  2514. // Reference: https://wg21.link/sort#:~:text=ranges::sort(R
  2515. template <typename Range,
  2516. typename Comp = ranges::less,
  2517. typename Proj = identity,
  2518. typename = internal::range_category_t<Range>,
  2519. typename = indirect_result_t<Comp&,
  2520. projected<iterator_t<Range>, Proj>,
  2521. projected<iterator_t<Range>, Proj>>>
  2522. constexpr auto sort(Range&& range, Comp comp = {}, Proj proj = {}) {
  2523. return ranges::sort(ranges::begin(range), ranges::end(range), std::move(comp),
  2524. std::move(proj));
  2525. }
  2526. // [stable.sort] stable_sort
  2527. // Reference: https://wg21.link/stable.sort
  2528. // Effects: Sorts the elements in the range `[first, last)` with respect to
  2529. // `comp` and `proj`.
  2530. //
  2531. // Returns: `last`.
  2532. //
  2533. // Complexity: Let `N` be `last - first`. If enough extra memory is available,
  2534. // `N log (N)` comparisons. Otherwise, at most `N log^2 (N)` comparisons. In
  2535. // either case, twice as many projections as the number of comparisons.
  2536. //
  2537. // Remarks: Stable.
  2538. //
  2539. // Reference: https://wg21.link/stable.sort#:~:text=ranges::stable_sort(I
  2540. template <typename RandomAccessIterator,
  2541. typename Comp = ranges::less,
  2542. typename Proj = identity,
  2543. typename = internal::iterator_category_t<RandomAccessIterator>,
  2544. typename = indirect_result_t<Comp&,
  2545. projected<RandomAccessIterator, Proj>,
  2546. projected<RandomAccessIterator, Proj>>>
  2547. constexpr auto stable_sort(RandomAccessIterator first,
  2548. RandomAccessIterator last,
  2549. Comp comp = {},
  2550. Proj proj = {}) {
  2551. std::stable_sort(first, last,
  2552. internal::ProjectedBinaryPredicate(comp, proj, proj));
  2553. return last;
  2554. }
  2555. // Effects: Sorts the elements in `range` with respect to `comp` and `proj`.
  2556. //
  2557. // Returns: `end(rang)`.
  2558. //
  2559. // Complexity: Let `N` be `size(range)`. If enough extra memory is available,
  2560. // `N log (N)` comparisons. Otherwise, at most `N log^2 (N)` comparisons. In
  2561. // either case, twice as many projections as the number of comparisons.
  2562. //
  2563. // Remarks: Stable.
  2564. //
  2565. // Reference: https://wg21.link/stable.sort#:~:text=ranges::stable_sort(R
  2566. template <typename Range,
  2567. typename Comp = ranges::less,
  2568. typename Proj = identity,
  2569. typename = internal::range_category_t<Range>,
  2570. typename = indirect_result_t<Comp&,
  2571. projected<iterator_t<Range>, Proj>,
  2572. projected<iterator_t<Range>, Proj>>>
  2573. constexpr auto stable_sort(Range&& range, Comp comp = {}, Proj proj = {}) {
  2574. return ranges::stable_sort(ranges::begin(range), ranges::end(range),
  2575. std::move(comp), std::move(proj));
  2576. }
  2577. // [partial.sort] partial_sort
  2578. // Reference: https://wg21.link/partial.sort
  2579. // Preconditions: `[first, middle)` and `[middle, last)` are valid ranges.
  2580. //
  2581. // Effects: Places the first `middle - first` elements from the range
  2582. // `[first, last)` as sorted with respect to `comp` and `proj` into the range
  2583. // `[first, middle)`. The rest of the elements in the range `[middle, last)` are
  2584. // placed in an unspecified order.
  2585. //
  2586. // Returns: `last`.
  2587. //
  2588. // Complexity: Approximately `(last - first) * log(middle - first)` comparisons,
  2589. // and twice as many projections.
  2590. //
  2591. // Reference: https://wg21.link/partial.sort#:~:text=ranges::partial_sort(I
  2592. template <typename RandomAccessIterator,
  2593. typename Comp = ranges::less,
  2594. typename Proj = identity,
  2595. typename = internal::iterator_category_t<RandomAccessIterator>,
  2596. typename = indirect_result_t<Comp&,
  2597. projected<RandomAccessIterator, Proj>,
  2598. projected<RandomAccessIterator, Proj>>>
  2599. constexpr auto partial_sort(RandomAccessIterator first,
  2600. RandomAccessIterator middle,
  2601. RandomAccessIterator last,
  2602. Comp comp = {},
  2603. Proj proj = {}) {
  2604. std::partial_sort(first, middle, last,
  2605. internal::ProjectedBinaryPredicate(comp, proj, proj));
  2606. return last;
  2607. }
  2608. // Preconditions: `[begin(range), middle)` and `[middle, end(range))` are valid
  2609. // ranges.
  2610. //
  2611. // Effects: Places the first `middle - begin(range)` elements from `range` as
  2612. // sorted with respect to `comp` and `proj` into the range
  2613. // `[begin(range), middle)`. The rest of the elements in the range
  2614. // `[middle, end(range))` are placed in an unspecified order.
  2615. //
  2616. // Returns: `end(range)`.
  2617. //
  2618. // Complexity: Approximately `size(range) * log(middle - begin(range))`
  2619. // comparisons, and twice as many projections.
  2620. //
  2621. // Reference: https://wg21.link/partial.sort#:~:text=ranges::partial_sort(R
  2622. template <typename Range,
  2623. typename Comp = ranges::less,
  2624. typename Proj = identity,
  2625. typename = internal::range_category_t<Range>,
  2626. typename = indirect_result_t<Comp&,
  2627. projected<iterator_t<Range>, Proj>,
  2628. projected<iterator_t<Range>, Proj>>>
  2629. constexpr auto partial_sort(Range&& range,
  2630. iterator_t<Range> middle,
  2631. Comp comp = {},
  2632. Proj proj = {}) {
  2633. return ranges::partial_sort(ranges::begin(range), middle, ranges::end(range),
  2634. std::move(comp), std::move(proj));
  2635. }
  2636. // [partial.sort.copy] partial_sort_copy
  2637. // Reference: https://wg21.link/partial.sort.copy
  2638. // Let `N` be `min(last - first, result_last - result_first)`.
  2639. //
  2640. // Preconditions: For iterators `a1` and `b1` in `[first, last)`, and iterators
  2641. // `x2` and `y2` in `[result_first, result_last)`, after evaluating the
  2642. // assignment `*y2 = *b1`, let `E` be the value of `bool(invoke(comp,
  2643. // invoke(proj1, *a1), invoke(proj2, *y2)))`. Then, after evaluating the
  2644. // assignment `*x2 = *a1`, `E` is equal to `bool(invoke(comp, invoke(proj2,
  2645. // *x2), invoke(proj2, *y2)))`.
  2646. //
  2647. // Effects: Places the first `N` elements as sorted with respect to `comp` and
  2648. // `proj2` into the range `[result_first, result_first + N)`.
  2649. //
  2650. // Returns: `result_first + N`.
  2651. //
  2652. // Complexity: Approximately `(last - first) * log N` comparisons, and twice as
  2653. // many projections.
  2654. //
  2655. // Reference:
  2656. // https://wg21.link/partial.sort.copy#:~:text=ranges::partial_sort_copy(I1
  2657. template <typename InputIterator,
  2658. typename RandomAccessIterator,
  2659. typename Comp = ranges::less,
  2660. typename Proj1 = identity,
  2661. typename Proj2 = identity,
  2662. typename = internal::iterator_category_t<InputIterator>,
  2663. typename = internal::iterator_category_t<RandomAccessIterator>,
  2664. typename = indirect_result_t<Comp&,
  2665. projected<InputIterator, Proj1>,
  2666. projected<RandomAccessIterator, Proj2>>,
  2667. typename = indirect_result_t<Comp&,
  2668. projected<RandomAccessIterator, Proj2>,
  2669. projected<InputIterator, Proj1>>>
  2670. constexpr auto partial_sort_copy(InputIterator first,
  2671. InputIterator last,
  2672. RandomAccessIterator result_first,
  2673. RandomAccessIterator result_last,
  2674. Comp comp = {},
  2675. Proj1 proj1 = {},
  2676. Proj2 proj2 = {}) {
  2677. // Needs to opt-in to all permutations, since std::partial_sort_copy expects
  2678. // comp(proj2(lhs), proj1(rhs)) to compile.
  2679. return std::partial_sort_copy(
  2680. first, last, result_first, result_last,
  2681. internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
  2682. }
  2683. // Let `N` be `min(size(range), size(result_range))`.
  2684. //
  2685. // Preconditions: For iterators `a1` and `b1` in `range`, and iterators
  2686. // `x2` and `y2` in `result_range`, after evaluating the assignment
  2687. // `*y2 = *b1`, let `E` be the value of
  2688. // `bool(invoke(comp, invoke(proj1, *a1), invoke(proj2, *y2)))`. Then, after
  2689. // evaluating the assignment `*x2 = *a1`, `E` is equal to
  2690. // `bool(invoke(comp, invoke(proj2, *x2), invoke(proj2, *y2)))`.
  2691. //
  2692. // Effects: Places the first `N` elements as sorted with respect to `comp` and
  2693. // `proj2` into the range `[begin(result_range), begin(result_range) + N)`.
  2694. //
  2695. // Returns: `begin(result_range) + N`.
  2696. //
  2697. // Complexity: Approximately `size(range) * log N` comparisons, and twice as
  2698. // many projections.
  2699. //
  2700. // Reference:
  2701. // https://wg21.link/partial.sort.copy#:~:text=ranges::partial_sort_copy(R1
  2702. template <typename Range1,
  2703. typename Range2,
  2704. typename Comp = ranges::less,
  2705. typename Proj1 = identity,
  2706. typename Proj2 = identity,
  2707. typename = internal::range_category_t<Range1>,
  2708. typename = internal::range_category_t<Range2>,
  2709. typename = indirect_result_t<Comp&,
  2710. projected<iterator_t<Range1>, Proj1>,
  2711. projected<iterator_t<Range2>, Proj2>>,
  2712. typename = indirect_result_t<Comp&,
  2713. projected<iterator_t<Range2>, Proj2>,
  2714. projected<iterator_t<Range1>, Proj1>>>
  2715. constexpr auto partial_sort_copy(Range1&& range,
  2716. Range2&& result_range,
  2717. Comp comp = {},
  2718. Proj1 proj1 = {},
  2719. Proj2 proj2 = {}) {
  2720. return ranges::partial_sort_copy(ranges::begin(range), ranges::end(range),
  2721. ranges::begin(result_range),
  2722. ranges::end(result_range), std::move(comp),
  2723. std::move(proj1), std::move(proj2));
  2724. }
  2725. // [is.sorted] is_sorted
  2726. // Reference: https://wg21.link/is.sorted
  2727. // Returns: Whether the range `[first, last)` is sorted with respect to `comp`
  2728. // and `proj`.
  2729. //
  2730. // Complexity: Linear.
  2731. //
  2732. // Reference: https://wg21.link/is.sorted#:~:text=ranges::is_sorted(I
  2733. template <typename ForwardIterator,
  2734. typename Comp = ranges::less,
  2735. typename Proj = identity,
  2736. typename = internal::iterator_category_t<ForwardIterator>,
  2737. typename = indirect_result_t<Comp&,
  2738. projected<ForwardIterator, Proj>,
  2739. projected<ForwardIterator, Proj>>>
  2740. constexpr auto is_sorted(ForwardIterator first,
  2741. ForwardIterator last,
  2742. Comp comp = {},
  2743. Proj proj = {}) {
  2744. return std::is_sorted(first, last,
  2745. internal::ProjectedBinaryPredicate(comp, proj, proj));
  2746. }
  2747. // Returns: Whether `range` is sorted with respect to `comp` and `proj`.
  2748. //
  2749. // Complexity: Linear.
  2750. //
  2751. // Reference: https://wg21.link/is.sorted#:~:text=ranges::is_sorted(R
  2752. template <typename Range,
  2753. typename Comp = ranges::less,
  2754. typename Proj = identity,
  2755. typename = internal::range_category_t<Range>,
  2756. typename = indirect_result_t<Comp&,
  2757. projected<iterator_t<Range>, Proj>,
  2758. projected<iterator_t<Range>, Proj>>>
  2759. constexpr auto is_sorted(Range&& range, Comp comp = {}, Proj proj = {}) {
  2760. return ranges::is_sorted(ranges::begin(range), ranges::end(range),
  2761. std::move(comp), std::move(proj));
  2762. }
  2763. // Returns: The last iterator `i` in `[first, last]` for which the range
  2764. // `[first, i)` is sorted with respect to `comp` and `proj`.
  2765. //
  2766. // Complexity: Linear.
  2767. //
  2768. // Reference: https://wg21.link/is.sorted#:~:text=ranges::is_sorted_until(I
  2769. template <typename ForwardIterator,
  2770. typename Comp = ranges::less,
  2771. typename Proj = identity,
  2772. typename = internal::iterator_category_t<ForwardIterator>,
  2773. typename = indirect_result_t<Comp&,
  2774. projected<ForwardIterator, Proj>,
  2775. projected<ForwardIterator, Proj>>>
  2776. constexpr auto is_sorted_until(ForwardIterator first,
  2777. ForwardIterator last,
  2778. Comp comp = {},
  2779. Proj proj = {}) {
  2780. return std::is_sorted_until(
  2781. first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
  2782. }
  2783. // Returns: The last iterator `i` in `[begin(range), end(range)]` for which the
  2784. // range `[begin(range), i)` is sorted with respect to `comp` and `proj`.
  2785. //
  2786. // Complexity: Linear.
  2787. //
  2788. // Reference: https://wg21.link/is.sorted#:~:text=ranges::is_sorted_until(R
  2789. template <typename Range,
  2790. typename Comp = ranges::less,
  2791. typename Proj = identity,
  2792. typename = internal::range_category_t<Range>,
  2793. typename = indirect_result_t<Comp&,
  2794. projected<iterator_t<Range>, Proj>,
  2795. projected<iterator_t<Range>, Proj>>>
  2796. constexpr auto is_sorted_until(Range&& range, Comp comp = {}, Proj proj = {}) {
  2797. return ranges::is_sorted_until(ranges::begin(range), ranges::end(range),
  2798. std::move(comp), std::move(proj));
  2799. }
  2800. // [alg.nth.element] Nth element
  2801. // Reference: https://wg21.link/alg.nth.element
  2802. // Preconditions: `[first, nth)` and `[nth, last)` are valid ranges.
  2803. //
  2804. // Effects: After `nth_element` the element in the position pointed to by `nth`
  2805. // is the element that would be in that position if the whole range were sorted
  2806. // with respect to `comp` and `proj`, unless `nth == last`. Also for every
  2807. // iterator `i` in the range `[first, nth)` and every iterator `j` in the range
  2808. // `[nth, last)` it holds that:
  2809. // `bool(invoke(comp, invoke(proj, *j), invoke(proj, *i)))` is false.
  2810. //
  2811. // Returns: `last`.
  2812. //
  2813. // Complexity: Linear on average.
  2814. //
  2815. // Reference: https://wg21.link/alg.nth.element#:~:text=ranges::nth_element(I
  2816. template <typename RandomAccessIterator,
  2817. typename Comp = ranges::less,
  2818. typename Proj = identity,
  2819. typename = internal::iterator_category_t<RandomAccessIterator>,
  2820. typename = indirect_result_t<Comp&,
  2821. projected<RandomAccessIterator, Proj>,
  2822. projected<RandomAccessIterator, Proj>>>
  2823. constexpr auto nth_element(RandomAccessIterator first,
  2824. RandomAccessIterator nth,
  2825. RandomAccessIterator last,
  2826. Comp comp = {},
  2827. Proj proj = {}) {
  2828. std::nth_element(first, nth, last,
  2829. internal::ProjectedBinaryPredicate(comp, proj, proj));
  2830. return last;
  2831. }
  2832. // Preconditions: `[begin(range), nth)` and `[nth, end(range))` are valid
  2833. // ranges.
  2834. //
  2835. // Effects: After `nth_element` the element in the position pointed to by `nth`
  2836. // is the element that would be in that position if the whole range were sorted
  2837. // with respect to `comp` and `proj`, unless `nth == end(range)`. Also for every
  2838. // iterator `i` in the range `[begin(range), nth)` and every iterator `j` in the
  2839. // range `[nth, end(range))` it holds that:
  2840. // `bool(invoke(comp, invoke(proj, *j), invoke(proj, *i)))` is false.
  2841. //
  2842. // Returns: `end(range)`.
  2843. //
  2844. // Complexity: Linear on average.
  2845. //
  2846. // Reference: https://wg21.link/alg.nth.element#:~:text=ranges::nth_element(R
  2847. template <typename Range,
  2848. typename Comp = ranges::less,
  2849. typename Proj = identity,
  2850. typename = internal::range_category_t<Range>,
  2851. typename = indirect_result_t<Comp&,
  2852. projected<iterator_t<Range>, Proj>,
  2853. projected<iterator_t<Range>, Proj>>>
  2854. constexpr auto nth_element(Range&& range,
  2855. iterator_t<Range> nth,
  2856. Comp comp = {},
  2857. Proj proj = {}) {
  2858. return ranges::nth_element(ranges::begin(range), nth, ranges::end(range),
  2859. std::move(comp), std::move(proj));
  2860. }
  2861. // [alg.binary.search] Binary search
  2862. // Reference: https://wg21.link/alg.binary.search
  2863. // [lower.bound] lower_bound
  2864. // Reference: https://wg21.link/lower.bound
  2865. // Preconditions: The elements `e` of `[first, last)` are partitioned with
  2866. // respect to the expression `bool(invoke(comp, invoke(proj, e), value))`.
  2867. //
  2868. // Returns: The furthermost iterator `i` in the range `[first, last]` such that
  2869. // for every iterator `j` in the range `[first, i)`,
  2870. // `bool(invoke(comp, invoke(proj, *j), value))` is true.
  2871. //
  2872. // Complexity: At most `log_2(last - first) + O(1)` comparisons and projections.
  2873. //
  2874. // Reference: https://wg21.link/lower.bound#:~:text=ranges::lower_bound(I
  2875. template <typename ForwardIterator,
  2876. typename T,
  2877. typename Comp = ranges::less,
  2878. typename Proj = identity,
  2879. typename = internal::iterator_category_t<ForwardIterator>>
  2880. constexpr auto lower_bound(ForwardIterator first,
  2881. ForwardIterator last,
  2882. const T& value,
  2883. Comp comp = {},
  2884. Proj proj = {}) {
  2885. // The second arg is guaranteed to be `value`, so we'll simply apply the
  2886. // identity projection.
  2887. identity value_proj;
  2888. return std::lower_bound(
  2889. first, last, value,
  2890. internal::ProjectedBinaryPredicate(comp, proj, value_proj));
  2891. }
  2892. // Preconditions: The elements `e` of `range` are partitioned with respect to
  2893. // the expression `bool(invoke(comp, invoke(proj, e), value))`.
  2894. //
  2895. // Returns: The furthermost iterator `i` in the range
  2896. // `[begin(range), end(range)]` such that for every iterator `j` in the range
  2897. // `[begin(range), i)`, `bool(invoke(comp, invoke(proj, *j), value))` is true.
  2898. //
  2899. // Complexity: At most `log_2(size(range)) + O(1)` comparisons and projections.
  2900. //
  2901. // Reference: https://wg21.link/lower.bound#:~:text=ranges::lower_bound(R
  2902. template <typename Range,
  2903. typename T,
  2904. typename Comp = ranges::less,
  2905. typename Proj = identity,
  2906. typename = internal::range_category_t<Range>>
  2907. constexpr auto lower_bound(Range&& range,
  2908. const T& value,
  2909. Comp comp = {},
  2910. Proj proj = {}) {
  2911. return ranges::lower_bound(ranges::begin(range), ranges::end(range), value,
  2912. std::move(comp), std::move(proj));
  2913. }
  2914. // [upper.bound] upper_bound
  2915. // Reference: https://wg21.link/upper.bound
  2916. // Preconditions: The elements `e` of `[first, last)` are partitioned with
  2917. // respect to the expression `!bool(invoke(comp, value, invoke(proj, e)))`.
  2918. //
  2919. // Returns: The furthermost iterator `i` in the range `[first, last]` such that
  2920. // for every iterator `j` in the range `[first, i)`,
  2921. // `!bool(invoke(comp, value, invoke(proj, *j)))` is true.
  2922. //
  2923. // Complexity: At most `log_2(last - first) + O(1)` comparisons and projections.
  2924. //
  2925. // Reference: https://wg21.link/upper.bound#:~:text=ranges::upper_bound(I
  2926. template <typename ForwardIterator,
  2927. typename T,
  2928. typename Comp = ranges::less,
  2929. typename Proj = identity,
  2930. typename = internal::iterator_category_t<ForwardIterator>>
  2931. constexpr auto upper_bound(ForwardIterator first,
  2932. ForwardIterator last,
  2933. const T& value,
  2934. Comp comp = {},
  2935. Proj proj = {}) {
  2936. // The first arg is guaranteed to be `value`, so we'll simply apply the
  2937. // identity projection.
  2938. identity value_proj;
  2939. return std::upper_bound(
  2940. first, last, value,
  2941. internal::ProjectedBinaryPredicate(comp, value_proj, proj));
  2942. }
  2943. // Preconditions: The elements `e` of `range` are partitioned with
  2944. // respect to the expression `!bool(invoke(comp, value, invoke(proj, e)))`.
  2945. //
  2946. // Returns: The furthermost iterator `i` in the range
  2947. // `[begin(range), end(range)]` such that for every iterator `j` in the range
  2948. // `[begin(range), i)`, `!bool(invoke(comp, value, invoke(proj, *j)))` is true.
  2949. //
  2950. // Complexity: At most `log_2(size(range)) + O(1)` comparisons and projections.
  2951. //
  2952. // Reference: https://wg21.link/upper.bound#:~:text=ranges::upper_bound(R
  2953. template <typename Range,
  2954. typename T,
  2955. typename Comp = ranges::less,
  2956. typename Proj = identity,
  2957. typename = internal::range_category_t<Range>>
  2958. constexpr auto upper_bound(Range&& range,
  2959. const T& value,
  2960. Comp comp = {},
  2961. Proj proj = {}) {
  2962. return ranges::upper_bound(ranges::begin(range), ranges::end(range), value,
  2963. std::move(comp), std::move(proj));
  2964. }
  2965. // [equal.range] equal_range
  2966. // Reference: https://wg21.link/equal.range
  2967. // Preconditions: The elements `e` of `[first, last)` are partitioned with
  2968. // respect to the expressions `bool(invoke(comp, invoke(proj, e), value))` and
  2969. // `!bool(invoke(comp, value, invoke(proj, e)))`.
  2970. //
  2971. // Returns: `{ranges::lower_bound(first, last, value, comp, proj),
  2972. // ranges::upper_bound(first, last, value, comp, proj)}`.
  2973. //
  2974. // Complexity: At most 2 ∗ log_2(last - first) + O(1) comparisons and
  2975. // projections.
  2976. //
  2977. // Reference: https://wg21.link/equal.range#:~:text=ranges::equal_range(I
  2978. template <typename ForwardIterator,
  2979. typename T,
  2980. typename Comp = ranges::less,
  2981. typename Proj = identity,
  2982. typename = internal::iterator_category_t<ForwardIterator>>
  2983. constexpr auto equal_range(ForwardIterator first,
  2984. ForwardIterator last,
  2985. const T& value,
  2986. Comp comp = {},
  2987. Proj proj = {}) {
  2988. // Note: This does not dispatch to std::equal_range, as otherwise it would not
  2989. // be possible to prevent applying `proj` to `value`, which can result in
  2990. // unintended behavior.
  2991. return std::make_pair(ranges::lower_bound(first, last, value, comp, proj),
  2992. ranges::upper_bound(first, last, value, comp, proj));
  2993. }
  2994. // Preconditions: The elements `e` of `range` are partitioned with
  2995. // respect to the expressions `bool(invoke(comp, invoke(proj, e), value))` and
  2996. // `!bool(invoke(comp, value, invoke(proj, e)))`.
  2997. //
  2998. // Returns: `{ranges::lower_bound(range, value, comp, proj),
  2999. // ranges::upper_bound(range, value, comp, proj)}`.
  3000. //
  3001. // Complexity: At most 2 ∗ log_2(size(range)) + O(1) comparisons and
  3002. // projections.
  3003. //
  3004. // Reference: https://wg21.link/equal.range#:~:text=ranges::equal_range(R
  3005. template <typename Range,
  3006. typename T,
  3007. typename Comp = ranges::less,
  3008. typename Proj = identity,
  3009. typename = internal::range_category_t<Range>>
  3010. constexpr auto equal_range(Range&& range,
  3011. const T& value,
  3012. Comp comp = {},
  3013. Proj proj = {}) {
  3014. return ranges::equal_range(ranges::begin(range), ranges::end(range), value,
  3015. std::move(comp), std::move(proj));
  3016. }
  3017. // [binary.search] binary_search
  3018. // Reference: https://wg21.link/binary.search
  3019. // Preconditions: The elements `e` of `[first, last)` are partitioned with
  3020. // respect to the expressions `bool(invoke(comp, invoke(proj, e), value))` and
  3021. // `!bool(invoke(comp, value, invoke(proj, e)))`.
  3022. //
  3023. // Returns: `true` if and only if for some iterator `i` in the range
  3024. // `[first, last)`, `!bool(invoke(comp, invoke(proj, *i), value)) &&
  3025. // !bool(invoke(comp, value, invoke(proj, *i)))` is true.
  3026. //
  3027. // Complexity: At most `log_2(last - first) + O(1)` comparisons and projections.
  3028. //
  3029. // Reference: https://wg21.link/binary.search#:~:text=ranges::binary_search(I
  3030. template <typename ForwardIterator,
  3031. typename T,
  3032. typename Comp = ranges::less,
  3033. typename Proj = identity,
  3034. typename = internal::iterator_category_t<ForwardIterator>>
  3035. constexpr auto binary_search(ForwardIterator first,
  3036. ForwardIterator last,
  3037. const T& value,
  3038. Comp comp = {},
  3039. Proj proj = {}) {
  3040. first = ranges::lower_bound(first, last, value, comp, proj);
  3041. return first != last && !invoke(comp, value, invoke(proj, *first));
  3042. }
  3043. // Preconditions: The elements `e` of `range` are partitioned with
  3044. // respect to the expressions `bool(invoke(comp, invoke(proj, e), value))` and
  3045. // `!bool(invoke(comp, value, invoke(proj, e)))`.
  3046. //
  3047. // Returns: `true` if and only if for some iterator `i` in `range`
  3048. // `!bool(invoke(comp, invoke(proj, *i), value)) &&
  3049. // !bool(invoke(comp, value, invoke(proj, *i)))` is true.
  3050. //
  3051. // Complexity: At most `log_2(size(range)) + O(1)` comparisons and projections.
  3052. //
  3053. // Reference: https://wg21.link/binary.search#:~:text=ranges::binary_search(R
  3054. template <typename Range,
  3055. typename T,
  3056. typename Comp = ranges::less,
  3057. typename Proj = identity,
  3058. typename = internal::range_category_t<Range>>
  3059. constexpr auto binary_search(Range&& range,
  3060. const T& value,
  3061. Comp comp = {},
  3062. Proj proj = {}) {
  3063. return ranges::binary_search(ranges::begin(range), ranges::end(range), value,
  3064. std::move(comp), std::move(proj));
  3065. }
  3066. // [alg.partitions] Partitions
  3067. // Reference: https://wg21.link/alg.partitions
  3068. // Returns: `true` if and only if the elements `e` of `[first, last)` are
  3069. // partitioned with respect to the expression
  3070. // `bool(invoke(pred, invoke(proj, e)))`.
  3071. //
  3072. // Complexity: Linear. At most `last - first` applications of `pred` and `proj`.
  3073. //
  3074. // Reference: https://wg21.link/alg.partitions#:~:text=ranges::is_partitioned(I
  3075. template <typename ForwardIterator,
  3076. typename Pred,
  3077. typename Proj = identity,
  3078. typename = internal::iterator_category_t<ForwardIterator>>
  3079. constexpr auto is_partitioned(ForwardIterator first,
  3080. ForwardIterator last,
  3081. Pred pred,
  3082. Proj proj = {}) {
  3083. return std::is_partitioned(first, last,
  3084. internal::ProjectedUnaryPredicate(pred, proj));
  3085. }
  3086. // Returns: `true` if and only if the elements `e` of `range` are partitioned
  3087. // with respect to the expression `bool(invoke(pred, invoke(proj, e)))`.
  3088. //
  3089. // Complexity: Linear. At most `size(range)` applications of `pred` and `proj`.
  3090. //
  3091. // Reference: https://wg21.link/alg.partitions#:~:text=ranges::is_partitioned(R
  3092. template <typename Range,
  3093. typename Pred,
  3094. typename Proj = identity,
  3095. typename = internal::range_category_t<Range>>
  3096. constexpr auto is_partitioned(Range&& range, Pred pred, Proj proj = {}) {
  3097. return ranges::is_partitioned(ranges::begin(range), ranges::end(range),
  3098. std::move(pred), std::move(proj));
  3099. }
  3100. // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
  3101. //
  3102. // Effects: Places all the elements `e` in `[first, last)` that satisfy `E(e)`
  3103. // before all the elements that do not.
  3104. //
  3105. // Returns: Let `i` be an iterator such that `E(*j)` is `true` for every
  3106. // iterator `j` in `[first, i)` and `false` for every iterator `j` in
  3107. // `[i, last)`. Returns: i.
  3108. //
  3109. // Complexity: Let `N = last - first`:
  3110. // Exactly `N` applications of the predicate and projection. At most `N / 2`
  3111. // swaps if the type of `first` models `bidirectional_iterator`, and at most `N`
  3112. // swaps otherwise.
  3113. //
  3114. // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition(I
  3115. template <typename ForwardIterator,
  3116. typename Pred,
  3117. typename Proj = identity,
  3118. typename = internal::iterator_category_t<ForwardIterator>>
  3119. constexpr auto partition(ForwardIterator first,
  3120. ForwardIterator last,
  3121. Pred pred,
  3122. Proj proj = {}) {
  3123. return std::partition(first, last,
  3124. internal::ProjectedUnaryPredicate(pred, proj));
  3125. }
  3126. // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
  3127. //
  3128. // Effects: Places all the elements `e` in `range` that satisfy `E(e)` before
  3129. // all the elements that do not.
  3130. //
  3131. // Returns: Let `i` be an iterator such that `E(*j)` is `true` for every
  3132. // iterator `j` in `[begin(range), i)` and `false` for every iterator `j` in
  3133. // `[i, last)`. Returns: i.
  3134. //
  3135. // Complexity: Let `N = size(range)`:
  3136. // Exactly `N` applications of the predicate and projection. At most `N / 2`
  3137. // swaps if the type of `first` models `bidirectional_iterator`, and at most `N`
  3138. // swaps otherwise.
  3139. //
  3140. // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition(R
  3141. template <typename Range,
  3142. typename Pred,
  3143. typename Proj = identity,
  3144. typename = internal::range_category_t<Range>>
  3145. constexpr auto partition(Range&& range, Pred pred, Proj proj = {}) {
  3146. return ranges::partition(ranges::begin(range), ranges::end(range),
  3147. std::move(pred), std::move(proj));
  3148. }
  3149. // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
  3150. //
  3151. // Effects: Places all the elements `e` in `[first, last)` that satisfy `E(e)`
  3152. // before all the elements that do not. The relative order of the elements in
  3153. // both groups is preserved.
  3154. //
  3155. // Returns: Let `i` be an iterator such that for every iterator `j` in
  3156. // `[first, i)`, `E(*j)` is `true`, and for every iterator `j` in the range
  3157. // `[i, last)`, `E(*j)` is `false`. Returns: `i`.
  3158. //
  3159. // Complexity: Let `N = last - first`:
  3160. // At most `N log N` swaps, but only `O(N)` swaps if there is enough extra
  3161. // memory. Exactly `N` applications of the predicate and projection.
  3162. //
  3163. // Reference:
  3164. // https://wg21.link/alg.partitions#:~:text=ranges::stable_partition(I
  3165. template <typename BidirectionalIterator,
  3166. typename Pred,
  3167. typename Proj = identity,
  3168. typename = internal::iterator_category_t<BidirectionalIterator>>
  3169. constexpr auto stable_partition(BidirectionalIterator first,
  3170. BidirectionalIterator last,
  3171. Pred pred,
  3172. Proj proj = {}) {
  3173. return std::stable_partition(first, last,
  3174. internal::ProjectedUnaryPredicate(pred, proj));
  3175. }
  3176. // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
  3177. //
  3178. // Effects: Places all the elements `e` in `range` that satisfy `E(e)` before
  3179. // all the elements that do not. The relative order of the elements in both
  3180. // groups is preserved.
  3181. //
  3182. // Returns: Let `i` be an iterator such that for every iterator `j` in
  3183. // `[begin(range), i)`, `E(*j)` is `true`, and for every iterator `j` in the
  3184. // range `[i, end(range))`, `E(*j)` is `false`. Returns: `i`.
  3185. //
  3186. // Complexity: Let `N = size(range)`:
  3187. // At most `N log N` swaps, but only `O(N)` swaps if there is enough extra
  3188. // memory. Exactly `N` applications of the predicate and projection.
  3189. //
  3190. // Reference:
  3191. // https://wg21.link/alg.partitions#:~:text=ranges::stable_partition(R
  3192. template <typename Range,
  3193. typename Pred,
  3194. typename Proj = identity,
  3195. typename = internal::range_category_t<Range>>
  3196. constexpr auto stable_partition(Range&& range, Pred pred, Proj proj = {}) {
  3197. return ranges::stable_partition(ranges::begin(range), ranges::end(range),
  3198. std::move(pred), std::move(proj));
  3199. }
  3200. // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
  3201. //
  3202. // Mandates: The expression `*first` is writable to `out_true` and `out_false`.
  3203. //
  3204. // Preconditions: The input range and output ranges do not overlap.
  3205. //
  3206. // Effects: For each iterator `i` in `[first, last)`, copies `*i` to the output
  3207. // range beginning with `out_true` if `E(*i)` is `true`, or to the output range
  3208. // beginning with `out_false` otherwise.
  3209. //
  3210. // Returns: Let `o1` be the end of the output range beginning at `out_true`, and
  3211. // `o2` the end of the output range beginning at `out_false`.
  3212. // Returns `{o1, o2}`.
  3213. //
  3214. // Complexity: Exactly `last - first` applications of `pred` and `proj`.
  3215. //
  3216. // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition_copy(I
  3217. template <typename InputIterator,
  3218. typename OutputIterator1,
  3219. typename OutputIterator2,
  3220. typename Pred,
  3221. typename Proj = identity,
  3222. typename = internal::iterator_category_t<InputIterator>,
  3223. typename = internal::iterator_category_t<OutputIterator1>,
  3224. typename = internal::iterator_category_t<OutputIterator2>>
  3225. constexpr auto partition_copy(InputIterator first,
  3226. InputIterator last,
  3227. OutputIterator1 out_true,
  3228. OutputIterator2 out_false,
  3229. Pred pred,
  3230. Proj proj = {}) {
  3231. return std::partition_copy(first, last, out_true, out_false,
  3232. internal::ProjectedUnaryPredicate(pred, proj));
  3233. }
  3234. // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
  3235. //
  3236. // Mandates: The expression `*begin(range)` is writable to `out_true` and
  3237. // `out_false`.
  3238. //
  3239. // Preconditions: The input range and output ranges do not overlap.
  3240. //
  3241. // Effects: For each iterator `i` in `range`, copies `*i` to the output range
  3242. // beginning with `out_true` if `E(*i)` is `true`, or to the output range
  3243. // beginning with `out_false` otherwise.
  3244. //
  3245. // Returns: Let `o1` be the end of the output range beginning at `out_true`, and
  3246. // `o2` the end of the output range beginning at `out_false`.
  3247. // Returns `{o1, o2}`.
  3248. //
  3249. // Complexity: Exactly `size(range)` applications of `pred` and `proj`.
  3250. //
  3251. // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition_copy(R
  3252. template <typename Range,
  3253. typename OutputIterator1,
  3254. typename OutputIterator2,
  3255. typename Pred,
  3256. typename Proj = identity,
  3257. typename = internal::range_category_t<Range>,
  3258. typename = internal::iterator_category_t<OutputIterator1>,
  3259. typename = internal::iterator_category_t<OutputIterator2>>
  3260. constexpr auto partition_copy(Range&& range,
  3261. OutputIterator1 out_true,
  3262. OutputIterator2 out_false,
  3263. Pred pred,
  3264. Proj proj = {}) {
  3265. return ranges::partition_copy(ranges::begin(range), ranges::end(range),
  3266. out_true, out_false, std::move(pred),
  3267. std::move(proj));
  3268. }
  3269. // let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
  3270. //
  3271. // Preconditions: The elements `e` of `[first, last)` are partitioned with
  3272. // respect to `E(e)`.
  3273. //
  3274. // Returns: An iterator `mid` such that `E(*i)` is `true` for all iterators `i`
  3275. // in `[first, mid)`, and `false` for all iterators `i` in `[mid, last)`.
  3276. //
  3277. // Complexity: `O(log(last - first))` applications of `pred` and `proj`.
  3278. //
  3279. // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition_point(I
  3280. template <typename ForwardIterator,
  3281. typename Pred,
  3282. typename Proj = identity,
  3283. typename = internal::iterator_category_t<ForwardIterator>>
  3284. constexpr auto partition_point(ForwardIterator first,
  3285. ForwardIterator last,
  3286. Pred pred,
  3287. Proj proj = {}) {
  3288. return std::partition_point(first, last,
  3289. internal::ProjectedUnaryPredicate(pred, proj));
  3290. }
  3291. // let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
  3292. //
  3293. // Preconditions: The elements `e` of `range` are partitioned with respect to
  3294. // `E(e)`.
  3295. //
  3296. // Returns: An iterator `mid` such that `E(*i)` is `true` for all iterators `i`
  3297. // in `[begin(range), mid)`, and `false` for all iterators `i` in
  3298. // `[mid, end(range))`.
  3299. //
  3300. // Complexity: `O(log(size(range)))` applications of `pred` and `proj`.
  3301. //
  3302. // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition_point(R
  3303. template <typename Range,
  3304. typename Pred,
  3305. typename Proj = identity,
  3306. typename = internal::range_category_t<Range>>
  3307. constexpr auto partition_point(Range&& range, Pred pred, Proj proj = {}) {
  3308. return ranges::partition_point(ranges::begin(range), ranges::end(range),
  3309. std::move(pred), std::move(proj));
  3310. }
  3311. // [alg.merge] Merge
  3312. // Reference: https://wg21.link/alg.merge
  3313. // Let `N` be `(last1 - first1) + (last2 - first2)`.
  3314. //
  3315. // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
  3316. // with respect to `comp` and `proj1` or `proj2`, respectively. The resulting
  3317. // range does not overlap with either of the original ranges.
  3318. //
  3319. // Effects: Copies all the elements of the two ranges `[first1, last1)` and
  3320. // `[first2, last2)` into the range `[result, result_last)`, where `result_last`
  3321. // is `result + N`. If an element `a` precedes `b` in an input range, `a` is
  3322. // copied into the output range before `b`. If `e1` is an element of
  3323. // `[first1, last1)` and `e2` of `[first2, last2)`, `e2` is copied into the
  3324. // output range before `e1` if and only if
  3325. // `bool(invoke(comp, invoke(proj2, e2), invoke(proj1, e1)))` is `true`.
  3326. //
  3327. // Returns: `result_last`.
  3328. //
  3329. // Complexity: At most `N - 1` comparisons and applications of each projection.
  3330. //
  3331. // Remarks: Stable.
  3332. //
  3333. // Reference: https://wg21.link/alg.merge#:~:text=ranges::merge(I1
  3334. template <typename InputIterator1,
  3335. typename InputIterator2,
  3336. typename OutputIterator,
  3337. typename Comp = ranges::less,
  3338. typename Proj1 = identity,
  3339. typename Proj2 = identity,
  3340. typename = internal::iterator_category_t<InputIterator1>,
  3341. typename = internal::iterator_category_t<InputIterator2>,
  3342. typename = internal::iterator_category_t<OutputIterator>,
  3343. typename = indirect_result_t<Comp&,
  3344. projected<InputIterator1, Proj1>,
  3345. projected<InputIterator2, Proj2>>,
  3346. typename = indirect_result_t<Comp&,
  3347. projected<InputIterator2, Proj2>,
  3348. projected<InputIterator1, Proj1>>>
  3349. constexpr auto merge(InputIterator1 first1,
  3350. InputIterator1 last1,
  3351. InputIterator2 first2,
  3352. InputIterator2 last2,
  3353. OutputIterator result,
  3354. Comp comp = {},
  3355. Proj1 proj1 = {},
  3356. Proj2 proj2 = {}) {
  3357. // Needs to opt-in to all permutations, since std::merge expects
  3358. // comp(proj2(lhs), proj1(rhs)) to compile.
  3359. return std::merge(
  3360. first1, last1, first2, last2, result,
  3361. internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
  3362. }
  3363. // Let `N` be `size(range1) + size(range2)`.
  3364. //
  3365. // Preconditions: The ranges `range1` and `range2` are sorted with respect to
  3366. // `comp` and `proj1` or `proj2`, respectively. The resulting range does not
  3367. // overlap with either of the original ranges.
  3368. //
  3369. // Effects: Copies all the elements of the two ranges `range1` and `range2` into
  3370. // the range `[result, result_last)`, where `result_last` is `result + N`. If an
  3371. // element `a` precedes `b` in an input range, `a` is copied into the output
  3372. // range before `b`. If `e1` is an element of `range1` and `e2` of `range2`,
  3373. // `e2` is copied into the output range before `e1` if and only if
  3374. // `bool(invoke(comp, invoke(proj2, e2), invoke(proj1, e1)))` is `true`.
  3375. //
  3376. // Returns: `result_last`.
  3377. //
  3378. // Complexity: At most `N - 1` comparisons and applications of each projection.
  3379. //
  3380. // Remarks: Stable.
  3381. //
  3382. // Reference: https://wg21.link/alg.merge#:~:text=ranges::merge(R1
  3383. template <typename Range1,
  3384. typename Range2,
  3385. typename OutputIterator,
  3386. typename Comp = ranges::less,
  3387. typename Proj1 = identity,
  3388. typename Proj2 = identity,
  3389. typename = internal::range_category_t<Range1>,
  3390. typename = internal::range_category_t<Range2>,
  3391. typename = internal::iterator_category_t<OutputIterator>,
  3392. typename = indirect_result_t<Comp&,
  3393. projected<iterator_t<Range1>, Proj1>,
  3394. projected<iterator_t<Range2>, Proj2>>,
  3395. typename = indirect_result_t<Comp&,
  3396. projected<iterator_t<Range2>, Proj2>,
  3397. projected<iterator_t<Range1>, Proj1>>>
  3398. constexpr auto merge(Range1&& range1,
  3399. Range2&& range2,
  3400. OutputIterator result,
  3401. Comp comp = {},
  3402. Proj1 proj1 = {},
  3403. Proj2 proj2 = {}) {
  3404. return ranges::merge(ranges::begin(range1), ranges::end(range1),
  3405. ranges::begin(range2), ranges::end(range2), result,
  3406. std::move(comp), std::move(proj1), std::move(proj2));
  3407. }
  3408. // Preconditions: `[first, middle)` and `[middle, last)` are valid ranges sorted
  3409. // with respect to `comp` and `proj`.
  3410. //
  3411. // Effects: Merges two sorted consecutive ranges `[first, middle)` and
  3412. // `[middle, last)`, putting the result of the merge into the range
  3413. // `[first, last)`. The resulting range is sorted with respect to `comp` and
  3414. // `proj`.
  3415. //
  3416. // Returns: `last`.
  3417. //
  3418. // Complexity: Let `N = last - first`: If enough additional memory is available,
  3419. // exactly `N - 1` comparisons. Otherwise, `O(N log N)` comparisons. In either
  3420. // case, twice as many projections as comparisons.
  3421. //
  3422. // Remarks: Stable.
  3423. //
  3424. // Reference: https://wg21.link/alg.merge#:~:text=ranges::inplace_merge(I
  3425. template <typename BidirectionalIterator,
  3426. typename Comp = ranges::less,
  3427. typename Proj = identity,
  3428. typename = internal::iterator_category_t<BidirectionalIterator>>
  3429. constexpr auto inplace_merge(BidirectionalIterator first,
  3430. BidirectionalIterator middle,
  3431. BidirectionalIterator last,
  3432. Comp comp = {},
  3433. Proj proj = {}) {
  3434. std::inplace_merge(first, middle, last,
  3435. internal::ProjectedBinaryPredicate(comp, proj, proj));
  3436. return last;
  3437. }
  3438. // Preconditions: `[begin(range), middle)` and `[middle, end(range))` are valid
  3439. // ranges sorted with respect to `comp` and `proj`.
  3440. //
  3441. // Effects: Merges two sorted consecutive ranges `[begin(range), middle)` and
  3442. // `[middle, end(range))`, putting the result of the merge into `range`. The
  3443. // resulting range is sorted with respect to `comp` and `proj`.
  3444. //
  3445. // Returns: `end(range)`.
  3446. //
  3447. // Complexity: Let `N = size(range)`: If enough additional memory is available,
  3448. // exactly `N - 1` comparisons. Otherwise, `O(N log N)` comparisons. In either
  3449. // case, twice as many projections as comparisons.
  3450. //
  3451. // Remarks: Stable.
  3452. //
  3453. // Reference: https://wg21.link/alg.merge#:~:text=ranges::inplace_merge(R
  3454. template <typename Range,
  3455. typename Comp = ranges::less,
  3456. typename Proj = identity,
  3457. typename = internal::range_category_t<Range>>
  3458. constexpr auto inplace_merge(Range&& range,
  3459. iterator_t<Range> middle,
  3460. Comp comp = {},
  3461. Proj proj = {}) {
  3462. return ranges::inplace_merge(ranges::begin(range), middle, ranges::end(range),
  3463. std::move(comp), std::move(proj));
  3464. }
  3465. // [alg.set.operations] Set operations on sorted structures
  3466. // Reference: https://wg21.link/alg.set.operations
  3467. // [includes] includes
  3468. // Reference: https://wg21.link/includes
  3469. // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
  3470. // with respect to `comp` and `proj1` or `proj2`, respectively.
  3471. //
  3472. // Returns: `true` if and only if `[first2, last2)` is a subsequence of
  3473. // `[first1, last1)`.
  3474. //
  3475. // Complexity: At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
  3476. // comparisons and applications of each projection.
  3477. //
  3478. // Reference: https://wg21.link/includes#:~:text=ranges::includes(I1
  3479. template <typename InputIterator1,
  3480. typename InputIterator2,
  3481. typename Comp = ranges::less,
  3482. typename Proj1 = identity,
  3483. typename Proj2 = identity,
  3484. typename = internal::iterator_category_t<InputIterator1>,
  3485. typename = internal::iterator_category_t<InputIterator2>,
  3486. typename = indirect_result_t<Comp&,
  3487. projected<InputIterator1, Proj1>,
  3488. projected<InputIterator2, Proj2>>,
  3489. typename = indirect_result_t<Comp&,
  3490. projected<InputIterator2, Proj2>,
  3491. projected<InputIterator1, Proj1>>>
  3492. constexpr auto includes(InputIterator1 first1,
  3493. InputIterator1 last1,
  3494. InputIterator2 first2,
  3495. InputIterator2 last2,
  3496. Comp comp = {},
  3497. Proj1 proj1 = {},
  3498. Proj2 proj2 = {}) {
  3499. // Needs to opt-in to all permutations, since std::includes expects
  3500. // comp(proj1(lhs), proj2(rhs)) and comp(proj2(lhs), proj1(rhs)) to compile.
  3501. return std::includes(
  3502. first1, last1, first2, last2,
  3503. internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
  3504. }
  3505. // Preconditions: The ranges `range1` and `range2` are sorted with respect to
  3506. // `comp` and `proj1` or `proj2`, respectively.
  3507. //
  3508. // Returns: `true` if and only if `range2` is a subsequence of `range1`.
  3509. //
  3510. // Complexity: At most `2 * (size(range1) + size(range2)) - 1` comparisons and
  3511. // applications of each projection.
  3512. //
  3513. // Reference: https://wg21.link/includes#:~:text=ranges::includes(R1
  3514. template <typename Range1,
  3515. typename Range2,
  3516. typename Comp = ranges::less,
  3517. typename Proj1 = identity,
  3518. typename Proj2 = identity,
  3519. typename = internal::range_category_t<Range1>,
  3520. typename = internal::range_category_t<Range2>,
  3521. typename = indirect_result_t<Comp&,
  3522. projected<iterator_t<Range1>, Proj1>,
  3523. projected<iterator_t<Range2>, Proj2>>,
  3524. typename = indirect_result_t<Comp&,
  3525. projected<iterator_t<Range2>, Proj2>,
  3526. projected<iterator_t<Range1>, Proj1>>>
  3527. constexpr auto includes(Range1&& range1,
  3528. Range2&& range2,
  3529. Comp comp = {},
  3530. Proj1 proj1 = {},
  3531. Proj2 proj2 = {}) {
  3532. return ranges::includes(ranges::begin(range1), ranges::end(range1),
  3533. ranges::begin(range2), ranges::end(range2),
  3534. std::move(comp), std::move(proj1), std::move(proj2));
  3535. }
  3536. // [set.union] set_union
  3537. // Reference: https://wg21.link/set.union
  3538. // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
  3539. // with respect to `comp` and `proj1` or `proj2`, respectively. The resulting
  3540. // range does not overlap with either of the original ranges.
  3541. //
  3542. // Effects: Constructs a sorted union of the elements from the two ranges; that
  3543. // is, the set of elements that are present in one or both of the ranges.
  3544. //
  3545. // Returns: The end of the constructed range.
  3546. //
  3547. // Complexity: At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
  3548. // comparisons and applications of each projection.
  3549. //
  3550. // Remarks: Stable. If `[first1, last1)` contains `m` elements that are
  3551. // equivalent to each other and `[first2, last2)` contains `n` elements that are
  3552. // equivalent to them, then all `m` elements from the first range are copied to
  3553. // the output range, in order, and then the final `max(n - m , 0)` elements from
  3554. // the second range are copied to the output range, in order.
  3555. //
  3556. // Reference: https://wg21.link/set.union#:~:text=ranges::set_union(I1
  3557. template <typename InputIterator1,
  3558. typename InputIterator2,
  3559. typename OutputIterator,
  3560. typename Comp = ranges::less,
  3561. typename Proj1 = identity,
  3562. typename Proj2 = identity,
  3563. typename = internal::iterator_category_t<InputIterator1>,
  3564. typename = internal::iterator_category_t<InputIterator2>,
  3565. typename = internal::iterator_category_t<OutputIterator>,
  3566. typename = indirect_result_t<Comp&,
  3567. projected<InputIterator1, Proj1>,
  3568. projected<InputIterator2, Proj2>>,
  3569. typename = indirect_result_t<Comp&,
  3570. projected<InputIterator2, Proj2>,
  3571. projected<InputIterator1, Proj1>>>
  3572. constexpr auto set_union(InputIterator1 first1,
  3573. InputIterator1 last1,
  3574. InputIterator2 first2,
  3575. InputIterator2 last2,
  3576. OutputIterator result,
  3577. Comp comp = {},
  3578. Proj1 proj1 = {},
  3579. Proj2 proj2 = {}) {
  3580. // Needs to opt-in to all permutations, since std::set_union expects
  3581. // comp(proj1(lhs), proj2(rhs)) and comp(proj2(lhs), proj1(rhs)) to compile.
  3582. return std::set_union(
  3583. first1, last1, first2, last2, result,
  3584. internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
  3585. }
  3586. // Preconditions: The ranges `range1` and `range2` are sorted with respect to
  3587. // `comp` and `proj1` or `proj2`, respectively. The resulting range does not
  3588. // overlap with either of the original ranges.
  3589. //
  3590. // Effects: Constructs a sorted union of the elements from the two ranges; that
  3591. // is, the set of elements that are present in one or both of the ranges.
  3592. //
  3593. // Returns: The end of the constructed range.
  3594. //
  3595. // Complexity: At most `2 * (size(range1) + size(range2)) - 1` comparisons and
  3596. // applications of each projection.
  3597. //
  3598. // Remarks: Stable. If `range1` contains `m` elements that are equivalent to
  3599. // each other and `range2` contains `n` elements that are equivalent to them,
  3600. // then all `m` elements from the first range are copied to the output range, in
  3601. // order, and then the final `max(n - m , 0)` elements from the second range are
  3602. // copied to the output range, in order.
  3603. //
  3604. // Reference: https://wg21.link/set.union#:~:text=ranges::set_union(R1
  3605. template <typename Range1,
  3606. typename Range2,
  3607. typename OutputIterator,
  3608. typename Comp = ranges::less,
  3609. typename Proj1 = identity,
  3610. typename Proj2 = identity,
  3611. typename = internal::range_category_t<Range1>,
  3612. typename = internal::range_category_t<Range2>,
  3613. typename = internal::iterator_category_t<OutputIterator>,
  3614. typename = indirect_result_t<Comp&,
  3615. projected<iterator_t<Range1>, Proj1>,
  3616. projected<iterator_t<Range2>, Proj2>>,
  3617. typename = indirect_result_t<Comp&,
  3618. projected<iterator_t<Range2>, Proj2>,
  3619. projected<iterator_t<Range1>, Proj1>>>
  3620. constexpr auto set_union(Range1&& range1,
  3621. Range2&& range2,
  3622. OutputIterator result,
  3623. Comp comp = {},
  3624. Proj1 proj1 = {},
  3625. Proj2 proj2 = {}) {
  3626. return ranges::set_union(ranges::begin(range1), ranges::end(range1),
  3627. ranges::begin(range2), ranges::end(range2), result,
  3628. std::move(comp), std::move(proj1), std::move(proj2));
  3629. }
  3630. // [set.intersection] set_intersection
  3631. // Reference: https://wg21.link/set.intersection
  3632. // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
  3633. // with respect to `comp` and `proj1` or `proj2`, respectively. The resulting
  3634. // range does not overlap with either of the original ranges.
  3635. //
  3636. // Effects: Constructs a sorted intersection of the elements from the two
  3637. // ranges; that is, the set of elements that are present in both of the ranges.
  3638. //
  3639. // Returns: The end of the constructed range.
  3640. //
  3641. // Complexity: At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
  3642. // comparisons and applications of each projection.
  3643. //
  3644. // Remarks: Stable. If `[first1, last1)` contains `m` elements that are
  3645. // equivalent to each other and `[first2, last2)` contains `n` elements that are
  3646. // equivalent to them, the first `min(m, n)` elements are copied from the first
  3647. // range to the output range, in order.
  3648. //
  3649. // Reference:
  3650. // https://wg21.link/set.intersection#:~:text=ranges::set_intersection(I1
  3651. template <typename InputIterator1,
  3652. typename InputIterator2,
  3653. typename OutputIterator,
  3654. typename Comp = ranges::less,
  3655. typename Proj1 = identity,
  3656. typename Proj2 = identity,
  3657. typename = internal::iterator_category_t<InputIterator1>,
  3658. typename = internal::iterator_category_t<InputIterator2>,
  3659. typename = internal::iterator_category_t<OutputIterator>,
  3660. typename = indirect_result_t<Comp&,
  3661. projected<InputIterator1, Proj1>,
  3662. projected<InputIterator2, Proj2>>,
  3663. typename = indirect_result_t<Comp&,
  3664. projected<InputIterator2, Proj2>,
  3665. projected<InputIterator1, Proj1>>>
  3666. constexpr auto set_intersection(InputIterator1 first1,
  3667. InputIterator1 last1,
  3668. InputIterator2 first2,
  3669. InputIterator2 last2,
  3670. OutputIterator result,
  3671. Comp comp = {},
  3672. Proj1 proj1 = {},
  3673. Proj2 proj2 = {}) {
  3674. // Needs to opt-in to all permutations, since std::set_intersection expects
  3675. // comp(proj1(lhs), proj2(rhs)) and comp(proj2(lhs), proj1(rhs)) to compile.
  3676. return std::set_intersection(
  3677. first1, last1, first2, last2, result,
  3678. internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
  3679. }
  3680. // Preconditions: The ranges `range1` and `range2` are sorted with respect to
  3681. // `comp` and `proj1` or `proj2`, respectively. The resulting range does not
  3682. // overlap with either of the original ranges.
  3683. //
  3684. // Effects: Constructs a sorted intersection of the elements from the two
  3685. // ranges; that is, the set of elements that are present in both of the ranges.
  3686. //
  3687. // Returns: The end of the constructed range.
  3688. //
  3689. // Complexity: At most `2 * (size(range1) + size(range2)) - 1` comparisons and
  3690. // applications of each projection.
  3691. //
  3692. // Remarks: Stable. If `range1` contains `m` elements that are equivalent to
  3693. // each other and `range2` contains `n` elements that are equivalent to them,
  3694. // the first `min(m, n)` elements are copied from the first range to the output
  3695. // range, in order.
  3696. //
  3697. // Reference:
  3698. // https://wg21.link/set.intersection#:~:text=ranges::set_intersection(R1
  3699. template <typename Range1,
  3700. typename Range2,
  3701. typename OutputIterator,
  3702. typename Comp = ranges::less,
  3703. typename Proj1 = identity,
  3704. typename Proj2 = identity,
  3705. typename = internal::range_category_t<Range1>,
  3706. typename = internal::range_category_t<Range2>,
  3707. typename = internal::iterator_category_t<OutputIterator>,
  3708. typename = indirect_result_t<Comp&,
  3709. projected<iterator_t<Range1>, Proj1>,
  3710. projected<iterator_t<Range2>, Proj2>>,
  3711. typename = indirect_result_t<Comp&,
  3712. projected<iterator_t<Range2>, Proj2>,
  3713. projected<iterator_t<Range1>, Proj1>>>
  3714. constexpr auto set_intersection(Range1&& range1,
  3715. Range2&& range2,
  3716. OutputIterator result,
  3717. Comp comp = {},
  3718. Proj1 proj1 = {},
  3719. Proj2 proj2 = {}) {
  3720. return ranges::set_intersection(ranges::begin(range1), ranges::end(range1),
  3721. ranges::begin(range2), ranges::end(range2),
  3722. result, std::move(comp), std::move(proj1),
  3723. std::move(proj2));
  3724. }
  3725. // [set.difference] set_difference
  3726. // Reference: https://wg21.link/set.difference
  3727. // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
  3728. // with respect to `comp` and `proj1` or `proj2`, respectively. The resulting
  3729. // range does not overlap with either of the original ranges.
  3730. //
  3731. // Effects: Copies the elements of the range `[first1, last1)` which are not
  3732. // present in the range `[first2, last2)` to the range beginning at `result`.
  3733. // The elements in the constructed range are sorted.
  3734. //
  3735. // Returns: The end of the constructed range.
  3736. //
  3737. // Complexity: At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
  3738. // comparisons and applications of each projection.
  3739. //
  3740. // Remarks: If `[first1, last1)` contains `m` elements that are equivalent to
  3741. // each other and `[first2, last2)` contains `n` elements that are equivalent to
  3742. // them, the last `max(m - n, 0)` elements from `[first1, last1)` are copied to
  3743. // the output range, in order.
  3744. //
  3745. // Reference:
  3746. // https://wg21.link/set.difference#:~:text=ranges::set_difference(I1
  3747. template <typename InputIterator1,
  3748. typename InputIterator2,
  3749. typename OutputIterator,
  3750. typename Comp = ranges::less,
  3751. typename Proj1 = identity,
  3752. typename Proj2 = identity,
  3753. typename = internal::iterator_category_t<InputIterator1>,
  3754. typename = internal::iterator_category_t<InputIterator2>,
  3755. typename = internal::iterator_category_t<OutputIterator>,
  3756. typename = indirect_result_t<Comp&,
  3757. projected<InputIterator1, Proj1>,
  3758. projected<InputIterator2, Proj2>>,
  3759. typename = indirect_result_t<Comp&,
  3760. projected<InputIterator2, Proj2>,
  3761. projected<InputIterator1, Proj1>>>
  3762. constexpr auto set_difference(InputIterator1 first1,
  3763. InputIterator1 last1,
  3764. InputIterator2 first2,
  3765. InputIterator2 last2,
  3766. OutputIterator result,
  3767. Comp comp = {},
  3768. Proj1 proj1 = {},
  3769. Proj2 proj2 = {}) {
  3770. // Needs to opt-in to all permutations, since std::set_difference expects
  3771. // comp(proj1(lhs), proj2(rhs)) and comp(proj2(lhs), proj1(rhs)) to compile.
  3772. return std::set_difference(
  3773. first1, last1, first2, last2, result,
  3774. internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
  3775. }
  3776. // Preconditions: The ranges `range1` and `range2` are sorted with respect to
  3777. // `comp` and `proj1` or `proj2`, respectively. The resulting range does not
  3778. // overlap with either of the original ranges.
  3779. //
  3780. // Effects: Copies the elements of `range1` which are not present in `range2`
  3781. // to the range beginning at `result`. The elements in the constructed range are
  3782. // sorted.
  3783. //
  3784. // Returns: The end of the constructed range.
  3785. //
  3786. // Complexity: At most `2 * (size(range1) + size(range2)) - 1` comparisons and
  3787. // applications of each projection.
  3788. //
  3789. // Remarks: Stable. If `range1` contains `m` elements that are equivalent to
  3790. // each other and `range2` contains `n` elements that are equivalent to them,
  3791. // the last `max(m - n, 0)` elements from `range1` are copied to the output
  3792. // range, in order.
  3793. //
  3794. // Reference:
  3795. // https://wg21.link/set.difference#:~:text=ranges::set_difference(R1
  3796. template <typename Range1,
  3797. typename Range2,
  3798. typename OutputIterator,
  3799. typename Comp = ranges::less,
  3800. typename Proj1 = identity,
  3801. typename Proj2 = identity,
  3802. typename = internal::range_category_t<Range1>,
  3803. typename = internal::range_category_t<Range2>,
  3804. typename = internal::iterator_category_t<OutputIterator>,
  3805. typename = indirect_result_t<Comp&,
  3806. projected<iterator_t<Range1>, Proj1>,
  3807. projected<iterator_t<Range2>, Proj2>>,
  3808. typename = indirect_result_t<Comp&,
  3809. projected<iterator_t<Range2>, Proj2>,
  3810. projected<iterator_t<Range1>, Proj1>>>
  3811. constexpr auto set_difference(Range1&& range1,
  3812. Range2&& range2,
  3813. OutputIterator result,
  3814. Comp comp = {},
  3815. Proj1 proj1 = {},
  3816. Proj2 proj2 = {}) {
  3817. return ranges::set_difference(ranges::begin(range1), ranges::end(range1),
  3818. ranges::begin(range2), ranges::end(range2),
  3819. result, std::move(comp), std::move(proj1),
  3820. std::move(proj2));
  3821. }
  3822. // [set.symmetric.difference] set_symmetric_difference
  3823. // Reference: https://wg21.link/set.symmetric.difference
  3824. // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
  3825. // with respect to `comp` and `proj1` or `proj2`, respectively. The resulting
  3826. // range does not overlap with either of the original ranges.
  3827. //
  3828. // Effects: Copies the elements of the range `[first1, last1)` that are not
  3829. // present in the range `[first2, last2)`, and the elements of the range
  3830. // `[first2, last2)` that are not present in the range `[first1, last1)` to the
  3831. // range beginning at `result`. The elements in the constructed range are
  3832. // sorted.
  3833. //
  3834. // Returns: The end of the constructed range.
  3835. //
  3836. // Complexity: At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
  3837. // comparisons and applications of each projection.
  3838. //
  3839. // Remarks: Stable. If `[first1, last1)` contains `m` elements that are
  3840. // equivalent to each other and `[first2, last2)` contains `n` elements that are
  3841. // equivalent to them, then `|m - n|` of those elements shall be copied to the
  3842. // output range: the last `m - n` of these elements from `[first1, last1)` if
  3843. // `m > n`, and the last `n - m` of these elements from `[first2, last2)` if
  3844. // `m < n`. In either case, the elements are copied in order.
  3845. //
  3846. // Reference:
  3847. // https://wg21.link/set.symmetric.difference#:~:text=set_symmetric_difference(I1
  3848. template <typename InputIterator1,
  3849. typename InputIterator2,
  3850. typename OutputIterator,
  3851. typename Comp = ranges::less,
  3852. typename Proj1 = identity,
  3853. typename Proj2 = identity,
  3854. typename = internal::iterator_category_t<InputIterator1>,
  3855. typename = internal::iterator_category_t<InputIterator2>,
  3856. typename = internal::iterator_category_t<OutputIterator>,
  3857. typename = indirect_result_t<Comp&,
  3858. projected<InputIterator1, Proj1>,
  3859. projected<InputIterator2, Proj2>>,
  3860. typename = indirect_result_t<Comp&,
  3861. projected<InputIterator2, Proj2>,
  3862. projected<InputIterator1, Proj1>>>
  3863. constexpr auto set_symmetric_difference(InputIterator1 first1,
  3864. InputIterator1 last1,
  3865. InputIterator2 first2,
  3866. InputIterator2 last2,
  3867. OutputIterator result,
  3868. Comp comp = {},
  3869. Proj1 proj1 = {},
  3870. Proj2 proj2 = {}) {
  3871. // Needs to opt-in to all permutations, since std::set_symmetric_difference
  3872. // expects comp(proj1(lhs), proj2(rhs)) and comp(proj2(lhs), proj1(rhs)) to
  3873. // compile.
  3874. return std::set_symmetric_difference(
  3875. first1, last1, first2, last2, result,
  3876. internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
  3877. }
  3878. // Preconditions: The ranges `range1` and `range2` are sorted with respect to
  3879. // `comp` and `proj1` or `proj2`, respectively. The resulting range does not
  3880. // overlap with either of the original ranges.
  3881. //
  3882. // Effects: Copies the elements of `range1` that are not present in `range2`,
  3883. // and the elements of `range2` that are not present in `range1` to the range
  3884. // beginning at `result`. The elements in the constructed range are sorted.
  3885. //
  3886. // Returns: The end of the constructed range.
  3887. //
  3888. // Complexity: At most `2 * (size(range1) + size(range2)) - 1` comparisons and
  3889. // applications of each projection.
  3890. //
  3891. // Remarks: Stable. If `range1` contains `m` elements that are equivalent to
  3892. // each other and `range2` contains `n` elements that are equivalent to them,
  3893. // then `|m - n|` of those elements shall be copied to the output range: the
  3894. // last `m - n` of these elements from `range1` if `m > n`, and the last `n - m`
  3895. // of these elements from `range2` if `m < n`. In either case, the elements are
  3896. // copied in order.
  3897. //
  3898. // Reference:
  3899. // https://wg21.link/set.symmetric.difference#:~:text=set_symmetric_difference(R1
  3900. template <typename Range1,
  3901. typename Range2,
  3902. typename OutputIterator,
  3903. typename Comp = ranges::less,
  3904. typename Proj1 = identity,
  3905. typename Proj2 = identity,
  3906. typename = internal::range_category_t<Range1>,
  3907. typename = internal::range_category_t<Range2>,
  3908. typename = internal::iterator_category_t<OutputIterator>,
  3909. typename = indirect_result_t<Comp&,
  3910. projected<iterator_t<Range1>, Proj1>,
  3911. projected<iterator_t<Range2>, Proj2>>,
  3912. typename = indirect_result_t<Comp&,
  3913. projected<iterator_t<Range2>, Proj2>,
  3914. projected<iterator_t<Range1>, Proj1>>>
  3915. constexpr auto set_symmetric_difference(Range1&& range1,
  3916. Range2&& range2,
  3917. OutputIterator result,
  3918. Comp comp = {},
  3919. Proj1 proj1 = {},
  3920. Proj2 proj2 = {}) {
  3921. return ranges::set_symmetric_difference(
  3922. ranges::begin(range1), ranges::end(range1), ranges::begin(range2),
  3923. ranges::end(range2), result, std::move(comp), std::move(proj1),
  3924. std::move(proj2));
  3925. }
  3926. // [alg.heap.operations] Heap operations
  3927. // Reference: https://wg21.link/alg.heap.operations
  3928. // [push.heap] push_heap
  3929. // Reference: https://wg21.link/push.heap
  3930. // Preconditions: The range `[first, last - 1)` is a valid heap with respect to
  3931. // `comp` and `proj`.
  3932. //
  3933. // Effects: Places the value in the location `last - 1` into the resulting heap
  3934. // `[first, last)`.
  3935. //
  3936. // Returns: `last`.
  3937. //
  3938. // Complexity: At most `log(last - first)` comparisons and twice as many
  3939. // projections.
  3940. //
  3941. // Reference: https://wg21.link/push.heap#:~:text=ranges::push_heap(I
  3942. template <typename RandomAccessIterator,
  3943. typename Comp = ranges::less,
  3944. typename Proj = identity,
  3945. typename = internal::iterator_category_t<RandomAccessIterator>,
  3946. typename = indirect_result_t<Comp&,
  3947. projected<RandomAccessIterator, Proj>,
  3948. projected<RandomAccessIterator, Proj>>>
  3949. constexpr auto push_heap(RandomAccessIterator first,
  3950. RandomAccessIterator last,
  3951. Comp comp = {},
  3952. Proj proj = {}) {
  3953. std::push_heap(first, last,
  3954. internal::ProjectedBinaryPredicate(comp, proj, proj));
  3955. return last;
  3956. }
  3957. // Preconditions: The range `[begin(range), end(range) - 1)` is a valid heap
  3958. // with respect to `comp` and `proj`.
  3959. //
  3960. // Effects: Places the value in the location `end(range) - 1` into the resulting
  3961. // heap `range`.
  3962. //
  3963. // Returns: `end(range)`.
  3964. //
  3965. // Complexity: At most `log(size(range))` comparisons and twice as many
  3966. // projections.
  3967. //
  3968. // Reference: https://wg21.link/push.heap#:~:text=ranges::push_heap(R
  3969. template <typename Range,
  3970. typename Comp = ranges::less,
  3971. typename Proj = identity,
  3972. typename = internal::range_category_t<Range>,
  3973. typename = indirect_result_t<Comp&,
  3974. projected<iterator_t<Range>, Proj>,
  3975. projected<iterator_t<Range>, Proj>>>
  3976. constexpr auto push_heap(Range&& range, Comp comp = {}, Proj proj = {}) {
  3977. return ranges::push_heap(ranges::begin(range), ranges::end(range),
  3978. std::move(comp), std::move(proj));
  3979. }
  3980. // [pop.heap] pop_heap
  3981. // Reference: https://wg21.link/pop.heap
  3982. // Preconditions: The range `[first, last)` is a valid non-empty heap with
  3983. // respect to `comp` and `proj`.
  3984. //
  3985. // Effects: Swaps the value in the location `first` with the value in the
  3986. // location `last - 1` and makes `[first, last - 1)` into a heap with respect to
  3987. // `comp` and `proj`.
  3988. //
  3989. // Returns: `last`.
  3990. //
  3991. // Complexity: At most `2 log(last - first)` comparisons and twice as many
  3992. // projections.
  3993. //
  3994. // Reference: https://wg21.link/pop.heap#:~:text=ranges::pop_heap(I
  3995. template <typename RandomAccessIterator,
  3996. typename Comp = ranges::less,
  3997. typename Proj = identity,
  3998. typename = internal::iterator_category_t<RandomAccessIterator>,
  3999. typename = indirect_result_t<Comp&,
  4000. projected<RandomAccessIterator, Proj>,
  4001. projected<RandomAccessIterator, Proj>>>
  4002. constexpr auto pop_heap(RandomAccessIterator first,
  4003. RandomAccessIterator last,
  4004. Comp comp = {},
  4005. Proj proj = {}) {
  4006. std::pop_heap(first, last,
  4007. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4008. return last;
  4009. }
  4010. // Preconditions: `range` is a valid non-empty heap with respect to `comp` and
  4011. // `proj`.
  4012. //
  4013. // Effects: Swaps the value in the location `begin(range)` with the value in the
  4014. // location `end(range) - 1` and makes `[begin(range), end(range) - 1)` into a
  4015. // heap with respect to `comp` and `proj`.
  4016. //
  4017. // Returns: `end(range)`.
  4018. //
  4019. // Complexity: At most `2 log(size(range))` comparisons and twice as many
  4020. // projections.
  4021. //
  4022. // Reference: https://wg21.link/pop.heap#:~:text=ranges::pop_heap(R
  4023. template <typename Range,
  4024. typename Comp = ranges::less,
  4025. typename Proj = identity,
  4026. typename = internal::range_category_t<Range>,
  4027. typename = indirect_result_t<Comp&,
  4028. projected<iterator_t<Range>, Proj>,
  4029. projected<iterator_t<Range>, Proj>>>
  4030. constexpr auto pop_heap(Range&& range, Comp comp = {}, Proj proj = {}) {
  4031. return ranges::pop_heap(ranges::begin(range), ranges::end(range),
  4032. std::move(comp), std::move(proj));
  4033. }
  4034. // [make.heap] make_heap
  4035. // Reference: https://wg21.link/make.heap
  4036. // Effects: Constructs a heap with respect to `comp` and `proj` out of the range
  4037. // `[first, last)`.
  4038. //
  4039. // Returns: `last`.
  4040. //
  4041. // Complexity: At most `3 log(last - first)` comparisons and twice as many
  4042. // projections.
  4043. //
  4044. // Reference: https://wg21.link/make.heap#:~:text=ranges::make_heap(I
  4045. template <typename RandomAccessIterator,
  4046. typename Comp = ranges::less,
  4047. typename Proj = identity,
  4048. typename = internal::iterator_category_t<RandomAccessIterator>,
  4049. typename = indirect_result_t<Comp&,
  4050. projected<RandomAccessIterator, Proj>,
  4051. projected<RandomAccessIterator, Proj>>>
  4052. constexpr auto make_heap(RandomAccessIterator first,
  4053. RandomAccessIterator last,
  4054. Comp comp = {},
  4055. Proj proj = {}) {
  4056. std::make_heap(first, last,
  4057. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4058. return last;
  4059. }
  4060. // Effects: Constructs a heap with respect to `comp` and `proj` out of `range`.
  4061. //
  4062. // Returns: `end(range)`.
  4063. //
  4064. // Complexity: At most `3 log(size(range))` comparisons and twice as many
  4065. // projections.
  4066. //
  4067. // Reference: https://wg21.link/make.heap#:~:text=ranges::make_heap(R
  4068. template <typename Range,
  4069. typename Comp = ranges::less,
  4070. typename Proj = identity,
  4071. typename = internal::range_category_t<Range>,
  4072. typename = indirect_result_t<Comp&,
  4073. projected<iterator_t<Range>, Proj>,
  4074. projected<iterator_t<Range>, Proj>>>
  4075. constexpr auto make_heap(Range&& range, Comp comp = {}, Proj proj = {}) {
  4076. return ranges::make_heap(ranges::begin(range), ranges::end(range),
  4077. std::move(comp), std::move(proj));
  4078. }
  4079. // [sort.heap] sort_heap
  4080. // Reference: https://wg21.link/sort.heap
  4081. // Preconditions: The range `[first, last)` is a valid heap with respect to
  4082. // `comp` and `proj`.
  4083. //
  4084. // Effects: Sorts elements in the heap `[first, last)` with respect to `comp`
  4085. // and `proj`.
  4086. //
  4087. // Returns: `last`.
  4088. //
  4089. // Complexity: At most `2 N log N` comparisons, where `N = last - first`, and
  4090. // twice as many projections.
  4091. //
  4092. // Reference: https://wg21.link/sort.heap#:~:text=ranges::sort_heap(I
  4093. template <typename RandomAccessIterator,
  4094. typename Comp = ranges::less,
  4095. typename Proj = identity,
  4096. typename = internal::iterator_category_t<RandomAccessIterator>,
  4097. typename = indirect_result_t<Comp&,
  4098. projected<RandomAccessIterator, Proj>,
  4099. projected<RandomAccessIterator, Proj>>>
  4100. constexpr auto sort_heap(RandomAccessIterator first,
  4101. RandomAccessIterator last,
  4102. Comp comp = {},
  4103. Proj proj = {}) {
  4104. std::sort_heap(first, last,
  4105. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4106. return last;
  4107. }
  4108. // Preconditions: `range` is a valid heap with respect to `comp` and `proj`.
  4109. //
  4110. // Effects: Sorts elements in the heap `range` with respect to `comp` and
  4111. // `proj`.
  4112. //
  4113. // Returns: `end(range)`.
  4114. //
  4115. // Complexity: At most `2 N log N` comparisons, where `N = size(range)`, and
  4116. // twice as many projections.
  4117. //
  4118. // Reference: https://wg21.link/sort.heap#:~:text=ranges::sort_heap(R
  4119. template <typename Range,
  4120. typename Comp = ranges::less,
  4121. typename Proj = identity,
  4122. typename = internal::range_category_t<Range>,
  4123. typename = indirect_result_t<Comp&,
  4124. projected<iterator_t<Range>, Proj>,
  4125. projected<iterator_t<Range>, Proj>>>
  4126. constexpr auto sort_heap(Range&& range, Comp comp = {}, Proj proj = {}) {
  4127. return ranges::sort_heap(ranges::begin(range), ranges::end(range),
  4128. std::move(comp), std::move(proj));
  4129. }
  4130. // [is.heap] is_heap
  4131. // Reference: https://wg21.link/is.heap
  4132. // Returns: Whether the range `[first, last)` is a heap with respect to `comp`
  4133. // and `proj`.
  4134. //
  4135. // Complexity: Linear.
  4136. //
  4137. // Reference: https://wg21.link/is.heap#:~:text=ranges::is_heap(I
  4138. template <typename RandomAccessIterator,
  4139. typename Comp = ranges::less,
  4140. typename Proj = identity,
  4141. typename = internal::iterator_category_t<RandomAccessIterator>,
  4142. typename = indirect_result_t<Comp&,
  4143. projected<RandomAccessIterator, Proj>,
  4144. projected<RandomAccessIterator, Proj>>>
  4145. constexpr auto is_heap(RandomAccessIterator first,
  4146. RandomAccessIterator last,
  4147. Comp comp = {},
  4148. Proj proj = {}) {
  4149. return std::is_heap(first, last,
  4150. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4151. }
  4152. // Returns: Whether `range` is a heap with respect to `comp` and `proj`.
  4153. //
  4154. // Complexity: Linear.
  4155. //
  4156. // Reference: https://wg21.link/is.heap#:~:text=ranges::is_heap(R
  4157. template <typename Range,
  4158. typename Comp = ranges::less,
  4159. typename Proj = identity,
  4160. typename = internal::range_category_t<Range>,
  4161. typename = indirect_result_t<Comp&,
  4162. projected<iterator_t<Range>, Proj>,
  4163. projected<iterator_t<Range>, Proj>>>
  4164. constexpr auto is_heap(Range&& range, Comp comp = {}, Proj proj = {}) {
  4165. return ranges::is_heap(ranges::begin(range), ranges::end(range),
  4166. std::move(comp), std::move(proj));
  4167. }
  4168. // Returns: The last iterator `i` in `[first, last]` for which the range
  4169. // `[first, i)` is a heap with respect to `comp` and `proj`.
  4170. //
  4171. // Complexity: Linear.
  4172. //
  4173. // Reference: https://wg21.link/is.heap#:~:text=ranges::is_heap_until(I
  4174. template <typename RandomAccessIterator,
  4175. typename Comp = ranges::less,
  4176. typename Proj = identity,
  4177. typename = internal::iterator_category_t<RandomAccessIterator>,
  4178. typename = indirect_result_t<Comp&,
  4179. projected<RandomAccessIterator, Proj>,
  4180. projected<RandomAccessIterator, Proj>>>
  4181. constexpr auto is_heap_until(RandomAccessIterator first,
  4182. RandomAccessIterator last,
  4183. Comp comp = {},
  4184. Proj proj = {}) {
  4185. return std::is_heap_until(
  4186. first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
  4187. }
  4188. // Returns: The last iterator `i` in `[begin(range), end(range)]` for which the
  4189. // range `[begin(range), i)` is a heap with respect to `comp` and `proj`.
  4190. //
  4191. // Complexity: Linear.
  4192. //
  4193. // Reference: https://wg21.link/is.heap#:~:text=ranges::is_heap_until(R
  4194. template <typename Range,
  4195. typename Comp = ranges::less,
  4196. typename Proj = identity,
  4197. typename = internal::range_category_t<Range>,
  4198. typename = indirect_result_t<Comp&,
  4199. projected<iterator_t<Range>, Proj>,
  4200. projected<iterator_t<Range>, Proj>>>
  4201. constexpr auto is_heap_until(Range&& range, Comp comp = {}, Proj proj = {}) {
  4202. return ranges::is_heap_until(ranges::begin(range), ranges::end(range),
  4203. std::move(comp), std::move(proj));
  4204. }
  4205. // [alg.min.max] Minimum and maximum
  4206. // Reference: https://wg21.link/alg.min.max
  4207. // Returns: The smaller value. Returns the first argument when the arguments are
  4208. // equivalent.
  4209. //
  4210. // Complexity: Exactly one comparison and two applications of the projection, if
  4211. // any.
  4212. //
  4213. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::min
  4214. template <typename T, typename Comp = ranges::less, typename Proj = identity>
  4215. constexpr const T& min(const T& a, const T& b, Comp comp = {}, Proj proj = {}) {
  4216. return invoke(comp, invoke(proj, b), invoke(proj, a)) ? b : a;
  4217. }
  4218. // Preconditions: `!empty(ilist)`.
  4219. //
  4220. // Returns: The smallest value in the input range. Returns a copy of the
  4221. // leftmost element when several elements are equivalent to the smallest.
  4222. //
  4223. // Complexity: Exactly `size(ilist) - 1` comparisons and twice as many
  4224. // applications of the projection, if any.
  4225. //
  4226. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::min(initializer_list
  4227. template <typename T, typename Comp = ranges::less, typename Proj = identity>
  4228. constexpr T min(std::initializer_list<T> ilist,
  4229. Comp comp = {},
  4230. Proj proj = {}) {
  4231. return *std::min_element(
  4232. ilist.begin(), ilist.end(),
  4233. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4234. }
  4235. // Preconditions: `!empty(range)`.
  4236. //
  4237. // Returns: The smallest value in the input range. Returns a copy of the
  4238. // leftmost element when several elements are equivalent to the smallest.
  4239. //
  4240. // Complexity: Exactly `size(range) - 1` comparisons and twice as many
  4241. // applications of the projection, if any.
  4242. //
  4243. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::min(R
  4244. template <typename Range,
  4245. typename Comp = ranges::less,
  4246. typename Proj = identity,
  4247. typename = internal::range_category_t<Range>>
  4248. constexpr auto min(Range&& range, Comp comp = {}, Proj proj = {}) {
  4249. return *std::min_element(
  4250. ranges::begin(range), ranges::end(range),
  4251. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4252. }
  4253. // Returns: The larger value. Returns the first argument when the arguments are
  4254. // equivalent.
  4255. //
  4256. // Complexity: Exactly one comparison and two applications of the projection, if
  4257. // any.
  4258. //
  4259. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::max
  4260. template <typename T, typename Comp = ranges::less, typename Proj = identity>
  4261. constexpr const T& max(const T& a, const T& b, Comp comp = {}, Proj proj = {}) {
  4262. return invoke(comp, invoke(proj, a), invoke(proj, b)) ? b : a;
  4263. }
  4264. // Preconditions: `!empty(ilist)`.
  4265. //
  4266. // Returns: The largest value in the input range. Returns a copy of the leftmost
  4267. // element when several elements are equivalent to the largest.
  4268. //
  4269. // Complexity: Exactly `size(ilist) - 1` comparisons and twice as many
  4270. // applications of the projection, if any.
  4271. //
  4272. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::max(initializer_list
  4273. template <typename T, typename Comp = ranges::less, typename Proj = identity>
  4274. constexpr T max(std::initializer_list<T> ilist,
  4275. Comp comp = {},
  4276. Proj proj = {}) {
  4277. return *std::max_element(
  4278. ilist.begin(), ilist.end(),
  4279. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4280. }
  4281. // Preconditions: `!empty(range)`.
  4282. //
  4283. // Returns: The largest value in the input range. Returns a copy of the leftmost
  4284. // element when several elements are equivalent to the smallest.
  4285. //
  4286. // Complexity: Exactly `size(range) - 1` comparisons and twice as many
  4287. // applications of the projection, if any.
  4288. //
  4289. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::max(R
  4290. template <typename Range,
  4291. typename Comp = ranges::less,
  4292. typename Proj = identity,
  4293. typename = internal::range_category_t<Range>>
  4294. constexpr auto max(Range&& range, Comp comp = {}, Proj proj = {}) {
  4295. return *std::max_element(
  4296. ranges::begin(range), ranges::end(range),
  4297. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4298. }
  4299. // Returns: `{b, a}` if `b` is smaller than `a`, and `{a, b}` otherwise.
  4300. //
  4301. // Complexity: Exactly one comparison and two applications of the projection, if
  4302. // any.
  4303. //
  4304. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::minmax
  4305. template <typename T, typename Comp = ranges::less, typename Proj = identity>
  4306. constexpr auto minmax(const T& a, const T& b, Comp comp = {}, Proj proj = {}) {
  4307. return std::minmax(a, b,
  4308. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4309. }
  4310. // Preconditions: `!empty(ilist)`.
  4311. //
  4312. // Returns: Let `X` be the return type. Returns `X{x, y}`, where `x` is a copy
  4313. // of the leftmost element with the smallest value and `y` a copy of the
  4314. // rightmost element with the largest value in the input range.
  4315. //
  4316. // Complexity: At most `(3/2) size(ilist)` applications of the corresponding
  4317. // predicate and twice as many applications of the projection, if any.
  4318. //
  4319. // Reference:
  4320. // https://wg21.link/alg.min.max#:~:text=ranges::minmax(initializer_list
  4321. template <typename T, typename Comp = ranges::less, typename Proj = identity>
  4322. constexpr auto minmax(std::initializer_list<T> ilist,
  4323. Comp comp = {},
  4324. Proj proj = {}) {
  4325. auto it =
  4326. std::minmax_element(ranges::begin(ilist), ranges::end(ilist),
  4327. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4328. return std::pair<T, T>{*it.first, *it.second};
  4329. }
  4330. // Preconditions: `!empty(range)`.
  4331. //
  4332. // Returns: Let `X` be the return type. Returns `X{x, y}`, where `x` is a copy
  4333. // of the leftmost element with the smallest value and `y` a copy of the
  4334. // rightmost element with the largest value in the input range.
  4335. //
  4336. // Complexity: At most `(3/2) size(range)` applications of the corresponding
  4337. // predicate and twice as many applications of the projection, if any.
  4338. //
  4339. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::minmax(R
  4340. template <typename Range,
  4341. typename Comp = ranges::less,
  4342. typename Proj = identity,
  4343. typename = internal::range_category_t<Range>>
  4344. constexpr auto minmax(Range&& range, Comp comp = {}, Proj proj = {}) {
  4345. using T = range_value_t<Range>;
  4346. auto it =
  4347. std::minmax_element(ranges::begin(range), ranges::end(range),
  4348. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4349. return std::pair<T, T>{*it.first, *it.second};
  4350. }
  4351. // Returns: The first iterator i in the range `[first, last)` such that for
  4352. // every iterator `j` in the range `[first, last)`,
  4353. // `bool(invoke(comp, invoke(proj, *j), invoke(proj, *i)))` is `false`. Returns
  4354. // `last` if `first == last`.
  4355. //
  4356. // Complexity: Exactly `max(last - first - 1, 0)` comparisons and twice as
  4357. // many projections.
  4358. //
  4359. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::min_element(I
  4360. template <typename ForwardIterator,
  4361. typename Comp = ranges::less,
  4362. typename Proj = identity,
  4363. typename = internal::iterator_category_t<ForwardIterator>,
  4364. typename = indirect_result_t<Comp&,
  4365. projected<ForwardIterator, Proj>,
  4366. projected<ForwardIterator, Proj>>>
  4367. constexpr auto min_element(ForwardIterator first,
  4368. ForwardIterator last,
  4369. Comp comp = {},
  4370. Proj proj = {}) {
  4371. return std::min_element(first, last,
  4372. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4373. }
  4374. // Returns: The first iterator i in `range` such that for every iterator `j` in
  4375. // `range`, `bool(invoke(comp, invoke(proj, *j), invoke(proj, *i)))` is `false`.
  4376. // Returns `end(range)` if `empty(range)`.
  4377. //
  4378. // Complexity: Exactly `max(size(range) - 1, 0)` comparisons and twice as many
  4379. // projections.
  4380. //
  4381. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::min_element(R
  4382. template <typename Range,
  4383. typename Comp = ranges::less,
  4384. typename Proj = identity,
  4385. typename = internal::range_category_t<Range>,
  4386. typename = indirect_result_t<Comp&,
  4387. projected<iterator_t<Range>, Proj>,
  4388. projected<iterator_t<Range>, Proj>>>
  4389. constexpr auto min_element(Range&& range, Comp comp = {}, Proj proj = {}) {
  4390. return ranges::min_element(ranges::begin(range), ranges::end(range),
  4391. std::move(comp), std::move(proj));
  4392. }
  4393. // Returns: The first iterator i in the range `[first, last)` such that for
  4394. // every iterator `j` in the range `[first, last)`,
  4395. // `bool(invoke(comp, invoke(proj, *i), invoke(proj, *j)))` is `false`.
  4396. // Returns `last` if `first == last`.
  4397. //
  4398. // Complexity: Exactly `max(last - first - 1, 0)` comparisons and twice as
  4399. // many projections.
  4400. //
  4401. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::max_element(I
  4402. template <typename ForwardIterator,
  4403. typename Comp = ranges::less,
  4404. typename Proj = identity,
  4405. typename = internal::iterator_category_t<ForwardIterator>,
  4406. typename = indirect_result_t<Comp&,
  4407. projected<ForwardIterator, Proj>,
  4408. projected<ForwardIterator, Proj>>>
  4409. constexpr auto max_element(ForwardIterator first,
  4410. ForwardIterator last,
  4411. Comp comp = {},
  4412. Proj proj = {}) {
  4413. return std::max_element(first, last,
  4414. internal::ProjectedBinaryPredicate(comp, proj, proj));
  4415. }
  4416. // Returns: The first iterator i in `range` such that for every iterator `j`
  4417. // in `range`, `bool(invoke(comp, invoke(proj, *j), invoke(proj, *j)))` is
  4418. // `false`. Returns `end(range)` if `empty(range)`.
  4419. //
  4420. // Complexity: Exactly `max(size(range) - 1, 0)` comparisons and twice as many
  4421. // projections.
  4422. //
  4423. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::max_element(R
  4424. template <typename Range,
  4425. typename Comp = ranges::less,
  4426. typename Proj = identity,
  4427. typename = internal::range_category_t<Range>,
  4428. typename = indirect_result_t<Comp&,
  4429. projected<iterator_t<Range>, Proj>,
  4430. projected<iterator_t<Range>, Proj>>>
  4431. constexpr auto max_element(Range&& range, Comp comp = {}, Proj proj = {}) {
  4432. return ranges::max_element(ranges::begin(range), ranges::end(range),
  4433. std::move(comp), std::move(proj));
  4434. }
  4435. // Returns: `{first, first}` if `[first, last)` is empty, otherwise `{m, M}`,
  4436. // where `m` is the first iterator in `[first, last)` such that no iterator in
  4437. // the range refers to a smaller element, and where `M` is the last iterator
  4438. // in
  4439. // `[first, last)` such that no iterator in the range refers to a larger
  4440. // element.
  4441. //
  4442. // Complexity: Let `N` be `last - first`. At most `max(3/2 (N − 1), 0)`
  4443. // comparisons and twice as many applications of the projection, if any.
  4444. //
  4445. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::minmax_element(I
  4446. template <typename ForwardIterator,
  4447. typename Comp = ranges::less,
  4448. typename Proj = identity,
  4449. typename = internal::iterator_category_t<ForwardIterator>,
  4450. typename = indirect_result_t<Comp&,
  4451. projected<ForwardIterator, Proj>,
  4452. projected<ForwardIterator, Proj>>>
  4453. constexpr auto minmax_element(ForwardIterator first,
  4454. ForwardIterator last,
  4455. Comp comp = {},
  4456. Proj proj = {}) {
  4457. return std::minmax_element(
  4458. first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
  4459. }
  4460. // Returns: `{begin(range), begin(range)}` if `range` is empty, otherwise
  4461. // `{m, M}`, where `m` is the first iterator in `range` such that no iterator
  4462. // in the range refers to a smaller element, and where `M` is the last
  4463. // iterator in `range` such that no iterator in the range refers to a larger
  4464. // element.
  4465. //
  4466. // Complexity: Let `N` be `size(range)`. At most `max(3/2 (N − 1), 0)`
  4467. // comparisons and twice as many applications of the projection, if any.
  4468. //
  4469. // Reference: https://wg21.link/alg.min.max#:~:text=ranges::minmax_element(R
  4470. template <typename Range,
  4471. typename Comp = ranges::less,
  4472. typename Proj = identity,
  4473. typename = internal::range_category_t<Range>,
  4474. typename = indirect_result_t<Comp&,
  4475. projected<iterator_t<Range>, Proj>,
  4476. projected<iterator_t<Range>, Proj>>>
  4477. constexpr auto minmax_element(Range&& range, Comp comp = {}, Proj proj = {}) {
  4478. return ranges::minmax_element(ranges::begin(range), ranges::end(range),
  4479. std::move(comp), std::move(proj));
  4480. }
  4481. // [alg.clamp] Bounded value
  4482. // Reference: https://wg21.link/alg.clamp
  4483. // Preconditions: `bool(invoke(comp, invoke(proj, hi), invoke(proj, lo)))` is
  4484. // `false`.
  4485. //
  4486. // Returns: `lo` if `bool(invoke(comp, invoke(proj, v), invoke(proj, lo)))` is
  4487. // `true`, `hi` if `bool(invoke(comp, invoke(proj, hi), invoke(proj, v)))` is
  4488. // `true`, otherwise `v`.
  4489. //
  4490. // Complexity: At most two comparisons and three applications of the
  4491. // projection.
  4492. //
  4493. // Reference: https://wg21.link/alg.clamp#:~:text=ranges::clamp
  4494. template <typename T, typename Comp = ranges::less, typename Proj = identity>
  4495. constexpr const T& clamp(const T& v,
  4496. const T& lo,
  4497. const T& hi,
  4498. Comp comp = {},
  4499. Proj proj = {}) {
  4500. auto&& projected_v = invoke(proj, v);
  4501. if (invoke(comp, projected_v, invoke(proj, lo)))
  4502. return lo;
  4503. return invoke(comp, invoke(proj, hi), projected_v) ? hi : v;
  4504. }
  4505. // [alg.lex.comparison] Lexicographical comparison
  4506. // Reference: https://wg21.link/alg.lex.comparison
  4507. // Returns: `true` if and only if the sequence of elements defined by the range
  4508. // `[first1, last1)` is lexicographically less than the sequence of elements
  4509. // defined by the range `[first2, last2)`.
  4510. //
  4511. // Complexity: At most `2 min(last1 - first1, last2 - first2)` applications of
  4512. // the corresponding comparison and each projection, if any.
  4513. //
  4514. // Remarks: If two sequences have the same number of elements and their
  4515. // corresponding elements (if any) are equivalent, then neither sequence is
  4516. // lexicographically less than the other. If one sequence is a proper prefix of
  4517. // the other, then the shorter sequence is lexicographically less than the
  4518. // longer sequence. Otherwise, the lexicographical comparison of the sequences
  4519. // yields the same result as the comparison of the first corresponding pair of
  4520. // elements that are not equivalent.
  4521. //
  4522. // Reference:
  4523. // https://wg21.link/alg.lex.comparison#:~:text=lexicographical_compare(I1
  4524. template <typename ForwardIterator1,
  4525. typename ForwardIterator2,
  4526. typename Comp = ranges::less,
  4527. typename Proj1 = identity,
  4528. typename Proj2 = identity,
  4529. typename = internal::iterator_category_t<ForwardIterator1>,
  4530. typename = internal::iterator_category_t<ForwardIterator2>,
  4531. typename = indirect_result_t<Comp&,
  4532. projected<ForwardIterator1, Proj1>,
  4533. projected<ForwardIterator2, Proj2>>,
  4534. typename = indirect_result_t<Comp&,
  4535. projected<ForwardIterator2, Proj2>,
  4536. projected<ForwardIterator1, Proj1>>>
  4537. constexpr bool lexicographical_compare(ForwardIterator1 first1,
  4538. ForwardIterator1 last1,
  4539. ForwardIterator2 first2,
  4540. ForwardIterator2 last2,
  4541. Comp comp = {},
  4542. Proj1 proj1 = {},
  4543. Proj2 proj2 = {}) {
  4544. for (; first1 != last1 && first2 != last2; ++first1, ++first2) {
  4545. auto&& projected_first1 = invoke(proj1, *first1);
  4546. auto&& projected_first2 = invoke(proj2, *first2);
  4547. if (invoke(comp, projected_first1, projected_first2))
  4548. return true;
  4549. if (invoke(comp, projected_first2, projected_first1))
  4550. return false;
  4551. }
  4552. // `first2 != last2` is equivalent to `first1 == last1 && first2 != last2`
  4553. // here, since we broke out of the loop above.
  4554. return first2 != last2;
  4555. }
  4556. // Returns: `true` if and only if the sequence of elements defined by `range1`
  4557. // is lexicographically less than the sequence of elements defined by `range2`.
  4558. //
  4559. // Complexity: At most `2 min(size(range1), size(range2))` applications of the
  4560. // corresponding comparison and each projection, if any.
  4561. //
  4562. // Remarks: If two sequences have the same number of elements and their
  4563. // corresponding elements (if any) are equivalent, then neither sequence is
  4564. // lexicographically less than the other. If one sequence is a proper prefix of
  4565. // the other, then the shorter sequence is lexicographically less than the
  4566. // longer sequence. Otherwise, the lexicographical comparison of the sequences
  4567. // yields the same result as the comparison of the first corresponding pair of
  4568. // elements that are not equivalent.
  4569. //
  4570. // Reference:
  4571. // https://wg21.link/alg.lex.comparison#:~:text=lexicographical_compare(R1
  4572. template <typename Range1,
  4573. typename Range2,
  4574. typename Comp = ranges::less,
  4575. typename Proj1 = identity,
  4576. typename Proj2 = identity,
  4577. typename = internal::range_category_t<Range1>,
  4578. typename = internal::range_category_t<Range2>,
  4579. typename = indirect_result_t<Comp&,
  4580. projected<iterator_t<Range1>, Proj1>,
  4581. projected<iterator_t<Range2>, Proj2>>,
  4582. typename = indirect_result_t<Comp&,
  4583. projected<iterator_t<Range2>, Proj2>,
  4584. projected<iterator_t<Range1>, Proj1>>>
  4585. constexpr bool lexicographical_compare(Range1&& range1,
  4586. Range2&& range2,
  4587. Comp comp = {},
  4588. Proj1 proj1 = {},
  4589. Proj2 proj2 = {}) {
  4590. return ranges::lexicographical_compare(
  4591. ranges::begin(range1), ranges::end(range1), ranges::begin(range2),
  4592. ranges::end(range2), std::move(comp), std::move(proj1), std::move(proj2));
  4593. }
  4594. // [alg.permutation.generators] Permutation generators
  4595. // Reference: https://wg21.link/alg.permutation.generators
  4596. // Effects: Takes a sequence defined by the range `[first, last)` and transforms
  4597. // it into the next permutation. The next permutation is found by assuming that
  4598. // the set of all permutations is lexicographically sorted with respect to
  4599. // `comp` and `proj`. If no such permutation exists, transforms the sequence
  4600. // into the first permutation; that is, the ascendingly-sorted one.
  4601. //
  4602. // Returns: `true` if a next permutation was found and otherwise `false`.
  4603. //
  4604. // Complexity: At most `(last - first) / 2` swaps.
  4605. //
  4606. // Reference:
  4607. // https://wg21.link/alg.permutation.generators#:~:text=next_permutation(I
  4608. template <typename BidirectionalIterator,
  4609. typename Comp = ranges::less,
  4610. typename Proj = identity,
  4611. typename = internal::iterator_category_t<BidirectionalIterator>,
  4612. typename = indirect_result_t<Comp&,
  4613. projected<BidirectionalIterator, Proj>,
  4614. projected<BidirectionalIterator, Proj>>>
  4615. constexpr auto next_permutation(BidirectionalIterator first,
  4616. BidirectionalIterator last,
  4617. Comp comp = {},
  4618. Proj proj = {}) {
  4619. return std::next_permutation(
  4620. first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
  4621. }
  4622. // Effects: Takes a sequence defined by `range` and transforms it into the next
  4623. // permutation. The next permutation is found by assuming that the set of all
  4624. // permutations is lexicographically sorted with respect to `comp` and `proj`.
  4625. // If no such permutation exists, transforms the sequence into the first
  4626. // permutation; that is, the ascendingly-sorted one.
  4627. //
  4628. // Returns: `true` if a next permutation was found and otherwise `false`.
  4629. //
  4630. // Complexity: At most `size(range) / 2` swaps.
  4631. //
  4632. // Reference:
  4633. // https://wg21.link/alg.permutation.generators#:~:text=next_permutation(R
  4634. template <typename Range,
  4635. typename Comp = ranges::less,
  4636. typename Proj = identity,
  4637. typename = internal::range_category_t<Range>,
  4638. typename = indirect_result_t<Comp&,
  4639. projected<iterator_t<Range>, Proj>,
  4640. projected<iterator_t<Range>, Proj>>>
  4641. constexpr auto next_permutation(Range&& range, Comp comp = {}, Proj proj = {}) {
  4642. return ranges::next_permutation(ranges::begin(range), ranges::end(range),
  4643. std::move(comp), std::move(proj));
  4644. }
  4645. // Effects: Takes a sequence defined by the range `[first, last)` and transforms
  4646. // it into the previous permutation. The previous permutation is found by
  4647. // assuming that the set of all permutations is lexicographically sorted with
  4648. // respect to `comp` and `proj`. If no such permutation exists, transforms the
  4649. // sequence into the last permutation; that is, the decreasingly-sorted one.
  4650. //
  4651. // Returns: `true` if a next permutation was found and otherwise `false`.
  4652. //
  4653. // Complexity: At most `(last - first) / 2` swaps.
  4654. //
  4655. // Reference:
  4656. // https://wg21.link/alg.permutation.generators#:~:text=prev_permutation(I
  4657. template <typename BidirectionalIterator,
  4658. typename Comp = ranges::less,
  4659. typename Proj = identity,
  4660. typename = internal::iterator_category_t<BidirectionalIterator>,
  4661. typename = indirect_result_t<Comp&,
  4662. projected<BidirectionalIterator, Proj>,
  4663. projected<BidirectionalIterator, Proj>>>
  4664. constexpr auto prev_permutation(BidirectionalIterator first,
  4665. BidirectionalIterator last,
  4666. Comp comp = {},
  4667. Proj proj = {}) {
  4668. return std::prev_permutation(
  4669. first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
  4670. }
  4671. // Effects: Takes a sequence defined by `range` and transforms it into the
  4672. // previous permutation. The previous permutation is found by assuming that the
  4673. // set of all permutations is lexicographically sorted with respect to `comp`
  4674. // and `proj`. If no such permutation exists, transforms the sequence into the
  4675. // last permutation; that is, the decreasingly-sorted one.
  4676. //
  4677. // Returns: `true` if a previous permutation was found and otherwise `false`.
  4678. //
  4679. // Complexity: At most `size(range) / 2` swaps.
  4680. //
  4681. // Reference:
  4682. // https://wg21.link/alg.permutation.generators#:~:text=prev_permutation(R
  4683. template <typename Range,
  4684. typename Comp = ranges::less,
  4685. typename Proj = identity,
  4686. typename = internal::range_category_t<Range>,
  4687. typename = indirect_result_t<Comp&,
  4688. projected<iterator_t<Range>, Proj>,
  4689. projected<iterator_t<Range>, Proj>>>
  4690. constexpr auto prev_permutation(Range&& range, Comp comp = {}, Proj proj = {}) {
  4691. return ranges::prev_permutation(ranges::begin(range), ranges::end(range),
  4692. std::move(comp), std::move(proj));
  4693. }
  4694. } // namespace ranges
  4695. } // namespace base
  4696. #endif // BASE_RANGES_ALGORITHM_H_