123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679 |
- // -*- C++ -*-
- //===-------------------------- algorithm ---------------------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is dual licensed under the MIT and the University of Illinois Open
- // Source Licenses. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- #ifndef _LIBCPP_ALGORITHM
- #define _LIBCPP_ALGORITHM
- /*
- algorithm synopsis
- #include <initializer_list>
- namespace std
- {
- template <class InputIterator, class Predicate>
- bool
- all_of(InputIterator first, InputIterator last, Predicate pred);
- template <class InputIterator, class Predicate>
- bool
- any_of(InputIterator first, InputIterator last, Predicate pred);
- template <class InputIterator, class Predicate>
- bool
- none_of(InputIterator first, InputIterator last, Predicate pred);
- template <class InputIterator, class Function>
- Function
- for_each(InputIterator first, InputIterator last, Function f);
- template <class InputIterator, class T>
- InputIterator
- find(InputIterator first, InputIterator last, const T& value);
- template <class InputIterator, class Predicate>
- InputIterator
- find_if(InputIterator first, InputIterator last, Predicate pred);
- template<class InputIterator, class Predicate>
- InputIterator
- find_if_not(InputIterator first, InputIterator last, Predicate pred);
- template <class ForwardIterator1, class ForwardIterator2>
- ForwardIterator1
- find_end(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2);
- template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
- ForwardIterator1
- find_end(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
- template <class ForwardIterator1, class ForwardIterator2>
- ForwardIterator1
- find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2);
- template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
- ForwardIterator1
- find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
- template <class ForwardIterator>
- ForwardIterator
- adjacent_find(ForwardIterator first, ForwardIterator last);
- template <class ForwardIterator, class BinaryPredicate>
- ForwardIterator
- adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
- template <class InputIterator, class T>
- typename iterator_traits<InputIterator>::difference_type
- count(InputIterator first, InputIterator last, const T& value);
- template <class InputIterator, class Predicate>
- typename iterator_traits<InputIterator>::difference_type
- count_if(InputIterator first, InputIterator last, Predicate pred);
- template <class InputIterator1, class InputIterator2>
- pair<InputIterator1, InputIterator2>
- mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
- template <class InputIterator1, class InputIterator2>
- pair<InputIterator1, InputIterator2>
- mismatch(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2); // **C++14**
- template <class InputIterator1, class InputIterator2, class BinaryPredicate>
- pair<InputIterator1, InputIterator2>
- mismatch(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, BinaryPredicate pred);
- template <class InputIterator1, class InputIterator2, class BinaryPredicate>
- pair<InputIterator1, InputIterator2>
- mismatch(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2,
- BinaryPredicate pred); // **C++14**
- template <class InputIterator1, class InputIterator2>
- bool
- equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
- template <class InputIterator1, class InputIterator2>
- bool
- equal(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2); // **C++14**
- template <class InputIterator1, class InputIterator2, class BinaryPredicate>
- bool
- equal(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, BinaryPredicate pred);
- template <class InputIterator1, class InputIterator2, class BinaryPredicate>
- bool
- equal(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2,
- BinaryPredicate pred); // **C++14**
- template<class ForwardIterator1, class ForwardIterator2>
- bool
- is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2);
- template<class ForwardIterator1, class ForwardIterator2>
- bool
- is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
- template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
- bool
- is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, BinaryPredicate pred);
- template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
- bool
- is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2,
- BinaryPredicate pred); // **C++14**
- template <class ForwardIterator1, class ForwardIterator2>
- ForwardIterator1
- search(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2);
- template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
- ForwardIterator1
- search(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
- template <class ForwardIterator, class Size, class T>
- ForwardIterator
- search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
- template <class ForwardIterator, class Size, class T, class BinaryPredicate>
- ForwardIterator
- search_n(ForwardIterator first, ForwardIterator last,
- Size count, const T& value, BinaryPredicate pred);
- template <class InputIterator, class OutputIterator>
- OutputIterator
- copy(InputIterator first, InputIterator last, OutputIterator result);
- template<class InputIterator, class OutputIterator, class Predicate>
- OutputIterator
- copy_if(InputIterator first, InputIterator last,
- OutputIterator result, Predicate pred);
- template<class InputIterator, class Size, class OutputIterator>
- OutputIterator
- copy_n(InputIterator first, Size n, OutputIterator result);
- template <class BidirectionalIterator1, class BidirectionalIterator2>
- BidirectionalIterator2
- copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
- BidirectionalIterator2 result);
- template <class ForwardIterator1, class ForwardIterator2>
- ForwardIterator2
- swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
- template <class ForwardIterator1, class ForwardIterator2>
- void
- iter_swap(ForwardIterator1 a, ForwardIterator2 b);
- template <class InputIterator, class OutputIterator, class UnaryOperation>
- OutputIterator
- transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
- OutputIterator
- transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
- OutputIterator result, BinaryOperation binary_op);
- template <class ForwardIterator, class T>
- void
- replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
- template <class ForwardIterator, class Predicate, class T>
- void
- replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
- template <class InputIterator, class OutputIterator, class T>
- OutputIterator
- replace_copy(InputIterator first, InputIterator last, OutputIterator result,
- const T& old_value, const T& new_value);
- template <class InputIterator, class OutputIterator, class Predicate, class T>
- OutputIterator
- replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
- template <class ForwardIterator, class T>
- void
- fill(ForwardIterator first, ForwardIterator last, const T& value);
- template <class OutputIterator, class Size, class T>
- OutputIterator
- fill_n(OutputIterator first, Size n, const T& value);
- template <class ForwardIterator, class Generator>
- void
- generate(ForwardIterator first, ForwardIterator last, Generator gen);
- template <class OutputIterator, class Size, class Generator>
- OutputIterator
- generate_n(OutputIterator first, Size n, Generator gen);
- template <class ForwardIterator, class T>
- ForwardIterator
- remove(ForwardIterator first, ForwardIterator last, const T& value);
- template <class ForwardIterator, class Predicate>
- ForwardIterator
- remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
- template <class InputIterator, class OutputIterator, class T>
- OutputIterator
- remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
- template <class InputIterator, class OutputIterator, class Predicate>
- OutputIterator
- remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
- template <class ForwardIterator>
- ForwardIterator
- unique(ForwardIterator first, ForwardIterator last);
- template <class ForwardIterator, class BinaryPredicate>
- ForwardIterator
- unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
- template <class InputIterator, class OutputIterator>
- OutputIterator
- unique_copy(InputIterator first, InputIterator last, OutputIterator result);
- template <class InputIterator, class OutputIterator, class BinaryPredicate>
- OutputIterator
- unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
- template <class BidirectionalIterator>
- void
- reverse(BidirectionalIterator first, BidirectionalIterator last);
- template <class BidirectionalIterator, class OutputIterator>
- OutputIterator
- reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
- template <class ForwardIterator>
- ForwardIterator
- rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
- template <class ForwardIterator, class OutputIterator>
- OutputIterator
- rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
- template <class RandomAccessIterator>
- void
- random_shuffle(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class RandomNumberGenerator>
- void
- random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator& rand);
- template<class RandomAccessIterator, class UniformRandomNumberGenerator>
- void shuffle(RandomAccessIterator first, RandomAccessIterator last,
- UniformRandomNumberGenerator&& g);
- template <class InputIterator, class Predicate>
- bool
- is_partitioned(InputIterator first, InputIterator last, Predicate pred);
- template <class ForwardIterator, class Predicate>
- ForwardIterator
- partition(ForwardIterator first, ForwardIterator last, Predicate pred);
- template <class InputIterator, class OutputIterator1,
- class OutputIterator2, class Predicate>
- pair<OutputIterator1, OutputIterator2>
- partition_copy(InputIterator first, InputIterator last,
- OutputIterator1 out_true, OutputIterator2 out_false,
- Predicate pred);
- template <class ForwardIterator, class Predicate>
- ForwardIterator
- stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
- template<class ForwardIterator, class Predicate>
- ForwardIterator
- partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
- template <class ForwardIterator>
- bool
- is_sorted(ForwardIterator first, ForwardIterator last);
- template <class ForwardIterator, class Compare>
- bool
- is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
- template<class ForwardIterator>
- ForwardIterator
- is_sorted_until(ForwardIterator first, ForwardIterator last);
- template <class ForwardIterator, class Compare>
- ForwardIterator
- is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- sort(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- stable_sort(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
- template <class InputIterator, class RandomAccessIterator>
- RandomAccessIterator
- partial_sort_copy(InputIterator first, InputIterator last,
- RandomAccessIterator result_first, RandomAccessIterator result_last);
- template <class InputIterator, class RandomAccessIterator, class Compare>
- RandomAccessIterator
- partial_sort_copy(InputIterator first, InputIterator last,
- RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
- template <class RandomAccessIterator>
- void
- nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
- template <class ForwardIterator, class T>
- ForwardIterator
- lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
- template <class ForwardIterator, class T, class Compare>
- ForwardIterator
- lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
- template <class ForwardIterator, class T>
- ForwardIterator
- upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
- template <class ForwardIterator, class T, class Compare>
- ForwardIterator
- upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
- template <class ForwardIterator, class T>
- pair<ForwardIterator, ForwardIterator>
- equal_range(ForwardIterator first, ForwardIterator last, const T& value);
- template <class ForwardIterator, class T, class Compare>
- pair<ForwardIterator, ForwardIterator>
- equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
- template <class ForwardIterator, class T>
- bool
- binary_search(ForwardIterator first, ForwardIterator last, const T& value);
- template <class ForwardIterator, class T, class Compare>
- bool
- binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
- template <class InputIterator1, class InputIterator2, class OutputIterator>
- OutputIterator
- merge(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
- OutputIterator
- merge(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
- template <class BidirectionalIterator>
- void
- inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
- template <class BidirectionalIterator, class Compare>
- void
- inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
- template <class InputIterator1, class InputIterator2>
- bool
- includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
- template <class InputIterator1, class InputIterator2, class Compare>
- bool
- includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
- template <class InputIterator1, class InputIterator2, class OutputIterator>
- OutputIterator
- set_union(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
- OutputIterator
- set_union(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
- template <class InputIterator1, class InputIterator2, class OutputIterator>
- OutputIterator
- set_intersection(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
- OutputIterator
- set_intersection(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
- template <class InputIterator1, class InputIterator2, class OutputIterator>
- OutputIterator
- set_difference(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
- OutputIterator
- set_difference(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
- template <class InputIterator1, class InputIterator2, class OutputIterator>
- OutputIterator
- set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
- OutputIterator
- set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
- template <class RandomAccessIterator>
- void
- push_heap(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- pop_heap(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- make_heap(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- sort_heap(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- bool
- is_heap(RandomAccessIterator first, RandomAccessiterator last);
- template <class RandomAccessIterator, class Compare>
- bool
- is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
- template <class RandomAccessIterator>
- RandomAccessIterator
- is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
- template <class RandomAccessIterator, class Compare>
- RandomAccessIterator
- is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
- template <class ForwardIterator>
- ForwardIterator
- min_element(ForwardIterator first, ForwardIterator last);
- template <class ForwardIterator, class Compare>
- ForwardIterator
- min_element(ForwardIterator first, ForwardIterator last, Compare comp);
- template <class T>
- const T&
- min(const T& a, const T& b);
- template <class T, class Compare>
- const T&
- min(const T& a, const T& b, Compare comp);
- template<class T>
- T
- min(initializer_list<T> t);
- template<class T, class Compare>
- T
- min(initializer_list<T> t, Compare comp);
- template <class ForwardIterator>
- ForwardIterator
- max_element(ForwardIterator first, ForwardIterator last);
- template <class ForwardIterator, class Compare>
- ForwardIterator
- max_element(ForwardIterator first, ForwardIterator last, Compare comp);
- template <class T>
- const T&
- max(const T& a, const T& b);
- template <class T, class Compare>
- const T&
- max(const T& a, const T& b, Compare comp);
- template<class T>
- T
- max(initializer_list<T> t);
- template<class T, class Compare>
- T
- max(initializer_list<T> t, Compare comp);
- template<class ForwardIterator>
- pair<ForwardIterator, ForwardIterator>
- minmax_element(ForwardIterator first, ForwardIterator last);
- template<class ForwardIterator, class Compare>
- pair<ForwardIterator, ForwardIterator>
- minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
- template<class T>
- pair<const T&, const T&>
- minmax(const T& a, const T& b);
- template<class T, class Compare>
- pair<const T&, const T&>
- minmax(const T& a, const T& b, Compare comp);
- template<class T>
- pair<T, T>
- minmax(initializer_list<T> t);
- template<class T, class Compare>
- pair<T, T>
- minmax(initializer_list<T> t, Compare comp);
- template <class InputIterator1, class InputIterator2>
- bool
- lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
- template <class InputIterator1, class InputIterator2, class Compare>
- bool
- lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, Compare comp);
- template <class BidirectionalIterator>
- bool
- next_permutation(BidirectionalIterator first, BidirectionalIterator last);
- template <class BidirectionalIterator, class Compare>
- bool
- next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
- template <class BidirectionalIterator>
- bool
- prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
- template <class BidirectionalIterator, class Compare>
- bool
- prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
- } // std
- */
- #include <__config>
- #include <initializer_list>
- #include <type_traits>
- #include <cstring>
- #include <utility>
- #include <memory>
- #include <iterator>
- #include <cstddef>
- #if defined(__IBMCPP__)
- #include "support/ibm/support.h"
- #endif
- #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
- #include "support/win32/support.h"
- #endif
- #include <__undef_min_max>
- #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
- #pragma GCC system_header
- #endif
- _LIBCPP_BEGIN_NAMESPACE_STD
- template <class _T1, class _T2 = _T1>
- struct __equal_to
- {
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
- };
- template <class _T1>
- struct __equal_to<_T1, _T1>
- {
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
- };
- template <class _T1>
- struct __equal_to<const _T1, _T1>
- {
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
- };
- template <class _T1>
- struct __equal_to<_T1, const _T1>
- {
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
- };
- template <class _T1, class _T2 = _T1>
- struct __less
- {
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
- };
- template <class _T1>
- struct __less<_T1, _T1>
- {
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
- };
- template <class _T1>
- struct __less<const _T1, _T1>
- {
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
- };
- template <class _T1>
- struct __less<_T1, const _T1>
- {
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
- };
- template <class _Predicate>
- class __negate
- {
- private:
- _Predicate __p_;
- public:
- _LIBCPP_INLINE_VISIBILITY __negate() {}
- _LIBCPP_INLINE_VISIBILITY
- explicit __negate(_Predicate __p) : __p_(__p) {}
- template <class _T1>
- _LIBCPP_INLINE_VISIBILITY
- bool operator()(const _T1& __x) {return !__p_(__x);}
- template <class _T1, class _T2>
- _LIBCPP_INLINE_VISIBILITY
- bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
- };
- #ifdef _LIBCPP_DEBUG
- template <class _Compare>
- struct __debug_less
- {
- _Compare __comp_;
- __debug_less(_Compare& __c) : __comp_(__c) {}
- template <class _Tp, class _Up>
- bool operator()(const _Tp& __x, const _Up& __y)
- {
- bool __r = __comp_(__x, __y);
- if (__r)
- _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
- return __r;
- }
- };
- #endif // _LIBCPP_DEBUG
- // Precondition: __x != 0
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned
- __ctz(unsigned __x)
- {
- return static_cast<unsigned>(__builtin_ctz(__x));
- }
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned long
- __ctz(unsigned long __x)
- {
- return static_cast<unsigned long>(__builtin_ctzl(__x));
- }
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned long long
- __ctz(unsigned long long __x)
- {
- return static_cast<unsigned long long>(__builtin_ctzll(__x));
- }
- // Precondition: __x != 0
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned
- __clz(unsigned __x)
- {
- return static_cast<unsigned>(__builtin_clz(__x));
- }
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned long
- __clz(unsigned long __x)
- {
- return static_cast<unsigned long>(__builtin_clzl (__x));
- }
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned long long
- __clz(unsigned long long __x)
- {
- return static_cast<unsigned long long>(__builtin_clzll(__x));
- }
- inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);}
- inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);}
- inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
- // all_of
- template <class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (!__pred(*__first))
- return false;
- return true;
- }
- // any_of
- template <class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- return true;
- return false;
- }
- // none_of
- template <class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- return false;
- return true;
- }
- // for_each
- template <class _InputIterator, class _Function>
- inline _LIBCPP_INLINE_VISIBILITY
- _Function
- for_each(_InputIterator __first, _InputIterator __last, _Function __f)
- {
- for (; __first != __last; ++__first)
- __f(*__first);
- return _VSTD::move(__f); // explicitly moved for (emulated) C++03
- }
- // find
- template <class _InputIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _InputIterator
- find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
- {
- for (; __first != __last; ++__first)
- if (*__first == __value_)
- break;
- return __first;
- }
- // find_if
- template <class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _InputIterator
- find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- break;
- return __first;
- }
- // find_if_not
- template<class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _InputIterator
- find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (!__pred(*__first))
- break;
- return __first;
- }
- // find_end
- template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
- _ForwardIterator1
- __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
- forward_iterator_tag, forward_iterator_tag)
- {
- // modeled after search algorithm
- _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
- if (__first2 == __last2)
- return __r;
- while (true)
- {
- while (true)
- {
- if (__first1 == __last1) // if source exhausted return last correct answer
- return __r; // (or __last1 if never found)
- if (__pred(*__first1, *__first2))
- break;
- ++__first1;
- }
- // *__first1 matches *__first2, now match elements after here
- _ForwardIterator1 __m1 = __first1;
- _ForwardIterator2 __m2 = __first2;
- while (true)
- {
- if (++__m2 == __last2)
- { // Pattern exhaused, record answer and search for another one
- __r = __first1;
- ++__first1;
- break;
- }
- if (++__m1 == __last1) // Source exhausted, return last answer
- return __r;
- if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
- {
- ++__first1;
- break;
- } // else there is a match, check next elements
- }
- }
- }
- template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
- _BidirectionalIterator1
- __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
- _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
- bidirectional_iterator_tag, bidirectional_iterator_tag)
- {
- // modeled after search algorithm (in reverse)
- if (__first2 == __last2)
- return __last1; // Everything matches an empty sequence
- _BidirectionalIterator1 __l1 = __last1;
- _BidirectionalIterator2 __l2 = __last2;
- --__l2;
- while (true)
- {
- // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
- while (true)
- {
- if (__first1 == __l1) // return __last1 if no element matches *__first2
- return __last1;
- if (__pred(*--__l1, *__l2))
- break;
- }
- // *__l1 matches *__l2, now match elements before here
- _BidirectionalIterator1 __m1 = __l1;
- _BidirectionalIterator2 __m2 = __l2;
- while (true)
- {
- if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
- return __m1;
- if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
- return __last1;
- if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
- {
- break;
- } // else there is a match, check next elements
- }
- }
- }
- template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
- _RandomAccessIterator1
- __find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
- _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
- random_access_iterator_tag, random_access_iterator_tag)
- {
- // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
- typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
- if (__len2 == 0)
- return __last1;
- typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
- if (__len1 < __len2)
- return __last1;
- const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
- _RandomAccessIterator1 __l1 = __last1;
- _RandomAccessIterator2 __l2 = __last2;
- --__l2;
- while (true)
- {
- while (true)
- {
- if (__s == __l1)
- return __last1;
- if (__pred(*--__l1, *__l2))
- break;
- }
- _RandomAccessIterator1 __m1 = __l1;
- _RandomAccessIterator2 __m2 = __l2;
- while (true)
- {
- if (__m2 == __first2)
- return __m1;
- // no need to check range on __m1 because __s guarantees we have enough source
- if (!__pred(*--__m1, *--__m2))
- {
- break;
- }
- }
- }
- }
- template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator1
- find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
- {
- return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __last2, __pred,
- typename iterator_traits<_ForwardIterator1>::iterator_category(),
- typename iterator_traits<_ForwardIterator2>::iterator_category());
- }
- template <class _ForwardIterator1, class _ForwardIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator1
- find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2)
- {
- typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
- typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
- return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
- }
- // find_first_of
- template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
- _ForwardIterator1
- find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
- {
- for (; __first1 != __last1; ++__first1)
- for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
- if (__pred(*__first1, *__j))
- return __first1;
- return __last1;
- }
- template <class _ForwardIterator1, class _ForwardIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator1
- find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2)
- {
- typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
- typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
- return _VSTD::find_first_of(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
- }
- // adjacent_find
- template <class _ForwardIterator, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
- {
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- {
- if (__pred(*__first, *__i))
- return __first;
- __first = __i;
- }
- }
- return __last;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
- {
- typedef typename iterator_traits<_ForwardIterator>::value_type __v;
- return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
- }
- // count
- template <class _InputIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename iterator_traits<_InputIterator>::difference_type
- count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
- {
- typename iterator_traits<_InputIterator>::difference_type __r(0);
- for (; __first != __last; ++__first)
- if (*__first == __value_)
- ++__r;
- return __r;
- }
- // count_if
- template <class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- typename iterator_traits<_InputIterator>::difference_type
- count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- typename iterator_traits<_InputIterator>::difference_type __r(0);
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- ++__r;
- return __r;
- }
- // mismatch
- template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_InputIterator1, _InputIterator2>
- mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _BinaryPredicate __pred)
- {
- for (; __first1 != __last1; ++__first1, ++__first2)
- if (!__pred(*__first1, *__first2))
- break;
- return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_InputIterator1, _InputIterator2>
- mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type __v1;
- typedef typename iterator_traits<_InputIterator2>::value_type __v2;
- return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
- }
- #if _LIBCPP_STD_VER > 11
- template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_InputIterator1, _InputIterator2>
- mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _BinaryPredicate __pred)
- {
- for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
- if (!__pred(*__first1, *__first2))
- break;
- return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_InputIterator1, _InputIterator2>
- mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type __v1;
- typedef typename iterator_traits<_InputIterator2>::value_type __v2;
- return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
- }
- #endif
- // equal
- template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
- {
- for (; __first1 != __last1; ++__first1, ++__first2)
- if (!__pred(*__first1, *__first2))
- return false;
- return true;
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type __v1;
- typedef typename iterator_traits<_InputIterator2>::value_type __v2;
- return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
- }
- #if _LIBCPP_STD_VER > 11
- template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- __equal(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
- input_iterator_tag, input_iterator_tag )
- {
- for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
- if (!__pred(*__first1, *__first2))
- return false;
- return __first1 == __last1 && __first2 == __last2;
- }
- template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
- _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
- random_access_iterator_tag, random_access_iterator_tag )
- {
- if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
- return false;
- return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
- typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __pred );
- }
- template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- equal(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
- {
- return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __last2, __pred,
- typename iterator_traits<_InputIterator1>::iterator_category(),
- typename iterator_traits<_InputIterator2>::iterator_category());
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- equal(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type __v1;
- typedef typename iterator_traits<_InputIterator2>::value_type __v2;
- return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
- typename iterator_traits<_InputIterator1>::iterator_category(),
- typename iterator_traits<_InputIterator2>::iterator_category());
- }
- #endif
- // is_permutation
- template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
- bool
- is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _BinaryPredicate __pred)
- {
- // shorten sequences as much as possible by lopping of any equal parts
- for (; __first1 != __last1; ++__first1, ++__first2)
- if (!__pred(*__first1, *__first2))
- goto __not_done;
- return true;
- __not_done:
- // __first1 != __last1 && *__first1 != *__first2
- typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
- _D1 __l1 = _VSTD::distance(__first1, __last1);
- if (__l1 == _D1(1))
- return false;
- _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
- // For each element in [f1, l1) see if there are the same number of
- // equal elements in [f2, l2)
- for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
- {
- // Have we already counted the number of *__i in [f1, l1)?
- for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
- if (__pred(*__j, *__i))
- goto __next_iter;
- {
- // Count number of *__i in [f2, l2)
- _D1 __c2 = 0;
- for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
- if (__pred(*__i, *__j))
- ++__c2;
- if (__c2 == 0)
- return false;
- // Count number of *__i in [__i, l1) (we can start with 1)
- _D1 __c1 = 1;
- for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
- if (__pred(*__i, *__j))
- ++__c1;
- if (__c1 != __c2)
- return false;
- }
- __next_iter:;
- }
- return true;
- }
- template<class _ForwardIterator1, class _ForwardIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2)
- {
- typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
- typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
- return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
- }
- #if _LIBCPP_STD_VER > 11
- template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
- bool
- __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2,
- _BinaryPredicate __pred,
- forward_iterator_tag, forward_iterator_tag )
- {
- // shorten sequences as much as possible by lopping of any equal parts
- for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
- if (!__pred(*__first1, *__first2))
- goto __not_done;
- return __first1 == __last1 && __first2 == __last2;
- __not_done:
- // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
- typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
- _D1 __l1 = _VSTD::distance(__first1, __last1);
- typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
- _D2 __l2 = _VSTD::distance(__first2, __last2);
- if (__l1 != __l2)
- return false;
- // For each element in [f1, l1) see if there are the same number of
- // equal elements in [f2, l2)
- for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
- {
- // Have we already counted the number of *__i in [f1, l1)?
- for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
- if (__pred(*__j, *__i))
- goto __next_iter;
- {
- // Count number of *__i in [f2, l2)
- _D1 __c2 = 0;
- for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
- if (__pred(*__i, *__j))
- ++__c2;
- if (__c2 == 0)
- return false;
- // Count number of *__i in [__i, l1) (we can start with 1)
- _D1 __c1 = 1;
- for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
- if (__pred(*__i, *__j))
- ++__c1;
- if (__c1 != __c2)
- return false;
- }
- __next_iter:;
- }
- return true;
- }
- template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
- bool
- __is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
- _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
- _BinaryPredicate __pred,
- random_access_iterator_tag, random_access_iterator_tag )
- {
- if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
- return false;
- return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
- typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __pred );
- }
- template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2,
- _BinaryPredicate __pred )
- {
- return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __last2, __pred,
- typename iterator_traits<_ForwardIterator1>::iterator_category(),
- typename iterator_traits<_ForwardIterator2>::iterator_category());
- }
- template<class _ForwardIterator1, class _ForwardIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2)
- {
- typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
- typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
- return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
- __equal_to<__v1, __v2>(),
- typename iterator_traits<_ForwardIterator1>::iterator_category(),
- typename iterator_traits<_ForwardIterator2>::iterator_category());
- }
- #endif
- // search
- template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
- _ForwardIterator1
- __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
- forward_iterator_tag, forward_iterator_tag)
- {
- if (__first2 == __last2)
- return __first1; // Everything matches an empty sequence
- while (true)
- {
- // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
- while (true)
- {
- if (__first1 == __last1) // return __last1 if no element matches *__first2
- return __last1;
- if (__pred(*__first1, *__first2))
- break;
- ++__first1;
- }
- // *__first1 matches *__first2, now match elements after here
- _ForwardIterator1 __m1 = __first1;
- _ForwardIterator2 __m2 = __first2;
- while (true)
- {
- if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
- return __first1;
- if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
- return __last1;
- if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
- {
- ++__first1;
- break;
- } // else there is a match, check next elements
- }
- }
- }
- template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
- _RandomAccessIterator1
- __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
- _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
- random_access_iterator_tag, random_access_iterator_tag)
- {
- typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
- typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
- // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
- _D2 __len2 = __last2 - __first2;
- if (__len2 == 0)
- return __first1;
- _D1 __len1 = __last1 - __first1;
- if (__len1 < __len2)
- return __last1;
- const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
- while (true)
- {
- #if !_LIBCPP_UNROLL_LOOPS
- while (true)
- {
- if (__first1 == __s)
- return __last1;
- if (__pred(*__first1, *__first2))
- break;
- ++__first1;
- }
- #else // !_LIBCPP_UNROLL_LOOPS
- for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
- {
- if (__pred(*__first1, *__first2))
- goto __phase2;
- if (__pred(*++__first1, *__first2))
- goto __phase2;
- if (__pred(*++__first1, *__first2))
- goto __phase2;
- if (__pred(*++__first1, *__first2))
- goto __phase2;
- ++__first1;
- }
- switch (__s - __first1)
- {
- case 3:
- if (__pred(*__first1, *__first2))
- break;
- ++__first1;
- case 2:
- if (__pred(*__first1, *__first2))
- break;
- ++__first1;
- case 1:
- if (__pred(*__first1, *__first2))
- break;
- case 0:
- return __last1;
- }
- __phase2:
- #endif // !_LIBCPP_UNROLL_LOOPS
- _RandomAccessIterator1 __m1 = __first1;
- _RandomAccessIterator2 __m2 = __first2;
- #if !_LIBCPP_UNROLL_LOOPS
- while (true)
- {
- if (++__m2 == __last2)
- return __first1;
- ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
- if (!__pred(*__m1, *__m2))
- {
- ++__first1;
- break;
- }
- }
- #else // !_LIBCPP_UNROLL_LOOPS
- ++__m2;
- ++__m1;
- for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
- {
- if (!__pred(*__m1, *__m2))
- goto __continue;
- if (!__pred(*++__m1, *++__m2))
- goto __continue;
- if (!__pred(*++__m1, *++__m2))
- goto __continue;
- if (!__pred(*++__m1, *++__m2))
- goto __continue;
- ++__m1;
- ++__m2;
- }
- switch (__last2 - __m2)
- {
- case 3:
- if (!__pred(*__m1, *__m2))
- break;
- ++__m1;
- ++__m2;
- case 2:
- if (!__pred(*__m1, *__m2))
- break;
- ++__m1;
- ++__m2;
- case 1:
- if (!__pred(*__m1, *__m2))
- break;
- case 0:
- return __first1;
- }
- __continue:
- ++__first1;
- #endif // !_LIBCPP_UNROLL_LOOPS
- }
- }
- template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator1
- search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
- {
- return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __last2, __pred,
- typename std::iterator_traits<_ForwardIterator1>::iterator_category(),
- typename std::iterator_traits<_ForwardIterator2>::iterator_category());
- }
- template <class _ForwardIterator1, class _ForwardIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator1
- search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2)
- {
- typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1;
- typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2;
- return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
- }
- // search_n
- template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
- _ForwardIterator
- __search_n(_ForwardIterator __first, _ForwardIterator __last,
- _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
- {
- if (__count <= 0)
- return __first;
- while (true)
- {
- // Find first element in sequence that matchs __value_, with a mininum of loop checks
- while (true)
- {
- if (__first == __last) // return __last if no element matches __value_
- return __last;
- if (__pred(*__first, __value_))
- break;
- ++__first;
- }
- // *__first matches __value_, now match elements after here
- _ForwardIterator __m = __first;
- _Size __c(0);
- while (true)
- {
- if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
- return __first;
- if (++__m == __last) // Otherwise if source exhaused, pattern not found
- return __last;
- if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
- {
- __first = __m;
- ++__first;
- break;
- } // else there is a match, check next elements
- }
- }
- }
- template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
- _RandomAccessIterator
- __search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
- _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
- {
- if (__count <= 0)
- return __first;
- _Size __len = static_cast<_Size>(__last - __first);
- if (__len < __count)
- return __last;
- const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
- while (true)
- {
- // Find first element in sequence that matchs __value_, with a mininum of loop checks
- while (true)
- {
- if (__first >= __s) // return __last if no element matches __value_
- return __last;
- if (__pred(*__first, __value_))
- break;
- ++__first;
- }
- // *__first matches __value_, now match elements after here
- _RandomAccessIterator __m = __first;
- _Size __c(0);
- while (true)
- {
- if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
- return __first;
- ++__m; // no need to check range on __m because __s guarantees we have enough source
- if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
- {
- __first = __m;
- ++__first;
- break;
- } // else there is a match, check next elements
- }
- }
- }
- template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- search_n(_ForwardIterator __first, _ForwardIterator __last,
- _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
- {
- return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first, __last, __count, __value_, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
- }
- template <class _ForwardIterator, class _Size, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
- {
- typedef typename iterator_traits<_ForwardIterator>::value_type __v;
- return _VSTD::search_n(__first, __last, __count, __value_, __equal_to<__v, _Tp>());
- }
- // copy
- template <class _Iter>
- struct __libcpp_is_trivial_iterator
- {
- static const bool value = is_pointer<_Iter>::value;
- };
- template <class _Iter>
- struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
- {
- static const bool value = is_pointer<_Iter>::value;
- };
- template <class _Iter>
- struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
- {
- static const bool value = is_pointer<_Iter>::value;
- };
- template <class _Iter>
- inline _LIBCPP_INLINE_VISIBILITY
- _Iter
- __unwrap_iter(_Iter __i)
- {
- return __i;
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_trivially_copy_assignable<_Tp>::value,
- _Tp*
- >::type
- __unwrap_iter(move_iterator<_Tp*> __i)
- {
- return __i.base();
- }
- #if _LIBCPP_DEBUG_LEVEL < 2
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_trivially_copy_assignable<_Tp>::value,
- _Tp*
- >::type
- __unwrap_iter(__wrap_iter<_Tp*> __i)
- {
- return __i.base();
- }
- #endif // _LIBCPP_DEBUG_LEVEL < 2
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- for (; __first != __last; ++__first, ++__result)
- *__result = *__first;
- return __result;
- }
- template <class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_same<typename remove_const<_Tp>::type, _Up>::value &&
- is_trivially_copy_assignable<_Up>::value,
- _Up*
- >::type
- __copy(_Tp* __first, _Tp* __last, _Up* __result)
- {
- const size_t __n = static_cast<size_t>(__last - __first);
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
- return __result + __n;
- }
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
- }
- // copy_backward
- template <class _BidirectionalIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
- {
- while (__first != __last)
- *--__result = *--__last;
- return __result;
- }
- template <class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_same<typename remove_const<_Tp>::type, _Up>::value &&
- is_trivially_copy_assignable<_Up>::value,
- _Up*
- >::type
- __copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
- {
- const size_t __n = static_cast<size_t>(__last - __first);
- __result -= __n;
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
- return __result;
- }
- template <class _BidirectionalIterator1, class _BidirectionalIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- _BidirectionalIterator2
- copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
- _BidirectionalIterator2 __result)
- {
- return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
- }
- // copy_if
- template<class _InputIterator, class _OutputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- copy_if(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- {
- if (__pred(*__first))
- {
- *__result = *__first;
- ++__result;
- }
- }
- return __result;
- }
- // copy_n
- template<class _InputIterator, class _Size, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- __is_input_iterator<_InputIterator>::value &&
- !__is_random_access_iterator<_InputIterator>::value,
- _OutputIterator
- >::type
- copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
- {
- if (__n > 0)
- {
- *__result = *__first;
- ++__result;
- for (--__n; __n > 0; --__n)
- {
- ++__first;
- *__result = *__first;
- ++__result;
- }
- }
- return __result;
- }
- template<class _InputIterator, class _Size, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- __is_random_access_iterator<_InputIterator>::value,
- _OutputIterator
- >::type
- copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
- {
- return _VSTD::copy(__first, __first + __n, __result);
- }
- // move
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- __move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- for (; __first != __last; ++__first, ++__result)
- *__result = _VSTD::move(*__first);
- return __result;
- }
- template <class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_same<typename remove_const<_Tp>::type, _Up>::value &&
- is_trivially_copy_assignable<_Up>::value,
- _Up*
- >::type
- __move(_Tp* __first, _Tp* __last, _Up* __result)
- {
- const size_t __n = static_cast<size_t>(__last - __first);
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
- return __result + __n;
- }
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
- }
- // move_backward
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- __move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- while (__first != __last)
- *--__result = _VSTD::move(*--__last);
- return __result;
- }
- template <class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_same<typename remove_const<_Tp>::type, _Up>::value &&
- is_trivially_copy_assignable<_Up>::value,
- _Up*
- >::type
- __move_backward(_Tp* __first, _Tp* __last, _Up* __result)
- {
- const size_t __n = static_cast<size_t>(__last - __first);
- __result -= __n;
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
- return __result;
- }
- template <class _BidirectionalIterator1, class _BidirectionalIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- _BidirectionalIterator2
- move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
- _BidirectionalIterator2 __result)
- {
- return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
- }
- // iter_swap
- // moved to <type_traits> for better swap / noexcept support
- // transform
- template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
- {
- for (; __first != __last; ++__first, ++__result)
- *__result = __op(*__first);
- return __result;
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
- _OutputIterator __result, _BinaryOperation __binary_op)
- {
- for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
- *__result = __binary_op(*__first1, *__first2);
- return __result;
- }
- // replace
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
- {
- for (; __first != __last; ++__first)
- if (*__first == __old_value)
- *__first = __new_value;
- }
- // replace_if
- template <class _ForwardIterator, class _Predicate, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
- {
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- *__first = __new_value;
- }
- // replace_copy
- template <class _InputIterator, class _OutputIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
- const _Tp& __old_value, const _Tp& __new_value)
- {
- for (; __first != __last; ++__first, ++__result)
- if (*__first == __old_value)
- *__result = __new_value;
- else
- *__result = *__first;
- return __result;
- }
- // replace_copy_if
- template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
- _Predicate __pred, const _Tp& __new_value)
- {
- for (; __first != __last; ++__first, ++__result)
- if (__pred(*__first))
- *__result = __new_value;
- else
- *__result = *__first;
- return __result;
- }
- // fill_n
- template <class _OutputIterator, class _Size, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
- {
- for (; __n > 0; ++__first, --__n)
- *__first = __value_;
- return __first;
- }
- template <class _Tp, class _Size, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
- !is_same<_Tp, bool>::value &&
- is_integral<_Up>::value && sizeof(_Up) == 1,
- _Tp*
- >::type
- __fill_n(_Tp* __first, _Size __n,_Up __value_)
- {
- if (__n > 0)
- _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
- return __first + __n;
- }
- template <class _OutputIterator, class _Size, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
- {
- return _VSTD::__fill_n(__first, __n, __value_);
- }
- // fill
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
- {
- for (; __first != __last; ++__first)
- *__first = __value_;
- }
- template <class _RandomAccessIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
- {
- _VSTD::fill_n(__first, __last - __first, __value_);
- }
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
- }
- // generate
- template <class _ForwardIterator, class _Generator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
- {
- for (; __first != __last; ++__first)
- *__first = __gen();
- }
- // generate_n
- template <class _OutputIterator, class _Size, class _Generator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
- {
- for (; __n > 0; ++__first, --__n)
- *__first = __gen();
- return __first;
- }
- // remove
- template <class _ForwardIterator, class _Tp>
- _ForwardIterator
- remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- __first = _VSTD::find(__first, __last, __value_);
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- {
- if (!(*__i == __value_))
- {
- *__first = _VSTD::move(*__i);
- ++__first;
- }
- }
- }
- return __first;
- }
- // remove_if
- template <class _ForwardIterator, class _Predicate>
- _ForwardIterator
- remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
- {
- __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
- (__first, __last, __pred);
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- {
- if (!__pred(*__i))
- {
- *__first = _VSTD::move(*__i);
- ++__first;
- }
- }
- }
- return __first;
- }
- // remove_copy
- template <class _InputIterator, class _OutputIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
- {
- for (; __first != __last; ++__first)
- {
- if (!(*__first == __value_))
- {
- *__result = *__first;
- ++__result;
- }
- }
- return __result;
- }
- // remove_copy_if
- template <class _InputIterator, class _OutputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- {
- if (!__pred(*__first))
- {
- *__result = *__first;
- ++__result;
- }
- }
- return __result;
- }
- // unique
- template <class _ForwardIterator, class _BinaryPredicate>
- _ForwardIterator
- unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
- {
- __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first, __last, __pred);
- if (__first != __last)
- {
- // ... a a ? ...
- // f i
- _ForwardIterator __i = __first;
- for (++__i; ++__i != __last;)
- if (!__pred(*__first, *__i))
- *++__first = _VSTD::move(*__i);
- ++__first;
- }
- return __first;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- unique(_ForwardIterator __first, _ForwardIterator __last)
- {
- typedef typename iterator_traits<_ForwardIterator>::value_type __v;
- return _VSTD::unique(__first, __last, __equal_to<__v>());
- }
- // unique_copy
- template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
- _OutputIterator
- __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
- input_iterator_tag, output_iterator_tag)
- {
- if (__first != __last)
- {
- typename iterator_traits<_InputIterator>::value_type __t(*__first);
- *__result = __t;
- ++__result;
- while (++__first != __last)
- {
- if (!__pred(__t, *__first))
- {
- __t = *__first;
- *__result = __t;
- ++__result;
- }
- }
- }
- return __result;
- }
- template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
- _OutputIterator
- __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
- forward_iterator_tag, output_iterator_tag)
- {
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- *__result = *__i;
- ++__result;
- while (++__first != __last)
- {
- if (!__pred(*__i, *__first))
- {
- *__result = *__first;
- ++__result;
- __i = __first;
- }
- }
- }
- return __result;
- }
- template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
- _ForwardIterator
- __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
- input_iterator_tag, forward_iterator_tag)
- {
- if (__first != __last)
- {
- *__result = *__first;
- while (++__first != __last)
- if (!__pred(*__result, *__first))
- *++__result = *__first;
- ++__result;
- }
- return __result;
- }
- template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
- {
- return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first, __last, __result, __pred,
- typename iterator_traits<_InputIterator>::iterator_category(),
- typename iterator_traits<_OutputIterator>::iterator_category());
- }
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- typedef typename iterator_traits<_InputIterator>::value_type __v;
- return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
- }
- // reverse
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
- {
- while (__first != __last)
- {
- if (__first == --__last)
- break;
- swap(*__first, *__last);
- ++__first;
- }
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
- {
- if (__first != __last)
- for (; __first < --__last; ++__first)
- swap(*__first, *__last);
- }
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
- {
- _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
- }
- // reverse_copy
- template <class _BidirectionalIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
- {
- for (; __first != __last; ++__result)
- *__result = *--__last;
- return __result;
- }
- // rotate
- template <class _ForwardIterator>
- _ForwardIterator
- __rotate_left(_ForwardIterator __first, _ForwardIterator __last)
- {
- typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
- value_type __tmp = _VSTD::move(*__first);
- _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
- *__lm1 = _VSTD::move(__tmp);
- return __lm1;
- }
- template <class _BidirectionalIterator>
- _BidirectionalIterator
- __rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
- {
- typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
- _BidirectionalIterator __lm1 = _VSTD::prev(__last);
- value_type __tmp = _VSTD::move(*__lm1);
- _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
- *__first = _VSTD::move(__tmp);
- return __fp1;
- }
- template <class _ForwardIterator>
- _ForwardIterator
- __rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
- {
- _ForwardIterator __i = __middle;
- while (true)
- {
- swap(*__first, *__i);
- ++__first;
- if (++__i == __last)
- break;
- if (__first == __middle)
- __middle = __i;
- }
- _ForwardIterator __r = __first;
- if (__first != __middle)
- {
- __i = __middle;
- while (true)
- {
- swap(*__first, *__i);
- ++__first;
- if (++__i == __last)
- {
- if (__first == __middle)
- break;
- __i = __middle;
- }
- else if (__first == __middle)
- __middle = __i;
- }
- }
- return __r;
- }
- template<typename _Integral>
- inline _LIBCPP_INLINE_VISIBILITY
- _Integral
- __gcd(_Integral __x, _Integral __y)
- {
- do
- {
- _Integral __t = __x % __y;
- __x = __y;
- __y = __t;
- } while (__y);
- return __x;
- }
- template<typename _RandomAccessIterator>
- _RandomAccessIterator
- __rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- const difference_type __m1 = __middle - __first;
- const difference_type __m2 = __last - __middle;
- if (__m1 == __m2)
- {
- _VSTD::swap_ranges(__first, __middle, __middle);
- return __middle;
- }
- const difference_type __g = _VSTD::__gcd(__m1, __m2);
- for (_RandomAccessIterator __p = __first + __g; __p != __first;)
- {
- value_type __t(_VSTD::move(*--__p));
- _RandomAccessIterator __p1 = __p;
- _RandomAccessIterator __p2 = __p1 + __m1;
- do
- {
- *__p1 = _VSTD::move(*__p2);
- __p1 = __p2;
- const difference_type __d = __last - __p2;
- if (__m1 < __d)
- __p2 += __m1;
- else
- __p2 = __first + (__m1 - __d);
- } while (__p2 != __p);
- *__p1 = _VSTD::move(__t);
- }
- return __first + __m2;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- __rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
- _VSTD::forward_iterator_tag)
- {
- typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
- if (_VSTD::is_trivially_move_assignable<value_type>::value)
- {
- if (_VSTD::next(__first) == __middle)
- return _VSTD::__rotate_left(__first, __last);
- }
- return _VSTD::__rotate_forward(__first, __middle, __last);
- }
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _BidirectionalIterator
- __rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
- _VSTD::bidirectional_iterator_tag)
- {
- typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
- if (_VSTD::is_trivially_move_assignable<value_type>::value)
- {
- if (_VSTD::next(__first) == __middle)
- return _VSTD::__rotate_left(__first, __last);
- if (_VSTD::next(__middle) == __last)
- return _VSTD::__rotate_right(__first, __last);
- }
- return _VSTD::__rotate_forward(__first, __middle, __last);
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _RandomAccessIterator
- __rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
- _VSTD::random_access_iterator_tag)
- {
- typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
- if (_VSTD::is_trivially_move_assignable<value_type>::value)
- {
- if (_VSTD::next(__first) == __middle)
- return _VSTD::__rotate_left(__first, __last);
- if (_VSTD::next(__middle) == __last)
- return _VSTD::__rotate_right(__first, __last);
- return _VSTD::__rotate_gcd(__first, __middle, __last);
- }
- return _VSTD::__rotate_forward(__first, __middle, __last);
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
- {
- if (__first == __middle)
- return __last;
- if (__middle == __last)
- return __first;
- return _VSTD::__rotate(__first, __middle, __last,
- typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
- }
- // rotate_copy
- template <class _ForwardIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
- {
- return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
- }
- // min_element
- template <class _ForwardIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
- {
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- if (__comp(*__i, *__first))
- __first = __i;
- }
- return __first;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- min_element(_ForwardIterator __first, _ForwardIterator __last)
- {
- return _VSTD::min_element(__first, __last,
- __less<typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // min
- template <class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- const _Tp&
- min(const _Tp& __a, const _Tp& __b, _Compare __comp)
- {
- return __comp(__b, __a) ? __b : __a;
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- const _Tp&
- min(const _Tp& __a, const _Tp& __b)
- {
- return _VSTD::min(__a, __b, __less<_Tp>());
- }
- #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- template<class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _Tp
- min(initializer_list<_Tp> __t, _Compare __comp)
- {
- return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _Tp
- min(initializer_list<_Tp> __t)
- {
- return *_VSTD::min_element(__t.begin(), __t.end());
- }
- #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- // max_element
- template <class _ForwardIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
- {
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- if (__comp(*__first, *__i))
- __first = __i;
- }
- return __first;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- max_element(_ForwardIterator __first, _ForwardIterator __last)
- {
- return _VSTD::max_element(__first, __last,
- __less<typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // max
- template <class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- const _Tp&
- max(const _Tp& __a, const _Tp& __b, _Compare __comp)
- {
- return __comp(__a, __b) ? __b : __a;
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- const _Tp&
- max(const _Tp& __a, const _Tp& __b)
- {
- return _VSTD::max(__a, __b, __less<_Tp>());
- }
- #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- template<class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _Tp
- max(initializer_list<_Tp> __t, _Compare __comp)
- {
- return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _Tp
- max(initializer_list<_Tp> __t)
- {
- return *_VSTD::max_element(__t.begin(), __t.end());
- }
- #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- // minmax_element
- template <class _ForwardIterator, class _Compare>
- std::pair<_ForwardIterator, _ForwardIterator>
- minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
- {
- std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
- if (__first != __last)
- {
- if (++__first != __last)
- {
- if (__comp(*__first, *__result.first))
- __result.first = __first;
- else
- __result.second = __first;
- while (++__first != __last)
- {
- _ForwardIterator __i = __first;
- if (++__first == __last)
- {
- if (__comp(*__i, *__result.first))
- __result.first = __i;
- else if (!__comp(*__i, *__result.second))
- __result.second = __i;
- break;
- }
- else
- {
- if (__comp(*__first, *__i))
- {
- if (__comp(*__first, *__result.first))
- __result.first = __first;
- if (!__comp(*__i, *__result.second))
- __result.second = __i;
- }
- else
- {
- if (__comp(*__i, *__result.first))
- __result.first = __i;
- if (!__comp(*__first, *__result.second))
- __result.second = __first;
- }
- }
- }
- }
- }
- return __result;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- std::pair<_ForwardIterator, _ForwardIterator>
- minmax_element(_ForwardIterator __first, _ForwardIterator __last)
- {
- return _VSTD::minmax_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // minmax
- template<class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<const _Tp&, const _Tp&>
- minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
- {
- return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
- pair<const _Tp&, const _Tp&>(__a, __b);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<const _Tp&, const _Tp&>
- minmax(const _Tp& __a, const _Tp& __b)
- {
- return _VSTD::minmax(__a, __b, __less<_Tp>());
- }
- #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_Tp, _Tp>
- minmax(initializer_list<_Tp> __t)
- {
- pair<const _Tp*, const _Tp*> __p =
- _VSTD::minmax_element(__t.begin(), __t.end());
- return pair<_Tp, _Tp>(*__p.first, *__p.second);
- }
- template<class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_Tp, _Tp>
- minmax(initializer_list<_Tp> __t, _Compare __comp)
- {
- pair<const _Tp*, const _Tp*> __p =
- _VSTD::minmax_element(__t.begin(), __t.end(), __comp);
- return pair<_Tp, _Tp>(*__p.first, *__p.second);
- }
- #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- // random_shuffle
- // __independent_bits_engine
- template <unsigned long long _Xp, size_t _Rp>
- struct __log2_imp
- {
- static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
- : __log2_imp<_Xp, _Rp - 1>::value;
- };
- template <unsigned long long _Xp>
- struct __log2_imp<_Xp, 0>
- {
- static const size_t value = 0;
- };
- template <size_t _Rp>
- struct __log2_imp<0, _Rp>
- {
- static const size_t value = _Rp + 1;
- };
- template <class _UI, _UI _Xp>
- struct __log2
- {
- static const size_t value = __log2_imp<_Xp,
- sizeof(_UI) * __CHAR_BIT__ - 1>::value;
- };
- template<class _Engine, class _UIntType>
- class __independent_bits_engine
- {
- public:
- // types
- typedef _UIntType result_type;
- private:
- typedef typename _Engine::result_type _Engine_result_type;
- typedef typename conditional
- <
- sizeof(_Engine_result_type) <= sizeof(result_type),
- result_type,
- _Engine_result_type
- >::type _Working_result_type;
- _Engine& __e_;
- size_t __w_;
- size_t __w0_;
- size_t __n_;
- size_t __n0_;
- _Working_result_type __y0_;
- _Working_result_type __y1_;
- _Engine_result_type __mask0_;
- _Engine_result_type __mask1_;
- #ifdef _LIBCPP_HAS_NO_CONSTEXPR
- static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
- + _Working_result_type(1);
- #else
- static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
- + _Working_result_type(1);
- #endif
- static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
- static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
- static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
- public:
- // constructors and seeding functions
- __independent_bits_engine(_Engine& __e, size_t __w);
- // generating functions
- result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
- private:
- result_type __eval(false_type);
- result_type __eval(true_type);
- };
- template<class _Engine, class _UIntType>
- __independent_bits_engine<_Engine, _UIntType>
- ::__independent_bits_engine(_Engine& __e, size_t __w)
- : __e_(__e),
- __w_(__w)
- {
- __n_ = __w_ / __m + (__w_ % __m != 0);
- __w0_ = __w_ / __n_;
- if (_Rp == 0)
- __y0_ = _Rp;
- else if (__w0_ < _WDt)
- __y0_ = (_Rp >> __w0_) << __w0_;
- else
- __y0_ = 0;
- if (_Rp - __y0_ > __y0_ / __n_)
- {
- ++__n_;
- __w0_ = __w_ / __n_;
- if (__w0_ < _WDt)
- __y0_ = (_Rp >> __w0_) << __w0_;
- else
- __y0_ = 0;
- }
- __n0_ = __n_ - __w_ % __n_;
- if (__w0_ < _WDt - 1)
- __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
- else
- __y1_ = 0;
- __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
- _Engine_result_type(0);
- __mask1_ = __w0_ < _EDt - 1 ?
- _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
- _Engine_result_type(~0);
- }
- template<class _Engine, class _UIntType>
- inline
- _UIntType
- __independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
- {
- return static_cast<result_type>(__e_() & __mask0_);
- }
- template<class _Engine, class _UIntType>
- _UIntType
- __independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
- {
- result_type _Sp = 0;
- for (size_t __k = 0; __k < __n0_; ++__k)
- {
- _Engine_result_type __u;
- do
- {
- __u = __e_() - _Engine::min();
- } while (__u >= __y0_);
- if (__w0_ < _WDt)
- _Sp <<= __w0_;
- else
- _Sp = 0;
- _Sp += __u & __mask0_;
- }
- for (size_t __k = __n0_; __k < __n_; ++__k)
- {
- _Engine_result_type __u;
- do
- {
- __u = __e_() - _Engine::min();
- } while (__u >= __y1_);
- if (__w0_ < _WDt - 1)
- _Sp <<= __w0_ + 1;
- else
- _Sp = 0;
- _Sp += __u & __mask1_;
- }
- return _Sp;
- }
- // uniform_int_distribution
- template<class _IntType = int>
- class uniform_int_distribution
- {
- public:
- // types
- typedef _IntType result_type;
- class param_type
- {
- result_type __a_;
- result_type __b_;
- public:
- typedef uniform_int_distribution distribution_type;
- explicit param_type(result_type __a = 0,
- result_type __b = numeric_limits<result_type>::max())
- : __a_(__a), __b_(__b) {}
- result_type a() const {return __a_;}
- result_type b() const {return __b_;}
- friend bool operator==(const param_type& __x, const param_type& __y)
- {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
- friend bool operator!=(const param_type& __x, const param_type& __y)
- {return !(__x == __y);}
- };
- private:
- param_type __p_;
- public:
- // constructors and reset functions
- explicit uniform_int_distribution(result_type __a = 0,
- result_type __b = numeric_limits<result_type>::max())
- : __p_(param_type(__a, __b)) {}
- explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
- void reset() {}
- // generating functions
- template<class _URNG> result_type operator()(_URNG& __g)
- {return (*this)(__g, __p_);}
- template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
- // property functions
- result_type a() const {return __p_.a();}
- result_type b() const {return __p_.b();}
- param_type param() const {return __p_;}
- void param(const param_type& __p) {__p_ = __p;}
- result_type min() const {return a();}
- result_type max() const {return b();}
- friend bool operator==(const uniform_int_distribution& __x,
- const uniform_int_distribution& __y)
- {return __x.__p_ == __y.__p_;}
- friend bool operator!=(const uniform_int_distribution& __x,
- const uniform_int_distribution& __y)
- {return !(__x == __y);}
- };
- template<class _IntType>
- template<class _URNG>
- typename uniform_int_distribution<_IntType>::result_type
- uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
- {
- typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
- uint32_t, uint64_t>::type _UIntType;
- const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
- if (_Rp == 1)
- return __p.a();
- const size_t _Dt = numeric_limits<_UIntType>::digits;
- typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
- if (_Rp == 0)
- return static_cast<result_type>(_Eng(__g, _Dt)());
- size_t __w = _Dt - __clz(_Rp) - 1;
- if ((_Rp & (_UIntType(~0) >> (_Dt - __w))) != 0)
- ++__w;
- _Eng __e(__g, __w);
- _UIntType __u;
- do
- {
- __u = __e();
- } while (__u >= _Rp);
- return static_cast<result_type>(__u + __p.a());
- }
- class _LIBCPP_TYPE_VIS __rs_default;
- _LIBCPP_FUNC_VIS __rs_default __rs_get();
- class _LIBCPP_TYPE_VIS __rs_default
- {
- static unsigned __c_;
- __rs_default();
- public:
- typedef uint_fast32_t result_type;
- static const result_type _Min = 0;
- static const result_type _Max = 0xFFFFFFFF;
- __rs_default(const __rs_default&);
- ~__rs_default();
- result_type operator()();
- static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
- static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
- friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
- };
- _LIBCPP_FUNC_VIS __rs_default __rs_get();
- template <class _RandomAccessIterator>
- void
- random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- typedef uniform_int_distribution<ptrdiff_t> _Dp;
- typedef typename _Dp::param_type _Pp;
- difference_type __d = __last - __first;
- if (__d > 1)
- {
- _Dp __uid;
- __rs_default __g = __rs_get();
- for (--__last, --__d; __first < __last; ++__first, --__d)
- {
- difference_type __i = __uid(__g, _Pp(0, __d));
- if (__i != difference_type(0))
- swap(*__first, *(__first + __i));
- }
- }
- }
- template <class _RandomAccessIterator, class _RandomNumberGenerator>
- void
- random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
- #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
- _RandomNumberGenerator&& __rand)
- #else
- _RandomNumberGenerator& __rand)
- #endif
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- difference_type __d = __last - __first;
- if (__d > 1)
- {
- for (--__last; __first < __last; ++__first, --__d)
- {
- difference_type __i = __rand(__d);
- swap(*__first, *(__first + __i));
- }
- }
- }
- template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
- void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
- #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
- _UniformRandomNumberGenerator&& __g)
- #else
- _UniformRandomNumberGenerator& __g)
- #endif
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- typedef uniform_int_distribution<ptrdiff_t> _Dp;
- typedef typename _Dp::param_type _Pp;
- difference_type __d = __last - __first;
- if (__d > 1)
- {
- _Dp __uid;
- for (--__last, --__d; __first < __last; ++__first, --__d)
- {
- difference_type __i = __uid(__g, _Pp(0, __d));
- if (__i != difference_type(0))
- swap(*__first, *(__first + __i));
- }
- }
- }
- template <class _InputIterator, class _Predicate>
- bool
- is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (!__pred(*__first))
- break;
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- return false;
- return true;
- }
- // partition
- template <class _Predicate, class _ForwardIterator>
- _ForwardIterator
- __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
- {
- while (true)
- {
- if (__first == __last)
- return __first;
- if (!__pred(*__first))
- break;
- ++__first;
- }
- for (_ForwardIterator __p = __first; ++__p != __last;)
- {
- if (__pred(*__p))
- {
- swap(*__first, *__p);
- ++__first;
- }
- }
- return __first;
- }
- template <class _Predicate, class _BidirectionalIterator>
- _BidirectionalIterator
- __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
- bidirectional_iterator_tag)
- {
- while (true)
- {
- while (true)
- {
- if (__first == __last)
- return __first;
- if (!__pred(*__first))
- break;
- ++__first;
- }
- do
- {
- if (__first == --__last)
- return __first;
- } while (!__pred(*__last));
- swap(*__first, *__last);
- ++__first;
- }
- }
- template <class _ForwardIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
- {
- return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
- (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
- }
- // partition_copy
- template <class _InputIterator, class _OutputIterator1,
- class _OutputIterator2, class _Predicate>
- pair<_OutputIterator1, _OutputIterator2>
- partition_copy(_InputIterator __first, _InputIterator __last,
- _OutputIterator1 __out_true, _OutputIterator2 __out_false,
- _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- {
- if (__pred(*__first))
- {
- *__out_true = *__first;
- ++__out_true;
- }
- else
- {
- *__out_false = *__first;
- ++__out_false;
- }
- }
- return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
- }
- // partition_point
- template<class _ForwardIterator, class _Predicate>
- _ForwardIterator
- partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
- {
- typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
- difference_type __len = _VSTD::distance(__first, __last);
- while (__len != 0)
- {
- difference_type __l2 = __len / 2;
- _ForwardIterator __m = __first;
- _VSTD::advance(__m, __l2);
- if (__pred(*__m))
- {
- __first = ++__m;
- __len -= __l2 + 1;
- }
- else
- __len = __l2;
- }
- return __first;
- }
- // stable_partition
- template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
- _ForwardIterator
- __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
- _Distance __len, _Pair __p, forward_iterator_tag __fit)
- {
- // *__first is known to be false
- // __len >= 1
- if (__len == 1)
- return __first;
- if (__len == 2)
- {
- _ForwardIterator __m = __first;
- if (__pred(*++__m))
- {
- swap(*__first, *__m);
- return __m;
- }
- return __first;
- }
- if (__len <= __p.second)
- { // The buffer is big enough to use
- typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
- // Move the falses into the temporary buffer, and the trues to the front of the line
- // Update __first to always point to the end of the trues
- value_type* __t = __p.first;
- ::new(__t) value_type(_VSTD::move(*__first));
- __d.__incr((value_type*)0);
- ++__t;
- _ForwardIterator __i = __first;
- while (++__i != __last)
- {
- if (__pred(*__i))
- {
- *__first = _VSTD::move(*__i);
- ++__first;
- }
- else
- {
- ::new(__t) value_type(_VSTD::move(*__i));
- __d.__incr((value_type*)0);
- ++__t;
- }
- }
- // All trues now at start of range, all falses in buffer
- // Move falses back into range, but don't mess up __first which points to first false
- __i = __first;
- for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
- *__i = _VSTD::move(*__t2);
- // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
- return __first;
- }
- // Else not enough buffer, do in place
- // __len >= 3
- _ForwardIterator __m = __first;
- _Distance __len2 = __len / 2; // __len2 >= 2
- _VSTD::advance(__m, __len2);
- // recurse on [__first, __m), *__first know to be false
- // F?????????????????
- // f m l
- typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
- _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
- // TTTFFFFF??????????
- // f ff m l
- // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
- _ForwardIterator __m1 = __m;
- _ForwardIterator __second_false = __last;
- _Distance __len_half = __len - __len2;
- while (__pred(*__m1))
- {
- if (++__m1 == __last)
- goto __second_half_done;
- --__len_half;
- }
- // TTTFFFFFTTTF??????
- // f ff m m1 l
- __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
- __second_half_done:
- // TTTFFFFFTTTTTFFFFF
- // f ff m sf l
- return _VSTD::rotate(__first_false, __m, __second_false);
- // TTTTTTTTFFFFFFFFFF
- // |
- }
- struct __return_temporary_buffer
- {
- template <class _Tp>
- _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
- };
- template <class _Predicate, class _ForwardIterator>
- _ForwardIterator
- __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
- forward_iterator_tag)
- {
- const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
- // Either prove all true and return __first or point to first false
- while (true)
- {
- if (__first == __last)
- return __first;
- if (!__pred(*__first))
- break;
- ++__first;
- }
- // We now have a reduced range [__first, __last)
- // *__first is known to be false
- typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
- typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
- difference_type __len = _VSTD::distance(__first, __last);
- pair<value_type*, ptrdiff_t> __p(0, 0);
- unique_ptr<value_type, __return_temporary_buffer> __h;
- if (__len >= __alloc_limit)
- {
- __p = _VSTD::get_temporary_buffer<value_type>(__len);
- __h.reset(__p.first);
- }
- return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
- (__first, __last, __pred, __len, __p, forward_iterator_tag());
- }
- template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
- _BidirectionalIterator
- __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
- _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
- {
- // *__first is known to be false
- // *__last is known to be true
- // __len >= 2
- if (__len == 2)
- {
- swap(*__first, *__last);
- return __last;
- }
- if (__len == 3)
- {
- _BidirectionalIterator __m = __first;
- if (__pred(*++__m))
- {
- swap(*__first, *__m);
- swap(*__m, *__last);
- return __last;
- }
- swap(*__m, *__last);
- swap(*__first, *__m);
- return __m;
- }
- if (__len <= __p.second)
- { // The buffer is big enough to use
- typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
- // Move the falses into the temporary buffer, and the trues to the front of the line
- // Update __first to always point to the end of the trues
- value_type* __t = __p.first;
- ::new(__t) value_type(_VSTD::move(*__first));
- __d.__incr((value_type*)0);
- ++__t;
- _BidirectionalIterator __i = __first;
- while (++__i != __last)
- {
- if (__pred(*__i))
- {
- *__first = _VSTD::move(*__i);
- ++__first;
- }
- else
- {
- ::new(__t) value_type(_VSTD::move(*__i));
- __d.__incr((value_type*)0);
- ++__t;
- }
- }
- // move *__last, known to be true
- *__first = _VSTD::move(*__i);
- __i = ++__first;
- // All trues now at start of range, all falses in buffer
- // Move falses back into range, but don't mess up __first which points to first false
- for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
- *__i = _VSTD::move(*__t2);
- // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
- return __first;
- }
- // Else not enough buffer, do in place
- // __len >= 4
- _BidirectionalIterator __m = __first;
- _Distance __len2 = __len / 2; // __len2 >= 2
- _VSTD::advance(__m, __len2);
- // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
- // F????????????????T
- // f m l
- _BidirectionalIterator __m1 = __m;
- _BidirectionalIterator __first_false = __first;
- _Distance __len_half = __len2;
- while (!__pred(*--__m1))
- {
- if (__m1 == __first)
- goto __first_half_done;
- --__len_half;
- }
- // F???TFFF?????????T
- // f m1 m l
- typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
- __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
- __first_half_done:
- // TTTFFFFF?????????T
- // f ff m l
- // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
- __m1 = __m;
- _BidirectionalIterator __second_false = __last;
- ++__second_false;
- __len_half = __len - __len2;
- while (__pred(*__m1))
- {
- if (++__m1 == __last)
- goto __second_half_done;
- --__len_half;
- }
- // TTTFFFFFTTTF?????T
- // f ff m m1 l
- __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
- __second_half_done:
- // TTTFFFFFTTTTTFFFFF
- // f ff m sf l
- return _VSTD::rotate(__first_false, __m, __second_false);
- // TTTTTTTTFFFFFFFFFF
- // |
- }
- template <class _Predicate, class _BidirectionalIterator>
- _BidirectionalIterator
- __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
- bidirectional_iterator_tag)
- {
- typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
- typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
- const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
- // Either prove all true and return __first or point to first false
- while (true)
- {
- if (__first == __last)
- return __first;
- if (!__pred(*__first))
- break;
- ++__first;
- }
- // __first points to first false, everything prior to __first is already set.
- // Either prove [__first, __last) is all false and return __first, or point __last to last true
- do
- {
- if (__first == --__last)
- return __first;
- } while (!__pred(*__last));
- // We now have a reduced range [__first, __last]
- // *__first is known to be false
- // *__last is known to be true
- // __len >= 2
- difference_type __len = _VSTD::distance(__first, __last) + 1;
- pair<value_type*, ptrdiff_t> __p(0, 0);
- unique_ptr<value_type, __return_temporary_buffer> __h;
- if (__len >= __alloc_limit)
- {
- __p = _VSTD::get_temporary_buffer<value_type>(__len);
- __h.reset(__p.first);
- }
- return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
- (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
- }
- template <class _ForwardIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
- {
- return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
- (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
- }
- // is_sorted_until
- template <class _ForwardIterator, class _Compare>
- _ForwardIterator
- is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
- {
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- {
- if (__comp(*__i, *__first))
- return __i;
- __first = __i;
- }
- }
- return __last;
- }
- template<class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
- {
- return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // is_sorted
- template <class _ForwardIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
- {
- return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
- }
- template<class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_sorted(_ForwardIterator __first, _ForwardIterator __last)
- {
- return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // sort
- // stable, 2-3 compares, 0-2 swaps
- template <class _Compare, class _ForwardIterator>
- unsigned
- __sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
- {
- unsigned __r = 0;
- if (!__c(*__y, *__x)) // if x <= y
- {
- if (!__c(*__z, *__y)) // if y <= z
- return __r; // x <= y && y <= z
- // x <= y && y > z
- swap(*__y, *__z); // x <= z && y < z
- __r = 1;
- if (__c(*__y, *__x)) // if x > y
- {
- swap(*__x, *__y); // x < y && y <= z
- __r = 2;
- }
- return __r; // x <= y && y < z
- }
- if (__c(*__z, *__y)) // x > y, if y > z
- {
- swap(*__x, *__z); // x < y && y < z
- __r = 1;
- return __r;
- }
- swap(*__x, *__y); // x > y && y <= z
- __r = 1; // x < y && x <= z
- if (__c(*__z, *__y)) // if y > z
- {
- swap(*__y, *__z); // x <= y && y < z
- __r = 2;
- }
- return __r;
- } // x <= y && y <= z
- // stable, 3-6 compares, 0-5 swaps
- template <class _Compare, class _ForwardIterator>
- unsigned
- __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
- _ForwardIterator __x4, _Compare __c)
- {
- unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
- if (__c(*__x4, *__x3))
- {
- swap(*__x3, *__x4);
- ++__r;
- if (__c(*__x3, *__x2))
- {
- swap(*__x2, *__x3);
- ++__r;
- if (__c(*__x2, *__x1))
- {
- swap(*__x1, *__x2);
- ++__r;
- }
- }
- }
- return __r;
- }
- // stable, 4-10 compares, 0-9 swaps
- template <class _Compare, class _ForwardIterator>
- unsigned
- __sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
- _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
- {
- unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
- if (__c(*__x5, *__x4))
- {
- swap(*__x4, *__x5);
- ++__r;
- if (__c(*__x4, *__x3))
- {
- swap(*__x3, *__x4);
- ++__r;
- if (__c(*__x3, *__x2))
- {
- swap(*__x2, *__x3);
- ++__r;
- if (__c(*__x2, *__x1))
- {
- swap(*__x1, *__x2);
- ++__r;
- }
- }
- }
- }
- return __r;
- }
- // Assumes size > 0
- template <class _Compare, class _BirdirectionalIterator>
- void
- __selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
- {
- _BirdirectionalIterator __lm1 = __last;
- for (--__lm1; __first != __lm1; ++__first)
- {
- _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
- typename add_lvalue_reference<_Compare>::type>
- (__first, __last, __comp);
- if (__i != __first)
- swap(*__first, *__i);
- }
- }
- template <class _Compare, class _BirdirectionalIterator>
- void
- __insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
- {
- typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
- if (__first != __last)
- {
- _BirdirectionalIterator __i = __first;
- for (++__i; __i != __last; ++__i)
- {
- _BirdirectionalIterator __j = __i;
- value_type __t(_VSTD::move(*__j));
- for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
- *__j = _VSTD::move(*__k);
- *__j = _VSTD::move(__t);
- }
- }
- }
- template <class _Compare, class _RandomAccessIterator>
- void
- __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- _RandomAccessIterator __j = __first+2;
- __sort3<_Compare>(__first, __first+1, __j, __comp);
- for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
- {
- if (__comp(*__i, *__j))
- {
- value_type __t(_VSTD::move(*__i));
- _RandomAccessIterator __k = __j;
- __j = __i;
- do
- {
- *__j = _VSTD::move(*__k);
- __j = __k;
- } while (__j != __first && __comp(__t, *--__k));
- *__j = _VSTD::move(__t);
- }
- __j = __i;
- }
- }
- template <class _Compare, class _RandomAccessIterator>
- bool
- __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- switch (__last - __first)
- {
- case 0:
- case 1:
- return true;
- case 2:
- if (__comp(*--__last, *__first))
- swap(*__first, *__last);
- return true;
- case 3:
- _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
- return true;
- case 4:
- _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
- return true;
- case 5:
- _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
- return true;
- }
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- _RandomAccessIterator __j = __first+2;
- __sort3<_Compare>(__first, __first+1, __j, __comp);
- const unsigned __limit = 8;
- unsigned __count = 0;
- for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
- {
- if (__comp(*__i, *__j))
- {
- value_type __t(_VSTD::move(*__i));
- _RandomAccessIterator __k = __j;
- __j = __i;
- do
- {
- *__j = _VSTD::move(*__k);
- __j = __k;
- } while (__j != __first && __comp(__t, *--__k));
- *__j = _VSTD::move(__t);
- if (++__count == __limit)
- return ++__i == __last;
- }
- __j = __i;
- }
- return true;
- }
- template <class _Compare, class _BirdirectionalIterator>
- void
- __insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
- typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
- {
- typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
- if (__first1 != __last1)
- {
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
- value_type* __last2 = __first2;
- ::new(__last2) value_type(_VSTD::move(*__first1));
- __d.__incr((value_type*)0);
- for (++__last2; ++__first1 != __last1; ++__last2)
- {
- value_type* __j2 = __last2;
- value_type* __i2 = __j2;
- if (__comp(*__first1, *--__i2))
- {
- ::new(__j2) value_type(_VSTD::move(*__i2));
- __d.__incr((value_type*)0);
- for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
- *__j2 = _VSTD::move(*__i2);
- *__j2 = _VSTD::move(*__first1);
- }
- else
- {
- ::new(__j2) value_type(_VSTD::move(*__first1));
- __d.__incr((value_type*)0);
- }
- }
- __h.release();
- }
- }
- template <class _Compare, class _RandomAccessIterator>
- void
- __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- // _Compare is known to be a reference type
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
- is_trivially_copy_assignable<value_type>::value ? 30 : 6;
- while (true)
- {
- __restart:
- difference_type __len = __last - __first;
- switch (__len)
- {
- case 0:
- case 1:
- return;
- case 2:
- if (__comp(*--__last, *__first))
- swap(*__first, *__last);
- return;
- case 3:
- _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
- return;
- case 4:
- _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
- return;
- case 5:
- _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
- return;
- }
- if (__len <= __limit)
- {
- _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
- return;
- }
- // __len > 5
- _RandomAccessIterator __m = __first;
- _RandomAccessIterator __lm1 = __last;
- --__lm1;
- unsigned __n_swaps;
- {
- difference_type __delta;
- if (__len >= 1000)
- {
- __delta = __len/2;
- __m += __delta;
- __delta /= 2;
- __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
- }
- else
- {
- __delta = __len/2;
- __m += __delta;
- __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
- }
- }
- // *__m is median
- // partition [__first, __m) < *__m and *__m <= [__m, __last)
- // (this inhibits tossing elements equivalent to __m around unnecessarily)
- _RandomAccessIterator __i = __first;
- _RandomAccessIterator __j = __lm1;
- // j points beyond range to be tested, *__m is known to be <= *__lm1
- // The search going up is known to be guarded but the search coming down isn't.
- // Prime the downward search with a guard.
- if (!__comp(*__i, *__m)) // if *__first == *__m
- {
- // *__first == *__m, *__first doesn't go in first part
- // manually guard downward moving __j against __i
- while (true)
- {
- if (__i == --__j)
- {
- // *__first == *__m, *__m <= all other elements
- // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
- ++__i; // __first + 1
- __j = __last;
- if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
- {
- while (true)
- {
- if (__i == __j)
- return; // [__first, __last) all equivalent elements
- if (__comp(*__first, *__i))
- {
- swap(*__i, *__j);
- ++__n_swaps;
- ++__i;
- break;
- }
- ++__i;
- }
- }
- // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
- if (__i == __j)
- return;
- while (true)
- {
- while (!__comp(*__first, *__i))
- ++__i;
- while (__comp(*__first, *--__j))
- ;
- if (__i >= __j)
- break;
- swap(*__i, *__j);
- ++__n_swaps;
- ++__i;
- }
- // [__first, __i) == *__first and *__first < [__i, __last)
- // The first part is sorted, sort the secod part
- // _VSTD::__sort<_Compare>(__i, __last, __comp);
- __first = __i;
- goto __restart;
- }
- if (__comp(*__j, *__m))
- {
- swap(*__i, *__j);
- ++__n_swaps;
- break; // found guard for downward moving __j, now use unguarded partition
- }
- }
- }
- // It is known that *__i < *__m
- ++__i;
- // j points beyond range to be tested, *__m is known to be <= *__lm1
- // if not yet partitioned...
- if (__i < __j)
- {
- // known that *(__i - 1) < *__m
- // known that __i <= __m
- while (true)
- {
- // __m still guards upward moving __i
- while (__comp(*__i, *__m))
- ++__i;
- // It is now known that a guard exists for downward moving __j
- while (!__comp(*--__j, *__m))
- ;
- if (__i > __j)
- break;
- swap(*__i, *__j);
- ++__n_swaps;
- // It is known that __m != __j
- // If __m just moved, follow it
- if (__m == __i)
- __m = __j;
- ++__i;
- }
- }
- // [__first, __i) < *__m and *__m <= [__i, __last)
- if (__i != __m && __comp(*__m, *__i))
- {
- swap(*__i, *__m);
- ++__n_swaps;
- }
- // [__first, __i) < *__i and *__i <= [__i+1, __last)
- // If we were given a perfect partition, see if insertion sort is quick...
- if (__n_swaps == 0)
- {
- bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
- if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
- {
- if (__fs)
- return;
- __last = __i;
- continue;
- }
- else
- {
- if (__fs)
- {
- __first = ++__i;
- continue;
- }
- }
- }
- // sort smaller range with recursive call and larger with tail recursion elimination
- if (__i - __first < __last - __i)
- {
- _VSTD::__sort<_Compare>(__first, __i, __comp);
- // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
- __first = ++__i;
- }
- else
- {
- _VSTD::__sort<_Compare>(__i+1, __last, __comp);
- // _VSTD::__sort<_Compare>(__first, __i, __comp);
- __last = __i;
- }
- }
- }
- // This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __sort<_Comp_ref>(__first, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __sort<_Comp_ref>(__first, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort(_Tp** __first, _Tp** __last)
- {
- _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
- {
- _VSTD::sort(__first.base(), __last.base());
- }
- template <class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
- {
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
- }
- #ifdef _LIBCPP_MSVC
- #pragma warning( push )
- #pragma warning( disable: 4231)
- #endif // _LIBCPP_MSVC
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&))
- #ifdef _LIBCPP_MSVC
- #pragma warning( pop )
- #endif // _LIBCPP_MSVC
- // lower_bound
- template <class _Compare, class _ForwardIterator, class _Tp>
- _ForwardIterator
- __lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
- difference_type __len = _VSTD::distance(__first, __last);
- while (__len != 0)
- {
- difference_type __l2 = __len / 2;
- _ForwardIterator __m = __first;
- _VSTD::advance(__m, __l2);
- if (__comp(*__m, __value_))
- {
- __first = ++__m;
- __len -= __l2 + 1;
- }
- else
- __len = __l2;
- }
- return __first;
- }
- template <class _ForwardIterator, class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- return _VSTD::lower_bound(__first, __last, __value_,
- __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
- }
- // upper_bound
- template <class _Compare, class _ForwardIterator, class _Tp>
- _ForwardIterator
- __upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
- difference_type __len = _VSTD::distance(__first, __last);
- while (__len != 0)
- {
- difference_type __l2 = __len / 2;
- _ForwardIterator __m = __first;
- _VSTD::advance(__m, __l2);
- if (__comp(__value_, *__m))
- __len = __l2;
- else
- {
- __first = ++__m;
- __len -= __l2 + 1;
- }
- }
- return __first;
- }
- template <class _ForwardIterator, class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- return _VSTD::upper_bound(__first, __last, __value_,
- __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // equal_range
- template <class _Compare, class _ForwardIterator, class _Tp>
- pair<_ForwardIterator, _ForwardIterator>
- __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
- difference_type __len = _VSTD::distance(__first, __last);
- while (__len != 0)
- {
- difference_type __l2 = __len / 2;
- _ForwardIterator __m = __first;
- _VSTD::advance(__m, __l2);
- if (__comp(*__m, __value_))
- {
- __first = ++__m;
- __len -= __l2 + 1;
- }
- else if (__comp(__value_, *__m))
- {
- __last = __m;
- __len = __l2;
- }
- else
- {
- _ForwardIterator __mp1 = __m;
- return pair<_ForwardIterator, _ForwardIterator>
- (
- __lower_bound<_Compare>(__first, __m, __value_, __comp),
- __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
- );
- }
- }
- return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
- }
- template <class _ForwardIterator, class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_ForwardIterator, _ForwardIterator>
- equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_ForwardIterator, _ForwardIterator>
- equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- return _VSTD::equal_range(__first, __last, __value_,
- __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
- }
- // binary_search
- template <class _Compare, class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- __binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
- return __first != __last && !__comp(__value_, *__first);
- }
- template <class _ForwardIterator, class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- return _VSTD::binary_search(__first, __last, __value_,
- __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
- }
- // merge
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- _OutputIterator
- __merge(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- for (; __first1 != __last1; ++__result)
- {
- if (__first2 == __last2)
- return _VSTD::copy(__first1, __last1, __result);
- if (__comp(*__first2, *__first1))
- {
- *__result = *__first2;
- ++__first2;
- }
- else
- {
- *__result = *__first1;
- ++__first1;
- }
- }
- return _VSTD::copy(__first2, __last2, __result);
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- merge(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- merge(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type __v1;
- typedef typename iterator_traits<_InputIterator2>::value_type __v2;
- return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
- }
- // inplace_merge
- template <class _Compare, class _BidirectionalIterator>
- void
- __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
- _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
- typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
- typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
- {
- typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
- typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
- typedef typename iterator_traits<_BidirectionalIterator>::pointer pointer;
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
- if (__len1 <= __len2)
- {
- value_type* __p = __buff;
- for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), ++__i, ++__p)
- ::new(__p) value_type(_VSTD::move(*__i));
- __merge<_Compare>(move_iterator<value_type*>(__buff),
- move_iterator<value_type*>(__p),
- move_iterator<_BidirectionalIterator>(__middle),
- move_iterator<_BidirectionalIterator>(__last),
- __first, __comp);
- }
- else
- {
- value_type* __p = __buff;
- for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), ++__i, ++__p)
- ::new(__p) value_type(_VSTD::move(*__i));
- typedef reverse_iterator<_BidirectionalIterator> _RBi;
- typedef reverse_iterator<value_type*> _Rv;
- __merge(move_iterator<_RBi>(_RBi(__middle)), move_iterator<_RBi>(_RBi(__first)),
- move_iterator<_Rv>(_Rv(__p)), move_iterator<_Rv>(_Rv(__buff)),
- _RBi(__last), __negate<_Compare>(__comp));
- }
- }
- template <class _Compare, class _BidirectionalIterator>
- void
- __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
- _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
- typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
- typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
- {
- typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
- typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
- while (true)
- {
- // if __middle == __last, we're done
- if (__len2 == 0)
- return;
- // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
- for (; true; ++__first, --__len1)
- {
- if (__len1 == 0)
- return;
- if (__comp(*__middle, *__first))
- break;
- }
- if (__len1 <= __buff_size || __len2 <= __buff_size)
- {
- __buffered_inplace_merge<_Compare>(__first, __middle, __last, __comp, __len1, __len2, __buff);
- return;
- }
- // __first < __middle < __last
- // *__first > *__middle
- // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
- // all elements in:
- // [__first, __m1) <= [__middle, __m2)
- // [__middle, __m2) < [__m1, __middle)
- // [__m1, __middle) <= [__m2, __last)
- // and __m1 or __m2 is in the middle of its range
- _BidirectionalIterator __m1; // "median" of [__first, __middle)
- _BidirectionalIterator __m2; // "median" of [__middle, __last)
- difference_type __len11; // distance(__first, __m1)
- difference_type __len21; // distance(__middle, __m2)
- // binary search smaller range
- if (__len1 < __len2)
- { // __len >= 1, __len2 >= 2
- __len21 = __len2 / 2;
- __m2 = __middle;
- _VSTD::advance(__m2, __len21);
- __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
- __len11 = _VSTD::distance(__first, __m1);
- }
- else
- {
- if (__len1 == 1)
- { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
- // It is known *__first > *__middle
- swap(*__first, *__middle);
- return;
- }
- // __len1 >= 2, __len2 >= 1
- __len11 = __len1 / 2;
- __m1 = __first;
- _VSTD::advance(__m1, __len11);
- __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
- __len21 = _VSTD::distance(__middle, __m2);
- }
- difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
- difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
- // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
- // swap middle two partitions
- __middle = _VSTD::rotate(__m1, __middle, __m2);
- // __len12 and __len21 now have swapped meanings
- // merge smaller range with recurisve call and larger with tail recursion elimination
- if (__len11 + __len21 < __len12 + __len22)
- {
- __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
- // __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
- __first = __middle;
- __middle = __m2;
- __len1 = __len12;
- __len2 = __len22;
- }
- else
- {
- __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
- // __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
- __last = __middle;
- __middle = __m1;
- __len1 = __len11;
- __len2 = __len21;
- }
- }
- }
- template <class _Tp>
- struct __inplace_merge_switch
- {
- static const unsigned value = is_trivially_copy_assignable<_Tp>::value;
- };
- template <class _BidirectionalIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
- _Compare __comp)
- {
- typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
- typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
- difference_type __len1 = _VSTD::distance(__first, __middle);
- difference_type __len2 = _VSTD::distance(__middle, __last);
- difference_type __buf_size = _VSTD::min(__len1, __len2);
- pair<value_type*, ptrdiff_t> __buf(0, 0);
- unique_ptr<value_type, __return_temporary_buffer> __h;
- if (__inplace_merge_switch<value_type>::value && __buf_size > 8)
- {
- __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
- __h.reset(__buf.first);
- }
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
- __buf.first, __buf.second);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
- __buf.first, __buf.second);
- #endif // _LIBCPP_DEBUG
- }
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
- {
- _VSTD::inplace_merge(__first, __middle, __last,
- __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
- }
- // stable_sort
- template <class _Compare, class _InputIterator1, class _InputIterator2>
- void
- __merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type value_type;
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h(__result, __d);
- for (; true; ++__result)
- {
- if (__first1 == __last1)
- {
- for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
- ::new (__result) value_type(_VSTD::move(*__first2));
- __h.release();
- return;
- }
- if (__first2 == __last2)
- {
- for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
- ::new (__result) value_type(_VSTD::move(*__first1));
- __h.release();
- return;
- }
- if (__comp(*__first2, *__first1))
- {
- ::new (__result) value_type(_VSTD::move(*__first2));
- __d.__incr((value_type*)0);
- ++__first2;
- }
- else
- {
- ::new (__result) value_type(_VSTD::move(*__first1));
- __d.__incr((value_type*)0);
- ++__first1;
- }
- }
- }
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- void
- __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- for (; __first1 != __last1; ++__result)
- {
- if (__first2 == __last2)
- {
- for (; __first1 != __last1; ++__first1, ++__result)
- *__result = _VSTD::move(*__first1);
- return;
- }
- if (__comp(*__first2, *__first1))
- {
- *__result = _VSTD::move(*__first2);
- ++__first2;
- }
- else
- {
- *__result = _VSTD::move(*__first1);
- ++__first1;
- }
- }
- for (; __first2 != __last2; ++__first2, ++__result)
- *__result = _VSTD::move(*__first2);
- }
- template <class _Compare, class _RandomAccessIterator>
- void
- __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len,
- typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
- template <class _Compare, class _RandomAccessIterator>
- void
- __stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len,
- typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- switch (__len)
- {
- case 0:
- return;
- case 1:
- ::new(__first2) value_type(_VSTD::move(*__first1));
- return;
- case 2:
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
- if (__comp(*--__last1, *__first1))
- {
- ::new(__first2) value_type(_VSTD::move(*__last1));
- __d.__incr((value_type*)0);
- ++__first2;
- ::new(__first2) value_type(_VSTD::move(*__first1));
- }
- else
- {
- ::new(__first2) value_type(_VSTD::move(*__first1));
- __d.__incr((value_type*)0);
- ++__first2;
- ::new(__first2) value_type(_VSTD::move(*__last1));
- }
- __h2.release();
- return;
- }
- if (__len <= 8)
- {
- __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
- return;
- }
- typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
- _RandomAccessIterator __m = __first1 + __l2;
- __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
- __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
- __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
- }
- template <class _Tp>
- struct __stable_sort_switch
- {
- static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
- };
- template <class _Compare, class _RandomAccessIterator>
- void
- __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len,
- typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- switch (__len)
- {
- case 0:
- case 1:
- return;
- case 2:
- if (__comp(*--__last, *__first))
- swap(*__first, *__last);
- return;
- }
- if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
- {
- __insertion_sort<_Compare>(__first, __last, __comp);
- return;
- }
- typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
- _RandomAccessIterator __m = __first + __l2;
- if (__len <= __buff_size)
- {
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
- __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
- __d.__set(__l2, (value_type*)0);
- __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
- __d.__set(__len, (value_type*)0);
- __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
- // __merge<_Compare>(move_iterator<value_type*>(__buff),
- // move_iterator<value_type*>(__buff + __l2),
- // move_iterator<_RandomAccessIterator>(__buff + __l2),
- // move_iterator<_RandomAccessIterator>(__buff + __len),
- // __first, __comp);
- return;
- }
- __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
- __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
- __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- difference_type __len = __last - __first;
- pair<value_type*, ptrdiff_t> __buf(0, 0);
- unique_ptr<value_type, __return_temporary_buffer> __h;
- if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
- {
- __buf = _VSTD::get_temporary_buffer<value_type>(__len);
- __h.reset(__buf.first);
- }
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // is_heap_until
- template <class _RandomAccessIterator, class _Compare>
- _RandomAccessIterator
- is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- difference_type __len = __last - __first;
- difference_type __p = 0;
- difference_type __c = 1;
- _RandomAccessIterator __pp = __first;
- while (__c < __len)
- {
- _RandomAccessIterator __cp = __first + __c;
- if (__comp(*__pp, *__cp))
- return __cp;
- ++__c;
- ++__cp;
- if (__c == __len)
- return __last;
- if (__comp(*__pp, *__cp))
- return __cp;
- ++__p;
- ++__pp;
- __c = 2 * __p + 1;
- }
- return __last;
- }
- template<class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _RandomAccessIterator
- is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // is_heap
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- return _VSTD::is_heap_until(__first, __last, __comp) == __last;
- }
- template<class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // push_heap
- template <class _Compare, class _RandomAccessIterator>
- void
- __push_heap_front(_RandomAccessIterator __first, _RandomAccessIterator, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- if (__len > 1)
- {
- difference_type __p = 0;
- _RandomAccessIterator __pp = __first;
- difference_type __c = 2;
- _RandomAccessIterator __cp = __first + __c;
- if (__c == __len || __comp(*__cp, *(__cp - 1)))
- {
- --__c;
- --__cp;
- }
- if (__comp(*__pp, *__cp))
- {
- value_type __t(_VSTD::move(*__pp));
- do
- {
- *__pp = _VSTD::move(*__cp);
- __pp = __cp;
- __p = __c;
- __c = (__p + 1) * 2;
- if (__c > __len)
- break;
- __cp = __first + __c;
- if (__c == __len || __comp(*__cp, *(__cp - 1)))
- {
- --__c;
- --__cp;
- }
- } while (__comp(__t, *__cp));
- *__pp = _VSTD::move(__t);
- }
- }
- }
- template <class _Compare, class _RandomAccessIterator>
- void
- __push_heap_back(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- if (__len > 1)
- {
- __len = (__len - 2) / 2;
- _RandomAccessIterator __ptr = __first + __len;
- if (__comp(*__ptr, *--__last))
- {
- value_type __t(_VSTD::move(*__last));
- do
- {
- *__last = _VSTD::move(*__ptr);
- __last = __ptr;
- if (__len == 0)
- break;
- __len = (__len - 1) / 2;
- __ptr = __first + __len;
- } while (__comp(*__ptr, __t));
- *__last = _VSTD::move(__t);
- }
- }
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __push_heap_back<_Comp_ref>(__first, __last, __c, __last - __first);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __push_heap_back<_Comp_ref>(__first, __last, __comp, __last - __first);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // pop_heap
- template <class _Compare, class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len)
- {
- if (__len > 1)
- {
- swap(*__first, *--__last);
- __push_heap_front<_Compare>(__first, __last, __comp, __len-1);
- }
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // make_heap
- template <class _Compare, class _RandomAccessIterator>
- void
- __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- difference_type __n = __last - __first;
- if (__n > 1)
- {
- __last = __first;
- ++__last;
- for (difference_type __i = 1; __i < __n;)
- __push_heap_back<_Compare>(__first, ++__last, __comp, ++__i);
- }
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __make_heap<_Comp_ref>(__first, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __make_heap<_Comp_ref>(__first, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // sort_heap
- template <class _Compare, class _RandomAccessIterator>
- void
- __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
- __pop_heap<_Compare>(__first, __last, __comp, __n);
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __sort_heap<_Comp_ref>(__first, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __sort_heap<_Comp_ref>(__first, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // partial_sort
- template <class _Compare, class _RandomAccessIterator>
- void
- __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
- _Compare __comp)
- {
- __make_heap<_Compare>(__first, __middle, __comp);
- typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
- for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
- {
- if (__comp(*__i, *__first))
- {
- swap(*__i, *__first);
- __push_heap_front<_Compare>(__first, __middle, __comp, __len);
- }
- }
- __sort_heap<_Compare>(__first, __middle, __comp);
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
- _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
- {
- _VSTD::partial_sort(__first, __middle, __last,
- __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // partial_sort_copy
- template <class _Compare, class _InputIterator, class _RandomAccessIterator>
- _RandomAccessIterator
- __partial_sort_copy(_InputIterator __first, _InputIterator __last,
- _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
- {
- _RandomAccessIterator __r = __result_first;
- if (__r != __result_last)
- {
- typename iterator_traits<_RandomAccessIterator>::difference_type __len = 0;
- for (; __first != __last && __r != __result_last; ++__first, ++__r, ++__len)
- *__r = *__first;
- __make_heap<_Compare>(__result_first, __r, __comp);
- for (; __first != __last; ++__first)
- if (__comp(*__first, *__result_first))
- {
- *__result_first = *__first;
- __push_heap_front<_Compare>(__result_first, __r, __comp, __len);
- }
- __sort_heap<_Compare>(__result_first, __r, __comp);
- }
- return __r;
- }
- template <class _InputIterator, class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _RandomAccessIterator
- partial_sort_copy(_InputIterator __first, _InputIterator __last,
- _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator, class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _RandomAccessIterator
- partial_sort_copy(_InputIterator __first, _InputIterator __last,
- _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
- {
- return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
- __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // nth_element
- template <class _Compare, class _RandomAccessIterator>
- void
- __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
- {
- // _Compare is known to be a reference type
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- const difference_type __limit = 7;
- while (true)
- {
- __restart:
- if (__nth == __last)
- return;
- difference_type __len = __last - __first;
- switch (__len)
- {
- case 0:
- case 1:
- return;
- case 2:
- if (__comp(*--__last, *__first))
- swap(*__first, *__last);
- return;
- case 3:
- {
- _RandomAccessIterator __m = __first;
- _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
- return;
- }
- }
- if (__len <= __limit)
- {
- __selection_sort<_Compare>(__first, __last, __comp);
- return;
- }
- // __len > __limit >= 3
- _RandomAccessIterator __m = __first + __len/2;
- _RandomAccessIterator __lm1 = __last;
- unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
- // *__m is median
- // partition [__first, __m) < *__m and *__m <= [__m, __last)
- // (this inhibits tossing elements equivalent to __m around unnecessarily)
- _RandomAccessIterator __i = __first;
- _RandomAccessIterator __j = __lm1;
- // j points beyond range to be tested, *__lm1 is known to be <= *__m
- // The search going up is known to be guarded but the search coming down isn't.
- // Prime the downward search with a guard.
- if (!__comp(*__i, *__m)) // if *__first == *__m
- {
- // *__first == *__m, *__first doesn't go in first part
- // manually guard downward moving __j against __i
- while (true)
- {
- if (__i == --__j)
- {
- // *__first == *__m, *__m <= all other elements
- // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
- ++__i; // __first + 1
- __j = __last;
- if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
- {
- while (true)
- {
- if (__i == __j)
- return; // [__first, __last) all equivalent elements
- if (__comp(*__first, *__i))
- {
- swap(*__i, *__j);
- ++__n_swaps;
- ++__i;
- break;
- }
- ++__i;
- }
- }
- // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
- if (__i == __j)
- return;
- while (true)
- {
- while (!__comp(*__first, *__i))
- ++__i;
- while (__comp(*__first, *--__j))
- ;
- if (__i >= __j)
- break;
- swap(*__i, *__j);
- ++__n_swaps;
- ++__i;
- }
- // [__first, __i) == *__first and *__first < [__i, __last)
- // The first part is sorted,
- if (__nth < __i)
- return;
- // __nth_element the secod part
- // __nth_element<_Compare>(__i, __nth, __last, __comp);
- __first = __i;
- goto __restart;
- }
- if (__comp(*__j, *__m))
- {
- swap(*__i, *__j);
- ++__n_swaps;
- break; // found guard for downward moving __j, now use unguarded partition
- }
- }
- }
- ++__i;
- // j points beyond range to be tested, *__lm1 is known to be <= *__m
- // if not yet partitioned...
- if (__i < __j)
- {
- // known that *(__i - 1) < *__m
- while (true)
- {
- // __m still guards upward moving __i
- while (__comp(*__i, *__m))
- ++__i;
- // It is now known that a guard exists for downward moving __j
- while (!__comp(*--__j, *__m))
- ;
- if (__i >= __j)
- break;
- swap(*__i, *__j);
- ++__n_swaps;
- // It is known that __m != __j
- // If __m just moved, follow it
- if (__m == __i)
- __m = __j;
- ++__i;
- }
- }
- // [__first, __i) < *__m and *__m <= [__i, __last)
- if (__i != __m && __comp(*__m, *__i))
- {
- swap(*__i, *__m);
- ++__n_swaps;
- }
- // [__first, __i) < *__i and *__i <= [__i+1, __last)
- if (__nth == __i)
- return;
- if (__n_swaps == 0)
- {
- // We were given a perfectly partitioned sequence. Coincidence?
- if (__nth < __i)
- {
- // Check for [__first, __i) already sorted
- __j = __m = __first;
- while (++__j != __i)
- {
- if (__comp(*__j, *__m))
- // not yet sorted, so sort
- goto not_sorted;
- __m = __j;
- }
- // [__first, __i) sorted
- return;
- }
- else
- {
- // Check for [__i, __last) already sorted
- __j = __m = __i;
- while (++__j != __last)
- {
- if (__comp(*__j, *__m))
- // not yet sorted, so sort
- goto not_sorted;
- __m = __j;
- }
- // [__i, __last) sorted
- return;
- }
- }
- not_sorted:
- // __nth_element on range containing __nth
- if (__nth < __i)
- {
- // __nth_element<_Compare>(__first, __nth, __i, __comp);
- __last = __i;
- }
- else
- {
- // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
- __first = ++__i;
- }
- }
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __nth_element<_Comp_ref>(__first, __nth, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
- {
- _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // includes
- template <class _Compare, class _InputIterator1, class _InputIterator2>
- bool
- __includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
- _Compare __comp)
- {
- for (; __first2 != __last2; ++__first1)
- {
- if (__first1 == __last1 || __comp(*__first2, *__first1))
- return false;
- if (!__comp(*__first1, *__first2))
- ++__first2;
- }
- return true;
- }
- template <class _InputIterator1, class _InputIterator2, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
- _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
- {
- return _VSTD::includes(__first1, __last1, __first2, __last2,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // set_union
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- _OutputIterator
- __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- for (; __first1 != __last1; ++__result)
- {
- if (__first2 == __last2)
- return _VSTD::copy(__first1, __last1, __result);
- if (__comp(*__first2, *__first1))
- {
- *__result = *__first2;
- ++__first2;
- }
- else
- {
- *__result = *__first1;
- if (!__comp(*__first1, *__first2))
- ++__first2;
- ++__first1;
- }
- }
- return _VSTD::copy(__first2, __last2, __result);
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_union(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_union(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
- {
- return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // set_intersection
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- _OutputIterator
- __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1 && __first2 != __last2)
- {
- if (__comp(*__first1, *__first2))
- ++__first1;
- else
- {
- if (!__comp(*__first2, *__first1))
- {
- *__result = *__first1;
- ++__result;
- ++__first1;
- }
- ++__first2;
- }
- }
- return __result;
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
- {
- return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // set_difference
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- _OutputIterator
- __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1)
- {
- if (__first2 == __last2)
- return _VSTD::copy(__first1, __last1, __result);
- if (__comp(*__first1, *__first2))
- {
- *__result = *__first1;
- ++__result;
- ++__first1;
- }
- else
- {
- if (!__comp(*__first2, *__first1))
- ++__first1;
- ++__first2;
- }
- }
- return __result;
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
- {
- return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // set_symmetric_difference
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- _OutputIterator
- __set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1)
- {
- if (__first2 == __last2)
- return _VSTD::copy(__first1, __last1, __result);
- if (__comp(*__first1, *__first2))
- {
- *__result = *__first1;
- ++__result;
- ++__first1;
- }
- else
- {
- if (__comp(*__first2, *__first1))
- {
- *__result = *__first2;
- ++__result;
- }
- else
- ++__first1;
- ++__first2;
- }
- }
- return _VSTD::copy(__first2, __last2, __result);
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
- {
- return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // lexicographical_compare
- template <class _Compare, class _InputIterator1, class _InputIterator2>
- bool
- __lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
- {
- for (; __first2 != __last2; ++__first1, ++__first2)
- {
- if (__first1 == __last1 || __comp(*__first1, *__first2))
- return true;
- if (__comp(*__first2, *__first1))
- return false;
- }
- return false;
- }
- template <class _InputIterator1, class _InputIterator2, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2)
- {
- return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // next_permutation
- template <class _Compare, class _BidirectionalIterator>
- bool
- __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
- {
- _BidirectionalIterator __i = __last;
- if (__first == __last || __first == --__i)
- return false;
- while (true)
- {
- _BidirectionalIterator __ip1 = __i;
- if (__comp(*--__i, *__ip1))
- {
- _BidirectionalIterator __j = __last;
- while (!__comp(*__i, *--__j))
- ;
- swap(*__i, *__j);
- _VSTD::reverse(__ip1, __last);
- return true;
- }
- if (__i == __first)
- {
- _VSTD::reverse(__first, __last);
- return false;
- }
- }
- }
- template <class _BidirectionalIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __next_permutation<_Comp_ref>(__first, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __next_permutation<_Comp_ref>(__first, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
- {
- return _VSTD::next_permutation(__first, __last,
- __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
- }
- // prev_permutation
- template <class _Compare, class _BidirectionalIterator>
- bool
- __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
- {
- _BidirectionalIterator __i = __last;
- if (__first == __last || __first == --__i)
- return false;
- while (true)
- {
- _BidirectionalIterator __ip1 = __i;
- if (__comp(*__ip1, *--__i))
- {
- _BidirectionalIterator __j = __last;
- while (!__comp(*--__j, *__i))
- ;
- swap(*__i, *__j);
- _VSTD::reverse(__ip1, __last);
- return true;
- }
- if (__i == __first)
- {
- _VSTD::reverse(__first, __last);
- return false;
- }
- }
- }
- template <class _BidirectionalIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __prev_permutation<_Comp_ref>(__first, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __prev_permutation<_Comp_ref>(__first, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
- {
- return _VSTD::prev_permutation(__first, __last,
- __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_integral<_Tp>::value,
- _Tp
- >::type
- __rotate_left(_Tp __t, _Tp __n = 1)
- {
- const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
- __n &= __bits;
- return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_integral<_Tp>::value,
- _Tp
- >::type
- __rotate_right(_Tp __t, _Tp __n = 1)
- {
- const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
- __n &= __bits;
- return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
- }
- _LIBCPP_END_NAMESPACE_STD
- #endif // _LIBCPP_ALGORITHM
|