algorithm 200 KB

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