algorithm 198 KB

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