algorithm 196 KB

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