algorithm 201 KB

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