algorithm 200 KB

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