algorithm 203 KB

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