blob: 63a44aae443bcd7fcc6665ccc9d0865c74729be1 [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>
669 class geometric_distribution;
670
671template<class IntType = int>
672 class negative_binomial_distribution;
673
674template<class IntType = int>
Howard Hinnant4ff556c2010-05-14 21:38:54 +0000675class poisson_distribution
676{
677public:
678 // types
679 typedef IntType result_type;
680
681 class param_type
682 {
683 public:
684 typedef poisson_distribution distribution_type;
685
686 explicit param_type(double mean = 1.0);
687
688 double mean() const;
689
690 friend bool operator==(const param_type& x, const param_type& y);
691 friend bool operator!=(const param_type& x, const param_type& y);
692 };
693
694 // constructors and reset functions
695 explicit poisson_distribution(double mean = 1.0);
696 explicit poisson_distribution(const param_type& parm);
697 void reset();
698
699 // generating functions
700 template<class URNG> result_type operator()(URNG& g);
701 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
702
703 // property functions
704 double mean() const;
705
706 param_type param() const;
707 void param(const param_type& parm);
708
709 result_type min() const;
710 result_type max() const;
711
712 friend bool operator==(const poisson_distribution& x,
713 const poisson_distribution& y);
714 friend bool operator!=(const poisson_distribution& x,
715 const poisson_distribution& y);
716
717 template <class charT, class traits>
718 friend
719 basic_ostream<charT, traits>&
720 operator<<(basic_ostream<charT, traits>& os,
721 const poisson_distribution& x);
722
723 template <class charT, class traits>
724 friend
725 basic_istream<charT, traits>&
726 operator>>(basic_istream<charT, traits>& is,
727 poisson_distribution& x);
728};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000729
730template<class RealType = double>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000731class exponential_distribution
732{
733public:
734 // types
735 typedef RealType result_type;
736
737 class param_type
738 {
739 public:
740 typedef exponential_distribution distribution_type;
741
Howard Hinnanta64111c2010-05-12 21:02:31 +0000742 explicit param_type(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000743
Howard Hinnanta64111c2010-05-12 21:02:31 +0000744 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000745
746 friend bool operator==(const param_type& x, const param_type& y);
747 friend bool operator!=(const param_type& x, const param_type& y);
748 };
749
750 // constructors and reset functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000751 explicit exponential_distribution(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000752 explicit exponential_distribution(const param_type& parm);
753 void reset();
754
755 // generating functions
756 template<class URNG> result_type operator()(URNG& g);
757 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
758
759 // property functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000760 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000761
762 param_type param() const;
763 void param(const param_type& parm);
764
765 result_type min() const;
766 result_type max() const;
767
768 friend bool operator==(const exponential_distribution& x,
769 const exponential_distribution& y);
770 friend bool operator!=(const exponential_distribution& x,
771 const exponential_distribution& y);
772
773 template <class charT, class traits>
774 friend
775 basic_ostream<charT, traits>&
776 operator<<(basic_ostream<charT, traits>& os,
777 const exponential_distribution& x);
778
779 template <class charT, class traits>
780 friend
781 basic_istream<charT, traits>&
782 operator>>(basic_istream<charT, traits>& is,
783 exponential_distribution& x);
784};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000785
786template<class RealType = double>
Howard Hinnantc7c49132010-05-13 17:58:28 +0000787class gamma_distribution
788{
789public:
790 // types
791 typedef RealType result_type;
792
793 class param_type
794 {
795 public:
796 typedef gamma_distribution distribution_type;
797
798 explicit param_type(result_type alpha = 1, result_type beta = 1);
799
800 result_type alpha() const;
801 result_type beta() const;
802
803 friend bool operator==(const param_type& x, const param_type& y);
804 friend bool operator!=(const param_type& x, const param_type& y);
805 };
806
807 // constructors and reset functions
808 explicit gamma_distribution(result_type alpha = 1, result_type beta = 1);
809 explicit gamma_distribution(const param_type& parm);
810 void reset();
811
812 // generating functions
813 template<class URNG> result_type operator()(URNG& g);
814 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
815
816 // property functions
817 result_type alpha() const;
818 result_type beta() const;
819
820 param_type param() const;
821 void param(const param_type& parm);
822
823 result_type min() const;
824 result_type max() const;
825
826 friend bool operator==(const gamma_distribution& x,
827 const gamma_distribution& y);
828 friend bool operator!=(const gamma_distribution& x,
829 const gamma_distribution& y);
830
831 template <class charT, class traits>
832 friend
833 basic_ostream<charT, traits>&
834 operator<<(basic_ostream<charT, traits>& os,
835 const gamma_distribution& x);
836
837 template <class charT, class traits>
838 friend
839 basic_istream<charT, traits>&
840 operator>>(basic_istream<charT, traits>& is,
841 gamma_distribution& x);
842};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000843
844template<class RealType = double>
845 class weibull_distribution;
846
847template<class RealType = double>
848 class extreme_value_distribution;
849
850template<class RealType = double>
Howard Hinnanta64111c2010-05-12 21:02:31 +0000851class normal_distribution
852{
853public:
854 // types
855 typedef RealType result_type;
856
857 class param_type
858 {
859 public:
860 typedef normal_distribution distribution_type;
861
862 explicit param_type(result_type mean = 0, result_type stddev = 1);
863
864 result_type mean() const;
865 result_type stddev() const;
866
867 friend bool operator==(const param_type& x, const param_type& y);
868 friend bool operator!=(const param_type& x, const param_type& y);
869 };
870
871 // constructors and reset functions
872 explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
873 explicit normal_distribution(const param_type& parm);
874 void reset();
875
876 // generating functions
877 template<class URNG> result_type operator()(URNG& g);
878 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
879
880 // property functions
881 result_type mean() const;
882 result_type stddev() const;
883
884 param_type param() const;
885 void param(const param_type& parm);
886
887 result_type min() const;
888 result_type max() const;
889
890 friend bool operator==(const normal_distribution& x,
891 const normal_distribution& y);
892 friend bool operator!=(const normal_distribution& x,
893 const normal_distribution& y);
894
895 template <class charT, class traits>
896 friend
897 basic_ostream<charT, traits>&
898 operator<<(basic_ostream<charT, traits>& os,
899 const normal_distribution& x);
900
901 template <class charT, class traits>
902 friend
903 basic_istream<charT, traits>&
904 operator>>(basic_istream<charT, traits>& is,
905 normal_distribution& x);
906};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000907
908template<class RealType = double>
909 class lognormal_distribution;
910
911template<class RealType = double>
912 class chi_squared_distribution;
913
914template<class RealType = double>
915 class cauchy_distribution;
916
917template<class RealType = double>
918 class fisher_f_distribution;
919
920template<class RealType = double>
921 class student_t_distribution;
922
923template<class IntType = int>
924 class discrete_distribution;
925
926template<class RealType = double>
927 class piecewise_constant_distribution;
928
929template<class RealType = double>
930 class piecewise_linear_distribution;
931
932} // std
933*/
934
935#include <__config>
936#include <cstddef>
937#include <type_traits>
938#include <initializer_list>
939#include <cstdint>
940#include <limits>
941#include <algorithm>
942#include <vector>
943#include <string>
944#include <istream>
945#include <ostream>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000946#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947
948#pragma GCC system_header
949
950_LIBCPP_BEGIN_NAMESPACE_STD
951
952// linear_congruential_engine
953
954template <unsigned long long __a, unsigned long long __c,
955 unsigned long long __m, unsigned long long _M,
956 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
957struct __lce_ta;
958
959// 64
960
961template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
962struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
963{
964 typedef unsigned long long result_type;
965 static result_type next(result_type __x)
966 {
967 // Schrage's algorithm
968 const result_type __q = __m / __a;
969 const result_type __r = __m % __a;
970 const result_type __t0 = __a * (__x % __q);
971 const result_type __t1 = __r * (__x / __q);
972 __x = __t0 + (__t0 < __t1) * __m - __t1;
973 __x += __c - (__x >= __m - __c) * __m;
974 return __x;
975 }
976};
977
978template <unsigned long long __a, unsigned long long __m>
979struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
980{
981 typedef unsigned long long result_type;
982 static result_type next(result_type __x)
983 {
984 // Schrage's algorithm
985 const result_type __q = __m / __a;
986 const result_type __r = __m % __a;
987 const result_type __t0 = __a * (__x % __q);
988 const result_type __t1 = __r * (__x / __q);
989 __x = __t0 + (__t0 < __t1) * __m - __t1;
990 return __x;
991 }
992};
993
994template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
995struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
996{
997 typedef unsigned long long result_type;
998 static result_type next(result_type __x)
999 {
1000 return (__a * __x + __c) % __m;
1001 }
1002};
1003
1004template <unsigned long long __a, unsigned long long __c>
1005struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1006{
1007 typedef unsigned long long result_type;
1008 static result_type next(result_type __x)
1009 {
1010 return __a * __x + __c;
1011 }
1012};
1013
1014// 32
1015
1016template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1017struct __lce_ta<_A, _C, _M, unsigned(~0), true>
1018{
1019 typedef unsigned result_type;
1020 static result_type next(result_type __x)
1021 {
1022 const result_type __a = static_cast<result_type>(_A);
1023 const result_type __c = static_cast<result_type>(_C);
1024 const result_type __m = static_cast<result_type>(_M);
1025 // Schrage's algorithm
1026 const result_type __q = __m / __a;
1027 const result_type __r = __m % __a;
1028 const result_type __t0 = __a * (__x % __q);
1029 const result_type __t1 = __r * (__x / __q);
1030 __x = __t0 + (__t0 < __t1) * __m - __t1;
1031 __x += __c - (__x >= __m - __c) * __m;
1032 return __x;
1033 }
1034};
1035
1036template <unsigned long long _A, unsigned long long _M>
1037struct __lce_ta<_A, 0, _M, unsigned(~0), true>
1038{
1039 typedef unsigned result_type;
1040 static result_type next(result_type __x)
1041 {
1042 const result_type __a = static_cast<result_type>(_A);
1043 const result_type __m = static_cast<result_type>(_M);
1044 // Schrage's algorithm
1045 const result_type __q = __m / __a;
1046 const result_type __r = __m % __a;
1047 const result_type __t0 = __a * (__x % __q);
1048 const result_type __t1 = __r * (__x / __q);
1049 __x = __t0 + (__t0 < __t1) * __m - __t1;
1050 return __x;
1051 }
1052};
1053
1054template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1055struct __lce_ta<_A, _C, _M, unsigned(~0), false>
1056{
1057 typedef unsigned result_type;
1058 static result_type next(result_type __x)
1059 {
1060 const result_type __a = static_cast<result_type>(_A);
1061 const result_type __c = static_cast<result_type>(_C);
1062 const result_type __m = static_cast<result_type>(_M);
1063 return (__a * __x + __c) % __m;
1064 }
1065};
1066
1067template <unsigned long long _A, unsigned long long _C>
1068struct __lce_ta<_A, _C, 0, unsigned(~0), false>
1069{
1070 typedef unsigned result_type;
1071 static result_type next(result_type __x)
1072 {
1073 const result_type __a = static_cast<result_type>(_A);
1074 const result_type __c = static_cast<result_type>(_C);
1075 return __a * __x + __c;
1076 }
1077};
1078
1079// 16
1080
1081template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1082struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1083{
1084 typedef unsigned short result_type;
1085 static result_type next(result_type __x)
1086 {
1087 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1088 }
1089};
1090
1091template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1092class linear_congruential_engine;
1093
1094template <class _CharT, class _Traits,
1095 class _U, _U _A, _U _C, _U _N>
1096basic_ostream<_CharT, _Traits>&
1097operator<<(basic_ostream<_CharT, _Traits>& __os,
1098 const linear_congruential_engine<_U, _A, _C, _N>&);
1099
1100template <class _CharT, class _Traits,
1101 class _U, _U _A, _U _C, _U _N>
1102basic_istream<_CharT, _Traits>&
1103operator>>(basic_istream<_CharT, _Traits>& __is,
1104 linear_congruential_engine<_U, _A, _C, _N>& __x);
1105
1106template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1107class linear_congruential_engine
1108{
1109public:
1110 // types
1111 typedef _UIntType result_type;
1112
1113private:
1114 result_type __x_;
1115
1116 static const result_type _M = result_type(~0);
1117
1118 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1119 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1120public:
1121 static const result_type _Min = __c == 0u ? 1u: 0u;
1122 static const result_type _Max = __m - 1u;
1123 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1124
1125 // engine characteristics
1126 static const/*expr*/ result_type multiplier = __a;
1127 static const/*expr*/ result_type increment = __c;
1128 static const/*expr*/ result_type modulus = __m;
1129 static const/*expr*/ result_type min() {return _Min;}
1130 static const/*expr*/ result_type max() {return _Max;}
1131 static const/*expr*/ result_type default_seed = 1u;
1132
1133 // constructors and seeding functions
1134 explicit linear_congruential_engine(result_type __s = default_seed)
1135 {seed(__s);}
1136 template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q)
1137 {seed(__q);}
1138 void seed(result_type __s = default_seed)
1139 {seed(integral_constant<bool, __m == 0>(),
1140 integral_constant<bool, __c == 0>(), __s);}
1141 template<class _Sseq>
1142 typename enable_if
1143 <
1144 !is_convertible<_Sseq, result_type>::value,
1145 void
1146 >::type
1147 seed(_Sseq& __q)
1148 {__seed(__q, integral_constant<unsigned,
1149 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1150 : (__m-1) / 0x100000000ull)>());}
1151
1152 // generating functions
1153 result_type operator()()
1154 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
1155 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1156
1157 friend bool operator==(const linear_congruential_engine& __x,
1158 const linear_congruential_engine& __y)
1159 {return __x.__x_ == __y.__x_;}
1160 friend bool operator!=(const linear_congruential_engine& __x,
1161 const linear_congruential_engine& __y)
1162 {return !(__x == __y);}
1163
1164private:
1165
1166 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
1167 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
1168 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1169 1 : __s % __m;}
1170 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1171
1172 template<class _Sseq>
1173 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1174 template<class _Sseq>
1175 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1176
1177 template <class _CharT, class _Traits,
1178 class _U, _U _A, _U _C, _U _N>
1179 friend
1180 basic_ostream<_CharT, _Traits>&
1181 operator<<(basic_ostream<_CharT, _Traits>& __os,
1182 const linear_congruential_engine<_U, _A, _C, _N>&);
1183
1184 template <class _CharT, class _Traits,
1185 class _U, _U _A, _U _C, _U _N>
1186 friend
1187 basic_istream<_CharT, _Traits>&
1188 operator>>(basic_istream<_CharT, _Traits>& __is,
1189 linear_congruential_engine<_U, _A, _C, _N>& __x);
1190};
1191
1192template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1193template<class _Sseq>
1194void
1195linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1196 integral_constant<unsigned, 1>)
1197{
1198 const unsigned __k = 1;
1199 uint32_t __ar[__k+3];
1200 __q.generate(__ar, __ar + __k + 3);
1201 result_type __s = static_cast<result_type>(__ar[3] % __m);
1202 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1203}
1204
1205template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1206template<class _Sseq>
1207void
1208linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1209 integral_constant<unsigned, 2>)
1210{
1211 const unsigned __k = 2;
1212 uint32_t __ar[__k+3];
1213 __q.generate(__ar, __ar + __k + 3);
1214 result_type __s = static_cast<result_type>((__ar[3] +
1215 (uint64_t)__ar[4] << 32) % __m);
1216 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1217}
1218
1219template <class _CharT, class _Traits>
1220class __save_flags
1221{
1222 typedef basic_ios<_CharT, _Traits> __stream_type;
1223 typedef typename __stream_type::fmtflags fmtflags;
1224
1225 __stream_type& __stream_;
1226 fmtflags __fmtflags_;
1227 _CharT __fill_;
1228
1229 __save_flags(const __save_flags&);
1230 __save_flags& operator=(const __save_flags&);
1231public:
1232 explicit __save_flags(__stream_type& __stream)
1233 : __stream_(__stream),
1234 __fmtflags_(__stream.flags()),
1235 __fill_(__stream.fill())
1236 {}
1237 ~__save_flags()
1238 {
1239 __stream_.flags(__fmtflags_);
1240 __stream_.fill(__fill_);
1241 }
1242};
1243
1244template <class _CharT, class _Traits,
1245 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1246inline
1247basic_ostream<_CharT, _Traits>&
1248operator<<(basic_ostream<_CharT, _Traits>& __os,
1249 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1250{
1251 __save_flags<_CharT, _Traits> _(__os);
1252 __os.flags(ios_base::dec | ios_base::left);
1253 __os.fill(__os.widen(' '));
1254 return __os << __x.__x_;
1255}
1256
1257template <class _CharT, class _Traits,
1258 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1259basic_istream<_CharT, _Traits>&
1260operator>>(basic_istream<_CharT, _Traits>& __is,
1261 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1262{
1263 __save_flags<_CharT, _Traits> _(__is);
1264 __is.flags(ios_base::dec | ios_base::skipws);
1265 _UIntType __t;
1266 __is >> __t;
1267 if (!__is.fail())
1268 __x.__x_ = __t;
1269 return __is;
1270}
1271
1272typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
1273 minstd_rand0;
1274typedef minstd_rand0 default_random_engine;
1275typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
1276 minstd_rand;
1277// mersenne_twister_engine
1278
1279template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1280 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1281 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1282class mersenne_twister_engine;
1283
1284template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1285 _UI _A, size_t _U, _UI _D, size_t _S,
1286 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1287bool
1288operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1289 _B, _T, _C, _L, _F>& __x,
1290 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1291 _B, _T, _C, _L, _F>& __y);
1292
1293template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1294 _UI _A, size_t _U, _UI _D, size_t _S,
1295 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1296bool
1297operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1298 _B, _T, _C, _L, _F>& __x,
1299 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1300 _B, _T, _C, _L, _F>& __y);
1301
1302template <class _CharT, class _Traits,
1303 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1304 _UI _A, size_t _U, _UI _D, size_t _S,
1305 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1306basic_ostream<_CharT, _Traits>&
1307operator<<(basic_ostream<_CharT, _Traits>& __os,
1308 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1309 _B, _T, _C, _L, _F>& __x);
1310
1311template <class _CharT, class _Traits,
1312 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1313 _UI _A, size_t _U, _UI _D, size_t _S,
1314 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1315basic_istream<_CharT, _Traits>&
1316operator>>(basic_istream<_CharT, _Traits>& __is,
1317 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1318 _B, _T, _C, _L, _F>& __x);
1319
1320template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1321 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1322 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1323class mersenne_twister_engine
1324{
1325public:
1326 // types
1327 typedef _UIntType result_type;
1328
1329private:
1330 result_type __x_[__n];
1331 size_t __i_;
1332
1333 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
1334 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
1335 static const result_type _Dt = numeric_limits<result_type>::digits;
1336 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
1337 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
1338 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
1339 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
1340 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
1341 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
1342 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
1343public:
1344 static const result_type _Min = 0;
1345 static const result_type _Max = __w == _Dt ? result_type(~0) :
1346 (result_type(1) << __w) - result_type(1);
1347 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
1348 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
1349 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
1350 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
1351 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
1352 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
1353
1354 // engine characteristics
1355 static const/*expr*/ size_t word_size = __w;
1356 static const/*expr*/ size_t state_size = __n;
1357 static const/*expr*/ size_t shift_size = __m;
1358 static const/*expr*/ size_t mask_bits = __r;
1359 static const/*expr*/ result_type xor_mask = __a;
1360 static const/*expr*/ size_t tempering_u = __u;
1361 static const/*expr*/ result_type tempering_d = __d;
1362 static const/*expr*/ size_t tempering_s = __s;
1363 static const/*expr*/ result_type tempering_b = __b;
1364 static const/*expr*/ size_t tempering_t = __t;
1365 static const/*expr*/ result_type tempering_c = __c;
1366 static const/*expr*/ size_t tempering_l = __l;
1367 static const/*expr*/ result_type initialization_multiplier = __f;
1368 static const/*expr*/ result_type min() { return _Min; }
1369 static const/*expr*/ result_type max() { return _Max; }
1370 static const/*expr*/ result_type default_seed = 5489u;
1371
1372 // constructors and seeding functions
1373 explicit mersenne_twister_engine(result_type __sd = default_seed)
1374 {seed(__sd);}
1375 template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q)
1376 {seed(__q);}
1377 void seed(result_type __sd = default_seed);
1378 template<class _Sseq>
1379 typename enable_if
1380 <
1381 !is_convertible<_Sseq, result_type>::value,
1382 void
1383 >::type
1384 seed(_Sseq& __q)
1385 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1386
1387 // generating functions
1388 result_type operator()();
1389 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1390
1391 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1392 _UI _A, size_t _U, _UI _D, size_t _S,
1393 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1394 friend
1395 bool
1396 operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1397 _B, _T, _C, _L, _F>& __x,
1398 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1399 _B, _T, _C, _L, _F>& __y);
1400
1401 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1402 _UI _A, size_t _U, _UI _D, size_t _S,
1403 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1404 friend
1405 bool
1406 operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1407 _B, _T, _C, _L, _F>& __x,
1408 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1409 _B, _T, _C, _L, _F>& __y);
1410
1411 template <class _CharT, class _Traits,
1412 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1413 _UI _A, size_t _U, _UI _D, size_t _S,
1414 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1415 friend
1416 basic_ostream<_CharT, _Traits>&
1417 operator<<(basic_ostream<_CharT, _Traits>& __os,
1418 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1419 _B, _T, _C, _L, _F>& __x);
1420
1421 template <class _CharT, class _Traits,
1422 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1423 _UI _A, size_t _U, _UI _D, size_t _S,
1424 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1425 friend
1426 basic_istream<_CharT, _Traits>&
1427 operator>>(basic_istream<_CharT, _Traits>& __is,
1428 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1429 _B, _T, _C, _L, _F>& __x);
1430private:
1431
1432 template<class _Sseq>
1433 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1434 template<class _Sseq>
1435 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1436
1437 template <size_t __count>
1438 static
1439 typename enable_if
1440 <
1441 __count < __w,
1442 result_type
1443 >::type
1444 __lshift(result_type __x) {return (__x << __count) & _Max;}
1445
1446 template <size_t __count>
1447 static
1448 typename enable_if
1449 <
1450 (__count >= __w),
1451 result_type
1452 >::type
1453 __lshift(result_type __x) {return result_type(0);}
1454
1455 template <size_t __count>
1456 static
1457 typename enable_if
1458 <
1459 __count < _Dt,
1460 result_type
1461 >::type
1462 __rshift(result_type __x) {return __x >> __count;}
1463
1464 template <size_t __count>
1465 static
1466 typename enable_if
1467 <
1468 (__count >= _Dt),
1469 result_type
1470 >::type
1471 __rshift(result_type __x) {return result_type(0);}
1472};
1473
1474template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1475 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1476 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1477void
1478mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1479 __t, __c, __l, __f>::seed(result_type __sd)
1480{ // __w >= 2
1481 __x_[0] = __sd & _Max;
1482 for (size_t __i = 1; __i < __n; ++__i)
1483 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
1484 __i_ = 0;
1485}
1486
1487template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1488 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1489 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1490template<class _Sseq>
1491void
1492mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1493 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
1494{
1495 const unsigned __k = 1;
1496 uint32_t __ar[__n * __k];
1497 __q.generate(__ar, __ar + __n * __k);
1498 for (size_t __i = 0; __i < __n; ++__i)
1499 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1500 const result_type __mask = __r == _Dt ? result_type(~0) :
1501 (result_type(1) << __r) - result_type(1);
1502 __i_ = 0;
1503 if ((__x_[0] & ~__mask) == 0)
1504 {
1505 for (size_t __i = 1; __i < __n; ++__i)
1506 if (__x_[__i] != 0)
1507 return;
1508 __x_[0] = _Max;
1509 }
1510}
1511
1512template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1513 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1514 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1515template<class _Sseq>
1516void
1517mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1518 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
1519{
1520 const unsigned __k = 2;
1521 uint32_t __ar[__n * __k];
1522 __q.generate(__ar, __ar + __n * __k);
1523 for (size_t __i = 0; __i < __n; ++__i)
1524 __x_[__i] = static_cast<result_type>(
1525 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1526 const result_type __mask = __r == _Dt ? result_type(~0) :
1527 (result_type(1) << __r) - result_type(1);
1528 __i_ = 0;
1529 if ((__x_[0] & ~__mask) == 0)
1530 {
1531 for (size_t __i = 1; __i < __n; ++__i)
1532 if (__x_[__i] != 0)
1533 return;
1534 __x_[0] = _Max;
1535 }
1536}
1537
1538template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1539 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1540 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1541_UIntType
1542mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1543 __t, __c, __l, __f>::operator()()
1544{
1545 const size_t __j = (__i_ + 1) % __n;
1546 const result_type __mask = __r == _Dt ? result_type(~0) :
1547 (result_type(1) << __r) - result_type(1);
1548 const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
1549 const size_t __k = (__i_ + __m) % __n;
1550 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
1551 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
1552 __i_ = __j;
1553 __z ^= __lshift<__s>(__z) & __b;
1554 __z ^= __lshift<__t>(__z) & __c;
1555 return __z ^ __rshift<__l>(__z);
1556}
1557
1558template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1559 _UI _A, size_t _U, _UI _D, size_t _S,
1560 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1561bool
1562operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1563 _B, _T, _C, _L, _F>& __x,
1564 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1565 _B, _T, _C, _L, _F>& __y)
1566{
1567 if (__x.__i_ == __y.__i_)
1568 return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
1569 if (__x.__i_ == 0 || __y.__i_ == 0)
1570 {
1571 size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
1572 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1573 __y.__x_ + __y.__i_))
1574 return false;
1575 if (__x.__i_ == 0)
1576 return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
1577 return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
1578 }
1579 if (__x.__i_ < __y.__i_)
1580 {
1581 size_t __j = _N - __y.__i_;
1582 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1583 __y.__x_ + __y.__i_))
1584 return false;
1585 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
1586 __y.__x_))
1587 return false;
1588 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1589 __y.__x_ + (_N - (__x.__i_ + __j)));
1590 }
1591 size_t __j = _N - __x.__i_;
1592 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1593 __x.__x_ + __x.__i_))
1594 return false;
1595 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
1596 __x.__x_))
1597 return false;
1598 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1599 __x.__x_ + (_N - (__y.__i_ + __j)));
1600}
1601
1602template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1603 _UI _A, size_t _U, _UI _D, size_t _S,
1604 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1605inline
1606bool
1607operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1608 _B, _T, _C, _L, _F>& __x,
1609 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1610 _B, _T, _C, _L, _F>& __y)
1611{
1612 return !(__x == __y);
1613}
1614
1615template <class _CharT, class _Traits,
1616 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1617 _UI _A, size_t _U, _UI _D, size_t _S,
1618 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1619basic_ostream<_CharT, _Traits>&
1620operator<<(basic_ostream<_CharT, _Traits>& __os,
1621 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1622 _B, _T, _C, _L, _F>& __x)
1623{
1624 __save_flags<_CharT, _Traits> _(__os);
1625 __os.flags(ios_base::dec | ios_base::left);
1626 _CharT __sp = __os.widen(' ');
1627 __os.fill(__sp);
1628 __os << __x.__x_[__x.__i_];
1629 for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
1630 __os << __sp << __x.__x_[__j];
1631 for (size_t __j = 0; __j < __x.__i_; ++__j)
1632 __os << __sp << __x.__x_[__j];
1633 return __os;
1634}
1635
1636template <class _CharT, class _Traits,
1637 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1638 _UI _A, size_t _U, _UI _D, size_t _S,
1639 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1640basic_istream<_CharT, _Traits>&
1641operator>>(basic_istream<_CharT, _Traits>& __is,
1642 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1643 _B, _T, _C, _L, _F>& __x)
1644{
1645 __save_flags<_CharT, _Traits> _(__is);
1646 __is.flags(ios_base::dec | ios_base::skipws);
1647 _UI __t[_N];
1648 for (size_t __i = 0; __i < _N; ++__i)
1649 __is >> __t[__i];
1650 if (!__is.fail())
1651 {
1652 for (size_t __i = 0; __i < _N; ++__i)
1653 __x.__x_[__i] = __t[__i];
1654 __x.__i_ = 0;
1655 }
1656 return __is;
1657}
1658
1659typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
1660 0x9908b0df, 11, 0xffffffff,
1661 7, 0x9d2c5680,
1662 15, 0xefc60000,
1663 18, 1812433253> mt19937;
1664typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
1665 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
1666 17, 0x71d67fffeda60000ULL,
1667 37, 0xfff7eee000000000ULL,
1668 43, 6364136223846793005ULL> mt19937_64;
1669
1670// subtract_with_carry_engine
1671
1672template<class _UIntType, size_t __w, size_t __s, size_t __r>
1673class subtract_with_carry_engine;
1674
1675template<class _UI, size_t _W, size_t _S, size_t _R>
1676bool
1677operator==(
1678 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1679 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1680
1681template<class _UI, size_t _W, size_t _S, size_t _R>
1682bool
1683operator!=(
1684 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1685 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1686
1687template <class _CharT, class _Traits,
1688 class _UI, size_t _W, size_t _S, size_t _R>
1689basic_ostream<_CharT, _Traits>&
1690operator<<(basic_ostream<_CharT, _Traits>& __os,
1691 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1692
1693template <class _CharT, class _Traits,
1694 class _UI, size_t _W, size_t _S, size_t _R>
1695basic_istream<_CharT, _Traits>&
1696operator>>(basic_istream<_CharT, _Traits>& __is,
1697 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1698
1699template<class _UIntType, size_t __w, size_t __s, size_t __r>
1700class subtract_with_carry_engine
1701{
1702public:
1703 // types
1704 typedef _UIntType result_type;
1705
1706private:
1707 result_type __x_[__r];
1708 result_type __c_;
1709 size_t __i_;
1710
1711 static const result_type _Dt = numeric_limits<result_type>::digits;
1712 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
1713 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
1714 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
1715 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
1716public:
1717 static const result_type _Min = 0;
1718 static const result_type _Max = __w == _Dt ? result_type(~0) :
1719 (result_type(1) << __w) - result_type(1);
1720 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
1721
1722 // engine characteristics
1723 static const/*expr*/ size_t word_size = __w;
1724 static const/*expr*/ size_t short_lag = __s;
1725 static const/*expr*/ size_t long_lag = __r;
1726 static const/*expr*/ result_type min() { return _Min; }
1727 static const/*expr*/ result_type max() { return _Max; }
1728 static const/*expr*/ result_type default_seed = 19780503u;
1729
1730 // constructors and seeding functions
1731 explicit subtract_with_carry_engine(result_type __sd = default_seed)
1732 {seed(__sd);}
1733 template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q)
1734 {seed(__q);}
1735 void seed(result_type __sd = default_seed)
1736 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1737 template<class _Sseq>
1738 typename enable_if
1739 <
1740 !is_convertible<_Sseq, result_type>::value,
1741 void
1742 >::type
1743 seed(_Sseq& __q)
1744 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1745
1746 // generating functions
1747 result_type operator()();
1748 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1749
1750 template<class _UI, size_t _W, size_t _S, size_t _R>
1751 friend
1752 bool
1753 operator==(
1754 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1755 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1756
1757 template<class _UI, size_t _W, size_t _S, size_t _R>
1758 friend
1759 bool
1760 operator!=(
1761 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1762 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1763
1764 template <class _CharT, class _Traits,
1765 class _UI, size_t _W, size_t _S, size_t _R>
1766 friend
1767 basic_ostream<_CharT, _Traits>&
1768 operator<<(basic_ostream<_CharT, _Traits>& __os,
1769 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1770
1771 template <class _CharT, class _Traits,
1772 class _UI, size_t _W, size_t _S, size_t _R>
1773 friend
1774 basic_istream<_CharT, _Traits>&
1775 operator>>(basic_istream<_CharT, _Traits>& __is,
1776 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1777
1778private:
1779
1780 void seed(result_type __sd, integral_constant<unsigned, 1>);
1781 void seed(result_type __sd, integral_constant<unsigned, 2>);
1782 template<class _Sseq>
1783 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1784 template<class _Sseq>
1785 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1786};
1787
1788template<class _UIntType, size_t __w, size_t __s, size_t __r>
1789void
1790subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1791 integral_constant<unsigned, 1>)
1792{
1793 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1794 __e(__sd == 0u ? default_seed : __sd);
1795 for (size_t __i = 0; __i < __r; ++__i)
1796 __x_[__i] = static_cast<result_type>(__e() & _Max);
1797 __c_ = __x_[__r-1] == 0;
1798 __i_ = 0;
1799}
1800
1801template<class _UIntType, size_t __w, size_t __s, size_t __r>
1802void
1803subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1804 integral_constant<unsigned, 2>)
1805{
1806 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1807 __e(__sd == 0u ? default_seed : __sd);
1808 for (size_t __i = 0; __i < __r; ++__i)
1809 __x_[__i] = static_cast<result_type>(
1810 (__e() + ((uint64_t)__e() << 32)) & _Max);
1811 __c_ = __x_[__r-1] == 0;
1812 __i_ = 0;
1813}
1814
1815template<class _UIntType, size_t __w, size_t __s, size_t __r>
1816template<class _Sseq>
1817void
1818subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1819 integral_constant<unsigned, 1>)
1820{
1821 const unsigned __k = 1;
1822 uint32_t __ar[__r * __k];
1823 __q.generate(__ar, __ar + __r * __k);
1824 for (size_t __i = 0; __i < __r; ++__i)
1825 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1826 __c_ = __x_[__r-1] == 0;
1827 __i_ = 0;
1828}
1829
1830template<class _UIntType, size_t __w, size_t __s, size_t __r>
1831template<class _Sseq>
1832void
1833subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1834 integral_constant<unsigned, 2>)
1835{
1836 const unsigned __k = 2;
1837 uint32_t __ar[__r * __k];
1838 __q.generate(__ar, __ar + __r * __k);
1839 for (size_t __i = 0; __i < __r; ++__i)
1840 __x_[__i] = static_cast<result_type>(
1841 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1842 __c_ = __x_[__r-1] == 0;
1843 __i_ = 0;
1844}
1845
1846template<class _UIntType, size_t __w, size_t __s, size_t __r>
1847_UIntType
1848subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
1849{
1850 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
1851 result_type& __xr = __x_[__i_];
1852 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
1853 __xr = (__xs - __xr - __c_) & _Max;
1854 __c_ = __new_c;
1855 __i_ = (__i_ + 1) % __r;
1856 return __xr;
1857}
1858
1859template<class _UI, size_t _W, size_t _S, size_t _R>
1860bool
1861operator==(
1862 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1863 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
1864{
1865 if (__x.__c_ != __y.__c_)
1866 return false;
1867 if (__x.__i_ == __y.__i_)
1868 return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
1869 if (__x.__i_ == 0 || __y.__i_ == 0)
1870 {
1871 size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
1872 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1873 __y.__x_ + __y.__i_))
1874 return false;
1875 if (__x.__i_ == 0)
1876 return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
1877 return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
1878 }
1879 if (__x.__i_ < __y.__i_)
1880 {
1881 size_t __j = _R - __y.__i_;
1882 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1883 __y.__x_ + __y.__i_))
1884 return false;
1885 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
1886 __y.__x_))
1887 return false;
1888 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1889 __y.__x_ + (_R - (__x.__i_ + __j)));
1890 }
1891 size_t __j = _R - __x.__i_;
1892 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1893 __x.__x_ + __x.__i_))
1894 return false;
1895 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
1896 __x.__x_))
1897 return false;
1898 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1899 __x.__x_ + (_R - (__y.__i_ + __j)));
1900}
1901
1902template<class _UI, size_t _W, size_t _S, size_t _R>
1903inline
1904bool
1905operator!=(
1906 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1907 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
1908{
1909 return !(__x == __y);
1910}
1911
1912template <class _CharT, class _Traits,
1913 class _UI, size_t _W, size_t _S, size_t _R>
1914basic_ostream<_CharT, _Traits>&
1915operator<<(basic_ostream<_CharT, _Traits>& __os,
1916 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
1917{
1918 __save_flags<_CharT, _Traits> _(__os);
1919 __os.flags(ios_base::dec | ios_base::left);
1920 _CharT __sp = __os.widen(' ');
1921 __os.fill(__sp);
1922 __os << __x.__x_[__x.__i_];
1923 for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
1924 __os << __sp << __x.__x_[__j];
1925 for (size_t __j = 0; __j < __x.__i_; ++__j)
1926 __os << __sp << __x.__x_[__j];
1927 __os << __sp << __x.__c_;
1928 return __os;
1929}
1930
1931template <class _CharT, class _Traits,
1932 class _UI, size_t _W, size_t _S, size_t _R>
1933basic_istream<_CharT, _Traits>&
1934operator>>(basic_istream<_CharT, _Traits>& __is,
1935 subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
1936{
1937 __save_flags<_CharT, _Traits> _(__is);
1938 __is.flags(ios_base::dec | ios_base::skipws);
1939 _UI __t[_R+1];
1940 for (size_t __i = 0; __i < _R+1; ++__i)
1941 __is >> __t[__i];
1942 if (!__is.fail())
1943 {
1944 for (size_t __i = 0; __i < _R; ++__i)
1945 __x.__x_[__i] = __t[__i];
1946 __x.__c_ = __t[_R];
1947 __x.__i_ = 0;
1948 }
1949 return __is;
1950}
1951
1952typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
1953typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
1954
1955// discard_block_engine
1956
1957template<class _Engine, size_t __p, size_t __r>
1958class discard_block_engine
1959{
1960 _Engine __e_;
1961 int __n_;
1962
1963 static_assert( 0 < __r, "discard_block_engine invalid parameters");
1964 static_assert(__r <= __p, "discard_block_engine invalid parameters");
1965public:
1966 // types
1967 typedef typename _Engine::result_type result_type;
1968
1969 // engine characteristics
1970 static const/*expr*/ size_t block_size = __p;
1971 static const/*expr*/ size_t used_block = __r;
1972
1973 // Temporary work around for lack of constexpr
1974 static const result_type _Min = _Engine::_Min;
1975 static const result_type _Max = _Engine::_Max;
1976
1977 static const/*expr*/ result_type min() { return _Engine::min(); }
1978 static const/*expr*/ result_type max() { return _Engine::max(); }
1979
1980 // constructors and seeding functions
1981 discard_block_engine() : __n_(0) {}
1982// explicit discard_block_engine(const _Engine& __e);
1983// explicit discard_block_engine(_Engine&& __e);
1984 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
1985 template<class _Sseq> explicit discard_block_engine(_Sseq& __q)
1986 : __e_(__q), __n_(0) {}
1987 void seed() {__e_.seed(); __n_ = 0;}
1988 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
1989 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
1990
1991 // generating functions
1992 result_type operator()();
1993 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1994
1995 // property functions
1996 const _Engine& base() const {return __e_;}
1997
1998 template<class _Eng, size_t _P, size_t _R>
1999 friend
2000 bool
2001 operator==(
2002 const discard_block_engine<_Eng, _P, _R>& __x,
2003 const discard_block_engine<_Eng, _P, _R>& __y);
2004
2005 template<class _Eng, size_t _P, size_t _R>
2006 friend
2007 bool
2008 operator!=(
2009 const discard_block_engine<_Eng, _P, _R>& __x,
2010 const discard_block_engine<_Eng, _P, _R>& __y);
2011
2012 template <class _CharT, class _Traits,
2013 class _Eng, size_t _P, size_t _R>
2014 friend
2015 basic_ostream<_CharT, _Traits>&
2016 operator<<(basic_ostream<_CharT, _Traits>& __os,
2017 const discard_block_engine<_Eng, _P, _R>& __x);
2018
2019 template <class _CharT, class _Traits,
2020 class _Eng, size_t _P, size_t _R>
2021 friend
2022 basic_istream<_CharT, _Traits>&
2023 operator>>(basic_istream<_CharT, _Traits>& __is,
2024 discard_block_engine<_Eng, _P, _R>& __x);
2025};
2026
2027template<class _Engine, size_t __p, size_t __r>
2028typename discard_block_engine<_Engine, __p, __r>::result_type
2029discard_block_engine<_Engine, __p, __r>::operator()()
2030{
2031 if (__n_ >= __r)
2032 {
2033 __e_.discard(__p - __r);
2034 __n_ = 0;
2035 }
2036 ++__n_;
2037 return __e_();
2038}
2039
2040template<class _Eng, size_t _P, size_t _R>
2041inline
2042bool
2043operator==(const discard_block_engine<_Eng, _P, _R>& __x,
2044 const discard_block_engine<_Eng, _P, _R>& __y)
2045{
2046 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2047}
2048
2049template<class _Eng, size_t _P, size_t _R>
2050inline
2051bool
2052operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
2053 const discard_block_engine<_Eng, _P, _R>& __y)
2054{
2055 return !(__x == __y);
2056}
2057
2058template <class _CharT, class _Traits,
2059 class _Eng, size_t _P, size_t _R>
2060basic_ostream<_CharT, _Traits>&
2061operator<<(basic_ostream<_CharT, _Traits>& __os,
2062 const discard_block_engine<_Eng, _P, _R>& __x)
2063{
2064 __save_flags<_CharT, _Traits> _(__os);
2065 __os.flags(ios_base::dec | ios_base::left);
2066 _CharT __sp = __os.widen(' ');
2067 __os.fill(__sp);
2068 return __os << __x.__e_ << __sp << __x.__n_;
2069}
2070
2071template <class _CharT, class _Traits,
2072 class _Eng, size_t _P, size_t _R>
2073basic_istream<_CharT, _Traits>&
2074operator>>(basic_istream<_CharT, _Traits>& __is,
2075 discard_block_engine<_Eng, _P, _R>& __x)
2076{
2077 __save_flags<_CharT, _Traits> _(__is);
2078 __is.flags(ios_base::dec | ios_base::skipws);
2079 _Eng __e;
2080 int __n;
2081 __is >> __e >> __n;
2082 if (!__is.fail())
2083 {
2084 __x.__e_ = __e;
2085 __x.__n_ = __n;
2086 }
2087 return __is;
2088}
2089
2090typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2091typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2092
2093// independent_bits_engine
2094
2095template <unsigned long long _X, size_t _R>
2096struct __log2_imp
2097{
2098 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
2099 : __log2_imp<_X, _R - 1>::value;
2100};
2101
2102template <unsigned long long _X>
2103struct __log2_imp<_X, 0>
2104{
2105 static const size_t value = 0;
2106};
2107
2108template <size_t _R>
2109struct __log2_imp<0, _R>
2110{
2111 static const size_t value = _R + 1;
2112};
2113
2114template <class _UI, _UI _X>
2115struct __log2
2116{
2117 static const size_t value = __log2_imp<_X,
2118 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2119};
2120
2121template<class _Engine, size_t __w, class _UIntType>
2122class independent_bits_engine
2123{
2124 template <class _UI, _UI _R0, size_t _W, size_t _M>
2125 class __get_n
2126 {
2127 static const size_t _Dt = numeric_limits<_UI>::digits;
2128 static const size_t _N = _W / _M + (_W % _M != 0);
2129 static const size_t _W0 = _W / _N;
2130 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
2131 public:
2132 static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
2133 };
2134public:
2135 // types
2136 typedef _UIntType result_type;
2137
2138private:
2139 _Engine __e_;
2140
2141 static const result_type _Dt = numeric_limits<result_type>::digits;
2142 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2143 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2144
2145 typedef typename _Engine::result_type _Engine_result_type;
2146 typedef typename conditional
2147 <
2148 sizeof(_Engine_result_type) <= sizeof(result_type),
2149 result_type,
2150 _Engine_result_type
2151 >::type _Working_result_type;
2152 // Temporary work around for lack of constexpr
2153 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2154 + _Working_result_type(1);
2155 static const size_t __m = __log2<_Working_result_type, _R>::value;
2156 static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
2157 static const size_t __w0 = __w / __n;
2158 static const size_t __n0 = __n - __w % __n;
2159 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2160 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2161 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2162 (_R >> __w0) << __w0;
2163 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2164 (_R >> (__w0+1)) << (__w0+1);
2165 static const _Engine_result_type __mask0 = __w0 > 0 ?
2166 _Engine_result_type(~0) >> (_EDt - __w0) :
2167 _Engine_result_type(0);
2168 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2169 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2170 _Engine_result_type(~0);
2171public:
2172 static const result_type _Min = 0;
2173 static const result_type _Max = __w == _Dt ? result_type(~0) :
2174 (result_type(1) << __w) - result_type(1);
2175 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2176
2177 // engine characteristics
2178 static const/*expr*/ result_type min() { return _Min; }
2179 static const/*expr*/ result_type max() { return _Max; }
2180
2181 // constructors and seeding functions
2182 independent_bits_engine() {}
2183// explicit independent_bits_engine(const _Engine& __e);
2184// explicit independent_bits_engine(_Engine&& __e);
2185 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
2186 template<class _Sseq> explicit independent_bits_engine(_Sseq& __q)
2187 : __e_(__q) {}
2188 void seed() {__e_.seed();}
2189 void seed(result_type __sd) {__e_.seed(__sd);}
2190 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);}
2191
2192 // generating functions
2193 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2194 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2195
2196 // property functions
2197 const _Engine& base() const {return __e_;}
2198
2199 template<class _Eng, size_t _W, class _UI>
2200 friend
2201 bool
2202 operator==(
2203 const independent_bits_engine<_Eng, _W, _UI>& __x,
2204 const independent_bits_engine<_Eng, _W, _UI>& __y);
2205
2206 template<class _Eng, size_t _W, class _UI>
2207 friend
2208 bool
2209 operator!=(
2210 const independent_bits_engine<_Eng, _W, _UI>& __x,
2211 const independent_bits_engine<_Eng, _W, _UI>& __y);
2212
2213 template <class _CharT, class _Traits,
2214 class _Eng, size_t _W, class _UI>
2215 friend
2216 basic_ostream<_CharT, _Traits>&
2217 operator<<(basic_ostream<_CharT, _Traits>& __os,
2218 const independent_bits_engine<_Eng, _W, _UI>& __x);
2219
2220 template <class _CharT, class _Traits,
2221 class _Eng, size_t _W, class _UI>
2222 friend
2223 basic_istream<_CharT, _Traits>&
2224 operator>>(basic_istream<_CharT, _Traits>& __is,
2225 independent_bits_engine<_Eng, _W, _UI>& __x);
2226
2227private:
2228 result_type __eval(false_type);
2229 result_type __eval(true_type);
2230
2231 template <size_t __count>
2232 static
2233 typename enable_if
2234 <
2235 __count < _Dt,
2236 result_type
2237 >::type
2238 __lshift(result_type __x) {return __x << __count;}
2239
2240 template <size_t __count>
2241 static
2242 typename enable_if
2243 <
2244 (__count >= _Dt),
2245 result_type
2246 >::type
2247 __lshift(result_type __x) {return result_type(0);}
2248};
2249
2250template<class _Engine, size_t __w, class _UIntType>
2251inline
2252_UIntType
2253independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
2254{
2255 return static_cast<result_type>(__e_() & __mask0);
2256}
2257
2258template<class _Engine, size_t __w, class _UIntType>
2259_UIntType
2260independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
2261{
2262 result_type _S = 0;
2263 for (size_t __k = 0; __k < __n0; ++__k)
2264 {
2265 _Engine_result_type __u;
2266 do
2267 {
2268 __u = __e_() - _Engine::min();
2269 } while (__u >= __y0);
2270 _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
2271 }
2272 for (size_t __k = __n0; __k < __n; ++__k)
2273 {
2274 _Engine_result_type __u;
2275 do
2276 {
2277 __u = __e_() - _Engine::min();
2278 } while (__u >= __y1);
2279 _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
2280 }
2281 return _S;
2282}
2283
2284template<class _Eng, size_t _W, class _UI>
2285inline
2286bool
2287operator==(
2288 const independent_bits_engine<_Eng, _W, _UI>& __x,
2289 const independent_bits_engine<_Eng, _W, _UI>& __y)
2290{
2291 return __x.base() == __y.base();
2292}
2293
2294template<class _Eng, size_t _W, class _UI>
2295inline
2296bool
2297operator!=(
2298 const independent_bits_engine<_Eng, _W, _UI>& __x,
2299 const independent_bits_engine<_Eng, _W, _UI>& __y)
2300{
2301 return !(__x == __y);
2302}
2303
2304template <class _CharT, class _Traits,
2305 class _Eng, size_t _W, class _UI>
2306basic_ostream<_CharT, _Traits>&
2307operator<<(basic_ostream<_CharT, _Traits>& __os,
2308 const independent_bits_engine<_Eng, _W, _UI>& __x)
2309{
2310 return __os << __x.base();
2311}
2312
2313template <class _CharT, class _Traits,
2314 class _Eng, size_t _W, class _UI>
2315basic_istream<_CharT, _Traits>&
2316operator>>(basic_istream<_CharT, _Traits>& __is,
2317 independent_bits_engine<_Eng, _W, _UI>& __x)
2318{
2319 _Eng __e;
2320 __is >> __e;
2321 if (!__is.fail())
2322 __x.__e_ = __e;
2323 return __is;
2324}
2325
2326// shuffle_order_engine
2327
2328template <uint64_t _Xp, uint64_t _Yp>
2329struct __ugcd
2330{
2331 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
2332};
2333
2334template <uint64_t _Xp>
2335struct __ugcd<_Xp, 0>
2336{
2337 static const uint64_t value = _Xp;
2338};
2339
2340template <uint64_t _N, uint64_t _D>
2341class __uratio
2342{
2343 static_assert(_D != 0, "__uratio divide by 0");
2344 static const uint64_t __gcd = __ugcd<_N, _D>::value;
2345public:
2346 static const uint64_t num = _N / __gcd;
2347 static const uint64_t den = _D / __gcd;
2348
2349 typedef __uratio<num, den> type;
2350};
2351
2352template<class _Engine, size_t __k>
2353class shuffle_order_engine
2354{
2355 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
2356public:
2357 // types
2358 typedef typename _Engine::result_type result_type;
2359
2360private:
2361 _Engine __e_;
2362 result_type _V_[__k];
2363 result_type _Y_;
2364
2365public:
2366 // engine characteristics
2367 static const/*expr*/ size_t table_size = __k;
2368
2369 static const result_type _Min = _Engine::_Min;
2370 static const result_type _Max = _Engine::_Max;
2371 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
2372 static const/*expr*/ result_type min() { return _Min; }
2373 static const/*expr*/ result_type max() { return _Max; }
2374
2375 static const unsigned long long _R = _Max - _Min + 1ull;
2376
2377 // constructors and seeding functions
2378 shuffle_order_engine() {__init();}
2379// explicit shuffle_order_engine(const _Engine& __e);
2380// explicit shuffle_order_engine(_Engine&& e);
2381 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
2382 template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q)
2383 : __e_(__q) {__init();}
2384 void seed() {__e_.seed(); __init();}
2385 void seed(result_type __sd) {__e_.seed(__sd); __init();}
2386 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();}
2387
2388 // generating functions
2389 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2390 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2391
2392 // property functions
2393 const _Engine& base() const {return __e_;}
2394
2395private:
2396 template<class _Eng, size_t _K>
2397 friend
2398 bool
2399 operator==(
2400 const shuffle_order_engine<_Eng, _K>& __x,
2401 const shuffle_order_engine<_Eng, _K>& __y);
2402
2403 template<class _Eng, size_t _K>
2404 friend
2405 bool
2406 operator!=(
2407 const shuffle_order_engine<_Eng, _K>& __x,
2408 const shuffle_order_engine<_Eng, _K>& __y);
2409
2410 template <class _CharT, class _Traits,
2411 class _Eng, size_t _K>
2412 friend
2413 basic_ostream<_CharT, _Traits>&
2414 operator<<(basic_ostream<_CharT, _Traits>& __os,
2415 const shuffle_order_engine<_Eng, _K>& __x);
2416
2417 template <class _CharT, class _Traits,
2418 class _Eng, size_t _K>
2419 friend
2420 basic_istream<_CharT, _Traits>&
2421 operator>>(basic_istream<_CharT, _Traits>& __is,
2422 shuffle_order_engine<_Eng, _K>& __x);
2423
2424 void __init()
2425 {
2426 for (size_t __i = 0; __i < __k; ++__i)
2427 _V_[__i] = __e_();
2428 _Y_ = __e_();
2429 }
2430
2431 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
2432 result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
2433
2434 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
2435 result_type __eval2(true_type) {return __evalf<__k, 0>();}
2436
2437 template <uint64_t _N, uint64_t _D>
2438 typename enable_if
2439 <
2440 (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
2441 result_type
2442 >::type
2443 __eval(__uratio<_N, _D>)
2444 {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
2445
2446 template <uint64_t _N, uint64_t _D>
2447 typename enable_if
2448 <
2449 __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
2450 result_type
2451 >::type
2452 __eval(__uratio<_N, _D>)
2453 {
2454 const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
2455 / __uratio<_N, _D>::den);
2456 _Y_ = _V_[__j];
2457 _V_[__j] = __e_();
2458 return _Y_;
2459 }
2460
2461 template <uint64_t __n, uint64_t __d>
2462 result_type __evalf()
2463 {
2464 const double _F = __d == 0 ?
2465 __n / (2. * 0x8000000000000000ull) :
2466 __n / (double)__d;
2467 const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
2468 _Y_ = _V_[__j];
2469 _V_[__j] = __e_();
2470 return _Y_;
2471 }
2472};
2473
2474template<class _Eng, size_t _K>
2475bool
2476operator==(
2477 const shuffle_order_engine<_Eng, _K>& __x,
2478 const shuffle_order_engine<_Eng, _K>& __y)
2479{
2480 return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
2481 __x.__e_ == __y.__e_;
2482}
2483
2484template<class _Eng, size_t _K>
2485inline
2486bool
2487operator!=(
2488 const shuffle_order_engine<_Eng, _K>& __x,
2489 const shuffle_order_engine<_Eng, _K>& __y)
2490{
2491 return !(__x == __y);
2492}
2493
2494template <class _CharT, class _Traits,
2495 class _Eng, size_t _K>
2496basic_ostream<_CharT, _Traits>&
2497operator<<(basic_ostream<_CharT, _Traits>& __os,
2498 const shuffle_order_engine<_Eng, _K>& __x)
2499{
2500 __save_flags<_CharT, _Traits> _(__os);
2501 __os.flags(ios_base::dec | ios_base::left);
2502 _CharT __sp = __os.widen(' ');
2503 __os.fill(__sp);
2504 __os << __x.__e_ << __sp << __x._V_[0];
2505 for (size_t __i = 1; __i < _K; ++__i)
2506 __os << __sp << __x._V_[__i];
2507 return __os << __sp << __x._Y_;
2508}
2509
2510template <class _CharT, class _Traits,
2511 class _Eng, size_t _K>
2512basic_istream<_CharT, _Traits>&
2513operator>>(basic_istream<_CharT, _Traits>& __is,
2514 shuffle_order_engine<_Eng, _K>& __x)
2515{
2516 typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
2517 __save_flags<_CharT, _Traits> _(__is);
2518 __is.flags(ios_base::dec | ios_base::skipws);
2519 _Eng __e;
2520 result_type _V[_K+1];
2521 __is >> __e;
2522 for (size_t __i = 0; __i < _K+1; ++__i)
2523 __is >> _V[__i];
2524 if (!__is.fail())
2525 {
2526 __x.__e_ = __e;
2527 for (size_t __i = 0; __i < _K; ++__i)
2528 __x._V_[__i] = _V[__i];
2529 __x._Y_ = _V[_K];
2530 }
2531 return __is;
2532}
2533
2534typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
2535
2536// random_device
2537
2538class random_device
2539{
2540 int __f_;
2541public:
2542 // types
2543 typedef unsigned result_type;
2544
2545 // generator characteristics
2546 static const result_type _Min = 0;
2547 static const result_type _Max = 0xFFFFFFFFu;
2548
2549 static const/*expr*/ result_type min() { return _Min;}
2550 static const/*expr*/ result_type max() { return _Max;}
2551
2552 // constructors
2553 explicit random_device(const string& __token = "/dev/urandom");
2554 ~random_device();
2555
2556 // generating functions
2557 result_type operator()();
2558
2559 // property functions
2560 double entropy() const;
2561
2562private:
2563 // no copy functions
2564 random_device(const random_device&); // = delete;
2565 random_device& operator=(const random_device&); // = delete;
2566};
2567
2568// seed_seq
2569
2570class seed_seq
2571{
2572public:
2573 // types
2574 typedef uint32_t result_type;
2575
2576private:
2577 vector<result_type> __v_;
2578
2579 template<class _InputIterator>
2580 void init(_InputIterator __first, _InputIterator __last);
2581public:
2582 // constructors
2583 seed_seq() {}
2584 template<class _Tp>
2585 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
2586
2587 template<class _InputIterator>
2588 seed_seq(_InputIterator __first, _InputIterator __last)
2589 {init(__first, __last);}
2590
2591 // generating functions
2592 template<class _RandomAccessIterator>
2593 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
2594
2595 // property functions
2596 size_t size() const {return __v_.size();}
2597 template<class _OutputIterator>
2598 void param(_OutputIterator __dest) const
2599 {_STD::copy(__v_.begin(), __v_.end(), __dest);}
2600
2601private:
2602 // no copy functions
2603 seed_seq(const seed_seq&); // = delete;
2604 void operator=(const seed_seq&); // = delete;
2605
2606 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
2607};
2608
2609template<class _InputIterator>
2610void
2611seed_seq::init(_InputIterator __first, _InputIterator __last)
2612{
2613 for (_InputIterator __s = __first; __s != __last; ++__s)
2614 __v_.push_back(*__s & 0xFFFFFFFF);
2615}
2616
2617template<class _RandomAccessIterator>
2618void
2619seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
2620{
2621 if (__first != __last)
2622 {
2623 _STD::fill(__first, __last, 0x8b8b8b8b);
2624 const size_t __n = static_cast<size_t>(__last - __first);
2625 const size_t __s = __v_.size();
2626 const size_t __t = (__n >= 623) ? 11
2627 : (__n >= 68) ? 7
2628 : (__n >= 39) ? 5
2629 : (__n >= 7) ? 3
2630 : (__n - 1) / 2;
2631 const size_t __p = (__n - __t) / 2;
2632 const size_t __q = __p + __t;
2633 const size_t __m = _STD::max(__s + 1, __n);
2634 // __k = 0;
2635 {
2636 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
2637 ^ __first[__n - 1]);
2638 __first[__p] += __r;
2639 __r += __s;
2640 __first[__q] += __r;
2641 __first[0] = __r;
2642 }
2643 for (size_t __k = 1; __k <= __s; ++__k)
2644 {
2645 const size_t __kmodn = __k % __n;
2646 const size_t __kpmodn = (__k + __p) % __n;
2647 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2648 ^ __first[(__k - 1) % __n]);
2649 __first[__kpmodn] += __r;
2650 __r += __kmodn + __v_[__k-1];
2651 __first[(__k + __q) % __n] += __r;
2652 __first[__kmodn] = __r;
2653 }
2654 for (size_t __k = __s + 1; __k < __m; ++__k)
2655 {
2656 const size_t __kmodn = __k % __n;
2657 const size_t __kpmodn = (__k + __p) % __n;
2658 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2659 ^ __first[(__k - 1) % __n]);
2660 __first[__kpmodn] += __r;
2661 __r += __kmodn;
2662 __first[(__k + __q) % __n] += __r;
2663 __first[__kmodn] = __r;
2664 }
2665 for (size_t __k = __m; __k < __m + __n; ++__k)
2666 {
2667 const size_t __kmodn = __k % __n;
2668 const size_t __kpmodn = (__k + __p) % __n;
2669 result_type __r = 1566083941 * _T(__first[__kmodn] +
2670 __first[__kpmodn] +
2671 __first[(__k - 1) % __n]);
2672 __first[__kpmodn] ^= __r;
2673 __r -= __kmodn;
2674 __first[(__k + __q) % __n] ^= __r;
2675 __first[__kmodn] = __r;
2676 }
2677 }
2678}
2679
Howard Hinnant30a840f2010-05-12 17:08:57 +00002680// generate_canonical
2681
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002682template<class _RealType, size_t __bits, class _URNG>
2683_RealType
2684generate_canonical(_URNG& __g)
2685{
2686 const size_t _Dt = numeric_limits<_RealType>::digits;
2687 const size_t __b = _Dt < __bits ? _Dt : __bits;
2688 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
2689 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
2690 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
2691 _RealType __base = _R;
2692 _RealType _S = __g() - _URNG::_Min;
2693 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
2694 _S += (__g() - _URNG::_Min) * __base;
2695 return _S / __base;
2696}
2697
2698// __independent_bits_engine
2699
2700template<class _Engine, class _UIntType>
2701class __independent_bits_engine
2702{
2703public:
2704 // types
2705 typedef _UIntType result_type;
2706
2707private:
2708 typedef typename _Engine::result_type _Engine_result_type;
2709 typedef typename conditional
2710 <
2711 sizeof(_Engine_result_type) <= sizeof(result_type),
2712 result_type,
2713 _Engine_result_type
2714 >::type _Working_result_type;
2715
2716 _Engine& __e_;
2717 size_t __w_;
2718 size_t __w0_;
2719 size_t __n_;
2720 size_t __n0_;
2721 _Working_result_type __y0_;
2722 _Working_result_type __y1_;
2723 _Engine_result_type __mask0_;
2724 _Engine_result_type __mask1_;
2725
2726 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2727 + _Working_result_type(1);
2728 static const size_t __m = __log2<_Working_result_type, _R>::value;
2729 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2730 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2731
2732public:
2733 // constructors and seeding functions
2734 __independent_bits_engine(_Engine& __e, size_t __w);
2735
2736 // generating functions
2737 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2738
2739private:
2740 result_type __eval(false_type);
2741 result_type __eval(true_type);
2742};
2743
2744template<class _Engine, class _UIntType>
2745__independent_bits_engine<_Engine, _UIntType>
2746 ::__independent_bits_engine(_Engine& __e, size_t __w)
2747 : __e_(__e),
2748 __w_(__w)
2749{
2750 __n_ = __w_ / __m + (__w_ % __m != 0);
2751 __w0_ = __w_ / __n_;
2752 if (_R == 0)
2753 __y0_ = _R;
2754 else if (__w0_ < _WDt)
2755 __y0_ = (_R >> __w0_) << __w0_;
2756 else
2757 __y0_ = 0;
2758 if (_R - __y0_ > __y0_ / __n_)
2759 {
2760 ++__n_;
2761 __w0_ = __w_ / __n_;
2762 if (__w0_ < _WDt)
2763 __y0_ = (_R >> __w0_) << __w0_;
2764 else
2765 __y0_ = 0;
2766 }
2767 __n0_ = __n_ - __w_ % __n_;
2768 if (__w0_ < _WDt - 1)
2769 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
2770 else
2771 __y1_ = 0;
2772 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2773 _Engine_result_type(0);
2774 __mask1_ = __w0_ < _EDt - 1 ?
2775 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2776 _Engine_result_type(~0);
2777}
2778
2779template<class _Engine, class _UIntType>
2780inline
2781_UIntType
2782__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2783{
2784 return static_cast<result_type>(__e_() & __mask0_);
2785}
2786
2787template<class _Engine, class _UIntType>
2788_UIntType
2789__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2790{
2791 result_type _S = 0;
2792 for (size_t __k = 0; __k < __n0_; ++__k)
2793 {
2794 _Engine_result_type __u;
2795 do
2796 {
2797 __u = __e_() - _Engine::min();
2798 } while (__u >= __y0_);
2799 if (__w0_ < _EDt)
2800 _S <<= __w0_;
2801 else
2802 _S = 0;
2803 _S += __u & __mask0_;
2804 }
2805 for (size_t __k = __n0_; __k < __n_; ++__k)
2806 {
2807 _Engine_result_type __u;
2808 do
2809 {
2810 __u = __e_() - _Engine::min();
2811 } while (__u >= __y1_);
2812 if (__w0_ < _EDt - 1)
2813 _S <<= __w0_ + 1;
2814 else
2815 _S = 0;
2816 _S += __u & __mask1_;
2817 }
2818 return _S;
2819}
2820
Howard Hinnant30a840f2010-05-12 17:08:57 +00002821// uniform_int_distribution
2822
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823template<class _IntType = int>
2824class uniform_int_distribution
2825{
2826public:
2827 // types
2828 typedef _IntType result_type;
2829
2830 class param_type
2831 {
2832 result_type __a_;
2833 result_type __b_;
2834 public:
2835 typedef uniform_int_distribution distribution_type;
2836
2837 explicit param_type(result_type __a = 0,
2838 result_type __b = numeric_limits<result_type>::max())
2839 : __a_(__a), __b_(__b) {}
2840
2841 result_type a() const {return __a_;}
2842 result_type b() const {return __b_;}
2843
2844 friend bool operator==(const param_type& __x, const param_type& __y)
2845 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2846 friend bool operator!=(const param_type& __x, const param_type& __y)
2847 {return !(__x == __y);}
2848 };
2849
2850private:
2851 param_type __p_;
2852
2853public:
2854 // constructors and reset functions
2855 explicit uniform_int_distribution(result_type __a = 0,
2856 result_type __b = numeric_limits<result_type>::max())
2857 : __p_(param_type(__a, __b)) {}
2858 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2859 void reset() {}
2860
2861 // generating functions
2862 template<class _URNG> result_type operator()(_URNG& __g)
2863 {return (*this)(__g, __p_);}
2864 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2865
2866 // property functions
2867 result_type a() const {return __p_.a();}
2868 result_type b() const {return __p_.b();}
2869
2870 param_type param() const {return __p_;}
2871 void param(const param_type& __p) {__p_ = __p;}
2872
2873 result_type min() const {return a();}
2874 result_type max() const {return b();}
2875
2876 friend bool operator==(const uniform_int_distribution& __x,
2877 const uniform_int_distribution& __y)
2878 {return __x.__p_ == __y.__p_;}
2879 friend bool operator!=(const uniform_int_distribution& __x,
2880 const uniform_int_distribution& __y)
2881 {return !(__x == __y);}
2882};
2883
2884template<class _IntType>
2885template<class _URNG>
2886typename uniform_int_distribution<_IntType>::result_type
2887uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2888{
2889 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2890 uint32_t, uint64_t>::type _UIntType;
2891 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
2892 if (_R == 1)
2893 return __p.a();
2894 const size_t _Dt = numeric_limits<_UIntType>::digits;
2895 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
2896 if (_R == 0)
2897 return static_cast<result_type>(_Eng(__g, _Dt)());
2898 size_t __w = _Dt - __clz(_R) - 1;
2899 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
2900 ++__w;
2901 _Eng __e(__g, __w);
2902 _UIntType __u;
2903 do
2904 {
2905 __u = __e();
2906 } while (__u >= _R);
2907 return static_cast<result_type>(__u + __p.a());
2908}
2909
2910template <class _CharT, class _Traits, class _IT>
2911basic_ostream<_CharT, _Traits>&
2912operator<<(basic_ostream<_CharT, _Traits>& __os,
2913 const uniform_int_distribution<_IT>& __x)
2914{
2915 __save_flags<_CharT, _Traits> _(__os);
2916 __os.flags(ios_base::dec | ios_base::left);
2917 _CharT __sp = __os.widen(' ');
2918 __os.fill(__sp);
2919 return __os << __x.a() << __sp << __x.b();
2920}
2921
2922template <class _CharT, class _Traits, class _IT>
2923basic_istream<_CharT, _Traits>&
2924operator>>(basic_istream<_CharT, _Traits>& __is,
2925 uniform_int_distribution<_IT>& __x)
2926{
2927 typedef uniform_int_distribution<_IT> _Eng;
2928 typedef typename _Eng::result_type result_type;
2929 typedef typename _Eng::param_type param_type;
2930 __save_flags<_CharT, _Traits> _(__is);
2931 __is.flags(ios_base::dec | ios_base::skipws);
2932 result_type __a;
2933 result_type __b;
2934 __is >> __a >> __b;
2935 if (!__is.fail())
2936 __x.param(param_type(__a, __b));
2937 return __is;
2938}
2939
Howard Hinnant30a840f2010-05-12 17:08:57 +00002940// uniform_real_distribution
2941
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002942template<class _RealType = double>
2943class uniform_real_distribution
2944{
2945public:
2946 // types
2947 typedef _RealType result_type;
2948
2949 class param_type
2950 {
2951 result_type __a_;
2952 result_type __b_;
2953 public:
2954 typedef uniform_real_distribution distribution_type;
2955
2956 explicit param_type(result_type __a = 0,
2957 result_type __b = 1)
2958 : __a_(__a), __b_(__b) {}
2959
2960 result_type a() const {return __a_;}
2961 result_type b() const {return __b_;}
2962
2963 friend bool operator==(const param_type& __x, const param_type& __y)
2964 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2965 friend bool operator!=(const param_type& __x, const param_type& __y)
2966 {return !(__x == __y);}
2967 };
2968
2969private:
2970 param_type __p_;
2971
2972public:
2973 // constructors and reset functions
2974 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
2975 : __p_(param_type(__a, __b)) {}
2976 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
2977 void reset() {}
2978
2979 // generating functions
2980 template<class _URNG> result_type operator()(_URNG& __g)
2981 {return (*this)(__g, __p_);}
2982 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2983
2984 // property functions
2985 result_type a() const {return __p_.a();}
2986 result_type b() const {return __p_.b();}
2987
2988 param_type param() const {return __p_;}
2989 void param(const param_type& __p) {__p_ = __p;}
2990
2991 result_type min() const {return a();}
2992 result_type max() const {return b();}
2993
2994 friend bool operator==(const uniform_real_distribution& __x,
2995 const uniform_real_distribution& __y)
2996 {return __x.__p_ == __y.__p_;}
2997 friend bool operator!=(const uniform_real_distribution& __x,
2998 const uniform_real_distribution& __y)
2999 {return !(__x == __y);}
3000};
3001
3002template<class _RealType>
3003template<class _URNG>
3004inline
3005typename uniform_real_distribution<_RealType>::result_type
3006uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3007{
3008 return (__p.b() - __p.a())
3009 * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
3010 + __p.a();
3011}
3012
3013template <class _CharT, class _Traits, class _RT>
3014basic_ostream<_CharT, _Traits>&
3015operator<<(basic_ostream<_CharT, _Traits>& __os,
3016 const uniform_real_distribution<_RT>& __x)
3017{
3018 __save_flags<_CharT, _Traits> _(__os);
3019 __os.flags(ios_base::dec | ios_base::left);
3020 _CharT __sp = __os.widen(' ');
3021 __os.fill(__sp);
3022 return __os << __x.a() << __sp << __x.b();
3023}
3024
3025template <class _CharT, class _Traits, class _RT>
3026basic_istream<_CharT, _Traits>&
3027operator>>(basic_istream<_CharT, _Traits>& __is,
3028 uniform_real_distribution<_RT>& __x)
3029{
3030 typedef uniform_real_distribution<_RT> _Eng;
3031 typedef typename _Eng::result_type result_type;
3032 typedef typename _Eng::param_type param_type;
3033 __save_flags<_CharT, _Traits> _(__is);
3034 __is.flags(ios_base::dec | ios_base::skipws);
3035 result_type __a;
3036 result_type __b;
3037 __is >> __a >> __b;
3038 if (!__is.fail())
3039 __x.param(param_type(__a, __b));
3040 return __is;
3041}
3042
Howard Hinnant30a840f2010-05-12 17:08:57 +00003043// bernoulli_distribution
3044
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003045class bernoulli_distribution
3046{
3047public:
3048 // types
3049 typedef bool result_type;
3050
3051 class param_type
3052 {
3053 double __p_;
3054 public:
3055 typedef bernoulli_distribution distribution_type;
3056
3057 explicit param_type(double __p = 0.5) : __p_(__p) {}
3058
3059 double p() const {return __p_;}
3060
3061 friend bool operator==(const param_type& __x, const param_type& __y)
3062 {return __x.__p_ == __y.__p_;}
3063 friend bool operator!=(const param_type& __x, const param_type& __y)
3064 {return !(__x == __y);}
3065 };
3066
3067private:
3068 param_type __p_;
3069
3070public:
3071 // constructors and reset functions
3072 explicit bernoulli_distribution(double __p = 0.5)
3073 : __p_(param_type(__p)) {}
3074 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
3075 void reset() {}
3076
3077 // generating functions
3078 template<class _URNG> result_type operator()(_URNG& __g)
3079 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003080 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003081
3082 // property functions
3083 double p() const {return __p_.p();}
3084
3085 param_type param() const {return __p_;}
3086 void param(const param_type& __p) {__p_ = __p;}
3087
3088 result_type min() const {return false;}
3089 result_type max() const {return true;}
3090
3091 friend bool operator==(const bernoulli_distribution& __x,
3092 const bernoulli_distribution& __y)
3093 {return __x.__p_ == __y.__p_;}
3094 friend bool operator!=(const bernoulli_distribution& __x,
3095 const bernoulli_distribution& __y)
3096 {return !(__x == __y);}
3097};
3098
Howard Hinnant03aad812010-05-11 23:26:59 +00003099template<class _URNG>
3100inline
3101bernoulli_distribution::result_type
3102bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3103{
3104 return (__g() - __g.min()) < __p.p() * (__g.max() - __g.min() + 1.);
3105}
3106
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003107template <class _CharT, class _Traits>
3108basic_ostream<_CharT, _Traits>&
3109operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3110{
3111 __save_flags<_CharT, _Traits> _(__os);
3112 __os.flags(ios_base::dec | ios_base::left);
3113 _CharT __sp = __os.widen(' ');
3114 __os.fill(__sp);
3115 return __os << __x.p();
3116}
3117
3118template <class _CharT, class _Traits>
3119basic_istream<_CharT, _Traits>&
3120operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3121{
3122 typedef bernoulli_distribution _Eng;
3123 typedef typename _Eng::param_type param_type;
3124 __save_flags<_CharT, _Traits> _(__is);
3125 __is.flags(ios_base::dec | ios_base::skipws);
3126 double __p;
3127 __is >> __p;
3128 if (!__is.fail())
3129 __x.param(param_type(__p));
3130 return __is;
3131}
3132
Howard Hinnant30a840f2010-05-12 17:08:57 +00003133// binomial_distribution
3134
Howard Hinnant03aad812010-05-11 23:26:59 +00003135template<class _IntType = int>
3136class binomial_distribution
3137{
3138public:
3139 // types
3140 typedef _IntType result_type;
3141
3142 class param_type
3143 {
3144 result_type __t_;
3145 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003146 double __pr_;
3147 double __odds_ratio_;
3148 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003149 public:
3150 typedef binomial_distribution distribution_type;
3151
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003152 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003153
3154 result_type t() const {return __t_;}
3155 double p() const {return __p_;}
3156
3157 friend bool operator==(const param_type& __x, const param_type& __y)
3158 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
3159 friend bool operator!=(const param_type& __x, const param_type& __y)
3160 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003161
3162 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003163 };
3164
3165private:
3166 param_type __p_;
3167
3168public:
3169 // constructors and reset functions
3170 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3171 : __p_(param_type(__t, __p)) {}
3172 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
3173 void reset() {}
3174
3175 // generating functions
3176 template<class _URNG> result_type operator()(_URNG& __g)
3177 {return (*this)(__g, __p_);}
3178 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3179
3180 // property functions
3181 result_type t() const {return __p_.t();}
3182 double p() const {return __p_.p();}
3183
3184 param_type param() const {return __p_;}
3185 void param(const param_type& __p) {__p_ = __p;}
3186
3187 result_type min() const {return 0;}
3188 result_type max() const {return t();}
3189
3190 friend bool operator==(const binomial_distribution& __x,
3191 const binomial_distribution& __y)
3192 {return __x.__p_ == __y.__p_;}
3193 friend bool operator!=(const binomial_distribution& __x,
3194 const binomial_distribution& __y)
3195 {return !(__x == __y);}
3196};
3197
3198template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003199binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3200 : __t_(__t), __p_(__p)
3201{
3202 if (0 < __p_ && __p_ < 1)
3203 {
3204 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
3205 __pr_ = _STD::exp(_STD::lgamma(__t_ + 1.) - _STD::lgamma(__r0_ + 1.) -
3206 _STD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _STD::log(__p_) +
3207 (__t_ - __r0_) * _STD::log(1 - __p_));
3208 __odds_ratio_ = __p_ / (1 - __p_);
3209 }
3210}
3211
3212template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00003213template<class _URNG>
3214_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003215binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00003216{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003217 if (__pr.__t_ == 0 || __pr.__p_ == 0)
3218 return 0;
3219 if (__pr.__p_ == 1)
3220 return __pr.__t_;
3221 uniform_real_distribution<double> __gen;
3222 double __u = __gen(__g) - __pr.__pr_;
3223 if (__u < 0)
3224 return __pr.__r0_;
3225 double __pu = __pr.__pr_;
3226 double __pd = __pu;
3227 result_type __ru = __pr.__r0_;
3228 result_type __rd = __ru;
3229 while (true)
3230 {
3231 if (__rd >= 1)
3232 {
3233 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3234 __u -= __pd;
3235 if (__u < 0)
3236 return __rd - 1;
3237 }
3238 --__rd;
3239 ++__ru;
3240 if (__ru <= __pr.__t_)
3241 {
3242 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3243 __u -= __pu;
3244 if (__u < 0)
3245 return __ru;
3246 }
3247 }
Howard Hinnant03aad812010-05-11 23:26:59 +00003248}
3249
3250template <class _CharT, class _Traits, class _IntType>
3251basic_ostream<_CharT, _Traits>&
3252operator<<(basic_ostream<_CharT, _Traits>& __os,
3253 const binomial_distribution<_IntType>& __x)
3254{
3255 __save_flags<_CharT, _Traits> _(__os);
3256 __os.flags(ios_base::dec | ios_base::left);
3257 _CharT __sp = __os.widen(' ');
3258 __os.fill(__sp);
3259 return __os << __x.t() << __sp << __x.p();
3260}
3261
3262template <class _CharT, class _Traits, class _IntType>
3263basic_istream<_CharT, _Traits>&
3264operator>>(basic_istream<_CharT, _Traits>& __is,
3265 binomial_distribution<_IntType>& __x)
3266{
3267 typedef binomial_distribution<_IntType> _Eng;
3268 typedef typename _Eng::result_type result_type;
3269 typedef typename _Eng::param_type param_type;
3270 __save_flags<_CharT, _Traits> _(__is);
3271 __is.flags(ios_base::dec | ios_base::skipws);
3272 result_type __t;
3273 double __p;
3274 __is >> __t >> __p;
3275 if (!__is.fail())
3276 __x.param(param_type(__t, __p));
3277 return __is;
3278}
3279
Howard Hinnant30a840f2010-05-12 17:08:57 +00003280// exponential_distribution
3281
3282template<class _RealType = double>
3283class exponential_distribution
3284{
3285public:
3286 // types
3287 typedef _RealType result_type;
3288
3289 class param_type
3290 {
3291 result_type __lambda_;
3292 public:
3293 typedef exponential_distribution distribution_type;
3294
3295 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3296
3297 result_type lambda() const {return __lambda_;}
3298
3299 friend bool operator==(const param_type& __x, const param_type& __y)
3300 {return __x.__lambda_ == __y.__lambda_;}
3301 friend bool operator!=(const param_type& __x, const param_type& __y)
3302 {return !(__x == __y);}
3303 };
3304
3305private:
3306 param_type __p_;
3307
3308public:
3309 // constructors and reset functions
3310 explicit exponential_distribution(result_type __lambda = 1)
3311 : __p_(param_type(__lambda)) {}
3312 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
3313 void reset() {}
3314
3315 // generating functions
3316 template<class _URNG> result_type operator()(_URNG& __g)
3317 {return (*this)(__g, __p_);}
3318 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3319
3320 // property functions
3321 result_type lambda() const {return __p_.lambda();}
3322
3323 param_type param() const {return __p_;}
3324 void param(const param_type& __p) {__p_ = __p;}
3325
3326 result_type min() const {return 0;}
3327 result_type max() const
3328 {return -std::log(1-std::nextafter(result_type(1), result_type(-1))) /
3329 __p_.lambda();}
3330
3331 friend bool operator==(const exponential_distribution& __x,
3332 const exponential_distribution& __y)
3333 {return __x.__p_ == __y.__p_;}
3334 friend bool operator!=(const exponential_distribution& __x,
3335 const exponential_distribution& __y)
3336 {return !(__x == __y);}
3337};
3338
3339template <class _RealType>
3340template<class _URNG>
3341_RealType
3342exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3343{
3344 return -_STD::log
3345 (
3346 result_type(1) -
3347 _STD::generate_canonical<result_type,
3348 numeric_limits<result_type>::digits>(__g)
3349 )
3350 / __p.lambda();
3351}
3352
3353template <class _CharT, class _Traits, class _RealType>
3354basic_ostream<_CharT, _Traits>&
3355operator<<(basic_ostream<_CharT, _Traits>& __os,
3356 const exponential_distribution<_RealType>& __x)
3357{
3358 __save_flags<_CharT, _Traits> _(__os);
3359 __os.flags(ios_base::dec | ios_base::left);
3360 return __os << __x.lambda();
3361}
3362
3363template <class _CharT, class _Traits, class _RealType>
3364basic_istream<_CharT, _Traits>&
3365operator>>(basic_istream<_CharT, _Traits>& __is,
3366 exponential_distribution<_RealType>& __x)
3367{
3368 typedef exponential_distribution<_RealType> _Eng;
3369 typedef typename _Eng::result_type result_type;
3370 typedef typename _Eng::param_type param_type;
3371 __save_flags<_CharT, _Traits> _(__is);
3372 __is.flags(ios_base::dec | ios_base::skipws);
3373 result_type __lambda;
3374 __is >> __lambda;
3375 if (!__is.fail())
3376 __x.param(param_type(__lambda));
3377 return __is;
3378}
3379
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003380// normal_distribution
3381
3382template<class _RealType = double>
3383class normal_distribution
3384{
3385public:
3386 // types
3387 typedef _RealType result_type;
3388
3389 class param_type
3390 {
3391 result_type __mean_;
3392 result_type __stddev_;
3393 public:
3394 typedef normal_distribution distribution_type;
3395
3396 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
3397 : __mean_(__mean), __stddev_(__stddev) {}
3398
3399 result_type mean() const {return __mean_;}
3400 result_type stddev() const {return __stddev_;}
3401
3402 friend bool operator==(const param_type& __x, const param_type& __y)
3403 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
3404 friend bool operator!=(const param_type& __x, const param_type& __y)
3405 {return !(__x == __y);}
3406 };
3407
3408private:
3409 param_type __p_;
3410 result_type _V_;
3411 bool _V_hot_;
3412
3413public:
3414 // constructors and reset functions
3415 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
3416 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
3417 explicit normal_distribution(const param_type& __p)
3418 : __p_(__p), _V_hot_(false) {}
3419 void reset() {_V_hot_ = false;}
3420
3421 // generating functions
3422 template<class _URNG> result_type operator()(_URNG& __g)
3423 {return (*this)(__g, __p_);}
3424 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3425
3426 // property functions
3427 result_type mean() const {return __p_.mean();}
3428 result_type stddev() const {return __p_.stddev();}
3429
3430 param_type param() const {return __p_;}
3431 void param(const param_type& __p) {__p_ = __p;}
3432
3433 result_type min() const {return -numeric_limits<result_type>::infinity();}
3434 result_type max() const {return numeric_limits<result_type>::infinity();}
3435
3436 friend bool operator==(const normal_distribution& __x,
3437 const normal_distribution& __y)
3438 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
3439 (!__x._V_hot_ || __x._V_ == __y._V_);}
3440 friend bool operator!=(const normal_distribution& __x,
3441 const normal_distribution& __y)
3442 {return !(__x == __y);}
3443
3444 template <class _CharT, class _Traits, class _RT>
3445 friend
3446 basic_ostream<_CharT, _Traits>&
3447 operator<<(basic_ostream<_CharT, _Traits>& __os,
3448 const normal_distribution<_RT>& __x);
3449
3450 template <class _CharT, class _Traits, class _RT>
3451 friend
3452 basic_istream<_CharT, _Traits>&
3453 operator>>(basic_istream<_CharT, _Traits>& __is,
3454 normal_distribution<_RT>& __x);
3455};
3456
3457template <class _RealType>
3458template<class _URNG>
3459_RealType
3460normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3461{
3462 result_type _U;
3463 if (_V_hot_)
3464 {
3465 _V_hot_ = false;
3466 _U = _V_;
3467 }
3468 else
3469 {
3470 uniform_real_distribution<result_type> _Uni(-1, 1);
3471 result_type __u;
3472 result_type __v;
3473 result_type __s;
3474 do
3475 {
3476 __u = _Uni(__g);
3477 __v = _Uni(__g);
3478 __s = __u * __u + __v * __v;
3479 } while (__s > 1 || __s == 0);
3480 result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s);
3481 _V_ = __v * _F;
3482 _V_hot_ = true;
3483 _U = __u * _F;
3484 }
3485 return _U * __p.stddev() + __p.mean();
3486}
3487
3488template <class _CharT, class _Traits, class _RT>
3489basic_ostream<_CharT, _Traits>&
3490operator<<(basic_ostream<_CharT, _Traits>& __os,
3491 const normal_distribution<_RT>& __x)
3492{
3493 __save_flags<_CharT, _Traits> _(__os);
3494 __os.flags(ios_base::dec | ios_base::left);
3495 _CharT __sp = __os.widen(' ');
3496 __os.fill(__sp);
3497 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
3498 if (__x._V_hot_)
3499 __os << __sp << __x._V_;
3500 return __os;
3501}
3502
3503template <class _CharT, class _Traits, class _RT>
3504basic_istream<_CharT, _Traits>&
3505operator>>(basic_istream<_CharT, _Traits>& __is,
3506 normal_distribution<_RT>& __x)
3507{
3508 typedef normal_distribution<_RT> _Eng;
3509 typedef typename _Eng::result_type result_type;
3510 typedef typename _Eng::param_type param_type;
3511 __save_flags<_CharT, _Traits> _(__is);
3512 __is.flags(ios_base::dec | ios_base::skipws);
3513 result_type __mean;
3514 result_type __stddev;
3515 result_type _V = 0;
3516 bool _V_hot = false;
3517 __is >> __mean >> __stddev >> _V_hot;
3518 if (_V_hot)
3519 __is >> _V;
3520 if (!__is.fail())
3521 {
3522 __x.param(param_type(__mean, __stddev));
3523 __x._V_hot_ = _V_hot;
3524 __x._V_ = _V;
3525 }
3526 return __is;
3527}
3528
3529// poisson_distribution
3530
3531template<class _IntType = int>
3532class poisson_distribution
3533{
3534public:
3535 // types
3536 typedef _IntType result_type;
3537
3538 class param_type
3539 {
3540 double __mean_;
3541 double __s_;
3542 double __d_;
3543 double __l_;
3544 double __omega_;
3545 double __c0_;
3546 double __c1_;
3547 double __c2_;
3548 double __c3_;
3549 double __c_;
3550
3551 public:
3552 typedef poisson_distribution distribution_type;
3553
3554 explicit param_type(double __mean = 1.0);
3555
3556 double mean() const {return __mean_;}
3557
3558 friend bool operator==(const param_type& __x, const param_type& __y)
3559 {return __x.__mean_ == __y.__mean_;}
3560 friend bool operator!=(const param_type& __x, const param_type& __y)
3561 {return !(__x == __y);}
3562
3563 friend class poisson_distribution;
3564 };
3565
3566private:
3567 param_type __p_;
3568
3569public:
3570 // constructors and reset functions
3571 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
3572 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
3573 void reset() {}
3574
3575 // generating functions
3576 template<class _URNG> result_type operator()(_URNG& __g)
3577 {return (*this)(__g, __p_);}
3578 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3579
3580 // property functions
3581 double mean() const {return __p_.mean();}
3582
3583 param_type param() const {return __p_;}
3584 void param(const param_type& __p) {__p_ = __p;}
3585
3586 result_type min() const {return 0;}
3587 result_type max() const {return numeric_limits<result_type>::max();}
3588
3589 friend bool operator==(const poisson_distribution& __x,
3590 const poisson_distribution& __y)
3591 {return __x.__p_ == __y.__p_;}
3592 friend bool operator!=(const poisson_distribution& __x,
3593 const poisson_distribution& __y)
3594 {return !(__x == __y);}
3595};
3596
3597template<class _IntType>
3598poisson_distribution<_IntType>::param_type::param_type(double __mean)
3599 : __mean_(__mean)
3600{
3601 if (__mean_ < 10)
3602 {
3603 __s_ = 0;
3604 __d_ = 0;
3605 __l_ = _STD::exp(-__mean_);
3606 __omega_ = 0;
3607 __c3_ = 0;
3608 __c2_ = 0;
3609 __c1_ = 0;
3610 __c0_ = 0;
3611 __c_ = 0;
3612 }
3613 else
3614 {
3615 __s_ = _STD::sqrt(__mean_);
3616 __d_ = 6 * __mean_ * __mean_;
3617 __l_ = static_cast<result_type>(__mean_ - 1.1484);
3618 __omega_ = .3989423 / __s_;
3619 double __b1_ = .4166667E-1 / __mean_;
3620 double __b2_ = .3 * __b1_ * __b1_;
3621 __c3_ = .1428571 * __b1_ * __b2_;
3622 __c2_ = __b2_ - 15. * __c3_;
3623 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
3624 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
3625 __c_ = .1069 / __mean_;
3626 }
3627}
3628
3629template <class _IntType>
3630template<class _URNG>
3631_IntType
3632poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
3633{
3634 result_type __x;
3635 uniform_real_distribution<double> __urd;
3636 if (__pr.__mean_ <= 10)
3637 {
3638 __x = 0;
3639 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
3640 __p *= __urd(__urng);
3641 }
3642 else
3643 {
3644 double __difmuk;
3645 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
3646 double __u;
3647 if (__g > 0)
3648 {
3649 __x = static_cast<result_type>(__g);
3650 if (__x >= __pr.__l_)
3651 return __x;
3652 __difmuk = __pr.__mean_ - __x;
3653 __u = __urd(__urng);
3654 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
3655 return __x;
3656 }
3657 exponential_distribution<double> __edist;
3658 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
3659 {
3660 double __e;
3661 if (__using_exp_dist || __g < 0)
3662 {
3663 double __t;
3664 do
3665 {
3666 __e = __edist(__urng);
3667 __u = __urd(__urng);
3668 __u += __u - 1;
3669 __t = 1.8 + (__u < 0 ? -__e : __e);
3670 } while (__t <= -.6744);
3671 __x = __pr.__mean_ + __pr.__s_ * __t;
3672 __difmuk = __pr.__mean_ - __x;
3673 __using_exp_dist = true;
3674 }
3675 double __px;
3676 double __py;
3677 if (__x < 10)
3678 {
3679 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
3680 40320, 362880};
3681 __px = -__pr.__mean_;
3682 __py = _STD::pow(__pr.__mean_, (double)__x) / __fac[__x];
3683 }
3684 else
3685 {
3686 double __del = .8333333E-1 / __x;
3687 __del -= 4.8 * __del * __del * __del;
3688 double __v = __difmuk / __x;
3689 if (_STD::abs(__v) > 0.25)
3690 __px = __x * _STD::log(1 + __v) - __difmuk - __del;
3691 else
3692 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
3693 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
3694 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
3695 __py = .3989423 / _STD::sqrt(__x);
3696 }
3697 double __r = (0.5 - __difmuk) / __pr.__s_;
3698 double __r2 = __r * __r;
3699 double __fx = -0.5 * __r2;
3700 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
3701 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
3702 if (__using_exp_dist)
3703 {
3704 if (__pr.__c_ * _STD::abs(__u) <= __py * _STD::exp(__px + __e) -
3705 __fy * _STD::exp(__fx + __e))
3706 break;
3707 }
3708 else
3709 {
3710 if (__fy - __u * __fy <= __py * _STD::exp(__px - __fx))
3711 break;
3712 }
3713 }
3714 }
3715 return __x;
3716}
3717
3718template <class _CharT, class _Traits, class _IntType>
3719basic_ostream<_CharT, _Traits>&
3720operator<<(basic_ostream<_CharT, _Traits>& __os,
3721 const poisson_distribution<_IntType>& __x)
3722{
3723 __save_flags<_CharT, _Traits> _(__os);
3724 __os.flags(ios_base::dec | ios_base::left);
3725 return __os << __x.mean();
3726}
3727
3728template <class _CharT, class _Traits, class _IntType>
3729basic_istream<_CharT, _Traits>&
3730operator>>(basic_istream<_CharT, _Traits>& __is,
3731 poisson_distribution<_IntType>& __x)
3732{
3733 typedef poisson_distribution<_IntType> _Eng;
3734 typedef typename _Eng::param_type param_type;
3735 __save_flags<_CharT, _Traits> _(__is);
3736 __is.flags(ios_base::dec | ios_base::skipws);
3737 double __mean;
3738 __is >> __mean;
3739 if (!__is.fail())
3740 __x.param(param_type(__mean));
3741 return __is;
3742}
3743
Howard Hinnantc7c49132010-05-13 17:58:28 +00003744// gamma_distribution
3745
3746template<class _RealType = double>
3747class gamma_distribution
3748{
3749public:
3750 // types
3751 typedef _RealType result_type;
3752
3753 class param_type
3754 {
3755 result_type __alpha_;
3756 result_type __beta_;
3757 public:
3758 typedef gamma_distribution distribution_type;
3759
3760 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
3761 : __alpha_(__alpha), __beta_(__beta) {}
3762
3763 result_type alpha() const {return __alpha_;}
3764 result_type beta() const {return __beta_;}
3765
3766 friend bool operator==(const param_type& __x, const param_type& __y)
3767 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
3768 friend bool operator!=(const param_type& __x, const param_type& __y)
3769 {return !(__x == __y);}
3770 };
3771
3772private:
3773 param_type __p_;
3774
3775public:
3776 // constructors and reset functions
3777 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
3778 : __p_(param_type(__alpha, __beta)) {}
3779 explicit gamma_distribution(const param_type& __p)
3780 : __p_(__p) {}
3781 void reset() {}
3782
3783 // generating functions
3784 template<class _URNG> result_type operator()(_URNG& __g)
3785 {return (*this)(__g, __p_);}
3786 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3787
3788 // property functions
3789 result_type alpha() const {return __p_.alpha();}
3790 result_type beta() const {return __p_.beta();}
3791
3792 param_type param() const {return __p_;}
3793 void param(const param_type& __p) {__p_ = __p;}
3794
3795 result_type min() const {return 0;}
3796 result_type max() const {return numeric_limits<result_type>::infinity();}
3797
3798 friend bool operator==(const gamma_distribution& __x,
3799 const gamma_distribution& __y)
3800 {return __x.__p_ == __y.__p_;}
3801 friend bool operator!=(const gamma_distribution& __x,
3802 const gamma_distribution& __y)
3803 {return !(__x == __y);}
3804};
3805
3806template <class _RealType>
3807template<class _URNG>
3808_RealType
3809gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3810{
Howard Hinnantf417abe2010-05-14 18:43:10 +00003811 result_type __a = __p.alpha();
3812 uniform_real_distribution<result_type> __gen(0, 1);
3813 exponential_distribution<result_type> __egen;
3814 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00003815 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00003816 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00003817 else if (__a > 1)
3818 {
3819 const result_type __b = __a - 1;
3820 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00003821 while (true)
3822 {
3823 const result_type __u = __gen(__g);
3824 const result_type __v = __gen(__g);
3825 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00003826 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00003827 {
3828 const result_type __y = _STD::sqrt(__c / __w) *
3829 (__u - result_type(0.5));
3830 __x = __b + __y;
3831 if (__x >= 0)
3832 {
3833 const result_type __z = 64 * __w * __w * __w * __v * __v;
3834 if (__z <= 1 - 2 * __y * __y / __x)
3835 break;
3836 if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y))
3837 break;
3838 }
3839 }
3840 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00003841 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00003842 else // __a < 1
3843 {
3844 while (true)
3845 {
3846 const result_type __u = __gen(__g);
3847 const result_type __es = __egen(__g);
3848 if (__u <= 1 - __a)
3849 {
3850 __x = _STD::pow(__u, 1 / __a);
3851 if (__x <= __es)
3852 break;
3853 }
3854 else
3855 {
3856 const result_type __e = -_STD::log((1-__u)/__a);
3857 __x = _STD::pow(1 - __a + __a * __e, 1 / __a);
3858 if (__x <= __e + __es)
3859 break;
3860 }
3861 }
3862 }
3863 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00003864}
3865
3866template <class _CharT, class _Traits, class _RT>
3867basic_ostream<_CharT, _Traits>&
3868operator<<(basic_ostream<_CharT, _Traits>& __os,
3869 const gamma_distribution<_RT>& __x)
3870{
3871 __save_flags<_CharT, _Traits> _(__os);
3872 __os.flags(ios_base::dec | ios_base::left);
3873 _CharT __sp = __os.widen(' ');
3874 __os.fill(__sp);
3875 __os << __x.alpha() << __sp << __x.beta();
3876 return __os;
3877}
3878
3879template <class _CharT, class _Traits, class _RT>
3880basic_istream<_CharT, _Traits>&
3881operator>>(basic_istream<_CharT, _Traits>& __is,
3882 gamma_distribution<_RT>& __x)
3883{
3884 typedef gamma_distribution<_RT> _Eng;
3885 typedef typename _Eng::result_type result_type;
3886 typedef typename _Eng::param_type param_type;
3887 __save_flags<_CharT, _Traits> _(__is);
3888 __is.flags(ios_base::dec | ios_base::skipws);
3889 result_type __alpha;
3890 result_type __beta;
3891 __is >> __alpha >> __beta;
3892 if (!__is.fail())
3893 __x.param(param_type(__alpha, __beta));
3894 return __is;
3895}
Howard Hinnanta64111c2010-05-12 21:02:31 +00003896
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003897_LIBCPP_END_NAMESPACE_STD
3898
3899#endif // _LIBCPP_RANDOM