stl_algo.h 211 KB

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