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