algorithm 183 KB

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