stl_algo.h 209 KB

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