1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896 |
- // Algorithm implementation -*- C++ -*-
- // Copyright (C) 2001-2022 Free Software Foundation, Inc.
- //
- // This file is part of the GNU ISO C++ Library. This library is free
- // software; you can redistribute it and/or modify it under the
- // terms of the GNU General Public License as published by the
- // Free Software Foundation; either version 3, or (at your option)
- // any later version.
- // This library is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- // Under Section 7 of GPL version 3, you are granted additional
- // permissions described in the GCC Runtime Library Exception, version
- // 3.1, as published by the Free Software Foundation.
- // You should have received a copy of the GNU General Public License and
- // a copy of the GCC Runtime Library Exception along with this program;
- // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
- // <http://www.gnu.org/licenses/>.
- /*
- *
- * Copyright (c) 1994
- * Hewlett-Packard Company
- *
- * Permission to use, copy, modify, distribute and sell this software
- * and its documentation for any purpose is hereby granted without fee,
- * provided that the above copyright notice appear in all copies and
- * that both that copyright notice and this permission notice appear
- * in supporting documentation. Hewlett-Packard Company makes no
- * representations about the suitability of this software for any
- * purpose. It is provided "as is" without express or implied warranty.
- *
- *
- * Copyright (c) 1996
- * Silicon Graphics Computer Systems, Inc.
- *
- * Permission to use, copy, modify, distribute and sell this software
- * and its documentation for any purpose is hereby granted without fee,
- * provided that the above copyright notice appear in all copies and
- * that both that copyright notice and this permission notice appear
- * in supporting documentation. Silicon Graphics makes no
- * representations about the suitability of this software for any
- * purpose. It is provided "as is" without express or implied warranty.
- */
- /** @file bits/stl_algo.h
- * This is an internal header file, included by other library headers.
- * Do not attempt to use it directly. @headername{algorithm}
- */
- #ifndef _STL_ALGO_H
- #define _STL_ALGO_H 1
- #include <bits/algorithmfwd.h>
- #include <bits/stl_heap.h>
- #include <bits/stl_tempbuf.h> // for _Temporary_buffer
- #include <bits/predefined_ops.h>
- #if __cplusplus >= 201103L
- #include <bits/uniform_int_dist.h>
- #endif
- #if _GLIBCXX_HOSTED && (__cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED)
- #include <cstdlib> // for rand
- #endif
- // See concept_check.h for the __glibcxx_*_requires macros.
- namespace std _GLIBCXX_VISIBILITY(default)
- {
- _GLIBCXX_BEGIN_NAMESPACE_VERSION
- /// Swaps the median value of *__a, *__b and *__c under __comp to *__result
- template<typename _Iterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- void
- __move_median_to_first(_Iterator __result,_Iterator __a, _Iterator __b,
- _Iterator __c, _Compare __comp)
- {
- if (__comp(__a, __b))
- {
- if (__comp(__b, __c))
- std::iter_swap(__result, __b);
- else if (__comp(__a, __c))
- std::iter_swap(__result, __c);
- else
- std::iter_swap(__result, __a);
- }
- else if (__comp(__a, __c))
- std::iter_swap(__result, __a);
- else if (__comp(__b, __c))
- std::iter_swap(__result, __c);
- else
- std::iter_swap(__result, __b);
- }
- /// Provided for stable_partition to use.
- template<typename _InputIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- inline _InputIterator
- __find_if_not(_InputIterator __first, _InputIterator __last,
- _Predicate __pred)
- {
- return std::__find_if(__first, __last,
- __gnu_cxx::__ops::__negate(__pred),
- std::__iterator_category(__first));
- }
- /// Like find_if_not(), but uses and updates a count of the
- /// remaining range length instead of comparing against an end
- /// iterator.
- template<typename _InputIterator, typename _Predicate, typename _Distance>
- _GLIBCXX20_CONSTEXPR
- _InputIterator
- __find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred)
- {
- for (; __len; --__len, (void) ++__first)
- if (!__pred(__first))
- break;
- return __first;
- }
- // set_difference
- // set_intersection
- // set_symmetric_difference
- // set_union
- // for_each
- // find
- // find_if
- // find_first_of
- // adjacent_find
- // count
- // count_if
- // search
- template<typename _ForwardIterator1, typename _ForwardIterator2,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator1
- __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2,
- _BinaryPredicate __predicate)
- {
- // Test for empty ranges
- if (__first1 == __last1 || __first2 == __last2)
- return __first1;
- // Test for a pattern of length 1.
- _ForwardIterator2 __p1(__first2);
- if (++__p1 == __last2)
- return std::__find_if(__first1, __last1,
- __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
- // General case.
- _ForwardIterator1 __current = __first1;
- for (;;)
- {
- __first1 =
- std::__find_if(__first1, __last1,
- __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
- if (__first1 == __last1)
- return __last1;
- _ForwardIterator2 __p = __p1;
- __current = __first1;
- if (++__current == __last1)
- return __last1;
- while (__predicate(__current, __p))
- {
- if (++__p == __last2)
- return __first1;
- if (++__current == __last1)
- return __last1;
- }
- ++__first1;
- }
- return __first1;
- }
- // search_n
- /**
- * This is an helper function for search_n overloaded for forward iterators.
- */
- template<typename _ForwardIterator, typename _Integer,
- typename _UnaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator
- __search_n_aux(_ForwardIterator __first, _ForwardIterator __last,
- _Integer __count, _UnaryPredicate __unary_pred,
- std::forward_iterator_tag)
- {
- __first = std::__find_if(__first, __last, __unary_pred);
- while (__first != __last)
- {
- typename iterator_traits<_ForwardIterator>::difference_type
- __n = __count;
- _ForwardIterator __i = __first;
- ++__i;
- while (__i != __last && __n != 1 && __unary_pred(__i))
- {
- ++__i;
- --__n;
- }
- if (__n == 1)
- return __first;
- if (__i == __last)
- return __last;
- __first = std::__find_if(++__i, __last, __unary_pred);
- }
- return __last;
- }
- /**
- * This is an helper function for search_n overloaded for random access
- * iterators.
- */
- template<typename _RandomAccessIter, typename _Integer,
- typename _UnaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _RandomAccessIter
- __search_n_aux(_RandomAccessIter __first, _RandomAccessIter __last,
- _Integer __count, _UnaryPredicate __unary_pred,
- std::random_access_iterator_tag)
- {
- typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
- _DistanceType;
- _DistanceType __tailSize = __last - __first;
- _DistanceType __remainder = __count;
- while (__remainder <= __tailSize) // the main loop...
- {
- __first += __remainder;
- __tailSize -= __remainder;
- // __first here is always pointing to one past the last element of
- // next possible match.
- _RandomAccessIter __backTrack = __first;
- while (__unary_pred(--__backTrack))
- {
- if (--__remainder == 0)
- return (__first - __count); // Success
- }
- __remainder = __count + 1 - (__first - __backTrack);
- }
- return __last; // Failure
- }
- template<typename _ForwardIterator, typename _Integer,
- typename _UnaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator
- __search_n(_ForwardIterator __first, _ForwardIterator __last,
- _Integer __count,
- _UnaryPredicate __unary_pred)
- {
- if (__count <= 0)
- return __first;
- if (__count == 1)
- return std::__find_if(__first, __last, __unary_pred);
- return std::__search_n_aux(__first, __last, __count, __unary_pred,
- std::__iterator_category(__first));
- }
- // find_end for forward iterators.
- template<typename _ForwardIterator1, typename _ForwardIterator2,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator1
- __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2,
- forward_iterator_tag, forward_iterator_tag,
- _BinaryPredicate __comp)
- {
- if (__first2 == __last2)
- return __last1;
- _ForwardIterator1 __result = __last1;
- while (1)
- {
- _ForwardIterator1 __new_result
- = std::__search(__first1, __last1, __first2, __last2, __comp);
- if (__new_result == __last1)
- return __result;
- else
- {
- __result = __new_result;
- __first1 = __new_result;
- ++__first1;
- }
- }
- }
- // find_end for bidirectional iterators (much faster).
- template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _BidirectionalIterator1
- __find_end(_BidirectionalIterator1 __first1,
- _BidirectionalIterator1 __last1,
- _BidirectionalIterator2 __first2,
- _BidirectionalIterator2 __last2,
- bidirectional_iterator_tag, bidirectional_iterator_tag,
- _BinaryPredicate __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_BidirectionalIteratorConcept<
- _BidirectionalIterator1>)
- __glibcxx_function_requires(_BidirectionalIteratorConcept<
- _BidirectionalIterator2>)
- typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
- typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
- _RevIterator1 __rlast1(__first1);
- _RevIterator2 __rlast2(__first2);
- _RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1,
- _RevIterator2(__last2), __rlast2,
- __comp);
- if (__rresult == __rlast1)
- return __last1;
- else
- {
- _BidirectionalIterator1 __result = __rresult.base();
- std::advance(__result, -std::distance(__first2, __last2));
- return __result;
- }
- }
- /**
- * @brief Find last matching subsequence in a sequence.
- * @ingroup non_mutating_algorithms
- * @param __first1 Start of range to search.
- * @param __last1 End of range to search.
- * @param __first2 Start of sequence to match.
- * @param __last2 End of sequence to match.
- * @return The last iterator @c i in the range
- * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
- * @p *(__first2+N) for each @c N in the range @p
- * [0,__last2-__first2), or @p __last1 if no such iterator exists.
- *
- * Searches the range @p [__first1,__last1) for a sub-sequence that
- * compares equal value-by-value with the sequence given by @p
- * [__first2,__last2) and returns an iterator to the __first
- * element of the sub-sequence, or @p __last1 if the sub-sequence
- * is not found. The sub-sequence will be the last such
- * subsequence contained in [__first1,__last1).
- *
- * Because the sub-sequence must lie completely within the range @p
- * [__first1,__last1) it must start at a position less than @p
- * __last1-(__last2-__first2) where @p __last2-__first2 is the
- * length of the sub-sequence. This means that the returned
- * iterator @c i will be in the range @p
- * [__first1,__last1-(__last2-__first2))
- */
- template<typename _ForwardIterator1, typename _ForwardIterator2>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator1
- find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
- __glibcxx_function_requires(_EqualOpConcept<
- typename iterator_traits<_ForwardIterator1>::value_type,
- typename iterator_traits<_ForwardIterator2>::value_type>)
- __glibcxx_requires_valid_range(__first1, __last1);
- __glibcxx_requires_valid_range(__first2, __last2);
- return std::__find_end(__first1, __last1, __first2, __last2,
- std::__iterator_category(__first1),
- std::__iterator_category(__first2),
- __gnu_cxx::__ops::__iter_equal_to_iter());
- }
- /**
- * @brief Find last matching subsequence in a sequence using a predicate.
- * @ingroup non_mutating_algorithms
- * @param __first1 Start of range to search.
- * @param __last1 End of range to search.
- * @param __first2 Start of sequence to match.
- * @param __last2 End of sequence to match.
- * @param __comp The predicate to use.
- * @return The last iterator @c i in the range @p
- * [__first1,__last1-(__last2-__first2)) such that @c
- * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
- * range @p [0,__last2-__first2), or @p __last1 if no such iterator
- * exists.
- *
- * Searches the range @p [__first1,__last1) for a sub-sequence that
- * compares equal value-by-value with the sequence given by @p
- * [__first2,__last2) using comp as a predicate and returns an
- * iterator to the first element of the sub-sequence, or @p __last1
- * if the sub-sequence is not found. The sub-sequence will be the
- * last such subsequence contained in [__first,__last1).
- *
- * Because the sub-sequence must lie completely within the range @p
- * [__first1,__last1) it must start at a position less than @p
- * __last1-(__last2-__first2) where @p __last2-__first2 is the
- * length of the sub-sequence. This means that the returned
- * iterator @c i will be in the range @p
- * [__first1,__last1-(__last2-__first2))
- */
- template<typename _ForwardIterator1, typename _ForwardIterator2,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator1
- find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2,
- _BinaryPredicate __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
- typename iterator_traits<_ForwardIterator1>::value_type,
- typename iterator_traits<_ForwardIterator2>::value_type>)
- __glibcxx_requires_valid_range(__first1, __last1);
- __glibcxx_requires_valid_range(__first2, __last2);
- return std::__find_end(__first1, __last1, __first2, __last2,
- std::__iterator_category(__first1),
- std::__iterator_category(__first2),
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- #if __cplusplus >= 201103L
- /**
- * @brief Checks that a predicate is true for all the elements
- * of a sequence.
- * @ingroup non_mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __pred A predicate.
- * @return True if the check is true, false otherwise.
- *
- * Returns true if @p __pred is true for each element in the range
- * @p [__first,__last), and false otherwise.
- */
- template<typename _InputIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- inline bool
- all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- { return __last == std::find_if_not(__first, __last, __pred); }
- /**
- * @brief Checks that a predicate is false for all the elements
- * of a sequence.
- * @ingroup non_mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __pred A predicate.
- * @return True if the check is true, false otherwise.
- *
- * Returns true if @p __pred is false for each element in the range
- * @p [__first,__last), and false otherwise.
- */
- template<typename _InputIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- inline bool
- none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- { return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); }
- /**
- * @brief Checks that a predicate is true for at least one element
- * of a sequence.
- * @ingroup non_mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __pred A predicate.
- * @return True if the check is true, false otherwise.
- *
- * Returns true if an element exists in the range @p
- * [__first,__last) such that @p __pred is true, and false
- * otherwise.
- */
- template<typename _InputIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- inline bool
- any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- { return !std::none_of(__first, __last, __pred); }
- /**
- * @brief Find the first element in a sequence for which a
- * predicate is false.
- * @ingroup non_mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __pred A predicate.
- * @return The first iterator @c i in the range @p [__first,__last)
- * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
- */
- template<typename _InputIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- inline _InputIterator
- find_if_not(_InputIterator __first, _InputIterator __last,
- _Predicate __pred)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__find_if_not(__first, __last,
- __gnu_cxx::__ops::__pred_iter(__pred));
- }
- /**
- * @brief Checks whether the sequence is partitioned.
- * @ingroup mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __pred A predicate.
- * @return True if the range @p [__first,__last) is partioned by @p __pred,
- * i.e. if all elements that satisfy @p __pred appear before those that
- * do not.
- */
- template<typename _InputIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- inline bool
- is_partitioned(_InputIterator __first, _InputIterator __last,
- _Predicate __pred)
- {
- __first = std::find_if_not(__first, __last, __pred);
- if (__first == __last)
- return true;
- ++__first;
- return std::none_of(__first, __last, __pred);
- }
- /**
- * @brief Find the partition point of a partitioned range.
- * @ingroup mutating_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __pred A predicate.
- * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
- * and @p none_of(mid, __last, __pred) are both true.
- */
- template<typename _ForwardIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator
- partition_point(_ForwardIterator __first, _ForwardIterator __last,
- _Predicate __pred)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_ForwardIterator>::value_type>)
- // A specific debug-mode test will be necessary...
- __glibcxx_requires_valid_range(__first, __last);
- typedef typename iterator_traits<_ForwardIterator>::difference_type
- _DistanceType;
- _DistanceType __len = std::distance(__first, __last);
- while (__len > 0)
- {
- _DistanceType __half = __len >> 1;
- _ForwardIterator __middle = __first;
- std::advance(__middle, __half);
- if (__pred(*__middle))
- {
- __first = __middle;
- ++__first;
- __len = __len - __half - 1;
- }
- else
- __len = __half;
- }
- return __first;
- }
- #endif
- template<typename _InputIterator, typename _OutputIterator,
- typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- __remove_copy_if(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result, _Predicate __pred)
- {
- for (; __first != __last; ++__first)
- if (!__pred(__first))
- {
- *__result = *__first;
- ++__result;
- }
- return __result;
- }
- /**
- * @brief Copy a sequence, removing elements of a given value.
- * @ingroup mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __result An output iterator.
- * @param __value The value to be removed.
- * @return An iterator designating the end of the resulting sequence.
- *
- * Copies each element in the range @p [__first,__last) not equal
- * to @p __value to the range beginning at @p __result.
- * remove_copy() is stable, so the relative order of elements that
- * are copied is unchanged.
- */
- template<typename _InputIterator, typename _OutputIterator, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- remove_copy(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result, const _Tp& __value)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_function_requires(_EqualOpConcept<
- typename iterator_traits<_InputIterator>::value_type, _Tp>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__remove_copy_if(__first, __last, __result,
- __gnu_cxx::__ops::__iter_equals_val(__value));
- }
- /**
- * @brief Copy a sequence, removing elements for which a predicate is true.
- * @ingroup mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __result An output iterator.
- * @param __pred A predicate.
- * @return An iterator designating the end of the resulting sequence.
- *
- * Copies each element in the range @p [__first,__last) for which
- * @p __pred returns false to the range beginning at @p __result.
- *
- * remove_copy_if() is stable, so the relative order of elements that are
- * copied is unchanged.
- */
- template<typename _InputIterator, typename _OutputIterator,
- typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- remove_copy_if(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result, _Predicate __pred)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__remove_copy_if(__first, __last, __result,
- __gnu_cxx::__ops::__pred_iter(__pred));
- }
- #if __cplusplus >= 201103L
- /**
- * @brief Copy the elements of a sequence for which a predicate is true.
- * @ingroup mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __result An output iterator.
- * @param __pred A predicate.
- * @return An iterator designating the end of the resulting sequence.
- *
- * Copies each element in the range @p [__first,__last) for which
- * @p __pred returns true to the range beginning at @p __result.
- *
- * copy_if() is stable, so the relative order of elements that are
- * copied is unchanged.
- */
- template<typename _InputIterator, typename _OutputIterator,
- typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- copy_if(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result, _Predicate __pred)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- {
- *__result = *__first;
- ++__result;
- }
- return __result;
- }
- template<typename _InputIterator, typename _Size, typename _OutputIterator>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- __copy_n(_InputIterator __first, _Size __n,
- _OutputIterator __result, input_iterator_tag)
- {
- return std::__niter_wrap(__result,
- __copy_n_a(__first, __n,
- std::__niter_base(__result), true));
- }
- template<typename _RandomAccessIterator, typename _Size,
- typename _OutputIterator>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- __copy_n(_RandomAccessIterator __first, _Size __n,
- _OutputIterator __result, random_access_iterator_tag)
- { return std::copy(__first, __first + __n, __result); }
- /**
- * @brief Copies the range [first,first+n) into [result,result+n).
- * @ingroup mutating_algorithms
- * @param __first An input iterator.
- * @param __n The number of elements to copy.
- * @param __result An output iterator.
- * @return result+n.
- *
- * This inline function will boil down to a call to @c memmove whenever
- * possible. Failing that, if random access iterators are passed, then the
- * loop count will be known (and therefore a candidate for compiler
- * optimizations such as unrolling).
- */
- template<typename _InputIterator, typename _Size, typename _OutputIterator>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator>::value_type>)
- const auto __n2 = std::__size_to_integer(__n);
- if (__n2 <= 0)
- return __result;
- __glibcxx_requires_can_increment(__first, __n2);
- __glibcxx_requires_can_increment(__result, __n2);
- return std::__copy_n(__first, __n2, __result,
- std::__iterator_category(__first));
- }
- /**
- * @brief Copy the elements of a sequence to separate output sequences
- * depending on the truth value of a predicate.
- * @ingroup mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __out_true An output iterator.
- * @param __out_false An output iterator.
- * @param __pred A predicate.
- * @return A pair designating the ends of the resulting sequences.
- *
- * Copies each element in the range @p [__first,__last) for which
- * @p __pred returns true to the range beginning at @p out_true
- * and each element for which @p __pred returns false to @p __out_false.
- */
- template<typename _InputIterator, typename _OutputIterator1,
- typename _OutputIterator2, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- pair<_OutputIterator1, _OutputIterator2>
- partition_copy(_InputIterator __first, _InputIterator __last,
- _OutputIterator1 __out_true, _OutputIterator2 __out_false,
- _Predicate __pred)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
-
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- {
- *__out_true = *__first;
- ++__out_true;
- }
- else
- {
- *__out_false = *__first;
- ++__out_false;
- }
- return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
- }
- #endif // C++11
- /**
- * @brief Remove elements from a sequence.
- * @ingroup mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __value The value to be removed.
- * @return An iterator designating the end of the resulting sequence.
- *
- * All elements equal to @p __value are removed from the range
- * @p [__first,__last).
- *
- * remove() is stable, so the relative order of elements that are
- * not removed is unchanged.
- *
- * Elements between the end of the resulting sequence and @p __last
- * are still present, but their value is unspecified.
- */
- template<typename _ForwardIterator, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- remove(_ForwardIterator __first, _ForwardIterator __last,
- const _Tp& __value)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
- _ForwardIterator>)
- __glibcxx_function_requires(_EqualOpConcept<
- typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__remove_if(__first, __last,
- __gnu_cxx::__ops::__iter_equals_val(__value));
- }
- /**
- * @brief Remove elements from a sequence using a predicate.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __pred A predicate.
- * @return An iterator designating the end of the resulting sequence.
- *
- * All elements for which @p __pred returns true are removed from the range
- * @p [__first,__last).
- *
- * remove_if() is stable, so the relative order of elements that are
- * not removed is unchanged.
- *
- * Elements between the end of the resulting sequence and @p __last
- * are still present, but their value is unspecified.
- */
- template<typename _ForwardIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- remove_if(_ForwardIterator __first, _ForwardIterator __last,
- _Predicate __pred)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
- _ForwardIterator>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__remove_if(__first, __last,
- __gnu_cxx::__ops::__pred_iter(__pred));
- }
- template<typename _ForwardIterator, typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator
- __adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
- _BinaryPredicate __binary_pred)
- {
- if (__first == __last)
- return __last;
- _ForwardIterator __next = __first;
- while (++__next != __last)
- {
- if (__binary_pred(__first, __next))
- return __first;
- __first = __next;
- }
- return __last;
- }
- template<typename _ForwardIterator, typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator
- __unique(_ForwardIterator __first, _ForwardIterator __last,
- _BinaryPredicate __binary_pred)
- {
- // Skip the beginning, if already unique.
- __first = std::__adjacent_find(__first, __last, __binary_pred);
- if (__first == __last)
- return __last;
- // Do the real copy work.
- _ForwardIterator __dest = __first;
- ++__first;
- while (++__first != __last)
- if (!__binary_pred(__dest, __first))
- *++__dest = _GLIBCXX_MOVE(*__first);
- return ++__dest;
- }
- /**
- * @brief Remove consecutive duplicate values from a sequence.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @return An iterator designating the end of the resulting sequence.
- *
- * Removes all but the first element from each group of consecutive
- * values that compare equal.
- * unique() is stable, so the relative order of elements that are
- * not removed is unchanged.
- * Elements between the end of the resulting sequence and @p __last
- * are still present, but their value is unspecified.
- */
- template<typename _ForwardIterator>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- unique(_ForwardIterator __first, _ForwardIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
- _ForwardIterator>)
- __glibcxx_function_requires(_EqualityComparableConcept<
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__unique(__first, __last,
- __gnu_cxx::__ops::__iter_equal_to_iter());
- }
- /**
- * @brief Remove consecutive values from a sequence using a predicate.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __binary_pred A binary predicate.
- * @return An iterator designating the end of the resulting sequence.
- *
- * Removes all but the first element from each group of consecutive
- * values for which @p __binary_pred returns true.
- * unique() is stable, so the relative order of elements that are
- * not removed is unchanged.
- * Elements between the end of the resulting sequence and @p __last
- * are still present, but their value is unspecified.
- */
- template<typename _ForwardIterator, typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- unique(_ForwardIterator __first, _ForwardIterator __last,
- _BinaryPredicate __binary_pred)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
- _ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
- typename iterator_traits<_ForwardIterator>::value_type,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__unique(__first, __last,
- __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
- }
- /**
- * This is an uglified
- * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
- * _BinaryPredicate)
- * overloaded for forward iterators and output iterator as result.
- */
- template<typename _ForwardIterator, typename _OutputIterator,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
- _OutputIterator __result, _BinaryPredicate __binary_pred,
- forward_iterator_tag, output_iterator_tag)
- {
- // concept requirements -- iterators already checked
- __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
- typename iterator_traits<_ForwardIterator>::value_type,
- typename iterator_traits<_ForwardIterator>::value_type>)
- _ForwardIterator __next = __first;
- *__result = *__first;
- while (++__next != __last)
- if (!__binary_pred(__first, __next))
- {
- __first = __next;
- *++__result = *__first;
- }
- return ++__result;
- }
- /**
- * This is an uglified
- * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
- * _BinaryPredicate)
- * overloaded for input iterators and output iterator as result.
- */
- template<typename _InputIterator, typename _OutputIterator,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- __unique_copy(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result, _BinaryPredicate __binary_pred,
- input_iterator_tag, output_iterator_tag)
- {
- // concept requirements -- iterators already checked
- __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
- typename iterator_traits<_InputIterator>::value_type,
- typename iterator_traits<_InputIterator>::value_type>)
- typename iterator_traits<_InputIterator>::value_type __value = *__first;
- __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred))
- __rebound_pred
- = __gnu_cxx::__ops::__iter_comp_val(__binary_pred);
- *__result = __value;
- while (++__first != __last)
- if (!__rebound_pred(__first, __value))
- {
- __value = *__first;
- *++__result = __value;
- }
- return ++__result;
- }
- /**
- * This is an uglified
- * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
- * _BinaryPredicate)
- * overloaded for input iterators and forward iterator as result.
- */
- template<typename _InputIterator, typename _ForwardIterator,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator
- __unique_copy(_InputIterator __first, _InputIterator __last,
- _ForwardIterator __result, _BinaryPredicate __binary_pred,
- input_iterator_tag, forward_iterator_tag)
- {
- // concept requirements -- iterators already checked
- __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
- typename iterator_traits<_ForwardIterator>::value_type,
- typename iterator_traits<_InputIterator>::value_type>)
- *__result = *__first;
- while (++__first != __last)
- if (!__binary_pred(__result, __first))
- *++__result = *__first;
- return ++__result;
- }
- /**
- * This is an uglified reverse(_BidirectionalIterator,
- * _BidirectionalIterator)
- * overloaded for bidirectional iterators.
- */
- template<typename _BidirectionalIterator>
- _GLIBCXX20_CONSTEXPR
- void
- __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
- bidirectional_iterator_tag)
- {
- while (true)
- if (__first == __last || __first == --__last)
- return;
- else
- {
- std::iter_swap(__first, __last);
- ++__first;
- }
- }
- /**
- * This is an uglified reverse(_BidirectionalIterator,
- * _BidirectionalIterator)
- * overloaded for random access iterators.
- */
- template<typename _RandomAccessIterator>
- _GLIBCXX20_CONSTEXPR
- void
- __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
- random_access_iterator_tag)
- {
- if (__first == __last)
- return;
- --__last;
- while (__first < __last)
- {
- std::iter_swap(__first, __last);
- ++__first;
- --__last;
- }
- }
- /**
- * @brief Reverse a sequence.
- * @ingroup mutating_algorithms
- * @param __first A bidirectional iterator.
- * @param __last A bidirectional iterator.
- * @return reverse() returns no value.
- *
- * Reverses the order of the elements in the range @p [__first,__last),
- * so that the first element becomes the last etc.
- * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
- * swaps @p *(__first+i) and @p *(__last-(i+1))
- */
- template<typename _BidirectionalIterator>
- _GLIBCXX20_CONSTEXPR
- inline void
- reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
- _BidirectionalIterator>)
- __glibcxx_requires_valid_range(__first, __last);
- std::__reverse(__first, __last, std::__iterator_category(__first));
- }
- /**
- * @brief Copy a sequence, reversing its elements.
- * @ingroup mutating_algorithms
- * @param __first A bidirectional iterator.
- * @param __last A bidirectional iterator.
- * @param __result An output iterator.
- * @return An iterator designating the end of the resulting sequence.
- *
- * Copies the elements in the range @p [__first,__last) to the
- * range @p [__result,__result+(__last-__first)) such that the
- * order of the elements is reversed. For every @c i such that @p
- * 0<=i<=(__last-__first), @p reverse_copy() performs the
- * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
- * The ranges @p [__first,__last) and @p
- * [__result,__result+(__last-__first)) must not overlap.
- */
- template<typename _BidirectionalIterator, typename _OutputIterator>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
- _OutputIterator __result)
- {
- // concept requirements
- __glibcxx_function_requires(_BidirectionalIteratorConcept<
- _BidirectionalIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_BidirectionalIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- while (__first != __last)
- {
- --__last;
- *__result = *__last;
- ++__result;
- }
- return __result;
- }
- /**
- * This is a helper function for the rotate algorithm specialized on RAIs.
- * It returns the greatest common divisor of two integer values.
- */
- template<typename _EuclideanRingElement>
- _GLIBCXX20_CONSTEXPR
- _EuclideanRingElement
- __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
- {
- while (__n != 0)
- {
- _EuclideanRingElement __t = __m % __n;
- __m = __n;
- __n = __t;
- }
- return __m;
- }
- inline namespace _V2
- {
- /// This is a helper function for the rotate algorithm.
- template<typename _ForwardIterator>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator
- __rotate(_ForwardIterator __first,
- _ForwardIterator __middle,
- _ForwardIterator __last,
- forward_iterator_tag)
- {
- if (__first == __middle)
- return __last;
- else if (__last == __middle)
- return __first;
- _ForwardIterator __first2 = __middle;
- do
- {
- std::iter_swap(__first, __first2);
- ++__first;
- ++__first2;
- if (__first == __middle)
- __middle = __first2;
- }
- while (__first2 != __last);
- _ForwardIterator __ret = __first;
- __first2 = __middle;
- while (__first2 != __last)
- {
- std::iter_swap(__first, __first2);
- ++__first;
- ++__first2;
- if (__first == __middle)
- __middle = __first2;
- else if (__first2 == __last)
- __first2 = __middle;
- }
- return __ret;
- }
- /// This is a helper function for the rotate algorithm.
- template<typename _BidirectionalIterator>
- _GLIBCXX20_CONSTEXPR
- _BidirectionalIterator
- __rotate(_BidirectionalIterator __first,
- _BidirectionalIterator __middle,
- _BidirectionalIterator __last,
- bidirectional_iterator_tag)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
- _BidirectionalIterator>)
- if (__first == __middle)
- return __last;
- else if (__last == __middle)
- return __first;
- std::__reverse(__first, __middle, bidirectional_iterator_tag());
- std::__reverse(__middle, __last, bidirectional_iterator_tag());
- while (__first != __middle && __middle != __last)
- {
- std::iter_swap(__first, --__last);
- ++__first;
- }
- if (__first == __middle)
- {
- std::__reverse(__middle, __last, bidirectional_iterator_tag());
- return __last;
- }
- else
- {
- std::__reverse(__first, __middle, bidirectional_iterator_tag());
- return __first;
- }
- }
- /// This is a helper function for the rotate algorithm.
- template<typename _RandomAccessIterator>
- _GLIBCXX20_CONSTEXPR
- _RandomAccessIterator
- __rotate(_RandomAccessIterator __first,
- _RandomAccessIterator __middle,
- _RandomAccessIterator __last,
- random_access_iterator_tag)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- if (__first == __middle)
- return __last;
- else if (__last == __middle)
- return __first;
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type
- _Distance;
- typedef typename iterator_traits<_RandomAccessIterator>::value_type
- _ValueType;
- _Distance __n = __last - __first;
- _Distance __k = __middle - __first;
- if (__k == __n - __k)
- {
- std::swap_ranges(__first, __middle, __middle);
- return __middle;
- }
- _RandomAccessIterator __p = __first;
- _RandomAccessIterator __ret = __first + (__last - __middle);
- for (;;)
- {
- if (__k < __n - __k)
- {
- if (__is_pod(_ValueType) && __k == 1)
- {
- _ValueType __t = _GLIBCXX_MOVE(*__p);
- _GLIBCXX_MOVE3(__p + 1, __p + __n, __p);
- *(__p + __n - 1) = _GLIBCXX_MOVE(__t);
- return __ret;
- }
- _RandomAccessIterator __q = __p + __k;
- for (_Distance __i = 0; __i < __n - __k; ++ __i)
- {
- std::iter_swap(__p, __q);
- ++__p;
- ++__q;
- }
- __n %= __k;
- if (__n == 0)
- return __ret;
- std::swap(__n, __k);
- __k = __n - __k;
- }
- else
- {
- __k = __n - __k;
- if (__is_pod(_ValueType) && __k == 1)
- {
- _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1));
- _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n);
- *__p = _GLIBCXX_MOVE(__t);
- return __ret;
- }
- _RandomAccessIterator __q = __p + __n;
- __p = __q - __k;
- for (_Distance __i = 0; __i < __n - __k; ++ __i)
- {
- --__p;
- --__q;
- std::iter_swap(__p, __q);
- }
- __n %= __k;
- if (__n == 0)
- return __ret;
- std::swap(__n, __k);
- }
- }
- }
- // _GLIBCXX_RESOLVE_LIB_DEFECTS
- // DR 488. rotate throws away useful information
- /**
- * @brief Rotate the elements of a sequence.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __middle A forward iterator.
- * @param __last A forward iterator.
- * @return first + (last - middle).
- *
- * Rotates the elements of the range @p [__first,__last) by
- * @p (__middle - __first) positions so that the element at @p __middle
- * is moved to @p __first, the element at @p __middle+1 is moved to
- * @p __first+1 and so on for each element in the range
- * @p [__first,__last).
- *
- * This effectively swaps the ranges @p [__first,__middle) and
- * @p [__middle,__last).
- *
- * Performs
- * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
- * for each @p n in the range @p [0,__last-__first).
- */
- template<typename _ForwardIterator>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- rotate(_ForwardIterator __first, _ForwardIterator __middle,
- _ForwardIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
- _ForwardIterator>)
- __glibcxx_requires_valid_range(__first, __middle);
- __glibcxx_requires_valid_range(__middle, __last);
- return std::__rotate(__first, __middle, __last,
- std::__iterator_category(__first));
- }
- } // namespace _V2
- /**
- * @brief Copy a sequence, rotating its elements.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __middle A forward iterator.
- * @param __last A forward iterator.
- * @param __result An output iterator.
- * @return An iterator designating the end of the resulting sequence.
- *
- * Copies the elements of the range @p [__first,__last) to the
- * range beginning at @result, rotating the copied elements by
- * @p (__middle-__first) positions so that the element at @p __middle
- * is moved to @p __result, the element at @p __middle+1 is moved
- * to @p __result+1 and so on for each element in the range @p
- * [__first,__last).
- *
- * Performs
- * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
- * for each @p n in the range @p [0,__last-__first).
- */
- template<typename _ForwardIterator, typename _OutputIterator>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
- _ForwardIterator __last, _OutputIterator __result)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __middle);
- __glibcxx_requires_valid_range(__middle, __last);
- return std::copy(__first, __middle,
- std::copy(__middle, __last, __result));
- }
- /// This is a helper function...
- template<typename _ForwardIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator
- __partition(_ForwardIterator __first, _ForwardIterator __last,
- _Predicate __pred, forward_iterator_tag)
- {
- if (__first == __last)
- return __first;
- while (__pred(*__first))
- if (++__first == __last)
- return __first;
- _ForwardIterator __next = __first;
- while (++__next != __last)
- if (__pred(*__next))
- {
- std::iter_swap(__first, __next);
- ++__first;
- }
- return __first;
- }
- /// This is a helper function...
- template<typename _BidirectionalIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- _BidirectionalIterator
- __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
- _Predicate __pred, bidirectional_iterator_tag)
- {
- while (true)
- {
- while (true)
- if (__first == __last)
- return __first;
- else if (__pred(*__first))
- ++__first;
- else
- break;
- --__last;
- while (true)
- if (__first == __last)
- return __first;
- else if (!bool(__pred(*__last)))
- --__last;
- else
- break;
- std::iter_swap(__first, __last);
- ++__first;
- }
- }
- // partition
- /// This is a helper function...
- /// Requires __first != __last and !__pred(__first)
- /// and __len == distance(__first, __last).
- ///
- /// !__pred(__first) allows us to guarantee that we don't
- /// move-assign an element onto itself.
- template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
- typename _Distance>
- _ForwardIterator
- __stable_partition_adaptive(_ForwardIterator __first,
- _ForwardIterator __last,
- _Predicate __pred, _Distance __len,
- _Pointer __buffer,
- _Distance __buffer_size)
- {
- if (__len == 1)
- return __first;
- if (__len <= __buffer_size)
- {
- _ForwardIterator __result1 = __first;
- _Pointer __result2 = __buffer;
- // The precondition guarantees that !__pred(__first), so
- // move that element to the buffer before starting the loop.
- // This ensures that we only call __pred once per element.
- *__result2 = _GLIBCXX_MOVE(*__first);
- ++__result2;
- ++__first;
- for (; __first != __last; ++__first)
- if (__pred(__first))
- {
- *__result1 = _GLIBCXX_MOVE(*__first);
- ++__result1;
- }
- else
- {
- *__result2 = _GLIBCXX_MOVE(*__first);
- ++__result2;
- }
- _GLIBCXX_MOVE3(__buffer, __result2, __result1);
- return __result1;
- }
- _ForwardIterator __middle = __first;
- std::advance(__middle, __len / 2);
- _ForwardIterator __left_split =
- std::__stable_partition_adaptive(__first, __middle, __pred,
- __len / 2, __buffer,
- __buffer_size);
- // Advance past true-predicate values to satisfy this
- // function's preconditions.
- _Distance __right_len = __len - __len / 2;
- _ForwardIterator __right_split =
- std::__find_if_not_n(__middle, __right_len, __pred);
- if (__right_len)
- __right_split =
- std::__stable_partition_adaptive(__right_split, __last, __pred,
- __right_len,
- __buffer, __buffer_size);
- return std::rotate(__left_split, __middle, __right_split);
- }
- template<typename _ForwardIterator, typename _Predicate>
- _ForwardIterator
- __stable_partition(_ForwardIterator __first, _ForwardIterator __last,
- _Predicate __pred)
- {
- __first = std::__find_if_not(__first, __last, __pred);
- if (__first == __last)
- return __first;
- typedef typename iterator_traits<_ForwardIterator>::value_type
- _ValueType;
- typedef typename iterator_traits<_ForwardIterator>::difference_type
- _DistanceType;
- _Temporary_buffer<_ForwardIterator, _ValueType>
- __buf(__first, std::distance(__first, __last));
- return
- std::__stable_partition_adaptive(__first, __last, __pred,
- _DistanceType(__buf.requested_size()),
- __buf.begin(),
- _DistanceType(__buf.size()));
- }
- /**
- * @brief Move elements for which a predicate is true to the beginning
- * of a sequence, preserving relative ordering.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __pred A predicate functor.
- * @return An iterator @p middle such that @p __pred(i) is true for each
- * iterator @p i in the range @p [first,middle) and false for each @p i
- * in the range @p [middle,last).
- *
- * Performs the same function as @p partition() with the additional
- * guarantee that the relative ordering of elements in each group is
- * preserved, so any two elements @p x and @p y in the range
- * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
- * relative ordering after calling @p stable_partition().
- */
- template<typename _ForwardIterator, typename _Predicate>
- inline _ForwardIterator
- stable_partition(_ForwardIterator __first, _ForwardIterator __last,
- _Predicate __pred)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
- _ForwardIterator>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__stable_partition(__first, __last,
- __gnu_cxx::__ops::__pred_iter(__pred));
- }
- /// This is a helper function for the sort routines.
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- void
- __heap_select(_RandomAccessIterator __first,
- _RandomAccessIterator __middle,
- _RandomAccessIterator __last, _Compare __comp)
- {
- std::__make_heap(__first, __middle, __comp);
- for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
- if (__comp(__i, __first))
- std::__pop_heap(__first, __middle, __i, __comp);
- }
- // partial_sort
- template<typename _InputIterator, typename _RandomAccessIterator,
- typename _Compare>
- _GLIBCXX20_CONSTEXPR
- _RandomAccessIterator
- __partial_sort_copy(_InputIterator __first, _InputIterator __last,
- _RandomAccessIterator __result_first,
- _RandomAccessIterator __result_last,
- _Compare __comp)
- {
- typedef typename iterator_traits<_InputIterator>::value_type
- _InputValueType;
- typedef iterator_traits<_RandomAccessIterator> _RItTraits;
- typedef typename _RItTraits::difference_type _DistanceType;
- if (__result_first == __result_last)
- return __result_last;
- _RandomAccessIterator __result_real_last = __result_first;
- while (__first != __last && __result_real_last != __result_last)
- {
- *__result_real_last = *__first;
- ++__result_real_last;
- ++__first;
- }
-
- std::__make_heap(__result_first, __result_real_last, __comp);
- while (__first != __last)
- {
- if (__comp(__first, __result_first))
- std::__adjust_heap(__result_first, _DistanceType(0),
- _DistanceType(__result_real_last
- - __result_first),
- _InputValueType(*__first), __comp);
- ++__first;
- }
- std::__sort_heap(__result_first, __result_real_last, __comp);
- return __result_real_last;
- }
- /**
- * @brief Copy the smallest elements of a sequence.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __result_first A random-access iterator.
- * @param __result_last Another random-access iterator.
- * @return An iterator indicating the end of the resulting sequence.
- *
- * Copies and sorts the smallest N values from the range @p [__first,__last)
- * to the range beginning at @p __result_first, where the number of
- * elements to be copied, @p N, is the smaller of @p (__last-__first) and
- * @p (__result_last-__result_first).
- * After the sort if @e i and @e j are iterators in the range
- * @p [__result_first,__result_first+N) such that i precedes j then
- * *j<*i is false.
- * The value returned is @p __result_first+N.
- */
- template<typename _InputIterator, typename _RandomAccessIterator>
- _GLIBCXX20_CONSTEXPR
- inline _RandomAccessIterator
- partial_sort_copy(_InputIterator __first, _InputIterator __last,
- _RandomAccessIterator __result_first,
- _RandomAccessIterator __result_last)
- {
- #ifdef _GLIBCXX_CONCEPT_CHECKS
- typedef typename iterator_traits<_InputIterator>::value_type
- _InputValueType;
- typedef typename iterator_traits<_RandomAccessIterator>::value_type
- _OutputValueType;
- #endif
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
- _OutputValueType>)
- __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
- _OutputValueType>)
- __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- __glibcxx_requires_valid_range(__result_first, __result_last);
- return std::__partial_sort_copy(__first, __last,
- __result_first, __result_last,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Copy the smallest elements of a sequence using a predicate for
- * comparison.
- * @ingroup sorting_algorithms
- * @param __first An input iterator.
- * @param __last Another input iterator.
- * @param __result_first A random-access iterator.
- * @param __result_last Another random-access iterator.
- * @param __comp A comparison functor.
- * @return An iterator indicating the end of the resulting sequence.
- *
- * Copies and sorts the smallest N values from the range @p [__first,__last)
- * to the range beginning at @p result_first, where the number of
- * elements to be copied, @p N, is the smaller of @p (__last-__first) and
- * @p (__result_last-__result_first).
- * After the sort if @e i and @e j are iterators in the range
- * @p [__result_first,__result_first+N) such that i precedes j then
- * @p __comp(*j,*i) is false.
- * The value returned is @p __result_first+N.
- */
- template<typename _InputIterator, typename _RandomAccessIterator,
- typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline _RandomAccessIterator
- partial_sort_copy(_InputIterator __first, _InputIterator __last,
- _RandomAccessIterator __result_first,
- _RandomAccessIterator __result_last,
- _Compare __comp)
- {
- #ifdef _GLIBCXX_CONCEPT_CHECKS
- typedef typename iterator_traits<_InputIterator>::value_type
- _InputValueType;
- typedef typename iterator_traits<_RandomAccessIterator>::value_type
- _OutputValueType;
- #endif
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
- _OutputValueType>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- _InputValueType, _OutputValueType>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- _OutputValueType, _OutputValueType>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- __glibcxx_requires_valid_range(__result_first, __result_last);
- return std::__partial_sort_copy(__first, __last,
- __result_first, __result_last,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- /// This is a helper function for the sort routine.
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- void
- __unguarded_linear_insert(_RandomAccessIterator __last,
- _Compare __comp)
- {
- typename iterator_traits<_RandomAccessIterator>::value_type
- __val = _GLIBCXX_MOVE(*__last);
- _RandomAccessIterator __next = __last;
- --__next;
- while (__comp(__val, __next))
- {
- *__last = _GLIBCXX_MOVE(*__next);
- __last = __next;
- --__next;
- }
- *__last = _GLIBCXX_MOVE(__val);
- }
- /// This is a helper function for the sort routine.
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- void
- __insertion_sort(_RandomAccessIterator __first,
- _RandomAccessIterator __last, _Compare __comp)
- {
- if (__first == __last) return;
- for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
- {
- if (__comp(__i, __first))
- {
- typename iterator_traits<_RandomAccessIterator>::value_type
- __val = _GLIBCXX_MOVE(*__i);
- _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
- *__first = _GLIBCXX_MOVE(__val);
- }
- else
- std::__unguarded_linear_insert(__i,
- __gnu_cxx::__ops::__val_comp_iter(__comp));
- }
- }
- /// This is a helper function for the sort routine.
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline void
- __unguarded_insertion_sort(_RandomAccessIterator __first,
- _RandomAccessIterator __last, _Compare __comp)
- {
- for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
- std::__unguarded_linear_insert(__i,
- __gnu_cxx::__ops::__val_comp_iter(__comp));
- }
- /**
- * @doctodo
- * This controls some aspect of the sort routines.
- */
- enum { _S_threshold = 16 };
- /// This is a helper function for the sort routine.
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- void
- __final_insertion_sort(_RandomAccessIterator __first,
- _RandomAccessIterator __last, _Compare __comp)
- {
- if (__last - __first > int(_S_threshold))
- {
- std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
- std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
- __comp);
- }
- else
- std::__insertion_sort(__first, __last, __comp);
- }
- /// This is a helper function...
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- _RandomAccessIterator
- __unguarded_partition(_RandomAccessIterator __first,
- _RandomAccessIterator __last,
- _RandomAccessIterator __pivot, _Compare __comp)
- {
- while (true)
- {
- while (__comp(__first, __pivot))
- ++__first;
- --__last;
- while (__comp(__pivot, __last))
- --__last;
- if (!(__first < __last))
- return __first;
- std::iter_swap(__first, __last);
- ++__first;
- }
- }
- /// This is a helper function...
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline _RandomAccessIterator
- __unguarded_partition_pivot(_RandomAccessIterator __first,
- _RandomAccessIterator __last, _Compare __comp)
- {
- _RandomAccessIterator __mid = __first + (__last - __first) / 2;
- std::__move_median_to_first(__first, __first + 1, __mid, __last - 1,
- __comp);
- return std::__unguarded_partition(__first + 1, __last, __first, __comp);
- }
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline void
- __partial_sort(_RandomAccessIterator __first,
- _RandomAccessIterator __middle,
- _RandomAccessIterator __last,
- _Compare __comp)
- {
- std::__heap_select(__first, __middle, __last, __comp);
- std::__sort_heap(__first, __middle, __comp);
- }
- /// This is a helper function for the sort routine.
- template<typename _RandomAccessIterator, typename _Size, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- void
- __introsort_loop(_RandomAccessIterator __first,
- _RandomAccessIterator __last,
- _Size __depth_limit, _Compare __comp)
- {
- while (__last - __first > int(_S_threshold))
- {
- if (__depth_limit == 0)
- {
- std::__partial_sort(__first, __last, __last, __comp);
- return;
- }
- --__depth_limit;
- _RandomAccessIterator __cut =
- std::__unguarded_partition_pivot(__first, __last, __comp);
- std::__introsort_loop(__cut, __last, __depth_limit, __comp);
- __last = __cut;
- }
- }
- // sort
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline void
- __sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
- _Compare __comp)
- {
- if (__first != __last)
- {
- std::__introsort_loop(__first, __last,
- std::__lg(__last - __first) * 2,
- __comp);
- std::__final_insertion_sort(__first, __last, __comp);
- }
- }
- template<typename _RandomAccessIterator, typename _Size, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- void
- __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
- _RandomAccessIterator __last, _Size __depth_limit,
- _Compare __comp)
- {
- while (__last - __first > 3)
- {
- if (__depth_limit == 0)
- {
- std::__heap_select(__first, __nth + 1, __last, __comp);
- // Place the nth largest element in its final position.
- std::iter_swap(__first, __nth);
- return;
- }
- --__depth_limit;
- _RandomAccessIterator __cut =
- std::__unguarded_partition_pivot(__first, __last, __comp);
- if (__cut <= __nth)
- __first = __cut;
- else
- __last = __cut;
- }
- std::__insertion_sort(__first, __last, __comp);
- }
- // nth_element
- // lower_bound moved to stl_algobase.h
- /**
- * @brief Finds the first position in which @p __val could be inserted
- * without changing the ordering.
- * @ingroup binary_search_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __val The search term.
- * @param __comp A functor to use for comparisons.
- * @return An iterator pointing to the first element <em>not less
- * than</em> @p __val, or end() if every element is less
- * than @p __val.
- * @ingroup binary_search_algorithms
- *
- * The comparison function should have the same effects on ordering as
- * the function used for the initial sort.
- */
- template<typename _ForwardIterator, typename _Tp, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- lower_bound(_ForwardIterator __first, _ForwardIterator __last,
- const _Tp& __val, _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
- __glibcxx_requires_partitioned_lower_pred(__first, __last,
- __val, __comp);
- return std::__lower_bound(__first, __last, __val,
- __gnu_cxx::__ops::__iter_comp_val(__comp));
- }
- template<typename _ForwardIterator, typename _Tp, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator
- __upper_bound(_ForwardIterator __first, _ForwardIterator __last,
- const _Tp& __val, _Compare __comp)
- {
- typedef typename iterator_traits<_ForwardIterator>::difference_type
- _DistanceType;
- _DistanceType __len = std::distance(__first, __last);
- while (__len > 0)
- {
- _DistanceType __half = __len >> 1;
- _ForwardIterator __middle = __first;
- std::advance(__middle, __half);
- if (__comp(__val, __middle))
- __len = __half;
- else
- {
- __first = __middle;
- ++__first;
- __len = __len - __half - 1;
- }
- }
- return __first;
- }
- /**
- * @brief Finds the last position in which @p __val could be inserted
- * without changing the ordering.
- * @ingroup binary_search_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __val The search term.
- * @return An iterator pointing to the first element greater than @p __val,
- * or end() if no elements are greater than @p __val.
- * @ingroup binary_search_algorithms
- */
- template<typename _ForwardIterator, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- upper_bound(_ForwardIterator __first, _ForwardIterator __last,
- const _Tp& __val)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_LessThanOpConcept<
- _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_partitioned_upper(__first, __last, __val);
- return std::__upper_bound(__first, __last, __val,
- __gnu_cxx::__ops::__val_less_iter());
- }
- /**
- * @brief Finds the last position in which @p __val could be inserted
- * without changing the ordering.
- * @ingroup binary_search_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __val The search term.
- * @param __comp A functor to use for comparisons.
- * @return An iterator pointing to the first element greater than @p __val,
- * or end() if no elements are greater than @p __val.
- * @ingroup binary_search_algorithms
- *
- * The comparison function should have the same effects on ordering as
- * the function used for the initial sort.
- */
- template<typename _ForwardIterator, typename _Tp, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- upper_bound(_ForwardIterator __first, _ForwardIterator __last,
- const _Tp& __val, _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_partitioned_upper_pred(__first, __last,
- __val, __comp);
- return std::__upper_bound(__first, __last, __val,
- __gnu_cxx::__ops::__val_comp_iter(__comp));
- }
- template<typename _ForwardIterator, typename _Tp,
- typename _CompareItTp, typename _CompareTpIt>
- _GLIBCXX20_CONSTEXPR
- pair<_ForwardIterator, _ForwardIterator>
- __equal_range(_ForwardIterator __first, _ForwardIterator __last,
- const _Tp& __val,
- _CompareItTp __comp_it_val, _CompareTpIt __comp_val_it)
- {
- typedef typename iterator_traits<_ForwardIterator>::difference_type
- _DistanceType;
- _DistanceType __len = std::distance(__first, __last);
- while (__len > 0)
- {
- _DistanceType __half = __len >> 1;
- _ForwardIterator __middle = __first;
- std::advance(__middle, __half);
- if (__comp_it_val(__middle, __val))
- {
- __first = __middle;
- ++__first;
- __len = __len - __half - 1;
- }
- else if (__comp_val_it(__val, __middle))
- __len = __half;
- else
- {
- _ForwardIterator __left
- = std::__lower_bound(__first, __middle, __val, __comp_it_val);
- std::advance(__first, __len);
- _ForwardIterator __right
- = std::__upper_bound(++__middle, __first, __val, __comp_val_it);
- return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
- }
- }
- return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
- }
- /**
- * @brief Finds the largest subrange in which @p __val could be inserted
- * at any place in it without changing the ordering.
- * @ingroup binary_search_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __val The search term.
- * @return An pair of iterators defining the subrange.
- * @ingroup binary_search_algorithms
- *
- * This is equivalent to
- * @code
- * std::make_pair(lower_bound(__first, __last, __val),
- * upper_bound(__first, __last, __val))
- * @endcode
- * but does not actually call those functions.
- */
- template<typename _ForwardIterator, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- inline pair<_ForwardIterator, _ForwardIterator>
- equal_range(_ForwardIterator __first, _ForwardIterator __last,
- const _Tp& __val)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
- __glibcxx_function_requires(_LessThanOpConcept<
- _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_partitioned_lower(__first, __last, __val);
- __glibcxx_requires_partitioned_upper(__first, __last, __val);
- return std::__equal_range(__first, __last, __val,
- __gnu_cxx::__ops::__iter_less_val(),
- __gnu_cxx::__ops::__val_less_iter());
- }
- /**
- * @brief Finds the largest subrange in which @p __val could be inserted
- * at any place in it without changing the ordering.
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __val The search term.
- * @param __comp A functor to use for comparisons.
- * @return An pair of iterators defining the subrange.
- * @ingroup binary_search_algorithms
- *
- * This is equivalent to
- * @code
- * std::make_pair(lower_bound(__first, __last, __val, __comp),
- * upper_bound(__first, __last, __val, __comp))
- * @endcode
- * but does not actually call those functions.
- */
- template<typename _ForwardIterator, typename _Tp, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline pair<_ForwardIterator, _ForwardIterator>
- equal_range(_ForwardIterator __first, _ForwardIterator __last,
- const _Tp& __val, _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_partitioned_lower_pred(__first, __last,
- __val, __comp);
- __glibcxx_requires_partitioned_upper_pred(__first, __last,
- __val, __comp);
- return std::__equal_range(__first, __last, __val,
- __gnu_cxx::__ops::__iter_comp_val(__comp),
- __gnu_cxx::__ops::__val_comp_iter(__comp));
- }
- /**
- * @brief Determines whether an element exists in a range.
- * @ingroup binary_search_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __val The search term.
- * @return True if @p __val (or its equivalent) is in [@p
- * __first,@p __last ].
- *
- * Note that this does not actually return an iterator to @p __val. For
- * that, use std::find or a container's specialized find member functions.
- */
- template<typename _ForwardIterator, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- bool
- binary_search(_ForwardIterator __first, _ForwardIterator __last,
- const _Tp& __val)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_LessThanOpConcept<
- _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_partitioned_lower(__first, __last, __val);
- __glibcxx_requires_partitioned_upper(__first, __last, __val);
- _ForwardIterator __i
- = std::__lower_bound(__first, __last, __val,
- __gnu_cxx::__ops::__iter_less_val());
- return __i != __last && !(__val < *__i);
- }
- /**
- * @brief Determines whether an element exists in a range.
- * @ingroup binary_search_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __val The search term.
- * @param __comp A functor to use for comparisons.
- * @return True if @p __val (or its equivalent) is in @p [__first,__last].
- *
- * Note that this does not actually return an iterator to @p __val. For
- * that, use std::find or a container's specialized find member functions.
- *
- * The comparison function should have the same effects on ordering as
- * the function used for the initial sort.
- */
- template<typename _ForwardIterator, typename _Tp, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- bool
- binary_search(_ForwardIterator __first, _ForwardIterator __last,
- const _Tp& __val, _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- _Tp, typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_partitioned_lower_pred(__first, __last,
- __val, __comp);
- __glibcxx_requires_partitioned_upper_pred(__first, __last,
- __val, __comp);
- _ForwardIterator __i
- = std::__lower_bound(__first, __last, __val,
- __gnu_cxx::__ops::__iter_comp_val(__comp));
- return __i != __last && !bool(__comp(__val, *__i));
- }
- // merge
- /// This is a helper function for the __merge_adaptive routines.
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator, typename _Compare>
- void
- __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1 && __first2 != __last2)
- {
- if (__comp(__first2, __first1))
- {
- *__result = _GLIBCXX_MOVE(*__first2);
- ++__first2;
- }
- else
- {
- *__result = _GLIBCXX_MOVE(*__first1);
- ++__first1;
- }
- ++__result;
- }
- if (__first1 != __last1)
- _GLIBCXX_MOVE3(__first1, __last1, __result);
- }
- /// This is a helper function for the __merge_adaptive routines.
- template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
- typename _BidirectionalIterator3, typename _Compare>
- void
- __move_merge_adaptive_backward(_BidirectionalIterator1 __first1,
- _BidirectionalIterator1 __last1,
- _BidirectionalIterator2 __first2,
- _BidirectionalIterator2 __last2,
- _BidirectionalIterator3 __result,
- _Compare __comp)
- {
- if (__first1 == __last1)
- {
- _GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result);
- return;
- }
- else if (__first2 == __last2)
- return;
- --__last1;
- --__last2;
- while (true)
- {
- if (__comp(__last2, __last1))
- {
- *--__result = _GLIBCXX_MOVE(*__last1);
- if (__first1 == __last1)
- {
- _GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result);
- return;
- }
- --__last1;
- }
- else
- {
- *--__result = _GLIBCXX_MOVE(*__last2);
- if (__first2 == __last2)
- return;
- --__last2;
- }
- }
- }
- /// This is a helper function for the merge routines.
- template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
- typename _Distance>
- _BidirectionalIterator1
- __rotate_adaptive(_BidirectionalIterator1 __first,
- _BidirectionalIterator1 __middle,
- _BidirectionalIterator1 __last,
- _Distance __len1, _Distance __len2,
- _BidirectionalIterator2 __buffer,
- _Distance __buffer_size)
- {
- _BidirectionalIterator2 __buffer_end;
- if (__len1 > __len2 && __len2 <= __buffer_size)
- {
- if (__len2)
- {
- __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
- _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last);
- return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first);
- }
- else
- return __first;
- }
- else if (__len1 <= __buffer_size)
- {
- if (__len1)
- {
- __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
- _GLIBCXX_MOVE3(__middle, __last, __first);
- return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
- }
- else
- return __last;
- }
- else
- return std::rotate(__first, __middle, __last);
- }
- /// This is a helper function for the merge routines.
- template<typename _BidirectionalIterator, typename _Distance,
- typename _Pointer, typename _Compare>
- void
- __merge_adaptive(_BidirectionalIterator __first,
- _BidirectionalIterator __middle,
- _BidirectionalIterator __last,
- _Distance __len1, _Distance __len2,
- _Pointer __buffer, _Distance __buffer_size,
- _Compare __comp)
- {
- if (__len1 <= __len2 && __len1 <= __buffer_size)
- {
- _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
- std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last,
- __first, __comp);
- }
- else if (__len2 <= __buffer_size)
- {
- _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
- std::__move_merge_adaptive_backward(__first, __middle, __buffer,
- __buffer_end, __last, __comp);
- }
- else
- {
- _BidirectionalIterator __first_cut = __first;
- _BidirectionalIterator __second_cut = __middle;
- _Distance __len11 = 0;
- _Distance __len22 = 0;
- if (__len1 > __len2)
- {
- __len11 = __len1 / 2;
- std::advance(__first_cut, __len11);
- __second_cut
- = std::__lower_bound(__middle, __last, *__first_cut,
- __gnu_cxx::__ops::__iter_comp_val(__comp));
- __len22 = std::distance(__middle, __second_cut);
- }
- else
- {
- __len22 = __len2 / 2;
- std::advance(__second_cut, __len22);
- __first_cut
- = std::__upper_bound(__first, __middle, *__second_cut,
- __gnu_cxx::__ops::__val_comp_iter(__comp));
- __len11 = std::distance(__first, __first_cut);
- }
- _BidirectionalIterator __new_middle
- = std::__rotate_adaptive(__first_cut, __middle, __second_cut,
- __len1 - __len11, __len22, __buffer,
- __buffer_size);
- std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
- __len22, __buffer, __buffer_size, __comp);
- std::__merge_adaptive(__new_middle, __second_cut, __last,
- __len1 - __len11,
- __len2 - __len22, __buffer,
- __buffer_size, __comp);
- }
- }
- /// This is a helper function for the merge routines.
- template<typename _BidirectionalIterator, typename _Distance,
- typename _Compare>
- void
- __merge_without_buffer(_BidirectionalIterator __first,
- _BidirectionalIterator __middle,
- _BidirectionalIterator __last,
- _Distance __len1, _Distance __len2,
- _Compare __comp)
- {
- if (__len1 == 0 || __len2 == 0)
- return;
- if (__len1 + __len2 == 2)
- {
- if (__comp(__middle, __first))
- std::iter_swap(__first, __middle);
- return;
- }
- _BidirectionalIterator __first_cut = __first;
- _BidirectionalIterator __second_cut = __middle;
- _Distance __len11 = 0;
- _Distance __len22 = 0;
- if (__len1 > __len2)
- {
- __len11 = __len1 / 2;
- std::advance(__first_cut, __len11);
- __second_cut
- = std::__lower_bound(__middle, __last, *__first_cut,
- __gnu_cxx::__ops::__iter_comp_val(__comp));
- __len22 = std::distance(__middle, __second_cut);
- }
- else
- {
- __len22 = __len2 / 2;
- std::advance(__second_cut, __len22);
- __first_cut
- = std::__upper_bound(__first, __middle, *__second_cut,
- __gnu_cxx::__ops::__val_comp_iter(__comp));
- __len11 = std::distance(__first, __first_cut);
- }
- _BidirectionalIterator __new_middle
- = std::rotate(__first_cut, __middle, __second_cut);
- std::__merge_without_buffer(__first, __first_cut, __new_middle,
- __len11, __len22, __comp);
- std::__merge_without_buffer(__new_middle, __second_cut, __last,
- __len1 - __len11, __len2 - __len22, __comp);
- }
- template<typename _BidirectionalIterator, typename _Compare>
- void
- __inplace_merge(_BidirectionalIterator __first,
- _BidirectionalIterator __middle,
- _BidirectionalIterator __last,
- _Compare __comp)
- {
- typedef typename iterator_traits<_BidirectionalIterator>::value_type
- _ValueType;
- typedef typename iterator_traits<_BidirectionalIterator>::difference_type
- _DistanceType;
- typedef _Temporary_buffer<_BidirectionalIterator, _ValueType> _TmpBuf;
- if (__first == __middle || __middle == __last)
- return;
- const _DistanceType __len1 = std::distance(__first, __middle);
- const _DistanceType __len2 = std::distance(__middle, __last);
- // __merge_adaptive will use a buffer for the smaller of
- // [first,middle) and [middle,last).
- _TmpBuf __buf(__first, std::min(__len1, __len2));
- if (__buf.begin() == 0)
- std::__merge_without_buffer
- (__first, __middle, __last, __len1, __len2, __comp);
- else
- std::__merge_adaptive
- (__first, __middle, __last, __len1, __len2, __buf.begin(),
- _DistanceType(__buf.size()), __comp);
- }
- /**
- * @brief Merges two sorted ranges in place.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __middle Another iterator.
- * @param __last Another iterator.
- * @return Nothing.
- *
- * Merges two sorted and consecutive ranges, [__first,__middle) and
- * [__middle,__last), and puts the result in [__first,__last). The
- * output will be sorted. The sort is @e stable, that is, for
- * equivalent elements in the two ranges, elements from the first
- * range will always come before elements from the second.
- *
- * If enough additional memory is available, this takes (__last-__first)-1
- * comparisons. Otherwise an NlogN algorithm is used, where N is
- * distance(__first,__last).
- */
- template<typename _BidirectionalIterator>
- inline void
- inplace_merge(_BidirectionalIterator __first,
- _BidirectionalIterator __middle,
- _BidirectionalIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
- _BidirectionalIterator>)
- __glibcxx_function_requires(_LessThanComparableConcept<
- typename iterator_traits<_BidirectionalIterator>::value_type>)
- __glibcxx_requires_sorted(__first, __middle);
- __glibcxx_requires_sorted(__middle, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- std::__inplace_merge(__first, __middle, __last,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Merges two sorted ranges in place.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __middle Another iterator.
- * @param __last Another iterator.
- * @param __comp A functor to use for comparisons.
- * @return Nothing.
- *
- * Merges two sorted and consecutive ranges, [__first,__middle) and
- * [middle,last), and puts the result in [__first,__last). The output will
- * be sorted. The sort is @e stable, that is, for equivalent
- * elements in the two ranges, elements from the first range will always
- * come before elements from the second.
- *
- * If enough additional memory is available, this takes (__last-__first)-1
- * comparisons. Otherwise an NlogN algorithm is used, where N is
- * distance(__first,__last).
- *
- * The comparison function should have the same effects on ordering as
- * the function used for the initial sort.
- */
- template<typename _BidirectionalIterator, typename _Compare>
- inline void
- inplace_merge(_BidirectionalIterator __first,
- _BidirectionalIterator __middle,
- _BidirectionalIterator __last,
- _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
- _BidirectionalIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_BidirectionalIterator>::value_type,
- typename iterator_traits<_BidirectionalIterator>::value_type>)
- __glibcxx_requires_sorted_pred(__first, __middle, __comp);
- __glibcxx_requires_sorted_pred(__middle, __last, __comp);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- std::__inplace_merge(__first, __middle, __last,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- /// This is a helper function for the __merge_sort_loop routines.
- template<typename _InputIterator, typename _OutputIterator,
- typename _Compare>
- _OutputIterator
- __move_merge(_InputIterator __first1, _InputIterator __last1,
- _InputIterator __first2, _InputIterator __last2,
- _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1 && __first2 != __last2)
- {
- if (__comp(__first2, __first1))
- {
- *__result = _GLIBCXX_MOVE(*__first2);
- ++__first2;
- }
- else
- {
- *__result = _GLIBCXX_MOVE(*__first1);
- ++__first1;
- }
- ++__result;
- }
- return _GLIBCXX_MOVE3(__first2, __last2,
- _GLIBCXX_MOVE3(__first1, __last1,
- __result));
- }
- template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
- typename _Distance, typename _Compare>
- void
- __merge_sort_loop(_RandomAccessIterator1 __first,
- _RandomAccessIterator1 __last,
- _RandomAccessIterator2 __result, _Distance __step_size,
- _Compare __comp)
- {
- const _Distance __two_step = 2 * __step_size;
- while (__last - __first >= __two_step)
- {
- __result = std::__move_merge(__first, __first + __step_size,
- __first + __step_size,
- __first + __two_step,
- __result, __comp);
- __first += __two_step;
- }
- __step_size = std::min(_Distance(__last - __first), __step_size);
- std::__move_merge(__first, __first + __step_size,
- __first + __step_size, __last, __result, __comp);
- }
- template<typename _RandomAccessIterator, typename _Distance,
- typename _Compare>
- _GLIBCXX20_CONSTEXPR
- void
- __chunk_insertion_sort(_RandomAccessIterator __first,
- _RandomAccessIterator __last,
- _Distance __chunk_size, _Compare __comp)
- {
- while (__last - __first >= __chunk_size)
- {
- std::__insertion_sort(__first, __first + __chunk_size, __comp);
- __first += __chunk_size;
- }
- std::__insertion_sort(__first, __last, __comp);
- }
- enum { _S_chunk_size = 7 };
- template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
- void
- __merge_sort_with_buffer(_RandomAccessIterator __first,
- _RandomAccessIterator __last,
- _Pointer __buffer, _Compare __comp)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type
- _Distance;
- const _Distance __len = __last - __first;
- const _Pointer __buffer_last = __buffer + __len;
- _Distance __step_size = _S_chunk_size;
- std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
- while (__step_size < __len)
- {
- std::__merge_sort_loop(__first, __last, __buffer,
- __step_size, __comp);
- __step_size *= 2;
- std::__merge_sort_loop(__buffer, __buffer_last, __first,
- __step_size, __comp);
- __step_size *= 2;
- }
- }
- template<typename _RandomAccessIterator, typename _Pointer,
- typename _Distance, typename _Compare>
- void
- __stable_sort_adaptive(_RandomAccessIterator __first,
- _RandomAccessIterator __last,
- _Pointer __buffer, _Distance __buffer_size,
- _Compare __comp)
- {
- const _Distance __len = (__last - __first + 1) / 2;
- const _RandomAccessIterator __middle = __first + __len;
- if (__len > __buffer_size)
- {
- std::__stable_sort_adaptive(__first, __middle, __buffer,
- __buffer_size, __comp);
- std::__stable_sort_adaptive(__middle, __last, __buffer,
- __buffer_size, __comp);
- }
- else
- {
- std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
- std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
- }
- std::__merge_adaptive(__first, __middle, __last,
- _Distance(__middle - __first),
- _Distance(__last - __middle),
- __buffer, __buffer_size,
- __comp);
- }
- /// This is a helper function for the stable sorting routines.
- template<typename _RandomAccessIterator, typename _Compare>
- void
- __inplace_stable_sort(_RandomAccessIterator __first,
- _RandomAccessIterator __last, _Compare __comp)
- {
- if (__last - __first < 15)
- {
- std::__insertion_sort(__first, __last, __comp);
- return;
- }
- _RandomAccessIterator __middle = __first + (__last - __first) / 2;
- std::__inplace_stable_sort(__first, __middle, __comp);
- std::__inplace_stable_sort(__middle, __last, __comp);
- std::__merge_without_buffer(__first, __middle, __last,
- __middle - __first,
- __last - __middle,
- __comp);
- }
- // stable_sort
- // Set algorithms: includes, set_union, set_intersection, set_difference,
- // set_symmetric_difference. All of these algorithms have the precondition
- // that their input ranges are sorted and the postcondition that their output
- // ranges are sorted.
- template<typename _InputIterator1, typename _InputIterator2,
- typename _Compare>
- _GLIBCXX20_CONSTEXPR
- bool
- __includes(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _Compare __comp)
- {
- while (__first1 != __last1 && __first2 != __last2)
- {
- if (__comp(__first2, __first1))
- return false;
- if (!__comp(__first1, __first2))
- ++__first2;
- ++__first1;
- }
- return __first2 == __last2;
- }
- /**
- * @brief Determines whether all elements of a sequence exists in a range.
- * @param __first1 Start of search range.
- * @param __last1 End of search range.
- * @param __first2 Start of sequence
- * @param __last2 End of sequence.
- * @return True if each element in [__first2,__last2) is contained in order
- * within [__first1,__last1). False otherwise.
- * @ingroup set_algorithms
- *
- * This operation expects both [__first1,__last1) and
- * [__first2,__last2) to be sorted. Searches for the presence of
- * each element in [__first2,__last2) within [__first1,__last1).
- * The iterators over each range only move forward, so this is a
- * linear algorithm. If an element in [__first2,__last2) is not
- * found before the search iterator reaches @p __last2, false is
- * returned.
- */
- template<typename _InputIterator1, typename _InputIterator2>
- _GLIBCXX20_CONSTEXPR
- inline bool
- includes(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set(__first1, __last1, __first2);
- __glibcxx_requires_sorted_set(__first2, __last2, __first1);
- __glibcxx_requires_irreflexive2(__first1, __last1);
- __glibcxx_requires_irreflexive2(__first2, __last2);
- return std::__includes(__first1, __last1, __first2, __last2,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Determines whether all elements of a sequence exists in a range
- * using comparison.
- * @ingroup set_algorithms
- * @param __first1 Start of search range.
- * @param __last1 End of search range.
- * @param __first2 Start of sequence
- * @param __last2 End of sequence.
- * @param __comp Comparison function to use.
- * @return True if each element in [__first2,__last2) is contained
- * in order within [__first1,__last1) according to comp. False
- * otherwise. @ingroup set_algorithms
- *
- * This operation expects both [__first1,__last1) and
- * [__first2,__last2) to be sorted. Searches for the presence of
- * each element in [__first2,__last2) within [__first1,__last1),
- * using comp to decide. The iterators over each range only move
- * forward, so this is a linear algorithm. If an element in
- * [__first2,__last2) is not found before the search iterator
- * reaches @p __last2, false is returned.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline bool
- includes(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
- __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
- return std::__includes(__first1, __last1, __first2, __last2,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- // nth_element
- // merge
- // set_difference
- // set_intersection
- // set_union
- // stable_sort
- // set_symmetric_difference
- // min_element
- // max_element
- template<typename _BidirectionalIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- bool
- __next_permutation(_BidirectionalIterator __first,
- _BidirectionalIterator __last, _Compare __comp)
- {
- if (__first == __last)
- return false;
- _BidirectionalIterator __i = __first;
- ++__i;
- if (__i == __last)
- return false;
- __i = __last;
- --__i;
- for(;;)
- {
- _BidirectionalIterator __ii = __i;
- --__i;
- if (__comp(__i, __ii))
- {
- _BidirectionalIterator __j = __last;
- while (!__comp(__i, --__j))
- {}
- std::iter_swap(__i, __j);
- std::__reverse(__ii, __last,
- std::__iterator_category(__first));
- return true;
- }
- if (__i == __first)
- {
- std::__reverse(__first, __last,
- std::__iterator_category(__first));
- return false;
- }
- }
- }
- /**
- * @brief Permute range into the next @e dictionary ordering.
- * @ingroup sorting_algorithms
- * @param __first Start of range.
- * @param __last End of range.
- * @return False if wrapped to first permutation, true otherwise.
- *
- * Treats all permutations of the range as a set of @e dictionary sorted
- * sequences. Permutes the current sequence into the next one of this set.
- * Returns true if there are more sequences to generate. If the sequence
- * is the largest of the set, the smallest is generated and false returned.
- */
- template<typename _BidirectionalIterator>
- _GLIBCXX20_CONSTEXPR
- inline bool
- next_permutation(_BidirectionalIterator __first,
- _BidirectionalIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_BidirectionalIteratorConcept<
- _BidirectionalIterator>)
- __glibcxx_function_requires(_LessThanComparableConcept<
- typename iterator_traits<_BidirectionalIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- return std::__next_permutation
- (__first, __last, __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Permute range into the next @e dictionary ordering using
- * comparison functor.
- * @ingroup sorting_algorithms
- * @param __first Start of range.
- * @param __last End of range.
- * @param __comp A comparison functor.
- * @return False if wrapped to first permutation, true otherwise.
- *
- * Treats all permutations of the range [__first,__last) as a set of
- * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
- * sequence into the next one of this set. Returns true if there are more
- * sequences to generate. If the sequence is the largest of the set, the
- * smallest is generated and false returned.
- */
- template<typename _BidirectionalIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline bool
- next_permutation(_BidirectionalIterator __first,
- _BidirectionalIterator __last, _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_BidirectionalIteratorConcept<
- _BidirectionalIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_BidirectionalIterator>::value_type,
- typename iterator_traits<_BidirectionalIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- return std::__next_permutation
- (__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- template<typename _BidirectionalIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- bool
- __prev_permutation(_BidirectionalIterator __first,
- _BidirectionalIterator __last, _Compare __comp)
- {
- if (__first == __last)
- return false;
- _BidirectionalIterator __i = __first;
- ++__i;
- if (__i == __last)
- return false;
- __i = __last;
- --__i;
- for(;;)
- {
- _BidirectionalIterator __ii = __i;
- --__i;
- if (__comp(__ii, __i))
- {
- _BidirectionalIterator __j = __last;
- while (!__comp(--__j, __i))
- {}
- std::iter_swap(__i, __j);
- std::__reverse(__ii, __last,
- std::__iterator_category(__first));
- return true;
- }
- if (__i == __first)
- {
- std::__reverse(__first, __last,
- std::__iterator_category(__first));
- return false;
- }
- }
- }
- /**
- * @brief Permute range into the previous @e dictionary ordering.
- * @ingroup sorting_algorithms
- * @param __first Start of range.
- * @param __last End of range.
- * @return False if wrapped to last permutation, true otherwise.
- *
- * Treats all permutations of the range as a set of @e dictionary sorted
- * sequences. Permutes the current sequence into the previous one of this
- * set. Returns true if there are more sequences to generate. If the
- * sequence is the smallest of the set, the largest is generated and false
- * returned.
- */
- template<typename _BidirectionalIterator>
- _GLIBCXX20_CONSTEXPR
- inline bool
- prev_permutation(_BidirectionalIterator __first,
- _BidirectionalIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_BidirectionalIteratorConcept<
- _BidirectionalIterator>)
- __glibcxx_function_requires(_LessThanComparableConcept<
- typename iterator_traits<_BidirectionalIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- return std::__prev_permutation(__first, __last,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Permute range into the previous @e dictionary ordering using
- * comparison functor.
- * @ingroup sorting_algorithms
- * @param __first Start of range.
- * @param __last End of range.
- * @param __comp A comparison functor.
- * @return False if wrapped to last permutation, true otherwise.
- *
- * Treats all permutations of the range [__first,__last) as a set of
- * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
- * sequence into the previous one of this set. Returns true if there are
- * more sequences to generate. If the sequence is the smallest of the set,
- * the largest is generated and false returned.
- */
- template<typename _BidirectionalIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline bool
- prev_permutation(_BidirectionalIterator __first,
- _BidirectionalIterator __last, _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_BidirectionalIteratorConcept<
- _BidirectionalIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_BidirectionalIterator>::value_type,
- typename iterator_traits<_BidirectionalIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- return std::__prev_permutation(__first, __last,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- // replace
- // replace_if
- template<typename _InputIterator, typename _OutputIterator,
- typename _Predicate, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- __replace_copy_if(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result,
- _Predicate __pred, const _Tp& __new_value)
- {
- for (; __first != __last; ++__first, (void)++__result)
- if (__pred(__first))
- *__result = __new_value;
- else
- *__result = *__first;
- return __result;
- }
- /**
- * @brief Copy a sequence, replacing each element of one value with another
- * value.
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __result An output iterator.
- * @param __old_value The value to be replaced.
- * @param __new_value The replacement value.
- * @return The end of the output sequence, @p result+(last-first).
- *
- * Copies each element in the input range @p [__first,__last) to the
- * output range @p [__result,__result+(__last-__first)) replacing elements
- * equal to @p __old_value with @p __new_value.
- */
- template<typename _InputIterator, typename _OutputIterator, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- replace_copy(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result,
- const _Tp& __old_value, const _Tp& __new_value)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_function_requires(_EqualOpConcept<
- typename iterator_traits<_InputIterator>::value_type, _Tp>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__replace_copy_if(__first, __last, __result,
- __gnu_cxx::__ops::__iter_equals_val(__old_value),
- __new_value);
- }
- /**
- * @brief Copy a sequence, replacing each value for which a predicate
- * returns true with another value.
- * @ingroup mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __result An output iterator.
- * @param __pred A predicate.
- * @param __new_value The replacement value.
- * @return The end of the output sequence, @p __result+(__last-__first).
- *
- * Copies each element in the range @p [__first,__last) to the range
- * @p [__result,__result+(__last-__first)) replacing elements for which
- * @p __pred returns true with @p __new_value.
- */
- template<typename _InputIterator, typename _OutputIterator,
- typename _Predicate, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- replace_copy_if(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result,
- _Predicate __pred, const _Tp& __new_value)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__replace_copy_if(__first, __last, __result,
- __gnu_cxx::__ops::__pred_iter(__pred),
- __new_value);
- }
- #if __cplusplus >= 201103L
- /**
- * @brief Determines whether the elements of a sequence are sorted.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @return True if the elements are sorted, false otherwise.
- */
- template<typename _ForwardIterator>
- _GLIBCXX20_CONSTEXPR
- inline bool
- is_sorted(_ForwardIterator __first, _ForwardIterator __last)
- { return std::is_sorted_until(__first, __last) == __last; }
- /**
- * @brief Determines whether the elements of a sequence are sorted
- * according to a comparison functor.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __comp A comparison functor.
- * @return True if the elements are sorted, false otherwise.
- */
- template<typename _ForwardIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline bool
- is_sorted(_ForwardIterator __first, _ForwardIterator __last,
- _Compare __comp)
- { return std::is_sorted_until(__first, __last, __comp) == __last; }
- template<typename _ForwardIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- _ForwardIterator
- __is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
- _Compare __comp)
- {
- if (__first == __last)
- return __last;
- _ForwardIterator __next = __first;
- for (++__next; __next != __last; __first = __next, (void)++__next)
- if (__comp(__next, __first))
- return __next;
- return __next;
- }
- /**
- * @brief Determines the end of a sorted sequence.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @return An iterator pointing to the last iterator i in [__first, __last)
- * for which the range [__first, i) is sorted.
- */
- template<typename _ForwardIterator>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_LessThanComparableConcept<
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- return std::__is_sorted_until(__first, __last,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Determines the end of a sorted sequence using comparison functor.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __comp A comparison functor.
- * @return An iterator pointing to the last iterator i in [__first, __last)
- * for which the range [__first, i) is sorted.
- */
- template<typename _ForwardIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
- _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_ForwardIterator>::value_type,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- return std::__is_sorted_until(__first, __last,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- /**
- * @brief Determines min and max at once as an ordered pair.
- * @ingroup sorting_algorithms
- * @param __a A thing of arbitrary type.
- * @param __b Another thing of arbitrary type.
- * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
- * __b) otherwise.
- */
- template<typename _Tp>
- _GLIBCXX14_CONSTEXPR
- inline pair<const _Tp&, const _Tp&>
- minmax(const _Tp& __a, const _Tp& __b)
- {
- // concept requirements
- __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
- return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
- : pair<const _Tp&, const _Tp&>(__a, __b);
- }
- /**
- * @brief Determines min and max at once as an ordered pair.
- * @ingroup sorting_algorithms
- * @param __a A thing of arbitrary type.
- * @param __b Another thing of arbitrary type.
- * @param __comp A @link comparison_functors comparison functor @endlink.
- * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
- * __b) otherwise.
- */
- template<typename _Tp, typename _Compare>
- _GLIBCXX14_CONSTEXPR
- inline pair<const _Tp&, const _Tp&>
- minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
- {
- return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
- : pair<const _Tp&, const _Tp&>(__a, __b);
- }
- template<typename _ForwardIterator, typename _Compare>
- _GLIBCXX14_CONSTEXPR
- pair<_ForwardIterator, _ForwardIterator>
- __minmax_element(_ForwardIterator __first, _ForwardIterator __last,
- _Compare __comp)
- {
- _ForwardIterator __next = __first;
- if (__first == __last
- || ++__next == __last)
- return std::make_pair(__first, __first);
- _ForwardIterator __min{}, __max{};
- if (__comp(__next, __first))
- {
- __min = __next;
- __max = __first;
- }
- else
- {
- __min = __first;
- __max = __next;
- }
- __first = __next;
- ++__first;
- while (__first != __last)
- {
- __next = __first;
- if (++__next == __last)
- {
- if (__comp(__first, __min))
- __min = __first;
- else if (!__comp(__first, __max))
- __max = __first;
- break;
- }
- if (__comp(__next, __first))
- {
- if (__comp(__next, __min))
- __min = __next;
- if (!__comp(__first, __max))
- __max = __first;
- }
- else
- {
- if (__comp(__first, __min))
- __min = __first;
- if (!__comp(__next, __max))
- __max = __next;
- }
- __first = __next;
- ++__first;
- }
- return std::make_pair(__min, __max);
- }
- /**
- * @brief Return a pair of iterators pointing to the minimum and maximum
- * elements in a range.
- * @ingroup sorting_algorithms
- * @param __first Start of range.
- * @param __last End of range.
- * @return make_pair(m, M), where m is the first iterator i in
- * [__first, __last) such that no other element in the range is
- * smaller, and where M is the last iterator i in [__first, __last)
- * such that no other element in the range is larger.
- */
- template<typename _ForwardIterator>
- _GLIBCXX14_CONSTEXPR
- inline pair<_ForwardIterator, _ForwardIterator>
- minmax_element(_ForwardIterator __first, _ForwardIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_LessThanComparableConcept<
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- return std::__minmax_element(__first, __last,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Return a pair of iterators pointing to the minimum and maximum
- * elements in a range.
- * @ingroup sorting_algorithms
- * @param __first Start of range.
- * @param __last End of range.
- * @param __comp Comparison functor.
- * @return make_pair(m, M), where m is the first iterator i in
- * [__first, __last) such that no other element in the range is
- * smaller, and where M is the last iterator i in [__first, __last)
- * such that no other element in the range is larger.
- */
- template<typename _ForwardIterator, typename _Compare>
- _GLIBCXX14_CONSTEXPR
- inline pair<_ForwardIterator, _ForwardIterator>
- minmax_element(_ForwardIterator __first, _ForwardIterator __last,
- _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_ForwardIterator>::value_type,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- return std::__minmax_element(__first, __last,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- template<typename _Tp>
- _GLIBCXX14_CONSTEXPR
- inline pair<_Tp, _Tp>
- minmax(initializer_list<_Tp> __l)
- {
- __glibcxx_requires_irreflexive(__l.begin(), __l.end());
- pair<const _Tp*, const _Tp*> __p =
- std::__minmax_element(__l.begin(), __l.end(),
- __gnu_cxx::__ops::__iter_less_iter());
- return std::make_pair(*__p.first, *__p.second);
- }
- template<typename _Tp, typename _Compare>
- _GLIBCXX14_CONSTEXPR
- inline pair<_Tp, _Tp>
- minmax(initializer_list<_Tp> __l, _Compare __comp)
- {
- __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp);
- pair<const _Tp*, const _Tp*> __p =
- std::__minmax_element(__l.begin(), __l.end(),
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- return std::make_pair(*__p.first, *__p.second);
- }
- /**
- * @brief Checks whether a permutation of the second sequence is equal
- * to the first sequence.
- * @ingroup non_mutating_algorithms
- * @param __first1 Start of first range.
- * @param __last1 End of first range.
- * @param __first2 Start of second range.
- * @param __pred A binary predicate.
- * @return true if there exists a permutation of the elements in
- * the range [__first2, __first2 + (__last1 - __first1)),
- * beginning with ForwardIterator2 begin, such that
- * equal(__first1, __last1, __begin, __pred) returns true;
- * otherwise, returns false.
- */
- template<typename _ForwardIterator1, typename _ForwardIterator2,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- inline bool
- is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _BinaryPredicate __pred)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
- typename iterator_traits<_ForwardIterator1>::value_type,
- typename iterator_traits<_ForwardIterator2>::value_type>)
- __glibcxx_requires_valid_range(__first1, __last1);
- return std::__is_permutation(__first1, __last1, __first2,
- __gnu_cxx::__ops::__iter_comp_iter(__pred));
- }
- #if __cplusplus > 201103L
- template<typename _ForwardIterator1, typename _ForwardIterator2,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- bool
- __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2,
- _BinaryPredicate __pred)
- {
- using _Cat1
- = typename iterator_traits<_ForwardIterator1>::iterator_category;
- using _Cat2
- = typename iterator_traits<_ForwardIterator2>::iterator_category;
- using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>;
- using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>;
- constexpr bool __ra_iters = _It1_is_RA() && _It2_is_RA();
- if (__ra_iters)
- {
- auto __d1 = std::distance(__first1, __last1);
- auto __d2 = std::distance(__first2, __last2);
- if (__d1 != __d2)
- return false;
- }
- // Efficiently compare identical prefixes: O(N) if sequences
- // have the same elements in the same order.
- for (; __first1 != __last1 && __first2 != __last2;
- ++__first1, (void)++__first2)
- if (!__pred(__first1, __first2))
- break;
- if (__ra_iters)
- {
- if (__first1 == __last1)
- return true;
- }
- else
- {
- auto __d1 = std::distance(__first1, __last1);
- auto __d2 = std::distance(__first2, __last2);
- if (__d1 == 0 && __d2 == 0)
- return true;
- if (__d1 != __d2)
- return false;
- }
- for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
- {
- if (__scan != std::__find_if(__first1, __scan,
- __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
- continue; // We've seen this one before.
- auto __matches = std::__count_if(__first2, __last2,
- __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
- if (0 == __matches
- || std::__count_if(__scan, __last1,
- __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
- != __matches)
- return false;
- }
- return true;
- }
- /**
- * @brief Checks whether a permutaion of the second sequence is equal
- * to the first sequence.
- * @ingroup non_mutating_algorithms
- * @param __first1 Start of first range.
- * @param __last1 End of first range.
- * @param __first2 Start of second range.
- * @param __last2 End of first range.
- * @return true if there exists a permutation of the elements in the range
- * [__first2, __last2), beginning with ForwardIterator2 begin,
- * such that equal(__first1, __last1, begin) returns true;
- * otherwise, returns false.
- */
- template<typename _ForwardIterator1, typename _ForwardIterator2>
- _GLIBCXX20_CONSTEXPR
- inline bool
- is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2)
- {
- __glibcxx_requires_valid_range(__first1, __last1);
- __glibcxx_requires_valid_range(__first2, __last2);
- return
- std::__is_permutation(__first1, __last1, __first2, __last2,
- __gnu_cxx::__ops::__iter_equal_to_iter());
- }
- /**
- * @brief Checks whether a permutation of the second sequence is equal
- * to the first sequence.
- * @ingroup non_mutating_algorithms
- * @param __first1 Start of first range.
- * @param __last1 End of first range.
- * @param __first2 Start of second range.
- * @param __last2 End of first range.
- * @param __pred A binary predicate.
- * @return true if there exists a permutation of the elements in the range
- * [__first2, __last2), beginning with ForwardIterator2 begin,
- * such that equal(__first1, __last1, __begin, __pred) returns true;
- * otherwise, returns false.
- */
- template<typename _ForwardIterator1, typename _ForwardIterator2,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- inline bool
- is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2,
- _BinaryPredicate __pred)
- {
- __glibcxx_requires_valid_range(__first1, __last1);
- __glibcxx_requires_valid_range(__first2, __last2);
- return std::__is_permutation(__first1, __last1, __first2, __last2,
- __gnu_cxx::__ops::__iter_comp_iter(__pred));
- }
- #if __cplusplus >= 201703L
- #define __cpp_lib_clamp 201603L
- /**
- * @brief Returns the value clamped between lo and hi.
- * @ingroup sorting_algorithms
- * @param __val A value of arbitrary type.
- * @param __lo A lower limit of arbitrary type.
- * @param __hi An upper limit of arbitrary type.
- * @retval `__lo` if `__val < __lo`
- * @retval `__hi` if `__hi < __val`
- * @retval `__val` otherwise.
- * @pre `_Tp` is LessThanComparable and `(__hi < __lo)` is false.
- */
- template<typename _Tp>
- constexpr const _Tp&
- clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi)
- {
- __glibcxx_assert(!(__hi < __lo));
- return std::min(std::max(__val, __lo), __hi);
- }
- /**
- * @brief Returns the value clamped between lo and hi.
- * @ingroup sorting_algorithms
- * @param __val A value of arbitrary type.
- * @param __lo A lower limit of arbitrary type.
- * @param __hi An upper limit of arbitrary type.
- * @param __comp A comparison functor.
- * @retval `__lo` if `__comp(__val, __lo)`
- * @retval `__hi` if `__comp(__hi, __val)`
- * @retval `__val` otherwise.
- * @pre `__comp(__hi, __lo)` is false.
- */
- template<typename _Tp, typename _Compare>
- constexpr const _Tp&
- clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
- {
- __glibcxx_assert(!__comp(__hi, __lo));
- return std::min(std::max(__val, __lo, __comp), __hi, __comp);
- }
- #endif // C++17
- #endif // C++14
- #ifdef _GLIBCXX_USE_C99_STDINT_TR1
- /**
- * @brief Generate two uniformly distributed integers using a
- * single distribution invocation.
- * @param __b0 The upper bound for the first integer.
- * @param __b1 The upper bound for the second integer.
- * @param __g A UniformRandomBitGenerator.
- * @return A pair (i, j) with i and j uniformly distributed
- * over [0, __b0) and [0, __b1), respectively.
- *
- * Requires: __b0 * __b1 <= __g.max() - __g.min().
- *
- * Using uniform_int_distribution with a range that is very
- * small relative to the range of the generator ends up wasting
- * potentially expensively generated randomness, since
- * uniform_int_distribution does not store leftover randomness
- * between invocations.
- *
- * If we know we want two integers in ranges that are sufficiently
- * small, we can compose the ranges, use a single distribution
- * invocation, and significantly reduce the waste.
- */
- template<typename _IntType, typename _UniformRandomBitGenerator>
- pair<_IntType, _IntType>
- __gen_two_uniform_ints(_IntType __b0, _IntType __b1,
- _UniformRandomBitGenerator&& __g)
- {
- _IntType __x
- = uniform_int_distribution<_IntType>{0, (__b0 * __b1) - 1}(__g);
- return std::make_pair(__x / __b1, __x % __b1);
- }
- /**
- * @brief Shuffle the elements of a sequence using a uniform random
- * number generator.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __g A UniformRandomNumberGenerator (26.5.1.3).
- * @return Nothing.
- *
- * Reorders the elements in the range @p [__first,__last) using @p __g to
- * provide random numbers.
- */
- template<typename _RandomAccessIterator,
- typename _UniformRandomNumberGenerator>
- void
- shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
- _UniformRandomNumberGenerator&& __g)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_requires_valid_range(__first, __last);
- if (__first == __last)
- return;
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type
- _DistanceType;
- typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
- typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
- typedef typename __distr_type::param_type __p_type;
- typedef typename remove_reference<_UniformRandomNumberGenerator>::type
- _Gen;
- typedef typename common_type<typename _Gen::result_type, __ud_type>::type
- __uc_type;
- const __uc_type __urngrange = __g.max() - __g.min();
- const __uc_type __urange = __uc_type(__last - __first);
- if (__urngrange / __urange >= __urange)
- // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
- {
- _RandomAccessIterator __i = __first + 1;
- // Since we know the range isn't empty, an even number of elements
- // means an uneven number of elements /to swap/, in which case we
- // do the first one up front:
- if ((__urange % 2) == 0)
- {
- __distr_type __d{0, 1};
- std::iter_swap(__i++, __first + __d(__g));
- }
- // Now we know that __last - __i is even, so we do the rest in pairs,
- // using a single distribution invocation to produce swap positions
- // for two successive elements at a time:
- while (__i != __last)
- {
- const __uc_type __swap_range = __uc_type(__i - __first) + 1;
- const pair<__uc_type, __uc_type> __pospos =
- __gen_two_uniform_ints(__swap_range, __swap_range + 1, __g);
- std::iter_swap(__i++, __first + __pospos.first);
- std::iter_swap(__i++, __first + __pospos.second);
- }
- return;
- }
- __distr_type __d;
- for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
- std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
- }
- #endif // USE C99_STDINT
- #endif // C++11
- _GLIBCXX_BEGIN_NAMESPACE_ALGO
- /**
- * @brief Apply a function to every element of a sequence.
- * @ingroup non_mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __f A unary function object.
- * @return @p __f
- *
- * Applies the function object @p __f to each element in the range
- * @p [first,last). @p __f must not modify the order of the sequence.
- * If @p __f has a return value it is ignored.
- */
- template<typename _InputIterator, typename _Function>
- _GLIBCXX20_CONSTEXPR
- _Function
- for_each(_InputIterator __first, _InputIterator __last, _Function __f)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_requires_valid_range(__first, __last);
- for (; __first != __last; ++__first)
- __f(*__first);
- return __f; // N.B. [alg.foreach] says std::move(f) but it's redundant.
- }
- #if __cplusplus >= 201703L
- /**
- * @brief Apply a function to every element of a sequence.
- * @ingroup non_mutating_algorithms
- * @param __first An input iterator.
- * @param __n A value convertible to an integer.
- * @param __f A unary function object.
- * @return `__first+__n`
- *
- * Applies the function object `__f` to each element in the range
- * `[first, first+n)`. `__f` must not modify the order of the sequence.
- * If `__f` has a return value it is ignored.
- */
- template<typename _InputIterator, typename _Size, typename _Function>
- _GLIBCXX20_CONSTEXPR
- _InputIterator
- for_each_n(_InputIterator __first, _Size __n, _Function __f)
- {
- auto __n2 = std::__size_to_integer(__n);
- using _Cat = typename iterator_traits<_InputIterator>::iterator_category;
- if constexpr (is_base_of_v<random_access_iterator_tag, _Cat>)
- {
- if (__n2 <= 0)
- return __first;
- auto __last = __first + __n2;
- std::for_each(__first, __last, std::move(__f));
- return __last;
- }
- else
- {
- while (__n2-->0)
- {
- __f(*__first);
- ++__first;
- }
- return __first;
- }
- }
- #endif // C++17
- /**
- * @brief Find the first occurrence of a value in a sequence.
- * @ingroup non_mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __val The value to find.
- * @return The first iterator @c i in the range @p [__first,__last)
- * such that @c *i == @p __val, or @p __last if no such iterator exists.
- */
- template<typename _InputIterator, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- inline _InputIterator
- find(_InputIterator __first, _InputIterator __last,
- const _Tp& __val)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_EqualOpConcept<
- typename iterator_traits<_InputIterator>::value_type, _Tp>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__find_if(__first, __last,
- __gnu_cxx::__ops::__iter_equals_val(__val));
- }
- /**
- * @brief Find the first element in a sequence for which a
- * predicate is true.
- * @ingroup non_mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __pred A predicate.
- * @return The first iterator @c i in the range @p [__first,__last)
- * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
- */
- template<typename _InputIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- inline _InputIterator
- find_if(_InputIterator __first, _InputIterator __last,
- _Predicate __pred)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__find_if(__first, __last,
- __gnu_cxx::__ops::__pred_iter(__pred));
- }
- /**
- * @brief Find element from a set in a sequence.
- * @ingroup non_mutating_algorithms
- * @param __first1 Start of range to search.
- * @param __last1 End of range to search.
- * @param __first2 Start of match candidates.
- * @param __last2 End of match candidates.
- * @return The first iterator @c i in the range
- * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
- * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
- *
- * Searches the range @p [__first1,__last1) for an element that is
- * equal to some element in the range [__first2,__last2). If
- * found, returns an iterator in the range [__first1,__last1),
- * otherwise returns @p __last1.
- */
- template<typename _InputIterator, typename _ForwardIterator>
- _GLIBCXX20_CONSTEXPR
- _InputIterator
- find_first_of(_InputIterator __first1, _InputIterator __last1,
- _ForwardIterator __first2, _ForwardIterator __last2)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_EqualOpConcept<
- typename iterator_traits<_InputIterator>::value_type,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first1, __last1);
- __glibcxx_requires_valid_range(__first2, __last2);
- for (; __first1 != __last1; ++__first1)
- for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
- if (*__first1 == *__iter)
- return __first1;
- return __last1;
- }
- /**
- * @brief Find element from a set in a sequence using a predicate.
- * @ingroup non_mutating_algorithms
- * @param __first1 Start of range to search.
- * @param __last1 End of range to search.
- * @param __first2 Start of match candidates.
- * @param __last2 End of match candidates.
- * @param __comp Predicate to use.
- * @return The first iterator @c i in the range
- * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
- * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
- * such iterator exists.
- *
- * Searches the range @p [__first1,__last1) for an element that is
- * equal to some element in the range [__first2,__last2). If
- * found, returns an iterator in the range [__first1,__last1),
- * otherwise returns @p __last1.
- */
- template<typename _InputIterator, typename _ForwardIterator,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- _InputIterator
- find_first_of(_InputIterator __first1, _InputIterator __last1,
- _ForwardIterator __first2, _ForwardIterator __last2,
- _BinaryPredicate __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
- typename iterator_traits<_InputIterator>::value_type,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first1, __last1);
- __glibcxx_requires_valid_range(__first2, __last2);
- for (; __first1 != __last1; ++__first1)
- for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
- if (__comp(*__first1, *__iter))
- return __first1;
- return __last1;
- }
- /**
- * @brief Find two adjacent values in a sequence that are equal.
- * @ingroup non_mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @return The first iterator @c i such that @c i and @c i+1 are both
- * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
- * or @p __last if no such iterator exists.
- */
- template<typename _ForwardIterator>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_EqualityComparableConcept<
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__adjacent_find(__first, __last,
- __gnu_cxx::__ops::__iter_equal_to_iter());
- }
- /**
- * @brief Find two adjacent values in a sequence using a predicate.
- * @ingroup non_mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __binary_pred A binary predicate.
- * @return The first iterator @c i such that @c i and @c i+1 are both
- * valid iterators in @p [__first,__last) and such that
- * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
- * exists.
- */
- template<typename _ForwardIterator, typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
- _BinaryPredicate __binary_pred)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
- typename iterator_traits<_ForwardIterator>::value_type,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__adjacent_find(__first, __last,
- __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
- }
- /**
- * @brief Count the number of copies of a value in a sequence.
- * @ingroup non_mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __value The value to be counted.
- * @return The number of iterators @c i in the range @p [__first,__last)
- * for which @c *i == @p __value
- */
- template<typename _InputIterator, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- inline typename iterator_traits<_InputIterator>::difference_type
- count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_EqualOpConcept<
- typename iterator_traits<_InputIterator>::value_type, _Tp>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__count_if(__first, __last,
- __gnu_cxx::__ops::__iter_equals_val(__value));
- }
- /**
- * @brief Count the elements of a sequence for which a predicate is true.
- * @ingroup non_mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __pred A predicate.
- * @return The number of iterators @c i in the range @p [__first,__last)
- * for which @p __pred(*i) is true.
- */
- template<typename _InputIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- inline typename iterator_traits<_InputIterator>::difference_type
- count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__count_if(__first, __last,
- __gnu_cxx::__ops::__pred_iter(__pred));
- }
- /**
- * @brief Search a sequence for a matching sub-sequence.
- * @ingroup non_mutating_algorithms
- * @param __first1 A forward iterator.
- * @param __last1 A forward iterator.
- * @param __first2 A forward iterator.
- * @param __last2 A forward iterator.
- * @return The first iterator @c i in the range @p
- * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
- * *(__first2+N) for each @c N in the range @p
- * [0,__last2-__first2), or @p __last1 if no such iterator exists.
- *
- * Searches the range @p [__first1,__last1) for a sub-sequence that
- * compares equal value-by-value with the sequence given by @p
- * [__first2,__last2) and returns an iterator to the first element
- * of the sub-sequence, or @p __last1 if the sub-sequence is not
- * found.
- *
- * Because the sub-sequence must lie completely within the range @p
- * [__first1,__last1) it must start at a position less than @p
- * __last1-(__last2-__first2) where @p __last2-__first2 is the
- * length of the sub-sequence.
- *
- * This means that the returned iterator @c i will be in the range
- * @p [__first1,__last1-(__last2-__first2))
- */
- template<typename _ForwardIterator1, typename _ForwardIterator2>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator1
- search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
- __glibcxx_function_requires(_EqualOpConcept<
- typename iterator_traits<_ForwardIterator1>::value_type,
- typename iterator_traits<_ForwardIterator2>::value_type>)
- __glibcxx_requires_valid_range(__first1, __last1);
- __glibcxx_requires_valid_range(__first2, __last2);
- return std::__search(__first1, __last1, __first2, __last2,
- __gnu_cxx::__ops::__iter_equal_to_iter());
- }
- /**
- * @brief Search a sequence for a matching sub-sequence using a predicate.
- * @ingroup non_mutating_algorithms
- * @param __first1 A forward iterator.
- * @param __last1 A forward iterator.
- * @param __first2 A forward iterator.
- * @param __last2 A forward iterator.
- * @param __predicate A binary predicate.
- * @return The first iterator @c i in the range
- * @p [__first1,__last1-(__last2-__first2)) such that
- * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
- * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
- *
- * Searches the range @p [__first1,__last1) for a sub-sequence that
- * compares equal value-by-value with the sequence given by @p
- * [__first2,__last2), using @p __predicate to determine equality,
- * and returns an iterator to the first element of the
- * sub-sequence, or @p __last1 if no such iterator exists.
- *
- * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
- */
- template<typename _ForwardIterator1, typename _ForwardIterator2,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator1
- search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
- _ForwardIterator2 __first2, _ForwardIterator2 __last2,
- _BinaryPredicate __predicate)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
- typename iterator_traits<_ForwardIterator1>::value_type,
- typename iterator_traits<_ForwardIterator2>::value_type>)
- __glibcxx_requires_valid_range(__first1, __last1);
- __glibcxx_requires_valid_range(__first2, __last2);
- return std::__search(__first1, __last1, __first2, __last2,
- __gnu_cxx::__ops::__iter_comp_iter(__predicate));
- }
- /**
- * @brief Search a sequence for a number of consecutive values.
- * @ingroup non_mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __count The number of consecutive values.
- * @param __val The value to find.
- * @return The first iterator @c i in the range @p
- * [__first,__last-__count) such that @c *(i+N) == @p __val for
- * each @c N in the range @p [0,__count), or @p __last if no such
- * iterator exists.
- *
- * Searches the range @p [__first,__last) for @p count consecutive elements
- * equal to @p __val.
- */
- template<typename _ForwardIterator, typename _Integer, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- search_n(_ForwardIterator __first, _ForwardIterator __last,
- _Integer __count, const _Tp& __val)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_EqualOpConcept<
- typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__search_n(__first, __last, __count,
- __gnu_cxx::__ops::__iter_equals_val(__val));
- }
- /**
- * @brief Search a sequence for a number of consecutive values using a
- * predicate.
- * @ingroup non_mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __count The number of consecutive values.
- * @param __val The value to find.
- * @param __binary_pred A binary predicate.
- * @return The first iterator @c i in the range @p
- * [__first,__last-__count) such that @p
- * __binary_pred(*(i+N),__val) is true for each @c N in the range
- * @p [0,__count), or @p __last if no such iterator exists.
- *
- * Searches the range @p [__first,__last) for @p __count
- * consecutive elements for which the predicate returns true.
- */
- template<typename _ForwardIterator, typename _Integer, typename _Tp,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- search_n(_ForwardIterator __first, _ForwardIterator __last,
- _Integer __count, const _Tp& __val,
- _BinaryPredicate __binary_pred)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
- typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__search_n(__first, __last, __count,
- __gnu_cxx::__ops::__iter_comp_val(__binary_pred, __val));
- }
- #if __cplusplus >= 201703L
- /** @brief Search a sequence using a Searcher object.
- *
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __searcher A callable object.
- * @return @p __searcher(__first,__last).first
- */
- template<typename _ForwardIterator, typename _Searcher>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- search(_ForwardIterator __first, _ForwardIterator __last,
- const _Searcher& __searcher)
- { return __searcher(__first, __last).first; }
- #endif
- /**
- * @brief Perform an operation on a sequence.
- * @ingroup mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __result An output iterator.
- * @param __unary_op A unary operator.
- * @return An output iterator equal to @p __result+(__last-__first).
- *
- * Applies the operator to each element in the input range and assigns
- * the results to successive elements of the output sequence.
- * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
- * range @p [0,__last-__first).
- *
- * @p unary_op must not alter its argument.
- */
- template<typename _InputIterator, typename _OutputIterator,
- typename _UnaryOperation>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- transform(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result, _UnaryOperation __unary_op)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- // "the type returned by a _UnaryOperation"
- __typeof__(__unary_op(*__first))>)
- __glibcxx_requires_valid_range(__first, __last);
- for (; __first != __last; ++__first, (void)++__result)
- *__result = __unary_op(*__first);
- return __result;
- }
- /**
- * @brief Perform an operation on corresponding elements of two sequences.
- * @ingroup mutating_algorithms
- * @param __first1 An input iterator.
- * @param __last1 An input iterator.
- * @param __first2 An input iterator.
- * @param __result An output iterator.
- * @param __binary_op A binary operator.
- * @return An output iterator equal to @p result+(last-first).
- *
- * Applies the operator to the corresponding elements in the two
- * input ranges and assigns the results to successive elements of the
- * output sequence.
- * Evaluates @p
- * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
- * @c N in the range @p [0,__last1-__first1).
- *
- * @p binary_op must not alter either of its arguments.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator, typename _BinaryOperation>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- transform(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _OutputIterator __result,
- _BinaryOperation __binary_op)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- // "the type returned by a _BinaryOperation"
- __typeof__(__binary_op(*__first1,*__first2))>)
- __glibcxx_requires_valid_range(__first1, __last1);
- for (; __first1 != __last1; ++__first1, (void)++__first2, ++__result)
- *__result = __binary_op(*__first1, *__first2);
- return __result;
- }
- /**
- * @brief Replace each occurrence of one value in a sequence with another
- * value.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __old_value The value to be replaced.
- * @param __new_value The replacement value.
- * @return replace() returns no value.
- *
- * For each iterator @c i in the range @p [__first,__last) if @c *i ==
- * @p __old_value then the assignment @c *i = @p __new_value is performed.
- */
- template<typename _ForwardIterator, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- void
- replace(_ForwardIterator __first, _ForwardIterator __last,
- const _Tp& __old_value, const _Tp& __new_value)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
- _ForwardIterator>)
- __glibcxx_function_requires(_EqualOpConcept<
- typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
- __glibcxx_function_requires(_ConvertibleConcept<_Tp,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- for (; __first != __last; ++__first)
- if (*__first == __old_value)
- *__first = __new_value;
- }
- /**
- * @brief Replace each value in a sequence for which a predicate returns
- * true with another value.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __pred A predicate.
- * @param __new_value The replacement value.
- * @return replace_if() returns no value.
- *
- * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
- * is true then the assignment @c *i = @p __new_value is performed.
- */
- template<typename _ForwardIterator, typename _Predicate, typename _Tp>
- _GLIBCXX20_CONSTEXPR
- void
- replace_if(_ForwardIterator __first, _ForwardIterator __last,
- _Predicate __pred, const _Tp& __new_value)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
- _ForwardIterator>)
- __glibcxx_function_requires(_ConvertibleConcept<_Tp,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- for (; __first != __last; ++__first)
- if (__pred(*__first))
- *__first = __new_value;
- }
- /**
- * @brief Assign the result of a function object to each value in a
- * sequence.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __gen A function object taking no arguments and returning
- * std::iterator_traits<_ForwardIterator>::value_type
- * @return generate() returns no value.
- *
- * Performs the assignment @c *i = @p __gen() for each @c i in the range
- * @p [__first,__last).
- */
- template<typename _ForwardIterator, typename _Generator>
- _GLIBCXX20_CONSTEXPR
- void
- generate(_ForwardIterator __first, _ForwardIterator __last,
- _Generator __gen)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_GeneratorConcept<_Generator,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- for (; __first != __last; ++__first)
- *__first = __gen();
- }
- /**
- * @brief Assign the result of a function object to each value in a
- * sequence.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __n The length of the sequence.
- * @param __gen A function object taking no arguments and returning
- * std::iterator_traits<_ForwardIterator>::value_type
- * @return The end of the sequence, @p __first+__n
- *
- * Performs the assignment @c *i = @p __gen() for each @c i in the range
- * @p [__first,__first+__n).
- *
- * If @p __n is negative, the function does nothing and returns @p __first.
- */
- // _GLIBCXX_RESOLVE_LIB_DEFECTS
- // DR 865. More algorithms that throw away information
- // DR 426. search_n(), fill_n(), and generate_n() with negative n
- template<typename _OutputIterator, typename _Size, typename _Generator>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
- {
- // concept requirements
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- // "the type returned by a _Generator"
- __typeof__(__gen())>)
- typedef __decltype(std::__size_to_integer(__n)) _IntSize;
- for (_IntSize __niter = std::__size_to_integer(__n);
- __niter > 0; --__niter, (void) ++__first)
- *__first = __gen();
- return __first;
- }
- /**
- * @brief Copy a sequence, removing consecutive duplicate values.
- * @ingroup mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __result An output iterator.
- * @return An iterator designating the end of the resulting sequence.
- *
- * Copies each element in the range @p [__first,__last) to the range
- * beginning at @p __result, except that only the first element is copied
- * from groups of consecutive elements that compare equal.
- * unique_copy() is stable, so the relative order of elements that are
- * copied is unchanged.
- *
- * _GLIBCXX_RESOLVE_LIB_DEFECTS
- * DR 241. Does unique_copy() require CopyConstructible and Assignable?
- *
- * _GLIBCXX_RESOLVE_LIB_DEFECTS
- * DR 538. 241 again: Does unique_copy() require CopyConstructible and
- * Assignable?
- */
- template<typename _InputIterator, typename _OutputIterator>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- unique_copy(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_function_requires(_EqualityComparableConcept<
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- if (__first == __last)
- return __result;
- return std::__unique_copy(__first, __last, __result,
- __gnu_cxx::__ops::__iter_equal_to_iter(),
- std::__iterator_category(__first),
- std::__iterator_category(__result));
- }
- /**
- * @brief Copy a sequence, removing consecutive values using a predicate.
- * @ingroup mutating_algorithms
- * @param __first An input iterator.
- * @param __last An input iterator.
- * @param __result An output iterator.
- * @param __binary_pred A binary predicate.
- * @return An iterator designating the end of the resulting sequence.
- *
- * Copies each element in the range @p [__first,__last) to the range
- * beginning at @p __result, except that only the first element is copied
- * from groups of consecutive elements for which @p __binary_pred returns
- * true.
- * unique_copy() is stable, so the relative order of elements that are
- * copied is unchanged.
- *
- * _GLIBCXX_RESOLVE_LIB_DEFECTS
- * DR 241. Does unique_copy() require CopyConstructible and Assignable?
- */
- template<typename _InputIterator, typename _OutputIterator,
- typename _BinaryPredicate>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- unique_copy(_InputIterator __first, _InputIterator __last,
- _OutputIterator __result,
- _BinaryPredicate __binary_pred)
- {
- // concept requirements -- predicates checked later
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- if (__first == __last)
- return __result;
- return std::__unique_copy(__first, __last, __result,
- __gnu_cxx::__ops::__iter_comp_iter(__binary_pred),
- std::__iterator_category(__first),
- std::__iterator_category(__result));
- }
- #if __cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED
- #if _GLIBCXX_HOSTED
- /**
- * @brief Randomly shuffle the elements of a sequence.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @return Nothing.
- *
- * Reorder the elements in the range @p [__first,__last) using a random
- * distribution, so that every possible ordering of the sequence is
- * equally likely.
- *
- * @deprecated
- * Since C++14 `std::random_shuffle` is not part of the C++ standard.
- * Use `std::shuffle` instead, which was introduced in C++11.
- */
- template<typename _RandomAccessIterator>
- _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
- inline void
- random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_requires_valid_range(__first, __last);
- if (__first != __last)
- for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
- {
- // XXX rand() % N is not uniformly distributed
- _RandomAccessIterator __j = __first
- + std::rand() % ((__i - __first) + 1);
- if (__i != __j)
- std::iter_swap(__i, __j);
- }
- }
- #endif
- /**
- * @brief Shuffle the elements of a sequence using a random number
- * generator.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __rand The RNG functor or function.
- * @return Nothing.
- *
- * Reorders the elements in the range @p [__first,__last) using @p __rand to
- * provide a random distribution. Calling @p __rand(N) for a positive
- * integer @p N should return a randomly chosen integer from the
- * range [0,N).
- *
- * @deprecated
- * Since C++14 `std::random_shuffle` is not part of the C++ standard.
- * Use `std::shuffle` instead, which was introduced in C++11.
- */
- template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
- _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
- void
- random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
- #if __cplusplus >= 201103L
- _RandomNumberGenerator&& __rand)
- #else
- _RandomNumberGenerator& __rand)
- #endif
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_requires_valid_range(__first, __last);
- if (__first == __last)
- return;
- for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
- {
- _RandomAccessIterator __j = __first + __rand((__i - __first) + 1);
- if (__i != __j)
- std::iter_swap(__i, __j);
- }
- }
- #endif // C++11 || USE_DEPRECATED
- /**
- * @brief Move elements for which a predicate is true to the beginning
- * of a sequence.
- * @ingroup mutating_algorithms
- * @param __first A forward iterator.
- * @param __last A forward iterator.
- * @param __pred A predicate functor.
- * @return An iterator @p middle such that @p __pred(i) is true for each
- * iterator @p i in the range @p [__first,middle) and false for each @p i
- * in the range @p [middle,__last).
- *
- * @p __pred must not modify its operand. @p partition() does not preserve
- * the relative ordering of elements in each group, use
- * @p stable_partition() if this is needed.
- */
- template<typename _ForwardIterator, typename _Predicate>
- _GLIBCXX20_CONSTEXPR
- inline _ForwardIterator
- partition(_ForwardIterator __first, _ForwardIterator __last,
- _Predicate __pred)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
- _ForwardIterator>)
- __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- return std::__partition(__first, __last, __pred,
- std::__iterator_category(__first));
- }
- /**
- * @brief Sort the smallest elements of a sequence.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __middle Another iterator.
- * @param __last Another iterator.
- * @return Nothing.
- *
- * Sorts the smallest @p (__middle-__first) elements in the range
- * @p [first,last) and moves them to the range @p [__first,__middle). The
- * order of the remaining elements in the range @p [__middle,__last) is
- * undefined.
- * After the sort if @e i and @e j are iterators in the range
- * @p [__first,__middle) such that i precedes j and @e k is an iterator in
- * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
- */
- template<typename _RandomAccessIterator>
- _GLIBCXX20_CONSTEXPR
- inline void
- partial_sort(_RandomAccessIterator __first,
- _RandomAccessIterator __middle,
- _RandomAccessIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_function_requires(_LessThanComparableConcept<
- typename iterator_traits<_RandomAccessIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __middle);
- __glibcxx_requires_valid_range(__middle, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- std::__partial_sort(__first, __middle, __last,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Sort the smallest elements of a sequence using a predicate
- * for comparison.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __middle Another iterator.
- * @param __last Another iterator.
- * @param __comp A comparison functor.
- * @return Nothing.
- *
- * Sorts the smallest @p (__middle-__first) elements in the range
- * @p [__first,__last) and moves them to the range @p [__first,__middle). The
- * order of the remaining elements in the range @p [__middle,__last) is
- * undefined.
- * After the sort if @e i and @e j are iterators in the range
- * @p [__first,__middle) such that i precedes j and @e k is an iterator in
- * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
- * are both false.
- */
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline void
- partial_sort(_RandomAccessIterator __first,
- _RandomAccessIterator __middle,
- _RandomAccessIterator __last,
- _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_RandomAccessIterator>::value_type,
- typename iterator_traits<_RandomAccessIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __middle);
- __glibcxx_requires_valid_range(__middle, __last);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- std::__partial_sort(__first, __middle, __last,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- /**
- * @brief Sort a sequence just enough to find a particular position.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __nth Another iterator.
- * @param __last Another iterator.
- * @return Nothing.
- *
- * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
- * is the same element that would have been in that position had the
- * whole sequence been sorted. The elements either side of @p *__nth are
- * not completely sorted, but for any iterator @e i in the range
- * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
- * holds that *j < *i is false.
- */
- template<typename _RandomAccessIterator>
- _GLIBCXX20_CONSTEXPR
- inline void
- nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
- _RandomAccessIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_function_requires(_LessThanComparableConcept<
- typename iterator_traits<_RandomAccessIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __nth);
- __glibcxx_requires_valid_range(__nth, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- if (__first == __last || __nth == __last)
- return;
- std::__introselect(__first, __nth, __last,
- std::__lg(__last - __first) * 2,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Sort a sequence just enough to find a particular position
- * using a predicate for comparison.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __nth Another iterator.
- * @param __last Another iterator.
- * @param __comp A comparison functor.
- * @return Nothing.
- *
- * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
- * is the same element that would have been in that position had the
- * whole sequence been sorted. The elements either side of @p *__nth are
- * not completely sorted, but for any iterator @e i in the range
- * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
- * holds that @p __comp(*j,*i) is false.
- */
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline void
- nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
- _RandomAccessIterator __last, _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_RandomAccessIterator>::value_type,
- typename iterator_traits<_RandomAccessIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __nth);
- __glibcxx_requires_valid_range(__nth, __last);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- if (__first == __last || __nth == __last)
- return;
- std::__introselect(__first, __nth, __last,
- std::__lg(__last - __first) * 2,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- /**
- * @brief Sort the elements of a sequence.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @return Nothing.
- *
- * Sorts the elements in the range @p [__first,__last) in ascending order,
- * such that for each iterator @e i in the range @p [__first,__last-1),
- * *(i+1)<*i is false.
- *
- * The relative ordering of equivalent elements is not preserved, use
- * @p stable_sort() if this is needed.
- */
- template<typename _RandomAccessIterator>
- _GLIBCXX20_CONSTEXPR
- inline void
- sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_function_requires(_LessThanComparableConcept<
- typename iterator_traits<_RandomAccessIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Sort the elements of a sequence using a predicate for comparison.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __comp A comparison functor.
- * @return Nothing.
- *
- * Sorts the elements in the range @p [__first,__last) in ascending order,
- * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
- * range @p [__first,__last-1).
- *
- * The relative ordering of equivalent elements is not preserved, use
- * @p stable_sort() if this is needed.
- */
- template<typename _RandomAccessIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline void
- sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
- _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_RandomAccessIterator>::value_type,
- typename iterator_traits<_RandomAccessIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- __merge(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1 && __first2 != __last2)
- {
- if (__comp(__first2, __first1))
- {
- *__result = *__first2;
- ++__first2;
- }
- else
- {
- *__result = *__first1;
- ++__first1;
- }
- ++__result;
- }
- return std::copy(__first2, __last2,
- std::copy(__first1, __last1, __result));
- }
- /**
- * @brief Merges two sorted ranges.
- * @ingroup sorting_algorithms
- * @param __first1 An iterator.
- * @param __first2 Another iterator.
- * @param __last1 Another iterator.
- * @param __last2 Another iterator.
- * @param __result An iterator pointing to the end of the merged range.
- * @return An output iterator equal to @p __result + (__last1 - __first1)
- * + (__last2 - __first2).
- *
- * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
- * the sorted range @p [__result, __result + (__last1-__first1) +
- * (__last2-__first2)). Both input ranges must be sorted, and the
- * output range must not overlap with either of the input ranges.
- * The sort is @e stable, that is, for equivalent elements in the
- * two ranges, elements from the first range will always come
- * before elements from the second.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- merge(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set(__first1, __last1, __first2);
- __glibcxx_requires_sorted_set(__first2, __last2, __first1);
- __glibcxx_requires_irreflexive2(__first1, __last1);
- __glibcxx_requires_irreflexive2(__first2, __last2);
- return _GLIBCXX_STD_A::__merge(__first1, __last1,
- __first2, __last2, __result,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Merges two sorted ranges.
- * @ingroup sorting_algorithms
- * @param __first1 An iterator.
- * @param __first2 Another iterator.
- * @param __last1 Another iterator.
- * @param __last2 Another iterator.
- * @param __result An iterator pointing to the end of the merged range.
- * @param __comp A functor to use for comparisons.
- * @return An output iterator equal to @p __result + (__last1 - __first1)
- * + (__last2 - __first2).
- *
- * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
- * the sorted range @p [__result, __result + (__last1-__first1) +
- * (__last2-__first2)). Both input ranges must be sorted, and the
- * output range must not overlap with either of the input ranges.
- * The sort is @e stable, that is, for equivalent elements in the
- * two ranges, elements from the first range will always come
- * before elements from the second.
- *
- * The comparison function should have the same effects on ordering as
- * the function used for the initial sort.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- merge(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
- __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
- return _GLIBCXX_STD_A::__merge(__first1, __last1,
- __first2, __last2, __result,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- template<typename _RandomAccessIterator, typename _Compare>
- inline void
- __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
- _Compare __comp)
- {
- typedef typename iterator_traits<_RandomAccessIterator>::value_type
- _ValueType;
- typedef typename iterator_traits<_RandomAccessIterator>::difference_type
- _DistanceType;
- typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf;
- if (__first == __last)
- return;
- // __stable_sort_adaptive sorts the range in two halves,
- // so the buffer only needs to fit half the range at once.
- _TmpBuf __buf(__first, (__last - __first + 1) / 2);
- if (__buf.begin() == 0)
- std::__inplace_stable_sort(__first, __last, __comp);
- else
- std::__stable_sort_adaptive(__first, __last, __buf.begin(),
- _DistanceType(__buf.size()), __comp);
- }
- /**
- * @brief Sort the elements of a sequence, preserving the relative order
- * of equivalent elements.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @return Nothing.
- *
- * Sorts the elements in the range @p [__first,__last) in ascending order,
- * such that for each iterator @p i in the range @p [__first,__last-1),
- * @p *(i+1)<*i is false.
- *
- * The relative ordering of equivalent elements is preserved, so any two
- * elements @p x and @p y in the range @p [__first,__last) such that
- * @p x<y is false and @p y<x is false will have the same relative
- * ordering after calling @p stable_sort().
- */
- template<typename _RandomAccessIterator>
- inline void
- stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_function_requires(_LessThanComparableConcept<
- typename iterator_traits<_RandomAccessIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- _GLIBCXX_STD_A::__stable_sort(__first, __last,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Sort the elements of a sequence using a predicate for comparison,
- * preserving the relative order of equivalent elements.
- * @ingroup sorting_algorithms
- * @param __first An iterator.
- * @param __last Another iterator.
- * @param __comp A comparison functor.
- * @return Nothing.
- *
- * Sorts the elements in the range @p [__first,__last) in ascending order,
- * such that for each iterator @p i in the range @p [__first,__last-1),
- * @p __comp(*(i+1),*i) is false.
- *
- * The relative ordering of equivalent elements is preserved, so any two
- * elements @p x and @p y in the range @p [__first,__last) such that
- * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
- * relative ordering after calling @p stable_sort().
- */
- template<typename _RandomAccessIterator, typename _Compare>
- inline void
- stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
- _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
- _RandomAccessIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_RandomAccessIterator>::value_type,
- typename iterator_traits<_RandomAccessIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- _GLIBCXX_STD_A::__stable_sort(__first, __last,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator,
- typename _Compare>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1 && __first2 != __last2)
- {
- if (__comp(__first1, __first2))
- {
- *__result = *__first1;
- ++__first1;
- }
- else if (__comp(__first2, __first1))
- {
- *__result = *__first2;
- ++__first2;
- }
- else
- {
- *__result = *__first1;
- ++__first1;
- ++__first2;
- }
- ++__result;
- }
- return std::copy(__first2, __last2,
- std::copy(__first1, __last1, __result));
- }
- /**
- * @brief Return the union of two sorted ranges.
- * @ingroup set_algorithms
- * @param __first1 Start of first range.
- * @param __last1 End of first range.
- * @param __first2 Start of second range.
- * @param __last2 End of second range.
- * @param __result Start of output range.
- * @return End of the output range.
- * @ingroup set_algorithms
- *
- * This operation iterates over both ranges, copying elements present in
- * each range in order to the output range. Iterators increment for each
- * range. When the current element of one range is less than the other,
- * that element is copied and the iterator advanced. If an element is
- * contained in both ranges, the element from the first range is copied and
- * both ranges advance. The output range may not overlap either input
- * range.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- set_union(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set(__first1, __last1, __first2);
- __glibcxx_requires_sorted_set(__first2, __last2, __first1);
- __glibcxx_requires_irreflexive2(__first1, __last1);
- __glibcxx_requires_irreflexive2(__first2, __last2);
- return _GLIBCXX_STD_A::__set_union(__first1, __last1,
- __first2, __last2, __result,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Return the union of two sorted ranges using a comparison functor.
- * @ingroup set_algorithms
- * @param __first1 Start of first range.
- * @param __last1 End of first range.
- * @param __first2 Start of second range.
- * @param __last2 End of second range.
- * @param __result Start of output range.
- * @param __comp The comparison functor.
- * @return End of the output range.
- * @ingroup set_algorithms
- *
- * This operation iterates over both ranges, copying elements present in
- * each range in order to the output range. Iterators increment for each
- * range. When the current element of one range is less than the other
- * according to @p __comp, that element is copied and the iterator advanced.
- * If an equivalent element according to @p __comp is contained in both
- * ranges, the element from the first range is copied and both ranges
- * advance. The output range may not overlap either input range.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- set_union(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
- __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
- return _GLIBCXX_STD_A::__set_union(__first1, __last1,
- __first2, __last2, __result,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator,
- typename _Compare>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1 && __first2 != __last2)
- if (__comp(__first1, __first2))
- ++__first1;
- else if (__comp(__first2, __first1))
- ++__first2;
- else
- {
- *__result = *__first1;
- ++__first1;
- ++__first2;
- ++__result;
- }
- return __result;
- }
- /**
- * @brief Return the intersection of two sorted ranges.
- * @ingroup set_algorithms
- * @param __first1 Start of first range.
- * @param __last1 End of first range.
- * @param __first2 Start of second range.
- * @param __last2 End of second range.
- * @param __result Start of output range.
- * @return End of the output range.
- * @ingroup set_algorithms
- *
- * This operation iterates over both ranges, copying elements present in
- * both ranges in order to the output range. Iterators increment for each
- * range. When the current element of one range is less than the other,
- * that iterator advances. If an element is contained in both ranges, the
- * element from the first range is copied and both ranges advance. The
- * output range may not overlap either input range.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set(__first1, __last1, __first2);
- __glibcxx_requires_sorted_set(__first2, __last2, __first1);
- __glibcxx_requires_irreflexive2(__first1, __last1);
- __glibcxx_requires_irreflexive2(__first2, __last2);
- return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
- __first2, __last2, __result,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Return the intersection of two sorted ranges using comparison
- * functor.
- * @ingroup set_algorithms
- * @param __first1 Start of first range.
- * @param __last1 End of first range.
- * @param __first2 Start of second range.
- * @param __last2 End of second range.
- * @param __result Start of output range.
- * @param __comp The comparison functor.
- * @return End of the output range.
- * @ingroup set_algorithms
- *
- * This operation iterates over both ranges, copying elements present in
- * both ranges in order to the output range. Iterators increment for each
- * range. When the current element of one range is less than the other
- * according to @p __comp, that iterator advances. If an element is
- * contained in both ranges according to @p __comp, the element from the
- * first range is copied and both ranges advance. The output range may not
- * overlap either input range.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
- __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
- return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
- __first2, __last2, __result,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator,
- typename _Compare>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- while (__first1 != __last1 && __first2 != __last2)
- if (__comp(__first1, __first2))
- {
- *__result = *__first1;
- ++__first1;
- ++__result;
- }
- else if (__comp(__first2, __first1))
- ++__first2;
- else
- {
- ++__first1;
- ++__first2;
- }
- return std::copy(__first1, __last1, __result);
- }
- /**
- * @brief Return the difference of two sorted ranges.
- * @ingroup set_algorithms
- * @param __first1 Start of first range.
- * @param __last1 End of first range.
- * @param __first2 Start of second range.
- * @param __last2 End of second range.
- * @param __result Start of output range.
- * @return End of the output range.
- * @ingroup set_algorithms
- *
- * This operation iterates over both ranges, copying elements present in
- * the first range but not the second in order to the output range.
- * Iterators increment for each range. When the current element of the
- * first range is less than the second, that element is copied and the
- * iterator advances. If the current element of the second range is less,
- * the iterator advances, but no element is copied. If an element is
- * contained in both ranges, no elements are copied and both ranges
- * advance. The output range may not overlap either input range.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set(__first1, __last1, __first2);
- __glibcxx_requires_sorted_set(__first2, __last2, __first1);
- __glibcxx_requires_irreflexive2(__first1, __last1);
- __glibcxx_requires_irreflexive2(__first2, __last2);
- return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
- __first2, __last2, __result,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Return the difference of two sorted ranges using comparison
- * functor.
- * @ingroup set_algorithms
- * @param __first1 Start of first range.
- * @param __last1 End of first range.
- * @param __first2 Start of second range.
- * @param __last2 End of second range.
- * @param __result Start of output range.
- * @param __comp The comparison functor.
- * @return End of the output range.
- * @ingroup set_algorithms
- *
- * This operation iterates over both ranges, copying elements present in
- * the first range but not the second in order to the output range.
- * Iterators increment for each range. When the current element of the
- * first range is less than the second according to @p __comp, that element
- * is copied and the iterator advances. If the current element of the
- * second range is less, no element is copied and the iterator advances.
- * If an element is contained in both ranges according to @p __comp, no
- * elements are copied and both ranges advance. The output range may not
- * overlap either input range.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result, _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
- __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
- return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
- __first2, __last2, __result,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator,
- typename _Compare>
- _GLIBCXX20_CONSTEXPR
- _OutputIterator
- __set_symmetric_difference(_InputIterator1 __first1,
- _InputIterator1 __last1,
- _InputIterator2 __first2,
- _InputIterator2 __last2,
- _OutputIterator __result,
- _Compare __comp)
- {
- while (__first1 != __last1 && __first2 != __last2)
- if (__comp(__first1, __first2))
- {
- *__result = *__first1;
- ++__first1;
- ++__result;
- }
- else if (__comp(__first2, __first1))
- {
- *__result = *__first2;
- ++__first2;
- ++__result;
- }
- else
- {
- ++__first1;
- ++__first2;
- }
- return std::copy(__first2, __last2,
- std::copy(__first1, __last1, __result));
- }
- /**
- * @brief Return the symmetric difference of two sorted ranges.
- * @ingroup set_algorithms
- * @param __first1 Start of first range.
- * @param __last1 End of first range.
- * @param __first2 Start of second range.
- * @param __last2 End of second range.
- * @param __result Start of output range.
- * @return End of the output range.
- * @ingroup set_algorithms
- *
- * This operation iterates over both ranges, copying elements present in
- * one range but not the other in order to the output range. Iterators
- * increment for each range. When the current element of one range is less
- * than the other, that element is copied and the iterator advances. If an
- * element is contained in both ranges, no elements are copied and both
- * ranges advance. The output range may not overlap either input range.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_LessThanOpConcept<
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set(__first1, __last1, __first2);
- __glibcxx_requires_sorted_set(__first2, __last2, __first1);
- __glibcxx_requires_irreflexive2(__first1, __last1);
- __glibcxx_requires_irreflexive2(__first2, __last2);
- return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
- __first2, __last2, __result,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Return the symmetric difference of two sorted ranges using
- * comparison functor.
- * @ingroup set_algorithms
- * @param __first1 Start of first range.
- * @param __last1 End of first range.
- * @param __first2 Start of second range.
- * @param __last2 End of second range.
- * @param __result Start of output range.
- * @param __comp The comparison functor.
- * @return End of the output range.
- * @ingroup set_algorithms
- *
- * This operation iterates over both ranges, copying elements present in
- * one range but not the other in order to the output range. Iterators
- * increment for each range. When the current element of one range is less
- * than the other according to @p comp, that element is copied and the
- * iterator advances. If an element is contained in both ranges according
- * to @p __comp, no elements are copied and both ranges advance. The output
- * range may not overlap either input range.
- */
- template<typename _InputIterator1, typename _InputIterator2,
- typename _OutputIterator, typename _Compare>
- _GLIBCXX20_CONSTEXPR
- inline _OutputIterator
- set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
- _InputIterator2 __first2, _InputIterator2 __last2,
- _OutputIterator __result,
- _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
- __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_InputIterator1>::value_type,
- typename iterator_traits<_InputIterator2>::value_type>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_InputIterator2>::value_type,
- typename iterator_traits<_InputIterator1>::value_type>)
- __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
- __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
- __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
- return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
- __first2, __last2, __result,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- template<typename _ForwardIterator, typename _Compare>
- _GLIBCXX14_CONSTEXPR
- _ForwardIterator
- __min_element(_ForwardIterator __first, _ForwardIterator __last,
- _Compare __comp)
- {
- if (__first == __last)
- return __first;
- _ForwardIterator __result = __first;
- while (++__first != __last)
- if (__comp(__first, __result))
- __result = __first;
- return __result;
- }
- /**
- * @brief Return the minimum element in a range.
- * @ingroup sorting_algorithms
- * @param __first Start of range.
- * @param __last End of range.
- * @return Iterator referencing the first instance of the smallest value.
- */
- template<typename _ForwardIterator>
- _GLIBCXX14_CONSTEXPR
- _ForwardIterator
- inline min_element(_ForwardIterator __first, _ForwardIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_LessThanComparableConcept<
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- return _GLIBCXX_STD_A::__min_element(__first, __last,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Return the minimum element in a range using comparison functor.
- * @ingroup sorting_algorithms
- * @param __first Start of range.
- * @param __last End of range.
- * @param __comp Comparison functor.
- * @return Iterator referencing the first instance of the smallest value
- * according to __comp.
- */
- template<typename _ForwardIterator, typename _Compare>
- _GLIBCXX14_CONSTEXPR
- inline _ForwardIterator
- min_element(_ForwardIterator __first, _ForwardIterator __last,
- _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_ForwardIterator>::value_type,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- return _GLIBCXX_STD_A::__min_element(__first, __last,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- template<typename _ForwardIterator, typename _Compare>
- _GLIBCXX14_CONSTEXPR
- _ForwardIterator
- __max_element(_ForwardIterator __first, _ForwardIterator __last,
- _Compare __comp)
- {
- if (__first == __last) return __first;
- _ForwardIterator __result = __first;
- while (++__first != __last)
- if (__comp(__result, __first))
- __result = __first;
- return __result;
- }
- /**
- * @brief Return the maximum element in a range.
- * @ingroup sorting_algorithms
- * @param __first Start of range.
- * @param __last End of range.
- * @return Iterator referencing the first instance of the largest value.
- */
- template<typename _ForwardIterator>
- _GLIBCXX14_CONSTEXPR
- inline _ForwardIterator
- max_element(_ForwardIterator __first, _ForwardIterator __last)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_LessThanComparableConcept<
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive(__first, __last);
- return _GLIBCXX_STD_A::__max_element(__first, __last,
- __gnu_cxx::__ops::__iter_less_iter());
- }
- /**
- * @brief Return the maximum element in a range using comparison functor.
- * @ingroup sorting_algorithms
- * @param __first Start of range.
- * @param __last End of range.
- * @param __comp Comparison functor.
- * @return Iterator referencing the first instance of the largest value
- * according to __comp.
- */
- template<typename _ForwardIterator, typename _Compare>
- _GLIBCXX14_CONSTEXPR
- inline _ForwardIterator
- max_element(_ForwardIterator __first, _ForwardIterator __last,
- _Compare __comp)
- {
- // concept requirements
- __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
- __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
- typename iterator_traits<_ForwardIterator>::value_type,
- typename iterator_traits<_ForwardIterator>::value_type>)
- __glibcxx_requires_valid_range(__first, __last);
- __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
- return _GLIBCXX_STD_A::__max_element(__first, __last,
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- #if __cplusplus >= 201103L
- // N2722 + DR 915.
- template<typename _Tp>
- _GLIBCXX14_CONSTEXPR
- inline _Tp
- min(initializer_list<_Tp> __l)
- {
- __glibcxx_requires_irreflexive(__l.begin(), __l.end());
- return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(),
- __gnu_cxx::__ops::__iter_less_iter());
- }
- template<typename _Tp, typename _Compare>
- _GLIBCXX14_CONSTEXPR
- inline _Tp
- min(initializer_list<_Tp> __l, _Compare __comp)
- {
- __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp);
- return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(),
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- template<typename _Tp>
- _GLIBCXX14_CONSTEXPR
- inline _Tp
- max(initializer_list<_Tp> __l)
- {
- __glibcxx_requires_irreflexive(__l.begin(), __l.end());
- return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(),
- __gnu_cxx::__ops::__iter_less_iter());
- }
- template<typename _Tp, typename _Compare>
- _GLIBCXX14_CONSTEXPR
- inline _Tp
- max(initializer_list<_Tp> __l, _Compare __comp)
- {
- __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp);
- return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(),
- __gnu_cxx::__ops::__iter_comp_iter(__comp));
- }
- #endif // C++11
- #if __cplusplus >= 201402L
- /// Reservoir sampling algorithm.
- template<typename _InputIterator, typename _RandomAccessIterator,
- typename _Size, typename _UniformRandomBitGenerator>
- _RandomAccessIterator
- __sample(_InputIterator __first, _InputIterator __last, input_iterator_tag,
- _RandomAccessIterator __out, random_access_iterator_tag,
- _Size __n, _UniformRandomBitGenerator&& __g)
- {
- using __distrib_type = uniform_int_distribution<_Size>;
- using __param_type = typename __distrib_type::param_type;
- __distrib_type __d{};
- _Size __sample_sz = 0;
- while (__first != __last && __sample_sz != __n)
- {
- __out[__sample_sz++] = *__first;
- ++__first;
- }
- for (auto __pop_sz = __sample_sz; __first != __last;
- ++__first, (void) ++__pop_sz)
- {
- const auto __k = __d(__g, __param_type{0, __pop_sz});
- if (__k < __n)
- __out[__k] = *__first;
- }
- return __out + __sample_sz;
- }
- /// Selection sampling algorithm.
- template<typename _ForwardIterator, typename _OutputIterator, typename _Cat,
- typename _Size, typename _UniformRandomBitGenerator>
- _OutputIterator
- __sample(_ForwardIterator __first, _ForwardIterator __last,
- forward_iterator_tag,
- _OutputIterator __out, _Cat,
- _Size __n, _UniformRandomBitGenerator&& __g)
- {
- using __distrib_type = uniform_int_distribution<_Size>;
- using __param_type = typename __distrib_type::param_type;
- using _USize = make_unsigned_t<_Size>;
- using _Gen = remove_reference_t<_UniformRandomBitGenerator>;
- using __uc_type = common_type_t<typename _Gen::result_type, _USize>;
- if (__first == __last)
- return __out;
- __distrib_type __d{};
- _Size __unsampled_sz = std::distance(__first, __last);
- __n = std::min(__n, __unsampled_sz);
- // If possible, we use __gen_two_uniform_ints to efficiently produce
- // two random numbers using a single distribution invocation:
- const __uc_type __urngrange = __g.max() - __g.min();
- if (__urngrange / __uc_type(__unsampled_sz) >= __uc_type(__unsampled_sz))
- // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
- // wrapping issues.
- {
- while (__n != 0 && __unsampled_sz >= 2)
- {
- const pair<_Size, _Size> __p =
- __gen_two_uniform_ints(__unsampled_sz, __unsampled_sz - 1, __g);
- --__unsampled_sz;
- if (__p.first < __n)
- {
- *__out++ = *__first;
- --__n;
- }
- ++__first;
- if (__n == 0) break;
- --__unsampled_sz;
- if (__p.second < __n)
- {
- *__out++ = *__first;
- --__n;
- }
- ++__first;
- }
- }
- // The loop above is otherwise equivalent to this one-at-a-time version:
- for (; __n != 0; ++__first)
- if (__d(__g, __param_type{0, --__unsampled_sz}) < __n)
- {
- *__out++ = *__first;
- --__n;
- }
- return __out;
- }
- #if __cplusplus > 201402L
- #define __cpp_lib_sample 201603L
- /// Take a random sample from a population.
- template<typename _PopulationIterator, typename _SampleIterator,
- typename _Distance, typename _UniformRandomBitGenerator>
- _SampleIterator
- sample(_PopulationIterator __first, _PopulationIterator __last,
- _SampleIterator __out, _Distance __n,
- _UniformRandomBitGenerator&& __g)
- {
- using __pop_cat = typename
- std::iterator_traits<_PopulationIterator>::iterator_category;
- using __samp_cat = typename
- std::iterator_traits<_SampleIterator>::iterator_category;
- static_assert(
- __or_<is_convertible<__pop_cat, forward_iterator_tag>,
- is_convertible<__samp_cat, random_access_iterator_tag>>::value,
- "output range must use a RandomAccessIterator when input range"
- " does not meet the ForwardIterator requirements");
- static_assert(is_integral<_Distance>::value,
- "sample size must be an integer type");
- typename iterator_traits<_PopulationIterator>::difference_type __d = __n;
- return _GLIBCXX_STD_A::
- __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d,
- std::forward<_UniformRandomBitGenerator>(__g));
- }
- #endif // C++17
- #endif // C++14
- _GLIBCXX_END_NAMESPACE_ALGO
- _GLIBCXX_END_NAMESPACE_VERSION
- } // namespace std
- #endif /* _STL_ALGO_H */
|