stl_algo.h 210 KB

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