stl_algo.h 210 KB

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