algorithm 200 KB

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