algorithm 192 KB

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