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