algorithm 200 KB

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