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