algorithm 180 KB

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