algorithm 195 KB

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