algorithm 204 KB

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