algorithm 197 KB

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