123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959 |
- // Copyright 2020 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #ifndef BASE_RANGES_ALGORITHM_H_
- #define BASE_RANGES_ALGORITHM_H_
- #include <algorithm>
- #include <initializer_list>
- #include <iterator>
- #include <type_traits>
- #include <utility>
- #include "base/ranges/functional.h"
- #include "base/ranges/ranges.h"
- #include "base/stl_util.h"
- #include "base/template_util.h"
- namespace base {
- namespace internal {
- // Returns a transformed version of the unary predicate `pred` applying `proj`
- // to its argument before invoking `pred` on it.
- // Ensures that the return type of `invoke(pred, ...)` is convertible to bool.
- template <typename Pred, typename Proj>
- constexpr auto ProjectedUnaryPredicate(Pred& pred, Proj& proj) noexcept {
- return [&pred, &proj](auto&& arg) -> bool {
- return base::invoke(pred,
- base::invoke(proj, std::forward<decltype(arg)>(arg)));
- };
- }
- // Returns a transformed version of the binary predicate `pred` applying `proj1`
- // and `proj2` to its arguments before invoking `pred` on them.
- //
- // Provides an opt-in to considers all four permutations of projections and
- // argument types. This is sometimes necessary to allow usage with legacy
- // non-ranges std:: algorithms that don't support projections.
- //
- // These permutations are assigned different priorities to break ambiguities in
- // case several permutations are possible, e.g. when Proj1 and Proj2 are the
- // same type.
- //
- // Note that even when opting in to using all permutations of projections,
- // calling code should still ensure that the canonical mapping of {Proj1, Proj2}
- // to {LHS, RHS} compiles for all members of the range. This can be done by
- // adding the following constraint:
- //
- // typename = indirect_result_t<Pred&,
- // projected<iterator_t<Range1>, Proj1>,
- // projected<iterator_t<Range2>, Proj2>>
- //
- // Ensures that the return type of `invoke(pred, ...)` is convertible to bool.
- template <typename Pred, typename Proj1, typename Proj2, bool kPermute = false>
- class BinaryPredicateProjector {
- public:
- constexpr BinaryPredicateProjector(Pred& pred, Proj1& proj1, Proj2& proj2)
- : pred_(pred), proj1_(proj1), proj2_(proj2) {}
- private:
- template <typename ProjT, typename ProjU, typename T, typename U>
- using InvokeResult = invoke_result_t<Pred&,
- invoke_result_t<ProjT&, T&&>,
- invoke_result_t<ProjU&, U&&>>;
- template <typename T, typename U, typename = InvokeResult<Proj1, Proj2, T, U>>
- constexpr std::pair<Proj1&, Proj2&> GetProjs(priority_tag<3>) const {
- return {proj1_, proj2_};
- }
- template <typename T,
- typename U,
- bool LazyPermute = kPermute,
- typename = std::enable_if_t<LazyPermute>,
- typename = InvokeResult<Proj2, Proj1, T, U>>
- constexpr std::pair<Proj2&, Proj1&> GetProjs(priority_tag<2>) const {
- return {proj2_, proj1_};
- }
- template <typename T,
- typename U,
- bool LazyPermute = kPermute,
- typename = std::enable_if_t<LazyPermute>,
- typename = InvokeResult<Proj1, Proj1, T, U>>
- constexpr std::pair<Proj1&, Proj1&> GetProjs(priority_tag<1>) const {
- return {proj1_, proj1_};
- }
- template <typename T,
- typename U,
- bool LazyPermute = kPermute,
- typename = std::enable_if_t<LazyPermute>,
- typename = InvokeResult<Proj2, Proj2, T, U>>
- constexpr std::pair<Proj2&, Proj2&> GetProjs(priority_tag<0>) const {
- return {proj2_, proj2_};
- }
- public:
- template <typename T, typename U>
- constexpr bool operator()(T&& lhs, U&& rhs) const {
- auto projs = GetProjs<T, U>(priority_tag<3>());
- return base::invoke(pred_, base::invoke(projs.first, std::forward<T>(lhs)),
- base::invoke(projs.second, std::forward<U>(rhs)));
- }
- private:
- Pred& pred_;
- Proj1& proj1_;
- Proj2& proj2_;
- };
- // Small wrappers around BinaryPredicateProjector to make the calling side more
- // readable.
- template <typename Pred, typename Proj1, typename Proj2>
- constexpr auto ProjectedBinaryPredicate(Pred& pred,
- Proj1& proj1,
- Proj2& proj2) noexcept {
- return BinaryPredicateProjector<Pred, Proj1, Proj2>(pred, proj1, proj2);
- }
- template <typename Pred, typename Proj1, typename Proj2>
- constexpr auto PermutedProjectedBinaryPredicate(Pred& pred,
- Proj1& proj1,
- Proj2& proj2) noexcept {
- return BinaryPredicateProjector<Pred, Proj1, Proj2, true>(pred, proj1, proj2);
- }
- // This alias is used below to restrict iterator based APIs to types for which
- // `iterator_category` and the pre-increment and post-increment operators are
- // defined. This is required in situations where otherwise an undesired overload
- // would be chosen, e.g. copy_if. In spirit this is similar to C++20's
- // std::input_or_output_iterator, a concept that each iterator should satisfy.
- template <typename Iter,
- typename = decltype(++std::declval<Iter&>()),
- typename = decltype(std::declval<Iter&>()++)>
- using iterator_category_t =
- typename std::iterator_traits<Iter>::iterator_category;
- // This alias is used below to restrict range based APIs to types for which
- // `iterator_category_t` is defined for the underlying iterator. This is
- // required in situations where otherwise an undesired overload would be chosen,
- // e.g. transform. In spirit this is similar to C++20's std::ranges::range, a
- // concept that each range should satisfy.
- template <typename Range>
- using range_category_t = iterator_category_t<ranges::iterator_t<Range>>;
- } // namespace internal
- namespace ranges {
- // C++14 implementation of std::ranges::in_fun_result. Note the because C++14
- // lacks the `no_unique_address` attribute it is commented out.
- //
- // Reference: https://wg21.link/algorithms.results#:~:text=in_fun_result
- template <typename I, typename F>
- struct in_fun_result {
- /* [[no_unique_address]] */ I in;
- /* [[no_unique_address]] */ F fun;
- template <typename I2,
- typename F2,
- std::enable_if_t<std::is_convertible<const I&, I2>{} &&
- std::is_convertible<const F&, F2>{}>>
- constexpr operator in_fun_result<I2, F2>() const& {
- return {in, fun};
- }
- template <typename I2,
- typename F2,
- std::enable_if_t<std::is_convertible<I, I2>{} &&
- std::is_convertible<F, F2>{}>>
- constexpr operator in_fun_result<I2, F2>() && {
- return {std::move(in), std::move(fun)};
- }
- };
- // TODO(crbug.com/1071094): Implement the other result types.
- // [alg.nonmodifying] Non-modifying sequence operations
- // Reference: https://wg21.link/alg.nonmodifying
- // [alg.all.of] All of
- // Reference: https://wg21.link/alg.all.of
- // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
- //
- // Returns: `false` if `E(i)` is `false` for some iterator `i` in the range
- // `[first, last)`, and `true` otherwise.
- //
- // Complexity: At most `last - first` applications of the predicate and any
- // projection.
- //
- // Reference: https://wg21.link/alg.all.of#:~:text=ranges::all_of(I
- template <typename InputIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>>
- constexpr bool all_of(InputIterator first,
- InputIterator last,
- Pred pred,
- Proj proj = {}) {
- for (; first != last; ++first) {
- if (!invoke(pred, invoke(proj, *first)))
- return false;
- }
- return true;
- }
- // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
- //
- // Returns: `false` if `E(i)` is `false` for some iterator `i` in `range`, and
- // `true` otherwise.
- //
- // Complexity: At most `size(range)` applications of the predicate and any
- // projection.
- //
- // Reference: https://wg21.link/alg.all.of#:~:text=ranges::all_of(R
- template <typename Range,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr bool all_of(Range&& range, Pred pred, Proj proj = {}) {
- return ranges::all_of(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // [alg.any.of] Any of
- // Reference: https://wg21.link/alg.any.of
- // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
- //
- // Returns: `true` if `E(i)` is `true` for some iterator `i` in the range
- // `[first, last)`, and `false` otherwise.
- //
- // Complexity: At most `last - first` applications of the predicate and any
- // projection.
- //
- // Reference: https://wg21.link/alg.any.of#:~:text=ranges::any_of(I
- template <typename InputIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>>
- constexpr bool any_of(InputIterator first,
- InputIterator last,
- Pred pred,
- Proj proj = {}) {
- for (; first != last; ++first) {
- if (invoke(pred, invoke(proj, *first)))
- return true;
- }
- return false;
- }
- // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
- //
- // Returns: `true` if `E(i)` is `true` for some iterator `i` in `range`, and
- // `false` otherwise.
- //
- // Complexity: At most `size(range)` applications of the predicate and any
- // projection.
- //
- // Reference: https://wg21.link/alg.any.of#:~:text=ranges::any_of(R
- template <typename Range,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr bool any_of(Range&& range, Pred pred, Proj proj = {}) {
- return ranges::any_of(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // [alg.none.of] None of
- // Reference: https://wg21.link/alg.none.of
- // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
- //
- // Returns: `false` if `E(i)` is `true` for some iterator `i` in the range
- // `[first, last)`, and `true` otherwise.
- //
- // Complexity: At most `last - first` applications of the predicate and any
- // projection.
- //
- // Reference: https://wg21.link/alg.none.of#:~:text=ranges::none_of(I
- template <typename InputIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>>
- constexpr bool none_of(InputIterator first,
- InputIterator last,
- Pred pred,
- Proj proj = {}) {
- for (; first != last; ++first) {
- if (invoke(pred, invoke(proj, *first)))
- return false;
- }
- return true;
- }
- // Let `E(i)` be `invoke(pred, invoke(proj, *i))`.
- //
- // Returns: `false` if `E(i)` is `true` for some iterator `i` in `range`, and
- // `true` otherwise.
- //
- // Complexity: At most `size(range)` applications of the predicate and any
- // projection.
- //
- // Reference: https://wg21.link/alg.none.of#:~:text=ranges::none_of(R
- template <typename Range,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr bool none_of(Range&& range, Pred pred, Proj proj = {}) {
- return ranges::none_of(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // [alg.foreach] For each
- // Reference: https://wg21.link/alg.foreach
- // Reference: https://wg21.link/algorithm.syn#:~:text=for_each_result
- template <typename I, typename F>
- using for_each_result = in_fun_result<I, F>;
- // Effects: Calls `invoke(f, invoke(proj, *i))` for every iterator `i` in the
- // range `[first, last)`, starting from `first` and proceeding to `last - 1`.
- //
- // Returns: `{last, std::move(f)}`.
- //
- // Complexity: Applies `f` and `proj` exactly `last - first` times.
- //
- // Remarks: If `f` returns a result, the result is ignored.
- //
- // Reference: https://wg21.link/alg.foreach#:~:text=ranges::for_each(I
- template <typename InputIterator,
- typename Fun,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>>
- constexpr auto for_each(InputIterator first,
- InputIterator last,
- Fun f,
- Proj proj = {}) {
- for (; first != last; ++first)
- invoke(f, invoke(proj, *first));
- return for_each_result<InputIterator, Fun>{first, std::move(f)};
- }
- // Effects: Calls `invoke(f, invoke(proj, *i))` for every iterator `i` in the
- // range `range`, starting from `begin(range)` and proceeding to `end(range) -
- // 1`.
- //
- // Returns: `{last, std::move(f)}`.
- //
- // Complexity: Applies `f` and `proj` exactly `size(range)` times.
- //
- // Remarks: If `f` returns a result, the result is ignored.
- //
- // Reference: https://wg21.link/alg.foreach#:~:text=ranges::for_each(R
- template <typename Range,
- typename Fun,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto for_each(Range&& range, Fun f, Proj proj = {}) {
- return ranges::for_each(ranges::begin(range), ranges::end(range),
- std::move(f), std::move(proj));
- }
- // Reference: https://wg21.link/algorithm.syn#:~:text=for_each_n_result
- template <typename I, typename F>
- using for_each_n_result = in_fun_result<I, F>;
- // Preconditions: `n >= 0` is `true`.
- //
- // Effects: Calls `invoke(f, invoke(proj, *i))` for every iterator `i` in the
- // range `[first, first + n)` in order.
- //
- // Returns: `{first + n, std::move(f)}`.
- //
- // Remarks: If `f` returns a result, the result is ignored.
- //
- // Reference: https://wg21.link/alg.foreach#:~:text=ranges::for_each_n
- template <typename InputIterator,
- typename Size,
- typename Fun,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>>
- constexpr auto for_each_n(InputIterator first, Size n, Fun f, Proj proj = {}) {
- while (n > 0) {
- invoke(f, invoke(proj, *first));
- ++first;
- --n;
- }
- return for_each_n_result<InputIterator, Fun>{first, std::move(f)};
- }
- // [alg.find] Find
- // Reference: https://wg21.link/alg.find
- // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
- //
- // Returns: The first iterator `i` in the range `[first, last)` for which `E(i)`
- // is `true`. Returns `last` if no such iterator is found.
- //
- // Complexity: At most `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.find#:~:text=ranges::find(I
- template <typename InputIterator,
- typename T,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>>
- constexpr auto find(InputIterator first,
- InputIterator last,
- const T& value,
- Proj proj = {}) {
- // Note: In order to be able to apply `proj` to each element in [first, last)
- // we are dispatching to std::find_if instead of std::find.
- return std::find_if(first, last, [&proj, &value](auto&& lhs) {
- return invoke(proj, std::forward<decltype(lhs)>(lhs)) == value;
- });
- }
- // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
- //
- // Returns: The first iterator `i` in `range` for which `E(i)` is `true`.
- // Returns `end(range)` if no such iterator is found.
- //
- // Complexity: At most `size(range)` applications of the corresponding predicate
- // and any projection.
- //
- // Reference: https://wg21.link/alg.find#:~:text=ranges::find(R
- template <typename Range,
- typename T,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto find(Range&& range, const T& value, Proj proj = {}) {
- return ranges::find(ranges::begin(range), ranges::end(range), value,
- std::move(proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
- //
- // Returns: The first iterator `i` in the range `[first, last)` for which `E(i)`
- // is `true`. Returns `last` if no such iterator is found.
- //
- // Complexity: At most `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.find#:~:text=ranges::find_if(I
- template <typename InputIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>>
- constexpr auto find_if(InputIterator first,
- InputIterator last,
- Pred pred,
- Proj proj = {}) {
- return std::find_if(first, last,
- internal::ProjectedUnaryPredicate(pred, proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
- //
- // Returns: The first iterator `i` in `range` for which `E(i)` is `true`.
- // Returns `end(range)` if no such iterator is found.
- //
- // Complexity: At most `size(range)` applications of the corresponding predicate
- // and any projection.
- //
- // Reference: https://wg21.link/alg.find#:~:text=ranges::find_if(R
- template <typename Range,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto find_if(Range&& range, Pred pred, Proj proj = {}) {
- return ranges::find_if(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // Let `E(i)` be `bool(!invoke(pred, invoke(proj, *i)))`.
- //
- // Returns: The first iterator `i` in the range `[first, last)` for which `E(i)`
- // is `true`. Returns `last` if no such iterator is found.
- //
- // Complexity: At most `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.find#:~:text=ranges::find_if_not(I
- template <typename InputIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>>
- constexpr auto find_if_not(InputIterator first,
- InputIterator last,
- Pred pred,
- Proj proj = {}) {
- return std::find_if_not(first, last,
- internal::ProjectedUnaryPredicate(pred, proj));
- }
- // Let `E(i)` be `bool(!invoke(pred, invoke(proj, *i)))`.
- //
- // Returns: The first iterator `i` in `range` for which `E(i)` is `true`.
- // Returns `end(range)` if no such iterator is found.
- //
- // Complexity: At most `size(range)` applications of the corresponding predicate
- // and any projection.
- //
- // Reference: https://wg21.link/alg.find#:~:text=ranges::find_if_not(R
- template <typename Range,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto find_if_not(Range&& range, Pred pred, Proj proj = {}) {
- return ranges::find_if_not(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // [alg.find.end] Find end
- // Reference: https://wg21.link/alg.find.end
- // Let:
- // - `E(i,n)` be `invoke(pred, invoke(proj1, *(i + n)),
- // invoke(proj2, *(first2 + n)))`
- //
- // - `i` be `last1` if `[first2, last2)` is empty, or if
- // `(last2 - first2) > (last1 - first1)` is `true`, or if there is no iterator
- // in the range `[first1, last1 - (last2 - first2))` such that for every
- // non-negative integer `n < (last2 - first2)`, `E(i,n)` is `true`. Otherwise
- // `i` is the last such iterator in `[first1, last1 - (last2 - first2))`.
- //
- // Returns: `i`
- // Note: std::ranges::find_end(I1 first1,...) returns a range, rather than an
- // iterator. For simplicitly we match std::find_end's return type instead.
- //
- // Complexity:
- // At most `(last2 - first2) * (last1 - first1 - (last2 - first2) + 1)`
- // applications of the corresponding predicate and any projections.
- //
- // Reference: https://wg21.link/alg.find.end#:~:text=ranges::find_end(I1
- template <typename ForwardIterator1,
- typename ForwardIterator2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<ForwardIterator1>,
- typename = internal::iterator_category_t<ForwardIterator2>,
- typename = indirect_result_t<Pred&,
- projected<ForwardIterator1, Proj1>,
- projected<ForwardIterator2, Proj2>>>
- constexpr auto find_end(ForwardIterator1 first1,
- ForwardIterator1 last1,
- ForwardIterator2 first2,
- ForwardIterator2 last2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return std::find_end(first1, last1, first2, last2,
- internal::ProjectedBinaryPredicate(pred, proj1, proj2));
- }
- // Let:
- // - `E(i,n)` be `invoke(pred, invoke(proj1, *(i + n)),
- // invoke(proj2, *(first2 + n)))`
- //
- // - `i` be `end(range1)` if `range2` is empty, or if
- // `size(range2) > size(range1)` is `true`, or if there is no iterator in the
- // range `[begin(range1), end(range1) - size(range2))` such that for every
- // non-negative integer `n < size(range2)`, `E(i,n)` is `true`. Otherwise `i`
- // is the last such iterator in `[begin(range1), end(range1) - size(range2))`.
- //
- // Returns: `i`
- // Note: std::ranges::find_end(R1&& r1,...) returns a range, rather than an
- // iterator. For simplicitly we match std::find_end's return type instead.
- //
- // Complexity: At most `size(range2) * (size(range1) - size(range2) + 1)`
- // applications of the corresponding predicate and any projections.
- //
- // Reference: https://wg21.link/alg.find.end#:~:text=ranges::find_end(R1
- template <typename Range1,
- typename Range2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = indirect_result_t<Pred&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>>
- constexpr auto find_end(Range1&& range1,
- Range2&& range2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::find_end(ranges::begin(range1), ranges::end(range1),
- ranges::begin(range2), ranges::end(range2),
- std::move(pred), std::move(proj1), std::move(proj2));
- }
- // [alg.find.first.of] Find first
- // Reference: https://wg21.link/alg.find.first.of
- // Let `E(i,j)` be `bool(invoke(pred, invoke(proj1, *i), invoke(proj2, *j)))`.
- //
- // Effects: Finds an element that matches one of a set of values.
- //
- // Returns: The first iterator `i` in the range `[first1, last1)` such that for
- // some iterator `j` in the range `[first2, last2)` `E(i,j)` holds. Returns
- // `last1` if `[first2, last2)` is empty or if no such iterator is found.
- //
- // Complexity: At most `(last1 - first1) * (last2 - first2)` applications of the
- // corresponding predicate and any projections.
- //
- // Reference:
- // https://wg21.link/alg.find.first.of#:~:text=ranges::find_first_of(I1
- template <typename ForwardIterator1,
- typename ForwardIterator2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<ForwardIterator1>,
- typename = internal::iterator_category_t<ForwardIterator2>,
- typename = indirect_result_t<Pred&,
- projected<ForwardIterator1, Proj1>,
- projected<ForwardIterator2, Proj2>>>
- constexpr auto find_first_of(ForwardIterator1 first1,
- ForwardIterator1 last1,
- ForwardIterator2 first2,
- ForwardIterator2 last2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return std::find_first_of(
- first1, last1, first2, last2,
- internal::ProjectedBinaryPredicate(pred, proj1, proj2));
- }
- // Let `E(i,j)` be `bool(invoke(pred, invoke(proj1, *i), invoke(proj2, *j)))`.
- //
- // Effects: Finds an element that matches one of a set of values.
- //
- // Returns: The first iterator `i` in `range1` such that for some iterator `j`
- // in `range2` `E(i,j)` holds. Returns `end(range1)` if `range2` is empty or if
- // no such iterator is found.
- //
- // Complexity: At most `size(range1) * size(range2)` applications of the
- // corresponding predicate and any projections.
- //
- // Reference:
- // https://wg21.link/alg.find.first.of#:~:text=ranges::find_first_of(R1
- template <typename Range1,
- typename Range2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = indirect_result_t<Pred&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>>
- constexpr auto find_first_of(Range1&& range1,
- Range2&& range2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::find_first_of(
- ranges::begin(range1), ranges::end(range1), ranges::begin(range2),
- ranges::end(range2), std::move(pred), std::move(proj1), std::move(proj2));
- }
- // [alg.adjacent.find] Adjacent find
- // Reference: https://wg21.link/alg.adjacent.find
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i), invoke(proj, *(i + 1))))`.
- //
- // Returns: The first iterator `i` such that both `i` and `i + 1` are in the
- // range `[first, last)` for which `E(i)` holds. Returns `last` if no such
- // iterator is found.
- //
- // Complexity: Exactly `min((i - first) + 1, (last - first) - 1)` applications
- // of the corresponding predicate, where `i` is `adjacent_find`'s return value.
- //
- // Reference:
- // https://wg21.link/alg.adjacent.find#:~:text=ranges::adjacent_find(I
- template <typename ForwardIterator,
- typename Pred = ranges::equal_to,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto adjacent_find(ForwardIterator first,
- ForwardIterator last,
- Pred pred = {},
- Proj proj = {}) {
- return std::adjacent_find(
- first, last, internal::ProjectedBinaryPredicate(pred, proj, proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i), invoke(proj, *(i + 1))))`.
- //
- // Returns: The first iterator `i` such that both `i` and `i + 1` are in the
- // range `range` for which `E(i)` holds. Returns `end(range)` if no such
- // iterator is found.
- //
- // Complexity: Exactly `min((i - begin(range)) + 1, size(range) - 1)`
- // applications of the corresponding predicate, where `i` is `adjacent_find`'s
- // return value.
- //
- // Reference:
- // https://wg21.link/alg.adjacent.find#:~:text=ranges::adjacent_find(R
- template <typename Range,
- typename Pred = ranges::equal_to,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto adjacent_find(Range&& range, Pred pred = {}, Proj proj = {}) {
- return ranges::adjacent_find(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // [alg.count] Count
- // Reference: https://wg21.link/alg.count
- // Let `E(i)` be `invoke(proj, *i) == value`.
- //
- // Effects: Returns the number of iterators `i` in the range `[first, last)` for
- // which `E(i)` holds.
- //
- // Complexity: Exactly `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.count#:~:text=ranges::count(I
- template <typename InputIterator,
- typename T,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>>
- constexpr auto count(InputIterator first,
- InputIterator last,
- const T& value,
- Proj proj = {}) {
- // Note: In order to be able to apply `proj` to each element in [first, last)
- // we are dispatching to std::count_if instead of std::count.
- return std::count_if(first, last, [&proj, &value](auto&& lhs) {
- return invoke(proj, std::forward<decltype(lhs)>(lhs)) == value;
- });
- }
- // Let `E(i)` be `invoke(proj, *i) == value`.
- //
- // Effects: Returns the number of iterators `i` in `range` for which `E(i)`
- // holds.
- //
- // Complexity: Exactly `size(range)` applications of the corresponding predicate
- // and any projection.
- //
- // Reference: https://wg21.link/alg.count#:~:text=ranges::count(R
- template <typename Range,
- typename T,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto count(Range&& range, const T& value, Proj proj = {}) {
- return ranges::count(ranges::begin(range), ranges::end(range), value,
- std::move(proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
- //
- // Effects: Returns the number of iterators `i` in the range `[first, last)` for
- // which `E(i)` holds.
- //
- // Complexity: Exactly `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.count#:~:text=ranges::count_if(I
- template <typename InputIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>>
- constexpr auto count_if(InputIterator first,
- InputIterator last,
- Pred pred,
- Proj proj = {}) {
- return std::count_if(first, last,
- internal::ProjectedUnaryPredicate(pred, proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
- //
- // Effects: Returns the number of iterators `i` in `range` for which `E(i)`
- // holds.
- //
- // Complexity: Exactly `size(range)` applications of the corresponding predicate
- // and any projection.
- //
- // Reference: https://wg21.link/alg.count#:~:text=ranges::count_if(R
- template <typename Range,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto count_if(Range&& range, Pred pred, Proj proj = {}) {
- return ranges::count_if(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // [mismatch] Mismatch
- // Reference: https://wg21.link/mismatch
- // Let `E(n)` be `!invoke(pred, invoke(proj1, *(first1 + n)),
- // invoke(proj2, *(first2 + n)))`.
- //
- // Let `N` be `min(last1 - first1, last2 - first2)`.
- //
- // Returns: `{ first1 + n, first2 + n }`, where `n` is the smallest integer in
- // `[0, N)` such that `E(n)` holds, or `N` if no such integer exists.
- //
- // Complexity: At most `N` applications of the corresponding predicate and any
- // projections.
- //
- // Reference: https://wg21.link/mismatch#:~:text=ranges::mismatch(I1
- template <typename ForwardIterator1,
- typename ForwardIterator2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<ForwardIterator1>,
- typename = internal::iterator_category_t<ForwardIterator2>,
- typename = indirect_result_t<Pred&,
- projected<ForwardIterator1, Proj1>,
- projected<ForwardIterator2, Proj2>>>
- constexpr auto mismatch(ForwardIterator1 first1,
- ForwardIterator1 last1,
- ForwardIterator2 first2,
- ForwardIterator2 last2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return std::mismatch(first1, last1, first2, last2,
- internal::ProjectedBinaryPredicate(pred, proj1, proj2));
- }
- // Let `E(n)` be `!invoke(pred, invoke(proj1, *(begin(range1) + n)),
- // invoke(proj2, *(begin(range2) + n)))`.
- //
- // Let `N` be `min(size(range1), size(range2))`.
- //
- // Returns: `{ begin(range1) + n, begin(range2) + n }`, where `n` is the
- // smallest integer in `[0, N)` such that `E(n)` holds, or `N` if no such
- // integer exists.
- //
- // Complexity: At most `N` applications of the corresponding predicate and any
- // projections.
- //
- // Reference: https://wg21.link/mismatch#:~:text=ranges::mismatch(R1
- template <typename Range1,
- typename Range2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = indirect_result_t<Pred&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>>
- constexpr auto mismatch(Range1&& range1,
- Range2&& range2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::mismatch(ranges::begin(range1), ranges::end(range1),
- ranges::begin(range2), ranges::end(range2),
- std::move(pred), std::move(proj1), std::move(proj2));
- }
- // [alg.equal] Equal
- // Reference: https://wg21.link/alg.equal
- // Let `E(i)` be
- // `invoke(pred, invoke(proj1, *i), invoke(proj2, *(first2 + (i - first1))))`.
- //
- // Returns: If `last1 - first1 != last2 - first2`, return `false.` Otherwise
- // return `true` if `E(i)` holds for every iterator `i` in the range `[first1,
- // last1)`. Otherwise, returns `false`.
- //
- // Complexity: If the types of `first1`, `last1`, `first2`, and `last2` meet the
- // `RandomAccessIterator` requirements and `last1 - first1 != last2 - first2`,
- // then no applications of the corresponding predicate and each projection;
- // otherwise, at most `min(last1 - first1, last2 - first2)` applications of the
- // corresponding predicate and any projections.
- //
- // Reference: https://wg21.link/alg.equal#:~:text=ranges::equal(I1
- template <typename ForwardIterator1,
- typename ForwardIterator2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<ForwardIterator1>,
- typename = internal::iterator_category_t<ForwardIterator2>,
- typename = indirect_result_t<Pred&,
- projected<ForwardIterator1, Proj1>,
- projected<ForwardIterator2, Proj2>>>
- constexpr bool equal(ForwardIterator1 first1,
- ForwardIterator1 last1,
- ForwardIterator2 first2,
- ForwardIterator2 last2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return std::equal(first1, last1, first2, last2,
- internal::ProjectedBinaryPredicate(pred, proj1, proj2));
- }
- // Let `E(i)` be
- // `invoke(pred, invoke(proj1, *i),
- // invoke(proj2, *(begin(range2) + (i - begin(range1)))))`.
- //
- // Returns: If `size(range1) != size(range2)`, return `false.` Otherwise return
- // `true` if `E(i)` holds for every iterator `i` in `range1`. Otherwise, returns
- // `false`.
- //
- // Complexity: If the types of `begin(range1)`, `end(range1)`, `begin(range2)`,
- // and `end(range2)` meet the `RandomAccessIterator` requirements and
- // `size(range1) != size(range2)`, then no applications of the corresponding
- // predicate and each projection;
- // otherwise, at most `min(size(range1), size(range2))` applications of the
- // corresponding predicate and any projections.
- //
- // Reference: https://wg21.link/alg.equal#:~:text=ranges::equal(R1
- template <typename Range1,
- typename Range2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = indirect_result_t<Pred&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>>
- constexpr bool equal(Range1&& range1,
- Range2&& range2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::equal(ranges::begin(range1), ranges::end(range1),
- ranges::begin(range2), ranges::end(range2),
- std::move(pred), std::move(proj1), std::move(proj2));
- }
- // [alg.is.permutation] Is permutation
- // Reference: https://wg21.link/alg.is.permutation
- // Returns: If `last1 - first1 != last2 - first2`, return `false`. Otherwise
- // return `true` if there exists a permutation of the elements in the range
- // `[first2, last2)`, bounded by `[pfirst, plast)`, such that
- // `ranges::equal(first1, last1, pfirst, plast, pred, proj, proj)` returns
- // `true`; otherwise, returns `false`.
- //
- // Complexity: No applications of the corresponding predicate if
- // ForwardIterator1 and ForwardIterator2 meet the requirements of random access
- // iterators and `last1 - first1 != last2 - first2`. Otherwise, exactly
- // `last1 - first1` applications of the corresponding predicate and projections
- // if `ranges::equal(first1, last1, first2, last2, pred, proj, proj)` would
- // return true;
- // otherwise, at worst `O(N^2)`, where `N` has the value `last1 - first1`.
- //
- // Reference:
- // https://wg21.link/alg.is.permutation#:~:text=ranges::is_permutation(I1
- template <typename ForwardIterator1,
- typename ForwardIterator2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<ForwardIterator1>,
- typename = internal::iterator_category_t<ForwardIterator2>,
- typename = indirect_result_t<Pred&,
- projected<ForwardIterator1, Proj1>,
- projected<ForwardIterator2, Proj2>>>
- constexpr bool is_permutation(ForwardIterator1 first1,
- ForwardIterator1 last1,
- ForwardIterator2 first2,
- ForwardIterator2 last2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- // Needs to opt-in to all permutations, since std::is_permutation expects
- // pred(proj1(lhs), proj1(rhs)) to compile.
- return std::is_permutation(
- first1, last1, first2, last2,
- internal::PermutedProjectedBinaryPredicate(pred, proj1, proj2));
- }
- // Returns: If `size(range1) != size(range2)`, return `false`. Otherwise return
- // `true` if there exists a permutation of the elements in `range2`, bounded by
- // `[pbegin, pend)`, such that
- // `ranges::equal(range1, [pbegin, pend), pred, proj, proj)` returns `true`;
- // otherwise, returns `false`.
- //
- // Complexity: No applications of the corresponding predicate if Range1 and
- // Range2 meet the requirements of random access ranges and
- // `size(range1) != size(range2)`. Otherwise, exactly `size(range1)`
- // applications of the corresponding predicate and projections if
- // `ranges::equal(range1, range2, pred, proj, proj)` would return true;
- // otherwise, at worst `O(N^2)`, where `N` has the value `size(range1)`.
- //
- // Reference:
- // https://wg21.link/alg.is.permutation#:~:text=ranges::is_permutation(R1
- template <typename Range1,
- typename Range2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = indirect_result_t<Pred&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>>
- constexpr bool is_permutation(Range1&& range1,
- Range2&& range2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::is_permutation(
- ranges::begin(range1), ranges::end(range1), ranges::begin(range2),
- ranges::end(range2), std::move(pred), std::move(proj1), std::move(proj2));
- }
- // [alg.search] Search
- // Reference: https://wg21.link/alg.search
- // Returns: `i`, where `i` is the first iterator in the range
- // `[first1, last1 - (last2 - first2))` such that for every non-negative integer
- // `n` less than `last2 - first2` the condition
- // `bool(invoke(pred, invoke(proj1, *(i + n)), invoke(proj2, *(first2 + n))))`
- // is `true`.
- // Returns `last1` if no such iterator exists.
- // Note: std::ranges::search(I1 first1,...) returns a range, rather than an
- // iterator. For simplicitly we match std::search's return type instead.
- //
- // Complexity: At most `(last1 - first1) * (last2 - first2)` applications of the
- // corresponding predicate and projections.
- //
- // Reference: https://wg21.link/alg.search#:~:text=ranges::search(I1
- template <typename ForwardIterator1,
- typename ForwardIterator2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<ForwardIterator1>,
- typename = internal::iterator_category_t<ForwardIterator2>,
- typename = indirect_result_t<Pred&,
- projected<ForwardIterator1, Proj1>,
- projected<ForwardIterator2, Proj2>>>
- constexpr auto search(ForwardIterator1 first1,
- ForwardIterator1 last1,
- ForwardIterator2 first2,
- ForwardIterator2 last2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return std::search(first1, last1, first2, last2,
- internal::ProjectedBinaryPredicate(pred, proj1, proj2));
- }
- // Returns: `i`, where `i` is the first iterator in the range
- // `[begin(range1), end(range1) - size(range2))` such that for every
- // non-negative integer `n` less than `size(range2)` the condition
- // `bool(invoke(pred, invoke(proj1, *(i + n)),
- // invoke(proj2, *(begin(range2) + n))))` is `true`.
- // Returns `end(range1)` if no such iterator exists.
- // Note: std::ranges::search(R1&& r1,...) returns a range, rather than an
- // iterator. For simplicitly we match std::search's return type instead.
- //
- // Complexity: At most `size(range1) * size(range2)` applications of the
- // corresponding predicate and projections.
- //
- // Reference: https://wg21.link/alg.search#:~:text=ranges::search(R1
- template <typename Range1,
- typename Range2,
- typename Pred = ranges::equal_to,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = indirect_result_t<Pred&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>>
- constexpr auto search(Range1&& range1,
- Range2&& range2,
- Pred pred = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::search(ranges::begin(range1), ranges::end(range1),
- ranges::begin(range2), ranges::end(range2),
- std::move(pred), std::move(proj1), std::move(proj2));
- }
- // Mandates: The type `Size` is convertible to an integral type.
- //
- // Returns: `i` where `i` is the first iterator in the range
- // `[first, last - count)` such that for every non-negative integer `n` less
- // than `count`, the following condition holds:
- // `invoke(pred, invoke(proj, *(i + n)), value)`.
- // Returns `last` if no such iterator is found.
- // Note: std::ranges::search_n(I1 first1,...) returns a range, rather than an
- // iterator. For simplicitly we match std::search_n's return type instead.
- //
- // Complexity: At most `last - first` applications of the corresponding
- // predicate and projection.
- //
- // Reference: https://wg21.link/alg.search#:~:text=ranges::search_n(I
- template <typename ForwardIterator,
- typename Size,
- typename T,
- typename Pred = ranges::equal_to,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto search_n(ForwardIterator first,
- ForwardIterator last,
- Size count,
- const T& value,
- Pred pred = {},
- Proj proj = {}) {
- // The second arg is guaranteed to be `value`, so we'll simply apply the
- // identity projection.
- identity value_proj;
- return std::search_n(
- first, last, count, value,
- internal::ProjectedBinaryPredicate(pred, proj, value_proj));
- }
- // Mandates: The type `Size` is convertible to an integral type.
- //
- // Returns: `i` where `i` is the first iterator in the range
- // `[begin(range), end(range) - count)` such that for every non-negative integer
- // `n` less than `count`, the following condition holds:
- // `invoke(pred, invoke(proj, *(i + n)), value)`.
- // Returns `end(arnge)` if no such iterator is found.
- // Note: std::ranges::search_n(R1&& r1,...) returns a range, rather than an
- // iterator. For simplicitly we match std::search_n's return type instead.
- //
- // Complexity: At most `size(range)` applications of the corresponding predicate
- // and projection.
- //
- // Reference: https://wg21.link/alg.search#:~:text=ranges::search_n(R
- template <typename Range,
- typename Size,
- typename T,
- typename Pred = ranges::equal_to,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto search_n(Range&& range,
- Size count,
- const T& value,
- Pred pred = {},
- Proj proj = {}) {
- return ranges::search_n(ranges::begin(range), ranges::end(range), count,
- value, std::move(pred), std::move(proj));
- }
- // [alg.modifying.operations] Mutating sequence operations
- // Reference: https://wg21.link/alg.modifying.operations
- // [alg.copy] Copy
- // Reference: https://wg21.link/alg.copy
- // Let N be `last - first`.
- //
- // Preconditions: `result` is not in the range `[first, last)`.
- //
- // Effects: Copies elements in the range `[first, last)` into the range
- // `[result, result + N)` starting from `first` and proceeding to `last`. For
- // each non-negative integer `n < N` , performs `*(result + n) = *(first + n)`.
- //
- // Returns: `result + N`
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy(I
- template <typename InputIterator,
- typename OutputIterator,
- typename = internal::iterator_category_t<InputIterator>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto copy(InputIterator first,
- InputIterator last,
- OutputIterator result) {
- return std::copy(first, last, result);
- }
- // Let N be `size(range)`.
- //
- // Preconditions: `result` is not in `range`.
- //
- // Effects: Copies elements in `range` into the range `[result, result + N)`
- // starting from `begin(range)` and proceeding to `end(range)`. For each
- // non-negative integer `n < N` , performs
- // *(result + n) = *(begin(range) + n)`.
- //
- // Returns: `result + N`
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy(R
- template <typename Range,
- typename OutputIterator,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto copy(Range&& range, OutputIterator result) {
- return ranges::copy(ranges::begin(range), ranges::end(range), result);
- }
- // Let `N` be `max(0, n)`.
- //
- // Mandates: The type `Size` is convertible to an integral type.
- //
- // Effects: For each non-negative integer `i < N`, performs
- // `*(result + i) = *(first + i)`.
- //
- // Returns: `result + N`
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy_n
- template <typename InputIterator,
- typename Size,
- typename OutputIterator,
- typename = internal::iterator_category_t<InputIterator>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto copy_n(InputIterator first, Size n, OutputIterator result) {
- return std::copy_n(first, n, result);
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`, and `N` be the number
- // of iterators `i` in the range `[first, last)` for which the condition `E(i)`
- // holds.
- //
- // Preconditions: The ranges `[first, last)` and
- // `[result, result + (last - first))` do not overlap.
- //
- // Effects: Copies all of the elements referred to by the iterator `i` in the
- // range `[first, last)` for which `E(i)` is true.
- //
- // Returns: `result + N`
- //
- // Complexity: Exactly `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy_if(I
- template <typename InputIterator,
- typename OutputIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto copy_if(InputIterator first,
- InputIterator last,
- OutputIterator result,
- Pred pred,
- Proj proj = {}) {
- return std::copy_if(first, last, result,
- internal::ProjectedUnaryPredicate(pred, proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`, and `N` be the number
- // of iterators `i` in `range` for which the condition `E(i)` holds.
- //
- // Preconditions: `range` and `[result, result + size(range))` do not overlap.
- //
- // Effects: Copies all of the elements referred to by the iterator `i` in
- // `range` for which `E(i)` is true.
- //
- // Returns: `result + N`
- //
- // Complexity: Exactly `size(range)` applications of the corresponding predicate
- // and any projection.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy_if(R
- template <typename Range,
- typename OutputIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto copy_if(Range&& range,
- OutputIterator result,
- Pred pred,
- Proj proj = {}) {
- return ranges::copy_if(ranges::begin(range), ranges::end(range), result,
- std::move(pred), std::move(proj));
- }
- // Let `N` be `last - first`.
- //
- // Preconditions: `result` is not in the range `(first, last]`.
- //
- // Effects: Copies elements in the range `[first, last)` into the range
- // `[result - N, result)` starting from `last - 1` and proceeding to `first`.
- // For each positive integer `n ≤ N`, performs `*(result - n) = *(last - n)`.
- //
- // Returns: `result - N`
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy_backward(I1
- template <typename BidirectionalIterator1,
- typename BidirectionalIterator2,
- typename = internal::iterator_category_t<BidirectionalIterator1>,
- typename = internal::iterator_category_t<BidirectionalIterator2>>
- constexpr auto copy_backward(BidirectionalIterator1 first,
- BidirectionalIterator1 last,
- BidirectionalIterator2 result) {
- return std::copy_backward(first, last, result);
- }
- // Let `N` be `size(range)`.
- //
- // Preconditions: `result` is not in the range `(begin(range), end(range)]`.
- //
- // Effects: Copies elements in `range` into the range `[result - N, result)`
- // starting from `end(range) - 1` and proceeding to `begin(range)`. For each
- // positive integer `n ≤ N`, performs `*(result - n) = *(end(range) - n)`.
- //
- // Returns: `result - N`
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.copy#:~:text=ranges::copy_backward(R
- template <typename Range,
- typename BidirectionalIterator,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<BidirectionalIterator>>
- constexpr auto copy_backward(Range&& range, BidirectionalIterator result) {
- return ranges::copy_backward(ranges::begin(range), ranges::end(range),
- result);
- }
- // [alg.move] Move
- // Reference: https://wg21.link/alg.move
- // Let `E(n)` be `std::move(*(first + n))`.
- //
- // Let `N` be `last - first`.
- //
- // Preconditions: `result` is not in the range `[first, last)`.
- //
- // Effects: Moves elements in the range `[first, last)` into the range `[result,
- // result + N)` starting from `first` and proceeding to `last`. For each
- // non-negative integer `n < N`, performs `*(result + n) = E(n)`.
- //
- // Returns: `result + N`
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.move#:~:text=ranges::move(I
- template <typename InputIterator,
- typename OutputIterator,
- typename = internal::iterator_category_t<InputIterator>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto move(InputIterator first,
- InputIterator last,
- OutputIterator result) {
- return std::move(first, last, result);
- }
- // Let `E(n)` be `std::move(*(begin(range) + n))`.
- //
- // Let `N` be `size(range)`.
- //
- // Preconditions: `result` is not in `range`.
- //
- // Effects: Moves elements in `range` into the range `[result, result + N)`
- // starting from `begin(range)` and proceeding to `end(range)`. For each
- // non-negative integer `n < N`, performs `*(result + n) = E(n)`.
- //
- // Returns: `result + N`
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.move#:~:text=ranges::move(R
- template <typename Range,
- typename OutputIterator,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto move(Range&& range, OutputIterator result) {
- return ranges::move(ranges::begin(range), ranges::end(range), result);
- }
- // Let `E(n)` be `std::move(*(last - n))`.
- //
- // Let `N` be `last - first`.
- //
- // Preconditions: `result` is not in the range `(first, last]`.
- //
- // Effects: Moves elements in the range `[first, last)` into the range
- // `[result - N, result)` starting from `last - 1` and proceeding to `first`.
- // For each positive integer `n ≤ N`, performs `*(result - n) = E(n)`.
- //
- // Returns: `result - N`
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.move#:~:text=ranges::move_backward(I1
- template <typename BidirectionalIterator1,
- typename BidirectionalIterator2,
- typename = internal::iterator_category_t<BidirectionalIterator1>,
- typename = internal::iterator_category_t<BidirectionalIterator2>>
- constexpr auto move_backward(BidirectionalIterator1 first,
- BidirectionalIterator1 last,
- BidirectionalIterator2 result) {
- return std::move_backward(first, last, result);
- }
- // Let `E(n)` be `std::move(*(end(range) - n))`.
- //
- // Let `N` be `size(range)`.
- //
- // Preconditions: `result` is not in the range `(begin(range), end(range)]`.
- //
- // Effects: Moves elements in `range` into the range `[result - N, result)`
- // starting from `end(range) - 1` and proceeding to `begin(range)`. For each
- // positive integer `n ≤ N`, performs `*(result - n) = E(n)`.
- //
- // Returns: `result - N`
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.move#:~:text=ranges::move_backward(R
- template <typename Range,
- typename BidirectionalIterator,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<BidirectionalIterator>>
- constexpr auto move_backward(Range&& range, BidirectionalIterator result) {
- return ranges::move_backward(ranges::begin(range), ranges::end(range),
- result);
- }
- // [alg.swap] Swap
- // Reference: https://wg21.link/alg.swap
- // Let `M` be `min(last1 - first1, last2 - first2)`.
- //
- // Preconditions: The two ranges `[first1, last1)` and `[first2, last2)` do not
- // overlap. `*(first1 + n)` is swappable with `*(first2 + n)`.
- //
- // Effects: For each non-negative integer `n < M` performs
- // `swap(*(first1 + n), *(first2 + n))`
- //
- // Returns: `first2 + M`
- //
- // Complexity: Exactly `M` swaps.
- //
- // Reference: https://wg21.link/alg.swap#:~:text=ranges::swap_ranges(I1
- template <typename ForwardIterator1,
- typename ForwardIterator2,
- typename = internal::iterator_category_t<ForwardIterator1>,
- typename = internal::iterator_category_t<ForwardIterator2>>
- constexpr auto swap_ranges(ForwardIterator1 first1,
- ForwardIterator1 last1,
- ForwardIterator2 first2,
- ForwardIterator2 last2) {
- // std::swap_ranges does not have a `last2` overload. Thus we need to
- // adjust `last1` to ensure to not read past `last2`.
- last1 = std::next(first1, std::min(std::distance(first1, last1),
- std::distance(first2, last2)));
- return std::swap_ranges(first1, last1, first2);
- }
- // Let `M` be `min(size(range1), size(range2))`.
- //
- // Preconditions: The two ranges `range1` and `range2` do not overlap.
- // `*(begin(range1) + n)` is swappable with `*(begin(range2) + n)`.
- //
- // Effects: For each non-negative integer `n < M` performs
- // `swap(*(begin(range1) + n), *(begin(range2) + n))`
- //
- // Returns: `begin(range2) + M`
- //
- // Complexity: Exactly `M` swaps.
- //
- // Reference: https://wg21.link/alg.swap#:~:text=ranges::swap_ranges(R1
- template <typename Range1,
- typename Range2,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>>
- constexpr auto swap_ranges(Range1&& range1, Range2&& range2) {
- return ranges::swap_ranges(ranges::begin(range1), ranges::end(range1),
- ranges::begin(range2), ranges::end(range2));
- }
- // [alg.transform] Transform
- // Reference: https://wg21.link/alg.transform
- // Let `N` be `last1 - first1`,
- // `E(i)` be `invoke(op, invoke(proj, *(first1 + (i - result))))`.
- //
- // Preconditions: `op` does not invalidate iterators or subranges, nor modify
- // elements in the ranges `[first1, first1 + N]`, and `[result, result + N]`.
- //
- // Effects: Assigns through every iterator `i` in the range
- // `[result, result + N)` a new corresponding value equal to `E(i)`.
- //
- // Returns: `result + N`
- //
- // Complexity: Exactly `N` applications of `op` and any projections.
- //
- // Remarks: result may be equal to `first1`.
- //
- // Reference: https://wg21.link/alg.transform#:~:text=ranges::transform(I
- template <typename InputIterator,
- typename OutputIterator,
- typename UnaryOperation,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<UnaryOperation&,
- projected<InputIterator, Proj>>>
- constexpr auto transform(InputIterator first1,
- InputIterator last1,
- OutputIterator result,
- UnaryOperation op,
- Proj proj = {}) {
- return std::transform(first1, last1, result, [&op, &proj](auto&& arg) {
- return invoke(op, invoke(proj, std::forward<decltype(arg)>(arg)));
- });
- }
- // Let `N` be `size(range)`,
- // `E(i)` be `invoke(op, invoke(proj, *(begin(range) + (i - result))))`.
- //
- // Preconditions: `op` does not invalidate iterators or subranges, nor modify
- // elements in the ranges `[begin(range), end(range)]`, and
- // `[result, result + N]`.
- //
- // Effects: Assigns through every iterator `i` in the range
- // `[result, result + N)` a new corresponding value equal to `E(i)`.
- //
- // Returns: `result + N`
- //
- // Complexity: Exactly `N` applications of `op` and any projections.
- //
- // Remarks: result may be equal to `begin(range)`.
- //
- // Reference: https://wg21.link/alg.transform#:~:text=ranges::transform(R
- template <typename Range,
- typename OutputIterator,
- typename UnaryOperation,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<UnaryOperation&,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto transform(Range&& range,
- OutputIterator result,
- UnaryOperation op,
- Proj proj = {}) {
- return ranges::transform(ranges::begin(range), ranges::end(range), result,
- std::move(op), std::move(proj));
- }
- // Let:
- // `N` be `min(last1 - first1, last2 - first2)`,
- // `E(i)` be `invoke(binary_op, invoke(proj1, *(first1 + (i - result))),
- // invoke(proj2, *(first2 + (i - result))))`.
- //
- // Preconditions: `binary_op` does not invalidate iterators or subranges, nor
- // modify elements in the ranges `[first1, first1 + N]`, `[first2, first2 + N]`,
- // and `[result, result + N]`.
- //
- // Effects: Assigns through every iterator `i` in the range
- // `[result, result + N)` a new corresponding value equal to `E(i)`.
- //
- // Returns: `result + N`
- //
- // Complexity: Exactly `N` applications of `binary_op`, and any projections.
- //
- // Remarks: `result` may be equal to `first1` or `first2`.
- //
- // Reference: https://wg21.link/alg.transform#:~:text=ranges::transform(I1
- template <typename ForwardIterator1,
- typename ForwardIterator2,
- typename OutputIterator,
- typename BinaryOperation,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<ForwardIterator1>,
- typename = internal::iterator_category_t<ForwardIterator2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<BinaryOperation&,
- projected<ForwardIterator1, Proj1>,
- projected<ForwardIterator2, Proj2>>>
- constexpr auto transform(ForwardIterator1 first1,
- ForwardIterator1 last1,
- ForwardIterator2 first2,
- ForwardIterator2 last2,
- OutputIterator result,
- BinaryOperation binary_op,
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- // std::transform does not have a `last2` overload. Thus we need to adjust
- // `last1` to ensure to not read past `last2`.
- last1 = std::next(first1, std::min(std::distance(first1, last1),
- std::distance(first2, last2)));
- return std::transform(first1, last1, first2, result,
- [&binary_op, &proj1, &proj2](auto&& lhs, auto&& rhs) {
- return invoke(
- binary_op,
- invoke(proj1, std::forward<decltype(lhs)>(lhs)),
- invoke(proj2, std::forward<decltype(rhs)>(rhs)));
- });
- }
- // Let:
- // `N` be `min(size(range1), size(range2)`,
- // `E(i)` be `invoke(binary_op, invoke(proj1, *(begin(range1) + (i - result))),
- // invoke(proj2, *(begin(range2) + (i - result))))`
- //
- // Preconditions: `binary_op` does not invalidate iterators or subranges, nor
- // modify elements in the ranges `[begin(range1), end(range1)]`,
- // `[begin(range2), end(range2)]`, and `[result, result + N]`.
- //
- // Effects: Assigns through every iterator `i` in the range
- // `[result, result + N)` a new corresponding value equal to `E(i)`.
- //
- // Returns: `result + N`
- //
- // Complexity: Exactly `N` applications of `binary_op`, and any projections.
- //
- // Remarks: `result` may be equal to `begin(range1)` or `begin(range2)`.
- //
- // Reference: https://wg21.link/alg.transform#:~:text=ranges::transform(R1
- template <typename Range1,
- typename Range2,
- typename OutputIterator,
- typename BinaryOperation,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<BinaryOperation&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>>
- constexpr auto transform(Range1&& range1,
- Range2&& range2,
- OutputIterator result,
- BinaryOperation binary_op,
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::transform(ranges::begin(range1), ranges::end(range1),
- ranges::begin(range2), ranges::end(range2), result,
- std::move(binary_op), std::move(proj1),
- std::move(proj2));
- }
- // [alg.replace] Replace
- // Reference: https://wg21.link/alg.replace
- // Let `E(i)` be `bool(invoke(proj, *i) == old_value)`.
- //
- // Mandates: `new_value` is writable to `first`.
- //
- // Effects: Substitutes elements referred by the iterator `i` in the range
- // `[first, last)` with `new_value`, when `E(i)` is true.
- //
- // Returns: `last`
- //
- // Complexity: Exactly `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace(I
- template <typename ForwardIterator,
- typename T,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto replace(ForwardIterator first,
- ForwardIterator last,
- const T& old_value,
- const T& new_value,
- Proj proj = {}) {
- // Note: In order to be able to apply `proj` to each element in [first, last)
- // we are dispatching to std::replace_if instead of std::replace.
- std::replace_if(
- first, last,
- [&proj, &old_value](auto&& lhs) {
- return invoke(proj, std::forward<decltype(lhs)>(lhs)) == old_value;
- },
- new_value);
- return last;
- }
- // Let `E(i)` be `bool(invoke(proj, *i) == old_value)`.
- //
- // Mandates: `new_value` is writable to `begin(range)`.
- //
- // Effects: Substitutes elements referred by the iterator `i` in `range` with
- // `new_value`, when `E(i)` is true.
- //
- // Returns: `end(range)`
- //
- // Complexity: Exactly `size(range)` applications of the corresponding predicate
- // and any projection.
- //
- // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace(R
- template <typename Range,
- typename T,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto replace(Range&& range,
- const T& old_value,
- const T& new_value,
- Proj proj = {}) {
- return ranges::replace(ranges::begin(range), ranges::end(range), old_value,
- new_value, std::move(proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
- //
- // Mandates: `new_value` is writable to `first`.
- //
- // Effects: Substitutes elements referred by the iterator `i` in the range
- // `[first, last)` with `new_value`, when `E(i)` is true.
- //
- // Returns: `last`
- //
- // Complexity: Exactly `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_if(I
- template <typename ForwardIterator,
- typename Predicate,
- typename T,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto replace_if(ForwardIterator first,
- ForwardIterator last,
- Predicate pred,
- const T& new_value,
- Proj proj = {}) {
- std::replace_if(first, last, internal::ProjectedUnaryPredicate(pred, proj),
- new_value);
- return last;
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
- //
- // Mandates: `new_value` is writable to `begin(range)`.
- //
- // Effects: Substitutes elements referred by the iterator `i` in `range` with
- // `new_value`, when `E(i)` is true.
- //
- // Returns: `end(range)`
- //
- // Complexity: Exactly `size(range)` applications of the corresponding predicate
- // and any projection.
- //
- // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_if(R
- template <typename Range,
- typename Predicate,
- typename T,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto replace_if(Range&& range,
- Predicate pred,
- const T& new_value,
- Proj proj = {}) {
- return ranges::replace_if(ranges::begin(range), ranges::end(range),
- std::move(pred), new_value, std::move(proj));
- }
- // Let `E(i)` be `bool(invoke(proj, *(first + (i - result))) == old_value)`.
- //
- // Mandates: The results of the expressions `*first` and `new_value` are
- // writable to `result`.
- //
- // Preconditions: The ranges `[first, last)` and `[result, result + (last -
- // first))` do not overlap.
- //
- // Effects: Assigns through every iterator `i` in the range `[result, result +
- // (last - first))` a new corresponding value, `new_value` if `E(i)` is true, or
- // `*(first + (i - result))` otherwise.
- //
- // Returns: `result + (last - first)`.
- //
- // Complexity: Exactly `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_copy(I
- template <typename InputIterator,
- typename OutputIterator,
- typename T,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto replace_copy(InputIterator first,
- InputIterator last,
- OutputIterator result,
- const T& old_value,
- const T& new_value,
- Proj proj = {}) {
- // Note: In order to be able to apply `proj` to each element in [first, last)
- // we are dispatching to std::replace_copy_if instead of std::replace_copy.
- std::replace_copy_if(
- first, last, result,
- [&proj, &old_value](auto&& lhs) {
- return invoke(proj, std::forward<decltype(lhs)>(lhs)) == old_value;
- },
- new_value);
- return last;
- }
- // Let `E(i)` be
- // `bool(invoke(proj, *(begin(range) + (i - result))) == old_value)`.
- //
- // Mandates: The results of the expressions `*begin(range)` and `new_value` are
- // writable to `result`.
- //
- // Preconditions: The ranges `range` and `[result, result + size(range))` do not
- // overlap.
- //
- // Effects: Assigns through every iterator `i` in the range `[result, result +
- // size(range))` a new corresponding value, `new_value` if `E(i)` is true, or
- // `*(begin(range) + (i - result))` otherwise.
- //
- // Returns: `result + size(range)`.
- //
- // Complexity: Exactly `size(range)` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_copy(R
- template <typename Range,
- typename OutputIterator,
- typename T,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto replace_copy(Range&& range,
- OutputIterator result,
- const T& old_value,
- const T& new_value,
- Proj proj = {}) {
- return ranges::replace_copy(ranges::begin(range), ranges::end(range), result,
- old_value, new_value, std::move(proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *(first + (i - result)))))`.
- //
- // Mandates: The results of the expressions `*first` and `new_value` are
- // writable to `result`.
- //
- // Preconditions: The ranges `[first, last)` and `[result, result + (last -
- // first))` do not overlap.
- //
- // Effects: Assigns through every iterator `i` in the range `[result, result +
- // (last - first))` a new corresponding value, `new_value` if `E(i)` is true, or
- // `*(first + (i - result))` otherwise.
- //
- // Returns: `result + (last - first)`.
- //
- // Complexity: Exactly `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_copy_if(I
- template <typename InputIterator,
- typename OutputIterator,
- typename Predicate,
- typename T,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto replace_copy_if(InputIterator first,
- InputIterator last,
- OutputIterator result,
- Predicate pred,
- const T& new_value,
- Proj proj = {}) {
- return std::replace_copy_if(first, last, result,
- internal::ProjectedUnaryPredicate(pred, proj),
- new_value);
- }
- // Let `E(i)` be
- // `bool(invoke(pred, invoke(proj, *(begin(range) + (i - result)))))`.
- //
- // Mandates: The results of the expressions `*begin(range)` and `new_value` are
- // writable to `result`.
- //
- // Preconditions: The ranges `range` and `[result, result + size(range))` do not
- // overlap.
- //
- // Effects: Assigns through every iterator `i` in the range `[result, result +
- // size(range))` a new corresponding value, `new_value` if `E(i)` is true, or
- // `*(begin(range) + (i - result))` otherwise.
- //
- // Returns: `result + size(range)`.
- //
- // Complexity: Exactly `size(range)` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.replace#:~:text=ranges::replace_copy_if(R
- template <typename Range,
- typename OutputIterator,
- typename Predicate,
- typename T,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto replace_copy_if(Range&& range,
- OutputIterator result,
- Predicate pred,
- const T& new_value,
- Proj proj = {}) {
- return ranges::replace_copy_if(ranges::begin(range), ranges::end(range),
- result, pred, new_value, std::move(proj));
- }
- // [alg.fill] Fill
- // Reference: https://wg21.link/alg.fill
- // Let `N` be `last - first`.
- //
- // Mandates: The expression `value` is writable to the output iterator.
- //
- // Effects: Assigns `value` through all the iterators in the range
- // `[first, last)`.
- //
- // Returns: `last`.
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.fill#:~:text=ranges::fill(O
- template <typename OutputIterator,
- typename T,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto fill(OutputIterator first, OutputIterator last, const T& value) {
- std::fill(first, last, value);
- return last;
- }
- // Let `N` be `size(range)`.
- //
- // Mandates: The expression `value` is writable to the output iterator.
- //
- // Effects: Assigns `value` through all the iterators in `range`.
- //
- // Returns: `end(range)`.
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.fill#:~:text=ranges::fill(R
- template <typename Range,
- typename T,
- typename = internal::range_category_t<Range>>
- constexpr auto fill(Range&& range, const T& value) {
- return ranges::fill(ranges::begin(range), ranges::end(range), value);
- }
- // Let `N` be `max(0, n)`.
- //
- // Mandates: The expression `value` is writable to the output iterator.
- // The type `Size` is convertible to an integral type.
- //
- // Effects: Assigns `value` through all the iterators in `[first, first + N)`.
- //
- // Returns: `first + N`.
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.fill#:~:text=ranges::fill_n(O
- template <typename OutputIterator,
- typename Size,
- typename T,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto fill_n(OutputIterator first, Size n, const T& value) {
- return std::fill_n(first, n, value);
- }
- // [alg.generate] Generate
- // Reference: https://wg21.link/alg.generate
- // Let `N` be `last - first`.
- //
- // Effects: Assigns the result of successive evaluations of gen() through each
- // iterator in the range `[first, last)`.
- //
- // Returns: `last`.
- //
- // Complexity: Exactly `N` evaluations of `gen()` and assignments.
- //
- // Reference: https://wg21.link/alg.generate#:~:text=ranges::generate(O
- template <typename OutputIterator,
- typename Generator,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto generate(OutputIterator first,
- OutputIterator last,
- Generator gen) {
- std::generate(first, last, std::move(gen));
- return last;
- }
- // Let `N` be `size(range)`.
- //
- // Effects: Assigns the result of successive evaluations of gen() through each
- // iterator in `range`.
- //
- // Returns: `end(range)`.
- //
- // Complexity: Exactly `N` evaluations of `gen()` and assignments.
- //
- // Reference: https://wg21.link/alg.generate#:~:text=ranges::generate(R
- template <typename Range,
- typename Generator,
- typename = internal::range_category_t<Range>>
- constexpr auto generate(Range&& range, Generator gen) {
- return ranges::generate(ranges::begin(range), ranges::end(range),
- std::move(gen));
- }
- // Let `N` be `max(0, n)`.
- //
- // Mandates: `Size` is convertible to an integral type.
- //
- // Effects: Assigns the result of successive evaluations of gen() through each
- // iterator in the range `[first, first + N)`.
- //
- // Returns: `first + N`.
- //
- // Complexity: Exactly `N` evaluations of `gen()` and assignments.
- //
- // Reference: https://wg21.link/alg.generate#:~:text=ranges::generate_n(O
- template <typename OutputIterator,
- typename Size,
- typename Generator,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto generate_n(OutputIterator first, Size n, Generator gen) {
- return std::generate_n(first, n, std::move(gen));
- }
- // [alg.remove] Remove
- // Reference: https://wg21.link/alg.remove
- // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
- //
- // Effects: Eliminates all the elements referred to by iterator `i` in the range
- // `[first, last)` for which `E(i)` holds.
- //
- // Returns: The end of the resulting range.
- //
- // Remarks: Stable.
- //
- // Complexity: Exactly `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove(I
- template <typename ForwardIterator,
- typename T,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto remove(ForwardIterator first,
- ForwardIterator last,
- const T& value,
- Proj proj = {}) {
- // Note: In order to be able to apply `proj` to each element in [first, last)
- // we are dispatching to std::remove_if instead of std::remove.
- return std::remove_if(first, last, [&proj, &value](auto&& lhs) {
- return invoke(proj, std::forward<decltype(lhs)>(lhs)) == value;
- });
- }
- // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
- //
- // Effects: Eliminates all the elements referred to by iterator `i` in `range`
- // for which `E(i)` holds.
- //
- // Returns: The end of the resulting range.
- //
- // Remarks: Stable.
- //
- // Complexity: Exactly `size(range)` applications of the corresponding predicate
- // and any projection.
- //
- // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove(R
- template <typename Range,
- typename T,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto remove(Range&& range, const T& value, Proj proj = {}) {
- return ranges::remove(ranges::begin(range), ranges::end(range), value,
- std::move(proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
- //
- // Effects: Eliminates all the elements referred to by iterator `i` in the range
- // `[first, last)` for which `E(i)` holds.
- //
- // Returns: The end of the resulting range.
- //
- // Remarks: Stable.
- //
- // Complexity: Exactly `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_if(I
- template <typename ForwardIterator,
- typename Predicate,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto remove_if(ForwardIterator first,
- ForwardIterator last,
- Predicate pred,
- Proj proj = {}) {
- return std::remove_if(first, last,
- internal::ProjectedUnaryPredicate(pred, proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
- //
- // Effects: Eliminates all the elements referred to by iterator `i` in `range`.
- //
- // Returns: The end of the resulting range.
- //
- // Remarks: Stable.
- //
- // Complexity: Exactly `size(range)` applications of the corresponding predicate
- // and any projection.
- //
- // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_if(R
- template <typename Range,
- typename Predicate,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto remove_if(Range&& range, Predicate pred, Proj proj = {}) {
- return ranges::remove_if(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
- //
- // Let `N` be the number of elements in `[first, last)` for which `E(i)` is
- // false.
- //
- // Mandates: `*first` is writable to `result`.
- //
- // Preconditions: The ranges `[first, last)` and `[result, result + (last -
- // first))` do not overlap.
- //
- // Effects: Copies all the elements referred to by the iterator `i` in the range
- // `[first, last)` for which `E(i)` is false.
- //
- // Returns: `result + N`.
- //
- // Complexity: Exactly `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_copy(I
- template <typename InputIterator,
- typename OutputIterator,
- typename T,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto remove_copy(InputIterator first,
- InputIterator last,
- OutputIterator result,
- const T& value,
- Proj proj = {}) {
- // Note: In order to be able to apply `proj` to each element in [first, last)
- // we are dispatching to std::remove_copy_if instead of std::remove_copy.
- return std::remove_copy_if(first, last, result, [&proj, &value](auto&& lhs) {
- return invoke(proj, std::forward<decltype(lhs)>(lhs)) == value;
- });
- }
- // Let `E(i)` be `bool(invoke(proj, *i) == value)`.
- //
- // Let `N` be the number of elements in `range` for which `E(i)` is false.
- //
- // Mandates: `*begin(range)` is writable to `result`.
- //
- // Preconditions: The ranges `range` and `[result, result + size(range))` do not
- // overlap.
- //
- // Effects: Copies all the elements referred to by the iterator `i` in `range`
- // for which `E(i)` is false.
- //
- // Returns: `result + N`.
- //
- // Complexity: Exactly `size(range)` applications of the corresponding
- // predicate and any projection.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_copy(R
- template <typename Range,
- typename OutputIterator,
- typename T,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto remove_copy(Range&& range,
- OutputIterator result,
- const T& value,
- Proj proj = {}) {
- return ranges::remove_copy(ranges::begin(range), ranges::end(range), result,
- value, std::move(proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
- //
- // Let `N` be the number of elements in `[first, last)` for which `E(i)` is
- // false.
- //
- // Mandates: `*first` is writable to `result`.
- //
- // Preconditions: The ranges `[first, last)` and `[result, result + (last -
- // first))` do not overlap.
- //
- // Effects: Copies all the elements referred to by the iterator `i` in the range
- // `[first, last)` for which `E(i)` is false.
- //
- // Returns: `result + N`.
- //
- // Complexity: Exactly `last - first` applications of the corresponding
- // predicate and any projection.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_copy_if(I
- template <typename InputIterator,
- typename OutputIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto remove_copy_if(InputIterator first,
- InputIterator last,
- OutputIterator result,
- Pred pred,
- Proj proj = {}) {
- return std::remove_copy_if(first, last, result,
- internal::ProjectedUnaryPredicate(pred, proj));
- }
- // Let `E(i)` be `bool(invoke(pred, invoke(proj, *i)))`.
- //
- // Let `N` be the number of elements in `range` for which `E(i)` is false.
- //
- // Mandates: `*begin(range)` is writable to `result`.
- //
- // Preconditions: The ranges `range` and `[result, result + size(range))` do not
- // overlap.
- //
- // Effects: Copies all the elements referred to by the iterator `i` in `range`
- // for which `E(i)` is false.
- //
- // Returns: `result + N`.
- //
- // Complexity: Exactly `size(range)` applications of the corresponding
- // predicate and any projection.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/alg.remove#:~:text=ranges::remove_copy(R
- template <typename Range,
- typename OutputIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto remove_copy_if(Range&& range,
- OutputIterator result,
- Pred pred,
- Proj proj = {}) {
- return ranges::remove_copy_if(ranges::begin(range), ranges::end(range),
- result, std::move(pred), std::move(proj));
- }
- // [alg.unique] Unique
- // Reference: https://wg21.link/alg.unique
- // Let `E(i)` be `bool(invoke(comp, invoke(proj, *(i - 1)), invoke(proj, *i)))`.
- //
- // Effects: For a nonempty range, eliminates all but the first element from
- // every consecutive group of equivalent elements referred to by the iterator
- // `i` in the range `[first + 1, last)` for which `E(i)` is true.
- //
- // Returns: The end of the resulting range.
- //
- // Complexity: For nonempty ranges, exactly `(last - first) - 1` applications of
- // the corresponding predicate and no more than twice as many applications of
- // any projection.
- //
- // Reference: https://wg21.link/alg.unique#:~:text=ranges::unique(I
- template <typename ForwardIterator,
- typename Comp = ranges::equal_to,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>,
- typename = indirect_result_t<Comp&,
- projected<ForwardIterator, Proj>,
- projected<ForwardIterator, Proj>>>
- constexpr auto unique(ForwardIterator first,
- ForwardIterator last,
- Comp comp = {},
- Proj proj = {}) {
- return std::unique(first, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Let `E(i)` be `bool(invoke(comp, invoke(proj, *(i - 1)), invoke(proj, *i)))`.
- //
- // Effects: For a nonempty range, eliminates all but the first element from
- // every consecutive group of equivalent elements referred to by the iterator
- // `i` in the range `[begin(range) + 1, end(range))` for which `E(i)` is true.
- //
- // Returns: The end of the resulting range.
- //
- // Complexity: For nonempty ranges, exactly `size(range) - 1` applications of
- // the corresponding predicate and no more than twice as many applications of
- // any projection.
- //
- // Reference: https://wg21.link/alg.unique#:~:text=ranges::unique(R
- template <typename Range,
- typename Comp = ranges::equal_to,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto unique(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::unique(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // Let `E(i)` be `bool(invoke(comp, invoke(proj, *i), invoke(proj, *(i - 1))))`.
- //
- // Mandates: `*first` is writable to `result`.
- //
- // Preconditions: The ranges `[first, last)` and
- // `[result, result + (last - first))` do not overlap.
- //
- // Effects: Copies only the first element from every consecutive group of equal
- // elements referred to by the iterator `i` in the range `[first, last)` for
- // which `E(i)` holds.
- //
- // Returns: `result + N`.
- //
- // Complexity: Exactly `last - first - 1` applications of the corresponding
- // predicate and no more than twice as many applications of any projection.
- //
- // Reference: https://wg21.link/alg.unique#:~:text=ranges::unique_copy(I
- template <typename ForwardIterator,
- typename OutputIterator,
- typename Comp = ranges::equal_to,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto unique_copy(ForwardIterator first,
- ForwardIterator last,
- OutputIterator result,
- Comp comp = {},
- Proj proj = {}) {
- return std::unique_copy(first, last, result,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Let `E(i)` be `bool(invoke(comp, invoke(proj, *i), invoke(proj, *(i - 1))))`.
- //
- // Mandates: `*begin(range)` is writable to `result`.
- //
- // Preconditions: The ranges `range` and `[result, result + size(range))` do not
- // overlap.
- //
- // Effects: Copies only the first element from every consecutive group of equal
- // elements referred to by the iterator `i` in `range` for which `E(i)` holds.
- //
- // Returns: `result + N`.
- //
- // Complexity: Exactly `size(range) - 1` applications of the corresponding
- // predicate and no more than twice as many applications of any projection.
- //
- // Reference: https://wg21.link/alg.unique#:~:text=ranges::unique_copy(R
- template <typename Range,
- typename OutputIterator,
- typename Comp = ranges::equal_to,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto unique_copy(Range&& range,
- OutputIterator result,
- Comp comp = {},
- Proj proj = {}) {
- return ranges::unique_copy(ranges::begin(range), ranges::end(range), result,
- std::move(comp), std::move(proj));
- }
- // [alg.reverse] Reverse
- // Reference: https://wg21.link/alg.reverse
- // Effects: For each non-negative integer `i < (last - first) / 2`, applies
- // `std::iter_swap` to all pairs of iterators `first + i, (last - i) - 1`.
- //
- // Returns: `last`.
- //
- // Complexity: Exactly `(last - first)/2` swaps.
- //
- // Reference: https://wg21.link/alg.reverse#:~:text=ranges::reverse(I
- template <typename BidirectionalIterator,
- typename = internal::iterator_category_t<BidirectionalIterator>>
- constexpr auto reverse(BidirectionalIterator first,
- BidirectionalIterator last) {
- std::reverse(first, last);
- return last;
- }
- // Effects: For each non-negative integer `i < size(range) / 2`, applies
- // `std::iter_swap` to all pairs of iterators
- // `begin(range) + i, (end(range) - i) - 1`.
- //
- // Returns: `end(range)`.
- //
- // Complexity: Exactly `size(range)/2` swaps.
- //
- // Reference: https://wg21.link/alg.reverse#:~:text=ranges::reverse(R
- template <typename Range, typename = internal::range_category_t<Range>>
- constexpr auto reverse(Range&& range) {
- return ranges::reverse(ranges::begin(range), ranges::end(range));
- }
- // Let `N` be `last - first`.
- //
- // Preconditions: The ranges `[first, last)` and `[result, result + N)` do not
- // overlap.
- //
- // Effects: Copies the range `[first, last)` to the range `[result, result + N)`
- // such that for every non-negative integer `i < N` the following assignment
- // takes place: `*(result + N - 1 - i) = *(first + i)`.
- //
- // Returns: `result + N`.
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.reverse#:~:text=ranges::reverse_copy(I
- template <typename BidirectionalIterator,
- typename OutputIterator,
- typename = internal::iterator_category_t<BidirectionalIterator>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto reverse_copy(BidirectionalIterator first,
- BidirectionalIterator last,
- OutputIterator result) {
- return std::reverse_copy(first, last, result);
- }
- // Let `N` be `size(range)`.
- //
- // Preconditions: The ranges `range` and `[result, result + N)` do not
- // overlap.
- //
- // Effects: Copies `range` to the range `[result, result + N)` such that for
- // every non-negative integer `i < N` the following assignment takes place:
- // `*(result + N - 1 - i) = *(begin(range) + i)`.
- //
- // Returns: `result + N`.
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.reverse#:~:text=ranges::reverse_copy(R
- template <typename Range,
- typename OutputIterator,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto reverse_copy(Range&& range, OutputIterator result) {
- return ranges::reverse_copy(ranges::begin(range), ranges::end(range), result);
- }
- // [alg.rotate] Rotate
- // Reference: https://wg21.link/alg.rotate
- // Preconditions: `[first, middle)` and `[middle, last)` are valid ranges.
- //
- // Effects: For each non-negative integer `i < (last - first)`, places the
- // element from the position `first + i` into position
- // `first + (i + (last - middle)) % (last - first)`.
- //
- // Returns: `first + (last - middle)`.
- //
- // Complexity: At most `last - first` swaps.
- //
- // Reference: https://wg21.link/alg.rotate#:~:text=ranges::rotate(I
- template <typename ForwardIterator,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto rotate(ForwardIterator first,
- ForwardIterator middle,
- ForwardIterator last) {
- return std::rotate(first, middle, last);
- }
- // Preconditions: `[begin(range), middle)` and `[middle, end(range))` are valid
- // ranges.
- //
- // Effects: For each non-negative integer `i < size(range)`, places the element
- // from the position `begin(range) + i` into position
- // `begin(range) + (i + (end(range) - middle)) % size(range)`.
- //
- // Returns: `begin(range) + (end(range) - middle)`.
- //
- // Complexity: At most `size(range)` swaps.
- //
- // Reference: https://wg21.link/alg.rotate#:~:text=ranges::rotate(R
- template <typename Range, typename = internal::range_category_t<Range>>
- constexpr auto rotate(Range&& range, iterator_t<Range> middle) {
- return ranges::rotate(ranges::begin(range), middle, ranges::end(range));
- }
- // Let `N` be `last - first`.
- //
- // Preconditions: `[first, middle)` and `[middle, last)` are valid ranges. The
- // ranges `[first, last)` and `[result, result + N)` do not overlap.
- //
- // Effects: Copies the range `[first, last)` to the range `[result, result + N)`
- // such that for each non-negative integer `i < N` the following assignment
- // takes place: `*(result + i) = *(first + (i + (middle - first)) % N)`.
- //
- // Returns: `result + N`.
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.rotate#:~:text=ranges::rotate_copy(I
- template <typename ForwardIterator,
- typename OutputIterator,
- typename = internal::iterator_category_t<ForwardIterator>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto rotate_copy(ForwardIterator first,
- ForwardIterator middle,
- ForwardIterator last,
- OutputIterator result) {
- return std::rotate_copy(first, middle, last, result);
- }
- // Let `N` be `size(range)`.
- //
- // Preconditions: `[begin(range), middle)` and `[middle, end(range))` are valid
- // ranges. The ranges `range` and `[result, result + N)` do not overlap.
- //
- // Effects: Copies `range` to the range `[result, result + N)` such that for
- // each non-negative integer `i < N` the following assignment takes place:
- // `*(result + i) = *(begin(range) + (i + (middle - begin(range))) % N)`.
- //
- // Returns: `result + N`.
- //
- // Complexity: Exactly `N` assignments.
- //
- // Reference: https://wg21.link/alg.rotate#:~:text=ranges::rotate_copy(R
- template <typename Range,
- typename OutputIterator,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator>>
- constexpr auto rotate_copy(Range&& range,
- iterator_t<Range> middle,
- OutputIterator result) {
- return ranges::rotate_copy(ranges::begin(range), middle, ranges::end(range),
- result);
- }
- // [alg.random.sample] Sample
- // Reference: https://wg21.link/alg.random.sample
- // Currently not implemented due to lack of std::sample in C++14.
- // TODO(crbug.com/1071094): Consider implementing a hand-rolled version.
- // [alg.random.shuffle] Shuffle
- // Reference: https://wg21.link/alg.random.shuffle
- // Preconditions: The type `std::remove_reference_t<UniformRandomBitGenerator>`
- // meets the uniform random bit generator requirements.
- //
- // Effects: Permutes the elements in the range `[first, last)` such that each
- // possible permutation of those elements has equal probability of appearance.
- //
- // Returns: `last`.
- //
- // Complexity: Exactly `(last - first) - 1` swaps.
- //
- // Remarks: To the extent that the implementation of this function makes use of
- // random numbers, the object referenced by g shall serve as the
- // implementation's source of randomness.
- //
- // Reference: https://wg21.link/alg.random.shuffle#:~:text=ranges::shuffle(I
- template <typename RandomAccessIterator,
- typename UniformRandomBitGenerator,
- typename = internal::iterator_category_t<RandomAccessIterator>>
- constexpr auto shuffle(RandomAccessIterator first,
- RandomAccessIterator last,
- UniformRandomBitGenerator&& g) {
- std::shuffle(first, last, std::forward<UniformRandomBitGenerator>(g));
- return last;
- }
- // Preconditions: The type `std::remove_reference_t<UniformRandomBitGenerator>`
- // meets the uniform random bit generator requirements.
- //
- // Effects: Permutes the elements in `range` such that each possible permutation
- // of those elements has equal probability of appearance.
- //
- // Returns: `end(range)`.
- //
- // Complexity: Exactly `size(range) - 1` swaps.
- //
- // Remarks: To the extent that the implementation of this function makes use of
- // random numbers, the object referenced by g shall serve as the
- // implementation's source of randomness.
- //
- // Reference: https://wg21.link/alg.random.shuffle#:~:text=ranges::shuffle(R
- template <typename Range,
- typename UniformRandomBitGenerator,
- typename = internal::range_category_t<Range>>
- constexpr auto shuffle(Range&& range, UniformRandomBitGenerator&& g) {
- return ranges::shuffle(ranges::begin(range), ranges::end(range),
- std::forward<UniformRandomBitGenerator>(g));
- }
- // [alg.nonmodifying] Sorting and related operations
- // Reference: https://wg21.link/alg.sorting
- // [alg.sort] Sorting
- // Reference: https://wg21.link/alg.sort
- // [sort] sort
- // Reference: https://wg21.link/sort
- // Effects: Sorts the elements in the range `[first, last)` with respect to
- // `comp` and `proj`.
- //
- // Returns: `last`.
- //
- // Complexity: Let `N` be `last - first`. `O(N log N)` comparisons and
- // projections.
- //
- // Reference: https://wg21.link/sort#:~:text=ranges::sort(I
- template <typename RandomAccessIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<RandomAccessIterator>,
- typename = indirect_result_t<Comp&,
- projected<RandomAccessIterator, Proj>,
- projected<RandomAccessIterator, Proj>>>
- constexpr auto sort(RandomAccessIterator first,
- RandomAccessIterator last,
- Comp comp = {},
- Proj proj = {}) {
- std::sort(first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
- return last;
- }
- // Effects: Sorts the elements in `range` with respect to `comp` and `proj`.
- //
- // Returns: `end(range)`.
- //
- // Complexity: Let `N` be `size(range)`. `O(N log N)` comparisons and
- // projections.
- //
- // Reference: https://wg21.link/sort#:~:text=ranges::sort(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto sort(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::sort(ranges::begin(range), ranges::end(range), std::move(comp),
- std::move(proj));
- }
- // [stable.sort] stable_sort
- // Reference: https://wg21.link/stable.sort
- // Effects: Sorts the elements in the range `[first, last)` with respect to
- // `comp` and `proj`.
- //
- // Returns: `last`.
- //
- // Complexity: Let `N` be `last - first`. If enough extra memory is available,
- // `N log (N)` comparisons. Otherwise, at most `N log^2 (N)` comparisons. In
- // either case, twice as many projections as the number of comparisons.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/stable.sort#:~:text=ranges::stable_sort(I
- template <typename RandomAccessIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<RandomAccessIterator>,
- typename = indirect_result_t<Comp&,
- projected<RandomAccessIterator, Proj>,
- projected<RandomAccessIterator, Proj>>>
- constexpr auto stable_sort(RandomAccessIterator first,
- RandomAccessIterator last,
- Comp comp = {},
- Proj proj = {}) {
- std::stable_sort(first, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- return last;
- }
- // Effects: Sorts the elements in `range` with respect to `comp` and `proj`.
- //
- // Returns: `end(rang)`.
- //
- // Complexity: Let `N` be `size(range)`. If enough extra memory is available,
- // `N log (N)` comparisons. Otherwise, at most `N log^2 (N)` comparisons. In
- // either case, twice as many projections as the number of comparisons.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/stable.sort#:~:text=ranges::stable_sort(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto stable_sort(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::stable_sort(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // [partial.sort] partial_sort
- // Reference: https://wg21.link/partial.sort
- // Preconditions: `[first, middle)` and `[middle, last)` are valid ranges.
- //
- // Effects: Places the first `middle - first` elements from the range
- // `[first, last)` as sorted with respect to `comp` and `proj` into the range
- // `[first, middle)`. The rest of the elements in the range `[middle, last)` are
- // placed in an unspecified order.
- //
- // Returns: `last`.
- //
- // Complexity: Approximately `(last - first) * log(middle - first)` comparisons,
- // and twice as many projections.
- //
- // Reference: https://wg21.link/partial.sort#:~:text=ranges::partial_sort(I
- template <typename RandomAccessIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<RandomAccessIterator>,
- typename = indirect_result_t<Comp&,
- projected<RandomAccessIterator, Proj>,
- projected<RandomAccessIterator, Proj>>>
- constexpr auto partial_sort(RandomAccessIterator first,
- RandomAccessIterator middle,
- RandomAccessIterator last,
- Comp comp = {},
- Proj proj = {}) {
- std::partial_sort(first, middle, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- return last;
- }
- // Preconditions: `[begin(range), middle)` and `[middle, end(range))` are valid
- // ranges.
- //
- // Effects: Places the first `middle - begin(range)` elements from `range` as
- // sorted with respect to `comp` and `proj` into the range
- // `[begin(range), middle)`. The rest of the elements in the range
- // `[middle, end(range))` are placed in an unspecified order.
- //
- // Returns: `end(range)`.
- //
- // Complexity: Approximately `size(range) * log(middle - begin(range))`
- // comparisons, and twice as many projections.
- //
- // Reference: https://wg21.link/partial.sort#:~:text=ranges::partial_sort(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto partial_sort(Range&& range,
- iterator_t<Range> middle,
- Comp comp = {},
- Proj proj = {}) {
- return ranges::partial_sort(ranges::begin(range), middle, ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // [partial.sort.copy] partial_sort_copy
- // Reference: https://wg21.link/partial.sort.copy
- // Let `N` be `min(last - first, result_last - result_first)`.
- //
- // Preconditions: For iterators `a1` and `b1` in `[first, last)`, and iterators
- // `x2` and `y2` in `[result_first, result_last)`, after evaluating the
- // assignment `*y2 = *b1`, let `E` be the value of `bool(invoke(comp,
- // invoke(proj1, *a1), invoke(proj2, *y2)))`. Then, after evaluating the
- // assignment `*x2 = *a1`, `E` is equal to `bool(invoke(comp, invoke(proj2,
- // *x2), invoke(proj2, *y2)))`.
- //
- // Effects: Places the first `N` elements as sorted with respect to `comp` and
- // `proj2` into the range `[result_first, result_first + N)`.
- //
- // Returns: `result_first + N`.
- //
- // Complexity: Approximately `(last - first) * log N` comparisons, and twice as
- // many projections.
- //
- // Reference:
- // https://wg21.link/partial.sort.copy#:~:text=ranges::partial_sort_copy(I1
- template <typename InputIterator,
- typename RandomAccessIterator,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<InputIterator>,
- typename = internal::iterator_category_t<RandomAccessIterator>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator, Proj1>,
- projected<RandomAccessIterator, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<RandomAccessIterator, Proj2>,
- projected<InputIterator, Proj1>>>
- constexpr auto partial_sort_copy(InputIterator first,
- InputIterator last,
- RandomAccessIterator result_first,
- RandomAccessIterator result_last,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- // Needs to opt-in to all permutations, since std::partial_sort_copy expects
- // comp(proj2(lhs), proj1(rhs)) to compile.
- return std::partial_sort_copy(
- first, last, result_first, result_last,
- internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
- }
- // Let `N` be `min(size(range), size(result_range))`.
- //
- // Preconditions: For iterators `a1` and `b1` in `range`, and iterators
- // `x2` and `y2` in `result_range`, after evaluating the assignment
- // `*y2 = *b1`, let `E` be the value of
- // `bool(invoke(comp, invoke(proj1, *a1), invoke(proj2, *y2)))`. Then, after
- // evaluating the assignment `*x2 = *a1`, `E` is equal to
- // `bool(invoke(comp, invoke(proj2, *x2), invoke(proj2, *y2)))`.
- //
- // Effects: Places the first `N` elements as sorted with respect to `comp` and
- // `proj2` into the range `[begin(result_range), begin(result_range) + N)`.
- //
- // Returns: `begin(result_range) + N`.
- //
- // Complexity: Approximately `size(range) * log N` comparisons, and twice as
- // many projections.
- //
- // Reference:
- // https://wg21.link/partial.sort.copy#:~:text=ranges::partial_sort_copy(R1
- template <typename Range1,
- typename Range2,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range2>, Proj2>,
- projected<iterator_t<Range1>, Proj1>>>
- constexpr auto partial_sort_copy(Range1&& range,
- Range2&& result_range,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::partial_sort_copy(ranges::begin(range), ranges::end(range),
- ranges::begin(result_range),
- ranges::end(result_range), std::move(comp),
- std::move(proj1), std::move(proj2));
- }
- // [is.sorted] is_sorted
- // Reference: https://wg21.link/is.sorted
- // Returns: Whether the range `[first, last)` is sorted with respect to `comp`
- // and `proj`.
- //
- // Complexity: Linear.
- //
- // Reference: https://wg21.link/is.sorted#:~:text=ranges::is_sorted(I
- template <typename ForwardIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>,
- typename = indirect_result_t<Comp&,
- projected<ForwardIterator, Proj>,
- projected<ForwardIterator, Proj>>>
- constexpr auto is_sorted(ForwardIterator first,
- ForwardIterator last,
- Comp comp = {},
- Proj proj = {}) {
- return std::is_sorted(first, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Returns: Whether `range` is sorted with respect to `comp` and `proj`.
- //
- // Complexity: Linear.
- //
- // Reference: https://wg21.link/is.sorted#:~:text=ranges::is_sorted(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto is_sorted(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::is_sorted(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // Returns: The last iterator `i` in `[first, last]` for which the range
- // `[first, i)` is sorted with respect to `comp` and `proj`.
- //
- // Complexity: Linear.
- //
- // Reference: https://wg21.link/is.sorted#:~:text=ranges::is_sorted_until(I
- template <typename ForwardIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>,
- typename = indirect_result_t<Comp&,
- projected<ForwardIterator, Proj>,
- projected<ForwardIterator, Proj>>>
- constexpr auto is_sorted_until(ForwardIterator first,
- ForwardIterator last,
- Comp comp = {},
- Proj proj = {}) {
- return std::is_sorted_until(
- first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Returns: The last iterator `i` in `[begin(range), end(range)]` for which the
- // range `[begin(range), i)` is sorted with respect to `comp` and `proj`.
- //
- // Complexity: Linear.
- //
- // Reference: https://wg21.link/is.sorted#:~:text=ranges::is_sorted_until(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto is_sorted_until(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::is_sorted_until(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // [alg.nth.element] Nth element
- // Reference: https://wg21.link/alg.nth.element
- // Preconditions: `[first, nth)` and `[nth, last)` are valid ranges.
- //
- // Effects: After `nth_element` the element in the position pointed to by `nth`
- // is the element that would be in that position if the whole range were sorted
- // with respect to `comp` and `proj`, unless `nth == last`. Also for every
- // iterator `i` in the range `[first, nth)` and every iterator `j` in the range
- // `[nth, last)` it holds that:
- // `bool(invoke(comp, invoke(proj, *j), invoke(proj, *i)))` is false.
- //
- // Returns: `last`.
- //
- // Complexity: Linear on average.
- //
- // Reference: https://wg21.link/alg.nth.element#:~:text=ranges::nth_element(I
- template <typename RandomAccessIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<RandomAccessIterator>,
- typename = indirect_result_t<Comp&,
- projected<RandomAccessIterator, Proj>,
- projected<RandomAccessIterator, Proj>>>
- constexpr auto nth_element(RandomAccessIterator first,
- RandomAccessIterator nth,
- RandomAccessIterator last,
- Comp comp = {},
- Proj proj = {}) {
- std::nth_element(first, nth, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- return last;
- }
- // Preconditions: `[begin(range), nth)` and `[nth, end(range))` are valid
- // ranges.
- //
- // Effects: After `nth_element` the element in the position pointed to by `nth`
- // is the element that would be in that position if the whole range were sorted
- // with respect to `comp` and `proj`, unless `nth == end(range)`. Also for every
- // iterator `i` in the range `[begin(range), nth)` and every iterator `j` in the
- // range `[nth, end(range))` it holds that:
- // `bool(invoke(comp, invoke(proj, *j), invoke(proj, *i)))` is false.
- //
- // Returns: `end(range)`.
- //
- // Complexity: Linear on average.
- //
- // Reference: https://wg21.link/alg.nth.element#:~:text=ranges::nth_element(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto nth_element(Range&& range,
- iterator_t<Range> nth,
- Comp comp = {},
- Proj proj = {}) {
- return ranges::nth_element(ranges::begin(range), nth, ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // [alg.binary.search] Binary search
- // Reference: https://wg21.link/alg.binary.search
- // [lower.bound] lower_bound
- // Reference: https://wg21.link/lower.bound
- // Preconditions: The elements `e` of `[first, last)` are partitioned with
- // respect to the expression `bool(invoke(comp, invoke(proj, e), value))`.
- //
- // Returns: The furthermost iterator `i` in the range `[first, last]` such that
- // for every iterator `j` in the range `[first, i)`,
- // `bool(invoke(comp, invoke(proj, *j), value))` is true.
- //
- // Complexity: At most `log_2(last - first) + O(1)` comparisons and projections.
- //
- // Reference: https://wg21.link/lower.bound#:~:text=ranges::lower_bound(I
- template <typename ForwardIterator,
- typename T,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto lower_bound(ForwardIterator first,
- ForwardIterator last,
- const T& value,
- Comp comp = {},
- Proj proj = {}) {
- // The second arg is guaranteed to be `value`, so we'll simply apply the
- // identity projection.
- identity value_proj;
- return std::lower_bound(
- first, last, value,
- internal::ProjectedBinaryPredicate(comp, proj, value_proj));
- }
- // Preconditions: The elements `e` of `range` are partitioned with respect to
- // the expression `bool(invoke(comp, invoke(proj, e), value))`.
- //
- // Returns: The furthermost iterator `i` in the range
- // `[begin(range), end(range)]` such that for every iterator `j` in the range
- // `[begin(range), i)`, `bool(invoke(comp, invoke(proj, *j), value))` is true.
- //
- // Complexity: At most `log_2(size(range)) + O(1)` comparisons and projections.
- //
- // Reference: https://wg21.link/lower.bound#:~:text=ranges::lower_bound(R
- template <typename Range,
- typename T,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto lower_bound(Range&& range,
- const T& value,
- Comp comp = {},
- Proj proj = {}) {
- return ranges::lower_bound(ranges::begin(range), ranges::end(range), value,
- std::move(comp), std::move(proj));
- }
- // [upper.bound] upper_bound
- // Reference: https://wg21.link/upper.bound
- // Preconditions: The elements `e` of `[first, last)` are partitioned with
- // respect to the expression `!bool(invoke(comp, value, invoke(proj, e)))`.
- //
- // Returns: The furthermost iterator `i` in the range `[first, last]` such that
- // for every iterator `j` in the range `[first, i)`,
- // `!bool(invoke(comp, value, invoke(proj, *j)))` is true.
- //
- // Complexity: At most `log_2(last - first) + O(1)` comparisons and projections.
- //
- // Reference: https://wg21.link/upper.bound#:~:text=ranges::upper_bound(I
- template <typename ForwardIterator,
- typename T,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto upper_bound(ForwardIterator first,
- ForwardIterator last,
- const T& value,
- Comp comp = {},
- Proj proj = {}) {
- // The first arg is guaranteed to be `value`, so we'll simply apply the
- // identity projection.
- identity value_proj;
- return std::upper_bound(
- first, last, value,
- internal::ProjectedBinaryPredicate(comp, value_proj, proj));
- }
- // Preconditions: The elements `e` of `range` are partitioned with
- // respect to the expression `!bool(invoke(comp, value, invoke(proj, e)))`.
- //
- // Returns: The furthermost iterator `i` in the range
- // `[begin(range), end(range)]` such that for every iterator `j` in the range
- // `[begin(range), i)`, `!bool(invoke(comp, value, invoke(proj, *j)))` is true.
- //
- // Complexity: At most `log_2(size(range)) + O(1)` comparisons and projections.
- //
- // Reference: https://wg21.link/upper.bound#:~:text=ranges::upper_bound(R
- template <typename Range,
- typename T,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto upper_bound(Range&& range,
- const T& value,
- Comp comp = {},
- Proj proj = {}) {
- return ranges::upper_bound(ranges::begin(range), ranges::end(range), value,
- std::move(comp), std::move(proj));
- }
- // [equal.range] equal_range
- // Reference: https://wg21.link/equal.range
- // Preconditions: The elements `e` of `[first, last)` are partitioned with
- // respect to the expressions `bool(invoke(comp, invoke(proj, e), value))` and
- // `!bool(invoke(comp, value, invoke(proj, e)))`.
- //
- // Returns: `{ranges::lower_bound(first, last, value, comp, proj),
- // ranges::upper_bound(first, last, value, comp, proj)}`.
- //
- // Complexity: At most 2 ∗ log_2(last - first) + O(1) comparisons and
- // projections.
- //
- // Reference: https://wg21.link/equal.range#:~:text=ranges::equal_range(I
- template <typename ForwardIterator,
- typename T,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto equal_range(ForwardIterator first,
- ForwardIterator last,
- const T& value,
- Comp comp = {},
- Proj proj = {}) {
- // Note: This does not dispatch to std::equal_range, as otherwise it would not
- // be possible to prevent applying `proj` to `value`, which can result in
- // unintended behavior.
- return std::make_pair(ranges::lower_bound(first, last, value, comp, proj),
- ranges::upper_bound(first, last, value, comp, proj));
- }
- // Preconditions: The elements `e` of `range` are partitioned with
- // respect to the expressions `bool(invoke(comp, invoke(proj, e), value))` and
- // `!bool(invoke(comp, value, invoke(proj, e)))`.
- //
- // Returns: `{ranges::lower_bound(range, value, comp, proj),
- // ranges::upper_bound(range, value, comp, proj)}`.
- //
- // Complexity: At most 2 ∗ log_2(size(range)) + O(1) comparisons and
- // projections.
- //
- // Reference: https://wg21.link/equal.range#:~:text=ranges::equal_range(R
- template <typename Range,
- typename T,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto equal_range(Range&& range,
- const T& value,
- Comp comp = {},
- Proj proj = {}) {
- return ranges::equal_range(ranges::begin(range), ranges::end(range), value,
- std::move(comp), std::move(proj));
- }
- // [binary.search] binary_search
- // Reference: https://wg21.link/binary.search
- // Preconditions: The elements `e` of `[first, last)` are partitioned with
- // respect to the expressions `bool(invoke(comp, invoke(proj, e), value))` and
- // `!bool(invoke(comp, value, invoke(proj, e)))`.
- //
- // Returns: `true` if and only if for some iterator `i` in the range
- // `[first, last)`, `!bool(invoke(comp, invoke(proj, *i), value)) &&
- // !bool(invoke(comp, value, invoke(proj, *i)))` is true.
- //
- // Complexity: At most `log_2(last - first) + O(1)` comparisons and projections.
- //
- // Reference: https://wg21.link/binary.search#:~:text=ranges::binary_search(I
- template <typename ForwardIterator,
- typename T,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto binary_search(ForwardIterator first,
- ForwardIterator last,
- const T& value,
- Comp comp = {},
- Proj proj = {}) {
- first = ranges::lower_bound(first, last, value, comp, proj);
- return first != last && !invoke(comp, value, invoke(proj, *first));
- }
- // Preconditions: The elements `e` of `range` are partitioned with
- // respect to the expressions `bool(invoke(comp, invoke(proj, e), value))` and
- // `!bool(invoke(comp, value, invoke(proj, e)))`.
- //
- // Returns: `true` if and only if for some iterator `i` in `range`
- // `!bool(invoke(comp, invoke(proj, *i), value)) &&
- // !bool(invoke(comp, value, invoke(proj, *i)))` is true.
- //
- // Complexity: At most `log_2(size(range)) + O(1)` comparisons and projections.
- //
- // Reference: https://wg21.link/binary.search#:~:text=ranges::binary_search(R
- template <typename Range,
- typename T,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto binary_search(Range&& range,
- const T& value,
- Comp comp = {},
- Proj proj = {}) {
- return ranges::binary_search(ranges::begin(range), ranges::end(range), value,
- std::move(comp), std::move(proj));
- }
- // [alg.partitions] Partitions
- // Reference: https://wg21.link/alg.partitions
- // Returns: `true` if and only if the elements `e` of `[first, last)` are
- // partitioned with respect to the expression
- // `bool(invoke(pred, invoke(proj, e)))`.
- //
- // Complexity: Linear. At most `last - first` applications of `pred` and `proj`.
- //
- // Reference: https://wg21.link/alg.partitions#:~:text=ranges::is_partitioned(I
- template <typename ForwardIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto is_partitioned(ForwardIterator first,
- ForwardIterator last,
- Pred pred,
- Proj proj = {}) {
- return std::is_partitioned(first, last,
- internal::ProjectedUnaryPredicate(pred, proj));
- }
- // Returns: `true` if and only if the elements `e` of `range` are partitioned
- // with respect to the expression `bool(invoke(pred, invoke(proj, e)))`.
- //
- // Complexity: Linear. At most `size(range)` applications of `pred` and `proj`.
- //
- // Reference: https://wg21.link/alg.partitions#:~:text=ranges::is_partitioned(R
- template <typename Range,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto is_partitioned(Range&& range, Pred pred, Proj proj = {}) {
- return ranges::is_partitioned(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
- //
- // Effects: Places all the elements `e` in `[first, last)` that satisfy `E(e)`
- // before all the elements that do not.
- //
- // Returns: Let `i` be an iterator such that `E(*j)` is `true` for every
- // iterator `j` in `[first, i)` and `false` for every iterator `j` in
- // `[i, last)`. Returns: i.
- //
- // Complexity: Let `N = last - first`:
- // Exactly `N` applications of the predicate and projection. At most `N / 2`
- // swaps if the type of `first` models `bidirectional_iterator`, and at most `N`
- // swaps otherwise.
- //
- // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition(I
- template <typename ForwardIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto partition(ForwardIterator first,
- ForwardIterator last,
- Pred pred,
- Proj proj = {}) {
- return std::partition(first, last,
- internal::ProjectedUnaryPredicate(pred, proj));
- }
- // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
- //
- // Effects: Places all the elements `e` in `range` that satisfy `E(e)` before
- // all the elements that do not.
- //
- // Returns: Let `i` be an iterator such that `E(*j)` is `true` for every
- // iterator `j` in `[begin(range), i)` and `false` for every iterator `j` in
- // `[i, last)`. Returns: i.
- //
- // Complexity: Let `N = size(range)`:
- // Exactly `N` applications of the predicate and projection. At most `N / 2`
- // swaps if the type of `first` models `bidirectional_iterator`, and at most `N`
- // swaps otherwise.
- //
- // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition(R
- template <typename Range,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto partition(Range&& range, Pred pred, Proj proj = {}) {
- return ranges::partition(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
- //
- // Effects: Places all the elements `e` in `[first, last)` that satisfy `E(e)`
- // before all the elements that do not. The relative order of the elements in
- // both groups is preserved.
- //
- // Returns: Let `i` be an iterator such that for every iterator `j` in
- // `[first, i)`, `E(*j)` is `true`, and for every iterator `j` in the range
- // `[i, last)`, `E(*j)` is `false`. Returns: `i`.
- //
- // Complexity: Let `N = last - first`:
- // At most `N log N` swaps, but only `O(N)` swaps if there is enough extra
- // memory. Exactly `N` applications of the predicate and projection.
- //
- // Reference:
- // https://wg21.link/alg.partitions#:~:text=ranges::stable_partition(I
- template <typename BidirectionalIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<BidirectionalIterator>>
- constexpr auto stable_partition(BidirectionalIterator first,
- BidirectionalIterator last,
- Pred pred,
- Proj proj = {}) {
- return std::stable_partition(first, last,
- internal::ProjectedUnaryPredicate(pred, proj));
- }
- // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
- //
- // Effects: Places all the elements `e` in `range` that satisfy `E(e)` before
- // all the elements that do not. The relative order of the elements in both
- // groups is preserved.
- //
- // Returns: Let `i` be an iterator such that for every iterator `j` in
- // `[begin(range), i)`, `E(*j)` is `true`, and for every iterator `j` in the
- // range `[i, end(range))`, `E(*j)` is `false`. Returns: `i`.
- //
- // Complexity: Let `N = size(range)`:
- // At most `N log N` swaps, but only `O(N)` swaps if there is enough extra
- // memory. Exactly `N` applications of the predicate and projection.
- //
- // Reference:
- // https://wg21.link/alg.partitions#:~:text=ranges::stable_partition(R
- template <typename Range,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto stable_partition(Range&& range, Pred pred, Proj proj = {}) {
- return ranges::stable_partition(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
- //
- // Mandates: The expression `*first` is writable to `out_true` and `out_false`.
- //
- // Preconditions: The input range and output ranges do not overlap.
- //
- // Effects: For each iterator `i` in `[first, last)`, copies `*i` to the output
- // range beginning with `out_true` if `E(*i)` is `true`, or to the output range
- // beginning with `out_false` otherwise.
- //
- // Returns: Let `o1` be the end of the output range beginning at `out_true`, and
- // `o2` the end of the output range beginning at `out_false`.
- // Returns `{o1, o2}`.
- //
- // Complexity: Exactly `last - first` applications of `pred` and `proj`.
- //
- // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition_copy(I
- template <typename InputIterator,
- typename OutputIterator1,
- typename OutputIterator2,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<InputIterator>,
- typename = internal::iterator_category_t<OutputIterator1>,
- typename = internal::iterator_category_t<OutputIterator2>>
- constexpr auto partition_copy(InputIterator first,
- InputIterator last,
- OutputIterator1 out_true,
- OutputIterator2 out_false,
- Pred pred,
- Proj proj = {}) {
- return std::partition_copy(first, last, out_true, out_false,
- internal::ProjectedUnaryPredicate(pred, proj));
- }
- // Let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
- //
- // Mandates: The expression `*begin(range)` is writable to `out_true` and
- // `out_false`.
- //
- // Preconditions: The input range and output ranges do not overlap.
- //
- // Effects: For each iterator `i` in `range`, copies `*i` to the output range
- // beginning with `out_true` if `E(*i)` is `true`, or to the output range
- // beginning with `out_false` otherwise.
- //
- // Returns: Let `o1` be the end of the output range beginning at `out_true`, and
- // `o2` the end of the output range beginning at `out_false`.
- // Returns `{o1, o2}`.
- //
- // Complexity: Exactly `size(range)` applications of `pred` and `proj`.
- //
- // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition_copy(R
- template <typename Range,
- typename OutputIterator1,
- typename OutputIterator2,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = internal::iterator_category_t<OutputIterator1>,
- typename = internal::iterator_category_t<OutputIterator2>>
- constexpr auto partition_copy(Range&& range,
- OutputIterator1 out_true,
- OutputIterator2 out_false,
- Pred pred,
- Proj proj = {}) {
- return ranges::partition_copy(ranges::begin(range), ranges::end(range),
- out_true, out_false, std::move(pred),
- std::move(proj));
- }
- // let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
- //
- // Preconditions: The elements `e` of `[first, last)` are partitioned with
- // respect to `E(e)`.
- //
- // Returns: An iterator `mid` such that `E(*i)` is `true` for all iterators `i`
- // in `[first, mid)`, and `false` for all iterators `i` in `[mid, last)`.
- //
- // Complexity: `O(log(last - first))` applications of `pred` and `proj`.
- //
- // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition_point(I
- template <typename ForwardIterator,
- typename Pred,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>>
- constexpr auto partition_point(ForwardIterator first,
- ForwardIterator last,
- Pred pred,
- Proj proj = {}) {
- return std::partition_point(first, last,
- internal::ProjectedUnaryPredicate(pred, proj));
- }
- // let `E(x)` be `bool(invoke(pred, invoke(proj, x)))`.
- //
- // Preconditions: The elements `e` of `range` are partitioned with respect to
- // `E(e)`.
- //
- // Returns: An iterator `mid` such that `E(*i)` is `true` for all iterators `i`
- // in `[begin(range), mid)`, and `false` for all iterators `i` in
- // `[mid, end(range))`.
- //
- // Complexity: `O(log(size(range)))` applications of `pred` and `proj`.
- //
- // Reference: https://wg21.link/alg.partitions#:~:text=ranges::partition_point(R
- template <typename Range,
- typename Pred,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto partition_point(Range&& range, Pred pred, Proj proj = {}) {
- return ranges::partition_point(ranges::begin(range), ranges::end(range),
- std::move(pred), std::move(proj));
- }
- // [alg.merge] Merge
- // Reference: https://wg21.link/alg.merge
- // Let `N` be `(last1 - first1) + (last2 - first2)`.
- //
- // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
- // with respect to `comp` and `proj1` or `proj2`, respectively. The resulting
- // range does not overlap with either of the original ranges.
- //
- // Effects: Copies all the elements of the two ranges `[first1, last1)` and
- // `[first2, last2)` into the range `[result, result_last)`, where `result_last`
- // is `result + N`. If an element `a` precedes `b` in an input range, `a` is
- // copied into the output range before `b`. If `e1` is an element of
- // `[first1, last1)` and `e2` of `[first2, last2)`, `e2` is copied into the
- // output range before `e1` if and only if
- // `bool(invoke(comp, invoke(proj2, e2), invoke(proj1, e1)))` is `true`.
- //
- // Returns: `result_last`.
- //
- // Complexity: At most `N - 1` comparisons and applications of each projection.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/alg.merge#:~:text=ranges::merge(I1
- template <typename InputIterator1,
- typename InputIterator2,
- typename OutputIterator,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<InputIterator1>,
- typename = internal::iterator_category_t<InputIterator2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator1, Proj1>,
- projected<InputIterator2, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator2, Proj2>,
- projected<InputIterator1, Proj1>>>
- constexpr auto merge(InputIterator1 first1,
- InputIterator1 last1,
- InputIterator2 first2,
- InputIterator2 last2,
- OutputIterator result,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- // Needs to opt-in to all permutations, since std::merge expects
- // comp(proj2(lhs), proj1(rhs)) to compile.
- return std::merge(
- first1, last1, first2, last2, result,
- internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
- }
- // Let `N` be `size(range1) + size(range2)`.
- //
- // Preconditions: The ranges `range1` and `range2` are sorted with respect to
- // `comp` and `proj1` or `proj2`, respectively. The resulting range does not
- // overlap with either of the original ranges.
- //
- // Effects: Copies all the elements of the two ranges `range1` and `range2` into
- // the range `[result, result_last)`, where `result_last` is `result + N`. If an
- // element `a` precedes `b` in an input range, `a` is copied into the output
- // range before `b`. If `e1` is an element of `range1` and `e2` of `range2`,
- // `e2` is copied into the output range before `e1` if and only if
- // `bool(invoke(comp, invoke(proj2, e2), invoke(proj1, e1)))` is `true`.
- //
- // Returns: `result_last`.
- //
- // Complexity: At most `N - 1` comparisons and applications of each projection.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/alg.merge#:~:text=ranges::merge(R1
- template <typename Range1,
- typename Range2,
- typename OutputIterator,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range2>, Proj2>,
- projected<iterator_t<Range1>, Proj1>>>
- constexpr auto merge(Range1&& range1,
- Range2&& range2,
- OutputIterator result,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::merge(ranges::begin(range1), ranges::end(range1),
- ranges::begin(range2), ranges::end(range2), result,
- std::move(comp), std::move(proj1), std::move(proj2));
- }
- // Preconditions: `[first, middle)` and `[middle, last)` are valid ranges sorted
- // with respect to `comp` and `proj`.
- //
- // Effects: Merges two sorted consecutive ranges `[first, middle)` and
- // `[middle, last)`, putting the result of the merge into the range
- // `[first, last)`. The resulting range is sorted with respect to `comp` and
- // `proj`.
- //
- // Returns: `last`.
- //
- // Complexity: Let `N = last - first`: If enough additional memory is available,
- // exactly `N - 1` comparisons. Otherwise, `O(N log N)` comparisons. In either
- // case, twice as many projections as comparisons.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/alg.merge#:~:text=ranges::inplace_merge(I
- template <typename BidirectionalIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<BidirectionalIterator>>
- constexpr auto inplace_merge(BidirectionalIterator first,
- BidirectionalIterator middle,
- BidirectionalIterator last,
- Comp comp = {},
- Proj proj = {}) {
- std::inplace_merge(first, middle, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- return last;
- }
- // Preconditions: `[begin(range), middle)` and `[middle, end(range))` are valid
- // ranges sorted with respect to `comp` and `proj`.
- //
- // Effects: Merges two sorted consecutive ranges `[begin(range), middle)` and
- // `[middle, end(range))`, putting the result of the merge into `range`. The
- // resulting range is sorted with respect to `comp` and `proj`.
- //
- // Returns: `end(range)`.
- //
- // Complexity: Let `N = size(range)`: If enough additional memory is available,
- // exactly `N - 1` comparisons. Otherwise, `O(N log N)` comparisons. In either
- // case, twice as many projections as comparisons.
- //
- // Remarks: Stable.
- //
- // Reference: https://wg21.link/alg.merge#:~:text=ranges::inplace_merge(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto inplace_merge(Range&& range,
- iterator_t<Range> middle,
- Comp comp = {},
- Proj proj = {}) {
- return ranges::inplace_merge(ranges::begin(range), middle, ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // [alg.set.operations] Set operations on sorted structures
- // Reference: https://wg21.link/alg.set.operations
- // [includes] includes
- // Reference: https://wg21.link/includes
- // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
- // with respect to `comp` and `proj1` or `proj2`, respectively.
- //
- // Returns: `true` if and only if `[first2, last2)` is a subsequence of
- // `[first1, last1)`.
- //
- // Complexity: At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
- // comparisons and applications of each projection.
- //
- // Reference: https://wg21.link/includes#:~:text=ranges::includes(I1
- template <typename InputIterator1,
- typename InputIterator2,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<InputIterator1>,
- typename = internal::iterator_category_t<InputIterator2>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator1, Proj1>,
- projected<InputIterator2, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator2, Proj2>,
- projected<InputIterator1, Proj1>>>
- constexpr auto includes(InputIterator1 first1,
- InputIterator1 last1,
- InputIterator2 first2,
- InputIterator2 last2,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- // Needs to opt-in to all permutations, since std::includes expects
- // comp(proj1(lhs), proj2(rhs)) and comp(proj2(lhs), proj1(rhs)) to compile.
- return std::includes(
- first1, last1, first2, last2,
- internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
- }
- // Preconditions: The ranges `range1` and `range2` are sorted with respect to
- // `comp` and `proj1` or `proj2`, respectively.
- //
- // Returns: `true` if and only if `range2` is a subsequence of `range1`.
- //
- // Complexity: At most `2 * (size(range1) + size(range2)) - 1` comparisons and
- // applications of each projection.
- //
- // Reference: https://wg21.link/includes#:~:text=ranges::includes(R1
- template <typename Range1,
- typename Range2,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range2>, Proj2>,
- projected<iterator_t<Range1>, Proj1>>>
- constexpr auto includes(Range1&& range1,
- Range2&& range2,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::includes(ranges::begin(range1), ranges::end(range1),
- ranges::begin(range2), ranges::end(range2),
- std::move(comp), std::move(proj1), std::move(proj2));
- }
- // [set.union] set_union
- // Reference: https://wg21.link/set.union
- // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
- // with respect to `comp` and `proj1` or `proj2`, respectively. The resulting
- // range does not overlap with either of the original ranges.
- //
- // Effects: Constructs a sorted union of the elements from the two ranges; that
- // is, the set of elements that are present in one or both of the ranges.
- //
- // Returns: The end of the constructed range.
- //
- // Complexity: At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
- // comparisons and applications of each projection.
- //
- // Remarks: Stable. If `[first1, last1)` contains `m` elements that are
- // equivalent to each other and `[first2, last2)` contains `n` elements that are
- // equivalent to them, then all `m` elements from the first range are copied to
- // the output range, in order, and then the final `max(n - m , 0)` elements from
- // the second range are copied to the output range, in order.
- //
- // Reference: https://wg21.link/set.union#:~:text=ranges::set_union(I1
- template <typename InputIterator1,
- typename InputIterator2,
- typename OutputIterator,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<InputIterator1>,
- typename = internal::iterator_category_t<InputIterator2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator1, Proj1>,
- projected<InputIterator2, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator2, Proj2>,
- projected<InputIterator1, Proj1>>>
- constexpr auto set_union(InputIterator1 first1,
- InputIterator1 last1,
- InputIterator2 first2,
- InputIterator2 last2,
- OutputIterator result,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- // Needs to opt-in to all permutations, since std::set_union expects
- // comp(proj1(lhs), proj2(rhs)) and comp(proj2(lhs), proj1(rhs)) to compile.
- return std::set_union(
- first1, last1, first2, last2, result,
- internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
- }
- // Preconditions: The ranges `range1` and `range2` are sorted with respect to
- // `comp` and `proj1` or `proj2`, respectively. The resulting range does not
- // overlap with either of the original ranges.
- //
- // Effects: Constructs a sorted union of the elements from the two ranges; that
- // is, the set of elements that are present in one or both of the ranges.
- //
- // Returns: The end of the constructed range.
- //
- // Complexity: At most `2 * (size(range1) + size(range2)) - 1` comparisons and
- // applications of each projection.
- //
- // Remarks: Stable. If `range1` contains `m` elements that are equivalent to
- // each other and `range2` contains `n` elements that are equivalent to them,
- // then all `m` elements from the first range are copied to the output range, in
- // order, and then the final `max(n - m , 0)` elements from the second range are
- // copied to the output range, in order.
- //
- // Reference: https://wg21.link/set.union#:~:text=ranges::set_union(R1
- template <typename Range1,
- typename Range2,
- typename OutputIterator,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range2>, Proj2>,
- projected<iterator_t<Range1>, Proj1>>>
- constexpr auto set_union(Range1&& range1,
- Range2&& range2,
- OutputIterator result,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::set_union(ranges::begin(range1), ranges::end(range1),
- ranges::begin(range2), ranges::end(range2), result,
- std::move(comp), std::move(proj1), std::move(proj2));
- }
- // [set.intersection] set_intersection
- // Reference: https://wg21.link/set.intersection
- // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
- // with respect to `comp` and `proj1` or `proj2`, respectively. The resulting
- // range does not overlap with either of the original ranges.
- //
- // Effects: Constructs a sorted intersection of the elements from the two
- // ranges; that is, the set of elements that are present in both of the ranges.
- //
- // Returns: The end of the constructed range.
- //
- // Complexity: At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
- // comparisons and applications of each projection.
- //
- // Remarks: Stable. If `[first1, last1)` contains `m` elements that are
- // equivalent to each other and `[first2, last2)` contains `n` elements that are
- // equivalent to them, the first `min(m, n)` elements are copied from the first
- // range to the output range, in order.
- //
- // Reference:
- // https://wg21.link/set.intersection#:~:text=ranges::set_intersection(I1
- template <typename InputIterator1,
- typename InputIterator2,
- typename OutputIterator,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<InputIterator1>,
- typename = internal::iterator_category_t<InputIterator2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator1, Proj1>,
- projected<InputIterator2, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator2, Proj2>,
- projected<InputIterator1, Proj1>>>
- constexpr auto set_intersection(InputIterator1 first1,
- InputIterator1 last1,
- InputIterator2 first2,
- InputIterator2 last2,
- OutputIterator result,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- // Needs to opt-in to all permutations, since std::set_intersection expects
- // comp(proj1(lhs), proj2(rhs)) and comp(proj2(lhs), proj1(rhs)) to compile.
- return std::set_intersection(
- first1, last1, first2, last2, result,
- internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
- }
- // Preconditions: The ranges `range1` and `range2` are sorted with respect to
- // `comp` and `proj1` or `proj2`, respectively. The resulting range does not
- // overlap with either of the original ranges.
- //
- // Effects: Constructs a sorted intersection of the elements from the two
- // ranges; that is, the set of elements that are present in both of the ranges.
- //
- // Returns: The end of the constructed range.
- //
- // Complexity: At most `2 * (size(range1) + size(range2)) - 1` comparisons and
- // applications of each projection.
- //
- // Remarks: Stable. If `range1` contains `m` elements that are equivalent to
- // each other and `range2` contains `n` elements that are equivalent to them,
- // the first `min(m, n)` elements are copied from the first range to the output
- // range, in order.
- //
- // Reference:
- // https://wg21.link/set.intersection#:~:text=ranges::set_intersection(R1
- template <typename Range1,
- typename Range2,
- typename OutputIterator,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range2>, Proj2>,
- projected<iterator_t<Range1>, Proj1>>>
- constexpr auto set_intersection(Range1&& range1,
- Range2&& range2,
- OutputIterator result,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::set_intersection(ranges::begin(range1), ranges::end(range1),
- ranges::begin(range2), ranges::end(range2),
- result, std::move(comp), std::move(proj1),
- std::move(proj2));
- }
- // [set.difference] set_difference
- // Reference: https://wg21.link/set.difference
- // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
- // with respect to `comp` and `proj1` or `proj2`, respectively. The resulting
- // range does not overlap with either of the original ranges.
- //
- // Effects: Copies the elements of the range `[first1, last1)` which are not
- // present in the range `[first2, last2)` to the range beginning at `result`.
- // The elements in the constructed range are sorted.
- //
- // Returns: The end of the constructed range.
- //
- // Complexity: At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
- // comparisons and applications of each projection.
- //
- // Remarks: If `[first1, last1)` contains `m` elements that are equivalent to
- // each other and `[first2, last2)` contains `n` elements that are equivalent to
- // them, the last `max(m - n, 0)` elements from `[first1, last1)` are copied to
- // the output range, in order.
- //
- // Reference:
- // https://wg21.link/set.difference#:~:text=ranges::set_difference(I1
- template <typename InputIterator1,
- typename InputIterator2,
- typename OutputIterator,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<InputIterator1>,
- typename = internal::iterator_category_t<InputIterator2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator1, Proj1>,
- projected<InputIterator2, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator2, Proj2>,
- projected<InputIterator1, Proj1>>>
- constexpr auto set_difference(InputIterator1 first1,
- InputIterator1 last1,
- InputIterator2 first2,
- InputIterator2 last2,
- OutputIterator result,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- // Needs to opt-in to all permutations, since std::set_difference expects
- // comp(proj1(lhs), proj2(rhs)) and comp(proj2(lhs), proj1(rhs)) to compile.
- return std::set_difference(
- first1, last1, first2, last2, result,
- internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
- }
- // Preconditions: The ranges `range1` and `range2` are sorted with respect to
- // `comp` and `proj1` or `proj2`, respectively. The resulting range does not
- // overlap with either of the original ranges.
- //
- // Effects: Copies the elements of `range1` which are not present in `range2`
- // to the range beginning at `result`. The elements in the constructed range are
- // sorted.
- //
- // Returns: The end of the constructed range.
- //
- // Complexity: At most `2 * (size(range1) + size(range2)) - 1` comparisons and
- // applications of each projection.
- //
- // Remarks: Stable. If `range1` contains `m` elements that are equivalent to
- // each other and `range2` contains `n` elements that are equivalent to them,
- // the last `max(m - n, 0)` elements from `range1` are copied to the output
- // range, in order.
- //
- // Reference:
- // https://wg21.link/set.difference#:~:text=ranges::set_difference(R1
- template <typename Range1,
- typename Range2,
- typename OutputIterator,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range2>, Proj2>,
- projected<iterator_t<Range1>, Proj1>>>
- constexpr auto set_difference(Range1&& range1,
- Range2&& range2,
- OutputIterator result,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::set_difference(ranges::begin(range1), ranges::end(range1),
- ranges::begin(range2), ranges::end(range2),
- result, std::move(comp), std::move(proj1),
- std::move(proj2));
- }
- // [set.symmetric.difference] set_symmetric_difference
- // Reference: https://wg21.link/set.symmetric.difference
- // Preconditions: The ranges `[first1, last1)` and `[first2, last2)` are sorted
- // with respect to `comp` and `proj1` or `proj2`, respectively. The resulting
- // range does not overlap with either of the original ranges.
- //
- // Effects: Copies the elements of the range `[first1, last1)` that are not
- // present in the range `[first2, last2)`, and the elements of the range
- // `[first2, last2)` that are not present in the range `[first1, last1)` to the
- // range beginning at `result`. The elements in the constructed range are
- // sorted.
- //
- // Returns: The end of the constructed range.
- //
- // Complexity: At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
- // comparisons and applications of each projection.
- //
- // Remarks: Stable. If `[first1, last1)` contains `m` elements that are
- // equivalent to each other and `[first2, last2)` contains `n` elements that are
- // equivalent to them, then `|m - n|` of those elements shall be copied to the
- // output range: the last `m - n` of these elements from `[first1, last1)` if
- // `m > n`, and the last `n - m` of these elements from `[first2, last2)` if
- // `m < n`. In either case, the elements are copied in order.
- //
- // Reference:
- // https://wg21.link/set.symmetric.difference#:~:text=set_symmetric_difference(I1
- template <typename InputIterator1,
- typename InputIterator2,
- typename OutputIterator,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<InputIterator1>,
- typename = internal::iterator_category_t<InputIterator2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator1, Proj1>,
- projected<InputIterator2, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<InputIterator2, Proj2>,
- projected<InputIterator1, Proj1>>>
- constexpr auto set_symmetric_difference(InputIterator1 first1,
- InputIterator1 last1,
- InputIterator2 first2,
- InputIterator2 last2,
- OutputIterator result,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- // Needs to opt-in to all permutations, since std::set_symmetric_difference
- // expects comp(proj1(lhs), proj2(rhs)) and comp(proj2(lhs), proj1(rhs)) to
- // compile.
- return std::set_symmetric_difference(
- first1, last1, first2, last2, result,
- internal::PermutedProjectedBinaryPredicate(comp, proj1, proj2));
- }
- // Preconditions: The ranges `range1` and `range2` are sorted with respect to
- // `comp` and `proj1` or `proj2`, respectively. The resulting range does not
- // overlap with either of the original ranges.
- //
- // Effects: Copies the elements of `range1` that are not present in `range2`,
- // and the elements of `range2` that are not present in `range1` to the range
- // beginning at `result`. The elements in the constructed range are sorted.
- //
- // Returns: The end of the constructed range.
- //
- // Complexity: At most `2 * (size(range1) + size(range2)) - 1` comparisons and
- // applications of each projection.
- //
- // Remarks: Stable. If `range1` contains `m` elements that are equivalent to
- // each other and `range2` contains `n` elements that are equivalent to them,
- // then `|m - n|` of those elements shall be copied to the output range: the
- // last `m - n` of these elements from `range1` if `m > n`, and the last `n - m`
- // of these elements from `range2` if `m < n`. In either case, the elements are
- // copied in order.
- //
- // Reference:
- // https://wg21.link/set.symmetric.difference#:~:text=set_symmetric_difference(R1
- template <typename Range1,
- typename Range2,
- typename OutputIterator,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = internal::iterator_category_t<OutputIterator>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range2>, Proj2>,
- projected<iterator_t<Range1>, Proj1>>>
- constexpr auto set_symmetric_difference(Range1&& range1,
- Range2&& range2,
- OutputIterator result,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::set_symmetric_difference(
- ranges::begin(range1), ranges::end(range1), ranges::begin(range2),
- ranges::end(range2), result, std::move(comp), std::move(proj1),
- std::move(proj2));
- }
- // [alg.heap.operations] Heap operations
- // Reference: https://wg21.link/alg.heap.operations
- // [push.heap] push_heap
- // Reference: https://wg21.link/push.heap
- // Preconditions: The range `[first, last - 1)` is a valid heap with respect to
- // `comp` and `proj`.
- //
- // Effects: Places the value in the location `last - 1` into the resulting heap
- // `[first, last)`.
- //
- // Returns: `last`.
- //
- // Complexity: At most `log(last - first)` comparisons and twice as many
- // projections.
- //
- // Reference: https://wg21.link/push.heap#:~:text=ranges::push_heap(I
- template <typename RandomAccessIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<RandomAccessIterator>,
- typename = indirect_result_t<Comp&,
- projected<RandomAccessIterator, Proj>,
- projected<RandomAccessIterator, Proj>>>
- constexpr auto push_heap(RandomAccessIterator first,
- RandomAccessIterator last,
- Comp comp = {},
- Proj proj = {}) {
- std::push_heap(first, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- return last;
- }
- // Preconditions: The range `[begin(range), end(range) - 1)` is a valid heap
- // with respect to `comp` and `proj`.
- //
- // Effects: Places the value in the location `end(range) - 1` into the resulting
- // heap `range`.
- //
- // Returns: `end(range)`.
- //
- // Complexity: At most `log(size(range))` comparisons and twice as many
- // projections.
- //
- // Reference: https://wg21.link/push.heap#:~:text=ranges::push_heap(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto push_heap(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::push_heap(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // [pop.heap] pop_heap
- // Reference: https://wg21.link/pop.heap
- // Preconditions: The range `[first, last)` is a valid non-empty heap with
- // respect to `comp` and `proj`.
- //
- // Effects: Swaps the value in the location `first` with the value in the
- // location `last - 1` and makes `[first, last - 1)` into a heap with respect to
- // `comp` and `proj`.
- //
- // Returns: `last`.
- //
- // Complexity: At most `2 log(last - first)` comparisons and twice as many
- // projections.
- //
- // Reference: https://wg21.link/pop.heap#:~:text=ranges::pop_heap(I
- template <typename RandomAccessIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<RandomAccessIterator>,
- typename = indirect_result_t<Comp&,
- projected<RandomAccessIterator, Proj>,
- projected<RandomAccessIterator, Proj>>>
- constexpr auto pop_heap(RandomAccessIterator first,
- RandomAccessIterator last,
- Comp comp = {},
- Proj proj = {}) {
- std::pop_heap(first, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- return last;
- }
- // Preconditions: `range` is a valid non-empty heap with respect to `comp` and
- // `proj`.
- //
- // Effects: Swaps the value in the location `begin(range)` with the value in the
- // location `end(range) - 1` and makes `[begin(range), end(range) - 1)` into a
- // heap with respect to `comp` and `proj`.
- //
- // Returns: `end(range)`.
- //
- // Complexity: At most `2 log(size(range))` comparisons and twice as many
- // projections.
- //
- // Reference: https://wg21.link/pop.heap#:~:text=ranges::pop_heap(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto pop_heap(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::pop_heap(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // [make.heap] make_heap
- // Reference: https://wg21.link/make.heap
- // Effects: Constructs a heap with respect to `comp` and `proj` out of the range
- // `[first, last)`.
- //
- // Returns: `last`.
- //
- // Complexity: At most `3 log(last - first)` comparisons and twice as many
- // projections.
- //
- // Reference: https://wg21.link/make.heap#:~:text=ranges::make_heap(I
- template <typename RandomAccessIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<RandomAccessIterator>,
- typename = indirect_result_t<Comp&,
- projected<RandomAccessIterator, Proj>,
- projected<RandomAccessIterator, Proj>>>
- constexpr auto make_heap(RandomAccessIterator first,
- RandomAccessIterator last,
- Comp comp = {},
- Proj proj = {}) {
- std::make_heap(first, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- return last;
- }
- // Effects: Constructs a heap with respect to `comp` and `proj` out of `range`.
- //
- // Returns: `end(range)`.
- //
- // Complexity: At most `3 log(size(range))` comparisons and twice as many
- // projections.
- //
- // Reference: https://wg21.link/make.heap#:~:text=ranges::make_heap(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto make_heap(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::make_heap(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // [sort.heap] sort_heap
- // Reference: https://wg21.link/sort.heap
- // Preconditions: The range `[first, last)` is a valid heap with respect to
- // `comp` and `proj`.
- //
- // Effects: Sorts elements in the heap `[first, last)` with respect to `comp`
- // and `proj`.
- //
- // Returns: `last`.
- //
- // Complexity: At most `2 N log N` comparisons, where `N = last - first`, and
- // twice as many projections.
- //
- // Reference: https://wg21.link/sort.heap#:~:text=ranges::sort_heap(I
- template <typename RandomAccessIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<RandomAccessIterator>,
- typename = indirect_result_t<Comp&,
- projected<RandomAccessIterator, Proj>,
- projected<RandomAccessIterator, Proj>>>
- constexpr auto sort_heap(RandomAccessIterator first,
- RandomAccessIterator last,
- Comp comp = {},
- Proj proj = {}) {
- std::sort_heap(first, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- return last;
- }
- // Preconditions: `range` is a valid heap with respect to `comp` and `proj`.
- //
- // Effects: Sorts elements in the heap `range` with respect to `comp` and
- // `proj`.
- //
- // Returns: `end(range)`.
- //
- // Complexity: At most `2 N log N` comparisons, where `N = size(range)`, and
- // twice as many projections.
- //
- // Reference: https://wg21.link/sort.heap#:~:text=ranges::sort_heap(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto sort_heap(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::sort_heap(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // [is.heap] is_heap
- // Reference: https://wg21.link/is.heap
- // Returns: Whether the range `[first, last)` is a heap with respect to `comp`
- // and `proj`.
- //
- // Complexity: Linear.
- //
- // Reference: https://wg21.link/is.heap#:~:text=ranges::is_heap(I
- template <typename RandomAccessIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<RandomAccessIterator>,
- typename = indirect_result_t<Comp&,
- projected<RandomAccessIterator, Proj>,
- projected<RandomAccessIterator, Proj>>>
- constexpr auto is_heap(RandomAccessIterator first,
- RandomAccessIterator last,
- Comp comp = {},
- Proj proj = {}) {
- return std::is_heap(first, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Returns: Whether `range` is a heap with respect to `comp` and `proj`.
- //
- // Complexity: Linear.
- //
- // Reference: https://wg21.link/is.heap#:~:text=ranges::is_heap(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto is_heap(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::is_heap(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // Returns: The last iterator `i` in `[first, last]` for which the range
- // `[first, i)` is a heap with respect to `comp` and `proj`.
- //
- // Complexity: Linear.
- //
- // Reference: https://wg21.link/is.heap#:~:text=ranges::is_heap_until(I
- template <typename RandomAccessIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<RandomAccessIterator>,
- typename = indirect_result_t<Comp&,
- projected<RandomAccessIterator, Proj>,
- projected<RandomAccessIterator, Proj>>>
- constexpr auto is_heap_until(RandomAccessIterator first,
- RandomAccessIterator last,
- Comp comp = {},
- Proj proj = {}) {
- return std::is_heap_until(
- first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Returns: The last iterator `i` in `[begin(range), end(range)]` for which the
- // range `[begin(range), i)` is a heap with respect to `comp` and `proj`.
- //
- // Complexity: Linear.
- //
- // Reference: https://wg21.link/is.heap#:~:text=ranges::is_heap_until(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto is_heap_until(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::is_heap_until(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // [alg.min.max] Minimum and maximum
- // Reference: https://wg21.link/alg.min.max
- // Returns: The smaller value. Returns the first argument when the arguments are
- // equivalent.
- //
- // Complexity: Exactly one comparison and two applications of the projection, if
- // any.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::min
- template <typename T, typename Comp = ranges::less, typename Proj = identity>
- constexpr const T& min(const T& a, const T& b, Comp comp = {}, Proj proj = {}) {
- return invoke(comp, invoke(proj, b), invoke(proj, a)) ? b : a;
- }
- // Preconditions: `!empty(ilist)`.
- //
- // Returns: The smallest value in the input range. Returns a copy of the
- // leftmost element when several elements are equivalent to the smallest.
- //
- // Complexity: Exactly `size(ilist) - 1` comparisons and twice as many
- // applications of the projection, if any.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::min(initializer_list
- template <typename T, typename Comp = ranges::less, typename Proj = identity>
- constexpr T min(std::initializer_list<T> ilist,
- Comp comp = {},
- Proj proj = {}) {
- return *std::min_element(
- ilist.begin(), ilist.end(),
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Preconditions: `!empty(range)`.
- //
- // Returns: The smallest value in the input range. Returns a copy of the
- // leftmost element when several elements are equivalent to the smallest.
- //
- // Complexity: Exactly `size(range) - 1` comparisons and twice as many
- // applications of the projection, if any.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::min(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto min(Range&& range, Comp comp = {}, Proj proj = {}) {
- return *std::min_element(
- ranges::begin(range), ranges::end(range),
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Returns: The larger value. Returns the first argument when the arguments are
- // equivalent.
- //
- // Complexity: Exactly one comparison and two applications of the projection, if
- // any.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::max
- template <typename T, typename Comp = ranges::less, typename Proj = identity>
- constexpr const T& max(const T& a, const T& b, Comp comp = {}, Proj proj = {}) {
- return invoke(comp, invoke(proj, a), invoke(proj, b)) ? b : a;
- }
- // Preconditions: `!empty(ilist)`.
- //
- // Returns: The largest value in the input range. Returns a copy of the leftmost
- // element when several elements are equivalent to the largest.
- //
- // Complexity: Exactly `size(ilist) - 1` comparisons and twice as many
- // applications of the projection, if any.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::max(initializer_list
- template <typename T, typename Comp = ranges::less, typename Proj = identity>
- constexpr T max(std::initializer_list<T> ilist,
- Comp comp = {},
- Proj proj = {}) {
- return *std::max_element(
- ilist.begin(), ilist.end(),
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Preconditions: `!empty(range)`.
- //
- // Returns: The largest value in the input range. Returns a copy of the leftmost
- // element when several elements are equivalent to the smallest.
- //
- // Complexity: Exactly `size(range) - 1` comparisons and twice as many
- // applications of the projection, if any.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::max(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto max(Range&& range, Comp comp = {}, Proj proj = {}) {
- return *std::max_element(
- ranges::begin(range), ranges::end(range),
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Returns: `{b, a}` if `b` is smaller than `a`, and `{a, b}` otherwise.
- //
- // Complexity: Exactly one comparison and two applications of the projection, if
- // any.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::minmax
- template <typename T, typename Comp = ranges::less, typename Proj = identity>
- constexpr auto minmax(const T& a, const T& b, Comp comp = {}, Proj proj = {}) {
- return std::minmax(a, b,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Preconditions: `!empty(ilist)`.
- //
- // Returns: Let `X` be the return type. Returns `X{x, y}`, where `x` is a copy
- // of the leftmost element with the smallest value and `y` a copy of the
- // rightmost element with the largest value in the input range.
- //
- // Complexity: At most `(3/2) size(ilist)` applications of the corresponding
- // predicate and twice as many applications of the projection, if any.
- //
- // Reference:
- // https://wg21.link/alg.min.max#:~:text=ranges::minmax(initializer_list
- template <typename T, typename Comp = ranges::less, typename Proj = identity>
- constexpr auto minmax(std::initializer_list<T> ilist,
- Comp comp = {},
- Proj proj = {}) {
- auto it =
- std::minmax_element(ranges::begin(ilist), ranges::end(ilist),
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- return std::pair<T, T>{*it.first, *it.second};
- }
- // Preconditions: `!empty(range)`.
- //
- // Returns: Let `X` be the return type. Returns `X{x, y}`, where `x` is a copy
- // of the leftmost element with the smallest value and `y` a copy of the
- // rightmost element with the largest value in the input range.
- //
- // Complexity: At most `(3/2) size(range)` applications of the corresponding
- // predicate and twice as many applications of the projection, if any.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::minmax(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>>
- constexpr auto minmax(Range&& range, Comp comp = {}, Proj proj = {}) {
- using T = range_value_t<Range>;
- auto it =
- std::minmax_element(ranges::begin(range), ranges::end(range),
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- return std::pair<T, T>{*it.first, *it.second};
- }
- // Returns: The first iterator i in the range `[first, last)` such that for
- // every iterator `j` in the range `[first, last)`,
- // `bool(invoke(comp, invoke(proj, *j), invoke(proj, *i)))` is `false`. Returns
- // `last` if `first == last`.
- //
- // Complexity: Exactly `max(last - first - 1, 0)` comparisons and twice as
- // many projections.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::min_element(I
- template <typename ForwardIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>,
- typename = indirect_result_t<Comp&,
- projected<ForwardIterator, Proj>,
- projected<ForwardIterator, Proj>>>
- constexpr auto min_element(ForwardIterator first,
- ForwardIterator last,
- Comp comp = {},
- Proj proj = {}) {
- return std::min_element(first, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Returns: The first iterator i in `range` such that for every iterator `j` in
- // `range`, `bool(invoke(comp, invoke(proj, *j), invoke(proj, *i)))` is `false`.
- // Returns `end(range)` if `empty(range)`.
- //
- // Complexity: Exactly `max(size(range) - 1, 0)` comparisons and twice as many
- // projections.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::min_element(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto min_element(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::min_element(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // Returns: The first iterator i in the range `[first, last)` such that for
- // every iterator `j` in the range `[first, last)`,
- // `bool(invoke(comp, invoke(proj, *i), invoke(proj, *j)))` is `false`.
- // Returns `last` if `first == last`.
- //
- // Complexity: Exactly `max(last - first - 1, 0)` comparisons and twice as
- // many projections.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::max_element(I
- template <typename ForwardIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>,
- typename = indirect_result_t<Comp&,
- projected<ForwardIterator, Proj>,
- projected<ForwardIterator, Proj>>>
- constexpr auto max_element(ForwardIterator first,
- ForwardIterator last,
- Comp comp = {},
- Proj proj = {}) {
- return std::max_element(first, last,
- internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Returns: The first iterator i in `range` such that for every iterator `j`
- // in `range`, `bool(invoke(comp, invoke(proj, *j), invoke(proj, *j)))` is
- // `false`. Returns `end(range)` if `empty(range)`.
- //
- // Complexity: Exactly `max(size(range) - 1, 0)` comparisons and twice as many
- // projections.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::max_element(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto max_element(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::max_element(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // Returns: `{first, first}` if `[first, last)` is empty, otherwise `{m, M}`,
- // where `m` is the first iterator in `[first, last)` such that no iterator in
- // the range refers to a smaller element, and where `M` is the last iterator
- // in
- // `[first, last)` such that no iterator in the range refers to a larger
- // element.
- //
- // Complexity: Let `N` be `last - first`. At most `max(3/2 (N − 1), 0)`
- // comparisons and twice as many applications of the projection, if any.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::minmax_element(I
- template <typename ForwardIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<ForwardIterator>,
- typename = indirect_result_t<Comp&,
- projected<ForwardIterator, Proj>,
- projected<ForwardIterator, Proj>>>
- constexpr auto minmax_element(ForwardIterator first,
- ForwardIterator last,
- Comp comp = {},
- Proj proj = {}) {
- return std::minmax_element(
- first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Returns: `{begin(range), begin(range)}` if `range` is empty, otherwise
- // `{m, M}`, where `m` is the first iterator in `range` such that no iterator
- // in the range refers to a smaller element, and where `M` is the last
- // iterator in `range` such that no iterator in the range refers to a larger
- // element.
- //
- // Complexity: Let `N` be `size(range)`. At most `max(3/2 (N − 1), 0)`
- // comparisons and twice as many applications of the projection, if any.
- //
- // Reference: https://wg21.link/alg.min.max#:~:text=ranges::minmax_element(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto minmax_element(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::minmax_element(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // [alg.clamp] Bounded value
- // Reference: https://wg21.link/alg.clamp
- // Preconditions: `bool(invoke(comp, invoke(proj, hi), invoke(proj, lo)))` is
- // `false`.
- //
- // Returns: `lo` if `bool(invoke(comp, invoke(proj, v), invoke(proj, lo)))` is
- // `true`, `hi` if `bool(invoke(comp, invoke(proj, hi), invoke(proj, v)))` is
- // `true`, otherwise `v`.
- //
- // Complexity: At most two comparisons and three applications of the
- // projection.
- //
- // Reference: https://wg21.link/alg.clamp#:~:text=ranges::clamp
- template <typename T, typename Comp = ranges::less, typename Proj = identity>
- constexpr const T& clamp(const T& v,
- const T& lo,
- const T& hi,
- Comp comp = {},
- Proj proj = {}) {
- auto&& projected_v = invoke(proj, v);
- if (invoke(comp, projected_v, invoke(proj, lo)))
- return lo;
- return invoke(comp, invoke(proj, hi), projected_v) ? hi : v;
- }
- // [alg.lex.comparison] Lexicographical comparison
- // Reference: https://wg21.link/alg.lex.comparison
- // Returns: `true` if and only if the sequence of elements defined by the range
- // `[first1, last1)` is lexicographically less than the sequence of elements
- // defined by the range `[first2, last2)`.
- //
- // Complexity: At most `2 min(last1 - first1, last2 - first2)` applications of
- // the corresponding comparison and each projection, if any.
- //
- // Remarks: If two sequences have the same number of elements and their
- // corresponding elements (if any) are equivalent, then neither sequence is
- // lexicographically less than the other. If one sequence is a proper prefix of
- // the other, then the shorter sequence is lexicographically less than the
- // longer sequence. Otherwise, the lexicographical comparison of the sequences
- // yields the same result as the comparison of the first corresponding pair of
- // elements that are not equivalent.
- //
- // Reference:
- // https://wg21.link/alg.lex.comparison#:~:text=lexicographical_compare(I1
- template <typename ForwardIterator1,
- typename ForwardIterator2,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::iterator_category_t<ForwardIterator1>,
- typename = internal::iterator_category_t<ForwardIterator2>,
- typename = indirect_result_t<Comp&,
- projected<ForwardIterator1, Proj1>,
- projected<ForwardIterator2, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<ForwardIterator2, Proj2>,
- projected<ForwardIterator1, Proj1>>>
- constexpr bool lexicographical_compare(ForwardIterator1 first1,
- ForwardIterator1 last1,
- ForwardIterator2 first2,
- ForwardIterator2 last2,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- for (; first1 != last1 && first2 != last2; ++first1, ++first2) {
- auto&& projected_first1 = invoke(proj1, *first1);
- auto&& projected_first2 = invoke(proj2, *first2);
- if (invoke(comp, projected_first1, projected_first2))
- return true;
- if (invoke(comp, projected_first2, projected_first1))
- return false;
- }
- // `first2 != last2` is equivalent to `first1 == last1 && first2 != last2`
- // here, since we broke out of the loop above.
- return first2 != last2;
- }
- // Returns: `true` if and only if the sequence of elements defined by `range1`
- // is lexicographically less than the sequence of elements defined by `range2`.
- //
- // Complexity: At most `2 min(size(range1), size(range2))` applications of the
- // corresponding comparison and each projection, if any.
- //
- // Remarks: If two sequences have the same number of elements and their
- // corresponding elements (if any) are equivalent, then neither sequence is
- // lexicographically less than the other. If one sequence is a proper prefix of
- // the other, then the shorter sequence is lexicographically less than the
- // longer sequence. Otherwise, the lexicographical comparison of the sequences
- // yields the same result as the comparison of the first corresponding pair of
- // elements that are not equivalent.
- //
- // Reference:
- // https://wg21.link/alg.lex.comparison#:~:text=lexicographical_compare(R1
- template <typename Range1,
- typename Range2,
- typename Comp = ranges::less,
- typename Proj1 = identity,
- typename Proj2 = identity,
- typename = internal::range_category_t<Range1>,
- typename = internal::range_category_t<Range2>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range1>, Proj1>,
- projected<iterator_t<Range2>, Proj2>>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range2>, Proj2>,
- projected<iterator_t<Range1>, Proj1>>>
- constexpr bool lexicographical_compare(Range1&& range1,
- Range2&& range2,
- Comp comp = {},
- Proj1 proj1 = {},
- Proj2 proj2 = {}) {
- return ranges::lexicographical_compare(
- ranges::begin(range1), ranges::end(range1), ranges::begin(range2),
- ranges::end(range2), std::move(comp), std::move(proj1), std::move(proj2));
- }
- // [alg.permutation.generators] Permutation generators
- // Reference: https://wg21.link/alg.permutation.generators
- // Effects: Takes a sequence defined by the range `[first, last)` and transforms
- // it into the next permutation. The next permutation is found by assuming that
- // the set of all permutations is lexicographically sorted with respect to
- // `comp` and `proj`. If no such permutation exists, transforms the sequence
- // into the first permutation; that is, the ascendingly-sorted one.
- //
- // Returns: `true` if a next permutation was found and otherwise `false`.
- //
- // Complexity: At most `(last - first) / 2` swaps.
- //
- // Reference:
- // https://wg21.link/alg.permutation.generators#:~:text=next_permutation(I
- template <typename BidirectionalIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<BidirectionalIterator>,
- typename = indirect_result_t<Comp&,
- projected<BidirectionalIterator, Proj>,
- projected<BidirectionalIterator, Proj>>>
- constexpr auto next_permutation(BidirectionalIterator first,
- BidirectionalIterator last,
- Comp comp = {},
- Proj proj = {}) {
- return std::next_permutation(
- first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Effects: Takes a sequence defined by `range` and transforms it into the next
- // permutation. The next permutation is found by assuming that the set of all
- // permutations is lexicographically sorted with respect to `comp` and `proj`.
- // If no such permutation exists, transforms the sequence into the first
- // permutation; that is, the ascendingly-sorted one.
- //
- // Returns: `true` if a next permutation was found and otherwise `false`.
- //
- // Complexity: At most `size(range) / 2` swaps.
- //
- // Reference:
- // https://wg21.link/alg.permutation.generators#:~:text=next_permutation(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto next_permutation(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::next_permutation(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- // Effects: Takes a sequence defined by the range `[first, last)` and transforms
- // it into the previous permutation. The previous permutation is found by
- // assuming that the set of all permutations is lexicographically sorted with
- // respect to `comp` and `proj`. If no such permutation exists, transforms the
- // sequence into the last permutation; that is, the decreasingly-sorted one.
- //
- // Returns: `true` if a next permutation was found and otherwise `false`.
- //
- // Complexity: At most `(last - first) / 2` swaps.
- //
- // Reference:
- // https://wg21.link/alg.permutation.generators#:~:text=prev_permutation(I
- template <typename BidirectionalIterator,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::iterator_category_t<BidirectionalIterator>,
- typename = indirect_result_t<Comp&,
- projected<BidirectionalIterator, Proj>,
- projected<BidirectionalIterator, Proj>>>
- constexpr auto prev_permutation(BidirectionalIterator first,
- BidirectionalIterator last,
- Comp comp = {},
- Proj proj = {}) {
- return std::prev_permutation(
- first, last, internal::ProjectedBinaryPredicate(comp, proj, proj));
- }
- // Effects: Takes a sequence defined by `range` and transforms it into the
- // previous permutation. The previous permutation is found by assuming that the
- // set of all permutations is lexicographically sorted with respect to `comp`
- // and `proj`. If no such permutation exists, transforms the sequence into the
- // last permutation; that is, the decreasingly-sorted one.
- //
- // Returns: `true` if a previous permutation was found and otherwise `false`.
- //
- // Complexity: At most `size(range) / 2` swaps.
- //
- // Reference:
- // https://wg21.link/alg.permutation.generators#:~:text=prev_permutation(R
- template <typename Range,
- typename Comp = ranges::less,
- typename Proj = identity,
- typename = internal::range_category_t<Range>,
- typename = indirect_result_t<Comp&,
- projected<iterator_t<Range>, Proj>,
- projected<iterator_t<Range>, Proj>>>
- constexpr auto prev_permutation(Range&& range, Comp comp = {}, Proj proj = {}) {
- return ranges::prev_permutation(ranges::begin(range), ranges::end(range),
- std::move(comp), std::move(proj));
- }
- } // namespace ranges
- } // namespace base
- #endif // BASE_RANGES_ALGORITHM_H_
|