random 221 KB

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