algorithm 192 KB

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