1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761 |
- // -*- C++ -*-
- //===-------------------------- algorithm ---------------------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is dual licensed under the MIT and the University of Illinois Open
- // Source Licenses. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- #ifndef _LIBCPP_ALGORITHM
- #define _LIBCPP_ALGORITHM
- /*
- algorithm synopsis
- #include <initializer_list>
- namespace std
- {
- template <class InputIterator, class Predicate>
- bool
- all_of(InputIterator first, InputIterator last, Predicate pred);
- template <class InputIterator, class Predicate>
- bool
- any_of(InputIterator first, InputIterator last, Predicate pred);
- template <class InputIterator, class Predicate>
- bool
- none_of(InputIterator first, InputIterator last, Predicate pred);
- template <class InputIterator, class Function>
- Function
- for_each(InputIterator first, InputIterator last, Function f);
- template <class InputIterator, class T>
- InputIterator
- find(InputIterator first, InputIterator last, const T& value);
- template <class InputIterator, class Predicate>
- InputIterator
- find_if(InputIterator first, InputIterator last, Predicate pred);
- template<class InputIterator, class Predicate>
- InputIterator
- find_if_not(InputIterator first, InputIterator last, Predicate pred);
- template <class ForwardIterator1, class ForwardIterator2>
- ForwardIterator1
- find_end(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2);
- template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
- ForwardIterator1
- find_end(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
- template <class ForwardIterator1, class ForwardIterator2>
- ForwardIterator1
- find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2);
- template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
- ForwardIterator1
- find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
- template <class ForwardIterator>
- ForwardIterator
- adjacent_find(ForwardIterator first, ForwardIterator last);
- template <class ForwardIterator, class BinaryPredicate>
- ForwardIterator
- adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
- template <class InputIterator, class T>
- typename iterator_traits<InputIterator>::difference_type
- count(InputIterator first, InputIterator last, const T& value);
- template <class InputIterator, class Predicate>
- typename iterator_traits<InputIterator>::difference_type
- count_if(InputIterator first, InputIterator last, Predicate pred);
- template <class InputIterator1, class InputIterator2>
- pair<InputIterator1, InputIterator2>
- mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
- template <class InputIterator1, class InputIterator2>
- pair<InputIterator1, InputIterator2>
- mismatch(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2); // **C++14**
- template <class InputIterator1, class InputIterator2, class BinaryPredicate>
- pair<InputIterator1, InputIterator2>
- mismatch(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, BinaryPredicate pred);
- template <class InputIterator1, class InputIterator2, class BinaryPredicate>
- pair<InputIterator1, InputIterator2>
- mismatch(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2,
- BinaryPredicate pred); // **C++14**
- template <class InputIterator1, class InputIterator2>
- bool
- equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
- template <class InputIterator1, class InputIterator2>
- bool
- equal(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2); // **C++14**
- template <class InputIterator1, class InputIterator2, class BinaryPredicate>
- bool
- equal(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, BinaryPredicate pred);
- template <class InputIterator1, class InputIterator2, class BinaryPredicate>
- bool
- equal(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2,
- BinaryPredicate pred); // **C++14**
- template<class ForwardIterator1, class ForwardIterator2>
- bool
- is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2);
- template<class ForwardIterator1, class ForwardIterator2>
- bool
- is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
- template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
- bool
- is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, BinaryPredicate pred);
- template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
- bool
- is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2,
- BinaryPredicate pred); // **C++14**
- template <class ForwardIterator1, class ForwardIterator2>
- ForwardIterator1
- search(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2);
- template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
- ForwardIterator1
- search(ForwardIterator1 first1, ForwardIterator1 last1,
- ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
- template <class ForwardIterator, class Size, class T>
- ForwardIterator
- search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
- template <class ForwardIterator, class Size, class T, class BinaryPredicate>
- ForwardIterator
- search_n(ForwardIterator first, ForwardIterator last,
- Size count, const T& value, BinaryPredicate pred);
- template <class InputIterator, class OutputIterator>
- OutputIterator
- copy(InputIterator first, InputIterator last, OutputIterator result);
- template<class InputIterator, class OutputIterator, class Predicate>
- OutputIterator
- copy_if(InputIterator first, InputIterator last,
- OutputIterator result, Predicate pred);
- template<class InputIterator, class Size, class OutputIterator>
- OutputIterator
- copy_n(InputIterator first, Size n, OutputIterator result);
- template <class BidirectionalIterator1, class BidirectionalIterator2>
- BidirectionalIterator2
- copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
- BidirectionalIterator2 result);
- template <class ForwardIterator1, class ForwardIterator2>
- ForwardIterator2
- swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
- template <class ForwardIterator1, class ForwardIterator2>
- void
- iter_swap(ForwardIterator1 a, ForwardIterator2 b);
- template <class InputIterator, class OutputIterator, class UnaryOperation>
- OutputIterator
- transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
- OutputIterator
- transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
- OutputIterator result, BinaryOperation binary_op);
- template <class ForwardIterator, class T>
- void
- replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
- template <class ForwardIterator, class Predicate, class T>
- void
- replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
- template <class InputIterator, class OutputIterator, class T>
- OutputIterator
- replace_copy(InputIterator first, InputIterator last, OutputIterator result,
- const T& old_value, const T& new_value);
- template <class InputIterator, class OutputIterator, class Predicate, class T>
- OutputIterator
- replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
- template <class ForwardIterator, class T>
- void
- fill(ForwardIterator first, ForwardIterator last, const T& value);
- template <class OutputIterator, class Size, class T>
- OutputIterator
- fill_n(OutputIterator first, Size n, const T& value);
- template <class ForwardIterator, class Generator>
- void
- generate(ForwardIterator first, ForwardIterator last, Generator gen);
- template <class OutputIterator, class Size, class Generator>
- OutputIterator
- generate_n(OutputIterator first, Size n, Generator gen);
- template <class ForwardIterator, class T>
- ForwardIterator
- remove(ForwardIterator first, ForwardIterator last, const T& value);
- template <class ForwardIterator, class Predicate>
- ForwardIterator
- remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
- template <class InputIterator, class OutputIterator, class T>
- OutputIterator
- remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
- template <class InputIterator, class OutputIterator, class Predicate>
- OutputIterator
- remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
- template <class ForwardIterator>
- ForwardIterator
- unique(ForwardIterator first, ForwardIterator last);
- template <class ForwardIterator, class BinaryPredicate>
- ForwardIterator
- unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
- template <class InputIterator, class OutputIterator>
- OutputIterator
- unique_copy(InputIterator first, InputIterator last, OutputIterator result);
- template <class InputIterator, class OutputIterator, class BinaryPredicate>
- OutputIterator
- unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
- template <class BidirectionalIterator>
- void
- reverse(BidirectionalIterator first, BidirectionalIterator last);
- template <class BidirectionalIterator, class OutputIterator>
- OutputIterator
- reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
- template <class ForwardIterator>
- ForwardIterator
- rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
- template <class ForwardIterator, class OutputIterator>
- OutputIterator
- rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
- template <class RandomAccessIterator>
- void
- random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14
- template <class RandomAccessIterator, class RandomNumberGenerator>
- void
- random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
- RandomNumberGenerator& rand); // deprecated in C++14
- template<class RandomAccessIterator, class UniformRandomNumberGenerator>
- void shuffle(RandomAccessIterator first, RandomAccessIterator last,
- UniformRandomNumberGenerator&& g);
- template <class InputIterator, class Predicate>
- bool
- is_partitioned(InputIterator first, InputIterator last, Predicate pred);
- template <class ForwardIterator, class Predicate>
- ForwardIterator
- partition(ForwardIterator first, ForwardIterator last, Predicate pred);
- template <class InputIterator, class OutputIterator1,
- class OutputIterator2, class Predicate>
- pair<OutputIterator1, OutputIterator2>
- partition_copy(InputIterator first, InputIterator last,
- OutputIterator1 out_true, OutputIterator2 out_false,
- Predicate pred);
- template <class ForwardIterator, class Predicate>
- ForwardIterator
- stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
- template<class ForwardIterator, class Predicate>
- ForwardIterator
- partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
- template <class ForwardIterator>
- bool
- is_sorted(ForwardIterator first, ForwardIterator last);
- template <class ForwardIterator, class Compare>
- bool
- is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
- template<class ForwardIterator>
- ForwardIterator
- is_sorted_until(ForwardIterator first, ForwardIterator last);
- template <class ForwardIterator, class Compare>
- ForwardIterator
- is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- sort(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- stable_sort(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
- template <class InputIterator, class RandomAccessIterator>
- RandomAccessIterator
- partial_sort_copy(InputIterator first, InputIterator last,
- RandomAccessIterator result_first, RandomAccessIterator result_last);
- template <class InputIterator, class RandomAccessIterator, class Compare>
- RandomAccessIterator
- partial_sort_copy(InputIterator first, InputIterator last,
- RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
- template <class RandomAccessIterator>
- void
- nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
- template <class ForwardIterator, class T>
- ForwardIterator
- lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
- template <class ForwardIterator, class T, class Compare>
- ForwardIterator
- lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
- template <class ForwardIterator, class T>
- ForwardIterator
- upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
- template <class ForwardIterator, class T, class Compare>
- ForwardIterator
- upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
- template <class ForwardIterator, class T>
- pair<ForwardIterator, ForwardIterator>
- equal_range(ForwardIterator first, ForwardIterator last, const T& value);
- template <class ForwardIterator, class T, class Compare>
- pair<ForwardIterator, ForwardIterator>
- equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
- template <class ForwardIterator, class T>
- bool
- binary_search(ForwardIterator first, ForwardIterator last, const T& value);
- template <class ForwardIterator, class T, class Compare>
- bool
- binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
- template <class InputIterator1, class InputIterator2, class OutputIterator>
- OutputIterator
- merge(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
- OutputIterator
- merge(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
- template <class BidirectionalIterator>
- void
- inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
- template <class BidirectionalIterator, class Compare>
- void
- inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
- template <class InputIterator1, class InputIterator2>
- bool
- includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
- template <class InputIterator1, class InputIterator2, class Compare>
- bool
- includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
- template <class InputIterator1, class InputIterator2, class OutputIterator>
- OutputIterator
- set_union(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
- OutputIterator
- set_union(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
- template <class InputIterator1, class InputIterator2, class OutputIterator>
- OutputIterator
- set_intersection(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
- OutputIterator
- set_intersection(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
- template <class InputIterator1, class InputIterator2, class OutputIterator>
- OutputIterator
- set_difference(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
- OutputIterator
- set_difference(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
- template <class InputIterator1, class InputIterator2, class OutputIterator>
- OutputIterator
- set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result);
- template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
- OutputIterator
- set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
- template <class RandomAccessIterator>
- void
- push_heap(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- pop_heap(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- make_heap(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- void
- sort_heap(RandomAccessIterator first, RandomAccessIterator last);
- template <class RandomAccessIterator, class Compare>
- void
- sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- template <class RandomAccessIterator>
- bool
- is_heap(RandomAccessIterator first, RandomAccessiterator last);
- template <class RandomAccessIterator, class Compare>
- bool
- is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
- template <class RandomAccessIterator>
- RandomAccessIterator
- is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
- template <class RandomAccessIterator, class Compare>
- RandomAccessIterator
- is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
- template <class ForwardIterator>
- ForwardIterator
- min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
- template <class ForwardIterator, class Compare>
- ForwardIterator
- min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
- template <class T>
- const T&
- min(const T& a, const T& b); // constexpr in C++14
- template <class T, class Compare>
- const T&
- min(const T& a, const T& b, Compare comp); // constexpr in C++14
- template<class T>
- T
- min(initializer_list<T> t); // constexpr in C++14
- template<class T, class Compare>
- T
- min(initializer_list<T> t, Compare comp); // constexpr in C++14
- template<class T>
- constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
- template<class T, class Compare>
- constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
- template <class ForwardIterator>
- ForwardIterator
- max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
- template <class ForwardIterator, class Compare>
- ForwardIterator
- max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
- template <class T>
- const T&
- max(const T& a, const T& b); // constexpr in C++14
- template <class T, class Compare>
- const T&
- max(const T& a, const T& b, Compare comp); // constexpr in C++14
- template<class T>
- T
- max(initializer_list<T> t); // constexpr in C++14
- template<class T, class Compare>
- T
- max(initializer_list<T> t, Compare comp); // constexpr in C++14
- template<class ForwardIterator>
- pair<ForwardIterator, ForwardIterator>
- minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
- template<class ForwardIterator, class Compare>
- pair<ForwardIterator, ForwardIterator>
- minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
- template<class T>
- pair<const T&, const T&>
- minmax(const T& a, const T& b); // constexpr in C++14
- template<class T, class Compare>
- pair<const T&, const T&>
- minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
- template<class T>
- pair<T, T>
- minmax(initializer_list<T> t); // constexpr in C++14
- template<class T, class Compare>
- pair<T, T>
- minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
- template <class InputIterator1, class InputIterator2>
- bool
- lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
- template <class InputIterator1, class InputIterator2, class Compare>
- bool
- lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, InputIterator2 last2, Compare comp);
- template <class BidirectionalIterator>
- bool
- next_permutation(BidirectionalIterator first, BidirectionalIterator last);
- template <class BidirectionalIterator, class Compare>
- bool
- next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
- template <class BidirectionalIterator>
- bool
- prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
- template <class BidirectionalIterator, class Compare>
- bool
- prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
- } // std
- */
- #include <__config>
- #include <initializer_list>
- #include <type_traits>
- #include <cstring>
- #include <utility> // needed to provide swap_ranges.
- #include <memory>
- #include <iterator>
- #include <cstddef>
- #if defined(__IBMCPP__)
- #include "support/ibm/support.h"
- #endif
- #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
- #include "support/win32/support.h"
- #endif
- #include <__undef_min_max>
- #include <__debug>
- #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
- #pragma GCC system_header
- #endif
- _LIBCPP_BEGIN_NAMESPACE_STD
- // I'd like to replace these with _VSTD::equal_to<void>, but can't because:
- // * That only works with C++14 and later, and
- // * We haven't included <functional> here.
- template <class _T1, class _T2 = _T1>
- struct __equal_to
- {
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
- _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
- };
- template <class _T1>
- struct __equal_to<_T1, _T1>
- {
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
- };
- template <class _T1>
- struct __equal_to<const _T1, _T1>
- {
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
- };
- template <class _T1>
- struct __equal_to<_T1, const _T1>
- {
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
- };
- template <class _T1, class _T2 = _T1>
- struct __less
- {
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
- };
- template <class _T1>
- struct __less<_T1, _T1>
- {
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
- };
- template <class _T1>
- struct __less<const _T1, _T1>
- {
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
- };
- template <class _T1>
- struct __less<_T1, const _T1>
- {
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
- };
- template <class _Predicate>
- class __negate
- {
- private:
- _Predicate __p_;
- public:
- _LIBCPP_INLINE_VISIBILITY __negate() {}
- _LIBCPP_INLINE_VISIBILITY
- explicit __negate(_Predicate __p) : __p_(__p) {}
- template <class _T1>
- _LIBCPP_INLINE_VISIBILITY
- bool operator()(const _T1& __x) {return !__p_(__x);}
- template <class _T1, class _T2>
- _LIBCPP_INLINE_VISIBILITY
- bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
- };
- #ifdef _LIBCPP_DEBUG
- template <class _Compare>
- struct __debug_less
- {
- _Compare __comp_;
- __debug_less(_Compare& __c) : __comp_(__c) {}
- template <class _Tp, class _Up>
- bool operator()(const _Tp& __x, const _Up& __y)
- {
- bool __r = __comp_(__x, __y);
- if (__r)
- _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
- return __r;
- }
- };
- #endif // _LIBCPP_DEBUG
- // Precondition: __x != 0
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned
- __ctz(unsigned __x)
- {
- return static_cast<unsigned>(__builtin_ctz(__x));
- }
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned long
- __ctz(unsigned long __x)
- {
- return static_cast<unsigned long>(__builtin_ctzl(__x));
- }
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned long long
- __ctz(unsigned long long __x)
- {
- return static_cast<unsigned long long>(__builtin_ctzll(__x));
- }
- // Precondition: __x != 0
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned
- __clz(unsigned __x)
- {
- return static_cast<unsigned>(__builtin_clz(__x));
- }
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned long
- __clz(unsigned long __x)
- {
- return static_cast<unsigned long>(__builtin_clzl (__x));
- }
- inline _LIBCPP_INLINE_VISIBILITY
- unsigned long long
- __clz(unsigned long long __x)
- {
- return static_cast<unsigned long long>(__builtin_clzll(__x));
- }
- inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);}
- inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);}
- inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
- // all_of
- template <class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (!__pred(*__first))
- return false;
- return true;
- }
- // any_of
- template <class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- return true;
- return false;
- }
- // none_of
- template <class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- return false;
- return true;
- }
- // for_each
- template <class _InputIterator, class _Function>
- inline _LIBCPP_INLINE_VISIBILITY
- _Function
- for_each(_InputIterator __first, _InputIterator __last, _Function __f)
- {
- for (; __first != __last; ++__first)
- __f(*__first);
- return _LIBCPP_EXPLICIT_MOVE(__f); // explicitly moved for (emulated) C++03
- }
- // find
- template <class _InputIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _InputIterator
- find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
- {
- for (; __first != __last; ++__first)
- if (*__first == __value_)
- break;
- return __first;
- }
- // find_if
- template <class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _InputIterator
- find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- break;
- return __first;
- }
- // find_if_not
- template<class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _InputIterator
- find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (!__pred(*__first))
- break;
- return __first;
- }
- // find_end
- template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
- _ForwardIterator1
- __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
- forward_iterator_tag, forward_iterator_tag)
- {
- // modeled after search algorithm
- _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
- if (__first2 == __last2)
- return __r;
- while (true)
- {
- while (true)
- {
- if (__first1 == __last1) // if source exhausted return last correct answer
- return __r; // (or __last1 if never found)
- if (__pred(*__first1, *__first2))
- break;
- ++__first1;
- }
- // *__first1 matches *__first2, now match elements after here
- _ForwardIterator1 __m1 = __first1;
- _ForwardIterator2 __m2 = __first2;
- while (true)
- {
- if (++__m2 == __last2)
- { // Pattern exhaused, record answer and search for another one
- __r = __first1;
- ++__first1;
- break;
- }
- if (++__m1 == __last1) // Source exhausted, return last answer
- return __r;
- if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
- {
- ++__first1;
- break;
- } // else there is a match, check next elements
- }
- }
- }
- template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
- _BidirectionalIterator1
- __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
- _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
- bidirectional_iterator_tag, bidirectional_iterator_tag)
- {
- // modeled after search algorithm (in reverse)
- if (__first2 == __last2)
- return __last1; // Everything matches an empty sequence
- _BidirectionalIterator1 __l1 = __last1;
- _BidirectionalIterator2 __l2 = __last2;
- --__l2;
- while (true)
- {
- // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
- while (true)
- {
- if (__first1 == __l1) // return __last1 if no element matches *__first2
- return __last1;
- if (__pred(*--__l1, *__l2))
- break;
- }
- // *__l1 matches *__l2, now match elements before here
- _BidirectionalIterator1 __m1 = __l1;
- _BidirectionalIterator2 __m2 = __l2;
- while (true)
- {
- if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
- return __m1;
- if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
- return __last1;
- if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
- {
- break;
- } // else there is a match, check next elements
- }
- }
- }
- template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
- _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
- __find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
- _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
- random_access_iterator_tag, random_access_iterator_tag)
- {
- // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
- typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
- if (__len2 == 0)
- return __last1;
- typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
- if (__len1 < __len2)
- return __last1;
- const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
- _RandomAccessIterator1 __l1 = __last1;
- _RandomAccessIterator2 __l2 = __last2;
- --__l2;
- while (true)
- {
- while (true)
- {
- if (__s == __l1)
- return __last1;
- if (__pred(*--__l1, *__l2))
- break;
- }
- _RandomAccessIterator1 __m1 = __l1;
- _RandomAccessIterator2 __m2 = __l2;
- while (true)
- {
- if (__m2 == __first2)
- return __m1;
- // no need to check range on __m1 because __s guarantees we have enough source
- if (!__pred(*--__m1, *--__m2))
- {
- break;
- }
- }
- }
- }
- template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator1
- find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
- {
- return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __last2, __pred,
- typename iterator_traits<_ForwardIterator1>::iterator_category(),
- typename iterator_traits<_ForwardIterator2>::iterator_category());
- }
- template <class _ForwardIterator1, class _ForwardIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator1
- find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2)
- {
- typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
- typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
- return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
- }
- // find_first_of
- template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
- _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
- __find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
- {
- for (; __first1 != __last1; ++__first1)
- for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
- if (__pred(*__first1, *__j))
- return __first1;
- return __last1;
- }
- template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator1
- find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
- {
- return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
- }
- template <class _ForwardIterator1, class _ForwardIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator1
- find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2)
- {
- typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
- typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
- return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
- }
- // adjacent_find
- template <class _ForwardIterator, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
- {
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- {
- if (__pred(*__first, *__i))
- return __first;
- __first = __i;
- }
- }
- return __last;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
- {
- typedef typename iterator_traits<_ForwardIterator>::value_type __v;
- return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
- }
- // count
- template <class _InputIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename iterator_traits<_InputIterator>::difference_type
- count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
- {
- typename iterator_traits<_InputIterator>::difference_type __r(0);
- for (; __first != __last; ++__first)
- if (*__first == __value_)
- ++__r;
- return __r;
- }
- // count_if
- template <class _InputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- typename iterator_traits<_InputIterator>::difference_type
- count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- typename iterator_traits<_InputIterator>::difference_type __r(0);
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- ++__r;
- return __r;
- }
- // mismatch
- template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_InputIterator1, _InputIterator2>
- mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _BinaryPredicate __pred)
- {
- for (; __first1 != __last1; ++__first1, (void) ++__first2)
- if (!__pred(*__first1, *__first2))
- break;
- return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_InputIterator1, _InputIterator2>
- mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type __v1;
- typedef typename iterator_traits<_InputIterator2>::value_type __v2;
- return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
- }
- #if _LIBCPP_STD_VER > 11
- template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_InputIterator1, _InputIterator2>
- mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _BinaryPredicate __pred)
- {
- for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
- if (!__pred(*__first1, *__first2))
- break;
- return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_InputIterator1, _InputIterator2>
- mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type __v1;
- typedef typename iterator_traits<_InputIterator2>::value_type __v2;
- return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
- }
- #endif
- // equal
- template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
- {
- for (; __first1 != __last1; ++__first1, (void) ++__first2)
- if (!__pred(*__first1, *__first2))
- return false;
- return true;
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type __v1;
- typedef typename iterator_traits<_InputIterator2>::value_type __v2;
- return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
- }
- #if _LIBCPP_STD_VER > 11
- template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- __equal(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
- input_iterator_tag, input_iterator_tag )
- {
- for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
- if (!__pred(*__first1, *__first2))
- return false;
- return __first1 == __last1 && __first2 == __last2;
- }
- template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
- _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
- random_access_iterator_tag, random_access_iterator_tag )
- {
- if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
- return false;
- return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
- typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __pred );
- }
- template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- equal(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
- {
- return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __last2, __pred,
- typename iterator_traits<_InputIterator1>::iterator_category(),
- typename iterator_traits<_InputIterator2>::iterator_category());
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- equal(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type __v1;
- typedef typename iterator_traits<_InputIterator2>::value_type __v2;
- return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
- typename iterator_traits<_InputIterator1>::iterator_category(),
- typename iterator_traits<_InputIterator2>::iterator_category());
- }
- #endif
- // is_permutation
- template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
- bool
- is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _BinaryPredicate __pred)
- {
- // shorten sequences as much as possible by lopping of any equal parts
- for (; __first1 != __last1; ++__first1, (void) ++__first2)
- if (!__pred(*__first1, *__first2))
- goto __not_done;
- return true;
- __not_done:
- // __first1 != __last1 && *__first1 != *__first2
- typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
- _D1 __l1 = _VSTD::distance(__first1, __last1);
- if (__l1 == _D1(1))
- return false;
- _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
- // For each element in [f1, l1) see if there are the same number of
- // equal elements in [f2, l2)
- for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
- {
- // Have we already counted the number of *__i in [f1, l1)?
- for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
- if (__pred(*__j, *__i))
- goto __next_iter;
- {
- // Count number of *__i in [f2, l2)
- _D1 __c2 = 0;
- for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
- if (__pred(*__i, *__j))
- ++__c2;
- if (__c2 == 0)
- return false;
- // Count number of *__i in [__i, l1) (we can start with 1)
- _D1 __c1 = 1;
- for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
- if (__pred(*__i, *__j))
- ++__c1;
- if (__c1 != __c2)
- return false;
- }
- __next_iter:;
- }
- return true;
- }
- template<class _ForwardIterator1, class _ForwardIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2)
- {
- typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
- typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
- return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
- }
- #if _LIBCPP_STD_VER > 11
- template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
- bool
- __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2,
- _BinaryPredicate __pred,
- forward_iterator_tag, forward_iterator_tag )
- {
- // shorten sequences as much as possible by lopping of any equal parts
- for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
- if (!__pred(*__first1, *__first2))
- goto __not_done;
- return __first1 == __last1 && __first2 == __last2;
- __not_done:
- // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
- typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
- _D1 __l1 = _VSTD::distance(__first1, __last1);
- typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
- _D2 __l2 = _VSTD::distance(__first2, __last2);
- if (__l1 != __l2)
- return false;
- // For each element in [f1, l1) see if there are the same number of
- // equal elements in [f2, l2)
- for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
- {
- // Have we already counted the number of *__i in [f1, l1)?
- for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
- if (__pred(*__j, *__i))
- goto __next_iter;
- {
- // Count number of *__i in [f2, l2)
- _D1 __c2 = 0;
- for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
- if (__pred(*__i, *__j))
- ++__c2;
- if (__c2 == 0)
- return false;
- // Count number of *__i in [__i, l1) (we can start with 1)
- _D1 __c1 = 1;
- for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
- if (__pred(*__i, *__j))
- ++__c1;
- if (__c1 != __c2)
- return false;
- }
- __next_iter:;
- }
- return true;
- }
- template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
- bool
- __is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
- _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
- _BinaryPredicate __pred,
- random_access_iterator_tag, random_access_iterator_tag )
- {
- if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
- return false;
- return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
- typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __pred );
- }
- template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2,
- _BinaryPredicate __pred )
- {
- return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __last2, __pred,
- typename iterator_traits<_ForwardIterator1>::iterator_category(),
- typename iterator_traits<_ForwardIterator2>::iterator_category());
- }
- template<class _ForwardIterator1, class _ForwardIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2)
- {
- typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
- typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
- return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
- __equal_to<__v1, __v2>(),
- typename iterator_traits<_ForwardIterator1>::iterator_category(),
- typename iterator_traits<_ForwardIterator2>::iterator_category());
- }
- #endif
- // search
- template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
- pair<_ForwardIterator1, _ForwardIterator1>
- __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
- forward_iterator_tag, forward_iterator_tag)
- {
- if (__first2 == __last2)
- return make_pair(__first1, __first1); // Everything matches an empty sequence
- while (true)
- {
- // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
- while (true)
- {
- if (__first1 == __last1) // return __last1 if no element matches *__first2
- return make_pair(__last1, __last1);
- if (__pred(*__first1, *__first2))
- break;
- ++__first1;
- }
- // *__first1 matches *__first2, now match elements after here
- _ForwardIterator1 __m1 = __first1;
- _ForwardIterator2 __m2 = __first2;
- while (true)
- {
- if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
- return make_pair(__first1, __m1);
- if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
- return make_pair(__last1, __last1);
- if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
- {
- ++__first1;
- break;
- } // else there is a match, check next elements
- }
- }
- }
- template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
- _LIBCPP_CONSTEXPR_AFTER_CXX11
- pair<_RandomAccessIterator1, _RandomAccessIterator1>
- __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
- _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
- random_access_iterator_tag, random_access_iterator_tag)
- {
- typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
- typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
- // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
- const _D2 __len2 = __last2 - __first2;
- if (__len2 == 0)
- return make_pair(__first1, __first1);
- const _D1 __len1 = __last1 - __first1;
- if (__len1 < __len2)
- return make_pair(__last1, __last1);
- const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
- while (true)
- {
- #if !_LIBCPP_UNROLL_LOOPS
- while (true)
- {
- if (__first1 == __s)
- return make_pair(__last1, __last1);
- if (__pred(*__first1, *__first2))
- break;
- ++__first1;
- }
- #else // !_LIBCPP_UNROLL_LOOPS
- for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
- {
- if (__pred(*__first1, *__first2))
- goto __phase2;
- if (__pred(*++__first1, *__first2))
- goto __phase2;
- if (__pred(*++__first1, *__first2))
- goto __phase2;
- if (__pred(*++__first1, *__first2))
- goto __phase2;
- ++__first1;
- }
- switch (__s - __first1)
- {
- case 3:
- if (__pred(*__first1, *__first2))
- break;
- ++__first1;
- case 2:
- if (__pred(*__first1, *__first2))
- break;
- ++__first1;
- case 1:
- if (__pred(*__first1, *__first2))
- break;
- case 0:
- return make_pair(__last1, __last1);
- }
- __phase2:
- #endif // !_LIBCPP_UNROLL_LOOPS
- _RandomAccessIterator1 __m1 = __first1;
- _RandomAccessIterator2 __m2 = __first2;
- #if !_LIBCPP_UNROLL_LOOPS
- while (true)
- {
- if (++__m2 == __last2)
- return make_pair(__first1, __first1 + __len2);
- ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
- if (!__pred(*__m1, *__m2))
- {
- ++__first1;
- break;
- }
- }
- #else // !_LIBCPP_UNROLL_LOOPS
- ++__m2;
- ++__m1;
- for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
- {
- if (!__pred(*__m1, *__m2))
- goto __continue;
- if (!__pred(*++__m1, *++__m2))
- goto __continue;
- if (!__pred(*++__m1, *++__m2))
- goto __continue;
- if (!__pred(*++__m1, *++__m2))
- goto __continue;
- ++__m1;
- ++__m2;
- }
- switch (__last2 - __m2)
- {
- case 3:
- if (!__pred(*__m1, *__m2))
- break;
- ++__m1;
- ++__m2;
- case 2:
- if (!__pred(*__m1, *__m2))
- break;
- ++__m1;
- ++__m2;
- case 1:
- if (!__pred(*__m1, *__m2))
- break;
- case 0:
- return make_pair(__first1, __first1 + __len2);
- }
- __continue:
- ++__first1;
- #endif // !_LIBCPP_UNROLL_LOOPS
- }
- }
- template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator1
- search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
- {
- return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first1, __last1, __first2, __last2, __pred,
- typename iterator_traits<_ForwardIterator1>::iterator_category(),
- typename iterator_traits<_ForwardIterator2>::iterator_category())
- .first;
- }
- template <class _ForwardIterator1, class _ForwardIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator1
- search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2)
- {
- typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
- typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
- return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
- }
- // search_n
- template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
- _ForwardIterator
- __search_n(_ForwardIterator __first, _ForwardIterator __last,
- _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
- {
- if (__count <= 0)
- return __first;
- while (true)
- {
- // Find first element in sequence that matchs __value_, with a mininum of loop checks
- while (true)
- {
- if (__first == __last) // return __last if no element matches __value_
- return __last;
- if (__pred(*__first, __value_))
- break;
- ++__first;
- }
- // *__first matches __value_, now match elements after here
- _ForwardIterator __m = __first;
- _Size __c(0);
- while (true)
- {
- if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
- return __first;
- if (++__m == __last) // Otherwise if source exhaused, pattern not found
- return __last;
- if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
- {
- __first = __m;
- ++__first;
- break;
- } // else there is a match, check next elements
- }
- }
- }
- template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
- _RandomAccessIterator
- __search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
- _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
- {
- if (__count <= 0)
- return __first;
- _Size __len = static_cast<_Size>(__last - __first);
- if (__len < __count)
- return __last;
- const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
- while (true)
- {
- // Find first element in sequence that matchs __value_, with a mininum of loop checks
- while (true)
- {
- if (__first >= __s) // return __last if no element matches __value_
- return __last;
- if (__pred(*__first, __value_))
- break;
- ++__first;
- }
- // *__first matches __value_, now match elements after here
- _RandomAccessIterator __m = __first;
- _Size __c(0);
- while (true)
- {
- if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
- return __first;
- ++__m; // no need to check range on __m because __s guarantees we have enough source
- if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
- {
- __first = __m;
- ++__first;
- break;
- } // else there is a match, check next elements
- }
- }
- }
- template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- search_n(_ForwardIterator __first, _ForwardIterator __last,
- _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
- {
- return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first, __last, __convert_to_integral(__count), __value_, __pred,
- typename iterator_traits<_ForwardIterator>::iterator_category());
- }
- template <class _ForwardIterator, class _Size, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
- {
- typedef typename iterator_traits<_ForwardIterator>::value_type __v;
- return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
- __value_, __equal_to<__v, _Tp>());
- }
- // copy
- template <class _Iter>
- inline _LIBCPP_INLINE_VISIBILITY
- _Iter
- __unwrap_iter(_Iter __i)
- {
- return __i;
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_trivially_copy_assignable<_Tp>::value,
- _Tp*
- >::type
- __unwrap_iter(move_iterator<_Tp*> __i)
- {
- return __i.base();
- }
- #if _LIBCPP_DEBUG_LEVEL < 2
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_trivially_copy_assignable<_Tp>::value,
- _Tp*
- >::type
- __unwrap_iter(__wrap_iter<_Tp*> __i)
- {
- return __i.base();
- }
- #endif // _LIBCPP_DEBUG_LEVEL < 2
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- for (; __first != __last; ++__first, (void) ++__result)
- *__result = *__first;
- return __result;
- }
- template <class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_same<typename remove_const<_Tp>::type, _Up>::value &&
- is_trivially_copy_assignable<_Up>::value,
- _Up*
- >::type
- __copy(_Tp* __first, _Tp* __last, _Up* __result)
- {
- const size_t __n = static_cast<size_t>(__last - __first);
- if (__n > 0)
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
- return __result + __n;
- }
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
- }
- // copy_backward
- template <class _BidirectionalIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
- {
- while (__first != __last)
- *--__result = *--__last;
- return __result;
- }
- template <class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_same<typename remove_const<_Tp>::type, _Up>::value &&
- is_trivially_copy_assignable<_Up>::value,
- _Up*
- >::type
- __copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
- {
- const size_t __n = static_cast<size_t>(__last - __first);
- if (__n > 0)
- {
- __result -= __n;
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
- }
- return __result;
- }
- template <class _BidirectionalIterator1, class _BidirectionalIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- _BidirectionalIterator2
- copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
- _BidirectionalIterator2 __result)
- {
- return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
- }
- // copy_if
- template<class _InputIterator, class _OutputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- copy_if(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- {
- if (__pred(*__first))
- {
- *__result = *__first;
- ++__result;
- }
- }
- return __result;
- }
- // copy_n
- template<class _InputIterator, class _Size, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- __is_input_iterator<_InputIterator>::value &&
- !__is_random_access_iterator<_InputIterator>::value,
- _OutputIterator
- >::type
- copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
- {
- typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
- _IntegralSize __n = __orig_n;
- if (__n > 0)
- {
- *__result = *__first;
- ++__result;
- for (--__n; __n > 0; --__n)
- {
- ++__first;
- *__result = *__first;
- ++__result;
- }
- }
- return __result;
- }
- template<class _InputIterator, class _Size, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- __is_random_access_iterator<_InputIterator>::value,
- _OutputIterator
- >::type
- copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
- {
- typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
- _IntegralSize __n = __orig_n;
- return _VSTD::copy(__first, __first + __n, __result);
- }
- // move
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- __move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- for (; __first != __last; ++__first, (void) ++__result)
- *__result = _VSTD::move(*__first);
- return __result;
- }
- template <class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_same<typename remove_const<_Tp>::type, _Up>::value &&
- is_trivially_copy_assignable<_Up>::value,
- _Up*
- >::type
- __move(_Tp* __first, _Tp* __last, _Up* __result)
- {
- const size_t __n = static_cast<size_t>(__last - __first);
- if (__n > 0)
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
- return __result + __n;
- }
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
- }
- // move_backward
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- __move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- while (__first != __last)
- *--__result = _VSTD::move(*--__last);
- return __result;
- }
- template <class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_same<typename remove_const<_Tp>::type, _Up>::value &&
- is_trivially_copy_assignable<_Up>::value,
- _Up*
- >::type
- __move_backward(_Tp* __first, _Tp* __last, _Up* __result)
- {
- const size_t __n = static_cast<size_t>(__last - __first);
- if (__n > 0)
- {
- __result -= __n;
- _VSTD::memmove(__result, __first, __n * sizeof(_Up));
- }
- return __result;
- }
- template <class _BidirectionalIterator1, class _BidirectionalIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- _BidirectionalIterator2
- move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
- _BidirectionalIterator2 __result)
- {
- return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
- }
- // iter_swap
- // moved to <type_traits> for better swap / noexcept support
- // transform
- template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
- {
- for (; __first != __last; ++__first, (void) ++__result)
- *__result = __op(*__first);
- return __result;
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
- _OutputIterator __result, _BinaryOperation __binary_op)
- {
- for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
- *__result = __binary_op(*__first1, *__first2);
- return __result;
- }
- // replace
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
- {
- for (; __first != __last; ++__first)
- if (*__first == __old_value)
- *__first = __new_value;
- }
- // replace_if
- template <class _ForwardIterator, class _Predicate, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
- {
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- *__first = __new_value;
- }
- // replace_copy
- template <class _InputIterator, class _OutputIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
- const _Tp& __old_value, const _Tp& __new_value)
- {
- for (; __first != __last; ++__first, (void) ++__result)
- if (*__first == __old_value)
- *__result = __new_value;
- else
- *__result = *__first;
- return __result;
- }
- // replace_copy_if
- template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
- _Predicate __pred, const _Tp& __new_value)
- {
- for (; __first != __last; ++__first, (void) ++__result)
- if (__pred(*__first))
- *__result = __new_value;
- else
- *__result = *__first;
- return __result;
- }
- // fill_n
- template <class _OutputIterator, class _Size, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
- {
- for (; __n > 0; ++__first, (void) --__n)
- *__first = __value_;
- return __first;
- }
- template <class _Tp, class _Size, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
- !is_same<_Tp, bool>::value &&
- is_integral<_Up>::value && sizeof(_Up) == 1,
- _Tp*
- >::type
- __fill_n(_Tp* __first, _Size __n,_Up __value_)
- {
- if (__n > 0)
- _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
- return __first + __n;
- }
- template <class _OutputIterator, class _Size, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
- {
- return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
- }
- // fill
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
- {
- for (; __first != __last; ++__first)
- *__first = __value_;
- }
- template <class _RandomAccessIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
- {
- _VSTD::fill_n(__first, __last - __first, __value_);
- }
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
- }
- // generate
- template <class _ForwardIterator, class _Generator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
- {
- for (; __first != __last; ++__first)
- *__first = __gen();
- }
- // generate_n
- template <class _OutputIterator, class _Size, class _Generator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
- {
- typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
- _IntegralSize __n = __orig_n;
- for (; __n > 0; ++__first, (void) --__n)
- *__first = __gen();
- return __first;
- }
- // remove
- template <class _ForwardIterator, class _Tp>
- _ForwardIterator
- remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- __first = _VSTD::find(__first, __last, __value_);
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- {
- if (!(*__i == __value_))
- {
- *__first = _VSTD::move(*__i);
- ++__first;
- }
- }
- }
- return __first;
- }
- // remove_if
- template <class _ForwardIterator, class _Predicate>
- _ForwardIterator
- remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
- {
- __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
- (__first, __last, __pred);
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- {
- if (!__pred(*__i))
- {
- *__first = _VSTD::move(*__i);
- ++__first;
- }
- }
- }
- return __first;
- }
- // remove_copy
- template <class _InputIterator, class _OutputIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
- {
- for (; __first != __last; ++__first)
- {
- if (!(*__first == __value_))
- {
- *__result = *__first;
- ++__result;
- }
- }
- return __result;
- }
- // remove_copy_if
- template <class _InputIterator, class _OutputIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- {
- if (!__pred(*__first))
- {
- *__result = *__first;
- ++__result;
- }
- }
- return __result;
- }
- // unique
- template <class _ForwardIterator, class _BinaryPredicate>
- _ForwardIterator
- unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
- {
- __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first, __last, __pred);
- if (__first != __last)
- {
- // ... a a ? ...
- // f i
- _ForwardIterator __i = __first;
- for (++__i; ++__i != __last;)
- if (!__pred(*__first, *__i))
- *++__first = _VSTD::move(*__i);
- ++__first;
- }
- return __first;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- unique(_ForwardIterator __first, _ForwardIterator __last)
- {
- typedef typename iterator_traits<_ForwardIterator>::value_type __v;
- return _VSTD::unique(__first, __last, __equal_to<__v>());
- }
- // unique_copy
- template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
- _OutputIterator
- __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
- input_iterator_tag, output_iterator_tag)
- {
- if (__first != __last)
- {
- typename iterator_traits<_InputIterator>::value_type __t(*__first);
- *__result = __t;
- ++__result;
- while (++__first != __last)
- {
- if (!__pred(__t, *__first))
- {
- __t = *__first;
- *__result = __t;
- ++__result;
- }
- }
- }
- return __result;
- }
- template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
- _OutputIterator
- __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
- forward_iterator_tag, output_iterator_tag)
- {
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- *__result = *__i;
- ++__result;
- while (++__first != __last)
- {
- if (!__pred(*__i, *__first))
- {
- *__result = *__first;
- ++__result;
- __i = __first;
- }
- }
- }
- return __result;
- }
- template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
- _ForwardIterator
- __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
- input_iterator_tag, forward_iterator_tag)
- {
- if (__first != __last)
- {
- *__result = *__first;
- while (++__first != __last)
- if (!__pred(*__result, *__first))
- *++__result = *__first;
- ++__result;
- }
- return __result;
- }
- template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
- {
- return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
- (__first, __last, __result, __pred,
- typename iterator_traits<_InputIterator>::iterator_category(),
- typename iterator_traits<_OutputIterator>::iterator_category());
- }
- template <class _InputIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
- {
- typedef typename iterator_traits<_InputIterator>::value_type __v;
- return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
- }
- // reverse
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
- {
- while (__first != __last)
- {
- if (__first == --__last)
- break;
- _VSTD::iter_swap(__first, __last);
- ++__first;
- }
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
- {
- if (__first != __last)
- for (; __first < --__last; ++__first)
- _VSTD::iter_swap(__first, __last);
- }
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
- {
- _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
- }
- // reverse_copy
- template <class _BidirectionalIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
- {
- for (; __first != __last; ++__result)
- *__result = *--__last;
- return __result;
- }
- // rotate
- template <class _ForwardIterator>
- _ForwardIterator
- __rotate_left(_ForwardIterator __first, _ForwardIterator __last)
- {
- typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
- value_type __tmp = _VSTD::move(*__first);
- _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
- *__lm1 = _VSTD::move(__tmp);
- return __lm1;
- }
- template <class _BidirectionalIterator>
- _BidirectionalIterator
- __rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
- {
- typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
- _BidirectionalIterator __lm1 = _VSTD::prev(__last);
- value_type __tmp = _VSTD::move(*__lm1);
- _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
- *__first = _VSTD::move(__tmp);
- return __fp1;
- }
- template <class _ForwardIterator>
- _ForwardIterator
- __rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
- {
- _ForwardIterator __i = __middle;
- while (true)
- {
- swap(*__first, *__i);
- ++__first;
- if (++__i == __last)
- break;
- if (__first == __middle)
- __middle = __i;
- }
- _ForwardIterator __r = __first;
- if (__first != __middle)
- {
- __i = __middle;
- while (true)
- {
- swap(*__first, *__i);
- ++__first;
- if (++__i == __last)
- {
- if (__first == __middle)
- break;
- __i = __middle;
- }
- else if (__first == __middle)
- __middle = __i;
- }
- }
- return __r;
- }
- template<typename _Integral>
- inline _LIBCPP_INLINE_VISIBILITY
- _Integral
- __gcd(_Integral __x, _Integral __y)
- {
- do
- {
- _Integral __t = __x % __y;
- __x = __y;
- __y = __t;
- } while (__y);
- return __x;
- }
- template<typename _RandomAccessIterator>
- _RandomAccessIterator
- __rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- const difference_type __m1 = __middle - __first;
- const difference_type __m2 = __last - __middle;
- if (__m1 == __m2)
- {
- _VSTD::swap_ranges(__first, __middle, __middle);
- return __middle;
- }
- const difference_type __g = _VSTD::__gcd(__m1, __m2);
- for (_RandomAccessIterator __p = __first + __g; __p != __first;)
- {
- value_type __t(_VSTD::move(*--__p));
- _RandomAccessIterator __p1 = __p;
- _RandomAccessIterator __p2 = __p1 + __m1;
- do
- {
- *__p1 = _VSTD::move(*__p2);
- __p1 = __p2;
- const difference_type __d = __last - __p2;
- if (__m1 < __d)
- __p2 += __m1;
- else
- __p2 = __first + (__m1 - __d);
- } while (__p2 != __p);
- *__p1 = _VSTD::move(__t);
- }
- return __first + __m2;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- __rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
- _VSTD::forward_iterator_tag)
- {
- typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
- if (_VSTD::is_trivially_move_assignable<value_type>::value)
- {
- if (_VSTD::next(__first) == __middle)
- return _VSTD::__rotate_left(__first, __last);
- }
- return _VSTD::__rotate_forward(__first, __middle, __last);
- }
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _BidirectionalIterator
- __rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
- _VSTD::bidirectional_iterator_tag)
- {
- typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
- if (_VSTD::is_trivially_move_assignable<value_type>::value)
- {
- if (_VSTD::next(__first) == __middle)
- return _VSTD::__rotate_left(__first, __last);
- if (_VSTD::next(__middle) == __last)
- return _VSTD::__rotate_right(__first, __last);
- }
- return _VSTD::__rotate_forward(__first, __middle, __last);
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _RandomAccessIterator
- __rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
- _VSTD::random_access_iterator_tag)
- {
- typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
- if (_VSTD::is_trivially_move_assignable<value_type>::value)
- {
- if (_VSTD::next(__first) == __middle)
- return _VSTD::__rotate_left(__first, __last);
- if (_VSTD::next(__middle) == __last)
- return _VSTD::__rotate_right(__first, __last);
- return _VSTD::__rotate_gcd(__first, __middle, __last);
- }
- return _VSTD::__rotate_forward(__first, __middle, __last);
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
- {
- if (__first == __middle)
- return __last;
- if (__middle == __last)
- return __first;
- return _VSTD::__rotate(__first, __middle, __last,
- typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
- }
- // rotate_copy
- template <class _ForwardIterator, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
- {
- return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
- }
- // min_element
- template <class _ForwardIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- _ForwardIterator
- min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
- {
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- if (__comp(*__i, *__first))
- __first = __i;
- }
- return __first;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- _ForwardIterator
- min_element(_ForwardIterator __first, _ForwardIterator __last)
- {
- return _VSTD::min_element(__first, __last,
- __less<typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // min
- template <class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- const _Tp&
- min(const _Tp& __a, const _Tp& __b, _Compare __comp)
- {
- return __comp(__b, __a) ? __b : __a;
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- const _Tp&
- min(const _Tp& __a, const _Tp& __b)
- {
- return _VSTD::min(__a, __b, __less<_Tp>());
- }
- #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- template<class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- _Tp
- min(initializer_list<_Tp> __t, _Compare __comp)
- {
- return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- _Tp
- min(initializer_list<_Tp> __t)
- {
- return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
- }
- #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- // max_element
- template <class _ForwardIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- _ForwardIterator
- max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
- {
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- if (__comp(*__first, *__i))
- __first = __i;
- }
- return __first;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- _ForwardIterator
- max_element(_ForwardIterator __first, _ForwardIterator __last)
- {
- return _VSTD::max_element(__first, __last,
- __less<typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // max
- template <class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- const _Tp&
- max(const _Tp& __a, const _Tp& __b, _Compare __comp)
- {
- return __comp(__a, __b) ? __b : __a;
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- const _Tp&
- max(const _Tp& __a, const _Tp& __b)
- {
- return _VSTD::max(__a, __b, __less<_Tp>());
- }
- #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- template<class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- _Tp
- max(initializer_list<_Tp> __t, _Compare __comp)
- {
- return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- _Tp
- max(initializer_list<_Tp> __t)
- {
- return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
- }
- #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- #if _LIBCPP_STD_VER > 14
- // clamp
- template<class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
- const _Tp&
- clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
- {
- _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
- return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
- const _Tp&
- clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
- {
- return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
- }
- #endif
- // minmax_element
- template <class _ForwardIterator, class _Compare>
- _LIBCPP_CONSTEXPR_AFTER_CXX11
- std::pair<_ForwardIterator, _ForwardIterator>
- minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
- {
- std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
- if (__first != __last)
- {
- if (++__first != __last)
- {
- if (__comp(*__first, *__result.first))
- __result.first = __first;
- else
- __result.second = __first;
- while (++__first != __last)
- {
- _ForwardIterator __i = __first;
- if (++__first == __last)
- {
- if (__comp(*__i, *__result.first))
- __result.first = __i;
- else if (!__comp(*__i, *__result.second))
- __result.second = __i;
- break;
- }
- else
- {
- if (__comp(*__first, *__i))
- {
- if (__comp(*__first, *__result.first))
- __result.first = __first;
- if (!__comp(*__i, *__result.second))
- __result.second = __i;
- }
- else
- {
- if (__comp(*__i, *__result.first))
- __result.first = __i;
- if (!__comp(*__first, *__result.second))
- __result.second = __first;
- }
- }
- }
- }
- }
- return __result;
- }
- template <class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- std::pair<_ForwardIterator, _ForwardIterator>
- minmax_element(_ForwardIterator __first, _ForwardIterator __last)
- {
- return _VSTD::minmax_element(__first, __last,
- __less<typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // minmax
- template<class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- pair<const _Tp&, const _Tp&>
- minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
- {
- return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
- pair<const _Tp&, const _Tp&>(__a, __b);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- pair<const _Tp&, const _Tp&>
- minmax(const _Tp& __a, const _Tp& __b)
- {
- return _VSTD::minmax(__a, __b, __less<_Tp>());
- }
- #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- template<class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- pair<_Tp, _Tp>
- minmax(initializer_list<_Tp> __t, _Compare __comp)
- {
- typedef typename initializer_list<_Tp>::const_iterator _Iter;
- _Iter __first = __t.begin();
- _Iter __last = __t.end();
- std::pair<_Tp, _Tp> __result(*__first, *__first);
- ++__first;
- if (__t.size() % 2 == 0)
- {
- if (__comp(*__first, __result.first))
- __result.first = *__first;
- else
- __result.second = *__first;
- ++__first;
- }
-
- while (__first != __last)
- {
- _Tp __prev = *__first++;
- if (__comp(*__first, __prev)) {
- if ( __comp(*__first, __result.first)) __result.first = *__first;
- if (!__comp(__prev, __result.second)) __result.second = __prev;
- }
- else {
- if ( __comp(__prev, __result.first)) __result.first = __prev;
- if (!__comp(*__first, __result.second)) __result.second = *__first;
- }
-
- __first++;
- }
- return __result;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
- pair<_Tp, _Tp>
- minmax(initializer_list<_Tp> __t)
- {
- return _VSTD::minmax(__t, __less<_Tp>());
- }
- #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- // random_shuffle
- // __independent_bits_engine
- template <unsigned long long _Xp, size_t _Rp>
- struct __log2_imp
- {
- static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
- : __log2_imp<_Xp, _Rp - 1>::value;
- };
- template <unsigned long long _Xp>
- struct __log2_imp<_Xp, 0>
- {
- static const size_t value = 0;
- };
- template <size_t _Rp>
- struct __log2_imp<0, _Rp>
- {
- static const size_t value = _Rp + 1;
- };
- template <class _UI, _UI _Xp>
- struct __log2
- {
- static const size_t value = __log2_imp<_Xp,
- sizeof(_UI) * __CHAR_BIT__ - 1>::value;
- };
- template<class _Engine, class _UIntType>
- class __independent_bits_engine
- {
- public:
- // types
- typedef _UIntType result_type;
- private:
- typedef typename _Engine::result_type _Engine_result_type;
- typedef typename conditional
- <
- sizeof(_Engine_result_type) <= sizeof(result_type),
- result_type,
- _Engine_result_type
- >::type _Working_result_type;
- _Engine& __e_;
- size_t __w_;
- size_t __w0_;
- size_t __n_;
- size_t __n0_;
- _Working_result_type __y0_;
- _Working_result_type __y1_;
- _Engine_result_type __mask0_;
- _Engine_result_type __mask1_;
- #ifdef _LIBCPP_HAS_NO_CONSTEXPR
- static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
- + _Working_result_type(1);
- #else
- static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
- + _Working_result_type(1);
- #endif
- static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
- static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
- static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
- public:
- // constructors and seeding functions
- __independent_bits_engine(_Engine& __e, size_t __w);
- // generating functions
- result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
- private:
- result_type __eval(false_type);
- result_type __eval(true_type);
- };
- template<class _Engine, class _UIntType>
- __independent_bits_engine<_Engine, _UIntType>
- ::__independent_bits_engine(_Engine& __e, size_t __w)
- : __e_(__e),
- __w_(__w)
- {
- __n_ = __w_ / __m + (__w_ % __m != 0);
- __w0_ = __w_ / __n_;
- if (_Rp == 0)
- __y0_ = _Rp;
- else if (__w0_ < _WDt)
- __y0_ = (_Rp >> __w0_) << __w0_;
- else
- __y0_ = 0;
- if (_Rp - __y0_ > __y0_ / __n_)
- {
- ++__n_;
- __w0_ = __w_ / __n_;
- if (__w0_ < _WDt)
- __y0_ = (_Rp >> __w0_) << __w0_;
- else
- __y0_ = 0;
- }
- __n0_ = __n_ - __w_ % __n_;
- if (__w0_ < _WDt - 1)
- __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
- else
- __y1_ = 0;
- __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
- _Engine_result_type(0);
- __mask1_ = __w0_ < _EDt - 1 ?
- _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
- _Engine_result_type(~0);
- }
- template<class _Engine, class _UIntType>
- inline
- _UIntType
- __independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
- {
- return static_cast<result_type>(__e_() & __mask0_);
- }
- template<class _Engine, class _UIntType>
- _UIntType
- __independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
- {
- result_type _Sp = 0;
- for (size_t __k = 0; __k < __n0_; ++__k)
- {
- _Engine_result_type __u;
- do
- {
- __u = __e_() - _Engine::min();
- } while (__u >= __y0_);
- if (__w0_ < _WDt)
- _Sp <<= __w0_;
- else
- _Sp = 0;
- _Sp += __u & __mask0_;
- }
- for (size_t __k = __n0_; __k < __n_; ++__k)
- {
- _Engine_result_type __u;
- do
- {
- __u = __e_() - _Engine::min();
- } while (__u >= __y1_);
- if (__w0_ < _WDt - 1)
- _Sp <<= __w0_ + 1;
- else
- _Sp = 0;
- _Sp += __u & __mask1_;
- }
- return _Sp;
- }
- // uniform_int_distribution
- template<class _IntType = int>
- class uniform_int_distribution
- {
- public:
- // types
- typedef _IntType result_type;
- class param_type
- {
- result_type __a_;
- result_type __b_;
- public:
- typedef uniform_int_distribution distribution_type;
- explicit param_type(result_type __a = 0,
- result_type __b = numeric_limits<result_type>::max())
- : __a_(__a), __b_(__b) {}
- result_type a() const {return __a_;}
- result_type b() const {return __b_;}
- friend bool operator==(const param_type& __x, const param_type& __y)
- {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
- friend bool operator!=(const param_type& __x, const param_type& __y)
- {return !(__x == __y);}
- };
- private:
- param_type __p_;
- public:
- // constructors and reset functions
- explicit uniform_int_distribution(result_type __a = 0,
- result_type __b = numeric_limits<result_type>::max())
- : __p_(param_type(__a, __b)) {}
- explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
- void reset() {}
- // generating functions
- template<class _URNG> result_type operator()(_URNG& __g)
- {return (*this)(__g, __p_);}
- template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
- // property functions
- result_type a() const {return __p_.a();}
- result_type b() const {return __p_.b();}
- param_type param() const {return __p_;}
- void param(const param_type& __p) {__p_ = __p;}
- result_type min() const {return a();}
- result_type max() const {return b();}
- friend bool operator==(const uniform_int_distribution& __x,
- const uniform_int_distribution& __y)
- {return __x.__p_ == __y.__p_;}
- friend bool operator!=(const uniform_int_distribution& __x,
- const uniform_int_distribution& __y)
- {return !(__x == __y);}
- };
- template<class _IntType>
- template<class _URNG>
- typename uniform_int_distribution<_IntType>::result_type
- uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
- {
- typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
- uint32_t, uint64_t>::type _UIntType;
- const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
- if (_Rp == 1)
- return __p.a();
- const size_t _Dt = numeric_limits<_UIntType>::digits;
- typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
- if (_Rp == 0)
- return static_cast<result_type>(_Eng(__g, _Dt)());
- size_t __w = _Dt - __clz(_Rp) - 1;
- if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
- ++__w;
- _Eng __e(__g, __w);
- _UIntType __u;
- do
- {
- __u = __e();
- } while (__u >= _Rp);
- return static_cast<result_type>(__u + __p.a());
- }
- #if !defined(_LIBCPP_SGX_CONFIG)
- class _LIBCPP_TYPE_VIS __rs_default;
- _LIBCPP_FUNC_VIS __rs_default __rs_get();
- class _LIBCPP_TYPE_VIS __rs_default
- {
- static unsigned __c_;
- __rs_default();
- public:
- typedef uint_fast32_t result_type;
- static const result_type _Min = 0;
- static const result_type _Max = 0xFFFFFFFF;
- __rs_default(const __rs_default&);
- ~__rs_default();
- result_type operator()();
- static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
- static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
- friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
- };
- _LIBCPP_FUNC_VIS __rs_default __rs_get();
- template <class _RandomAccessIterator>
- void
- random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- typedef uniform_int_distribution<ptrdiff_t> _Dp;
- typedef typename _Dp::param_type _Pp;
- difference_type __d = __last - __first;
- if (__d > 1)
- {
- _Dp __uid;
- __rs_default __g = __rs_get();
- for (--__last, --__d; __first < __last; ++__first, --__d)
- {
- difference_type __i = __uid(__g, _Pp(0, __d));
- if (__i != difference_type(0))
- swap(*__first, *(__first + __i));
- }
- }
- }
- template <class _RandomAccessIterator, class _RandomNumberGenerator>
- void
- random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
- #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
- _RandomNumberGenerator&& __rand)
- #else
- _RandomNumberGenerator& __rand)
- #endif
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- difference_type __d = __last - __first;
- if (__d > 1)
- {
- for (--__last; __first < __last; ++__first, --__d)
- {
- difference_type __i = __rand(__d);
- swap(*__first, *(__first + __i));
- }
- }
- }
- #endif // !defined(_LIBCPP_SGX_CONFIG)
- template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
- void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
- #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
- _UniformRandomNumberGenerator&& __g)
- #else
- _UniformRandomNumberGenerator& __g)
- #endif
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- typedef uniform_int_distribution<ptrdiff_t> _Dp;
- typedef typename _Dp::param_type _Pp;
- difference_type __d = __last - __first;
- if (__d > 1)
- {
- _Dp __uid;
- for (--__last, --__d; __first < __last; ++__first, --__d)
- {
- difference_type __i = __uid(__g, _Pp(0, __d));
- if (__i != difference_type(0))
- swap(*__first, *(__first + __i));
- }
- }
- }
- template <class _InputIterator, class _Predicate>
- bool
- is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (!__pred(*__first))
- break;
- if ( __first == __last )
- return true;
- ++__first;
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- return false;
- return true;
- }
- // partition
- template <class _Predicate, class _ForwardIterator>
- _ForwardIterator
- __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
- {
- while (true)
- {
- if (__first == __last)
- return __first;
- if (!__pred(*__first))
- break;
- ++__first;
- }
- for (_ForwardIterator __p = __first; ++__p != __last;)
- {
- if (__pred(*__p))
- {
- swap(*__first, *__p);
- ++__first;
- }
- }
- return __first;
- }
- template <class _Predicate, class _BidirectionalIterator>
- _BidirectionalIterator
- __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
- bidirectional_iterator_tag)
- {
- while (true)
- {
- while (true)
- {
- if (__first == __last)
- return __first;
- if (!__pred(*__first))
- break;
- ++__first;
- }
- do
- {
- if (__first == --__last)
- return __first;
- } while (!__pred(*__last));
- swap(*__first, *__last);
- ++__first;
- }
- }
- template <class _ForwardIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
- {
- return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
- (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
- }
- // partition_copy
- template <class _InputIterator, class _OutputIterator1,
- class _OutputIterator2, class _Predicate>
- pair<_OutputIterator1, _OutputIterator2>
- partition_copy(_InputIterator __first, _InputIterator __last,
- _OutputIterator1 __out_true, _OutputIterator2 __out_false,
- _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- {
- if (__pred(*__first))
- {
- *__out_true = *__first;
- ++__out_true;
- }
- else
- {
- *__out_false = *__first;
- ++__out_false;
- }
- }
- return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
- }
- // partition_point
- template<class _ForwardIterator, class _Predicate>
- _ForwardIterator
- partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
- {
- typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
- difference_type __len = _VSTD::distance(__first, __last);
- while (__len != 0)
- {
- difference_type __l2 = __len / 2;
- _ForwardIterator __m = __first;
- _VSTD::advance(__m, __l2);
- if (__pred(*__m))
- {
- __first = ++__m;
- __len -= __l2 + 1;
- }
- else
- __len = __l2;
- }
- return __first;
- }
- // stable_partition
- template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
- _ForwardIterator
- __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
- _Distance __len, _Pair __p, forward_iterator_tag __fit)
- {
- // *__first is known to be false
- // __len >= 1
- if (__len == 1)
- return __first;
- if (__len == 2)
- {
- _ForwardIterator __m = __first;
- if (__pred(*++__m))
- {
- swap(*__first, *__m);
- return __m;
- }
- return __first;
- }
- if (__len <= __p.second)
- { // The buffer is big enough to use
- typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
- // Move the falses into the temporary buffer, and the trues to the front of the line
- // Update __first to always point to the end of the trues
- value_type* __t = __p.first;
- ::new(__t) value_type(_VSTD::move(*__first));
- __d.__incr((value_type*)0);
- ++__t;
- _ForwardIterator __i = __first;
- while (++__i != __last)
- {
- if (__pred(*__i))
- {
- *__first = _VSTD::move(*__i);
- ++__first;
- }
- else
- {
- ::new(__t) value_type(_VSTD::move(*__i));
- __d.__incr((value_type*)0);
- ++__t;
- }
- }
- // All trues now at start of range, all falses in buffer
- // Move falses back into range, but don't mess up __first which points to first false
- __i = __first;
- for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
- *__i = _VSTD::move(*__t2);
- // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
- return __first;
- }
- // Else not enough buffer, do in place
- // __len >= 3
- _ForwardIterator __m = __first;
- _Distance __len2 = __len / 2; // __len2 >= 2
- _VSTD::advance(__m, __len2);
- // recurse on [__first, __m), *__first know to be false
- // F?????????????????
- // f m l
- typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
- _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
- // TTTFFFFF??????????
- // f ff m l
- // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
- _ForwardIterator __m1 = __m;
- _ForwardIterator __second_false = __last;
- _Distance __len_half = __len - __len2;
- while (__pred(*__m1))
- {
- if (++__m1 == __last)
- goto __second_half_done;
- --__len_half;
- }
- // TTTFFFFFTTTF??????
- // f ff m m1 l
- __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
- __second_half_done:
- // TTTFFFFFTTTTTFFFFF
- // f ff m sf l
- return _VSTD::rotate(__first_false, __m, __second_false);
- // TTTTTTTTFFFFFFFFFF
- // |
- }
- struct __return_temporary_buffer
- {
- template <class _Tp>
- _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
- };
- template <class _Predicate, class _ForwardIterator>
- _ForwardIterator
- __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
- forward_iterator_tag)
- {
- const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
- // Either prove all true and return __first or point to first false
- while (true)
- {
- if (__first == __last)
- return __first;
- if (!__pred(*__first))
- break;
- ++__first;
- }
- // We now have a reduced range [__first, __last)
- // *__first is known to be false
- typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
- typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
- difference_type __len = _VSTD::distance(__first, __last);
- pair<value_type*, ptrdiff_t> __p(0, 0);
- unique_ptr<value_type, __return_temporary_buffer> __h;
- if (__len >= __alloc_limit)
- {
- __p = _VSTD::get_temporary_buffer<value_type>(__len);
- __h.reset(__p.first);
- }
- return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
- (__first, __last, __pred, __len, __p, forward_iterator_tag());
- }
- template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
- _BidirectionalIterator
- __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
- _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
- {
- // *__first is known to be false
- // *__last is known to be true
- // __len >= 2
- if (__len == 2)
- {
- swap(*__first, *__last);
- return __last;
- }
- if (__len == 3)
- {
- _BidirectionalIterator __m = __first;
- if (__pred(*++__m))
- {
- swap(*__first, *__m);
- swap(*__m, *__last);
- return __last;
- }
- swap(*__m, *__last);
- swap(*__first, *__m);
- return __m;
- }
- if (__len <= __p.second)
- { // The buffer is big enough to use
- typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
- // Move the falses into the temporary buffer, and the trues to the front of the line
- // Update __first to always point to the end of the trues
- value_type* __t = __p.first;
- ::new(__t) value_type(_VSTD::move(*__first));
- __d.__incr((value_type*)0);
- ++__t;
- _BidirectionalIterator __i = __first;
- while (++__i != __last)
- {
- if (__pred(*__i))
- {
- *__first = _VSTD::move(*__i);
- ++__first;
- }
- else
- {
- ::new(__t) value_type(_VSTD::move(*__i));
- __d.__incr((value_type*)0);
- ++__t;
- }
- }
- // move *__last, known to be true
- *__first = _VSTD::move(*__i);
- __i = ++__first;
- // All trues now at start of range, all falses in buffer
- // Move falses back into range, but don't mess up __first which points to first false
- for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
- *__i = _VSTD::move(*__t2);
- // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
- return __first;
- }
- // Else not enough buffer, do in place
- // __len >= 4
- _BidirectionalIterator __m = __first;
- _Distance __len2 = __len / 2; // __len2 >= 2
- _VSTD::advance(__m, __len2);
- // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
- // F????????????????T
- // f m l
- _BidirectionalIterator __m1 = __m;
- _BidirectionalIterator __first_false = __first;
- _Distance __len_half = __len2;
- while (!__pred(*--__m1))
- {
- if (__m1 == __first)
- goto __first_half_done;
- --__len_half;
- }
- // F???TFFF?????????T
- // f m1 m l
- typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
- __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
- __first_half_done:
- // TTTFFFFF?????????T
- // f ff m l
- // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
- __m1 = __m;
- _BidirectionalIterator __second_false = __last;
- ++__second_false;
- __len_half = __len - __len2;
- while (__pred(*__m1))
- {
- if (++__m1 == __last)
- goto __second_half_done;
- --__len_half;
- }
- // TTTFFFFFTTTF?????T
- // f ff m m1 l
- __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
- __second_half_done:
- // TTTFFFFFTTTTTFFFFF
- // f ff m sf l
- return _VSTD::rotate(__first_false, __m, __second_false);
- // TTTTTTTTFFFFFFFFFF
- // |
- }
- template <class _Predicate, class _BidirectionalIterator>
- _BidirectionalIterator
- __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
- bidirectional_iterator_tag)
- {
- typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
- typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
- const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
- // Either prove all true and return __first or point to first false
- while (true)
- {
- if (__first == __last)
- return __first;
- if (!__pred(*__first))
- break;
- ++__first;
- }
- // __first points to first false, everything prior to __first is already set.
- // Either prove [__first, __last) is all false and return __first, or point __last to last true
- do
- {
- if (__first == --__last)
- return __first;
- } while (!__pred(*__last));
- // We now have a reduced range [__first, __last]
- // *__first is known to be false
- // *__last is known to be true
- // __len >= 2
- difference_type __len = _VSTD::distance(__first, __last) + 1;
- pair<value_type*, ptrdiff_t> __p(0, 0);
- unique_ptr<value_type, __return_temporary_buffer> __h;
- if (__len >= __alloc_limit)
- {
- __p = _VSTD::get_temporary_buffer<value_type>(__len);
- __h.reset(__p.first);
- }
- return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
- (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
- }
- template <class _ForwardIterator, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
- {
- return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
- (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
- }
- // is_sorted_until
- template <class _ForwardIterator, class _Compare>
- _ForwardIterator
- is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
- {
- if (__first != __last)
- {
- _ForwardIterator __i = __first;
- while (++__i != __last)
- {
- if (__comp(*__i, *__first))
- return __i;
- __first = __i;
- }
- }
- return __last;
- }
- template<class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
- {
- return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // is_sorted
- template <class _ForwardIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
- {
- return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
- }
- template<class _ForwardIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_sorted(_ForwardIterator __first, _ForwardIterator __last)
- {
- return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // sort
- // stable, 2-3 compares, 0-2 swaps
- template <class _Compare, class _ForwardIterator>
- unsigned
- __sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
- {
- unsigned __r = 0;
- if (!__c(*__y, *__x)) // if x <= y
- {
- if (!__c(*__z, *__y)) // if y <= z
- return __r; // x <= y && y <= z
- // x <= y && y > z
- swap(*__y, *__z); // x <= z && y < z
- __r = 1;
- if (__c(*__y, *__x)) // if x > y
- {
- swap(*__x, *__y); // x < y && y <= z
- __r = 2;
- }
- return __r; // x <= y && y < z
- }
- if (__c(*__z, *__y)) // x > y, if y > z
- {
- swap(*__x, *__z); // x < y && y < z
- __r = 1;
- return __r;
- }
- swap(*__x, *__y); // x > y && y <= z
- __r = 1; // x < y && x <= z
- if (__c(*__z, *__y)) // if y > z
- {
- swap(*__y, *__z); // x <= y && y < z
- __r = 2;
- }
- return __r;
- } // x <= y && y <= z
- // stable, 3-6 compares, 0-5 swaps
- template <class _Compare, class _ForwardIterator>
- unsigned
- __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
- _ForwardIterator __x4, _Compare __c)
- {
- unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
- if (__c(*__x4, *__x3))
- {
- swap(*__x3, *__x4);
- ++__r;
- if (__c(*__x3, *__x2))
- {
- swap(*__x2, *__x3);
- ++__r;
- if (__c(*__x2, *__x1))
- {
- swap(*__x1, *__x2);
- ++__r;
- }
- }
- }
- return __r;
- }
- // stable, 4-10 compares, 0-9 swaps
- template <class _Compare, class _ForwardIterator>
- unsigned
- __sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
- _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
- {
- unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
- if (__c(*__x5, *__x4))
- {
- swap(*__x4, *__x5);
- ++__r;
- if (__c(*__x4, *__x3))
- {
- swap(*__x3, *__x4);
- ++__r;
- if (__c(*__x3, *__x2))
- {
- swap(*__x2, *__x3);
- ++__r;
- if (__c(*__x2, *__x1))
- {
- swap(*__x1, *__x2);
- ++__r;
- }
- }
- }
- }
- return __r;
- }
- // Assumes size > 0
- template <class _Compare, class _BirdirectionalIterator>
- void
- __selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
- {
- _BirdirectionalIterator __lm1 = __last;
- for (--__lm1; __first != __lm1; ++__first)
- {
- _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
- typename add_lvalue_reference<_Compare>::type>
- (__first, __last, __comp);
- if (__i != __first)
- swap(*__first, *__i);
- }
- }
- template <class _Compare, class _BirdirectionalIterator>
- void
- __insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
- {
- typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
- if (__first != __last)
- {
- _BirdirectionalIterator __i = __first;
- for (++__i; __i != __last; ++__i)
- {
- _BirdirectionalIterator __j = __i;
- value_type __t(_VSTD::move(*__j));
- for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
- *__j = _VSTD::move(*__k);
- *__j = _VSTD::move(__t);
- }
- }
- }
- template <class _Compare, class _RandomAccessIterator>
- void
- __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- _RandomAccessIterator __j = __first+2;
- __sort3<_Compare>(__first, __first+1, __j, __comp);
- for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
- {
- if (__comp(*__i, *__j))
- {
- value_type __t(_VSTD::move(*__i));
- _RandomAccessIterator __k = __j;
- __j = __i;
- do
- {
- *__j = _VSTD::move(*__k);
- __j = __k;
- } while (__j != __first && __comp(__t, *--__k));
- *__j = _VSTD::move(__t);
- }
- __j = __i;
- }
- }
- template <class _Compare, class _RandomAccessIterator>
- bool
- __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- switch (__last - __first)
- {
- case 0:
- case 1:
- return true;
- case 2:
- if (__comp(*--__last, *__first))
- swap(*__first, *__last);
- return true;
- case 3:
- _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
- return true;
- case 4:
- _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
- return true;
- case 5:
- _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
- return true;
- }
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- _RandomAccessIterator __j = __first+2;
- __sort3<_Compare>(__first, __first+1, __j, __comp);
- const unsigned __limit = 8;
- unsigned __count = 0;
- for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
- {
- if (__comp(*__i, *__j))
- {
- value_type __t(_VSTD::move(*__i));
- _RandomAccessIterator __k = __j;
- __j = __i;
- do
- {
- *__j = _VSTD::move(*__k);
- __j = __k;
- } while (__j != __first && __comp(__t, *--__k));
- *__j = _VSTD::move(__t);
- if (++__count == __limit)
- return ++__i == __last;
- }
- __j = __i;
- }
- return true;
- }
- template <class _Compare, class _BirdirectionalIterator>
- void
- __insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
- typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
- {
- typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
- if (__first1 != __last1)
- {
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
- value_type* __last2 = __first2;
- ::new(__last2) value_type(_VSTD::move(*__first1));
- __d.__incr((value_type*)0);
- for (++__last2; ++__first1 != __last1; ++__last2)
- {
- value_type* __j2 = __last2;
- value_type* __i2 = __j2;
- if (__comp(*__first1, *--__i2))
- {
- ::new(__j2) value_type(_VSTD::move(*__i2));
- __d.__incr((value_type*)0);
- for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
- *__j2 = _VSTD::move(*__i2);
- *__j2 = _VSTD::move(*__first1);
- }
- else
- {
- ::new(__j2) value_type(_VSTD::move(*__first1));
- __d.__incr((value_type*)0);
- }
- }
- __h.release();
- }
- }
- template <class _Compare, class _RandomAccessIterator>
- void
- __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- // _Compare is known to be a reference type
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
- is_trivially_copy_assignable<value_type>::value ? 30 : 6;
- while (true)
- {
- __restart:
- difference_type __len = __last - __first;
- switch (__len)
- {
- case 0:
- case 1:
- return;
- case 2:
- if (__comp(*--__last, *__first))
- swap(*__first, *__last);
- return;
- case 3:
- _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
- return;
- case 4:
- _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
- return;
- case 5:
- _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
- return;
- }
- if (__len <= __limit)
- {
- _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
- return;
- }
- // __len > 5
- _RandomAccessIterator __m = __first;
- _RandomAccessIterator __lm1 = __last;
- --__lm1;
- unsigned __n_swaps;
- {
- difference_type __delta;
- if (__len >= 1000)
- {
- __delta = __len/2;
- __m += __delta;
- __delta /= 2;
- __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
- }
- else
- {
- __delta = __len/2;
- __m += __delta;
- __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
- }
- }
- // *__m is median
- // partition [__first, __m) < *__m and *__m <= [__m, __last)
- // (this inhibits tossing elements equivalent to __m around unnecessarily)
- _RandomAccessIterator __i = __first;
- _RandomAccessIterator __j = __lm1;
- // j points beyond range to be tested, *__m is known to be <= *__lm1
- // The search going up is known to be guarded but the search coming down isn't.
- // Prime the downward search with a guard.
- if (!__comp(*__i, *__m)) // if *__first == *__m
- {
- // *__first == *__m, *__first doesn't go in first part
- // manually guard downward moving __j against __i
- while (true)
- {
- if (__i == --__j)
- {
- // *__first == *__m, *__m <= all other elements
- // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
- ++__i; // __first + 1
- __j = __last;
- if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
- {
- while (true)
- {
- if (__i == __j)
- return; // [__first, __last) all equivalent elements
- if (__comp(*__first, *__i))
- {
- swap(*__i, *__j);
- ++__n_swaps;
- ++__i;
- break;
- }
- ++__i;
- }
- }
- // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
- if (__i == __j)
- return;
- while (true)
- {
- while (!__comp(*__first, *__i))
- ++__i;
- while (__comp(*__first, *--__j))
- ;
- if (__i >= __j)
- break;
- swap(*__i, *__j);
- ++__n_swaps;
- ++__i;
- }
- // [__first, __i) == *__first and *__first < [__i, __last)
- // The first part is sorted, sort the secod part
- // _VSTD::__sort<_Compare>(__i, __last, __comp);
- __first = __i;
- goto __restart;
- }
- if (__comp(*__j, *__m))
- {
- swap(*__i, *__j);
- ++__n_swaps;
- break; // found guard for downward moving __j, now use unguarded partition
- }
- }
- }
- // It is known that *__i < *__m
- ++__i;
- // j points beyond range to be tested, *__m is known to be <= *__lm1
- // if not yet partitioned...
- if (__i < __j)
- {
- // known that *(__i - 1) < *__m
- // known that __i <= __m
- while (true)
- {
- // __m still guards upward moving __i
- while (__comp(*__i, *__m))
- ++__i;
- // It is now known that a guard exists for downward moving __j
- while (!__comp(*--__j, *__m))
- ;
- if (__i > __j)
- break;
- swap(*__i, *__j);
- ++__n_swaps;
- // It is known that __m != __j
- // If __m just moved, follow it
- if (__m == __i)
- __m = __j;
- ++__i;
- }
- }
- // [__first, __i) < *__m and *__m <= [__i, __last)
- if (__i != __m && __comp(*__m, *__i))
- {
- swap(*__i, *__m);
- ++__n_swaps;
- }
- // [__first, __i) < *__i and *__i <= [__i+1, __last)
- // If we were given a perfect partition, see if insertion sort is quick...
- if (__n_swaps == 0)
- {
- bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
- if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
- {
- if (__fs)
- return;
- __last = __i;
- continue;
- }
- else
- {
- if (__fs)
- {
- __first = ++__i;
- continue;
- }
- }
- }
- // sort smaller range with recursive call and larger with tail recursion elimination
- if (__i - __first < __last - __i)
- {
- _VSTD::__sort<_Compare>(__first, __i, __comp);
- // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
- __first = ++__i;
- }
- else
- {
- _VSTD::__sort<_Compare>(__i+1, __last, __comp);
- // _VSTD::__sort<_Compare>(__first, __i, __comp);
- __last = __i;
- }
- }
- }
- // This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __sort<_Comp_ref>(__first, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __sort<_Comp_ref>(__first, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort(_Tp** __first, _Tp** __last)
- {
- _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
- {
- _VSTD::sort(__first.base(), __last.base());
- }
- template <class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
- {
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
- }
- #ifdef _LIBCPP_MSVC
- #pragma warning( push )
- #pragma warning( disable: 4231)
- #endif // _LIBCPP_MSVC
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
- _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&))
- #ifdef _LIBCPP_MSVC
- #pragma warning( pop )
- #endif // _LIBCPP_MSVC
- // lower_bound
- template <class _Compare, class _ForwardIterator, class _Tp>
- _ForwardIterator
- __lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
- difference_type __len = _VSTD::distance(__first, __last);
- while (__len != 0)
- {
- difference_type __l2 = __len / 2;
- _ForwardIterator __m = __first;
- _VSTD::advance(__m, __l2);
- if (__comp(*__m, __value_))
- {
- __first = ++__m;
- __len -= __l2 + 1;
- }
- else
- __len = __l2;
- }
- return __first;
- }
- template <class _ForwardIterator, class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- return _VSTD::lower_bound(__first, __last, __value_,
- __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
- }
- // upper_bound
- template <class _Compare, class _ForwardIterator, class _Tp>
- _ForwardIterator
- __upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
- difference_type __len = _VSTD::distance(__first, __last);
- while (__len != 0)
- {
- difference_type __l2 = __len / 2;
- _ForwardIterator __m = __first;
- _VSTD::advance(__m, __l2);
- if (__comp(__value_, *__m))
- __len = __l2;
- else
- {
- __first = ++__m;
- __len -= __l2 + 1;
- }
- }
- return __first;
- }
- template <class _ForwardIterator, class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _ForwardIterator
- upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- return _VSTD::upper_bound(__first, __last, __value_,
- __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
- }
- // equal_range
- template <class _Compare, class _ForwardIterator, class _Tp>
- pair<_ForwardIterator, _ForwardIterator>
- __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
- difference_type __len = _VSTD::distance(__first, __last);
- while (__len != 0)
- {
- difference_type __l2 = __len / 2;
- _ForwardIterator __m = __first;
- _VSTD::advance(__m, __l2);
- if (__comp(*__m, __value_))
- {
- __first = ++__m;
- __len -= __l2 + 1;
- }
- else if (__comp(__value_, *__m))
- {
- __last = __m;
- __len = __l2;
- }
- else
- {
- _ForwardIterator __mp1 = __m;
- return pair<_ForwardIterator, _ForwardIterator>
- (
- __lower_bound<_Compare>(__first, __m, __value_, __comp),
- __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
- );
- }
- }
- return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
- }
- template <class _ForwardIterator, class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_ForwardIterator, _ForwardIterator>
- equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- pair<_ForwardIterator, _ForwardIterator>
- equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- return _VSTD::equal_range(__first, __last, __value_,
- __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
- }
- // binary_search
- template <class _Compare, class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- __binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
- return __first != __last && !__comp(__value_, *__first);
- }
- template <class _ForwardIterator, class _Tp, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _ForwardIterator, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
- {
- return _VSTD::binary_search(__first, __last, __value_,
- __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
- }
- // merge
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- _OutputIterator
- __merge(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- for (; __first1 != __last1; ++__result)
- {
- if (__first2 == __last2)
- return _VSTD::copy(__first1, __last1, __result);
- if (__comp(*__first2, *__first1))
- {
- *__result = *__first2;
- ++__first2;
- }
- else
- {
- *__result = *__first1;
- ++__first1;
- }
- }
- return _VSTD::copy(__first2, __last2, __result);
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- merge(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- merge(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type __v1;
- typedef typename iterator_traits<_InputIterator2>::value_type __v2;
- return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
- }
- // inplace_merge
- template <class _Compare, class _InputIterator1, class _InputIterator2,
- class _OutputIterator>
- void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- for (; __first1 != __last1; ++__result)
- {
- if (__first2 == __last2)
- {
- _VSTD::move(__first1, __last1, __result);
- return;
- }
- if (__comp(*__first2, *__first1))
- {
- *__result = _VSTD::move(*__first2);
- ++__first2;
- }
- else
- {
- *__result = _VSTD::move(*__first1);
- ++__first1;
- }
- }
- // __first2 through __last2 are already in the right spot.
- }
- template <class _Compare, class _BidirectionalIterator>
- void
- __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
- _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
- typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
- typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
- {
- typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
- if (__len1 <= __len2)
- {
- value_type* __p = __buff;
- for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
- ::new(__p) value_type(_VSTD::move(*__i));
- __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
- }
- else
- {
- value_type* __p = __buff;
- for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
- ::new(__p) value_type(_VSTD::move(*__i));
- typedef reverse_iterator<_BidirectionalIterator> _RBi;
- typedef reverse_iterator<value_type*> _Rv;
- __half_inplace_merge(_Rv(__p), _Rv(__buff),
- _RBi(__middle), _RBi(__first),
- _RBi(__last), __negate<_Compare>(__comp));
- }
- }
- template <class _Compare, class _BidirectionalIterator>
- void
- __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
- _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
- typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
- typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
- {
- typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
- while (true)
- {
- // if __middle == __last, we're done
- if (__len2 == 0)
- return;
- if (__len1 <= __buff_size || __len2 <= __buff_size)
- return __buffered_inplace_merge<_Compare>
- (__first, __middle, __last, __comp, __len1, __len2, __buff);
- // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
- for (; true; ++__first, (void) --__len1)
- {
- if (__len1 == 0)
- return;
- if (__comp(*__middle, *__first))
- break;
- }
- // __first < __middle < __last
- // *__first > *__middle
- // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
- // all elements in:
- // [__first, __m1) <= [__middle, __m2)
- // [__middle, __m2) < [__m1, __middle)
- // [__m1, __middle) <= [__m2, __last)
- // and __m1 or __m2 is in the middle of its range
- _BidirectionalIterator __m1; // "median" of [__first, __middle)
- _BidirectionalIterator __m2; // "median" of [__middle, __last)
- difference_type __len11; // distance(__first, __m1)
- difference_type __len21; // distance(__middle, __m2)
- // binary search smaller range
- if (__len1 < __len2)
- { // __len >= 1, __len2 >= 2
- __len21 = __len2 / 2;
- __m2 = __middle;
- _VSTD::advance(__m2, __len21);
- __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
- __len11 = _VSTD::distance(__first, __m1);
- }
- else
- {
- if (__len1 == 1)
- { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
- // It is known *__first > *__middle
- swap(*__first, *__middle);
- return;
- }
- // __len1 >= 2, __len2 >= 1
- __len11 = __len1 / 2;
- __m1 = __first;
- _VSTD::advance(__m1, __len11);
- __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
- __len21 = _VSTD::distance(__middle, __m2);
- }
- difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
- difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
- // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
- // swap middle two partitions
- __middle = _VSTD::rotate(__m1, __middle, __m2);
- // __len12 and __len21 now have swapped meanings
- // merge smaller range with recurisve call and larger with tail recursion elimination
- if (__len11 + __len21 < __len12 + __len22)
- {
- __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
- // __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
- __first = __middle;
- __middle = __m2;
- __len1 = __len12;
- __len2 = __len22;
- }
- else
- {
- __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
- // __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
- __last = __middle;
- __middle = __m1;
- __len1 = __len11;
- __len2 = __len21;
- }
- }
- }
- template <class _BidirectionalIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
- _Compare __comp)
- {
- typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
- typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
- difference_type __len1 = _VSTD::distance(__first, __middle);
- difference_type __len2 = _VSTD::distance(__middle, __last);
- difference_type __buf_size = _VSTD::min(__len1, __len2);
- pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
- unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
- __buf.first, __buf.second);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
- __buf.first, __buf.second);
- #endif // _LIBCPP_DEBUG
- }
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
- {
- _VSTD::inplace_merge(__first, __middle, __last,
- __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
- }
- // stable_sort
- template <class _Compare, class _InputIterator1, class _InputIterator2>
- void
- __merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
- {
- typedef typename iterator_traits<_InputIterator1>::value_type value_type;
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h(__result, __d);
- for (; true; ++__result)
- {
- if (__first1 == __last1)
- {
- for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
- ::new (__result) value_type(_VSTD::move(*__first2));
- __h.release();
- return;
- }
- if (__first2 == __last2)
- {
- for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
- ::new (__result) value_type(_VSTD::move(*__first1));
- __h.release();
- return;
- }
- if (__comp(*__first2, *__first1))
- {
- ::new (__result) value_type(_VSTD::move(*__first2));
- __d.__incr((value_type*)0);
- ++__first2;
- }
- else
- {
- ::new (__result) value_type(_VSTD::move(*__first1));
- __d.__incr((value_type*)0);
- ++__first1;
- }
- }
- }
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- void
- __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- for (; __first1 != __last1; ++__result)
- {
- if (__first2 == __last2)
- {
- for (; __first1 != __last1; ++__first1, ++__result)
- *__result = _VSTD::move(*__first1);
- return;
- }
- if (__comp(*__first2, *__first1))
- {
- *__result = _VSTD::move(*__first2);
- ++__first2;
- }
- else
- {
- *__result = _VSTD::move(*__first1);
- ++__first1;
- }
- }
- for (; __first2 != __last2; ++__first2, ++__result)
- *__result = _VSTD::move(*__first2);
- }
- template <class _Compare, class _RandomAccessIterator>
- void
- __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len,
- typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
- template <class _Compare, class _RandomAccessIterator>
- void
- __stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len,
- typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- switch (__len)
- {
- case 0:
- return;
- case 1:
- ::new(__first2) value_type(_VSTD::move(*__first1));
- return;
- case 2:
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
- if (__comp(*--__last1, *__first1))
- {
- ::new(__first2) value_type(_VSTD::move(*__last1));
- __d.__incr((value_type*)0);
- ++__first2;
- ::new(__first2) value_type(_VSTD::move(*__first1));
- }
- else
- {
- ::new(__first2) value_type(_VSTD::move(*__first1));
- __d.__incr((value_type*)0);
- ++__first2;
- ::new(__first2) value_type(_VSTD::move(*__last1));
- }
- __h2.release();
- return;
- }
- if (__len <= 8)
- {
- __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
- return;
- }
- typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
- _RandomAccessIterator __m = __first1 + __l2;
- __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
- __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
- __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
- }
- template <class _Tp>
- struct __stable_sort_switch
- {
- static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
- };
- template <class _Compare, class _RandomAccessIterator>
- void
- __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len,
- typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- switch (__len)
- {
- case 0:
- case 1:
- return;
- case 2:
- if (__comp(*--__last, *__first))
- swap(*__first, *__last);
- return;
- }
- if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
- {
- __insertion_sort<_Compare>(__first, __last, __comp);
- return;
- }
- typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
- _RandomAccessIterator __m = __first + __l2;
- if (__len <= __buff_size)
- {
- __destruct_n __d(0);
- unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
- __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
- __d.__set(__l2, (value_type*)0);
- __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
- __d.__set(__len, (value_type*)0);
- __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
- // __merge<_Compare>(move_iterator<value_type*>(__buff),
- // move_iterator<value_type*>(__buff + __l2),
- // move_iterator<_RandomAccessIterator>(__buff + __l2),
- // move_iterator<_RandomAccessIterator>(__buff + __len),
- // __first, __comp);
- return;
- }
- __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
- __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
- __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- difference_type __len = __last - __first;
- pair<value_type*, ptrdiff_t> __buf(0, 0);
- unique_ptr<value_type, __return_temporary_buffer> __h;
- if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
- {
- __buf = _VSTD::get_temporary_buffer<value_type>(__len);
- __h.reset(__buf.first);
- }
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // is_heap_until
- template <class _RandomAccessIterator, class _Compare>
- _RandomAccessIterator
- is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- difference_type __len = __last - __first;
- difference_type __p = 0;
- difference_type __c = 1;
- _RandomAccessIterator __pp = __first;
- while (__c < __len)
- {
- _RandomAccessIterator __cp = __first + __c;
- if (__comp(*__pp, *__cp))
- return __cp;
- ++__c;
- ++__cp;
- if (__c == __len)
- return __last;
- if (__comp(*__pp, *__cp))
- return __cp;
- ++__p;
- ++__pp;
- __c = 2 * __p + 1;
- }
- return __last;
- }
- template<class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _RandomAccessIterator
- is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // is_heap
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- return _VSTD::is_heap_until(__first, __last, __comp) == __last;
- }
- template<class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // push_heap
- template <class _Compare, class _RandomAccessIterator>
- void
- __sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- if (__len > 1)
- {
- __len = (__len - 2) / 2;
- _RandomAccessIterator __ptr = __first + __len;
- if (__comp(*__ptr, *--__last))
- {
- value_type __t(_VSTD::move(*__last));
- do
- {
- *__last = _VSTD::move(*__ptr);
- __last = __ptr;
- if (__len == 0)
- break;
- __len = (__len - 1) / 2;
- __ptr = __first + __len;
- } while (__comp(*__ptr, __t));
- *__last = _VSTD::move(__t);
- }
- }
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // pop_heap
- template <class _Compare, class _RandomAccessIterator>
- void
- __sift_down(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len,
- _RandomAccessIterator __start)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
- // left-child of __start is at 2 * __start + 1
- // right-child of __start is at 2 * __start + 2
- difference_type __child = __start - __first;
- if (__len < 2 || (__len - 2) / 2 < __child)
- return;
- __child = 2 * __child + 1;
- _RandomAccessIterator __child_i = __first + __child;
- if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
- // right-child exists and is greater than left-child
- ++__child_i;
- ++__child;
- }
- // check if we are in heap-order
- if (__comp(*__child_i, *__start))
- // we are, __start is larger than it's largest child
- return;
- value_type __top(_VSTD::move(*__start));
- do
- {
- // we are not in heap-order, swap the parent with it's largest child
- *__start = _VSTD::move(*__child_i);
- __start = __child_i;
- if ((__len - 2) / 2 < __child)
- break;
- // recompute the child based off of the updated parent
- __child = 2 * __child + 1;
- __child_i = __first + __child;
- if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
- // right-child exists and is greater than left-child
- ++__child_i;
- ++__child;
- }
- // check if we are in heap-order
- } while (!__comp(*__child_i, __top));
- *__start = _VSTD::move(__top);
- }
- template <class _Compare, class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
- typename iterator_traits<_RandomAccessIterator>::difference_type __len)
- {
- if (__len > 1)
- {
- swap(*__first, *--__last);
- __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
- }
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // make_heap
- template <class _Compare, class _RandomAccessIterator>
- void
- __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- difference_type __n = __last - __first;
- if (__n > 1)
- {
- // start from the first parent, there is no need to consider children
- for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
- {
- __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
- }
- }
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __make_heap<_Comp_ref>(__first, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __make_heap<_Comp_ref>(__first, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // sort_heap
- template <class _Compare, class _RandomAccessIterator>
- void
- __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
- __pop_heap<_Compare>(__first, __last, __comp, __n);
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __sort_heap<_Comp_ref>(__first, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __sort_heap<_Comp_ref>(__first, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // partial_sort
- template <class _Compare, class _RandomAccessIterator>
- void
- __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
- _Compare __comp)
- {
- __make_heap<_Compare>(__first, __middle, __comp);
- typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
- for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
- {
- if (__comp(*__i, *__first))
- {
- swap(*__i, *__first);
- __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
- }
- }
- __sort_heap<_Compare>(__first, __middle, __comp);
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
- _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
- {
- _VSTD::partial_sort(__first, __middle, __last,
- __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // partial_sort_copy
- template <class _Compare, class _InputIterator, class _RandomAccessIterator>
- _RandomAccessIterator
- __partial_sort_copy(_InputIterator __first, _InputIterator __last,
- _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
- {
- _RandomAccessIterator __r = __result_first;
- if (__r != __result_last)
- {
- for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
- *__r = *__first;
- __make_heap<_Compare>(__result_first, __r, __comp);
- typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
- for (; __first != __last; ++__first)
- if (__comp(*__first, *__result_first))
- {
- *__result_first = *__first;
- __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
- }
- __sort_heap<_Compare>(__result_first, __r, __comp);
- }
- return __r;
- }
- template <class _InputIterator, class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _RandomAccessIterator
- partial_sort_copy(_InputIterator __first, _InputIterator __last,
- _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator, class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _RandomAccessIterator
- partial_sort_copy(_InputIterator __first, _InputIterator __last,
- _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
- {
- return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
- __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // nth_element
- template <class _Compare, class _RandomAccessIterator>
- void
- __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
- {
- // _Compare is known to be a reference type
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- const difference_type __limit = 7;
- while (true)
- {
- __restart:
- if (__nth == __last)
- return;
- difference_type __len = __last - __first;
- switch (__len)
- {
- case 0:
- case 1:
- return;
- case 2:
- if (__comp(*--__last, *__first))
- swap(*__first, *__last);
- return;
- case 3:
- {
- _RandomAccessIterator __m = __first;
- _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
- return;
- }
- }
- if (__len <= __limit)
- {
- __selection_sort<_Compare>(__first, __last, __comp);
- return;
- }
- // __len > __limit >= 3
- _RandomAccessIterator __m = __first + __len/2;
- _RandomAccessIterator __lm1 = __last;
- unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
- // *__m is median
- // partition [__first, __m) < *__m and *__m <= [__m, __last)
- // (this inhibits tossing elements equivalent to __m around unnecessarily)
- _RandomAccessIterator __i = __first;
- _RandomAccessIterator __j = __lm1;
- // j points beyond range to be tested, *__lm1 is known to be <= *__m
- // The search going up is known to be guarded but the search coming down isn't.
- // Prime the downward search with a guard.
- if (!__comp(*__i, *__m)) // if *__first == *__m
- {
- // *__first == *__m, *__first doesn't go in first part
- // manually guard downward moving __j against __i
- while (true)
- {
- if (__i == --__j)
- {
- // *__first == *__m, *__m <= all other elements
- // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
- ++__i; // __first + 1
- __j = __last;
- if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
- {
- while (true)
- {
- if (__i == __j)
- return; // [__first, __last) all equivalent elements
- if (__comp(*__first, *__i))
- {
- swap(*__i, *__j);
- ++__n_swaps;
- ++__i;
- break;
- }
- ++__i;
- }
- }
- // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
- if (__i == __j)
- return;
- while (true)
- {
- while (!__comp(*__first, *__i))
- ++__i;
- while (__comp(*__first, *--__j))
- ;
- if (__i >= __j)
- break;
- swap(*__i, *__j);
- ++__n_swaps;
- ++__i;
- }
- // [__first, __i) == *__first and *__first < [__i, __last)
- // The first part is sorted,
- if (__nth < __i)
- return;
- // __nth_element the secod part
- // __nth_element<_Compare>(__i, __nth, __last, __comp);
- __first = __i;
- goto __restart;
- }
- if (__comp(*__j, *__m))
- {
- swap(*__i, *__j);
- ++__n_swaps;
- break; // found guard for downward moving __j, now use unguarded partition
- }
- }
- }
- ++__i;
- // j points beyond range to be tested, *__lm1 is known to be <= *__m
- // if not yet partitioned...
- if (__i < __j)
- {
- // known that *(__i - 1) < *__m
- while (true)
- {
- // __m still guards upward moving __i
- while (__comp(*__i, *__m))
- ++__i;
- // It is now known that a guard exists for downward moving __j
- while (!__comp(*--__j, *__m))
- ;
- if (__i >= __j)
- break;
- swap(*__i, *__j);
- ++__n_swaps;
- // It is known that __m != __j
- // If __m just moved, follow it
- if (__m == __i)
- __m = __j;
- ++__i;
- }
- }
- // [__first, __i) < *__m and *__m <= [__i, __last)
- if (__i != __m && __comp(*__m, *__i))
- {
- swap(*__i, *__m);
- ++__n_swaps;
- }
- // [__first, __i) < *__i and *__i <= [__i+1, __last)
- if (__nth == __i)
- return;
- if (__n_swaps == 0)
- {
- // We were given a perfectly partitioned sequence. Coincidence?
- if (__nth < __i)
- {
- // Check for [__first, __i) already sorted
- __j = __m = __first;
- while (++__j != __i)
- {
- if (__comp(*__j, *__m))
- // not yet sorted, so sort
- goto not_sorted;
- __m = __j;
- }
- // [__first, __i) sorted
- return;
- }
- else
- {
- // Check for [__i, __last) already sorted
- __j = __m = __i;
- while (++__j != __last)
- {
- if (__comp(*__j, *__m))
- // not yet sorted, so sort
- goto not_sorted;
- __m = __j;
- }
- // [__i, __last) sorted
- return;
- }
- }
- not_sorted:
- // __nth_element on range containing __nth
- if (__nth < __i)
- {
- // __nth_element<_Compare>(__first, __nth, __i, __comp);
- __last = __i;
- }
- else
- {
- // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
- __first = ++__i;
- }
- }
- }
- template <class _RandomAccessIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- __nth_element<_Comp_ref>(__first, __nth, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _RandomAccessIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
- {
- _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
- }
- // includes
- template <class _Compare, class _InputIterator1, class _InputIterator2>
- bool
- __includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
- _Compare __comp)
- {
- for (; __first2 != __last2; ++__first1)
- {
- if (__first1 == __last1 || __comp(*__first2, *__first1))
- return false;
- if (!__comp(*__first1, *__first2))
- ++__first2;
- }
- return true;
- }
- template <class _InputIterator1, class _InputIterator2, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
- _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
- {
- return _VSTD::includes(__first1, __last1, __first2, __last2,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // set_union
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- _OutputIterator
- __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- for (; __first1 != __last1; ++__result)
- {
- if (__first2 == __last2)
- return _VSTD::copy(__first1, __last1, __result);
- if (__comp(*__first2, *__first1))
- {
- *__result = *__first2;
- ++__first2;
- }
- else
- {
- *__result = *__first1;
- if (!__comp(*__first1, *__first2))
- ++__first2;
- ++__first1;
- }
- }
- return _VSTD::copy(__first2, __last2, __result);
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_union(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_union(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
- {
- return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // set_intersection
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- _OutputIterator
- __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1 && __first2 != __last2)
- {
- if (__comp(*__first1, *__first2))
- ++__first1;
- else
- {
- if (!__comp(*__first2, *__first1))
- {
- *__result = *__first1;
- ++__result;
- ++__first1;
- }
- ++__first2;
- }
- }
- return __result;
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
- {
- return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // set_difference
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- _OutputIterator
- __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1)
- {
- if (__first2 == __last2)
- return _VSTD::copy(__first1, __last1, __result);
- if (__comp(*__first1, *__first2))
- {
- *__result = *__first1;
- ++__result;
- ++__first1;
- }
- else
- {
- if (!__comp(*__first2, *__first1))
- ++__first1;
- ++__first2;
- }
- }
- return __result;
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
- {
- return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // set_symmetric_difference
- template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
- _OutputIterator
- __set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1)
- {
- if (__first2 == __last2)
- return _VSTD::copy(__first1, __last1, __result);
- if (__comp(*__first1, *__first2))
- {
- *__result = *__first1;
- ++__result;
- ++__first1;
- }
- else
- {
- if (__comp(*__first2, *__first1))
- {
- *__result = *__first2;
- ++__result;
- }
- else
- ++__first1;
- ++__first2;
- }
- }
- return _VSTD::copy(__first2, __last2, __result);
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- _OutputIterator
- set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
- {
- return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // lexicographical_compare
- template <class _Compare, class _InputIterator1, class _InputIterator2>
- bool
- __lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
- {
- for (; __first2 != __last2; ++__first1, (void) ++__first2)
- {
- if (__first1 == __last1 || __comp(*__first1, *__first2))
- return true;
- if (__comp(*__first2, *__first1))
- return false;
- }
- return false;
- }
- template <class _InputIterator1, class _InputIterator2, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _InputIterator1, class _InputIterator2>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2)
- {
- return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
- __less<typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>());
- }
- // next_permutation
- template <class _Compare, class _BidirectionalIterator>
- bool
- __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
- {
- _BidirectionalIterator __i = __last;
- if (__first == __last || __first == --__i)
- return false;
- while (true)
- {
- _BidirectionalIterator __ip1 = __i;
- if (__comp(*--__i, *__ip1))
- {
- _BidirectionalIterator __j = __last;
- while (!__comp(*__i, *--__j))
- ;
- swap(*__i, *__j);
- _VSTD::reverse(__ip1, __last);
- return true;
- }
- if (__i == __first)
- {
- _VSTD::reverse(__first, __last);
- return false;
- }
- }
- }
- template <class _BidirectionalIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __next_permutation<_Comp_ref>(__first, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __next_permutation<_Comp_ref>(__first, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
- {
- return _VSTD::next_permutation(__first, __last,
- __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
- }
- // prev_permutation
- template <class _Compare, class _BidirectionalIterator>
- bool
- __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
- {
- _BidirectionalIterator __i = __last;
- if (__first == __last || __first == --__i)
- return false;
- while (true)
- {
- _BidirectionalIterator __ip1 = __i;
- if (__comp(*__ip1, *--__i))
- {
- _BidirectionalIterator __j = __last;
- while (!__comp(*--__j, *__i))
- ;
- swap(*__i, *__j);
- _VSTD::reverse(__ip1, __last);
- return true;
- }
- if (__i == __first)
- {
- _VSTD::reverse(__first, __last);
- return false;
- }
- }
- }
- template <class _BidirectionalIterator, class _Compare>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
- {
- #ifdef _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
- __debug_less<_Compare> __c(__comp);
- return __prev_permutation<_Comp_ref>(__first, __last, __c);
- #else // _LIBCPP_DEBUG
- typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
- return __prev_permutation<_Comp_ref>(__first, __last, __comp);
- #endif // _LIBCPP_DEBUG
- }
- template <class _BidirectionalIterator>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
- {
- return _VSTD::prev_permutation(__first, __last,
- __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
- }
- _LIBCPP_END_NAMESPACE_STD
- #endif // _LIBCPP_ALGORITHM
|