algorithm 180 KB

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