random 221 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729
  1. // -*- C++ -*-
  2. //===--------------------------- random -----------------------------------===//
  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_RANDOM
  11. #define _LIBCPP_RANDOM
  12. /*
  13. random synopsis
  14. #include <initializer_list>
  15. namespace std
  16. {
  17. // Engines
  18. template <class UIntType, UIntType a, UIntType c, UIntType m>
  19. class linear_congruential_engine
  20. {
  21. public:
  22. // types
  23. typedef UIntType result_type;
  24. // engine characteristics
  25. static constexpr result_type multiplier = a;
  26. static constexpr result_type increment = c;
  27. static constexpr result_type modulus = m;
  28. static constexpr result_type min() { return c == 0u ? 1u: 0u;}
  29. static constexpr result_type max() { return m - 1u;}
  30. static constexpr result_type default_seed = 1u;
  31. // constructors and seeding functions
  32. explicit linear_congruential_engine(result_type s = default_seed);
  33. template<class Sseq> explicit linear_congruential_engine(Sseq& q);
  34. void seed(result_type s = default_seed);
  35. template<class Sseq> void seed(Sseq& q);
  36. // generating functions
  37. result_type operator()();
  38. void discard(unsigned long long z);
  39. };
  40. template <class UIntType, UIntType a, UIntType c, UIntType m>
  41. bool
  42. operator==(const linear_congruential_engine<UIntType, a, c, m>& x,
  43. const linear_congruential_engine<UIntType, a, c, m>& y);
  44. template <class UIntType, UIntType a, UIntType c, UIntType m>
  45. bool
  46. operator!=(const linear_congruential_engine<UIntType, a, c, m>& x,
  47. const linear_congruential_engine<UIntType, a, c, m>& y);
  48. template <class charT, class traits,
  49. class UIntType, UIntType a, UIntType c, UIntType m>
  50. basic_ostream<charT, traits>&
  51. operator<<(basic_ostream<charT, traits>& os,
  52. const linear_congruential_engine<UIntType, a, c, m>& x);
  53. template <class charT, class traits,
  54. class UIntType, UIntType a, UIntType c, UIntType m>
  55. basic_istream<charT, traits>&
  56. operator>>(basic_istream<charT, traits>& is,
  57. linear_congruential_engine<UIntType, a, c, m>& x);
  58. template <class UIntType, size_t w, size_t n, size_t m, size_t r,
  59. UIntType a, size_t u, UIntType d, size_t s,
  60. UIntType b, size_t t, UIntType c, size_t l, UIntType f>
  61. class mersenne_twister_engine
  62. {
  63. public:
  64. // types
  65. typedef UIntType result_type;
  66. // engine characteristics
  67. static constexpr size_t word_size = w;
  68. static constexpr size_t state_size = n;
  69. static constexpr size_t shift_size = m;
  70. static constexpr size_t mask_bits = r;
  71. static constexpr result_type xor_mask = a;
  72. static constexpr size_t tempering_u = u;
  73. static constexpr result_type tempering_d = d;
  74. static constexpr size_t tempering_s = s;
  75. static constexpr result_type tempering_b = b;
  76. static constexpr size_t tempering_t = t;
  77. static constexpr result_type tempering_c = c;
  78. static constexpr size_t tempering_l = l;
  79. static constexpr result_type initialization_multiplier = f;
  80. static constexpr result_type min () { return 0; }
  81. static constexpr result_type max() { return 2^w - 1; }
  82. static constexpr result_type default_seed = 5489u;
  83. // constructors and seeding functions
  84. explicit mersenne_twister_engine(result_type value = default_seed);
  85. template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
  86. void seed(result_type value = default_seed);
  87. template<class Sseq> void seed(Sseq& q);
  88. // generating functions
  89. result_type operator()();
  90. void discard(unsigned long long z);
  91. };
  92. template <class UIntType, size_t w, size_t n, size_t m, size_t r,
  93. UIntType a, size_t u, UIntType d, size_t s,
  94. UIntType b, size_t t, UIntType c, size_t l, UIntType f>
  95. bool
  96. operator==(
  97. const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
  98. const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
  99. template <class UIntType, size_t w, size_t n, size_t m, size_t r,
  100. UIntType a, size_t u, UIntType d, size_t s,
  101. UIntType b, size_t t, UIntType c, size_t l, UIntType f>
  102. bool
  103. operator!=(
  104. const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
  105. const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
  106. template <class charT, class traits,
  107. class UIntType, size_t w, size_t n, size_t m, size_t r,
  108. UIntType a, size_t u, UIntType d, size_t s,
  109. UIntType b, size_t t, UIntType c, size_t l, UIntType f>
  110. basic_ostream<charT, traits>&
  111. operator<<(basic_ostream<charT, traits>& os,
  112. const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
  113. template <class charT, class traits,
  114. class UIntType, size_t w, size_t n, size_t m, size_t r,
  115. UIntType a, size_t u, UIntType d, size_t s,
  116. UIntType b, size_t t, UIntType c, size_t l, UIntType f>
  117. basic_istream<charT, traits>&
  118. operator>>(basic_istream<charT, traits>& is,
  119. mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
  120. template<class UIntType, size_t w, size_t s, size_t r>
  121. class subtract_with_carry_engine
  122. {
  123. public:
  124. // types
  125. typedef UIntType result_type;
  126. // engine characteristics
  127. static constexpr size_t word_size = w;
  128. static constexpr size_t short_lag = s;
  129. static constexpr size_t long_lag = r;
  130. static constexpr result_type min() { return 0; }
  131. static constexpr result_type max() { return m-1; }
  132. static constexpr result_type default_seed = 19780503u;
  133. // constructors and seeding functions
  134. explicit subtract_with_carry_engine(result_type value = default_seed);
  135. template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
  136. void seed(result_type value = default_seed);
  137. template<class Sseq> void seed(Sseq& q);
  138. // generating functions
  139. result_type operator()();
  140. void discard(unsigned long long z);
  141. };
  142. template<class UIntType, size_t w, size_t s, size_t r>
  143. bool
  144. operator==(
  145. const subtract_with_carry_engine<UIntType, w, s, r>& x,
  146. const subtract_with_carry_engine<UIntType, w, s, r>& y);
  147. template<class UIntType, size_t w, size_t s, size_t r>
  148. bool
  149. operator!=(
  150. const subtract_with_carry_engine<UIntType, w, s, r>& x,
  151. const subtract_with_carry_engine<UIntType, w, s, r>& y);
  152. template <class charT, class traits,
  153. class UIntType, size_t w, size_t s, size_t r>
  154. basic_ostream<charT, traits>&
  155. operator<<(basic_ostream<charT, traits>& os,
  156. const subtract_with_carry_engine<UIntType, w, s, r>& x);
  157. template <class charT, class traits,
  158. class UIntType, size_t w, size_t s, size_t r>
  159. basic_istream<charT, traits>&
  160. operator>>(basic_istream<charT, traits>& is,
  161. subtract_with_carry_engine<UIntType, w, s, r>& x);
  162. template<class Engine, size_t p, size_t r>
  163. class discard_block_engine
  164. {
  165. public:
  166. // types
  167. typedef typename Engine::result_type result_type;
  168. // engine characteristics
  169. static constexpr size_t block_size = p;
  170. static constexpr size_t used_block = r;
  171. static constexpr result_type min() { return Engine::min(); }
  172. static constexpr result_type max() { return Engine::max(); }
  173. // constructors and seeding functions
  174. discard_block_engine();
  175. explicit discard_block_engine(const Engine& e);
  176. explicit discard_block_engine(Engine&& e);
  177. explicit discard_block_engine(result_type s);
  178. template<class Sseq> explicit discard_block_engine(Sseq& q);
  179. void seed();
  180. void seed(result_type s);
  181. template<class Sseq> void seed(Sseq& q);
  182. // generating functions
  183. result_type operator()();
  184. void discard(unsigned long long z);
  185. // property functions
  186. const Engine& base() const noexcept;
  187. };
  188. template<class Engine, size_t p, size_t r>
  189. bool
  190. operator==(
  191. const discard_block_engine<Engine, p, r>& x,
  192. const discard_block_engine<Engine, p, r>& y);
  193. template<class Engine, size_t p, size_t r>
  194. bool
  195. operator!=(
  196. const discard_block_engine<Engine, p, r>& x,
  197. const discard_block_engine<Engine, p, r>& y);
  198. template <class charT, class traits,
  199. class Engine, size_t p, size_t r>
  200. basic_ostream<charT, traits>&
  201. operator<<(basic_ostream<charT, traits>& os,
  202. const discard_block_engine<Engine, p, r>& x);
  203. template <class charT, class traits,
  204. class Engine, size_t p, size_t r>
  205. basic_istream<charT, traits>&
  206. operator>>(basic_istream<charT, traits>& is,
  207. discard_block_engine<Engine, p, r>& x);
  208. template<class Engine, size_t w, class UIntType>
  209. class independent_bits_engine
  210. {
  211. public:
  212. // types
  213. typedef UIntType result_type;
  214. // engine characteristics
  215. static constexpr result_type min() { return 0; }
  216. static constexpr result_type max() { return 2^w - 1; }
  217. // constructors and seeding functions
  218. independent_bits_engine();
  219. explicit independent_bits_engine(const Engine& e);
  220. explicit independent_bits_engine(Engine&& e);
  221. explicit independent_bits_engine(result_type s);
  222. template<class Sseq> explicit independent_bits_engine(Sseq& q);
  223. void seed();
  224. void seed(result_type s);
  225. template<class Sseq> void seed(Sseq& q);
  226. // generating functions
  227. result_type operator()(); void discard(unsigned long long z);
  228. // property functions
  229. const Engine& base() const noexcept;
  230. };
  231. template<class Engine, size_t w, class UIntType>
  232. bool
  233. operator==(
  234. const independent_bits_engine<Engine, w, UIntType>& x,
  235. const independent_bits_engine<Engine, w, UIntType>& y);
  236. template<class Engine, size_t w, class UIntType>
  237. bool
  238. operator!=(
  239. const independent_bits_engine<Engine, w, UIntType>& x,
  240. const independent_bits_engine<Engine, w, UIntType>& y);
  241. template <class charT, class traits,
  242. class Engine, size_t w, class UIntType>
  243. basic_ostream<charT, traits>&
  244. operator<<(basic_ostream<charT, traits>& os,
  245. const independent_bits_engine<Engine, w, UIntType>& x);
  246. template <class charT, class traits,
  247. class Engine, size_t w, class UIntType>
  248. basic_istream<charT, traits>&
  249. operator>>(basic_istream<charT, traits>& is,
  250. independent_bits_engine<Engine, w, UIntType>& x);
  251. template<class Engine, size_t k>
  252. class shuffle_order_engine
  253. {
  254. public:
  255. // types
  256. typedef typename Engine::result_type result_type;
  257. // engine characteristics
  258. static constexpr size_t table_size = k;
  259. static constexpr result_type min() { return Engine::min; }
  260. static constexpr result_type max() { return Engine::max; }
  261. // constructors and seeding functions
  262. shuffle_order_engine();
  263. explicit shuffle_order_engine(const Engine& e);
  264. explicit shuffle_order_engine(Engine&& e);
  265. explicit shuffle_order_engine(result_type s);
  266. template<class Sseq> explicit shuffle_order_engine(Sseq& q);
  267. void seed();
  268. void seed(result_type s);
  269. template<class Sseq> void seed(Sseq& q);
  270. // generating functions
  271. result_type operator()();
  272. void discard(unsigned long long z);
  273. // property functions
  274. const Engine& base() const noexcept;
  275. };
  276. template<class Engine, size_t k>
  277. bool
  278. operator==(
  279. const shuffle_order_engine<Engine, k>& x,
  280. const shuffle_order_engine<Engine, k>& y);
  281. template<class Engine, size_t k>
  282. bool
  283. operator!=(
  284. const shuffle_order_engine<Engine, k>& x,
  285. const shuffle_order_engine<Engine, k>& y);
  286. template <class charT, class traits,
  287. class Engine, size_t k>
  288. basic_ostream<charT, traits>&
  289. operator<<(basic_ostream<charT, traits>& os,
  290. const shuffle_order_engine<Engine, k>& x);
  291. template <class charT, class traits,
  292. class Engine, size_t k>
  293. basic_istream<charT, traits>&
  294. operator>>(basic_istream<charT, traits>& is,
  295. shuffle_order_engine<Engine, k>& x);
  296. typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
  297. minstd_rand0;
  298. typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
  299. minstd_rand;
  300. typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
  301. 0x9908b0df,
  302. 11, 0xffffffff,
  303. 7, 0x9d2c5680,
  304. 15, 0xefc60000,
  305. 18, 1812433253> mt19937;
  306. typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
  307. 0xb5026f5aa96619e9,
  308. 29, 0x5555555555555555,
  309. 17, 0x71d67fffeda60000,
  310. 37, 0xfff7eee000000000,
  311. 43, 6364136223846793005> mt19937_64;
  312. typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
  313. typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
  314. typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
  315. typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
  316. typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
  317. typedef minstd_rand default_random_engine;
  318. // Generators
  319. class random_device
  320. {
  321. public:
  322. // types
  323. typedef unsigned int result_type;
  324. // generator characteristics
  325. static constexpr result_type min() { return numeric_limits<result_type>::min(); }
  326. static constexpr result_type max() { return numeric_limits<result_type>::max(); }
  327. // constructors
  328. explicit random_device(const string& token = "/dev/urandom");
  329. // generating functions
  330. result_type operator()();
  331. // property functions
  332. double entropy() const noexcept;
  333. // no copy functions
  334. random_device(const random_device& ) = delete;
  335. void operator=(const random_device& ) = delete;
  336. };
  337. // Utilities
  338. class seed_seq
  339. {
  340. public:
  341. // types
  342. typedef uint_least32_t result_type;
  343. // constructors
  344. seed_seq();
  345. template<class T>
  346. seed_seq(initializer_list<T> il);
  347. template<class InputIterator>
  348. seed_seq(InputIterator begin, InputIterator end);
  349. // generating functions
  350. template<class RandomAccessIterator>
  351. void generate(RandomAccessIterator begin, RandomAccessIterator end);
  352. // property functions
  353. size_t size() const;
  354. template<class OutputIterator>
  355. void param(OutputIterator dest) const;
  356. // no copy functions
  357. seed_seq(const seed_seq&) = delete;
  358. void operator=(const seed_seq& ) = delete;
  359. };
  360. template<class RealType, size_t bits, class URNG>
  361. RealType generate_canonical(URNG& g);
  362. // Distributions
  363. template<class IntType = int>
  364. class uniform_int_distribution
  365. {
  366. public:
  367. // types
  368. typedef IntType result_type;
  369. class param_type
  370. {
  371. public:
  372. typedef uniform_int_distribution distribution_type;
  373. explicit param_type(IntType a = 0,
  374. IntType b = numeric_limits<IntType>::max());
  375. result_type a() const;
  376. result_type b() const;
  377. friend bool operator==(const param_type& x, const param_type& y);
  378. friend bool operator!=(const param_type& x, const param_type& y);
  379. };
  380. // constructors and reset functions
  381. explicit uniform_int_distribution(IntType a = 0,
  382. IntType b = numeric_limits<IntType>::max());
  383. explicit uniform_int_distribution(const param_type& parm);
  384. void reset();
  385. // generating functions
  386. template<class URNG> result_type operator()(URNG& g);
  387. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  388. // property functions
  389. result_type a() const;
  390. result_type b() const;
  391. param_type param() const;
  392. void param(const param_type& parm);
  393. result_type min() const;
  394. result_type max() const;
  395. friend bool operator==(const uniform_int_distribution& x,
  396. const uniform_int_distribution& y);
  397. friend bool operator!=(const uniform_int_distribution& x,
  398. const uniform_int_distribution& y);
  399. template <class charT, class traits>
  400. friend
  401. basic_ostream<charT, traits>&
  402. operator<<(basic_ostream<charT, traits>& os,
  403. const uniform_int_distribution& x);
  404. template <class charT, class traits>
  405. friend
  406. basic_istream<charT, traits>&
  407. operator>>(basic_istream<charT, traits>& is,
  408. uniform_int_distribution& x);
  409. };
  410. template<class RealType = double>
  411. class uniform_real_distribution
  412. {
  413. public:
  414. // types
  415. typedef RealType result_type;
  416. class param_type
  417. {
  418. public:
  419. typedef uniform_real_distribution distribution_type;
  420. explicit param_type(RealType a = 0,
  421. RealType b = 1);
  422. result_type a() const;
  423. result_type b() const;
  424. friend bool operator==(const param_type& x, const param_type& y);
  425. friend bool operator!=(const param_type& x, const param_type& y);
  426. };
  427. // constructors and reset functions
  428. explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);
  429. explicit uniform_real_distribution(const param_type& parm);
  430. void reset();
  431. // generating functions
  432. template<class URNG> result_type operator()(URNG& g);
  433. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  434. // property functions
  435. result_type a() const;
  436. result_type b() const;
  437. param_type param() const;
  438. void param(const param_type& parm);
  439. result_type min() const;
  440. result_type max() const;
  441. friend bool operator==(const uniform_real_distribution& x,
  442. const uniform_real_distribution& y);
  443. friend bool operator!=(const uniform_real_distribution& x,
  444. const uniform_real_distribution& y);
  445. template <class charT, class traits>
  446. friend
  447. basic_ostream<charT, traits>&
  448. operator<<(basic_ostream<charT, traits>& os,
  449. const uniform_real_distribution& x);
  450. template <class charT, class traits>
  451. friend
  452. basic_istream<charT, traits>&
  453. operator>>(basic_istream<charT, traits>& is,
  454. uniform_real_distribution& x);
  455. };
  456. class bernoulli_distribution
  457. {
  458. public:
  459. // types
  460. typedef bool result_type;
  461. class param_type
  462. {
  463. public:
  464. typedef bernoulli_distribution distribution_type;
  465. explicit param_type(double p = 0.5);
  466. double p() const;
  467. friend bool operator==(const param_type& x, const param_type& y);
  468. friend bool operator!=(const param_type& x, const param_type& y);
  469. };
  470. // constructors and reset functions
  471. explicit bernoulli_distribution(double p = 0.5);
  472. explicit bernoulli_distribution(const param_type& parm);
  473. void reset();
  474. // generating functions
  475. template<class URNG> result_type operator()(URNG& g);
  476. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  477. // property functions
  478. double p() const;
  479. param_type param() const;
  480. void param(const param_type& parm);
  481. result_type min() const;
  482. result_type max() const;
  483. friend bool operator==(const bernoulli_distribution& x,
  484. const bernoulli_distribution& y);
  485. friend bool operator!=(const bernoulli_distribution& x,
  486. const bernoulli_distribution& y);
  487. template <class charT, class traits>
  488. friend
  489. basic_ostream<charT, traits>&
  490. operator<<(basic_ostream<charT, traits>& os,
  491. const bernoulli_distribution& x);
  492. template <class charT, class traits>
  493. friend
  494. basic_istream<charT, traits>&
  495. operator>>(basic_istream<charT, traits>& is,
  496. bernoulli_distribution& x);
  497. };
  498. template<class IntType = int>
  499. class binomial_distribution
  500. {
  501. public:
  502. // types
  503. typedef IntType result_type;
  504. class param_type
  505. {
  506. public:
  507. typedef binomial_distribution distribution_type;
  508. explicit param_type(IntType t = 1, double p = 0.5);
  509. IntType t() const;
  510. double p() const;
  511. friend bool operator==(const param_type& x, const param_type& y);
  512. friend bool operator!=(const param_type& x, const param_type& y);
  513. };
  514. // constructors and reset functions
  515. explicit binomial_distribution(IntType t = 1, double p = 0.5);
  516. explicit binomial_distribution(const param_type& parm);
  517. void reset();
  518. // generating functions
  519. template<class URNG> result_type operator()(URNG& g);
  520. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  521. // property functions
  522. IntType t() const;
  523. double p() const;
  524. param_type param() const;
  525. void param(const param_type& parm);
  526. result_type min() const;
  527. result_type max() const;
  528. friend bool operator==(const binomial_distribution& x,
  529. const binomial_distribution& y);
  530. friend bool operator!=(const binomial_distribution& x,
  531. const binomial_distribution& y);
  532. template <class charT, class traits>
  533. friend
  534. basic_ostream<charT, traits>&
  535. operator<<(basic_ostream<charT, traits>& os,
  536. const binomial_distribution& x);
  537. template <class charT, class traits>
  538. friend
  539. basic_istream<charT, traits>&
  540. operator>>(basic_istream<charT, traits>& is,
  541. binomial_distribution& x);
  542. };
  543. template<class IntType = int>
  544. class geometric_distribution
  545. {
  546. public:
  547. // types
  548. typedef IntType result_type;
  549. class param_type
  550. {
  551. public:
  552. typedef geometric_distribution distribution_type;
  553. explicit param_type(double p = 0.5);
  554. double p() const;
  555. friend bool operator==(const param_type& x, const param_type& y);
  556. friend bool operator!=(const param_type& x, const param_type& y);
  557. };
  558. // constructors and reset functions
  559. explicit geometric_distribution(double p = 0.5);
  560. explicit geometric_distribution(const param_type& parm);
  561. void reset();
  562. // generating functions
  563. template<class URNG> result_type operator()(URNG& g);
  564. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  565. // property functions
  566. double p() const;
  567. param_type param() const;
  568. void param(const param_type& parm);
  569. result_type min() const;
  570. result_type max() const;
  571. friend bool operator==(const geometric_distribution& x,
  572. const geometric_distribution& y);
  573. friend bool operator!=(const geometric_distribution& x,
  574. const geometric_distribution& y);
  575. template <class charT, class traits>
  576. friend
  577. basic_ostream<charT, traits>&
  578. operator<<(basic_ostream<charT, traits>& os,
  579. const geometric_distribution& x);
  580. template <class charT, class traits>
  581. friend
  582. basic_istream<charT, traits>&
  583. operator>>(basic_istream<charT, traits>& is,
  584. geometric_distribution& x);
  585. };
  586. template<class IntType = int>
  587. class negative_binomial_distribution
  588. {
  589. public:
  590. // types
  591. typedef IntType result_type;
  592. class param_type
  593. {
  594. public:
  595. typedef negative_binomial_distribution distribution_type;
  596. explicit param_type(result_type k = 1, double p = 0.5);
  597. result_type k() const;
  598. double p() const;
  599. friend bool operator==(const param_type& x, const param_type& y);
  600. friend bool operator!=(const param_type& x, const param_type& y);
  601. };
  602. // constructor and reset functions
  603. explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
  604. explicit negative_binomial_distribution(const param_type& parm);
  605. void reset();
  606. // generating functions
  607. template<class URNG> result_type operator()(URNG& g);
  608. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  609. // property functions
  610. result_type k() const;
  611. double p() const;
  612. param_type param() const;
  613. void param(const param_type& parm);
  614. result_type min() const;
  615. result_type max() const;
  616. friend bool operator==(const negative_binomial_distribution& x,
  617. const negative_binomial_distribution& y);
  618. friend bool operator!=(const negative_binomial_distribution& x,
  619. const negative_binomial_distribution& y);
  620. template <class charT, class traits>
  621. friend
  622. basic_ostream<charT, traits>&
  623. operator<<(basic_ostream<charT, traits>& os,
  624. const negative_binomial_distribution& x);
  625. template <class charT, class traits>
  626. friend
  627. basic_istream<charT, traits>&
  628. operator>>(basic_istream<charT, traits>& is,
  629. negative_binomial_distribution& x);
  630. };
  631. template<class IntType = int>
  632. class poisson_distribution
  633. {
  634. public:
  635. // types
  636. typedef IntType result_type;
  637. class param_type
  638. {
  639. public:
  640. typedef poisson_distribution distribution_type;
  641. explicit param_type(double mean = 1.0);
  642. double mean() const;
  643. friend bool operator==(const param_type& x, const param_type& y);
  644. friend bool operator!=(const param_type& x, const param_type& y);
  645. };
  646. // constructors and reset functions
  647. explicit poisson_distribution(double mean = 1.0);
  648. explicit poisson_distribution(const param_type& parm);
  649. void reset();
  650. // generating functions
  651. template<class URNG> result_type operator()(URNG& g);
  652. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  653. // property functions
  654. double mean() const;
  655. param_type param() const;
  656. void param(const param_type& parm);
  657. result_type min() const;
  658. result_type max() const;
  659. friend bool operator==(const poisson_distribution& x,
  660. const poisson_distribution& y);
  661. friend bool operator!=(const poisson_distribution& x,
  662. const poisson_distribution& y);
  663. template <class charT, class traits>
  664. friend
  665. basic_ostream<charT, traits>&
  666. operator<<(basic_ostream<charT, traits>& os,
  667. const poisson_distribution& x);
  668. template <class charT, class traits>
  669. friend
  670. basic_istream<charT, traits>&
  671. operator>>(basic_istream<charT, traits>& is,
  672. poisson_distribution& x);
  673. };
  674. template<class RealType = double>
  675. class exponential_distribution
  676. {
  677. public:
  678. // types
  679. typedef RealType result_type;
  680. class param_type
  681. {
  682. public:
  683. typedef exponential_distribution distribution_type;
  684. explicit param_type(result_type lambda = 1.0);
  685. result_type lambda() const;
  686. friend bool operator==(const param_type& x, const param_type& y);
  687. friend bool operator!=(const param_type& x, const param_type& y);
  688. };
  689. // constructors and reset functions
  690. explicit exponential_distribution(result_type lambda = 1.0);
  691. explicit exponential_distribution(const param_type& parm);
  692. void reset();
  693. // generating functions
  694. template<class URNG> result_type operator()(URNG& g);
  695. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  696. // property functions
  697. result_type lambda() const;
  698. param_type param() const;
  699. void param(const param_type& parm);
  700. result_type min() const;
  701. result_type max() const;
  702. friend bool operator==(const exponential_distribution& x,
  703. const exponential_distribution& y);
  704. friend bool operator!=(const exponential_distribution& x,
  705. const exponential_distribution& y);
  706. template <class charT, class traits>
  707. friend
  708. basic_ostream<charT, traits>&
  709. operator<<(basic_ostream<charT, traits>& os,
  710. const exponential_distribution& x);
  711. template <class charT, class traits>
  712. friend
  713. basic_istream<charT, traits>&
  714. operator>>(basic_istream<charT, traits>& is,
  715. exponential_distribution& x);
  716. };
  717. template<class RealType = double>
  718. class gamma_distribution
  719. {
  720. public:
  721. // types
  722. typedef RealType result_type;
  723. class param_type
  724. {
  725. public:
  726. typedef gamma_distribution distribution_type;
  727. explicit param_type(result_type alpha = 1, result_type beta = 1);
  728. result_type alpha() const;
  729. result_type beta() const;
  730. friend bool operator==(const param_type& x, const param_type& y);
  731. friend bool operator!=(const param_type& x, const param_type& y);
  732. };
  733. // constructors and reset functions
  734. explicit gamma_distribution(result_type alpha = 1, result_type beta = 1);
  735. explicit gamma_distribution(const param_type& parm);
  736. void reset();
  737. // generating functions
  738. template<class URNG> result_type operator()(URNG& g);
  739. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  740. // property functions
  741. result_type alpha() const;
  742. result_type beta() const;
  743. param_type param() const;
  744. void param(const param_type& parm);
  745. result_type min() const;
  746. result_type max() const;
  747. friend bool operator==(const gamma_distribution& x,
  748. const gamma_distribution& y);
  749. friend bool operator!=(const gamma_distribution& x,
  750. const gamma_distribution& y);
  751. template <class charT, class traits>
  752. friend
  753. basic_ostream<charT, traits>&
  754. operator<<(basic_ostream<charT, traits>& os,
  755. const gamma_distribution& x);
  756. template <class charT, class traits>
  757. friend
  758. basic_istream<charT, traits>&
  759. operator>>(basic_istream<charT, traits>& is,
  760. gamma_distribution& x);
  761. };
  762. template<class RealType = double>
  763. class weibull_distribution
  764. {
  765. public:
  766. // types
  767. typedef RealType result_type;
  768. class param_type
  769. {
  770. public:
  771. typedef weibull_distribution distribution_type;
  772. explicit param_type(result_type alpha = 1, result_type beta = 1);
  773. result_type a() const;
  774. result_type b() const;
  775. friend bool operator==(const param_type& x, const param_type& y);
  776. friend bool operator!=(const param_type& x, const param_type& y);
  777. };
  778. // constructor and reset functions
  779. explicit weibull_distribution(result_type a = 1, result_type b = 1);
  780. explicit weibull_distribution(const param_type& parm);
  781. void reset();
  782. // generating functions
  783. template<class URNG> result_type operator()(URNG& g);
  784. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  785. // property functions
  786. result_type a() const;
  787. result_type b() const;
  788. param_type param() const;
  789. void param(const param_type& parm);
  790. result_type min() const;
  791. result_type max() const;
  792. friend bool operator==(const weibull_distribution& x,
  793. const weibull_distribution& y);
  794. friend bool operator!=(const weibull_distribution& x,
  795. const weibull_distribution& y);
  796. template <class charT, class traits>
  797. friend
  798. basic_ostream<charT, traits>&
  799. operator<<(basic_ostream<charT, traits>& os,
  800. const weibull_distribution& x);
  801. template <class charT, class traits>
  802. friend
  803. basic_istream<charT, traits>&
  804. operator>>(basic_istream<charT, traits>& is,
  805. weibull_distribution& x);
  806. };
  807. template<class RealType = double>
  808. class extreme_value_distribution
  809. {
  810. public:
  811. // types
  812. typedef RealType result_type;
  813. class param_type
  814. {
  815. public:
  816. typedef extreme_value_distribution distribution_type;
  817. explicit param_type(result_type a = 0, result_type b = 1);
  818. result_type a() const;
  819. result_type b() const;
  820. friend bool operator==(const param_type& x, const param_type& y);
  821. friend bool operator!=(const param_type& x, const param_type& y);
  822. };
  823. // constructor and reset functions
  824. explicit extreme_value_distribution(result_type a = 0, result_type b = 1);
  825. explicit extreme_value_distribution(const param_type& parm);
  826. void reset();
  827. // generating functions
  828. template<class URNG> result_type operator()(URNG& g);
  829. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  830. // property functions
  831. result_type a() const;
  832. result_type b() const;
  833. param_type param() const;
  834. void param(const param_type& parm);
  835. result_type min() const;
  836. result_type max() const;
  837. friend bool operator==(const extreme_value_distribution& x,
  838. const extreme_value_distribution& y);
  839. friend bool operator!=(const extreme_value_distribution& x,
  840. const extreme_value_distribution& y);
  841. template <class charT, class traits>
  842. friend
  843. basic_ostream<charT, traits>&
  844. operator<<(basic_ostream<charT, traits>& os,
  845. const extreme_value_distribution& x);
  846. template <class charT, class traits>
  847. friend
  848. basic_istream<charT, traits>&
  849. operator>>(basic_istream<charT, traits>& is,
  850. extreme_value_distribution& x);
  851. };
  852. template<class RealType = double>
  853. class normal_distribution
  854. {
  855. public:
  856. // types
  857. typedef RealType result_type;
  858. class param_type
  859. {
  860. public:
  861. typedef normal_distribution distribution_type;
  862. explicit param_type(result_type mean = 0, result_type stddev = 1);
  863. result_type mean() const;
  864. result_type stddev() const;
  865. friend bool operator==(const param_type& x, const param_type& y);
  866. friend bool operator!=(const param_type& x, const param_type& y);
  867. };
  868. // constructors and reset functions
  869. explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
  870. explicit normal_distribution(const param_type& parm);
  871. void reset();
  872. // generating functions
  873. template<class URNG> result_type operator()(URNG& g);
  874. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  875. // property functions
  876. result_type mean() const;
  877. result_type stddev() const;
  878. param_type param() const;
  879. void param(const param_type& parm);
  880. result_type min() const;
  881. result_type max() const;
  882. friend bool operator==(const normal_distribution& x,
  883. const normal_distribution& y);
  884. friend bool operator!=(const normal_distribution& x,
  885. const normal_distribution& y);
  886. template <class charT, class traits>
  887. friend
  888. basic_ostream<charT, traits>&
  889. operator<<(basic_ostream<charT, traits>& os,
  890. const normal_distribution& x);
  891. template <class charT, class traits>
  892. friend
  893. basic_istream<charT, traits>&
  894. operator>>(basic_istream<charT, traits>& is,
  895. normal_distribution& x);
  896. };
  897. template<class RealType = double>
  898. class lognormal_distribution
  899. {
  900. public:
  901. // types
  902. typedef RealType result_type;
  903. class param_type
  904. {
  905. public:
  906. typedef lognormal_distribution distribution_type;
  907. explicit param_type(result_type m = 0, result_type s = 1);
  908. result_type m() const;
  909. result_type s() const;
  910. friend bool operator==(const param_type& x, const param_type& y);
  911. friend bool operator!=(const param_type& x, const param_type& y);
  912. };
  913. // constructor and reset functions
  914. explicit lognormal_distribution(result_type m = 0, result_type s = 1);
  915. explicit lognormal_distribution(const param_type& parm);
  916. void reset();
  917. // generating functions
  918. template<class URNG> result_type operator()(URNG& g);
  919. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  920. // property functions
  921. result_type m() const;
  922. result_type s() const;
  923. param_type param() const;
  924. void param(const param_type& parm);
  925. result_type min() const;
  926. result_type max() const;
  927. friend bool operator==(const lognormal_distribution& x,
  928. const lognormal_distribution& y);
  929. friend bool operator!=(const lognormal_distribution& x,
  930. const lognormal_distribution& y);
  931. template <class charT, class traits>
  932. friend
  933. basic_ostream<charT, traits>&
  934. operator<<(basic_ostream<charT, traits>& os,
  935. const lognormal_distribution& x);
  936. template <class charT, class traits>
  937. friend
  938. basic_istream<charT, traits>&
  939. operator>>(basic_istream<charT, traits>& is,
  940. lognormal_distribution& x);
  941. };
  942. template<class RealType = double>
  943. class chi_squared_distribution
  944. {
  945. public:
  946. // types
  947. typedef RealType result_type;
  948. class param_type
  949. {
  950. public:
  951. typedef chi_squared_distribution distribution_type;
  952. explicit param_type(result_type n = 1);
  953. result_type n() const;
  954. friend bool operator==(const param_type& x, const param_type& y);
  955. friend bool operator!=(const param_type& x, const param_type& y);
  956. };
  957. // constructor and reset functions
  958. explicit chi_squared_distribution(result_type n = 1);
  959. explicit chi_squared_distribution(const param_type& parm);
  960. void reset();
  961. // generating functions
  962. template<class URNG> result_type operator()(URNG& g);
  963. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  964. // property functions
  965. result_type n() const;
  966. param_type param() const;
  967. void param(const param_type& parm);
  968. result_type min() const;
  969. result_type max() const;
  970. friend bool operator==(const chi_squared_distribution& x,
  971. const chi_squared_distribution& y);
  972. friend bool operator!=(const chi_squared_distribution& x,
  973. const chi_squared_distribution& y);
  974. template <class charT, class traits>
  975. friend
  976. basic_ostream<charT, traits>&
  977. operator<<(basic_ostream<charT, traits>& os,
  978. const chi_squared_distribution& x);
  979. template <class charT, class traits>
  980. friend
  981. basic_istream<charT, traits>&
  982. operator>>(basic_istream<charT, traits>& is,
  983. chi_squared_distribution& x);
  984. };
  985. template<class RealType = double>
  986. class cauchy_distribution
  987. {
  988. public:
  989. // types
  990. typedef RealType result_type;
  991. class param_type
  992. {
  993. public:
  994. typedef cauchy_distribution distribution_type;
  995. explicit param_type(result_type a = 0, result_type b = 1);
  996. result_type a() const;
  997. result_type b() const;
  998. friend bool operator==(const param_type& x, const param_type& y);
  999. friend bool operator!=(const param_type& x, const param_type& y);
  1000. };
  1001. // constructor and reset functions
  1002. explicit cauchy_distribution(result_type a = 0, result_type b = 1);
  1003. explicit cauchy_distribution(const param_type& parm);
  1004. void reset();
  1005. // generating functions
  1006. template<class URNG> result_type operator()(URNG& g);
  1007. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  1008. // property functions
  1009. result_type a() const;
  1010. result_type b() const;
  1011. param_type param() const;
  1012. void param(const param_type& parm);
  1013. result_type min() const;
  1014. result_type max() const;
  1015. friend bool operator==(const cauchy_distribution& x,
  1016. const cauchy_distribution& y);
  1017. friend bool operator!=(const cauchy_distribution& x,
  1018. const cauchy_distribution& y);
  1019. template <class charT, class traits>
  1020. friend
  1021. basic_ostream<charT, traits>&
  1022. operator<<(basic_ostream<charT, traits>& os,
  1023. const cauchy_distribution& x);
  1024. template <class charT, class traits>
  1025. friend
  1026. basic_istream<charT, traits>&
  1027. operator>>(basic_istream<charT, traits>& is,
  1028. cauchy_distribution& x);
  1029. };
  1030. template<class RealType = double>
  1031. class fisher_f_distribution
  1032. {
  1033. public:
  1034. // types
  1035. typedef RealType result_type;
  1036. class param_type
  1037. {
  1038. public:
  1039. typedef fisher_f_distribution distribution_type;
  1040. explicit param_type(result_type m = 1, result_type n = 1);
  1041. result_type m() const;
  1042. result_type n() const;
  1043. friend bool operator==(const param_type& x, const param_type& y);
  1044. friend bool operator!=(const param_type& x, const param_type& y);
  1045. };
  1046. // constructor and reset functions
  1047. explicit fisher_f_distribution(result_type m = 1, result_type n = 1);
  1048. explicit fisher_f_distribution(const param_type& parm);
  1049. void reset();
  1050. // generating functions
  1051. template<class URNG> result_type operator()(URNG& g);
  1052. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  1053. // property functions
  1054. result_type m() const;
  1055. result_type n() const;
  1056. param_type param() const;
  1057. void param(const param_type& parm);
  1058. result_type min() const;
  1059. result_type max() const;
  1060. friend bool operator==(const fisher_f_distribution& x,
  1061. const fisher_f_distribution& y);
  1062. friend bool operator!=(const fisher_f_distribution& x,
  1063. const fisher_f_distribution& y);
  1064. template <class charT, class traits>
  1065. friend
  1066. basic_ostream<charT, traits>&
  1067. operator<<(basic_ostream<charT, traits>& os,
  1068. const fisher_f_distribution& x);
  1069. template <class charT, class traits>
  1070. friend
  1071. basic_istream<charT, traits>&
  1072. operator>>(basic_istream<charT, traits>& is,
  1073. fisher_f_distribution& x);
  1074. };
  1075. template<class RealType = double>
  1076. class student_t_distribution
  1077. {
  1078. public:
  1079. // types
  1080. typedef RealType result_type;
  1081. class param_type
  1082. {
  1083. public:
  1084. typedef student_t_distribution distribution_type;
  1085. explicit param_type(result_type n = 1);
  1086. result_type n() const;
  1087. friend bool operator==(const param_type& x, const param_type& y);
  1088. friend bool operator!=(const param_type& x, const param_type& y);
  1089. };
  1090. // constructor and reset functions
  1091. explicit student_t_distribution(result_type n = 1);
  1092. explicit student_t_distribution(const param_type& parm);
  1093. void reset();
  1094. // generating functions
  1095. template<class URNG> result_type operator()(URNG& g);
  1096. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  1097. // property functions
  1098. result_type n() const;
  1099. param_type param() const;
  1100. void param(const param_type& parm);
  1101. result_type min() const;
  1102. result_type max() const;
  1103. friend bool operator==(const student_t_distribution& x,
  1104. const student_t_distribution& y);
  1105. friend bool operator!=(const student_t_distribution& x,
  1106. const student_t_distribution& y);
  1107. template <class charT, class traits>
  1108. friend
  1109. basic_ostream<charT, traits>&
  1110. operator<<(basic_ostream<charT, traits>& os,
  1111. const student_t_distribution& x);
  1112. template <class charT, class traits>
  1113. friend
  1114. basic_istream<charT, traits>&
  1115. operator>>(basic_istream<charT, traits>& is,
  1116. student_t_distribution& x);
  1117. };
  1118. template<class IntType = int>
  1119. class discrete_distribution
  1120. {
  1121. public:
  1122. // types
  1123. typedef IntType result_type;
  1124. class param_type
  1125. {
  1126. public:
  1127. typedef discrete_distribution distribution_type;
  1128. param_type();
  1129. template<class InputIterator>
  1130. param_type(InputIterator firstW, InputIterator lastW);
  1131. param_type(initializer_list<double> wl);
  1132. template<class UnaryOperation>
  1133. param_type(size_t nw, double xmin, double xmax, UnaryOperation fw);
  1134. vector<double> probabilities() const;
  1135. friend bool operator==(const param_type& x, const param_type& y);
  1136. friend bool operator!=(const param_type& x, const param_type& y);
  1137. };
  1138. // constructor and reset functions
  1139. discrete_distribution();
  1140. template<class InputIterator>
  1141. discrete_distribution(InputIterator firstW, InputIterator lastW);
  1142. discrete_distribution(initializer_list<double> wl);
  1143. template<class UnaryOperation>
  1144. discrete_distribution(size_t nw, double xmin, double xmax,
  1145. UnaryOperation fw);
  1146. explicit discrete_distribution(const param_type& parm);
  1147. void reset();
  1148. // generating functions
  1149. template<class URNG> result_type operator()(URNG& g);
  1150. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  1151. // property functions
  1152. vector<double> probabilities() const;
  1153. param_type param() const;
  1154. void param(const param_type& parm);
  1155. result_type min() const;
  1156. result_type max() const;
  1157. friend bool operator==(const discrete_distribution& x,
  1158. const discrete_distribution& y);
  1159. friend bool operator!=(const discrete_distribution& x,
  1160. const discrete_distribution& y);
  1161. template <class charT, class traits>
  1162. friend
  1163. basic_ostream<charT, traits>&
  1164. operator<<(basic_ostream<charT, traits>& os,
  1165. const discrete_distribution& x);
  1166. template <class charT, class traits>
  1167. friend
  1168. basic_istream<charT, traits>&
  1169. operator>>(basic_istream<charT, traits>& is,
  1170. discrete_distribution& x);
  1171. };
  1172. template<class RealType = double>
  1173. class piecewise_constant_distribution
  1174. {
  1175. // types
  1176. typedef RealType result_type;
  1177. class param_type
  1178. {
  1179. public:
  1180. typedef piecewise_constant_distribution distribution_type;
  1181. param_type();
  1182. template<class InputIteratorB, class InputIteratorW>
  1183. param_type(InputIteratorB firstB, InputIteratorB lastB,
  1184. InputIteratorW firstW);
  1185. template<class UnaryOperation>
  1186. param_type(initializer_list<result_type> bl, UnaryOperation fw);
  1187. template<class UnaryOperation>
  1188. param_type(size_t nw, result_type xmin, result_type xmax,
  1189. UnaryOperation fw);
  1190. vector<result_type> intervals() const;
  1191. vector<result_type> densities() const;
  1192. friend bool operator==(const param_type& x, const param_type& y);
  1193. friend bool operator!=(const param_type& x, const param_type& y);
  1194. };
  1195. // constructor and reset functions
  1196. piecewise_constant_distribution();
  1197. template<class InputIteratorB, class InputIteratorW>
  1198. piecewise_constant_distribution(InputIteratorB firstB,
  1199. InputIteratorB lastB,
  1200. InputIteratorW firstW);
  1201. template<class UnaryOperation>
  1202. piecewise_constant_distribution(initializer_list<result_type> bl,
  1203. UnaryOperation fw);
  1204. template<class UnaryOperation>
  1205. piecewise_constant_distribution(size_t nw, result_type xmin,
  1206. result_type xmax, UnaryOperation fw);
  1207. explicit piecewise_constant_distribution(const param_type& parm);
  1208. void reset();
  1209. // generating functions
  1210. template<class URNG> result_type operator()(URNG& g);
  1211. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  1212. // property functions
  1213. vector<result_type> intervals() const;
  1214. vector<result_type> densities() const;
  1215. param_type param() const;
  1216. void param(const param_type& parm);
  1217. result_type min() const;
  1218. result_type max() const;
  1219. friend bool operator==(const piecewise_constant_distribution& x,
  1220. const piecewise_constant_distribution& y);
  1221. friend bool operator!=(const piecewise_constant_distribution& x,
  1222. const piecewise_constant_distribution& y);
  1223. template <class charT, class traits>
  1224. friend
  1225. basic_ostream<charT, traits>&
  1226. operator<<(basic_ostream<charT, traits>& os,
  1227. const piecewise_constant_distribution& x);
  1228. template <class charT, class traits>
  1229. friend
  1230. basic_istream<charT, traits>&
  1231. operator>>(basic_istream<charT, traits>& is,
  1232. piecewise_constant_distribution& x);
  1233. };
  1234. template<class RealType = double>
  1235. class piecewise_linear_distribution
  1236. {
  1237. // types
  1238. typedef RealType result_type;
  1239. class param_type
  1240. {
  1241. public:
  1242. typedef piecewise_linear_distribution distribution_type;
  1243. param_type();
  1244. template<class InputIteratorB, class InputIteratorW>
  1245. param_type(InputIteratorB firstB, InputIteratorB lastB,
  1246. InputIteratorW firstW);
  1247. template<class UnaryOperation>
  1248. param_type(initializer_list<result_type> bl, UnaryOperation fw);
  1249. template<class UnaryOperation>
  1250. param_type(size_t nw, result_type xmin, result_type xmax,
  1251. UnaryOperation fw);
  1252. vector<result_type> intervals() const;
  1253. vector<result_type> densities() const;
  1254. friend bool operator==(const param_type& x, const param_type& y);
  1255. friend bool operator!=(const param_type& x, const param_type& y);
  1256. };
  1257. // constructor and reset functions
  1258. piecewise_linear_distribution();
  1259. template<class InputIteratorB, class InputIteratorW>
  1260. piecewise_linear_distribution(InputIteratorB firstB,
  1261. InputIteratorB lastB,
  1262. InputIteratorW firstW);
  1263. template<class UnaryOperation>
  1264. piecewise_linear_distribution(initializer_list<result_type> bl,
  1265. UnaryOperation fw);
  1266. template<class UnaryOperation>
  1267. piecewise_linear_distribution(size_t nw, result_type xmin,
  1268. result_type xmax, UnaryOperation fw);
  1269. explicit piecewise_linear_distribution(const param_type& parm);
  1270. void reset();
  1271. // generating functions
  1272. template<class URNG> result_type operator()(URNG& g);
  1273. template<class URNG> result_type operator()(URNG& g, const param_type& parm);
  1274. // property functions
  1275. vector<result_type> intervals() const;
  1276. vector<result_type> densities() const;
  1277. param_type param() const;
  1278. void param(const param_type& parm);
  1279. result_type min() const;
  1280. result_type max() const;
  1281. friend bool operator==(const piecewise_linear_distribution& x,
  1282. const piecewise_linear_distribution& y);
  1283. friend bool operator!=(const piecewise_linear_distribution& x,
  1284. const piecewise_linear_distribution& y);
  1285. template <class charT, class traits>
  1286. friend
  1287. basic_ostream<charT, traits>&
  1288. operator<<(basic_ostream<charT, traits>& os,
  1289. const piecewise_linear_distribution& x);
  1290. template <class charT, class traits>
  1291. friend
  1292. basic_istream<charT, traits>&
  1293. operator>>(basic_istream<charT, traits>& is,
  1294. piecewise_linear_distribution& x);
  1295. };
  1296. } // std
  1297. */
  1298. // Not supported in SGX.
  1299. #include <__config>
  1300. #if !defined(_LIBCPP_SGX_CONFIG)
  1301. #include <cstddef>
  1302. #include <cstdint>
  1303. #include <cmath>
  1304. #include <type_traits>
  1305. #include <initializer_list>
  1306. #include <limits>
  1307. #include <algorithm>
  1308. #include <numeric>
  1309. #include <vector>
  1310. #include <string>
  1311. #include <istream>
  1312. #include <ostream>
  1313. #include <__undef_min_max>
  1314. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  1315. #pragma GCC system_header
  1316. #endif
  1317. _LIBCPP_BEGIN_NAMESPACE_STD
  1318. // __is_seed_sequence
  1319. template <class _Sseq, class _Engine>
  1320. struct __is_seed_sequence
  1321. {
  1322. static _LIBCPP_CONSTEXPR const bool value =
  1323. !is_convertible<_Sseq, typename _Engine::result_type>::value &&
  1324. !is_same<typename remove_cv<_Sseq>::type, _Engine>::value;
  1325. };
  1326. // linear_congruential_engine
  1327. template <unsigned long long __a, unsigned long long __c,
  1328. unsigned long long __m, unsigned long long _Mp,
  1329. bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_Mp-__c)/__a)>
  1330. struct __lce_ta;
  1331. // 64
  1332. template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
  1333. struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
  1334. {
  1335. typedef unsigned long long result_type;
  1336. _LIBCPP_INLINE_VISIBILITY
  1337. static result_type next(result_type __x)
  1338. {
  1339. // Schrage's algorithm
  1340. const result_type __q = __m / __a;
  1341. const result_type __r = __m % __a;
  1342. const result_type __t0 = __a * (__x % __q);
  1343. const result_type __t1 = __r * (__x / __q);
  1344. __x = __t0 + (__t0 < __t1) * __m - __t1;
  1345. __x += __c - (__x >= __m - __c) * __m;
  1346. return __x;
  1347. }
  1348. };
  1349. template <unsigned long long __a, unsigned long long __m>
  1350. struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
  1351. {
  1352. typedef unsigned long long result_type;
  1353. _LIBCPP_INLINE_VISIBILITY
  1354. static result_type next(result_type __x)
  1355. {
  1356. // Schrage's algorithm
  1357. const result_type __q = __m / __a;
  1358. const result_type __r = __m % __a;
  1359. const result_type __t0 = __a * (__x % __q);
  1360. const result_type __t1 = __r * (__x / __q);
  1361. __x = __t0 + (__t0 < __t1) * __m - __t1;
  1362. return __x;
  1363. }
  1364. };
  1365. template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
  1366. struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
  1367. {
  1368. typedef unsigned long long result_type;
  1369. _LIBCPP_INLINE_VISIBILITY
  1370. static result_type next(result_type __x)
  1371. {
  1372. return (__a * __x + __c) % __m;
  1373. }
  1374. };
  1375. template <unsigned long long __a, unsigned long long __c>
  1376. struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
  1377. {
  1378. typedef unsigned long long result_type;
  1379. _LIBCPP_INLINE_VISIBILITY
  1380. static result_type next(result_type __x)
  1381. {
  1382. return __a * __x + __c;
  1383. }
  1384. };
  1385. // 32
  1386. template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
  1387. struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), true>
  1388. {
  1389. typedef unsigned result_type;
  1390. _LIBCPP_INLINE_VISIBILITY
  1391. static result_type next(result_type __x)
  1392. {
  1393. const result_type __a = static_cast<result_type>(_Ap);
  1394. const result_type __c = static_cast<result_type>(_Cp);
  1395. const result_type __m = static_cast<result_type>(_Mp);
  1396. // Schrage's algorithm
  1397. const result_type __q = __m / __a;
  1398. const result_type __r = __m % __a;
  1399. const result_type __t0 = __a * (__x % __q);
  1400. const result_type __t1 = __r * (__x / __q);
  1401. __x = __t0 + (__t0 < __t1) * __m - __t1;
  1402. __x += __c - (__x >= __m - __c) * __m;
  1403. return __x;
  1404. }
  1405. };
  1406. template <unsigned long long _Ap, unsigned long long _Mp>
  1407. struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true>
  1408. {
  1409. typedef unsigned result_type;
  1410. _LIBCPP_INLINE_VISIBILITY
  1411. static result_type next(result_type __x)
  1412. {
  1413. const result_type __a = static_cast<result_type>(_Ap);
  1414. const result_type __m = static_cast<result_type>(_Mp);
  1415. // Schrage's algorithm
  1416. const result_type __q = __m / __a;
  1417. const result_type __r = __m % __a;
  1418. const result_type __t0 = __a * (__x % __q);
  1419. const result_type __t1 = __r * (__x / __q);
  1420. __x = __t0 + (__t0 < __t1) * __m - __t1;
  1421. return __x;
  1422. }
  1423. };
  1424. template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
  1425. struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), false>
  1426. {
  1427. typedef unsigned result_type;
  1428. _LIBCPP_INLINE_VISIBILITY
  1429. static result_type next(result_type __x)
  1430. {
  1431. const result_type __a = static_cast<result_type>(_Ap);
  1432. const result_type __c = static_cast<result_type>(_Cp);
  1433. const result_type __m = static_cast<result_type>(_Mp);
  1434. return (__a * __x + __c) % __m;
  1435. }
  1436. };
  1437. template <unsigned long long _Ap, unsigned long long _Cp>
  1438. struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false>
  1439. {
  1440. typedef unsigned result_type;
  1441. _LIBCPP_INLINE_VISIBILITY
  1442. static result_type next(result_type __x)
  1443. {
  1444. const result_type __a = static_cast<result_type>(_Ap);
  1445. const result_type __c = static_cast<result_type>(_Cp);
  1446. return __a * __x + __c;
  1447. }
  1448. };
  1449. // 16
  1450. template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
  1451. struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
  1452. {
  1453. typedef unsigned short result_type;
  1454. _LIBCPP_INLINE_VISIBILITY
  1455. static result_type next(result_type __x)
  1456. {
  1457. return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
  1458. }
  1459. };
  1460. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  1461. class _LIBCPP_TYPE_VIS_ONLY linear_congruential_engine;
  1462. template <class _CharT, class _Traits,
  1463. class _Up, _Up _Ap, _Up _Cp, _Up _Np>
  1464. _LIBCPP_INLINE_VISIBILITY
  1465. basic_ostream<_CharT, _Traits>&
  1466. operator<<(basic_ostream<_CharT, _Traits>& __os,
  1467. const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
  1468. template <class _CharT, class _Traits,
  1469. class _Up, _Up _Ap, _Up _Cp, _Up _Np>
  1470. basic_istream<_CharT, _Traits>&
  1471. operator>>(basic_istream<_CharT, _Traits>& __is,
  1472. linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
  1473. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  1474. class _LIBCPP_TYPE_VIS_ONLY linear_congruential_engine
  1475. {
  1476. public:
  1477. // types
  1478. typedef _UIntType result_type;
  1479. private:
  1480. result_type __x_;
  1481. static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0);
  1482. static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
  1483. static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
  1484. public:
  1485. static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u: 0u;
  1486. static _LIBCPP_CONSTEXPR const result_type _Max = __m - 1u;
  1487. static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
  1488. // engine characteristics
  1489. static _LIBCPP_CONSTEXPR const result_type multiplier = __a;
  1490. static _LIBCPP_CONSTEXPR const result_type increment = __c;
  1491. static _LIBCPP_CONSTEXPR const result_type modulus = __m;
  1492. _LIBCPP_INLINE_VISIBILITY
  1493. static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
  1494. _LIBCPP_INLINE_VISIBILITY
  1495. static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
  1496. static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
  1497. // constructors and seeding functions
  1498. _LIBCPP_INLINE_VISIBILITY
  1499. explicit linear_congruential_engine(result_type __s = default_seed)
  1500. {seed(__s);}
  1501. template<class _Sseq>
  1502. _LIBCPP_INLINE_VISIBILITY
  1503. explicit linear_congruential_engine(_Sseq& __q,
  1504. typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
  1505. {seed(__q);}
  1506. _LIBCPP_INLINE_VISIBILITY
  1507. void seed(result_type __s = default_seed)
  1508. {seed(integral_constant<bool, __m == 0>(),
  1509. integral_constant<bool, __c == 0>(), __s);}
  1510. template<class _Sseq>
  1511. _LIBCPP_INLINE_VISIBILITY
  1512. typename enable_if
  1513. <
  1514. __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
  1515. void
  1516. >::type
  1517. seed(_Sseq& __q)
  1518. {__seed(__q, integral_constant<unsigned,
  1519. 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
  1520. : (__m > 0x100000000ull))>());}
  1521. // generating functions
  1522. _LIBCPP_INLINE_VISIBILITY
  1523. result_type operator()()
  1524. {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));}
  1525. _LIBCPP_INLINE_VISIBILITY
  1526. void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
  1527. friend _LIBCPP_INLINE_VISIBILITY
  1528. bool operator==(const linear_congruential_engine& __x,
  1529. const linear_congruential_engine& __y)
  1530. {return __x.__x_ == __y.__x_;}
  1531. friend _LIBCPP_INLINE_VISIBILITY
  1532. bool operator!=(const linear_congruential_engine& __x,
  1533. const linear_congruential_engine& __y)
  1534. {return !(__x == __y);}
  1535. private:
  1536. _LIBCPP_INLINE_VISIBILITY
  1537. void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
  1538. _LIBCPP_INLINE_VISIBILITY
  1539. void seed(true_type, false_type, result_type __s) {__x_ = __s;}
  1540. _LIBCPP_INLINE_VISIBILITY
  1541. void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
  1542. 1 : __s % __m;}
  1543. _LIBCPP_INLINE_VISIBILITY
  1544. void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
  1545. template<class _Sseq>
  1546. void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
  1547. template<class _Sseq>
  1548. void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
  1549. template <class _CharT, class _Traits,
  1550. class _Up, _Up _Ap, _Up _Cp, _Up _Np>
  1551. friend
  1552. basic_ostream<_CharT, _Traits>&
  1553. operator<<(basic_ostream<_CharT, _Traits>& __os,
  1554. const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
  1555. template <class _CharT, class _Traits,
  1556. class _Up, _Up _Ap, _Up _Cp, _Up _Np>
  1557. friend
  1558. basic_istream<_CharT, _Traits>&
  1559. operator>>(basic_istream<_CharT, _Traits>& __is,
  1560. linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
  1561. };
  1562. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  1563. _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
  1564. linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
  1565. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  1566. _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
  1567. linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
  1568. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  1569. _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
  1570. linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
  1571. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  1572. _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
  1573. linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
  1574. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  1575. template<class _Sseq>
  1576. void
  1577. linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
  1578. integral_constant<unsigned, 1>)
  1579. {
  1580. const unsigned __k = 1;
  1581. uint32_t __ar[__k+3];
  1582. __q.generate(__ar, __ar + __k + 3);
  1583. result_type __s = static_cast<result_type>(__ar[3] % __m);
  1584. __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
  1585. }
  1586. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  1587. template<class _Sseq>
  1588. void
  1589. linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
  1590. integral_constant<unsigned, 2>)
  1591. {
  1592. const unsigned __k = 2;
  1593. uint32_t __ar[__k+3];
  1594. __q.generate(__ar, __ar + __k + 3);
  1595. result_type __s = static_cast<result_type>((__ar[3] +
  1596. ((uint64_t)__ar[4] << 32)) % __m);
  1597. __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
  1598. }
  1599. template <class _CharT, class _Traits,
  1600. class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  1601. inline _LIBCPP_INLINE_VISIBILITY
  1602. basic_ostream<_CharT, _Traits>&
  1603. operator<<(basic_ostream<_CharT, _Traits>& __os,
  1604. const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
  1605. {
  1606. __save_flags<_CharT, _Traits> __lx(__os);
  1607. __os.flags(ios_base::dec | ios_base::left);
  1608. __os.fill(__os.widen(' '));
  1609. return __os << __x.__x_;
  1610. }
  1611. template <class _CharT, class _Traits,
  1612. class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  1613. basic_istream<_CharT, _Traits>&
  1614. operator>>(basic_istream<_CharT, _Traits>& __is,
  1615. linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
  1616. {
  1617. __save_flags<_CharT, _Traits> __lx(__is);
  1618. __is.flags(ios_base::dec | ios_base::skipws);
  1619. _UIntType __t;
  1620. __is >> __t;
  1621. if (!__is.fail())
  1622. __x.__x_ = __t;
  1623. return __is;
  1624. }
  1625. typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
  1626. minstd_rand0;
  1627. typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
  1628. minstd_rand;
  1629. typedef minstd_rand default_random_engine;
  1630. // mersenne_twister_engine
  1631. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1632. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1633. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1634. class _LIBCPP_TYPE_VIS_ONLY mersenne_twister_engine;
  1635. template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  1636. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  1637. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  1638. bool
  1639. operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1640. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  1641. const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1642. _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  1643. template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  1644. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  1645. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  1646. _LIBCPP_INLINE_VISIBILITY
  1647. bool
  1648. operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1649. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  1650. const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1651. _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  1652. template <class _CharT, class _Traits,
  1653. class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  1654. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  1655. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  1656. basic_ostream<_CharT, _Traits>&
  1657. operator<<(basic_ostream<_CharT, _Traits>& __os,
  1658. const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1659. _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  1660. template <class _CharT, class _Traits,
  1661. class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  1662. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  1663. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  1664. basic_istream<_CharT, _Traits>&
  1665. operator>>(basic_istream<_CharT, _Traits>& __is,
  1666. mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1667. _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  1668. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1669. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1670. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1671. class _LIBCPP_TYPE_VIS_ONLY mersenne_twister_engine
  1672. {
  1673. public:
  1674. // types
  1675. typedef _UIntType result_type;
  1676. private:
  1677. result_type __x_[__n];
  1678. size_t __i_;
  1679. static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
  1680. static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
  1681. static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
  1682. static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
  1683. static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
  1684. static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
  1685. static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
  1686. static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
  1687. static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
  1688. static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
  1689. public:
  1690. static _LIBCPP_CONSTEXPR const result_type _Min = 0;
  1691. static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
  1692. (result_type(1) << __w) - result_type(1);
  1693. static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
  1694. static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
  1695. static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
  1696. static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
  1697. static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
  1698. static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
  1699. // engine characteristics
  1700. static _LIBCPP_CONSTEXPR const size_t word_size = __w;
  1701. static _LIBCPP_CONSTEXPR const size_t state_size = __n;
  1702. static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
  1703. static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
  1704. static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
  1705. static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
  1706. static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
  1707. static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
  1708. static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
  1709. static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
  1710. static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
  1711. static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
  1712. static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
  1713. _LIBCPP_INLINE_VISIBILITY
  1714. static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
  1715. _LIBCPP_INLINE_VISIBILITY
  1716. static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
  1717. static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
  1718. // constructors and seeding functions
  1719. _LIBCPP_INLINE_VISIBILITY
  1720. explicit mersenne_twister_engine(result_type __sd = default_seed)
  1721. {seed(__sd);}
  1722. template<class _Sseq>
  1723. _LIBCPP_INLINE_VISIBILITY
  1724. explicit mersenne_twister_engine(_Sseq& __q,
  1725. typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
  1726. {seed(__q);}
  1727. void seed(result_type __sd = default_seed);
  1728. template<class _Sseq>
  1729. _LIBCPP_INLINE_VISIBILITY
  1730. typename enable_if
  1731. <
  1732. __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
  1733. void
  1734. >::type
  1735. seed(_Sseq& __q)
  1736. {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
  1737. // generating functions
  1738. result_type operator()();
  1739. _LIBCPP_INLINE_VISIBILITY
  1740. void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
  1741. template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  1742. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  1743. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  1744. friend
  1745. bool
  1746. operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1747. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  1748. const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1749. _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  1750. template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  1751. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  1752. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  1753. friend
  1754. bool
  1755. operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1756. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  1757. const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1758. _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
  1759. template <class _CharT, class _Traits,
  1760. class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  1761. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  1762. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  1763. friend
  1764. basic_ostream<_CharT, _Traits>&
  1765. operator<<(basic_ostream<_CharT, _Traits>& __os,
  1766. const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1767. _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  1768. template <class _CharT, class _Traits,
  1769. class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  1770. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  1771. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  1772. friend
  1773. basic_istream<_CharT, _Traits>&
  1774. operator>>(basic_istream<_CharT, _Traits>& __is,
  1775. mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1776. _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
  1777. private:
  1778. template<class _Sseq>
  1779. void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
  1780. template<class _Sseq>
  1781. void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
  1782. template <size_t __count>
  1783. _LIBCPP_INLINE_VISIBILITY
  1784. static
  1785. typename enable_if
  1786. <
  1787. __count < __w,
  1788. result_type
  1789. >::type
  1790. __lshift(result_type __x) {return (__x << __count) & _Max;}
  1791. template <size_t __count>
  1792. _LIBCPP_INLINE_VISIBILITY
  1793. static
  1794. typename enable_if
  1795. <
  1796. (__count >= __w),
  1797. result_type
  1798. >::type
  1799. __lshift(result_type) {return result_type(0);}
  1800. template <size_t __count>
  1801. _LIBCPP_INLINE_VISIBILITY
  1802. static
  1803. typename enable_if
  1804. <
  1805. __count < _Dt,
  1806. result_type
  1807. >::type
  1808. __rshift(result_type __x) {return __x >> __count;}
  1809. template <size_t __count>
  1810. _LIBCPP_INLINE_VISIBILITY
  1811. static
  1812. typename enable_if
  1813. <
  1814. (__count >= _Dt),
  1815. result_type
  1816. >::type
  1817. __rshift(result_type) {return result_type(0);}
  1818. };
  1819. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1820. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1821. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1822. _LIBCPP_CONSTEXPR const size_t
  1823. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size;
  1824. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1825. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1826. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1827. _LIBCPP_CONSTEXPR const size_t
  1828. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size;
  1829. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1830. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1831. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1832. _LIBCPP_CONSTEXPR const size_t
  1833. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size;
  1834. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1835. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1836. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1837. _LIBCPP_CONSTEXPR const size_t
  1838. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits;
  1839. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1840. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1841. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1842. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  1843. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask;
  1844. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1845. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1846. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1847. _LIBCPP_CONSTEXPR const size_t
  1848. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u;
  1849. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1850. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1851. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1852. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  1853. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d;
  1854. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1855. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1856. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1857. _LIBCPP_CONSTEXPR const size_t
  1858. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s;
  1859. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1860. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1861. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1862. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  1863. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b;
  1864. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1865. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1866. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1867. _LIBCPP_CONSTEXPR const size_t
  1868. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t;
  1869. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1870. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1871. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1872. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  1873. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c;
  1874. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1875. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1876. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1877. _LIBCPP_CONSTEXPR const size_t
  1878. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l;
  1879. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1880. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1881. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1882. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  1883. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier;
  1884. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1885. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1886. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1887. _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
  1888. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed;
  1889. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1890. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1891. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1892. void
  1893. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
  1894. __t, __c, __l, __f>::seed(result_type __sd)
  1895. { // __w >= 2
  1896. __x_[0] = __sd & _Max;
  1897. for (size_t __i = 1; __i < __n; ++__i)
  1898. __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
  1899. __i_ = 0;
  1900. }
  1901. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1902. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1903. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1904. template<class _Sseq>
  1905. void
  1906. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
  1907. __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
  1908. {
  1909. const unsigned __k = 1;
  1910. uint32_t __ar[__n * __k];
  1911. __q.generate(__ar, __ar + __n * __k);
  1912. for (size_t __i = 0; __i < __n; ++__i)
  1913. __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
  1914. const result_type __mask = __r == _Dt ? result_type(~0) :
  1915. (result_type(1) << __r) - result_type(1);
  1916. __i_ = 0;
  1917. if ((__x_[0] & ~__mask) == 0)
  1918. {
  1919. for (size_t __i = 1; __i < __n; ++__i)
  1920. if (__x_[__i] != 0)
  1921. return;
  1922. __x_[0] = _Max;
  1923. }
  1924. }
  1925. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1926. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1927. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1928. template<class _Sseq>
  1929. void
  1930. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
  1931. __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
  1932. {
  1933. const unsigned __k = 2;
  1934. uint32_t __ar[__n * __k];
  1935. __q.generate(__ar, __ar + __n * __k);
  1936. for (size_t __i = 0; __i < __n; ++__i)
  1937. __x_[__i] = static_cast<result_type>(
  1938. (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
  1939. const result_type __mask = __r == _Dt ? result_type(~0) :
  1940. (result_type(1) << __r) - result_type(1);
  1941. __i_ = 0;
  1942. if ((__x_[0] & ~__mask) == 0)
  1943. {
  1944. for (size_t __i = 1; __i < __n; ++__i)
  1945. if (__x_[__i] != 0)
  1946. return;
  1947. __x_[0] = _Max;
  1948. }
  1949. }
  1950. template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
  1951. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  1952. _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
  1953. _UIntType
  1954. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
  1955. __t, __c, __l, __f>::operator()()
  1956. {
  1957. const size_t __j = (__i_ + 1) % __n;
  1958. const result_type __mask = __r == _Dt ? result_type(~0) :
  1959. (result_type(1) << __r) - result_type(1);
  1960. const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
  1961. const size_t __k = (__i_ + __m) % __n;
  1962. __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1));
  1963. result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
  1964. __i_ = __j;
  1965. __z ^= __lshift<__s>(__z) & __b;
  1966. __z ^= __lshift<__t>(__z) & __c;
  1967. return __z ^ __rshift<__l>(__z);
  1968. }
  1969. template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  1970. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  1971. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  1972. bool
  1973. operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1974. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  1975. const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  1976. _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
  1977. {
  1978. if (__x.__i_ == __y.__i_)
  1979. return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
  1980. if (__x.__i_ == 0 || __y.__i_ == 0)
  1981. {
  1982. size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
  1983. if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
  1984. __y.__x_ + __y.__i_))
  1985. return false;
  1986. if (__x.__i_ == 0)
  1987. return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
  1988. return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
  1989. }
  1990. if (__x.__i_ < __y.__i_)
  1991. {
  1992. size_t __j = _Np - __y.__i_;
  1993. if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
  1994. __y.__x_ + __y.__i_))
  1995. return false;
  1996. if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
  1997. __y.__x_))
  1998. return false;
  1999. return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
  2000. __y.__x_ + (_Np - (__x.__i_ + __j)));
  2001. }
  2002. size_t __j = _Np - __x.__i_;
  2003. if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
  2004. __x.__x_ + __x.__i_))
  2005. return false;
  2006. if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
  2007. __x.__x_))
  2008. return false;
  2009. return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
  2010. __x.__x_ + (_Np - (__y.__i_ + __j)));
  2011. }
  2012. template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  2013. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  2014. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  2015. inline _LIBCPP_INLINE_VISIBILITY
  2016. bool
  2017. operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  2018. _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
  2019. const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  2020. _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
  2021. {
  2022. return !(__x == __y);
  2023. }
  2024. template <class _CharT, class _Traits,
  2025. class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  2026. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  2027. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  2028. basic_ostream<_CharT, _Traits>&
  2029. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2030. const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  2031. _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
  2032. {
  2033. __save_flags<_CharT, _Traits> __lx(__os);
  2034. __os.flags(ios_base::dec | ios_base::left);
  2035. _CharT __sp = __os.widen(' ');
  2036. __os.fill(__sp);
  2037. __os << __x.__x_[__x.__i_];
  2038. for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
  2039. __os << __sp << __x.__x_[__j];
  2040. for (size_t __j = 0; __j < __x.__i_; ++__j)
  2041. __os << __sp << __x.__x_[__j];
  2042. return __os;
  2043. }
  2044. template <class _CharT, class _Traits,
  2045. class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
  2046. _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
  2047. _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
  2048. basic_istream<_CharT, _Traits>&
  2049. operator>>(basic_istream<_CharT, _Traits>& __is,
  2050. mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
  2051. _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
  2052. {
  2053. __save_flags<_CharT, _Traits> __lx(__is);
  2054. __is.flags(ios_base::dec | ios_base::skipws);
  2055. _UI __t[_Np];
  2056. for (size_t __i = 0; __i < _Np; ++__i)
  2057. __is >> __t[__i];
  2058. if (!__is.fail())
  2059. {
  2060. for (size_t __i = 0; __i < _Np; ++__i)
  2061. __x.__x_[__i] = __t[__i];
  2062. __x.__i_ = 0;
  2063. }
  2064. return __is;
  2065. }
  2066. typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
  2067. 0x9908b0df, 11, 0xffffffff,
  2068. 7, 0x9d2c5680,
  2069. 15, 0xefc60000,
  2070. 18, 1812433253> mt19937;
  2071. typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
  2072. 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
  2073. 17, 0x71d67fffeda60000ULL,
  2074. 37, 0xfff7eee000000000ULL,
  2075. 43, 6364136223846793005ULL> mt19937_64;
  2076. // subtract_with_carry_engine
  2077. template<class _UIntType, size_t __w, size_t __s, size_t __r>
  2078. class _LIBCPP_TYPE_VIS_ONLY subtract_with_carry_engine;
  2079. template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2080. bool
  2081. operator==(
  2082. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
  2083. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
  2084. template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2085. _LIBCPP_INLINE_VISIBILITY
  2086. bool
  2087. operator!=(
  2088. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
  2089. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
  2090. template <class _CharT, class _Traits,
  2091. class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2092. basic_ostream<_CharT, _Traits>&
  2093. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2094. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
  2095. template <class _CharT, class _Traits,
  2096. class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2097. basic_istream<_CharT, _Traits>&
  2098. operator>>(basic_istream<_CharT, _Traits>& __is,
  2099. subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
  2100. template<class _UIntType, size_t __w, size_t __s, size_t __r>
  2101. class _LIBCPP_TYPE_VIS_ONLY subtract_with_carry_engine
  2102. {
  2103. public:
  2104. // types
  2105. typedef _UIntType result_type;
  2106. private:
  2107. result_type __x_[__r];
  2108. result_type __c_;
  2109. size_t __i_;
  2110. static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
  2111. static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
  2112. static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
  2113. static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
  2114. static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
  2115. public:
  2116. static _LIBCPP_CONSTEXPR const result_type _Min = 0;
  2117. static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
  2118. (result_type(1) << __w) - result_type(1);
  2119. static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
  2120. // engine characteristics
  2121. static _LIBCPP_CONSTEXPR const size_t word_size = __w;
  2122. static _LIBCPP_CONSTEXPR const size_t short_lag = __s;
  2123. static _LIBCPP_CONSTEXPR const size_t long_lag = __r;
  2124. _LIBCPP_INLINE_VISIBILITY
  2125. static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
  2126. _LIBCPP_INLINE_VISIBILITY
  2127. static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
  2128. static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
  2129. // constructors and seeding functions
  2130. _LIBCPP_INLINE_VISIBILITY
  2131. explicit subtract_with_carry_engine(result_type __sd = default_seed)
  2132. {seed(__sd);}
  2133. template<class _Sseq>
  2134. _LIBCPP_INLINE_VISIBILITY
  2135. explicit subtract_with_carry_engine(_Sseq& __q,
  2136. typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
  2137. {seed(__q);}
  2138. _LIBCPP_INLINE_VISIBILITY
  2139. void seed(result_type __sd = default_seed)
  2140. {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
  2141. template<class _Sseq>
  2142. _LIBCPP_INLINE_VISIBILITY
  2143. typename enable_if
  2144. <
  2145. __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
  2146. void
  2147. >::type
  2148. seed(_Sseq& __q)
  2149. {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
  2150. // generating functions
  2151. result_type operator()();
  2152. _LIBCPP_INLINE_VISIBILITY
  2153. void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
  2154. template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2155. friend
  2156. bool
  2157. operator==(
  2158. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
  2159. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
  2160. template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2161. friend
  2162. bool
  2163. operator!=(
  2164. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
  2165. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
  2166. template <class _CharT, class _Traits,
  2167. class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2168. friend
  2169. basic_ostream<_CharT, _Traits>&
  2170. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2171. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
  2172. template <class _CharT, class _Traits,
  2173. class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2174. friend
  2175. basic_istream<_CharT, _Traits>&
  2176. operator>>(basic_istream<_CharT, _Traits>& __is,
  2177. subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
  2178. private:
  2179. void seed(result_type __sd, integral_constant<unsigned, 1>);
  2180. void seed(result_type __sd, integral_constant<unsigned, 2>);
  2181. template<class _Sseq>
  2182. void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
  2183. template<class _Sseq>
  2184. void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
  2185. };
  2186. template<class _UIntType, size_t __w, size_t __s, size_t __r>
  2187. _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
  2188. template<class _UIntType, size_t __w, size_t __s, size_t __r>
  2189. _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
  2190. template<class _UIntType, size_t __w, size_t __s, size_t __r>
  2191. _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
  2192. template<class _UIntType, size_t __w, size_t __s, size_t __r>
  2193. _LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type
  2194. subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
  2195. template<class _UIntType, size_t __w, size_t __s, size_t __r>
  2196. void
  2197. subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
  2198. integral_constant<unsigned, 1>)
  2199. {
  2200. linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
  2201. __e(__sd == 0u ? default_seed : __sd);
  2202. for (size_t __i = 0; __i < __r; ++__i)
  2203. __x_[__i] = static_cast<result_type>(__e() & _Max);
  2204. __c_ = __x_[__r-1] == 0;
  2205. __i_ = 0;
  2206. }
  2207. template<class _UIntType, size_t __w, size_t __s, size_t __r>
  2208. void
  2209. subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
  2210. integral_constant<unsigned, 2>)
  2211. {
  2212. linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
  2213. __e(__sd == 0u ? default_seed : __sd);
  2214. for (size_t __i = 0; __i < __r; ++__i)
  2215. {
  2216. result_type __e0 = __e();
  2217. __x_[__i] = static_cast<result_type>(
  2218. (__e0 + ((uint64_t)__e() << 32)) & _Max);
  2219. }
  2220. __c_ = __x_[__r-1] == 0;
  2221. __i_ = 0;
  2222. }
  2223. template<class _UIntType, size_t __w, size_t __s, size_t __r>
  2224. template<class _Sseq>
  2225. void
  2226. subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
  2227. integral_constant<unsigned, 1>)
  2228. {
  2229. const unsigned __k = 1;
  2230. uint32_t __ar[__r * __k];
  2231. __q.generate(__ar, __ar + __r * __k);
  2232. for (size_t __i = 0; __i < __r; ++__i)
  2233. __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
  2234. __c_ = __x_[__r-1] == 0;
  2235. __i_ = 0;
  2236. }
  2237. template<class _UIntType, size_t __w, size_t __s, size_t __r>
  2238. template<class _Sseq>
  2239. void
  2240. subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
  2241. integral_constant<unsigned, 2>)
  2242. {
  2243. const unsigned __k = 2;
  2244. uint32_t __ar[__r * __k];
  2245. __q.generate(__ar, __ar + __r * __k);
  2246. for (size_t __i = 0; __i < __r; ++__i)
  2247. __x_[__i] = static_cast<result_type>(
  2248. (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
  2249. __c_ = __x_[__r-1] == 0;
  2250. __i_ = 0;
  2251. }
  2252. template<class _UIntType, size_t __w, size_t __s, size_t __r>
  2253. _UIntType
  2254. subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
  2255. {
  2256. const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
  2257. result_type& __xr = __x_[__i_];
  2258. result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
  2259. __xr = (__xs - __xr - __c_) & _Max;
  2260. __c_ = __new_c;
  2261. __i_ = (__i_ + 1) % __r;
  2262. return __xr;
  2263. }
  2264. template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2265. bool
  2266. operator==(
  2267. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
  2268. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
  2269. {
  2270. if (__x.__c_ != __y.__c_)
  2271. return false;
  2272. if (__x.__i_ == __y.__i_)
  2273. return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
  2274. if (__x.__i_ == 0 || __y.__i_ == 0)
  2275. {
  2276. size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_);
  2277. if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
  2278. __y.__x_ + __y.__i_))
  2279. return false;
  2280. if (__x.__i_ == 0)
  2281. return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
  2282. return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
  2283. }
  2284. if (__x.__i_ < __y.__i_)
  2285. {
  2286. size_t __j = _Rp - __y.__i_;
  2287. if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
  2288. __y.__x_ + __y.__i_))
  2289. return false;
  2290. if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp,
  2291. __y.__x_))
  2292. return false;
  2293. return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
  2294. __y.__x_ + (_Rp - (__x.__i_ + __j)));
  2295. }
  2296. size_t __j = _Rp - __x.__i_;
  2297. if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
  2298. __x.__x_ + __x.__i_))
  2299. return false;
  2300. if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp,
  2301. __x.__x_))
  2302. return false;
  2303. return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
  2304. __x.__x_ + (_Rp - (__y.__i_ + __j)));
  2305. }
  2306. template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2307. inline _LIBCPP_INLINE_VISIBILITY
  2308. bool
  2309. operator!=(
  2310. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
  2311. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
  2312. {
  2313. return !(__x == __y);
  2314. }
  2315. template <class _CharT, class _Traits,
  2316. class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2317. basic_ostream<_CharT, _Traits>&
  2318. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2319. const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
  2320. {
  2321. __save_flags<_CharT, _Traits> __lx(__os);
  2322. __os.flags(ios_base::dec | ios_base::left);
  2323. _CharT __sp = __os.widen(' ');
  2324. __os.fill(__sp);
  2325. __os << __x.__x_[__x.__i_];
  2326. for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
  2327. __os << __sp << __x.__x_[__j];
  2328. for (size_t __j = 0; __j < __x.__i_; ++__j)
  2329. __os << __sp << __x.__x_[__j];
  2330. __os << __sp << __x.__c_;
  2331. return __os;
  2332. }
  2333. template <class _CharT, class _Traits,
  2334. class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
  2335. basic_istream<_CharT, _Traits>&
  2336. operator>>(basic_istream<_CharT, _Traits>& __is,
  2337. subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
  2338. {
  2339. __save_flags<_CharT, _Traits> __lx(__is);
  2340. __is.flags(ios_base::dec | ios_base::skipws);
  2341. _UI __t[_Rp+1];
  2342. for (size_t __i = 0; __i < _Rp+1; ++__i)
  2343. __is >> __t[__i];
  2344. if (!__is.fail())
  2345. {
  2346. for (size_t __i = 0; __i < _Rp; ++__i)
  2347. __x.__x_[__i] = __t[__i];
  2348. __x.__c_ = __t[_Rp];
  2349. __x.__i_ = 0;
  2350. }
  2351. return __is;
  2352. }
  2353. typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
  2354. typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
  2355. // discard_block_engine
  2356. template<class _Engine, size_t __p, size_t __r>
  2357. class _LIBCPP_TYPE_VIS_ONLY discard_block_engine
  2358. {
  2359. _Engine __e_;
  2360. int __n_;
  2361. static_assert( 0 < __r, "discard_block_engine invalid parameters");
  2362. static_assert(__r <= __p, "discard_block_engine invalid parameters");
  2363. public:
  2364. // types
  2365. typedef typename _Engine::result_type result_type;
  2366. // engine characteristics
  2367. static _LIBCPP_CONSTEXPR const size_t block_size = __p;
  2368. static _LIBCPP_CONSTEXPR const size_t used_block = __r;
  2369. #ifdef _LIBCPP_HAS_NO_CONSTEXPR
  2370. static const result_type _Min = _Engine::_Min;
  2371. static const result_type _Max = _Engine::_Max;
  2372. #else
  2373. static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
  2374. static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
  2375. #endif
  2376. _LIBCPP_INLINE_VISIBILITY
  2377. static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
  2378. _LIBCPP_INLINE_VISIBILITY
  2379. static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
  2380. // constructors and seeding functions
  2381. _LIBCPP_INLINE_VISIBILITY
  2382. discard_block_engine() : __n_(0) {}
  2383. _LIBCPP_INLINE_VISIBILITY
  2384. explicit discard_block_engine(const _Engine& __e)
  2385. : __e_(__e), __n_(0) {}
  2386. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2387. _LIBCPP_INLINE_VISIBILITY
  2388. explicit discard_block_engine(_Engine&& __e)
  2389. : __e_(_VSTD::move(__e)), __n_(0) {}
  2390. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2391. _LIBCPP_INLINE_VISIBILITY
  2392. explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
  2393. template<class _Sseq>
  2394. _LIBCPP_INLINE_VISIBILITY
  2395. explicit discard_block_engine(_Sseq& __q,
  2396. typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
  2397. !is_convertible<_Sseq, _Engine>::value>::type* = 0)
  2398. : __e_(__q), __n_(0) {}
  2399. _LIBCPP_INLINE_VISIBILITY
  2400. void seed() {__e_.seed(); __n_ = 0;}
  2401. _LIBCPP_INLINE_VISIBILITY
  2402. void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
  2403. template<class _Sseq>
  2404. _LIBCPP_INLINE_VISIBILITY
  2405. typename enable_if
  2406. <
  2407. __is_seed_sequence<_Sseq, discard_block_engine>::value,
  2408. void
  2409. >::type
  2410. seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
  2411. // generating functions
  2412. result_type operator()();
  2413. _LIBCPP_INLINE_VISIBILITY
  2414. void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
  2415. // property functions
  2416. _LIBCPP_INLINE_VISIBILITY
  2417. const _Engine& base() const _NOEXCEPT {return __e_;}
  2418. template<class _Eng, size_t _Pp, size_t _Rp>
  2419. friend
  2420. bool
  2421. operator==(
  2422. const discard_block_engine<_Eng, _Pp, _Rp>& __x,
  2423. const discard_block_engine<_Eng, _Pp, _Rp>& __y);
  2424. template<class _Eng, size_t _Pp, size_t _Rp>
  2425. friend
  2426. bool
  2427. operator!=(
  2428. const discard_block_engine<_Eng, _Pp, _Rp>& __x,
  2429. const discard_block_engine<_Eng, _Pp, _Rp>& __y);
  2430. template <class _CharT, class _Traits,
  2431. class _Eng, size_t _Pp, size_t _Rp>
  2432. friend
  2433. basic_ostream<_CharT, _Traits>&
  2434. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2435. const discard_block_engine<_Eng, _Pp, _Rp>& __x);
  2436. template <class _CharT, class _Traits,
  2437. class _Eng, size_t _Pp, size_t _Rp>
  2438. friend
  2439. basic_istream<_CharT, _Traits>&
  2440. operator>>(basic_istream<_CharT, _Traits>& __is,
  2441. discard_block_engine<_Eng, _Pp, _Rp>& __x);
  2442. };
  2443. template<class _Engine, size_t __p, size_t __r>
  2444. _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
  2445. template<class _Engine, size_t __p, size_t __r>
  2446. _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
  2447. template<class _Engine, size_t __p, size_t __r>
  2448. typename discard_block_engine<_Engine, __p, __r>::result_type
  2449. discard_block_engine<_Engine, __p, __r>::operator()()
  2450. {
  2451. if (__n_ >= __r)
  2452. {
  2453. __e_.discard(__p - __r);
  2454. __n_ = 0;
  2455. }
  2456. ++__n_;
  2457. return __e_();
  2458. }
  2459. template<class _Eng, size_t _Pp, size_t _Rp>
  2460. inline _LIBCPP_INLINE_VISIBILITY
  2461. bool
  2462. operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
  2463. const discard_block_engine<_Eng, _Pp, _Rp>& __y)
  2464. {
  2465. return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
  2466. }
  2467. template<class _Eng, size_t _Pp, size_t _Rp>
  2468. inline _LIBCPP_INLINE_VISIBILITY
  2469. bool
  2470. operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
  2471. const discard_block_engine<_Eng, _Pp, _Rp>& __y)
  2472. {
  2473. return !(__x == __y);
  2474. }
  2475. template <class _CharT, class _Traits,
  2476. class _Eng, size_t _Pp, size_t _Rp>
  2477. basic_ostream<_CharT, _Traits>&
  2478. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2479. const discard_block_engine<_Eng, _Pp, _Rp>& __x)
  2480. {
  2481. __save_flags<_CharT, _Traits> __lx(__os);
  2482. __os.flags(ios_base::dec | ios_base::left);
  2483. _CharT __sp = __os.widen(' ');
  2484. __os.fill(__sp);
  2485. return __os << __x.__e_ << __sp << __x.__n_;
  2486. }
  2487. template <class _CharT, class _Traits,
  2488. class _Eng, size_t _Pp, size_t _Rp>
  2489. basic_istream<_CharT, _Traits>&
  2490. operator>>(basic_istream<_CharT, _Traits>& __is,
  2491. discard_block_engine<_Eng, _Pp, _Rp>& __x)
  2492. {
  2493. __save_flags<_CharT, _Traits> __lx(__is);
  2494. __is.flags(ios_base::dec | ios_base::skipws);
  2495. _Eng __e;
  2496. int __n;
  2497. __is >> __e >> __n;
  2498. if (!__is.fail())
  2499. {
  2500. __x.__e_ = __e;
  2501. __x.__n_ = __n;
  2502. }
  2503. return __is;
  2504. }
  2505. typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
  2506. typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
  2507. // independent_bits_engine
  2508. template<class _Engine, size_t __w, class _UIntType>
  2509. class _LIBCPP_TYPE_VIS_ONLY independent_bits_engine
  2510. {
  2511. template <class _UI, _UI _R0, size_t _Wp, size_t _Mp>
  2512. class __get_n
  2513. {
  2514. static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UI>::digits;
  2515. static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
  2516. static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
  2517. static _LIBCPP_CONSTEXPR const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
  2518. public:
  2519. static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
  2520. };
  2521. public:
  2522. // types
  2523. typedef _UIntType result_type;
  2524. private:
  2525. _Engine __e_;
  2526. static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
  2527. static_assert( 0 < __w, "independent_bits_engine invalid parameters");
  2528. static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
  2529. typedef typename _Engine::result_type _Engine_result_type;
  2530. typedef typename conditional
  2531. <
  2532. sizeof(_Engine_result_type) <= sizeof(result_type),
  2533. result_type,
  2534. _Engine_result_type
  2535. >::type _Working_result_type;
  2536. #ifdef _LIBCPP_HAS_NO_CONSTEXPR
  2537. static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
  2538. + _Working_result_type(1);
  2539. #else
  2540. static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
  2541. + _Working_result_type(1);
  2542. #endif
  2543. static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
  2544. static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
  2545. static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
  2546. static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
  2547. static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
  2548. static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
  2549. static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
  2550. (_Rp >> __w0) << __w0;
  2551. static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
  2552. (_Rp >> (__w0+1)) << (__w0+1);
  2553. static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
  2554. _Engine_result_type(~0) >> (_EDt - __w0) :
  2555. _Engine_result_type(0);
  2556. static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
  2557. _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
  2558. _Engine_result_type(~0);
  2559. public:
  2560. static _LIBCPP_CONSTEXPR const result_type _Min = 0;
  2561. static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
  2562. (result_type(1) << __w) - result_type(1);
  2563. static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
  2564. // engine characteristics
  2565. _LIBCPP_INLINE_VISIBILITY
  2566. static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
  2567. _LIBCPP_INLINE_VISIBILITY
  2568. static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
  2569. // constructors and seeding functions
  2570. _LIBCPP_INLINE_VISIBILITY
  2571. independent_bits_engine() {}
  2572. _LIBCPP_INLINE_VISIBILITY
  2573. explicit independent_bits_engine(const _Engine& __e)
  2574. : __e_(__e) {}
  2575. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2576. _LIBCPP_INLINE_VISIBILITY
  2577. explicit independent_bits_engine(_Engine&& __e)
  2578. : __e_(_VSTD::move(__e)) {}
  2579. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2580. _LIBCPP_INLINE_VISIBILITY
  2581. explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
  2582. template<class _Sseq>
  2583. _LIBCPP_INLINE_VISIBILITY
  2584. explicit independent_bits_engine(_Sseq& __q,
  2585. typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
  2586. !is_convertible<_Sseq, _Engine>::value>::type* = 0)
  2587. : __e_(__q) {}
  2588. _LIBCPP_INLINE_VISIBILITY
  2589. void seed() {__e_.seed();}
  2590. _LIBCPP_INLINE_VISIBILITY
  2591. void seed(result_type __sd) {__e_.seed(__sd);}
  2592. template<class _Sseq>
  2593. _LIBCPP_INLINE_VISIBILITY
  2594. typename enable_if
  2595. <
  2596. __is_seed_sequence<_Sseq, independent_bits_engine>::value,
  2597. void
  2598. >::type
  2599. seed(_Sseq& __q) {__e_.seed(__q);}
  2600. // generating functions
  2601. _LIBCPP_INLINE_VISIBILITY
  2602. result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
  2603. _LIBCPP_INLINE_VISIBILITY
  2604. void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
  2605. // property functions
  2606. _LIBCPP_INLINE_VISIBILITY
  2607. const _Engine& base() const _NOEXCEPT {return __e_;}
  2608. template<class _Eng, size_t _Wp, class _UI>
  2609. friend
  2610. bool
  2611. operator==(
  2612. const independent_bits_engine<_Eng, _Wp, _UI>& __x,
  2613. const independent_bits_engine<_Eng, _Wp, _UI>& __y);
  2614. template<class _Eng, size_t _Wp, class _UI>
  2615. friend
  2616. bool
  2617. operator!=(
  2618. const independent_bits_engine<_Eng, _Wp, _UI>& __x,
  2619. const independent_bits_engine<_Eng, _Wp, _UI>& __y);
  2620. template <class _CharT, class _Traits,
  2621. class _Eng, size_t _Wp, class _UI>
  2622. friend
  2623. basic_ostream<_CharT, _Traits>&
  2624. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2625. const independent_bits_engine<_Eng, _Wp, _UI>& __x);
  2626. template <class _CharT, class _Traits,
  2627. class _Eng, size_t _Wp, class _UI>
  2628. friend
  2629. basic_istream<_CharT, _Traits>&
  2630. operator>>(basic_istream<_CharT, _Traits>& __is,
  2631. independent_bits_engine<_Eng, _Wp, _UI>& __x);
  2632. private:
  2633. _LIBCPP_INLINE_VISIBILITY
  2634. result_type __eval(false_type);
  2635. result_type __eval(true_type);
  2636. template <size_t __count>
  2637. _LIBCPP_INLINE_VISIBILITY
  2638. static
  2639. typename enable_if
  2640. <
  2641. __count < _Dt,
  2642. result_type
  2643. >::type
  2644. __lshift(result_type __x) {return __x << __count;}
  2645. template <size_t __count>
  2646. _LIBCPP_INLINE_VISIBILITY
  2647. static
  2648. typename enable_if
  2649. <
  2650. (__count >= _Dt),
  2651. result_type
  2652. >::type
  2653. __lshift(result_type) {return result_type(0);}
  2654. };
  2655. template<class _Engine, size_t __w, class _UIntType>
  2656. inline
  2657. _UIntType
  2658. independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
  2659. {
  2660. return static_cast<result_type>(__e_() & __mask0);
  2661. }
  2662. template<class _Engine, size_t __w, class _UIntType>
  2663. _UIntType
  2664. independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
  2665. {
  2666. result_type _Sp = 0;
  2667. for (size_t __k = 0; __k < __n0; ++__k)
  2668. {
  2669. _Engine_result_type __u;
  2670. do
  2671. {
  2672. __u = __e_() - _Engine::min();
  2673. } while (__u >= __y0);
  2674. _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
  2675. }
  2676. for (size_t __k = __n0; __k < __n; ++__k)
  2677. {
  2678. _Engine_result_type __u;
  2679. do
  2680. {
  2681. __u = __e_() - _Engine::min();
  2682. } while (__u >= __y1);
  2683. _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
  2684. }
  2685. return _Sp;
  2686. }
  2687. template<class _Eng, size_t _Wp, class _UI>
  2688. inline _LIBCPP_INLINE_VISIBILITY
  2689. bool
  2690. operator==(
  2691. const independent_bits_engine<_Eng, _Wp, _UI>& __x,
  2692. const independent_bits_engine<_Eng, _Wp, _UI>& __y)
  2693. {
  2694. return __x.base() == __y.base();
  2695. }
  2696. template<class _Eng, size_t _Wp, class _UI>
  2697. inline _LIBCPP_INLINE_VISIBILITY
  2698. bool
  2699. operator!=(
  2700. const independent_bits_engine<_Eng, _Wp, _UI>& __x,
  2701. const independent_bits_engine<_Eng, _Wp, _UI>& __y)
  2702. {
  2703. return !(__x == __y);
  2704. }
  2705. template <class _CharT, class _Traits,
  2706. class _Eng, size_t _Wp, class _UI>
  2707. basic_ostream<_CharT, _Traits>&
  2708. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2709. const independent_bits_engine<_Eng, _Wp, _UI>& __x)
  2710. {
  2711. return __os << __x.base();
  2712. }
  2713. template <class _CharT, class _Traits,
  2714. class _Eng, size_t _Wp, class _UI>
  2715. basic_istream<_CharT, _Traits>&
  2716. operator>>(basic_istream<_CharT, _Traits>& __is,
  2717. independent_bits_engine<_Eng, _Wp, _UI>& __x)
  2718. {
  2719. _Eng __e;
  2720. __is >> __e;
  2721. if (!__is.fail())
  2722. __x.__e_ = __e;
  2723. return __is;
  2724. }
  2725. // shuffle_order_engine
  2726. template <uint64_t _Xp, uint64_t _Yp>
  2727. struct __ugcd
  2728. {
  2729. static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
  2730. };
  2731. template <uint64_t _Xp>
  2732. struct __ugcd<_Xp, 0>
  2733. {
  2734. static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
  2735. };
  2736. template <uint64_t _Np, uint64_t _Dp>
  2737. class __uratio
  2738. {
  2739. static_assert(_Dp != 0, "__uratio divide by 0");
  2740. static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
  2741. public:
  2742. static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
  2743. static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;
  2744. typedef __uratio<num, den> type;
  2745. };
  2746. template<class _Engine, size_t __k>
  2747. class _LIBCPP_TYPE_VIS_ONLY shuffle_order_engine
  2748. {
  2749. static_assert(0 < __k, "shuffle_order_engine invalid parameters");
  2750. public:
  2751. // types
  2752. typedef typename _Engine::result_type result_type;
  2753. private:
  2754. _Engine __e_;
  2755. result_type _V_[__k];
  2756. result_type _Y_;
  2757. public:
  2758. // engine characteristics
  2759. static _LIBCPP_CONSTEXPR const size_t table_size = __k;
  2760. #ifdef _LIBCPP_HAS_NO_CONSTEXPR
  2761. static const result_type _Min = _Engine::_Min;
  2762. static const result_type _Max = _Engine::_Max;
  2763. #else
  2764. static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
  2765. static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
  2766. #endif
  2767. static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
  2768. _LIBCPP_INLINE_VISIBILITY
  2769. static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
  2770. _LIBCPP_INLINE_VISIBILITY
  2771. static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
  2772. static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;
  2773. // constructors and seeding functions
  2774. _LIBCPP_INLINE_VISIBILITY
  2775. shuffle_order_engine() {__init();}
  2776. _LIBCPP_INLINE_VISIBILITY
  2777. explicit shuffle_order_engine(const _Engine& __e)
  2778. : __e_(__e) {__init();}
  2779. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2780. _LIBCPP_INLINE_VISIBILITY
  2781. explicit shuffle_order_engine(_Engine&& __e)
  2782. : __e_(_VSTD::move(__e)) {__init();}
  2783. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2784. _LIBCPP_INLINE_VISIBILITY
  2785. explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
  2786. template<class _Sseq>
  2787. _LIBCPP_INLINE_VISIBILITY
  2788. explicit shuffle_order_engine(_Sseq& __q,
  2789. typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
  2790. !is_convertible<_Sseq, _Engine>::value>::type* = 0)
  2791. : __e_(__q) {__init();}
  2792. _LIBCPP_INLINE_VISIBILITY
  2793. void seed() {__e_.seed(); __init();}
  2794. _LIBCPP_INLINE_VISIBILITY
  2795. void seed(result_type __sd) {__e_.seed(__sd); __init();}
  2796. template<class _Sseq>
  2797. _LIBCPP_INLINE_VISIBILITY
  2798. typename enable_if
  2799. <
  2800. __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
  2801. void
  2802. >::type
  2803. seed(_Sseq& __q) {__e_.seed(__q); __init();}
  2804. // generating functions
  2805. _LIBCPP_INLINE_VISIBILITY
  2806. result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
  2807. _LIBCPP_INLINE_VISIBILITY
  2808. void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
  2809. // property functions
  2810. _LIBCPP_INLINE_VISIBILITY
  2811. const _Engine& base() const _NOEXCEPT {return __e_;}
  2812. private:
  2813. template<class _Eng, size_t _Kp>
  2814. friend
  2815. bool
  2816. operator==(
  2817. const shuffle_order_engine<_Eng, _Kp>& __x,
  2818. const shuffle_order_engine<_Eng, _Kp>& __y);
  2819. template<class _Eng, size_t _Kp>
  2820. friend
  2821. bool
  2822. operator!=(
  2823. const shuffle_order_engine<_Eng, _Kp>& __x,
  2824. const shuffle_order_engine<_Eng, _Kp>& __y);
  2825. template <class _CharT, class _Traits,
  2826. class _Eng, size_t _Kp>
  2827. friend
  2828. basic_ostream<_CharT, _Traits>&
  2829. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2830. const shuffle_order_engine<_Eng, _Kp>& __x);
  2831. template <class _CharT, class _Traits,
  2832. class _Eng, size_t _Kp>
  2833. friend
  2834. basic_istream<_CharT, _Traits>&
  2835. operator>>(basic_istream<_CharT, _Traits>& __is,
  2836. shuffle_order_engine<_Eng, _Kp>& __x);
  2837. _LIBCPP_INLINE_VISIBILITY
  2838. void __init()
  2839. {
  2840. for (size_t __i = 0; __i < __k; ++__i)
  2841. _V_[__i] = __e_();
  2842. _Y_ = __e_();
  2843. }
  2844. _LIBCPP_INLINE_VISIBILITY
  2845. result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
  2846. _LIBCPP_INLINE_VISIBILITY
  2847. result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}
  2848. _LIBCPP_INLINE_VISIBILITY
  2849. result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
  2850. _LIBCPP_INLINE_VISIBILITY
  2851. result_type __eval2(true_type) {return __evalf<__k, 0>();}
  2852. template <uint64_t _Np, uint64_t _Dp>
  2853. _LIBCPP_INLINE_VISIBILITY
  2854. typename enable_if
  2855. <
  2856. (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
  2857. result_type
  2858. >::type
  2859. __eval(__uratio<_Np, _Dp>)
  2860. {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}
  2861. template <uint64_t _Np, uint64_t _Dp>
  2862. _LIBCPP_INLINE_VISIBILITY
  2863. typename enable_if
  2864. <
  2865. __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
  2866. result_type
  2867. >::type
  2868. __eval(__uratio<_Np, _Dp>)
  2869. {
  2870. const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
  2871. / __uratio<_Np, _Dp>::den);
  2872. _Y_ = _V_[__j];
  2873. _V_[__j] = __e_();
  2874. return _Y_;
  2875. }
  2876. template <uint64_t __n, uint64_t __d>
  2877. _LIBCPP_INLINE_VISIBILITY
  2878. result_type __evalf()
  2879. {
  2880. const double _Fp = __d == 0 ?
  2881. __n / (2. * 0x8000000000000000ull) :
  2882. __n / (double)__d;
  2883. const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
  2884. _Y_ = _V_[__j];
  2885. _V_[__j] = __e_();
  2886. return _Y_;
  2887. }
  2888. };
  2889. template<class _Engine, size_t __k>
  2890. _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size;
  2891. template<class _Eng, size_t _Kp>
  2892. bool
  2893. operator==(
  2894. const shuffle_order_engine<_Eng, _Kp>& __x,
  2895. const shuffle_order_engine<_Eng, _Kp>& __y)
  2896. {
  2897. return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
  2898. __x.__e_ == __y.__e_;
  2899. }
  2900. template<class _Eng, size_t _Kp>
  2901. inline _LIBCPP_INLINE_VISIBILITY
  2902. bool
  2903. operator!=(
  2904. const shuffle_order_engine<_Eng, _Kp>& __x,
  2905. const shuffle_order_engine<_Eng, _Kp>& __y)
  2906. {
  2907. return !(__x == __y);
  2908. }
  2909. template <class _CharT, class _Traits,
  2910. class _Eng, size_t _Kp>
  2911. basic_ostream<_CharT, _Traits>&
  2912. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2913. const shuffle_order_engine<_Eng, _Kp>& __x)
  2914. {
  2915. __save_flags<_CharT, _Traits> __lx(__os);
  2916. __os.flags(ios_base::dec | ios_base::left);
  2917. _CharT __sp = __os.widen(' ');
  2918. __os.fill(__sp);
  2919. __os << __x.__e_ << __sp << __x._V_[0];
  2920. for (size_t __i = 1; __i < _Kp; ++__i)
  2921. __os << __sp << __x._V_[__i];
  2922. return __os << __sp << __x._Y_;
  2923. }
  2924. template <class _CharT, class _Traits,
  2925. class _Eng, size_t _Kp>
  2926. basic_istream<_CharT, _Traits>&
  2927. operator>>(basic_istream<_CharT, _Traits>& __is,
  2928. shuffle_order_engine<_Eng, _Kp>& __x)
  2929. {
  2930. typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
  2931. __save_flags<_CharT, _Traits> __lx(__is);
  2932. __is.flags(ios_base::dec | ios_base::skipws);
  2933. _Eng __e;
  2934. result_type _Vp[_Kp+1];
  2935. __is >> __e;
  2936. for (size_t __i = 0; __i < _Kp+1; ++__i)
  2937. __is >> _Vp[__i];
  2938. if (!__is.fail())
  2939. {
  2940. __x.__e_ = __e;
  2941. for (size_t __i = 0; __i < _Kp; ++__i)
  2942. __x._V_[__i] = _Vp[__i];
  2943. __x._Y_ = _Vp[_Kp];
  2944. }
  2945. return __is;
  2946. }
  2947. typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
  2948. // random_device
  2949. class _LIBCPP_TYPE_VIS random_device
  2950. {
  2951. #ifdef _LIBCPP_USING_DEV_RANDOM
  2952. int __f_;
  2953. #endif // defined(_LIBCPP_USING_DEV_RANDOM)
  2954. public:
  2955. // types
  2956. typedef unsigned result_type;
  2957. // generator characteristics
  2958. static _LIBCPP_CONSTEXPR const result_type _Min = 0;
  2959. static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
  2960. _LIBCPP_INLINE_VISIBILITY
  2961. static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
  2962. _LIBCPP_INLINE_VISIBILITY
  2963. static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
  2964. // constructors
  2965. explicit random_device(const string& __token = "/dev/urandom");
  2966. ~random_device();
  2967. // generating functions
  2968. result_type operator()();
  2969. // property functions
  2970. double entropy() const _NOEXCEPT;
  2971. private:
  2972. // no copy functions
  2973. random_device(const random_device&); // = delete;
  2974. random_device& operator=(const random_device&); // = delete;
  2975. };
  2976. // seed_seq
  2977. class _LIBCPP_TYPE_VIS_ONLY seed_seq
  2978. {
  2979. public:
  2980. // types
  2981. typedef uint32_t result_type;
  2982. private:
  2983. vector<result_type> __v_;
  2984. template<class _InputIterator>
  2985. void init(_InputIterator __first, _InputIterator __last);
  2986. public:
  2987. // constructors
  2988. _LIBCPP_INLINE_VISIBILITY
  2989. seed_seq() _NOEXCEPT {}
  2990. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  2991. template<class _Tp>
  2992. _LIBCPP_INLINE_VISIBILITY
  2993. seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
  2994. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  2995. template<class _InputIterator>
  2996. _LIBCPP_INLINE_VISIBILITY
  2997. seed_seq(_InputIterator __first, _InputIterator __last)
  2998. {init(__first, __last);}
  2999. // generating functions
  3000. template<class _RandomAccessIterator>
  3001. void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
  3002. // property functions
  3003. _LIBCPP_INLINE_VISIBILITY
  3004. size_t size() const _NOEXCEPT {return __v_.size();}
  3005. template<class _OutputIterator>
  3006. _LIBCPP_INLINE_VISIBILITY
  3007. void param(_OutputIterator __dest) const
  3008. {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
  3009. private:
  3010. // no copy functions
  3011. seed_seq(const seed_seq&); // = delete;
  3012. void operator=(const seed_seq&); // = delete;
  3013. _LIBCPP_INLINE_VISIBILITY
  3014. static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
  3015. };
  3016. template<class _InputIterator>
  3017. void
  3018. seed_seq::init(_InputIterator __first, _InputIterator __last)
  3019. {
  3020. for (_InputIterator __s = __first; __s != __last; ++__s)
  3021. __v_.push_back(*__s & 0xFFFFFFFF);
  3022. }
  3023. template<class _RandomAccessIterator>
  3024. void
  3025. seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
  3026. {
  3027. if (__first != __last)
  3028. {
  3029. _VSTD::fill(__first, __last, 0x8b8b8b8b);
  3030. const size_t __n = static_cast<size_t>(__last - __first);
  3031. const size_t __s = __v_.size();
  3032. const size_t __t = (__n >= 623) ? 11
  3033. : (__n >= 68) ? 7
  3034. : (__n >= 39) ? 5
  3035. : (__n >= 7) ? 3
  3036. : (__n - 1) / 2;
  3037. const size_t __p = (__n - __t) / 2;
  3038. const size_t __q = __p + __t;
  3039. const size_t __m = _VSTD::max(__s + 1, __n);
  3040. // __k = 0;
  3041. {
  3042. result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
  3043. ^ __first[__n - 1]);
  3044. __first[__p] += __r;
  3045. __r += __s;
  3046. __first[__q] += __r;
  3047. __first[0] = __r;
  3048. }
  3049. for (size_t __k = 1; __k <= __s; ++__k)
  3050. {
  3051. const size_t __kmodn = __k % __n;
  3052. const size_t __kpmodn = (__k + __p) % __n;
  3053. result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
  3054. ^ __first[(__k - 1) % __n]);
  3055. __first[__kpmodn] += __r;
  3056. __r += __kmodn + __v_[__k-1];
  3057. __first[(__k + __q) % __n] += __r;
  3058. __first[__kmodn] = __r;
  3059. }
  3060. for (size_t __k = __s + 1; __k < __m; ++__k)
  3061. {
  3062. const size_t __kmodn = __k % __n;
  3063. const size_t __kpmodn = (__k + __p) % __n;
  3064. result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
  3065. ^ __first[(__k - 1) % __n]);
  3066. __first[__kpmodn] += __r;
  3067. __r += __kmodn;
  3068. __first[(__k + __q) % __n] += __r;
  3069. __first[__kmodn] = __r;
  3070. }
  3071. for (size_t __k = __m; __k < __m + __n; ++__k)
  3072. {
  3073. const size_t __kmodn = __k % __n;
  3074. const size_t __kpmodn = (__k + __p) % __n;
  3075. result_type __r = 1566083941 * _Tp(__first[__kmodn] +
  3076. __first[__kpmodn] +
  3077. __first[(__k - 1) % __n]);
  3078. __first[__kpmodn] ^= __r;
  3079. __r -= __kmodn;
  3080. __first[(__k + __q) % __n] ^= __r;
  3081. __first[__kmodn] = __r;
  3082. }
  3083. }
  3084. }
  3085. // generate_canonical
  3086. template<class _RealType, size_t __bits, class _URNG>
  3087. _RealType
  3088. generate_canonical(_URNG& __g)
  3089. {
  3090. const size_t _Dt = numeric_limits<_RealType>::digits;
  3091. const size_t __b = _Dt < __bits ? _Dt : __bits;
  3092. #ifdef _LIBCPP_HAS_NO_CONSTEXPR
  3093. const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
  3094. #else
  3095. const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
  3096. #endif
  3097. const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
  3098. const _RealType _Rp = _URNG::max() - _URNG::min() + _RealType(1);
  3099. _RealType __base = _Rp;
  3100. _RealType _Sp = __g() - _URNG::min();
  3101. for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp)
  3102. _Sp += (__g() - _URNG::min()) * __base;
  3103. return _Sp / __base;
  3104. }
  3105. // uniform_int_distribution
  3106. // in <algorithm>
  3107. template <class _CharT, class _Traits, class _IT>
  3108. basic_ostream<_CharT, _Traits>&
  3109. operator<<(basic_ostream<_CharT, _Traits>& __os,
  3110. const uniform_int_distribution<_IT>& __x)
  3111. {
  3112. __save_flags<_CharT, _Traits> __lx(__os);
  3113. __os.flags(ios_base::dec | ios_base::left);
  3114. _CharT __sp = __os.widen(' ');
  3115. __os.fill(__sp);
  3116. return __os << __x.a() << __sp << __x.b();
  3117. }
  3118. template <class _CharT, class _Traits, class _IT>
  3119. basic_istream<_CharT, _Traits>&
  3120. operator>>(basic_istream<_CharT, _Traits>& __is,
  3121. uniform_int_distribution<_IT>& __x)
  3122. {
  3123. typedef uniform_int_distribution<_IT> _Eng;
  3124. typedef typename _Eng::result_type result_type;
  3125. typedef typename _Eng::param_type param_type;
  3126. __save_flags<_CharT, _Traits> __lx(__is);
  3127. __is.flags(ios_base::dec | ios_base::skipws);
  3128. result_type __a;
  3129. result_type __b;
  3130. __is >> __a >> __b;
  3131. if (!__is.fail())
  3132. __x.param(param_type(__a, __b));
  3133. return __is;
  3134. }
  3135. // uniform_real_distribution
  3136. template<class _RealType = double>
  3137. class _LIBCPP_TYPE_VIS_ONLY uniform_real_distribution
  3138. {
  3139. public:
  3140. // types
  3141. typedef _RealType result_type;
  3142. class _LIBCPP_TYPE_VIS_ONLY param_type
  3143. {
  3144. result_type __a_;
  3145. result_type __b_;
  3146. public:
  3147. typedef uniform_real_distribution distribution_type;
  3148. _LIBCPP_INLINE_VISIBILITY
  3149. explicit param_type(result_type __a = 0,
  3150. result_type __b = 1)
  3151. : __a_(__a), __b_(__b) {}
  3152. _LIBCPP_INLINE_VISIBILITY
  3153. result_type a() const {return __a_;}
  3154. _LIBCPP_INLINE_VISIBILITY
  3155. result_type b() const {return __b_;}
  3156. friend _LIBCPP_INLINE_VISIBILITY
  3157. bool operator==(const param_type& __x, const param_type& __y)
  3158. {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
  3159. friend _LIBCPP_INLINE_VISIBILITY
  3160. bool operator!=(const param_type& __x, const param_type& __y)
  3161. {return !(__x == __y);}
  3162. };
  3163. private:
  3164. param_type __p_;
  3165. public:
  3166. // constructors and reset functions
  3167. _LIBCPP_INLINE_VISIBILITY
  3168. explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
  3169. : __p_(param_type(__a, __b)) {}
  3170. _LIBCPP_INLINE_VISIBILITY
  3171. explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
  3172. _LIBCPP_INLINE_VISIBILITY
  3173. void reset() {}
  3174. // generating functions
  3175. template<class _URNG>
  3176. _LIBCPP_INLINE_VISIBILITY
  3177. result_type operator()(_URNG& __g)
  3178. {return (*this)(__g, __p_);}
  3179. template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
  3180. // property functions
  3181. _LIBCPP_INLINE_VISIBILITY
  3182. result_type a() const {return __p_.a();}
  3183. _LIBCPP_INLINE_VISIBILITY
  3184. result_type b() const {return __p_.b();}
  3185. _LIBCPP_INLINE_VISIBILITY
  3186. param_type param() const {return __p_;}
  3187. _LIBCPP_INLINE_VISIBILITY
  3188. void param(const param_type& __p) {__p_ = __p;}
  3189. _LIBCPP_INLINE_VISIBILITY
  3190. result_type min() const {return a();}
  3191. _LIBCPP_INLINE_VISIBILITY
  3192. result_type max() const {return b();}
  3193. friend _LIBCPP_INLINE_VISIBILITY
  3194. bool operator==(const uniform_real_distribution& __x,
  3195. const uniform_real_distribution& __y)
  3196. {return __x.__p_ == __y.__p_;}
  3197. friend _LIBCPP_INLINE_VISIBILITY
  3198. bool operator!=(const uniform_real_distribution& __x,
  3199. const uniform_real_distribution& __y)
  3200. {return !(__x == __y);}
  3201. };
  3202. template<class _RealType>
  3203. template<class _URNG>
  3204. inline
  3205. typename uniform_real_distribution<_RealType>::result_type
  3206. uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  3207. {
  3208. return (__p.b() - __p.a())
  3209. * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
  3210. + __p.a();
  3211. }
  3212. template <class _CharT, class _Traits, class _RT>
  3213. basic_ostream<_CharT, _Traits>&
  3214. operator<<(basic_ostream<_CharT, _Traits>& __os,
  3215. const uniform_real_distribution<_RT>& __x)
  3216. {
  3217. __save_flags<_CharT, _Traits> __lx(__os);
  3218. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  3219. ios_base::scientific);
  3220. _CharT __sp = __os.widen(' ');
  3221. __os.fill(__sp);
  3222. return __os << __x.a() << __sp << __x.b();
  3223. }
  3224. template <class _CharT, class _Traits, class _RT>
  3225. basic_istream<_CharT, _Traits>&
  3226. operator>>(basic_istream<_CharT, _Traits>& __is,
  3227. uniform_real_distribution<_RT>& __x)
  3228. {
  3229. typedef uniform_real_distribution<_RT> _Eng;
  3230. typedef typename _Eng::result_type result_type;
  3231. typedef typename _Eng::param_type param_type;
  3232. __save_flags<_CharT, _Traits> __lx(__is);
  3233. __is.flags(ios_base::dec | ios_base::skipws);
  3234. result_type __a;
  3235. result_type __b;
  3236. __is >> __a >> __b;
  3237. if (!__is.fail())
  3238. __x.param(param_type(__a, __b));
  3239. return __is;
  3240. }
  3241. // bernoulli_distribution
  3242. class _LIBCPP_TYPE_VIS_ONLY bernoulli_distribution
  3243. {
  3244. public:
  3245. // types
  3246. typedef bool result_type;
  3247. class _LIBCPP_TYPE_VIS_ONLY param_type
  3248. {
  3249. double __p_;
  3250. public:
  3251. typedef bernoulli_distribution distribution_type;
  3252. _LIBCPP_INLINE_VISIBILITY
  3253. explicit param_type(double __p = 0.5) : __p_(__p) {}
  3254. _LIBCPP_INLINE_VISIBILITY
  3255. double p() const {return __p_;}
  3256. friend _LIBCPP_INLINE_VISIBILITY
  3257. bool operator==(const param_type& __x, const param_type& __y)
  3258. {return __x.__p_ == __y.__p_;}
  3259. friend _LIBCPP_INLINE_VISIBILITY
  3260. bool operator!=(const param_type& __x, const param_type& __y)
  3261. {return !(__x == __y);}
  3262. };
  3263. private:
  3264. param_type __p_;
  3265. public:
  3266. // constructors and reset functions
  3267. _LIBCPP_INLINE_VISIBILITY
  3268. explicit bernoulli_distribution(double __p = 0.5)
  3269. : __p_(param_type(__p)) {}
  3270. _LIBCPP_INLINE_VISIBILITY
  3271. explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
  3272. _LIBCPP_INLINE_VISIBILITY
  3273. void reset() {}
  3274. // generating functions
  3275. template<class _URNG>
  3276. _LIBCPP_INLINE_VISIBILITY
  3277. result_type operator()(_URNG& __g)
  3278. {return (*this)(__g, __p_);}
  3279. template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
  3280. // property functions
  3281. _LIBCPP_INLINE_VISIBILITY
  3282. double p() const {return __p_.p();}
  3283. _LIBCPP_INLINE_VISIBILITY
  3284. param_type param() const {return __p_;}
  3285. _LIBCPP_INLINE_VISIBILITY
  3286. void param(const param_type& __p) {__p_ = __p;}
  3287. _LIBCPP_INLINE_VISIBILITY
  3288. result_type min() const {return false;}
  3289. _LIBCPP_INLINE_VISIBILITY
  3290. result_type max() const {return true;}
  3291. friend _LIBCPP_INLINE_VISIBILITY
  3292. bool operator==(const bernoulli_distribution& __x,
  3293. const bernoulli_distribution& __y)
  3294. {return __x.__p_ == __y.__p_;}
  3295. friend _LIBCPP_INLINE_VISIBILITY
  3296. bool operator!=(const bernoulli_distribution& __x,
  3297. const bernoulli_distribution& __y)
  3298. {return !(__x == __y);}
  3299. };
  3300. template<class _URNG>
  3301. inline
  3302. bernoulli_distribution::result_type
  3303. bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
  3304. {
  3305. uniform_real_distribution<double> __gen;
  3306. return __gen(__g) < __p.p();
  3307. }
  3308. template <class _CharT, class _Traits>
  3309. basic_ostream<_CharT, _Traits>&
  3310. operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
  3311. {
  3312. __save_flags<_CharT, _Traits> __lx(__os);
  3313. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  3314. ios_base::scientific);
  3315. _CharT __sp = __os.widen(' ');
  3316. __os.fill(__sp);
  3317. return __os << __x.p();
  3318. }
  3319. template <class _CharT, class _Traits>
  3320. basic_istream<_CharT, _Traits>&
  3321. operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
  3322. {
  3323. typedef bernoulli_distribution _Eng;
  3324. typedef typename _Eng::param_type param_type;
  3325. __save_flags<_CharT, _Traits> __lx(__is);
  3326. __is.flags(ios_base::dec | ios_base::skipws);
  3327. double __p;
  3328. __is >> __p;
  3329. if (!__is.fail())
  3330. __x.param(param_type(__p));
  3331. return __is;
  3332. }
  3333. // binomial_distribution
  3334. template<class _IntType = int>
  3335. class _LIBCPP_TYPE_VIS_ONLY binomial_distribution
  3336. {
  3337. public:
  3338. // types
  3339. typedef _IntType result_type;
  3340. class _LIBCPP_TYPE_VIS_ONLY param_type
  3341. {
  3342. result_type __t_;
  3343. double __p_;
  3344. double __pr_;
  3345. double __odds_ratio_;
  3346. result_type __r0_;
  3347. public:
  3348. typedef binomial_distribution distribution_type;
  3349. explicit param_type(result_type __t = 1, double __p = 0.5);
  3350. _LIBCPP_INLINE_VISIBILITY
  3351. result_type t() const {return __t_;}
  3352. _LIBCPP_INLINE_VISIBILITY
  3353. double p() const {return __p_;}
  3354. friend _LIBCPP_INLINE_VISIBILITY
  3355. bool operator==(const param_type& __x, const param_type& __y)
  3356. {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
  3357. friend _LIBCPP_INLINE_VISIBILITY
  3358. bool operator!=(const param_type& __x, const param_type& __y)
  3359. {return !(__x == __y);}
  3360. friend class binomial_distribution;
  3361. };
  3362. private:
  3363. param_type __p_;
  3364. public:
  3365. // constructors and reset functions
  3366. _LIBCPP_INLINE_VISIBILITY
  3367. explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
  3368. : __p_(param_type(__t, __p)) {}
  3369. _LIBCPP_INLINE_VISIBILITY
  3370. explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
  3371. _LIBCPP_INLINE_VISIBILITY
  3372. void reset() {}
  3373. // generating functions
  3374. template<class _URNG>
  3375. _LIBCPP_INLINE_VISIBILITY
  3376. result_type operator()(_URNG& __g)
  3377. {return (*this)(__g, __p_);}
  3378. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  3379. // property functions
  3380. _LIBCPP_INLINE_VISIBILITY
  3381. result_type t() const {return __p_.t();}
  3382. _LIBCPP_INLINE_VISIBILITY
  3383. double p() const {return __p_.p();}
  3384. _LIBCPP_INLINE_VISIBILITY
  3385. param_type param() const {return __p_;}
  3386. _LIBCPP_INLINE_VISIBILITY
  3387. void param(const param_type& __p) {__p_ = __p;}
  3388. _LIBCPP_INLINE_VISIBILITY
  3389. result_type min() const {return 0;}
  3390. _LIBCPP_INLINE_VISIBILITY
  3391. result_type max() const {return t();}
  3392. friend _LIBCPP_INLINE_VISIBILITY
  3393. bool operator==(const binomial_distribution& __x,
  3394. const binomial_distribution& __y)
  3395. {return __x.__p_ == __y.__p_;}
  3396. friend _LIBCPP_INLINE_VISIBILITY
  3397. bool operator!=(const binomial_distribution& __x,
  3398. const binomial_distribution& __y)
  3399. {return !(__x == __y);}
  3400. };
  3401. template<class _IntType>
  3402. binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
  3403. : __t_(__t), __p_(__p)
  3404. {
  3405. if (0 < __p_ && __p_ < 1)
  3406. {
  3407. __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
  3408. __pr_ = _VSTD::exp(_VSTD::lgamma(__t_ + 1.) - _VSTD::lgamma(__r0_ + 1.) -
  3409. _VSTD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
  3410. (__t_ - __r0_) * _VSTD::log(1 - __p_));
  3411. __odds_ratio_ = __p_ / (1 - __p_);
  3412. }
  3413. }
  3414. // Reference: Kemp, C.D. (1986). `A modal method for generating binomial
  3415. // variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
  3416. template<class _IntType>
  3417. template<class _URNG>
  3418. _IntType
  3419. binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
  3420. {
  3421. if (__pr.__t_ == 0 || __pr.__p_ == 0)
  3422. return 0;
  3423. if (__pr.__p_ == 1)
  3424. return __pr.__t_;
  3425. uniform_real_distribution<double> __gen;
  3426. double __u = __gen(__g) - __pr.__pr_;
  3427. if (__u < 0)
  3428. return __pr.__r0_;
  3429. double __pu = __pr.__pr_;
  3430. double __pd = __pu;
  3431. result_type __ru = __pr.__r0_;
  3432. result_type __rd = __ru;
  3433. while (true)
  3434. {
  3435. if (__rd >= 1)
  3436. {
  3437. __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
  3438. __u -= __pd;
  3439. if (__u < 0)
  3440. return __rd - 1;
  3441. }
  3442. if ( __rd != 0 )
  3443. --__rd;
  3444. ++__ru;
  3445. if (__ru <= __pr.__t_)
  3446. {
  3447. __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
  3448. __u -= __pu;
  3449. if (__u < 0)
  3450. return __ru;
  3451. }
  3452. }
  3453. }
  3454. template <class _CharT, class _Traits, class _IntType>
  3455. basic_ostream<_CharT, _Traits>&
  3456. operator<<(basic_ostream<_CharT, _Traits>& __os,
  3457. const binomial_distribution<_IntType>& __x)
  3458. {
  3459. __save_flags<_CharT, _Traits> __lx(__os);
  3460. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  3461. ios_base::scientific);
  3462. _CharT __sp = __os.widen(' ');
  3463. __os.fill(__sp);
  3464. return __os << __x.t() << __sp << __x.p();
  3465. }
  3466. template <class _CharT, class _Traits, class _IntType>
  3467. basic_istream<_CharT, _Traits>&
  3468. operator>>(basic_istream<_CharT, _Traits>& __is,
  3469. binomial_distribution<_IntType>& __x)
  3470. {
  3471. typedef binomial_distribution<_IntType> _Eng;
  3472. typedef typename _Eng::result_type result_type;
  3473. typedef typename _Eng::param_type param_type;
  3474. __save_flags<_CharT, _Traits> __lx(__is);
  3475. __is.flags(ios_base::dec | ios_base::skipws);
  3476. result_type __t;
  3477. double __p;
  3478. __is >> __t >> __p;
  3479. if (!__is.fail())
  3480. __x.param(param_type(__t, __p));
  3481. return __is;
  3482. }
  3483. // exponential_distribution
  3484. template<class _RealType = double>
  3485. class _LIBCPP_TYPE_VIS_ONLY exponential_distribution
  3486. {
  3487. public:
  3488. // types
  3489. typedef _RealType result_type;
  3490. class _LIBCPP_TYPE_VIS_ONLY param_type
  3491. {
  3492. result_type __lambda_;
  3493. public:
  3494. typedef exponential_distribution distribution_type;
  3495. _LIBCPP_INLINE_VISIBILITY
  3496. explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
  3497. _LIBCPP_INLINE_VISIBILITY
  3498. result_type lambda() const {return __lambda_;}
  3499. friend _LIBCPP_INLINE_VISIBILITY
  3500. bool operator==(const param_type& __x, const param_type& __y)
  3501. {return __x.__lambda_ == __y.__lambda_;}
  3502. friend _LIBCPP_INLINE_VISIBILITY
  3503. bool operator!=(const param_type& __x, const param_type& __y)
  3504. {return !(__x == __y);}
  3505. };
  3506. private:
  3507. param_type __p_;
  3508. public:
  3509. // constructors and reset functions
  3510. _LIBCPP_INLINE_VISIBILITY
  3511. explicit exponential_distribution(result_type __lambda = 1)
  3512. : __p_(param_type(__lambda)) {}
  3513. _LIBCPP_INLINE_VISIBILITY
  3514. explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
  3515. _LIBCPP_INLINE_VISIBILITY
  3516. void reset() {}
  3517. // generating functions
  3518. template<class _URNG>
  3519. _LIBCPP_INLINE_VISIBILITY
  3520. result_type operator()(_URNG& __g)
  3521. {return (*this)(__g, __p_);}
  3522. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  3523. // property functions
  3524. _LIBCPP_INLINE_VISIBILITY
  3525. result_type lambda() const {return __p_.lambda();}
  3526. _LIBCPP_INLINE_VISIBILITY
  3527. param_type param() const {return __p_;}
  3528. _LIBCPP_INLINE_VISIBILITY
  3529. void param(const param_type& __p) {__p_ = __p;}
  3530. _LIBCPP_INLINE_VISIBILITY
  3531. result_type min() const {return 0;}
  3532. _LIBCPP_INLINE_VISIBILITY
  3533. result_type max() const {return numeric_limits<result_type>::infinity();}
  3534. friend _LIBCPP_INLINE_VISIBILITY
  3535. bool operator==(const exponential_distribution& __x,
  3536. const exponential_distribution& __y)
  3537. {return __x.__p_ == __y.__p_;}
  3538. friend _LIBCPP_INLINE_VISIBILITY
  3539. bool operator!=(const exponential_distribution& __x,
  3540. const exponential_distribution& __y)
  3541. {return !(__x == __y);}
  3542. };
  3543. template <class _RealType>
  3544. template<class _URNG>
  3545. _RealType
  3546. exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  3547. {
  3548. return -_VSTD::log
  3549. (
  3550. result_type(1) -
  3551. _VSTD::generate_canonical<result_type,
  3552. numeric_limits<result_type>::digits>(__g)
  3553. )
  3554. / __p.lambda();
  3555. }
  3556. template <class _CharT, class _Traits, class _RealType>
  3557. basic_ostream<_CharT, _Traits>&
  3558. operator<<(basic_ostream<_CharT, _Traits>& __os,
  3559. const exponential_distribution<_RealType>& __x)
  3560. {
  3561. __save_flags<_CharT, _Traits> __lx(__os);
  3562. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  3563. ios_base::scientific);
  3564. return __os << __x.lambda();
  3565. }
  3566. template <class _CharT, class _Traits, class _RealType>
  3567. basic_istream<_CharT, _Traits>&
  3568. operator>>(basic_istream<_CharT, _Traits>& __is,
  3569. exponential_distribution<_RealType>& __x)
  3570. {
  3571. typedef exponential_distribution<_RealType> _Eng;
  3572. typedef typename _Eng::result_type result_type;
  3573. typedef typename _Eng::param_type param_type;
  3574. __save_flags<_CharT, _Traits> __lx(__is);
  3575. __is.flags(ios_base::dec | ios_base::skipws);
  3576. result_type __lambda;
  3577. __is >> __lambda;
  3578. if (!__is.fail())
  3579. __x.param(param_type(__lambda));
  3580. return __is;
  3581. }
  3582. // normal_distribution
  3583. template<class _RealType = double>
  3584. class _LIBCPP_TYPE_VIS_ONLY normal_distribution
  3585. {
  3586. public:
  3587. // types
  3588. typedef _RealType result_type;
  3589. class _LIBCPP_TYPE_VIS_ONLY param_type
  3590. {
  3591. result_type __mean_;
  3592. result_type __stddev_;
  3593. public:
  3594. typedef normal_distribution distribution_type;
  3595. _LIBCPP_INLINE_VISIBILITY
  3596. explicit param_type(result_type __mean = 0, result_type __stddev = 1)
  3597. : __mean_(__mean), __stddev_(__stddev) {}
  3598. _LIBCPP_INLINE_VISIBILITY
  3599. result_type mean() const {return __mean_;}
  3600. _LIBCPP_INLINE_VISIBILITY
  3601. result_type stddev() const {return __stddev_;}
  3602. friend _LIBCPP_INLINE_VISIBILITY
  3603. bool operator==(const param_type& __x, const param_type& __y)
  3604. {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
  3605. friend _LIBCPP_INLINE_VISIBILITY
  3606. bool operator!=(const param_type& __x, const param_type& __y)
  3607. {return !(__x == __y);}
  3608. };
  3609. private:
  3610. param_type __p_;
  3611. result_type _V_;
  3612. bool _V_hot_;
  3613. public:
  3614. // constructors and reset functions
  3615. _LIBCPP_INLINE_VISIBILITY
  3616. explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
  3617. : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
  3618. _LIBCPP_INLINE_VISIBILITY
  3619. explicit normal_distribution(const param_type& __p)
  3620. : __p_(__p), _V_hot_(false) {}
  3621. _LIBCPP_INLINE_VISIBILITY
  3622. void reset() {_V_hot_ = false;}
  3623. // generating functions
  3624. template<class _URNG>
  3625. _LIBCPP_INLINE_VISIBILITY
  3626. result_type operator()(_URNG& __g)
  3627. {return (*this)(__g, __p_);}
  3628. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  3629. // property functions
  3630. _LIBCPP_INLINE_VISIBILITY
  3631. result_type mean() const {return __p_.mean();}
  3632. _LIBCPP_INLINE_VISIBILITY
  3633. result_type stddev() const {return __p_.stddev();}
  3634. _LIBCPP_INLINE_VISIBILITY
  3635. param_type param() const {return __p_;}
  3636. _LIBCPP_INLINE_VISIBILITY
  3637. void param(const param_type& __p) {__p_ = __p;}
  3638. _LIBCPP_INLINE_VISIBILITY
  3639. result_type min() const {return -numeric_limits<result_type>::infinity();}
  3640. _LIBCPP_INLINE_VISIBILITY
  3641. result_type max() const {return numeric_limits<result_type>::infinity();}
  3642. friend _LIBCPP_INLINE_VISIBILITY
  3643. bool operator==(const normal_distribution& __x,
  3644. const normal_distribution& __y)
  3645. {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
  3646. (!__x._V_hot_ || __x._V_ == __y._V_);}
  3647. friend _LIBCPP_INLINE_VISIBILITY
  3648. bool operator!=(const normal_distribution& __x,
  3649. const normal_distribution& __y)
  3650. {return !(__x == __y);}
  3651. template <class _CharT, class _Traits, class _RT>
  3652. friend
  3653. basic_ostream<_CharT, _Traits>&
  3654. operator<<(basic_ostream<_CharT, _Traits>& __os,
  3655. const normal_distribution<_RT>& __x);
  3656. template <class _CharT, class _Traits, class _RT>
  3657. friend
  3658. basic_istream<_CharT, _Traits>&
  3659. operator>>(basic_istream<_CharT, _Traits>& __is,
  3660. normal_distribution<_RT>& __x);
  3661. };
  3662. template <class _RealType>
  3663. template<class _URNG>
  3664. _RealType
  3665. normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  3666. {
  3667. result_type _Up;
  3668. if (_V_hot_)
  3669. {
  3670. _V_hot_ = false;
  3671. _Up = _V_;
  3672. }
  3673. else
  3674. {
  3675. uniform_real_distribution<result_type> _Uni(-1, 1);
  3676. result_type __u;
  3677. result_type __v;
  3678. result_type __s;
  3679. do
  3680. {
  3681. __u = _Uni(__g);
  3682. __v = _Uni(__g);
  3683. __s = __u * __u + __v * __v;
  3684. } while (__s > 1 || __s == 0);
  3685. result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
  3686. _V_ = __v * _Fp;
  3687. _V_hot_ = true;
  3688. _Up = __u * _Fp;
  3689. }
  3690. return _Up * __p.stddev() + __p.mean();
  3691. }
  3692. template <class _CharT, class _Traits, class _RT>
  3693. basic_ostream<_CharT, _Traits>&
  3694. operator<<(basic_ostream<_CharT, _Traits>& __os,
  3695. const normal_distribution<_RT>& __x)
  3696. {
  3697. __save_flags<_CharT, _Traits> __lx(__os);
  3698. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  3699. ios_base::scientific);
  3700. _CharT __sp = __os.widen(' ');
  3701. __os.fill(__sp);
  3702. __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
  3703. if (__x._V_hot_)
  3704. __os << __sp << __x._V_;
  3705. return __os;
  3706. }
  3707. template <class _CharT, class _Traits, class _RT>
  3708. basic_istream<_CharT, _Traits>&
  3709. operator>>(basic_istream<_CharT, _Traits>& __is,
  3710. normal_distribution<_RT>& __x)
  3711. {
  3712. typedef normal_distribution<_RT> _Eng;
  3713. typedef typename _Eng::result_type result_type;
  3714. typedef typename _Eng::param_type param_type;
  3715. __save_flags<_CharT, _Traits> __lx(__is);
  3716. __is.flags(ios_base::dec | ios_base::skipws);
  3717. result_type __mean;
  3718. result_type __stddev;
  3719. result_type _Vp = 0;
  3720. bool _V_hot = false;
  3721. __is >> __mean >> __stddev >> _V_hot;
  3722. if (_V_hot)
  3723. __is >> _Vp;
  3724. if (!__is.fail())
  3725. {
  3726. __x.param(param_type(__mean, __stddev));
  3727. __x._V_hot_ = _V_hot;
  3728. __x._V_ = _Vp;
  3729. }
  3730. return __is;
  3731. }
  3732. // lognormal_distribution
  3733. template<class _RealType = double>
  3734. class _LIBCPP_TYPE_VIS_ONLY lognormal_distribution
  3735. {
  3736. public:
  3737. // types
  3738. typedef _RealType result_type;
  3739. class _LIBCPP_TYPE_VIS_ONLY param_type
  3740. {
  3741. normal_distribution<result_type> __nd_;
  3742. public:
  3743. typedef lognormal_distribution distribution_type;
  3744. _LIBCPP_INLINE_VISIBILITY
  3745. explicit param_type(result_type __m = 0, result_type __s = 1)
  3746. : __nd_(__m, __s) {}
  3747. _LIBCPP_INLINE_VISIBILITY
  3748. result_type m() const {return __nd_.mean();}
  3749. _LIBCPP_INLINE_VISIBILITY
  3750. result_type s() const {return __nd_.stddev();}
  3751. friend _LIBCPP_INLINE_VISIBILITY
  3752. bool operator==(const param_type& __x, const param_type& __y)
  3753. {return __x.__nd_ == __y.__nd_;}
  3754. friend _LIBCPP_INLINE_VISIBILITY
  3755. bool operator!=(const param_type& __x, const param_type& __y)
  3756. {return !(__x == __y);}
  3757. friend class lognormal_distribution;
  3758. template <class _CharT, class _Traits, class _RT>
  3759. friend
  3760. basic_ostream<_CharT, _Traits>&
  3761. operator<<(basic_ostream<_CharT, _Traits>& __os,
  3762. const lognormal_distribution<_RT>& __x);
  3763. template <class _CharT, class _Traits, class _RT>
  3764. friend
  3765. basic_istream<_CharT, _Traits>&
  3766. operator>>(basic_istream<_CharT, _Traits>& __is,
  3767. lognormal_distribution<_RT>& __x);
  3768. };
  3769. private:
  3770. param_type __p_;
  3771. public:
  3772. // constructor and reset functions
  3773. _LIBCPP_INLINE_VISIBILITY
  3774. explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
  3775. : __p_(param_type(__m, __s)) {}
  3776. _LIBCPP_INLINE_VISIBILITY
  3777. explicit lognormal_distribution(const param_type& __p)
  3778. : __p_(__p) {}
  3779. _LIBCPP_INLINE_VISIBILITY
  3780. void reset() {__p_.__nd_.reset();}
  3781. // generating functions
  3782. template<class _URNG>
  3783. _LIBCPP_INLINE_VISIBILITY
  3784. result_type operator()(_URNG& __g)
  3785. {return (*this)(__g, __p_);}
  3786. template<class _URNG>
  3787. _LIBCPP_INLINE_VISIBILITY
  3788. result_type operator()(_URNG& __g, const param_type& __p)
  3789. {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
  3790. // property functions
  3791. _LIBCPP_INLINE_VISIBILITY
  3792. result_type m() const {return __p_.m();}
  3793. _LIBCPP_INLINE_VISIBILITY
  3794. result_type s() const {return __p_.s();}
  3795. _LIBCPP_INLINE_VISIBILITY
  3796. param_type param() const {return __p_;}
  3797. _LIBCPP_INLINE_VISIBILITY
  3798. void param(const param_type& __p) {__p_ = __p;}
  3799. _LIBCPP_INLINE_VISIBILITY
  3800. result_type min() const {return 0;}
  3801. _LIBCPP_INLINE_VISIBILITY
  3802. result_type max() const {return numeric_limits<result_type>::infinity();}
  3803. friend _LIBCPP_INLINE_VISIBILITY
  3804. bool operator==(const lognormal_distribution& __x,
  3805. const lognormal_distribution& __y)
  3806. {return __x.__p_ == __y.__p_;}
  3807. friend _LIBCPP_INLINE_VISIBILITY
  3808. bool operator!=(const lognormal_distribution& __x,
  3809. const lognormal_distribution& __y)
  3810. {return !(__x == __y);}
  3811. template <class _CharT, class _Traits, class _RT>
  3812. friend
  3813. basic_ostream<_CharT, _Traits>&
  3814. operator<<(basic_ostream<_CharT, _Traits>& __os,
  3815. const lognormal_distribution<_RT>& __x);
  3816. template <class _CharT, class _Traits, class _RT>
  3817. friend
  3818. basic_istream<_CharT, _Traits>&
  3819. operator>>(basic_istream<_CharT, _Traits>& __is,
  3820. lognormal_distribution<_RT>& __x);
  3821. };
  3822. template <class _CharT, class _Traits, class _RT>
  3823. inline _LIBCPP_INLINE_VISIBILITY
  3824. basic_ostream<_CharT, _Traits>&
  3825. operator<<(basic_ostream<_CharT, _Traits>& __os,
  3826. const lognormal_distribution<_RT>& __x)
  3827. {
  3828. return __os << __x.__p_.__nd_;
  3829. }
  3830. template <class _CharT, class _Traits, class _RT>
  3831. inline _LIBCPP_INLINE_VISIBILITY
  3832. basic_istream<_CharT, _Traits>&
  3833. operator>>(basic_istream<_CharT, _Traits>& __is,
  3834. lognormal_distribution<_RT>& __x)
  3835. {
  3836. return __is >> __x.__p_.__nd_;
  3837. }
  3838. // poisson_distribution
  3839. template<class _IntType = int>
  3840. class _LIBCPP_TYPE_VIS_ONLY poisson_distribution
  3841. {
  3842. public:
  3843. // types
  3844. typedef _IntType result_type;
  3845. class _LIBCPP_TYPE_VIS_ONLY param_type
  3846. {
  3847. double __mean_;
  3848. double __s_;
  3849. double __d_;
  3850. double __l_;
  3851. double __omega_;
  3852. double __c0_;
  3853. double __c1_;
  3854. double __c2_;
  3855. double __c3_;
  3856. double __c_;
  3857. public:
  3858. typedef poisson_distribution distribution_type;
  3859. explicit param_type(double __mean = 1.0);
  3860. _LIBCPP_INLINE_VISIBILITY
  3861. double mean() const {return __mean_;}
  3862. friend _LIBCPP_INLINE_VISIBILITY
  3863. bool operator==(const param_type& __x, const param_type& __y)
  3864. {return __x.__mean_ == __y.__mean_;}
  3865. friend _LIBCPP_INLINE_VISIBILITY
  3866. bool operator!=(const param_type& __x, const param_type& __y)
  3867. {return !(__x == __y);}
  3868. friend class poisson_distribution;
  3869. };
  3870. private:
  3871. param_type __p_;
  3872. public:
  3873. // constructors and reset functions
  3874. _LIBCPP_INLINE_VISIBILITY
  3875. explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
  3876. _LIBCPP_INLINE_VISIBILITY
  3877. explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
  3878. _LIBCPP_INLINE_VISIBILITY
  3879. void reset() {}
  3880. // generating functions
  3881. template<class _URNG>
  3882. _LIBCPP_INLINE_VISIBILITY
  3883. result_type operator()(_URNG& __g)
  3884. {return (*this)(__g, __p_);}
  3885. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  3886. // property functions
  3887. _LIBCPP_INLINE_VISIBILITY
  3888. double mean() const {return __p_.mean();}
  3889. _LIBCPP_INLINE_VISIBILITY
  3890. param_type param() const {return __p_;}
  3891. _LIBCPP_INLINE_VISIBILITY
  3892. void param(const param_type& __p) {__p_ = __p;}
  3893. _LIBCPP_INLINE_VISIBILITY
  3894. result_type min() const {return 0;}
  3895. _LIBCPP_INLINE_VISIBILITY
  3896. result_type max() const {return numeric_limits<result_type>::max();}
  3897. friend _LIBCPP_INLINE_VISIBILITY
  3898. bool operator==(const poisson_distribution& __x,
  3899. const poisson_distribution& __y)
  3900. {return __x.__p_ == __y.__p_;}
  3901. friend _LIBCPP_INLINE_VISIBILITY
  3902. bool operator!=(const poisson_distribution& __x,
  3903. const poisson_distribution& __y)
  3904. {return !(__x == __y);}
  3905. };
  3906. template<class _IntType>
  3907. poisson_distribution<_IntType>::param_type::param_type(double __mean)
  3908. : __mean_(__mean)
  3909. {
  3910. if (__mean_ < 10)
  3911. {
  3912. __s_ = 0;
  3913. __d_ = 0;
  3914. __l_ = _VSTD::exp(-__mean_);
  3915. __omega_ = 0;
  3916. __c3_ = 0;
  3917. __c2_ = 0;
  3918. __c1_ = 0;
  3919. __c0_ = 0;
  3920. __c_ = 0;
  3921. }
  3922. else
  3923. {
  3924. __s_ = _VSTD::sqrt(__mean_);
  3925. __d_ = 6 * __mean_ * __mean_;
  3926. __l_ = static_cast<result_type>(__mean_ - 1.1484);
  3927. __omega_ = .3989423 / __s_;
  3928. double __b1_ = .4166667E-1 / __mean_;
  3929. double __b2_ = .3 * __b1_ * __b1_;
  3930. __c3_ = .1428571 * __b1_ * __b2_;
  3931. __c2_ = __b2_ - 15. * __c3_;
  3932. __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
  3933. __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
  3934. __c_ = .1069 / __mean_;
  3935. }
  3936. }
  3937. template <class _IntType>
  3938. template<class _URNG>
  3939. _IntType
  3940. poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
  3941. {
  3942. result_type __x;
  3943. uniform_real_distribution<double> __urd;
  3944. if (__pr.__mean_ < 10)
  3945. {
  3946. __x = 0;
  3947. for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
  3948. __p *= __urd(__urng);
  3949. }
  3950. else
  3951. {
  3952. double __difmuk;
  3953. double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
  3954. double __u;
  3955. if (__g > 0)
  3956. {
  3957. __x = static_cast<result_type>(__g);
  3958. if (__x >= __pr.__l_)
  3959. return __x;
  3960. __difmuk = __pr.__mean_ - __x;
  3961. __u = __urd(__urng);
  3962. if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
  3963. return __x;
  3964. }
  3965. exponential_distribution<double> __edist;
  3966. for (bool __using_exp_dist = false; true; __using_exp_dist = true)
  3967. {
  3968. double __e;
  3969. if (__using_exp_dist || __g < 0)
  3970. {
  3971. double __t;
  3972. do
  3973. {
  3974. __e = __edist(__urng);
  3975. __u = __urd(__urng);
  3976. __u += __u - 1;
  3977. __t = 1.8 + (__u < 0 ? -__e : __e);
  3978. } while (__t <= -.6744);
  3979. __x = __pr.__mean_ + __pr.__s_ * __t;
  3980. __difmuk = __pr.__mean_ - __x;
  3981. __using_exp_dist = true;
  3982. }
  3983. double __px;
  3984. double __py;
  3985. if (__x < 10)
  3986. {
  3987. const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
  3988. 40320, 362880};
  3989. __px = -__pr.__mean_;
  3990. __py = _VSTD::pow(__pr.__mean_, (double)__x) / __fac[__x];
  3991. }
  3992. else
  3993. {
  3994. double __del = .8333333E-1 / __x;
  3995. __del -= 4.8 * __del * __del * __del;
  3996. double __v = __difmuk / __x;
  3997. if (_VSTD::abs(__v) > 0.25)
  3998. __px = __x * _VSTD::log(1 + __v) - __difmuk - __del;
  3999. else
  4000. __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
  4001. __v + .1421878) * __v + -.1661269) * __v + .2000118) *
  4002. __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
  4003. __py = .3989423 / _VSTD::sqrt(__x);
  4004. }
  4005. double __r = (0.5 - __difmuk) / __pr.__s_;
  4006. double __r2 = __r * __r;
  4007. double __fx = -0.5 * __r2;
  4008. double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
  4009. __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
  4010. if (__using_exp_dist)
  4011. {
  4012. if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
  4013. __fy * _VSTD::exp(__fx + __e))
  4014. break;
  4015. }
  4016. else
  4017. {
  4018. if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
  4019. break;
  4020. }
  4021. }
  4022. }
  4023. return __x;
  4024. }
  4025. template <class _CharT, class _Traits, class _IntType>
  4026. basic_ostream<_CharT, _Traits>&
  4027. operator<<(basic_ostream<_CharT, _Traits>& __os,
  4028. const poisson_distribution<_IntType>& __x)
  4029. {
  4030. __save_flags<_CharT, _Traits> __lx(__os);
  4031. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  4032. ios_base::scientific);
  4033. return __os << __x.mean();
  4034. }
  4035. template <class _CharT, class _Traits, class _IntType>
  4036. basic_istream<_CharT, _Traits>&
  4037. operator>>(basic_istream<_CharT, _Traits>& __is,
  4038. poisson_distribution<_IntType>& __x)
  4039. {
  4040. typedef poisson_distribution<_IntType> _Eng;
  4041. typedef typename _Eng::param_type param_type;
  4042. __save_flags<_CharT, _Traits> __lx(__is);
  4043. __is.flags(ios_base::dec | ios_base::skipws);
  4044. double __mean;
  4045. __is >> __mean;
  4046. if (!__is.fail())
  4047. __x.param(param_type(__mean));
  4048. return __is;
  4049. }
  4050. // weibull_distribution
  4051. template<class _RealType = double>
  4052. class _LIBCPP_TYPE_VIS_ONLY weibull_distribution
  4053. {
  4054. public:
  4055. // types
  4056. typedef _RealType result_type;
  4057. class _LIBCPP_TYPE_VIS_ONLY param_type
  4058. {
  4059. result_type __a_;
  4060. result_type __b_;
  4061. public:
  4062. typedef weibull_distribution distribution_type;
  4063. _LIBCPP_INLINE_VISIBILITY
  4064. explicit param_type(result_type __a = 1, result_type __b = 1)
  4065. : __a_(__a), __b_(__b) {}
  4066. _LIBCPP_INLINE_VISIBILITY
  4067. result_type a() const {return __a_;}
  4068. _LIBCPP_INLINE_VISIBILITY
  4069. result_type b() const {return __b_;}
  4070. friend _LIBCPP_INLINE_VISIBILITY
  4071. bool operator==(const param_type& __x, const param_type& __y)
  4072. {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
  4073. friend _LIBCPP_INLINE_VISIBILITY
  4074. bool operator!=(const param_type& __x, const param_type& __y)
  4075. {return !(__x == __y);}
  4076. };
  4077. private:
  4078. param_type __p_;
  4079. public:
  4080. // constructor and reset functions
  4081. _LIBCPP_INLINE_VISIBILITY
  4082. explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
  4083. : __p_(param_type(__a, __b)) {}
  4084. _LIBCPP_INLINE_VISIBILITY
  4085. explicit weibull_distribution(const param_type& __p)
  4086. : __p_(__p) {}
  4087. _LIBCPP_INLINE_VISIBILITY
  4088. void reset() {}
  4089. // generating functions
  4090. template<class _URNG>
  4091. _LIBCPP_INLINE_VISIBILITY
  4092. result_type operator()(_URNG& __g)
  4093. {return (*this)(__g, __p_);}
  4094. template<class _URNG>
  4095. _LIBCPP_INLINE_VISIBILITY
  4096. result_type operator()(_URNG& __g, const param_type& __p)
  4097. {return __p.b() *
  4098. _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
  4099. // property functions
  4100. _LIBCPP_INLINE_VISIBILITY
  4101. result_type a() const {return __p_.a();}
  4102. _LIBCPP_INLINE_VISIBILITY
  4103. result_type b() const {return __p_.b();}
  4104. _LIBCPP_INLINE_VISIBILITY
  4105. param_type param() const {return __p_;}
  4106. _LIBCPP_INLINE_VISIBILITY
  4107. void param(const param_type& __p) {__p_ = __p;}
  4108. _LIBCPP_INLINE_VISIBILITY
  4109. result_type min() const {return 0;}
  4110. _LIBCPP_INLINE_VISIBILITY
  4111. result_type max() const {return numeric_limits<result_type>::infinity();}
  4112. friend _LIBCPP_INLINE_VISIBILITY
  4113. bool operator==(const weibull_distribution& __x,
  4114. const weibull_distribution& __y)
  4115. {return __x.__p_ == __y.__p_;}
  4116. friend _LIBCPP_INLINE_VISIBILITY
  4117. bool operator!=(const weibull_distribution& __x,
  4118. const weibull_distribution& __y)
  4119. {return !(__x == __y);}
  4120. };
  4121. template <class _CharT, class _Traits, class _RT>
  4122. basic_ostream<_CharT, _Traits>&
  4123. operator<<(basic_ostream<_CharT, _Traits>& __os,
  4124. const weibull_distribution<_RT>& __x)
  4125. {
  4126. __save_flags<_CharT, _Traits> __lx(__os);
  4127. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  4128. ios_base::scientific);
  4129. _CharT __sp = __os.widen(' ');
  4130. __os.fill(__sp);
  4131. __os << __x.a() << __sp << __x.b();
  4132. return __os;
  4133. }
  4134. template <class _CharT, class _Traits, class _RT>
  4135. basic_istream<_CharT, _Traits>&
  4136. operator>>(basic_istream<_CharT, _Traits>& __is,
  4137. weibull_distribution<_RT>& __x)
  4138. {
  4139. typedef weibull_distribution<_RT> _Eng;
  4140. typedef typename _Eng::result_type result_type;
  4141. typedef typename _Eng::param_type param_type;
  4142. __save_flags<_CharT, _Traits> __lx(__is);
  4143. __is.flags(ios_base::dec | ios_base::skipws);
  4144. result_type __a;
  4145. result_type __b;
  4146. __is >> __a >> __b;
  4147. if (!__is.fail())
  4148. __x.param(param_type(__a, __b));
  4149. return __is;
  4150. }
  4151. template<class _RealType = double>
  4152. class _LIBCPP_TYPE_VIS_ONLY extreme_value_distribution
  4153. {
  4154. public:
  4155. // types
  4156. typedef _RealType result_type;
  4157. class _LIBCPP_TYPE_VIS_ONLY param_type
  4158. {
  4159. result_type __a_;
  4160. result_type __b_;
  4161. public:
  4162. typedef extreme_value_distribution distribution_type;
  4163. _LIBCPP_INLINE_VISIBILITY
  4164. explicit param_type(result_type __a = 0, result_type __b = 1)
  4165. : __a_(__a), __b_(__b) {}
  4166. _LIBCPP_INLINE_VISIBILITY
  4167. result_type a() const {return __a_;}
  4168. _LIBCPP_INLINE_VISIBILITY
  4169. result_type b() const {return __b_;}
  4170. friend _LIBCPP_INLINE_VISIBILITY
  4171. bool operator==(const param_type& __x, const param_type& __y)
  4172. {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
  4173. friend _LIBCPP_INLINE_VISIBILITY
  4174. bool operator!=(const param_type& __x, const param_type& __y)
  4175. {return !(__x == __y);}
  4176. };
  4177. private:
  4178. param_type __p_;
  4179. public:
  4180. // constructor and reset functions
  4181. _LIBCPP_INLINE_VISIBILITY
  4182. explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
  4183. : __p_(param_type(__a, __b)) {}
  4184. _LIBCPP_INLINE_VISIBILITY
  4185. explicit extreme_value_distribution(const param_type& __p)
  4186. : __p_(__p) {}
  4187. _LIBCPP_INLINE_VISIBILITY
  4188. void reset() {}
  4189. // generating functions
  4190. template<class _URNG>
  4191. _LIBCPP_INLINE_VISIBILITY
  4192. result_type operator()(_URNG& __g)
  4193. {return (*this)(__g, __p_);}
  4194. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  4195. // property functions
  4196. _LIBCPP_INLINE_VISIBILITY
  4197. result_type a() const {return __p_.a();}
  4198. _LIBCPP_INLINE_VISIBILITY
  4199. result_type b() const {return __p_.b();}
  4200. _LIBCPP_INLINE_VISIBILITY
  4201. param_type param() const {return __p_;}
  4202. _LIBCPP_INLINE_VISIBILITY
  4203. void param(const param_type& __p) {__p_ = __p;}
  4204. _LIBCPP_INLINE_VISIBILITY
  4205. result_type min() const {return -numeric_limits<result_type>::infinity();}
  4206. _LIBCPP_INLINE_VISIBILITY
  4207. result_type max() const {return numeric_limits<result_type>::infinity();}
  4208. friend _LIBCPP_INLINE_VISIBILITY
  4209. bool operator==(const extreme_value_distribution& __x,
  4210. const extreme_value_distribution& __y)
  4211. {return __x.__p_ == __y.__p_;}
  4212. friend _LIBCPP_INLINE_VISIBILITY
  4213. bool operator!=(const extreme_value_distribution& __x,
  4214. const extreme_value_distribution& __y)
  4215. {return !(__x == __y);}
  4216. };
  4217. template<class _RealType>
  4218. template<class _URNG>
  4219. _RealType
  4220. extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  4221. {
  4222. return __p.a() - __p.b() *
  4223. _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
  4224. }
  4225. template <class _CharT, class _Traits, class _RT>
  4226. basic_ostream<_CharT, _Traits>&
  4227. operator<<(basic_ostream<_CharT, _Traits>& __os,
  4228. const extreme_value_distribution<_RT>& __x)
  4229. {
  4230. __save_flags<_CharT, _Traits> __lx(__os);
  4231. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  4232. ios_base::scientific);
  4233. _CharT __sp = __os.widen(' ');
  4234. __os.fill(__sp);
  4235. __os << __x.a() << __sp << __x.b();
  4236. return __os;
  4237. }
  4238. template <class _CharT, class _Traits, class _RT>
  4239. basic_istream<_CharT, _Traits>&
  4240. operator>>(basic_istream<_CharT, _Traits>& __is,
  4241. extreme_value_distribution<_RT>& __x)
  4242. {
  4243. typedef extreme_value_distribution<_RT> _Eng;
  4244. typedef typename _Eng::result_type result_type;
  4245. typedef typename _Eng::param_type param_type;
  4246. __save_flags<_CharT, _Traits> __lx(__is);
  4247. __is.flags(ios_base::dec | ios_base::skipws);
  4248. result_type __a;
  4249. result_type __b;
  4250. __is >> __a >> __b;
  4251. if (!__is.fail())
  4252. __x.param(param_type(__a, __b));
  4253. return __is;
  4254. }
  4255. // gamma_distribution
  4256. template<class _RealType = double>
  4257. class _LIBCPP_TYPE_VIS_ONLY gamma_distribution
  4258. {
  4259. public:
  4260. // types
  4261. typedef _RealType result_type;
  4262. class _LIBCPP_TYPE_VIS_ONLY param_type
  4263. {
  4264. result_type __alpha_;
  4265. result_type __beta_;
  4266. public:
  4267. typedef gamma_distribution distribution_type;
  4268. _LIBCPP_INLINE_VISIBILITY
  4269. explicit param_type(result_type __alpha = 1, result_type __beta = 1)
  4270. : __alpha_(__alpha), __beta_(__beta) {}
  4271. _LIBCPP_INLINE_VISIBILITY
  4272. result_type alpha() const {return __alpha_;}
  4273. _LIBCPP_INLINE_VISIBILITY
  4274. result_type beta() const {return __beta_;}
  4275. friend _LIBCPP_INLINE_VISIBILITY
  4276. bool operator==(const param_type& __x, const param_type& __y)
  4277. {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
  4278. friend _LIBCPP_INLINE_VISIBILITY
  4279. bool operator!=(const param_type& __x, const param_type& __y)
  4280. {return !(__x == __y);}
  4281. };
  4282. private:
  4283. param_type __p_;
  4284. public:
  4285. // constructors and reset functions
  4286. _LIBCPP_INLINE_VISIBILITY
  4287. explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
  4288. : __p_(param_type(__alpha, __beta)) {}
  4289. _LIBCPP_INLINE_VISIBILITY
  4290. explicit gamma_distribution(const param_type& __p)
  4291. : __p_(__p) {}
  4292. _LIBCPP_INLINE_VISIBILITY
  4293. void reset() {}
  4294. // generating functions
  4295. template<class _URNG>
  4296. _LIBCPP_INLINE_VISIBILITY
  4297. result_type operator()(_URNG& __g)
  4298. {return (*this)(__g, __p_);}
  4299. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  4300. // property functions
  4301. _LIBCPP_INLINE_VISIBILITY
  4302. result_type alpha() const {return __p_.alpha();}
  4303. _LIBCPP_INLINE_VISIBILITY
  4304. result_type beta() const {return __p_.beta();}
  4305. _LIBCPP_INLINE_VISIBILITY
  4306. param_type param() const {return __p_;}
  4307. _LIBCPP_INLINE_VISIBILITY
  4308. void param(const param_type& __p) {__p_ = __p;}
  4309. _LIBCPP_INLINE_VISIBILITY
  4310. result_type min() const {return 0;}
  4311. _LIBCPP_INLINE_VISIBILITY
  4312. result_type max() const {return numeric_limits<result_type>::infinity();}
  4313. friend _LIBCPP_INLINE_VISIBILITY
  4314. bool operator==(const gamma_distribution& __x,
  4315. const gamma_distribution& __y)
  4316. {return __x.__p_ == __y.__p_;}
  4317. friend _LIBCPP_INLINE_VISIBILITY
  4318. bool operator!=(const gamma_distribution& __x,
  4319. const gamma_distribution& __y)
  4320. {return !(__x == __y);}
  4321. };
  4322. template <class _RealType>
  4323. template<class _URNG>
  4324. _RealType
  4325. gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  4326. {
  4327. result_type __a = __p.alpha();
  4328. uniform_real_distribution<result_type> __gen(0, 1);
  4329. exponential_distribution<result_type> __egen;
  4330. result_type __x;
  4331. if (__a == 1)
  4332. __x = __egen(__g);
  4333. else if (__a > 1)
  4334. {
  4335. const result_type __b = __a - 1;
  4336. const result_type __c = 3 * __a - result_type(0.75);
  4337. while (true)
  4338. {
  4339. const result_type __u = __gen(__g);
  4340. const result_type __v = __gen(__g);
  4341. const result_type __w = __u * (1 - __u);
  4342. if (__w != 0)
  4343. {
  4344. const result_type __y = _VSTD::sqrt(__c / __w) *
  4345. (__u - result_type(0.5));
  4346. __x = __b + __y;
  4347. if (__x >= 0)
  4348. {
  4349. const result_type __z = 64 * __w * __w * __w * __v * __v;
  4350. if (__z <= 1 - 2 * __y * __y / __x)
  4351. break;
  4352. if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
  4353. break;
  4354. }
  4355. }
  4356. }
  4357. }
  4358. else // __a < 1
  4359. {
  4360. while (true)
  4361. {
  4362. const result_type __u = __gen(__g);
  4363. const result_type __es = __egen(__g);
  4364. if (__u <= 1 - __a)
  4365. {
  4366. __x = _VSTD::pow(__u, 1 / __a);
  4367. if (__x <= __es)
  4368. break;
  4369. }
  4370. else
  4371. {
  4372. const result_type __e = -_VSTD::log((1-__u)/__a);
  4373. __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
  4374. if (__x <= __e + __es)
  4375. break;
  4376. }
  4377. }
  4378. }
  4379. return __x * __p.beta();
  4380. }
  4381. template <class _CharT, class _Traits, class _RT>
  4382. basic_ostream<_CharT, _Traits>&
  4383. operator<<(basic_ostream<_CharT, _Traits>& __os,
  4384. const gamma_distribution<_RT>& __x)
  4385. {
  4386. __save_flags<_CharT, _Traits> __lx(__os);
  4387. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  4388. ios_base::scientific);
  4389. _CharT __sp = __os.widen(' ');
  4390. __os.fill(__sp);
  4391. __os << __x.alpha() << __sp << __x.beta();
  4392. return __os;
  4393. }
  4394. template <class _CharT, class _Traits, class _RT>
  4395. basic_istream<_CharT, _Traits>&
  4396. operator>>(basic_istream<_CharT, _Traits>& __is,
  4397. gamma_distribution<_RT>& __x)
  4398. {
  4399. typedef gamma_distribution<_RT> _Eng;
  4400. typedef typename _Eng::result_type result_type;
  4401. typedef typename _Eng::param_type param_type;
  4402. __save_flags<_CharT, _Traits> __lx(__is);
  4403. __is.flags(ios_base::dec | ios_base::skipws);
  4404. result_type __alpha;
  4405. result_type __beta;
  4406. __is >> __alpha >> __beta;
  4407. if (!__is.fail())
  4408. __x.param(param_type(__alpha, __beta));
  4409. return __is;
  4410. }
  4411. // negative_binomial_distribution
  4412. template<class _IntType = int>
  4413. class _LIBCPP_TYPE_VIS_ONLY negative_binomial_distribution
  4414. {
  4415. public:
  4416. // types
  4417. typedef _IntType result_type;
  4418. class _LIBCPP_TYPE_VIS_ONLY param_type
  4419. {
  4420. result_type __k_;
  4421. double __p_;
  4422. public:
  4423. typedef negative_binomial_distribution distribution_type;
  4424. _LIBCPP_INLINE_VISIBILITY
  4425. explicit param_type(result_type __k = 1, double __p = 0.5)
  4426. : __k_(__k), __p_(__p) {}
  4427. _LIBCPP_INLINE_VISIBILITY
  4428. result_type k() const {return __k_;}
  4429. _LIBCPP_INLINE_VISIBILITY
  4430. double p() const {return __p_;}
  4431. friend _LIBCPP_INLINE_VISIBILITY
  4432. bool operator==(const param_type& __x, const param_type& __y)
  4433. {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
  4434. friend _LIBCPP_INLINE_VISIBILITY
  4435. bool operator!=(const param_type& __x, const param_type& __y)
  4436. {return !(__x == __y);}
  4437. };
  4438. private:
  4439. param_type __p_;
  4440. public:
  4441. // constructor and reset functions
  4442. _LIBCPP_INLINE_VISIBILITY
  4443. explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
  4444. : __p_(__k, __p) {}
  4445. _LIBCPP_INLINE_VISIBILITY
  4446. explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
  4447. _LIBCPP_INLINE_VISIBILITY
  4448. void reset() {}
  4449. // generating functions
  4450. template<class _URNG>
  4451. _LIBCPP_INLINE_VISIBILITY
  4452. result_type operator()(_URNG& __g)
  4453. {return (*this)(__g, __p_);}
  4454. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  4455. // property functions
  4456. _LIBCPP_INLINE_VISIBILITY
  4457. result_type k() const {return __p_.k();}
  4458. _LIBCPP_INLINE_VISIBILITY
  4459. double p() const {return __p_.p();}
  4460. _LIBCPP_INLINE_VISIBILITY
  4461. param_type param() const {return __p_;}
  4462. _LIBCPP_INLINE_VISIBILITY
  4463. void param(const param_type& __p) {__p_ = __p;}
  4464. _LIBCPP_INLINE_VISIBILITY
  4465. result_type min() const {return 0;}
  4466. _LIBCPP_INLINE_VISIBILITY
  4467. result_type max() const {return numeric_limits<result_type>::max();}
  4468. friend _LIBCPP_INLINE_VISIBILITY
  4469. bool operator==(const negative_binomial_distribution& __x,
  4470. const negative_binomial_distribution& __y)
  4471. {return __x.__p_ == __y.__p_;}
  4472. friend _LIBCPP_INLINE_VISIBILITY
  4473. bool operator!=(const negative_binomial_distribution& __x,
  4474. const negative_binomial_distribution& __y)
  4475. {return !(__x == __y);}
  4476. };
  4477. template <class _IntType>
  4478. template<class _URNG>
  4479. _IntType
  4480. negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
  4481. {
  4482. result_type __k = __pr.k();
  4483. double __p = __pr.p();
  4484. if (__k <= 21 * __p)
  4485. {
  4486. bernoulli_distribution __gen(__p);
  4487. result_type __f = 0;
  4488. result_type __s = 0;
  4489. while (__s < __k)
  4490. {
  4491. if (__gen(__urng))
  4492. ++__s;
  4493. else
  4494. ++__f;
  4495. }
  4496. return __f;
  4497. }
  4498. return poisson_distribution<result_type>(gamma_distribution<double>
  4499. (__k, (1-__p)/__p)(__urng))(__urng);
  4500. }
  4501. template <class _CharT, class _Traits, class _IntType>
  4502. basic_ostream<_CharT, _Traits>&
  4503. operator<<(basic_ostream<_CharT, _Traits>& __os,
  4504. const negative_binomial_distribution<_IntType>& __x)
  4505. {
  4506. __save_flags<_CharT, _Traits> __lx(__os);
  4507. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  4508. ios_base::scientific);
  4509. _CharT __sp = __os.widen(' ');
  4510. __os.fill(__sp);
  4511. return __os << __x.k() << __sp << __x.p();
  4512. }
  4513. template <class _CharT, class _Traits, class _IntType>
  4514. basic_istream<_CharT, _Traits>&
  4515. operator>>(basic_istream<_CharT, _Traits>& __is,
  4516. negative_binomial_distribution<_IntType>& __x)
  4517. {
  4518. typedef negative_binomial_distribution<_IntType> _Eng;
  4519. typedef typename _Eng::result_type result_type;
  4520. typedef typename _Eng::param_type param_type;
  4521. __save_flags<_CharT, _Traits> __lx(__is);
  4522. __is.flags(ios_base::dec | ios_base::skipws);
  4523. result_type __k;
  4524. double __p;
  4525. __is >> __k >> __p;
  4526. if (!__is.fail())
  4527. __x.param(param_type(__k, __p));
  4528. return __is;
  4529. }
  4530. // geometric_distribution
  4531. template<class _IntType = int>
  4532. class _LIBCPP_TYPE_VIS_ONLY geometric_distribution
  4533. {
  4534. public:
  4535. // types
  4536. typedef _IntType result_type;
  4537. class _LIBCPP_TYPE_VIS_ONLY param_type
  4538. {
  4539. double __p_;
  4540. public:
  4541. typedef geometric_distribution distribution_type;
  4542. _LIBCPP_INLINE_VISIBILITY
  4543. explicit param_type(double __p = 0.5) : __p_(__p) {}
  4544. _LIBCPP_INLINE_VISIBILITY
  4545. double p() const {return __p_;}
  4546. friend _LIBCPP_INLINE_VISIBILITY
  4547. bool operator==(const param_type& __x, const param_type& __y)
  4548. {return __x.__p_ == __y.__p_;}
  4549. friend _LIBCPP_INLINE_VISIBILITY
  4550. bool operator!=(const param_type& __x, const param_type& __y)
  4551. {return !(__x == __y);}
  4552. };
  4553. private:
  4554. param_type __p_;
  4555. public:
  4556. // constructors and reset functions
  4557. _LIBCPP_INLINE_VISIBILITY
  4558. explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
  4559. _LIBCPP_INLINE_VISIBILITY
  4560. explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
  4561. _LIBCPP_INLINE_VISIBILITY
  4562. void reset() {}
  4563. // generating functions
  4564. template<class _URNG>
  4565. _LIBCPP_INLINE_VISIBILITY
  4566. result_type operator()(_URNG& __g)
  4567. {return (*this)(__g, __p_);}
  4568. template<class _URNG>
  4569. _LIBCPP_INLINE_VISIBILITY
  4570. result_type operator()(_URNG& __g, const param_type& __p)
  4571. {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
  4572. // property functions
  4573. _LIBCPP_INLINE_VISIBILITY
  4574. double p() const {return __p_.p();}
  4575. _LIBCPP_INLINE_VISIBILITY
  4576. param_type param() const {return __p_;}
  4577. _LIBCPP_INLINE_VISIBILITY
  4578. void param(const param_type& __p) {__p_ = __p;}
  4579. _LIBCPP_INLINE_VISIBILITY
  4580. result_type min() const {return 0;}
  4581. _LIBCPP_INLINE_VISIBILITY
  4582. result_type max() const {return numeric_limits<result_type>::max();}
  4583. friend _LIBCPP_INLINE_VISIBILITY
  4584. bool operator==(const geometric_distribution& __x,
  4585. const geometric_distribution& __y)
  4586. {return __x.__p_ == __y.__p_;}
  4587. friend _LIBCPP_INLINE_VISIBILITY
  4588. bool operator!=(const geometric_distribution& __x,
  4589. const geometric_distribution& __y)
  4590. {return !(__x == __y);}
  4591. };
  4592. template <class _CharT, class _Traits, class _IntType>
  4593. basic_ostream<_CharT, _Traits>&
  4594. operator<<(basic_ostream<_CharT, _Traits>& __os,
  4595. const geometric_distribution<_IntType>& __x)
  4596. {
  4597. __save_flags<_CharT, _Traits> __lx(__os);
  4598. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  4599. ios_base::scientific);
  4600. return __os << __x.p();
  4601. }
  4602. template <class _CharT, class _Traits, class _IntType>
  4603. basic_istream<_CharT, _Traits>&
  4604. operator>>(basic_istream<_CharT, _Traits>& __is,
  4605. geometric_distribution<_IntType>& __x)
  4606. {
  4607. typedef geometric_distribution<_IntType> _Eng;
  4608. typedef typename _Eng::param_type param_type;
  4609. __save_flags<_CharT, _Traits> __lx(__is);
  4610. __is.flags(ios_base::dec | ios_base::skipws);
  4611. double __p;
  4612. __is >> __p;
  4613. if (!__is.fail())
  4614. __x.param(param_type(__p));
  4615. return __is;
  4616. }
  4617. // chi_squared_distribution
  4618. template<class _RealType = double>
  4619. class _LIBCPP_TYPE_VIS_ONLY chi_squared_distribution
  4620. {
  4621. public:
  4622. // types
  4623. typedef _RealType result_type;
  4624. class _LIBCPP_TYPE_VIS_ONLY param_type
  4625. {
  4626. result_type __n_;
  4627. public:
  4628. typedef chi_squared_distribution distribution_type;
  4629. _LIBCPP_INLINE_VISIBILITY
  4630. explicit param_type(result_type __n = 1) : __n_(__n) {}
  4631. _LIBCPP_INLINE_VISIBILITY
  4632. result_type n() const {return __n_;}
  4633. friend _LIBCPP_INLINE_VISIBILITY
  4634. bool operator==(const param_type& __x, const param_type& __y)
  4635. {return __x.__n_ == __y.__n_;}
  4636. friend _LIBCPP_INLINE_VISIBILITY
  4637. bool operator!=(const param_type& __x, const param_type& __y)
  4638. {return !(__x == __y);}
  4639. };
  4640. private:
  4641. param_type __p_;
  4642. public:
  4643. // constructor and reset functions
  4644. _LIBCPP_INLINE_VISIBILITY
  4645. explicit chi_squared_distribution(result_type __n = 1)
  4646. : __p_(param_type(__n)) {}
  4647. _LIBCPP_INLINE_VISIBILITY
  4648. explicit chi_squared_distribution(const param_type& __p)
  4649. : __p_(__p) {}
  4650. _LIBCPP_INLINE_VISIBILITY
  4651. void reset() {}
  4652. // generating functions
  4653. template<class _URNG>
  4654. _LIBCPP_INLINE_VISIBILITY
  4655. result_type operator()(_URNG& __g)
  4656. {return (*this)(__g, __p_);}
  4657. template<class _URNG>
  4658. _LIBCPP_INLINE_VISIBILITY
  4659. result_type operator()(_URNG& __g, const param_type& __p)
  4660. {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
  4661. // property functions
  4662. _LIBCPP_INLINE_VISIBILITY
  4663. result_type n() const {return __p_.n();}
  4664. _LIBCPP_INLINE_VISIBILITY
  4665. param_type param() const {return __p_;}
  4666. _LIBCPP_INLINE_VISIBILITY
  4667. void param(const param_type& __p) {__p_ = __p;}
  4668. _LIBCPP_INLINE_VISIBILITY
  4669. result_type min() const {return 0;}
  4670. _LIBCPP_INLINE_VISIBILITY
  4671. result_type max() const {return numeric_limits<result_type>::infinity();}
  4672. friend _LIBCPP_INLINE_VISIBILITY
  4673. bool operator==(const chi_squared_distribution& __x,
  4674. const chi_squared_distribution& __y)
  4675. {return __x.__p_ == __y.__p_;}
  4676. friend _LIBCPP_INLINE_VISIBILITY
  4677. bool operator!=(const chi_squared_distribution& __x,
  4678. const chi_squared_distribution& __y)
  4679. {return !(__x == __y);}
  4680. };
  4681. template <class _CharT, class _Traits, class _RT>
  4682. basic_ostream<_CharT, _Traits>&
  4683. operator<<(basic_ostream<_CharT, _Traits>& __os,
  4684. const chi_squared_distribution<_RT>& __x)
  4685. {
  4686. __save_flags<_CharT, _Traits> __lx(__os);
  4687. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  4688. ios_base::scientific);
  4689. __os << __x.n();
  4690. return __os;
  4691. }
  4692. template <class _CharT, class _Traits, class _RT>
  4693. basic_istream<_CharT, _Traits>&
  4694. operator>>(basic_istream<_CharT, _Traits>& __is,
  4695. chi_squared_distribution<_RT>& __x)
  4696. {
  4697. typedef chi_squared_distribution<_RT> _Eng;
  4698. typedef typename _Eng::result_type result_type;
  4699. typedef typename _Eng::param_type param_type;
  4700. __save_flags<_CharT, _Traits> __lx(__is);
  4701. __is.flags(ios_base::dec | ios_base::skipws);
  4702. result_type __n;
  4703. __is >> __n;
  4704. if (!__is.fail())
  4705. __x.param(param_type(__n));
  4706. return __is;
  4707. }
  4708. // cauchy_distribution
  4709. template<class _RealType = double>
  4710. class _LIBCPP_TYPE_VIS_ONLY cauchy_distribution
  4711. {
  4712. public:
  4713. // types
  4714. typedef _RealType result_type;
  4715. class _LIBCPP_TYPE_VIS_ONLY param_type
  4716. {
  4717. result_type __a_;
  4718. result_type __b_;
  4719. public:
  4720. typedef cauchy_distribution distribution_type;
  4721. _LIBCPP_INLINE_VISIBILITY
  4722. explicit param_type(result_type __a = 0, result_type __b = 1)
  4723. : __a_(__a), __b_(__b) {}
  4724. _LIBCPP_INLINE_VISIBILITY
  4725. result_type a() const {return __a_;}
  4726. _LIBCPP_INLINE_VISIBILITY
  4727. result_type b() const {return __b_;}
  4728. friend _LIBCPP_INLINE_VISIBILITY
  4729. bool operator==(const param_type& __x, const param_type& __y)
  4730. {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
  4731. friend _LIBCPP_INLINE_VISIBILITY
  4732. bool operator!=(const param_type& __x, const param_type& __y)
  4733. {return !(__x == __y);}
  4734. };
  4735. private:
  4736. param_type __p_;
  4737. public:
  4738. // constructor and reset functions
  4739. _LIBCPP_INLINE_VISIBILITY
  4740. explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
  4741. : __p_(param_type(__a, __b)) {}
  4742. _LIBCPP_INLINE_VISIBILITY
  4743. explicit cauchy_distribution(const param_type& __p)
  4744. : __p_(__p) {}
  4745. _LIBCPP_INLINE_VISIBILITY
  4746. void reset() {}
  4747. // generating functions
  4748. template<class _URNG>
  4749. _LIBCPP_INLINE_VISIBILITY
  4750. result_type operator()(_URNG& __g)
  4751. {return (*this)(__g, __p_);}
  4752. template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
  4753. // property functions
  4754. _LIBCPP_INLINE_VISIBILITY
  4755. result_type a() const {return __p_.a();}
  4756. _LIBCPP_INLINE_VISIBILITY
  4757. result_type b() const {return __p_.b();}
  4758. _LIBCPP_INLINE_VISIBILITY
  4759. param_type param() const {return __p_;}
  4760. _LIBCPP_INLINE_VISIBILITY
  4761. void param(const param_type& __p) {__p_ = __p;}
  4762. _LIBCPP_INLINE_VISIBILITY
  4763. result_type min() const {return -numeric_limits<result_type>::infinity();}
  4764. _LIBCPP_INLINE_VISIBILITY
  4765. result_type max() const {return numeric_limits<result_type>::infinity();}
  4766. friend _LIBCPP_INLINE_VISIBILITY
  4767. bool operator==(const cauchy_distribution& __x,
  4768. const cauchy_distribution& __y)
  4769. {return __x.__p_ == __y.__p_;}
  4770. friend _LIBCPP_INLINE_VISIBILITY
  4771. bool operator!=(const cauchy_distribution& __x,
  4772. const cauchy_distribution& __y)
  4773. {return !(__x == __y);}
  4774. };
  4775. template <class _RealType>
  4776. template<class _URNG>
  4777. inline
  4778. _RealType
  4779. cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  4780. {
  4781. uniform_real_distribution<result_type> __gen;
  4782. // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
  4783. return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
  4784. }
  4785. template <class _CharT, class _Traits, class _RT>
  4786. basic_ostream<_CharT, _Traits>&
  4787. operator<<(basic_ostream<_CharT, _Traits>& __os,
  4788. const cauchy_distribution<_RT>& __x)
  4789. {
  4790. __save_flags<_CharT, _Traits> __lx(__os);
  4791. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  4792. ios_base::scientific);
  4793. _CharT __sp = __os.widen(' ');
  4794. __os.fill(__sp);
  4795. __os << __x.a() << __sp << __x.b();
  4796. return __os;
  4797. }
  4798. template <class _CharT, class _Traits, class _RT>
  4799. basic_istream<_CharT, _Traits>&
  4800. operator>>(basic_istream<_CharT, _Traits>& __is,
  4801. cauchy_distribution<_RT>& __x)
  4802. {
  4803. typedef cauchy_distribution<_RT> _Eng;
  4804. typedef typename _Eng::result_type result_type;
  4805. typedef typename _Eng::param_type param_type;
  4806. __save_flags<_CharT, _Traits> __lx(__is);
  4807. __is.flags(ios_base::dec | ios_base::skipws);
  4808. result_type __a;
  4809. result_type __b;
  4810. __is >> __a >> __b;
  4811. if (!__is.fail())
  4812. __x.param(param_type(__a, __b));
  4813. return __is;
  4814. }
  4815. // fisher_f_distribution
  4816. template<class _RealType = double>
  4817. class _LIBCPP_TYPE_VIS_ONLY fisher_f_distribution
  4818. {
  4819. public:
  4820. // types
  4821. typedef _RealType result_type;
  4822. class _LIBCPP_TYPE_VIS_ONLY param_type
  4823. {
  4824. result_type __m_;
  4825. result_type __n_;
  4826. public:
  4827. typedef fisher_f_distribution distribution_type;
  4828. _LIBCPP_INLINE_VISIBILITY
  4829. explicit param_type(result_type __m = 1, result_type __n = 1)
  4830. : __m_(__m), __n_(__n) {}
  4831. _LIBCPP_INLINE_VISIBILITY
  4832. result_type m() const {return __m_;}
  4833. _LIBCPP_INLINE_VISIBILITY
  4834. result_type n() const {return __n_;}
  4835. friend _LIBCPP_INLINE_VISIBILITY
  4836. bool operator==(const param_type& __x, const param_type& __y)
  4837. {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
  4838. friend _LIBCPP_INLINE_VISIBILITY
  4839. bool operator!=(const param_type& __x, const param_type& __y)
  4840. {return !(__x == __y);}
  4841. };
  4842. private:
  4843. param_type __p_;
  4844. public:
  4845. // constructor and reset functions
  4846. _LIBCPP_INLINE_VISIBILITY
  4847. explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
  4848. : __p_(param_type(__m, __n)) {}
  4849. _LIBCPP_INLINE_VISIBILITY
  4850. explicit fisher_f_distribution(const param_type& __p)
  4851. : __p_(__p) {}
  4852. _LIBCPP_INLINE_VISIBILITY
  4853. void reset() {}
  4854. // generating functions
  4855. template<class _URNG>
  4856. _LIBCPP_INLINE_VISIBILITY
  4857. result_type operator()(_URNG& __g)
  4858. {return (*this)(__g, __p_);}
  4859. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  4860. // property functions
  4861. _LIBCPP_INLINE_VISIBILITY
  4862. result_type m() const {return __p_.m();}
  4863. _LIBCPP_INLINE_VISIBILITY
  4864. result_type n() const {return __p_.n();}
  4865. _LIBCPP_INLINE_VISIBILITY
  4866. param_type param() const {return __p_;}
  4867. _LIBCPP_INLINE_VISIBILITY
  4868. void param(const param_type& __p) {__p_ = __p;}
  4869. _LIBCPP_INLINE_VISIBILITY
  4870. result_type min() const {return 0;}
  4871. _LIBCPP_INLINE_VISIBILITY
  4872. result_type max() const {return numeric_limits<result_type>::infinity();}
  4873. friend _LIBCPP_INLINE_VISIBILITY
  4874. bool operator==(const fisher_f_distribution& __x,
  4875. const fisher_f_distribution& __y)
  4876. {return __x.__p_ == __y.__p_;}
  4877. friend _LIBCPP_INLINE_VISIBILITY
  4878. bool operator!=(const fisher_f_distribution& __x,
  4879. const fisher_f_distribution& __y)
  4880. {return !(__x == __y);}
  4881. };
  4882. template <class _RealType>
  4883. template<class _URNG>
  4884. _RealType
  4885. fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  4886. {
  4887. gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
  4888. gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
  4889. return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
  4890. }
  4891. template <class _CharT, class _Traits, class _RT>
  4892. basic_ostream<_CharT, _Traits>&
  4893. operator<<(basic_ostream<_CharT, _Traits>& __os,
  4894. const fisher_f_distribution<_RT>& __x)
  4895. {
  4896. __save_flags<_CharT, _Traits> __lx(__os);
  4897. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  4898. ios_base::scientific);
  4899. _CharT __sp = __os.widen(' ');
  4900. __os.fill(__sp);
  4901. __os << __x.m() << __sp << __x.n();
  4902. return __os;
  4903. }
  4904. template <class _CharT, class _Traits, class _RT>
  4905. basic_istream<_CharT, _Traits>&
  4906. operator>>(basic_istream<_CharT, _Traits>& __is,
  4907. fisher_f_distribution<_RT>& __x)
  4908. {
  4909. typedef fisher_f_distribution<_RT> _Eng;
  4910. typedef typename _Eng::result_type result_type;
  4911. typedef typename _Eng::param_type param_type;
  4912. __save_flags<_CharT, _Traits> __lx(__is);
  4913. __is.flags(ios_base::dec | ios_base::skipws);
  4914. result_type __m;
  4915. result_type __n;
  4916. __is >> __m >> __n;
  4917. if (!__is.fail())
  4918. __x.param(param_type(__m, __n));
  4919. return __is;
  4920. }
  4921. // student_t_distribution
  4922. template<class _RealType = double>
  4923. class _LIBCPP_TYPE_VIS_ONLY student_t_distribution
  4924. {
  4925. public:
  4926. // types
  4927. typedef _RealType result_type;
  4928. class _LIBCPP_TYPE_VIS_ONLY param_type
  4929. {
  4930. result_type __n_;
  4931. public:
  4932. typedef student_t_distribution distribution_type;
  4933. _LIBCPP_INLINE_VISIBILITY
  4934. explicit param_type(result_type __n = 1) : __n_(__n) {}
  4935. _LIBCPP_INLINE_VISIBILITY
  4936. result_type n() const {return __n_;}
  4937. friend _LIBCPP_INLINE_VISIBILITY
  4938. bool operator==(const param_type& __x, const param_type& __y)
  4939. {return __x.__n_ == __y.__n_;}
  4940. friend _LIBCPP_INLINE_VISIBILITY
  4941. bool operator!=(const param_type& __x, const param_type& __y)
  4942. {return !(__x == __y);}
  4943. };
  4944. private:
  4945. param_type __p_;
  4946. normal_distribution<result_type> __nd_;
  4947. public:
  4948. // constructor and reset functions
  4949. _LIBCPP_INLINE_VISIBILITY
  4950. explicit student_t_distribution(result_type __n = 1)
  4951. : __p_(param_type(__n)) {}
  4952. _LIBCPP_INLINE_VISIBILITY
  4953. explicit student_t_distribution(const param_type& __p)
  4954. : __p_(__p) {}
  4955. _LIBCPP_INLINE_VISIBILITY
  4956. void reset() {__nd_.reset();}
  4957. // generating functions
  4958. template<class _URNG>
  4959. _LIBCPP_INLINE_VISIBILITY
  4960. result_type operator()(_URNG& __g)
  4961. {return (*this)(__g, __p_);}
  4962. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  4963. // property functions
  4964. _LIBCPP_INLINE_VISIBILITY
  4965. result_type n() const {return __p_.n();}
  4966. _LIBCPP_INLINE_VISIBILITY
  4967. param_type param() const {return __p_;}
  4968. _LIBCPP_INLINE_VISIBILITY
  4969. void param(const param_type& __p) {__p_ = __p;}
  4970. _LIBCPP_INLINE_VISIBILITY
  4971. result_type min() const {return -numeric_limits<result_type>::infinity();}
  4972. _LIBCPP_INLINE_VISIBILITY
  4973. result_type max() const {return numeric_limits<result_type>::infinity();}
  4974. friend _LIBCPP_INLINE_VISIBILITY
  4975. bool operator==(const student_t_distribution& __x,
  4976. const student_t_distribution& __y)
  4977. {return __x.__p_ == __y.__p_;}
  4978. friend _LIBCPP_INLINE_VISIBILITY
  4979. bool operator!=(const student_t_distribution& __x,
  4980. const student_t_distribution& __y)
  4981. {return !(__x == __y);}
  4982. };
  4983. template <class _RealType>
  4984. template<class _URNG>
  4985. _RealType
  4986. student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  4987. {
  4988. gamma_distribution<result_type> __gd(__p.n() * .5, 2);
  4989. return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
  4990. }
  4991. template <class _CharT, class _Traits, class _RT>
  4992. basic_ostream<_CharT, _Traits>&
  4993. operator<<(basic_ostream<_CharT, _Traits>& __os,
  4994. const student_t_distribution<_RT>& __x)
  4995. {
  4996. __save_flags<_CharT, _Traits> __lx(__os);
  4997. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  4998. ios_base::scientific);
  4999. __os << __x.n();
  5000. return __os;
  5001. }
  5002. template <class _CharT, class _Traits, class _RT>
  5003. basic_istream<_CharT, _Traits>&
  5004. operator>>(basic_istream<_CharT, _Traits>& __is,
  5005. student_t_distribution<_RT>& __x)
  5006. {
  5007. typedef student_t_distribution<_RT> _Eng;
  5008. typedef typename _Eng::result_type result_type;
  5009. typedef typename _Eng::param_type param_type;
  5010. __save_flags<_CharT, _Traits> __lx(__is);
  5011. __is.flags(ios_base::dec | ios_base::skipws);
  5012. result_type __n;
  5013. __is >> __n;
  5014. if (!__is.fail())
  5015. __x.param(param_type(__n));
  5016. return __is;
  5017. }
  5018. // discrete_distribution
  5019. template<class _IntType = int>
  5020. class _LIBCPP_TYPE_VIS_ONLY discrete_distribution
  5021. {
  5022. public:
  5023. // types
  5024. typedef _IntType result_type;
  5025. class _LIBCPP_TYPE_VIS_ONLY param_type
  5026. {
  5027. vector<double> __p_;
  5028. public:
  5029. typedef discrete_distribution distribution_type;
  5030. _LIBCPP_INLINE_VISIBILITY
  5031. param_type() {}
  5032. template<class _InputIterator>
  5033. _LIBCPP_INLINE_VISIBILITY
  5034. param_type(_InputIterator __f, _InputIterator __l)
  5035. : __p_(__f, __l) {__init();}
  5036. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5037. _LIBCPP_INLINE_VISIBILITY
  5038. param_type(initializer_list<double> __wl)
  5039. : __p_(__wl.begin(), __wl.end()) {__init();}
  5040. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5041. template<class _UnaryOperation>
  5042. param_type(size_t __nw, double __xmin, double __xmax,
  5043. _UnaryOperation __fw);
  5044. vector<double> probabilities() const;
  5045. friend _LIBCPP_INLINE_VISIBILITY
  5046. bool operator==(const param_type& __x, const param_type& __y)
  5047. {return __x.__p_ == __y.__p_;}
  5048. friend _LIBCPP_INLINE_VISIBILITY
  5049. bool operator!=(const param_type& __x, const param_type& __y)
  5050. {return !(__x == __y);}
  5051. private:
  5052. void __init();
  5053. friend class discrete_distribution;
  5054. template <class _CharT, class _Traits, class _IT>
  5055. friend
  5056. basic_ostream<_CharT, _Traits>&
  5057. operator<<(basic_ostream<_CharT, _Traits>& __os,
  5058. const discrete_distribution<_IT>& __x);
  5059. template <class _CharT, class _Traits, class _IT>
  5060. friend
  5061. basic_istream<_CharT, _Traits>&
  5062. operator>>(basic_istream<_CharT, _Traits>& __is,
  5063. discrete_distribution<_IT>& __x);
  5064. };
  5065. private:
  5066. param_type __p_;
  5067. public:
  5068. // constructor and reset functions
  5069. _LIBCPP_INLINE_VISIBILITY
  5070. discrete_distribution() {}
  5071. template<class _InputIterator>
  5072. _LIBCPP_INLINE_VISIBILITY
  5073. discrete_distribution(_InputIterator __f, _InputIterator __l)
  5074. : __p_(__f, __l) {}
  5075. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5076. _LIBCPP_INLINE_VISIBILITY
  5077. discrete_distribution(initializer_list<double> __wl)
  5078. : __p_(__wl) {}
  5079. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5080. template<class _UnaryOperation>
  5081. _LIBCPP_INLINE_VISIBILITY
  5082. discrete_distribution(size_t __nw, double __xmin, double __xmax,
  5083. _UnaryOperation __fw)
  5084. : __p_(__nw, __xmin, __xmax, __fw) {}
  5085. _LIBCPP_INLINE_VISIBILITY
  5086. explicit discrete_distribution(const param_type& __p)
  5087. : __p_(__p) {}
  5088. _LIBCPP_INLINE_VISIBILITY
  5089. void reset() {}
  5090. // generating functions
  5091. template<class _URNG>
  5092. _LIBCPP_INLINE_VISIBILITY
  5093. result_type operator()(_URNG& __g)
  5094. {return (*this)(__g, __p_);}
  5095. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  5096. // property functions
  5097. _LIBCPP_INLINE_VISIBILITY
  5098. vector<double> probabilities() const {return __p_.probabilities();}
  5099. _LIBCPP_INLINE_VISIBILITY
  5100. param_type param() const {return __p_;}
  5101. _LIBCPP_INLINE_VISIBILITY
  5102. void param(const param_type& __p) {__p_ = __p;}
  5103. _LIBCPP_INLINE_VISIBILITY
  5104. result_type min() const {return 0;}
  5105. _LIBCPP_INLINE_VISIBILITY
  5106. result_type max() const {return __p_.__p_.size();}
  5107. friend _LIBCPP_INLINE_VISIBILITY
  5108. bool operator==(const discrete_distribution& __x,
  5109. const discrete_distribution& __y)
  5110. {return __x.__p_ == __y.__p_;}
  5111. friend _LIBCPP_INLINE_VISIBILITY
  5112. bool operator!=(const discrete_distribution& __x,
  5113. const discrete_distribution& __y)
  5114. {return !(__x == __y);}
  5115. template <class _CharT, class _Traits, class _IT>
  5116. friend
  5117. basic_ostream<_CharT, _Traits>&
  5118. operator<<(basic_ostream<_CharT, _Traits>& __os,
  5119. const discrete_distribution<_IT>& __x);
  5120. template <class _CharT, class _Traits, class _IT>
  5121. friend
  5122. basic_istream<_CharT, _Traits>&
  5123. operator>>(basic_istream<_CharT, _Traits>& __is,
  5124. discrete_distribution<_IT>& __x);
  5125. };
  5126. template<class _IntType>
  5127. template<class _UnaryOperation>
  5128. discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
  5129. double __xmin,
  5130. double __xmax,
  5131. _UnaryOperation __fw)
  5132. {
  5133. if (__nw > 1)
  5134. {
  5135. __p_.reserve(__nw - 1);
  5136. double __d = (__xmax - __xmin) / __nw;
  5137. double __d2 = __d / 2;
  5138. for (size_t __k = 0; __k < __nw; ++__k)
  5139. __p_.push_back(__fw(__xmin + __k * __d + __d2));
  5140. __init();
  5141. }
  5142. }
  5143. template<class _IntType>
  5144. void
  5145. discrete_distribution<_IntType>::param_type::__init()
  5146. {
  5147. if (!__p_.empty())
  5148. {
  5149. if (__p_.size() > 1)
  5150. {
  5151. double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
  5152. for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
  5153. __i < __e; ++__i)
  5154. *__i /= __s;
  5155. vector<double> __t(__p_.size() - 1);
  5156. _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
  5157. swap(__p_, __t);
  5158. }
  5159. else
  5160. {
  5161. __p_.clear();
  5162. __p_.shrink_to_fit();
  5163. }
  5164. }
  5165. }
  5166. template<class _IntType>
  5167. vector<double>
  5168. discrete_distribution<_IntType>::param_type::probabilities() const
  5169. {
  5170. size_t __n = __p_.size();
  5171. _VSTD::vector<double> __p(__n+1);
  5172. _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
  5173. if (__n > 0)
  5174. __p[__n] = 1 - __p_[__n-1];
  5175. else
  5176. __p[0] = 1;
  5177. return __p;
  5178. }
  5179. template<class _IntType>
  5180. template<class _URNG>
  5181. _IntType
  5182. discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
  5183. {
  5184. uniform_real_distribution<double> __gen;
  5185. return static_cast<_IntType>(
  5186. _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
  5187. __p.__p_.begin());
  5188. }
  5189. template <class _CharT, class _Traits, class _IT>
  5190. basic_ostream<_CharT, _Traits>&
  5191. operator<<(basic_ostream<_CharT, _Traits>& __os,
  5192. const discrete_distribution<_IT>& __x)
  5193. {
  5194. __save_flags<_CharT, _Traits> __lx(__os);
  5195. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  5196. ios_base::scientific);
  5197. _CharT __sp = __os.widen(' ');
  5198. __os.fill(__sp);
  5199. size_t __n = __x.__p_.__p_.size();
  5200. __os << __n;
  5201. for (size_t __i = 0; __i < __n; ++__i)
  5202. __os << __sp << __x.__p_.__p_[__i];
  5203. return __os;
  5204. }
  5205. template <class _CharT, class _Traits, class _IT>
  5206. basic_istream<_CharT, _Traits>&
  5207. operator>>(basic_istream<_CharT, _Traits>& __is,
  5208. discrete_distribution<_IT>& __x)
  5209. {
  5210. __save_flags<_CharT, _Traits> __lx(__is);
  5211. __is.flags(ios_base::dec | ios_base::skipws);
  5212. size_t __n;
  5213. __is >> __n;
  5214. vector<double> __p(__n);
  5215. for (size_t __i = 0; __i < __n; ++__i)
  5216. __is >> __p[__i];
  5217. if (!__is.fail())
  5218. swap(__x.__p_.__p_, __p);
  5219. return __is;
  5220. }
  5221. // piecewise_constant_distribution
  5222. template<class _RealType = double>
  5223. class _LIBCPP_TYPE_VIS_ONLY piecewise_constant_distribution
  5224. {
  5225. public:
  5226. // types
  5227. typedef _RealType result_type;
  5228. class _LIBCPP_TYPE_VIS_ONLY param_type
  5229. {
  5230. vector<result_type> __b_;
  5231. vector<result_type> __densities_;
  5232. vector<result_type> __areas_;
  5233. public:
  5234. typedef piecewise_constant_distribution distribution_type;
  5235. param_type();
  5236. template<class _InputIteratorB, class _InputIteratorW>
  5237. param_type(_InputIteratorB __fB, _InputIteratorB __lB,
  5238. _InputIteratorW __fW);
  5239. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5240. template<class _UnaryOperation>
  5241. param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
  5242. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5243. template<class _UnaryOperation>
  5244. param_type(size_t __nw, result_type __xmin, result_type __xmax,
  5245. _UnaryOperation __fw);
  5246. param_type & operator=(const param_type& __rhs);
  5247. _LIBCPP_INLINE_VISIBILITY
  5248. vector<result_type> intervals() const {return __b_;}
  5249. _LIBCPP_INLINE_VISIBILITY
  5250. vector<result_type> densities() const {return __densities_;}
  5251. friend _LIBCPP_INLINE_VISIBILITY
  5252. bool operator==(const param_type& __x, const param_type& __y)
  5253. {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
  5254. friend _LIBCPP_INLINE_VISIBILITY
  5255. bool operator!=(const param_type& __x, const param_type& __y)
  5256. {return !(__x == __y);}
  5257. private:
  5258. void __init();
  5259. friend class piecewise_constant_distribution;
  5260. template <class _CharT, class _Traits, class _RT>
  5261. friend
  5262. basic_ostream<_CharT, _Traits>&
  5263. operator<<(basic_ostream<_CharT, _Traits>& __os,
  5264. const piecewise_constant_distribution<_RT>& __x);
  5265. template <class _CharT, class _Traits, class _RT>
  5266. friend
  5267. basic_istream<_CharT, _Traits>&
  5268. operator>>(basic_istream<_CharT, _Traits>& __is,
  5269. piecewise_constant_distribution<_RT>& __x);
  5270. };
  5271. private:
  5272. param_type __p_;
  5273. public:
  5274. // constructor and reset functions
  5275. _LIBCPP_INLINE_VISIBILITY
  5276. piecewise_constant_distribution() {}
  5277. template<class _InputIteratorB, class _InputIteratorW>
  5278. _LIBCPP_INLINE_VISIBILITY
  5279. piecewise_constant_distribution(_InputIteratorB __fB,
  5280. _InputIteratorB __lB,
  5281. _InputIteratorW __fW)
  5282. : __p_(__fB, __lB, __fW) {}
  5283. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5284. template<class _UnaryOperation>
  5285. _LIBCPP_INLINE_VISIBILITY
  5286. piecewise_constant_distribution(initializer_list<result_type> __bl,
  5287. _UnaryOperation __fw)
  5288. : __p_(__bl, __fw) {}
  5289. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5290. template<class _UnaryOperation>
  5291. _LIBCPP_INLINE_VISIBILITY
  5292. piecewise_constant_distribution(size_t __nw, result_type __xmin,
  5293. result_type __xmax, _UnaryOperation __fw)
  5294. : __p_(__nw, __xmin, __xmax, __fw) {}
  5295. _LIBCPP_INLINE_VISIBILITY
  5296. explicit piecewise_constant_distribution(const param_type& __p)
  5297. : __p_(__p) {}
  5298. _LIBCPP_INLINE_VISIBILITY
  5299. void reset() {}
  5300. // generating functions
  5301. template<class _URNG>
  5302. _LIBCPP_INLINE_VISIBILITY
  5303. result_type operator()(_URNG& __g)
  5304. {return (*this)(__g, __p_);}
  5305. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  5306. // property functions
  5307. _LIBCPP_INLINE_VISIBILITY
  5308. vector<result_type> intervals() const {return __p_.intervals();}
  5309. _LIBCPP_INLINE_VISIBILITY
  5310. vector<result_type> densities() const {return __p_.densities();}
  5311. _LIBCPP_INLINE_VISIBILITY
  5312. param_type param() const {return __p_;}
  5313. _LIBCPP_INLINE_VISIBILITY
  5314. void param(const param_type& __p) {__p_ = __p;}
  5315. _LIBCPP_INLINE_VISIBILITY
  5316. result_type min() const {return __p_.__b_.front();}
  5317. _LIBCPP_INLINE_VISIBILITY
  5318. result_type max() const {return __p_.__b_.back();}
  5319. friend _LIBCPP_INLINE_VISIBILITY
  5320. bool operator==(const piecewise_constant_distribution& __x,
  5321. const piecewise_constant_distribution& __y)
  5322. {return __x.__p_ == __y.__p_;}
  5323. friend _LIBCPP_INLINE_VISIBILITY
  5324. bool operator!=(const piecewise_constant_distribution& __x,
  5325. const piecewise_constant_distribution& __y)
  5326. {return !(__x == __y);}
  5327. template <class _CharT, class _Traits, class _RT>
  5328. friend
  5329. basic_ostream<_CharT, _Traits>&
  5330. operator<<(basic_ostream<_CharT, _Traits>& __os,
  5331. const piecewise_constant_distribution<_RT>& __x);
  5332. template <class _CharT, class _Traits, class _RT>
  5333. friend
  5334. basic_istream<_CharT, _Traits>&
  5335. operator>>(basic_istream<_CharT, _Traits>& __is,
  5336. piecewise_constant_distribution<_RT>& __x);
  5337. };
  5338. template<class _RealType>
  5339. typename piecewise_constant_distribution<_RealType>::param_type &
  5340. piecewise_constant_distribution<_RealType>::param_type::operator=
  5341. (const param_type& __rhs)
  5342. {
  5343. // These can throw
  5344. __b_.reserve (__rhs.__b_.size ());
  5345. __densities_.reserve(__rhs.__densities_.size());
  5346. __areas_.reserve (__rhs.__areas_.size());
  5347. // These can not throw
  5348. __b_ = __rhs.__b_;
  5349. __densities_ = __rhs.__densities_;
  5350. __areas_ = __rhs.__areas_;
  5351. return *this;
  5352. }
  5353. template<class _RealType>
  5354. void
  5355. piecewise_constant_distribution<_RealType>::param_type::__init()
  5356. {
  5357. // __densities_ contains non-normalized areas
  5358. result_type __total_area = _VSTD::accumulate(__densities_.begin(),
  5359. __densities_.end(),
  5360. result_type());
  5361. for (size_t __i = 0; __i < __densities_.size(); ++__i)
  5362. __densities_[__i] /= __total_area;
  5363. // __densities_ contains normalized areas
  5364. __areas_.assign(__densities_.size(), result_type());
  5365. _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
  5366. __areas_.begin() + 1);
  5367. // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
  5368. __densities_.back() = 1 - __areas_.back(); // correct round off error
  5369. for (size_t __i = 0; __i < __densities_.size(); ++__i)
  5370. __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
  5371. // __densities_ now contains __densities_
  5372. }
  5373. template<class _RealType>
  5374. piecewise_constant_distribution<_RealType>::param_type::param_type()
  5375. : __b_(2),
  5376. __densities_(1, 1.0),
  5377. __areas_(1, 0.0)
  5378. {
  5379. __b_[1] = 1;
  5380. }
  5381. template<class _RealType>
  5382. template<class _InputIteratorB, class _InputIteratorW>
  5383. piecewise_constant_distribution<_RealType>::param_type::param_type(
  5384. _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
  5385. : __b_(__fB, __lB)
  5386. {
  5387. if (__b_.size() < 2)
  5388. {
  5389. __b_.resize(2);
  5390. __b_[0] = 0;
  5391. __b_[1] = 1;
  5392. __densities_.assign(1, 1.0);
  5393. __areas_.assign(1, 0.0);
  5394. }
  5395. else
  5396. {
  5397. __densities_.reserve(__b_.size() - 1);
  5398. for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
  5399. __densities_.push_back(*__fW);
  5400. __init();
  5401. }
  5402. }
  5403. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5404. template<class _RealType>
  5405. template<class _UnaryOperation>
  5406. piecewise_constant_distribution<_RealType>::param_type::param_type(
  5407. initializer_list<result_type> __bl, _UnaryOperation __fw)
  5408. : __b_(__bl.begin(), __bl.end())
  5409. {
  5410. if (__b_.size() < 2)
  5411. {
  5412. __b_.resize(2);
  5413. __b_[0] = 0;
  5414. __b_[1] = 1;
  5415. __densities_.assign(1, 1.0);
  5416. __areas_.assign(1, 0.0);
  5417. }
  5418. else
  5419. {
  5420. __densities_.reserve(__b_.size() - 1);
  5421. for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
  5422. __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
  5423. __init();
  5424. }
  5425. }
  5426. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5427. template<class _RealType>
  5428. template<class _UnaryOperation>
  5429. piecewise_constant_distribution<_RealType>::param_type::param_type(
  5430. size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
  5431. : __b_(__nw == 0 ? 2 : __nw + 1)
  5432. {
  5433. size_t __n = __b_.size() - 1;
  5434. result_type __d = (__xmax - __xmin) / __n;
  5435. __densities_.reserve(__n);
  5436. for (size_t __i = 0; __i < __n; ++__i)
  5437. {
  5438. __b_[__i] = __xmin + __i * __d;
  5439. __densities_.push_back(__fw(__b_[__i] + __d*.5));
  5440. }
  5441. __b_[__n] = __xmax;
  5442. __init();
  5443. }
  5444. template<class _RealType>
  5445. template<class _URNG>
  5446. _RealType
  5447. piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  5448. {
  5449. typedef uniform_real_distribution<result_type> _Gen;
  5450. result_type __u = _Gen()(__g);
  5451. ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
  5452. __u) - __p.__areas_.begin() - 1;
  5453. return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
  5454. }
  5455. template <class _CharT, class _Traits, class _RT>
  5456. basic_ostream<_CharT, _Traits>&
  5457. operator<<(basic_ostream<_CharT, _Traits>& __os,
  5458. const piecewise_constant_distribution<_RT>& __x)
  5459. {
  5460. __save_flags<_CharT, _Traits> __lx(__os);
  5461. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  5462. ios_base::scientific);
  5463. _CharT __sp = __os.widen(' ');
  5464. __os.fill(__sp);
  5465. size_t __n = __x.__p_.__b_.size();
  5466. __os << __n;
  5467. for (size_t __i = 0; __i < __n; ++__i)
  5468. __os << __sp << __x.__p_.__b_[__i];
  5469. __n = __x.__p_.__densities_.size();
  5470. __os << __sp << __n;
  5471. for (size_t __i = 0; __i < __n; ++__i)
  5472. __os << __sp << __x.__p_.__densities_[__i];
  5473. __n = __x.__p_.__areas_.size();
  5474. __os << __sp << __n;
  5475. for (size_t __i = 0; __i < __n; ++__i)
  5476. __os << __sp << __x.__p_.__areas_[__i];
  5477. return __os;
  5478. }
  5479. template <class _CharT, class _Traits, class _RT>
  5480. basic_istream<_CharT, _Traits>&
  5481. operator>>(basic_istream<_CharT, _Traits>& __is,
  5482. piecewise_constant_distribution<_RT>& __x)
  5483. {
  5484. typedef piecewise_constant_distribution<_RT> _Eng;
  5485. typedef typename _Eng::result_type result_type;
  5486. __save_flags<_CharT, _Traits> __lx(__is);
  5487. __is.flags(ios_base::dec | ios_base::skipws);
  5488. size_t __n;
  5489. __is >> __n;
  5490. vector<result_type> __b(__n);
  5491. for (size_t __i = 0; __i < __n; ++__i)
  5492. __is >> __b[__i];
  5493. __is >> __n;
  5494. vector<result_type> __densities(__n);
  5495. for (size_t __i = 0; __i < __n; ++__i)
  5496. __is >> __densities[__i];
  5497. __is >> __n;
  5498. vector<result_type> __areas(__n);
  5499. for (size_t __i = 0; __i < __n; ++__i)
  5500. __is >> __areas[__i];
  5501. if (!__is.fail())
  5502. {
  5503. swap(__x.__p_.__b_, __b);
  5504. swap(__x.__p_.__densities_, __densities);
  5505. swap(__x.__p_.__areas_, __areas);
  5506. }
  5507. return __is;
  5508. }
  5509. // piecewise_linear_distribution
  5510. template<class _RealType = double>
  5511. class _LIBCPP_TYPE_VIS_ONLY piecewise_linear_distribution
  5512. {
  5513. public:
  5514. // types
  5515. typedef _RealType result_type;
  5516. class _LIBCPP_TYPE_VIS_ONLY param_type
  5517. {
  5518. vector<result_type> __b_;
  5519. vector<result_type> __densities_;
  5520. vector<result_type> __areas_;
  5521. public:
  5522. typedef piecewise_linear_distribution distribution_type;
  5523. param_type();
  5524. template<class _InputIteratorB, class _InputIteratorW>
  5525. param_type(_InputIteratorB __fB, _InputIteratorB __lB,
  5526. _InputIteratorW __fW);
  5527. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5528. template<class _UnaryOperation>
  5529. param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
  5530. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5531. template<class _UnaryOperation>
  5532. param_type(size_t __nw, result_type __xmin, result_type __xmax,
  5533. _UnaryOperation __fw);
  5534. param_type & operator=(const param_type& __rhs);
  5535. _LIBCPP_INLINE_VISIBILITY
  5536. vector<result_type> intervals() const {return __b_;}
  5537. _LIBCPP_INLINE_VISIBILITY
  5538. vector<result_type> densities() const {return __densities_;}
  5539. friend _LIBCPP_INLINE_VISIBILITY
  5540. bool operator==(const param_type& __x, const param_type& __y)
  5541. {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
  5542. friend _LIBCPP_INLINE_VISIBILITY
  5543. bool operator!=(const param_type& __x, const param_type& __y)
  5544. {return !(__x == __y);}
  5545. private:
  5546. void __init();
  5547. friend class piecewise_linear_distribution;
  5548. template <class _CharT, class _Traits, class _RT>
  5549. friend
  5550. basic_ostream<_CharT, _Traits>&
  5551. operator<<(basic_ostream<_CharT, _Traits>& __os,
  5552. const piecewise_linear_distribution<_RT>& __x);
  5553. template <class _CharT, class _Traits, class _RT>
  5554. friend
  5555. basic_istream<_CharT, _Traits>&
  5556. operator>>(basic_istream<_CharT, _Traits>& __is,
  5557. piecewise_linear_distribution<_RT>& __x);
  5558. };
  5559. private:
  5560. param_type __p_;
  5561. public:
  5562. // constructor and reset functions
  5563. _LIBCPP_INLINE_VISIBILITY
  5564. piecewise_linear_distribution() {}
  5565. template<class _InputIteratorB, class _InputIteratorW>
  5566. _LIBCPP_INLINE_VISIBILITY
  5567. piecewise_linear_distribution(_InputIteratorB __fB,
  5568. _InputIteratorB __lB,
  5569. _InputIteratorW __fW)
  5570. : __p_(__fB, __lB, __fW) {}
  5571. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5572. template<class _UnaryOperation>
  5573. _LIBCPP_INLINE_VISIBILITY
  5574. piecewise_linear_distribution(initializer_list<result_type> __bl,
  5575. _UnaryOperation __fw)
  5576. : __p_(__bl, __fw) {}
  5577. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5578. template<class _UnaryOperation>
  5579. _LIBCPP_INLINE_VISIBILITY
  5580. piecewise_linear_distribution(size_t __nw, result_type __xmin,
  5581. result_type __xmax, _UnaryOperation __fw)
  5582. : __p_(__nw, __xmin, __xmax, __fw) {}
  5583. _LIBCPP_INLINE_VISIBILITY
  5584. explicit piecewise_linear_distribution(const param_type& __p)
  5585. : __p_(__p) {}
  5586. _LIBCPP_INLINE_VISIBILITY
  5587. void reset() {}
  5588. // generating functions
  5589. template<class _URNG>
  5590. _LIBCPP_INLINE_VISIBILITY
  5591. result_type operator()(_URNG& __g)
  5592. {return (*this)(__g, __p_);}
  5593. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  5594. // property functions
  5595. _LIBCPP_INLINE_VISIBILITY
  5596. vector<result_type> intervals() const {return __p_.intervals();}
  5597. _LIBCPP_INLINE_VISIBILITY
  5598. vector<result_type> densities() const {return __p_.densities();}
  5599. _LIBCPP_INLINE_VISIBILITY
  5600. param_type param() const {return __p_;}
  5601. _LIBCPP_INLINE_VISIBILITY
  5602. void param(const param_type& __p) {__p_ = __p;}
  5603. _LIBCPP_INLINE_VISIBILITY
  5604. result_type min() const {return __p_.__b_.front();}
  5605. _LIBCPP_INLINE_VISIBILITY
  5606. result_type max() const {return __p_.__b_.back();}
  5607. friend _LIBCPP_INLINE_VISIBILITY
  5608. bool operator==(const piecewise_linear_distribution& __x,
  5609. const piecewise_linear_distribution& __y)
  5610. {return __x.__p_ == __y.__p_;}
  5611. friend _LIBCPP_INLINE_VISIBILITY
  5612. bool operator!=(const piecewise_linear_distribution& __x,
  5613. const piecewise_linear_distribution& __y)
  5614. {return !(__x == __y);}
  5615. template <class _CharT, class _Traits, class _RT>
  5616. friend
  5617. basic_ostream<_CharT, _Traits>&
  5618. operator<<(basic_ostream<_CharT, _Traits>& __os,
  5619. const piecewise_linear_distribution<_RT>& __x);
  5620. template <class _CharT, class _Traits, class _RT>
  5621. friend
  5622. basic_istream<_CharT, _Traits>&
  5623. operator>>(basic_istream<_CharT, _Traits>& __is,
  5624. piecewise_linear_distribution<_RT>& __x);
  5625. };
  5626. template<class _RealType>
  5627. typename piecewise_linear_distribution<_RealType>::param_type &
  5628. piecewise_linear_distribution<_RealType>::param_type::operator=
  5629. (const param_type& __rhs)
  5630. {
  5631. // These can throw
  5632. __b_.reserve (__rhs.__b_.size ());
  5633. __densities_.reserve(__rhs.__densities_.size());
  5634. __areas_.reserve (__rhs.__areas_.size());
  5635. // These can not throw
  5636. __b_ = __rhs.__b_;
  5637. __densities_ = __rhs.__densities_;
  5638. __areas_ = __rhs.__areas_;
  5639. return *this;
  5640. }
  5641. template<class _RealType>
  5642. void
  5643. piecewise_linear_distribution<_RealType>::param_type::__init()
  5644. {
  5645. __areas_.assign(__densities_.size() - 1, result_type());
  5646. result_type _Sp = 0;
  5647. for (size_t __i = 0; __i < __areas_.size(); ++__i)
  5648. {
  5649. __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
  5650. (__b_[__i+1] - __b_[__i]) * .5;
  5651. _Sp += __areas_[__i];
  5652. }
  5653. for (size_t __i = __areas_.size(); __i > 1;)
  5654. {
  5655. --__i;
  5656. __areas_[__i] = __areas_[__i-1] / _Sp;
  5657. }
  5658. __areas_[0] = 0;
  5659. for (size_t __i = 1; __i < __areas_.size(); ++__i)
  5660. __areas_[__i] += __areas_[__i-1];
  5661. for (size_t __i = 0; __i < __densities_.size(); ++__i)
  5662. __densities_[__i] /= _Sp;
  5663. }
  5664. template<class _RealType>
  5665. piecewise_linear_distribution<_RealType>::param_type::param_type()
  5666. : __b_(2),
  5667. __densities_(2, 1.0),
  5668. __areas_(1, 0.0)
  5669. {
  5670. __b_[1] = 1;
  5671. }
  5672. template<class _RealType>
  5673. template<class _InputIteratorB, class _InputIteratorW>
  5674. piecewise_linear_distribution<_RealType>::param_type::param_type(
  5675. _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
  5676. : __b_(__fB, __lB)
  5677. {
  5678. if (__b_.size() < 2)
  5679. {
  5680. __b_.resize(2);
  5681. __b_[0] = 0;
  5682. __b_[1] = 1;
  5683. __densities_.assign(2, 1.0);
  5684. __areas_.assign(1, 0.0);
  5685. }
  5686. else
  5687. {
  5688. __densities_.reserve(__b_.size());
  5689. for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
  5690. __densities_.push_back(*__fW);
  5691. __init();
  5692. }
  5693. }
  5694. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5695. template<class _RealType>
  5696. template<class _UnaryOperation>
  5697. piecewise_linear_distribution<_RealType>::param_type::param_type(
  5698. initializer_list<result_type> __bl, _UnaryOperation __fw)
  5699. : __b_(__bl.begin(), __bl.end())
  5700. {
  5701. if (__b_.size() < 2)
  5702. {
  5703. __b_.resize(2);
  5704. __b_[0] = 0;
  5705. __b_[1] = 1;
  5706. __densities_.assign(2, 1.0);
  5707. __areas_.assign(1, 0.0);
  5708. }
  5709. else
  5710. {
  5711. __densities_.reserve(__b_.size());
  5712. for (size_t __i = 0; __i < __b_.size(); ++__i)
  5713. __densities_.push_back(__fw(__b_[__i]));
  5714. __init();
  5715. }
  5716. }
  5717. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  5718. template<class _RealType>
  5719. template<class _UnaryOperation>
  5720. piecewise_linear_distribution<_RealType>::param_type::param_type(
  5721. size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
  5722. : __b_(__nw == 0 ? 2 : __nw + 1)
  5723. {
  5724. size_t __n = __b_.size() - 1;
  5725. result_type __d = (__xmax - __xmin) / __n;
  5726. __densities_.reserve(__b_.size());
  5727. for (size_t __i = 0; __i < __n; ++__i)
  5728. {
  5729. __b_[__i] = __xmin + __i * __d;
  5730. __densities_.push_back(__fw(__b_[__i]));
  5731. }
  5732. __b_[__n] = __xmax;
  5733. __densities_.push_back(__fw(__b_[__n]));
  5734. __init();
  5735. }
  5736. template<class _RealType>
  5737. template<class _URNG>
  5738. _RealType
  5739. piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  5740. {
  5741. typedef uniform_real_distribution<result_type> _Gen;
  5742. result_type __u = _Gen()(__g);
  5743. ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
  5744. __u) - __p.__areas_.begin() - 1;
  5745. __u -= __p.__areas_[__k];
  5746. const result_type __dk = __p.__densities_[__k];
  5747. const result_type __dk1 = __p.__densities_[__k+1];
  5748. const result_type __deltad = __dk1 - __dk;
  5749. const result_type __bk = __p.__b_[__k];
  5750. if (__deltad == 0)
  5751. return __u / __dk + __bk;
  5752. const result_type __bk1 = __p.__b_[__k+1];
  5753. const result_type __deltab = __bk1 - __bk;
  5754. return (__bk * __dk1 - __bk1 * __dk +
  5755. _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
  5756. __deltad;
  5757. }
  5758. template <class _CharT, class _Traits, class _RT>
  5759. basic_ostream<_CharT, _Traits>&
  5760. operator<<(basic_ostream<_CharT, _Traits>& __os,
  5761. const piecewise_linear_distribution<_RT>& __x)
  5762. {
  5763. __save_flags<_CharT, _Traits> __lx(__os);
  5764. __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
  5765. ios_base::scientific);
  5766. _CharT __sp = __os.widen(' ');
  5767. __os.fill(__sp);
  5768. size_t __n = __x.__p_.__b_.size();
  5769. __os << __n;
  5770. for (size_t __i = 0; __i < __n; ++__i)
  5771. __os << __sp << __x.__p_.__b_[__i];
  5772. __n = __x.__p_.__densities_.size();
  5773. __os << __sp << __n;
  5774. for (size_t __i = 0; __i < __n; ++__i)
  5775. __os << __sp << __x.__p_.__densities_[__i];
  5776. __n = __x.__p_.__areas_.size();
  5777. __os << __sp << __n;
  5778. for (size_t __i = 0; __i < __n; ++__i)
  5779. __os << __sp << __x.__p_.__areas_[__i];
  5780. return __os;
  5781. }
  5782. template <class _CharT, class _Traits, class _RT>
  5783. basic_istream<_CharT, _Traits>&
  5784. operator>>(basic_istream<_CharT, _Traits>& __is,
  5785. piecewise_linear_distribution<_RT>& __x)
  5786. {
  5787. typedef piecewise_linear_distribution<_RT> _Eng;
  5788. typedef typename _Eng::result_type result_type;
  5789. __save_flags<_CharT, _Traits> __lx(__is);
  5790. __is.flags(ios_base::dec | ios_base::skipws);
  5791. size_t __n;
  5792. __is >> __n;
  5793. vector<result_type> __b(__n);
  5794. for (size_t __i = 0; __i < __n; ++__i)
  5795. __is >> __b[__i];
  5796. __is >> __n;
  5797. vector<result_type> __densities(__n);
  5798. for (size_t __i = 0; __i < __n; ++__i)
  5799. __is >> __densities[__i];
  5800. __is >> __n;
  5801. vector<result_type> __areas(__n);
  5802. for (size_t __i = 0; __i < __n; ++__i)
  5803. __is >> __areas[__i];
  5804. if (!__is.fail())
  5805. {
  5806. swap(__x.__p_.__b_, __b);
  5807. swap(__x.__p_.__densities_, __densities);
  5808. swap(__x.__p_.__areas_, __areas);
  5809. }
  5810. return __is;
  5811. }
  5812. _LIBCPP_END_NAMESPACE_STD
  5813. #endif // !defined(_LIBCPP_SGX_CONFIG)
  5814. #endif // _LIBCPP_RANDOM