algorithm 180 KB

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