blob: 1a680ff588a110b6f2d6c3be29ac61c3312626fe [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;
Howard Hinnantd6d11712010-05-20 15:11:46 +0000374typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375
376// Generators
377
378class random_device
379{
380public:
381 // types
382 typedef unsigned int result_type;
383
384 // generator characteristics
385 static constexpr result_type min() { return numeric_limits<result_type>::min(); }
386 static constexpr result_type max() { return numeric_limits<result_type>::max(); }
387
388 // constructors
389 explicit random_device(const string& token = "/dev/urandom");
390
391 // generating functions
392 result_type operator()();
393
394 // property functions
395 double entropy() const;
396
397 // no copy functions
398 random_device(const random_device& ) = delete;
399 void operator=(const random_device& ) = delete;
400};
401
402// Utilities
403
404class seed_seq
405{
406public:
407 // types
408 typedef uint_least32_t result_type;
409
410 // constructors
411 seed_seq();
412 template<class T>
413 seed_seq(initializer_list<T> il);
414 template<class InputIterator>
415 seed_seq(InputIterator begin, InputIterator end);
416
417 // generating functions
418 template<class RandomAccessIterator>
419 void generate(RandomAccessIterator begin, RandomAccessIterator end);
420
421 // property functions
422 size_t size() const;
423 template<class OutputIterator>
424 void param(OutputIterator dest) const;
425
426 // no copy functions
427 seed_seq(const seed_seq&) = delete;
428 void operator=(const seed_seq& ) = delete;
429};
430
431template<class RealType, size_t bits, class URNG>
432 RealType generate_canonical(URNG& g);
433
434// Distributions
435
436template<class IntType = int>
437class uniform_int_distribution
438{
439public:
440 // types
441 typedef IntType result_type;
442
443 class param_type
444 {
445 public:
446 typedef uniform_int_distribution distribution_type;
447
448 explicit param_type(IntType a = 0,
449 IntType b = numeric_limits<IntType>::max());
450
451 result_type a() const;
452 result_type b() const;
453
454 friend bool operator==(const param_type& x, const param_type& y);
455 friend bool operator!=(const param_type& x, const param_type& y);
456 };
457
458 // constructors and reset functions
459 explicit uniform_int_distribution(IntType a = 0,
460 IntType b = numeric_limits<IntType>::max());
461 explicit uniform_int_distribution(const param_type& parm);
462 void reset();
463
464 // generating functions
465 template<class URNG> result_type operator()(URNG& g);
466 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
467
468 // property functions
469 result_type a() const;
470 result_type b() const;
471
472 param_type param() const;
473 void param(const param_type& parm);
474
475 result_type min() const;
476 result_type max() const;
477
478 friend bool operator==(const uniform_int_distribution& x,
479 const uniform_int_distribution& y);
480 friend bool operator!=(const uniform_int_distribution& x,
481 const uniform_int_distribution& y);
482
483 template <class charT, class traits>
484 friend
485 basic_ostream<charT, traits>&
486 operator<<(basic_ostream<charT, traits>& os,
487 const uniform_int_distribution& x);
488
489 template <class charT, class traits>
490 friend
491 basic_istream<charT, traits>&
492 operator>>(basic_istream<charT, traits>& is,
493 uniform_int_distribution& x);
494};
495
496template<class RealType = double>
497class uniform_real_distribution
498{
499public:
500 // types
501 typedef RealType result_type;
502
503 class param_type
504 {
505 public:
506 typedef uniform_real_distribution distribution_type;
507
508 explicit param_type(RealType a = 0,
509 RealType b = 1);
510
511 result_type a() const;
512 result_type b() const;
513
514 friend bool operator==(const param_type& x, const param_type& y);
515 friend bool operator!=(const param_type& x, const param_type& y);
516 };
517
518 // constructors and reset functions
519 explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);
520 explicit uniform_real_distribution(const param_type& parm);
521 void reset();
522
523 // generating functions
524 template<class URNG> result_type operator()(URNG& g);
525 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
526
527 // property functions
528 result_type a() const;
529 result_type b() const;
530
531 param_type param() const;
532 void param(const param_type& parm);
533
534 result_type min() const;
535 result_type max() const;
536
537 friend bool operator==(const uniform_real_distribution& x,
538 const uniform_real_distribution& y);
539 friend bool operator!=(const uniform_real_distribution& x,
540 const uniform_real_distribution& y);
541
542 template <class charT, class traits>
543 friend
544 basic_ostream<charT, traits>&
545 operator<<(basic_ostream<charT, traits>& os,
546 const uniform_real_distribution& x);
547
548 template <class charT, class traits>
549 friend
550 basic_istream<charT, traits>&
551 operator>>(basic_istream<charT, traits>& is,
552 uniform_real_distribution& x);
553};
554
555class bernoulli_distribution
556{
557public:
558 // types
559 typedef bool result_type;
560
561 class param_type
562 {
563 public:
564 typedef bernoulli_distribution distribution_type;
565
566 explicit param_type(double p = 0.5);
567
568 double p() const;
569
570 friend bool operator==(const param_type& x, const param_type& y);
571 friend bool operator!=(const param_type& x, const param_type& y);
572 };
573
574 // constructors and reset functions
575 explicit bernoulli_distribution(double p = 0.5);
576 explicit bernoulli_distribution(const param_type& parm);
577 void reset();
578
579 // generating functions
580 template<class URNG> result_type operator()(URNG& g);
581 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
582
583 // property functions
584 double p() const;
585
586 param_type param() const;
587 void param(const param_type& parm);
588
589 result_type min() const;
590 result_type max() const;
591
592 friend bool operator==(const bernoulli_distribution& x,
593 const bernoulli_distribution& y);
594 friend bool operator!=(const bernoulli_distribution& x,
595 const bernoulli_distribution& y);
596
597 template <class charT, class traits>
598 friend
599 basic_ostream<charT, traits>&
600 operator<<(basic_ostream<charT, traits>& os,
601 const bernoulli_distribution& x);
602
603 template <class charT, class traits>
604 friend
605 basic_istream<charT, traits>&
606 operator>>(basic_istream<charT, traits>& is,
607 bernoulli_distribution& x);
608};
609
610template<class IntType = int>
Howard Hinnant03aad812010-05-11 23:26:59 +0000611class binomial_distribution
612{
613public:
614 // types
615 typedef IntType result_type;
616
617 class param_type
618 {
619 public:
620 typedef binomial_distribution distribution_type;
621
622 explicit param_type(IntType t = 1, double p = 0.5);
623
624 IntType t() const;
625 double p() const;
626
627 friend bool operator==(const param_type& x, const param_type& y);
628 friend bool operator!=(const param_type& x, const param_type& y);
629 };
630
631 // constructors and reset functions
632 explicit binomial_distribution(IntType t = 1, double p = 0.5);
633 explicit binomial_distribution(const param_type& parm);
634 void reset();
635
636 // generating functions
637 template<class URNG> result_type operator()(URNG& g);
638 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
639
640 // property functions
641 IntType t() const;
642 double p() const;
643
644 param_type param() const;
645 void param(const param_type& parm);
646
647 result_type min() const;
648 result_type max() const;
649
650 friend bool operator==(const binomial_distribution& x,
651 const binomial_distribution& y);
652 friend bool operator!=(const binomial_distribution& x,
653 const binomial_distribution& y);
654
655 template <class charT, class traits>
656 friend
657 basic_ostream<charT, traits>&
658 operator<<(basic_ostream<charT, traits>& os,
659 const binomial_distribution& x);
660
661 template <class charT, class traits>
662 friend
663 basic_istream<charT, traits>&
664 operator>>(basic_istream<charT, traits>& is,
665 binomial_distribution& x);
666};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667
668template<class IntType = int>
Howard Hinnant34e8a572010-05-17 13:44:27 +0000669class geometric_distribution
670{
671public:
672 // types
673 typedef IntType result_type;
674
675 class param_type
676 {
677 public:
678 typedef geometric_distribution distribution_type;
679
680 explicit param_type(double p = 0.5);
681
682 double p() const;
683
684 friend bool operator==(const param_type& x, const param_type& y);
685 friend bool operator!=(const param_type& x, const param_type& y);
686 };
687
688 // constructors and reset functions
689 explicit geometric_distribution(double p = 0.5);
690 explicit geometric_distribution(const param_type& parm);
691 void reset();
692
693 // generating functions
694 template<class URNG> result_type operator()(URNG& g);
695 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
696
697 // property functions
698 double p() const;
699
700 param_type param() const;
701 void param(const param_type& parm);
702
703 result_type min() const;
704 result_type max() const;
705
706 friend bool operator==(const geometric_distribution& x,
707 const geometric_distribution& y);
708 friend bool operator!=(const geometric_distribution& x,
709 const geometric_distribution& y);
710
711 template <class charT, class traits>
712 friend
713 basic_ostream<charT, traits>&
714 operator<<(basic_ostream<charT, traits>& os,
715 const geometric_distribution& x);
716
717 template <class charT, class traits>
718 friend
719 basic_istream<charT, traits>&
720 operator>>(basic_istream<charT, traits>& is,
721 geometric_distribution& x);
722};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723
724template<class IntType = int>
Howard Hinnantf2fe5d52010-05-17 00:09:38 +0000725class negative_binomial_distribution
726{
727public:
728 // types
729 typedef IntType result_type;
730
731 class param_type
732 {
733 public:
734 typedef negative_binomial_distribution distribution_type;
735
736 explicit param_type(result_type k = 1, double p = 0.5);
737
738 result_type k() const;
739 double p() const;
740
741 friend bool operator==(const param_type& x, const param_type& y);
742 friend bool operator!=(const param_type& x, const param_type& y);
743 };
744
745 // constructor and reset functions
746 explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
747 explicit negative_binomial_distribution(const param_type& parm);
748 void reset();
749
750 // generating functions
751 template<class URNG> result_type operator()(URNG& g);
752 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
753
754 // property functions
755 result_type k() const;
756 double p() const;
757
758 param_type param() const;
759 void param(const param_type& parm);
760
761 result_type min() const;
762 result_type max() const;
763
764 friend bool operator==(const negative_binomial_distribution& x,
765 const negative_binomial_distribution& y);
766 friend bool operator!=(const negative_binomial_distribution& x,
767 const negative_binomial_distribution& y);
768
769 template <class charT, class traits>
770 friend
771 basic_ostream<charT, traits>&
772 operator<<(basic_ostream<charT, traits>& os,
773 const negative_binomial_distribution& x);
774
775 template <class charT, class traits>
776 friend
777 basic_istream<charT, traits>&
778 operator>>(basic_istream<charT, traits>& is,
779 negative_binomial_distribution& x);
780};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000781
782template<class IntType = int>
Howard Hinnant4ff556c2010-05-14 21:38:54 +0000783class poisson_distribution
784{
785public:
786 // types
787 typedef IntType result_type;
788
789 class param_type
790 {
791 public:
792 typedef poisson_distribution distribution_type;
793
794 explicit param_type(double mean = 1.0);
795
796 double mean() const;
797
798 friend bool operator==(const param_type& x, const param_type& y);
799 friend bool operator!=(const param_type& x, const param_type& y);
800 };
801
802 // constructors and reset functions
803 explicit poisson_distribution(double mean = 1.0);
804 explicit poisson_distribution(const param_type& parm);
805 void reset();
806
807 // generating functions
808 template<class URNG> result_type operator()(URNG& g);
809 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
810
811 // property functions
812 double mean() const;
813
814 param_type param() const;
815 void param(const param_type& parm);
816
817 result_type min() const;
818 result_type max() const;
819
820 friend bool operator==(const poisson_distribution& x,
821 const poisson_distribution& y);
822 friend bool operator!=(const poisson_distribution& x,
823 const poisson_distribution& y);
824
825 template <class charT, class traits>
826 friend
827 basic_ostream<charT, traits>&
828 operator<<(basic_ostream<charT, traits>& os,
829 const poisson_distribution& x);
830
831 template <class charT, class traits>
832 friend
833 basic_istream<charT, traits>&
834 operator>>(basic_istream<charT, traits>& is,
835 poisson_distribution& x);
836};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837
838template<class RealType = double>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000839class exponential_distribution
840{
841public:
842 // types
843 typedef RealType result_type;
844
845 class param_type
846 {
847 public:
848 typedef exponential_distribution distribution_type;
849
Howard Hinnanta64111c2010-05-12 21:02:31 +0000850 explicit param_type(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000851
Howard Hinnanta64111c2010-05-12 21:02:31 +0000852 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000853
854 friend bool operator==(const param_type& x, const param_type& y);
855 friend bool operator!=(const param_type& x, const param_type& y);
856 };
857
858 // constructors and reset functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000859 explicit exponential_distribution(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000860 explicit exponential_distribution(const param_type& parm);
861 void reset();
862
863 // generating functions
864 template<class URNG> result_type operator()(URNG& g);
865 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
866
867 // property functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000868 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000869
870 param_type param() const;
871 void param(const param_type& parm);
872
873 result_type min() const;
874 result_type max() const;
875
876 friend bool operator==(const exponential_distribution& x,
877 const exponential_distribution& y);
878 friend bool operator!=(const exponential_distribution& x,
879 const exponential_distribution& y);
880
881 template <class charT, class traits>
882 friend
883 basic_ostream<charT, traits>&
884 operator<<(basic_ostream<charT, traits>& os,
885 const exponential_distribution& x);
886
887 template <class charT, class traits>
888 friend
889 basic_istream<charT, traits>&
890 operator>>(basic_istream<charT, traits>& is,
891 exponential_distribution& x);
892};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893
894template<class RealType = double>
Howard Hinnantc7c49132010-05-13 17:58:28 +0000895class gamma_distribution
896{
897public:
898 // types
899 typedef RealType result_type;
900
901 class param_type
902 {
903 public:
904 typedef gamma_distribution distribution_type;
905
906 explicit param_type(result_type alpha = 1, result_type beta = 1);
907
908 result_type alpha() const;
909 result_type beta() const;
910
911 friend bool operator==(const param_type& x, const param_type& y);
912 friend bool operator!=(const param_type& x, const param_type& y);
913 };
914
915 // constructors and reset functions
916 explicit gamma_distribution(result_type alpha = 1, result_type beta = 1);
917 explicit gamma_distribution(const param_type& parm);
918 void reset();
919
920 // generating functions
921 template<class URNG> result_type operator()(URNG& g);
922 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
923
924 // property functions
925 result_type alpha() const;
926 result_type beta() const;
927
928 param_type param() const;
929 void param(const param_type& parm);
930
931 result_type min() const;
932 result_type max() const;
933
934 friend bool operator==(const gamma_distribution& x,
935 const gamma_distribution& y);
936 friend bool operator!=(const gamma_distribution& x,
937 const gamma_distribution& y);
938
939 template <class charT, class traits>
940 friend
941 basic_ostream<charT, traits>&
942 operator<<(basic_ostream<charT, traits>& os,
943 const gamma_distribution& x);
944
945 template <class charT, class traits>
946 friend
947 basic_istream<charT, traits>&
948 operator>>(basic_istream<charT, traits>& is,
949 gamma_distribution& x);
950};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951
952template<class RealType = double>
Howard Hinnant9de6e302010-05-16 01:09:02 +0000953class weibull_distribution
954{
955public:
956 // types
957 typedef RealType result_type;
958
959 class param_type
960 {
961 public:
962 typedef weibull_distribution distribution_type;
963
964 explicit param_type(result_type alpha = 1, result_type beta = 1);
965
966 result_type a() const;
967 result_type b() const;
968
969 friend bool operator==(const param_type& x, const param_type& y);
970 friend bool operator!=(const param_type& x, const param_type& y);
971 };
972
973 // constructor and reset functions
974 explicit weibull_distribution(result_type a = 1, result_type b = 1);
975 explicit weibull_distribution(const param_type& parm);
976 void reset();
977
978 // generating functions
979 template<class URNG> result_type operator()(URNG& g);
980 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
981
982 // property functions
983 result_type a() const;
984 result_type b() const;
985
986 param_type param() const;
987 void param(const param_type& parm);
988
989 result_type min() const;
990 result_type max() const;
991
Howard Hinnant9de6e302010-05-16 01:09:02 +0000992 friend bool operator==(const weibull_distribution& x,
993 const weibull_distribution& y);
994 friend bool operator!=(const weibull_distribution& x,
995 const weibull_distribution& y);
996
997 template <class charT, class traits>
998 friend
999 basic_ostream<charT, traits>&
1000 operator<<(basic_ostream<charT, traits>& os,
1001 const weibull_distribution& x);
1002
1003 template <class charT, class traits>
1004 friend
1005 basic_istream<charT, traits>&
1006 operator>>(basic_istream<charT, traits>& is,
1007 weibull_distribution& x);
1008};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009
1010template<class RealType = double>
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00001011class extreme_value_distribution
1012{
1013public:
1014 // types
1015 typedef RealType result_type;
1016
1017 class param_type
1018 {
1019 public:
1020 typedef extreme_value_distribution distribution_type;
1021
1022 explicit param_type(result_type a = 0, result_type b = 1);
1023
1024 result_type a() const;
1025 result_type b() const;
1026
1027 friend bool operator==(const param_type& x, const param_type& y);
1028 friend bool operator!=(const param_type& x, const param_type& y);
1029 };
1030
1031 // constructor and reset functions
1032 explicit extreme_value_distribution(result_type a = 0, result_type b = 1);
1033 explicit extreme_value_distribution(const param_type& parm);
1034 void reset();
1035
1036 // generating functions
1037 template<class URNG> result_type operator()(URNG& g);
1038 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1039
1040 // property functions
1041 result_type a() const;
1042 result_type b() const;
1043
1044 param_type param() const;
1045 void param(const param_type& parm);
1046
1047 result_type min() const;
1048 result_type max() const;
1049
1050 friend bool operator==(const extreme_value_distribution& x,
1051 const extreme_value_distribution& y);
1052 friend bool operator!=(const extreme_value_distribution& x,
1053 const extreme_value_distribution& y);
1054
1055 template <class charT, class traits>
1056 friend
1057 basic_ostream<charT, traits>&
1058 operator<<(basic_ostream<charT, traits>& os,
1059 const extreme_value_distribution& x);
1060
1061 template <class charT, class traits>
1062 friend
1063 basic_istream<charT, traits>&
1064 operator>>(basic_istream<charT, traits>& is,
1065 extreme_value_distribution& x);
1066};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067
1068template<class RealType = double>
Howard Hinnanta64111c2010-05-12 21:02:31 +00001069class normal_distribution
1070{
1071public:
1072 // types
1073 typedef RealType result_type;
1074
1075 class param_type
1076 {
1077 public:
1078 typedef normal_distribution distribution_type;
1079
1080 explicit param_type(result_type mean = 0, result_type stddev = 1);
1081
1082 result_type mean() const;
1083 result_type stddev() const;
1084
1085 friend bool operator==(const param_type& x, const param_type& y);
1086 friend bool operator!=(const param_type& x, const param_type& y);
1087 };
1088
1089 // constructors and reset functions
1090 explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
1091 explicit normal_distribution(const param_type& parm);
1092 void reset();
1093
1094 // generating functions
1095 template<class URNG> result_type operator()(URNG& g);
1096 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1097
1098 // property functions
1099 result_type mean() const;
1100 result_type stddev() const;
1101
1102 param_type param() const;
1103 void param(const param_type& parm);
1104
1105 result_type min() const;
1106 result_type max() const;
1107
1108 friend bool operator==(const normal_distribution& x,
1109 const normal_distribution& y);
1110 friend bool operator!=(const normal_distribution& x,
1111 const normal_distribution& y);
1112
1113 template <class charT, class traits>
1114 friend
1115 basic_ostream<charT, traits>&
1116 operator<<(basic_ostream<charT, traits>& os,
1117 const normal_distribution& x);
1118
1119 template <class charT, class traits>
1120 friend
1121 basic_istream<charT, traits>&
1122 operator>>(basic_istream<charT, traits>& is,
1123 normal_distribution& x);
1124};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125
1126template<class RealType = double>
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00001127class lognormal_distribution
1128{
1129public:
1130 // types
1131 typedef RealType result_type;
1132
1133 class param_type
1134 {
1135 public:
1136 typedef lognormal_distribution distribution_type;
1137
1138 explicit param_type(result_type m = 0, result_type s = 1);
1139
1140 result_type m() const;
1141 result_type s() const;
1142
1143 friend bool operator==(const param_type& x, const param_type& y);
1144 friend bool operator!=(const param_type& x, const param_type& y);
1145 };
1146
1147 // constructor and reset functions
1148 explicit lognormal_distribution(result_type m = 0, result_type s = 1);
1149 explicit lognormal_distribution(const param_type& parm);
1150 void reset();
1151
1152 // generating functions
1153 template<class URNG> result_type operator()(URNG& g);
1154 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1155
1156 // property functions
1157 result_type m() const;
1158 result_type s() const;
1159
1160 param_type param() const;
1161 void param(const param_type& parm);
1162
1163 result_type min() const;
1164 result_type max() const;
1165
1166 friend bool operator==(const lognormal_distribution& x,
1167 const lognormal_distribution& y);
1168 friend bool operator!=(const lognormal_distribution& x,
1169 const lognormal_distribution& y);
1170
1171 template <class charT, class traits>
1172 friend
1173 basic_ostream<charT, traits>&
1174 operator<<(basic_ostream<charT, traits>& os,
1175 const lognormal_distribution& x);
1176
1177 template <class charT, class traits>
1178 friend
1179 basic_istream<charT, traits>&
1180 operator>>(basic_istream<charT, traits>& is,
1181 lognormal_distribution& x);
1182};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183
1184template<class RealType = double>
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001185class chi_squared_distribution
1186{
1187public:
1188 // types
1189 typedef RealType result_type;
1190
1191 class param_type
1192 {
1193 public:
1194 typedef chi_squared_distribution distribution_type;
1195
1196 explicit param_type(result_type n = 1);
1197
1198 result_type n() const;
1199
1200 friend bool operator==(const param_type& x, const param_type& y);
1201 friend bool operator!=(const param_type& x, const param_type& y);
1202 };
1203
1204 // constructor and reset functions
1205 explicit chi_squared_distribution(result_type n = 1);
1206 explicit chi_squared_distribution(const param_type& parm);
1207 void reset();
1208
1209 // generating functions
1210 template<class URNG> result_type operator()(URNG& g);
1211 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1212
1213 // property functions
1214 result_type n() const;
1215
1216 param_type param() const;
1217 void param(const param_type& parm);
1218
1219 result_type min() const;
1220 result_type max() const;
1221
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001222 friend bool operator==(const chi_squared_distribution& x,
1223 const chi_squared_distribution& y);
1224 friend bool operator!=(const chi_squared_distribution& x,
1225 const chi_squared_distribution& y);
1226
1227 template <class charT, class traits>
1228 friend
1229 basic_ostream<charT, traits>&
1230 operator<<(basic_ostream<charT, traits>& os,
1231 const chi_squared_distribution& x);
1232
1233 template <class charT, class traits>
1234 friend
1235 basic_istream<charT, traits>&
1236 operator>>(basic_istream<charT, traits>& is,
1237 chi_squared_distribution& x);
1238};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239
1240template<class RealType = double>
Howard Hinnantd7d01132010-05-17 21:55:46 +00001241class cauchy_distribution
1242{
1243public:
1244 // types
1245 typedef RealType result_type;
1246
1247 class param_type
1248 {
1249 public:
1250 typedef cauchy_distribution distribution_type;
1251
1252 explicit param_type(result_type a = 0, result_type b = 1);
1253
1254 result_type a() const;
1255 result_type b() const;
1256
1257 friend bool operator==(const param_type& x, const param_type& y);
1258 friend bool operator!=(const param_type& x, const param_type& y);
1259 };
1260
1261 // constructor and reset functions
1262 explicit cauchy_distribution(result_type a = 0, result_type b = 1);
1263 explicit cauchy_distribution(const param_type& parm);
1264 void reset();
1265
1266 // generating functions
1267 template<class URNG> result_type operator()(URNG& g);
1268 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1269
1270 // property functions
1271 result_type a() const;
1272 result_type b() const;
1273
1274 param_type param() const;
1275 void param(const param_type& parm);
1276
1277 result_type min() const;
1278 result_type max() const;
1279
1280 friend bool operator==(const cauchy_distribution& x,
1281 const cauchy_distribution& y);
1282 friend bool operator!=(const cauchy_distribution& x,
1283 const cauchy_distribution& y);
1284
1285 template <class charT, class traits>
1286 friend
1287 basic_ostream<charT, traits>&
1288 operator<<(basic_ostream<charT, traits>& os,
1289 const cauchy_distribution& x);
1290
1291 template <class charT, class traits>
1292 friend
1293 basic_istream<charT, traits>&
1294 operator>>(basic_istream<charT, traits>& is,
1295 cauchy_distribution& x);
1296};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297
1298template<class RealType = double>
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001299class fisher_f_distribution
1300{
1301public:
1302 // types
1303 typedef RealType result_type;
1304
1305 class param_type
1306 {
1307 public:
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001308 typedef fisher_f_distribution distribution_type;
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001309
1310 explicit param_type(result_type m = 1, result_type n = 1);
1311
1312 result_type m() const;
1313 result_type n() const;
1314
1315 friend bool operator==(const param_type& x, const param_type& y);
1316 friend bool operator!=(const param_type& x, const param_type& y);
1317 };
1318
1319 // constructor and reset functions
1320 explicit fisher_f_distribution(result_type m = 1, result_type n = 1);
1321 explicit fisher_f_distribution(const param_type& parm);
1322 void reset();
1323
1324 // generating functions
1325 template<class URNG> result_type operator()(URNG& g);
1326 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1327
1328 // property functions
1329 result_type m() const;
1330 result_type n() const;
1331
1332 param_type param() const;
1333 void param(const param_type& parm);
1334
1335 result_type min() const;
1336 result_type max() const;
1337
1338 friend bool operator==(const fisher_f_distribution& x,
1339 const fisher_f_distribution& y);
1340 friend bool operator!=(const fisher_f_distribution& x,
1341 const fisher_f_distribution& y);
1342
1343 template <class charT, class traits>
1344 friend
1345 basic_ostream<charT, traits>&
1346 operator<<(basic_ostream<charT, traits>& os,
1347 const fisher_f_distribution& x);
1348
1349 template <class charT, class traits>
1350 friend
1351 basic_istream<charT, traits>&
1352 operator>>(basic_istream<charT, traits>& is,
1353 fisher_f_distribution& x);
1354};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355
1356template<class RealType = double>
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001357class student_t_distribution
1358{
1359public:
1360 // types
1361 typedef RealType result_type;
1362
1363 class param_type
1364 {
1365 public:
1366 typedef student_t_distribution distribution_type;
1367
1368 explicit param_type(result_type n = 1);
1369
1370 result_type n() const;
1371
1372 friend bool operator==(const param_type& x, const param_type& y);
1373 friend bool operator!=(const param_type& x, const param_type& y);
1374 };
1375
1376 // constructor and reset functions
1377 explicit student_t_distribution(result_type n = 1);
1378 explicit student_t_distribution(const param_type& parm);
1379 void reset();
1380
1381 // generating functions
1382 template<class URNG> result_type operator()(URNG& g);
1383 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1384
1385 // property functions
1386 result_type n() const;
1387
1388 param_type param() const;
1389 void param(const param_type& parm);
1390
1391 result_type min() const;
1392 result_type max() const;
1393
1394 friend bool operator==(const student_t_distribution& x,
1395 const student_t_distribution& y);
1396 friend bool operator!=(const student_t_distribution& x,
1397 const student_t_distribution& y);
1398
1399 template <class charT, class traits>
1400 friend
1401 basic_ostream<charT, traits>&
1402 operator<<(basic_ostream<charT, traits>& os,
1403 const student_t_distribution& x);
1404
1405 template <class charT, class traits>
1406 friend
1407 basic_istream<charT, traits>&
1408 operator>>(basic_istream<charT, traits>& is,
1409 student_t_distribution& x);
1410};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411
1412template<class IntType = int>
Howard Hinnant551d8e42010-05-19 01:53:57 +00001413class discrete_distribution
1414{
1415public:
1416 // types
1417 typedef IntType result_type;
1418
1419 class param_type
1420 {
1421 public:
1422 typedef discrete_distribution distribution_type;
1423
1424 param_type();
1425 template<class InputIterator>
1426 param_type(InputIterator firstW, InputIterator lastW);
1427 param_type(initializer_list<double> wl);
1428 template<class UnaryOperation>
1429 param_type(size_t nw, double xmin, double xmax, UnaryOperation fw);
1430
1431 vector<double> probabilities() const;
1432
1433 friend bool operator==(const param_type& x, const param_type& y);
1434 friend bool operator!=(const param_type& x, const param_type& y);
1435 };
1436
1437 // constructor and reset functions
1438 discrete_distribution();
1439 template<class InputIterator>
1440 discrete_distribution(InputIterator firstW, InputIterator lastW);
1441 discrete_distribution(initializer_list<double> wl);
1442 template<class UnaryOperation>
1443 discrete_distribution(size_t nw, double xmin, double xmax,
1444 UnaryOperation fw);
1445 explicit discrete_distribution(const param_type& parm);
1446 void reset();
1447
1448 // generating functions
1449 template<class URNG> result_type operator()(URNG& g);
1450 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1451
1452 // property functions
1453 vector<double> probabilities() const;
1454
1455 param_type param() const;
1456 void param(const param_type& parm);
1457
1458 result_type min() const;
1459 result_type max() const;
1460
1461 friend bool operator==(const discrete_distribution& x,
1462 const discrete_distribution& y);
1463 friend bool operator!=(const discrete_distribution& x,
1464 const discrete_distribution& y);
1465
1466 template <class charT, class traits>
1467 friend
1468 basic_ostream<charT, traits>&
1469 operator<<(basic_ostream<charT, traits>& os,
1470 const discrete_distribution& x);
1471
1472 template <class charT, class traits>
1473 friend
1474 basic_istream<charT, traits>&
1475 operator>>(basic_istream<charT, traits>& is,
1476 discrete_distribution& x);
1477};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478
1479template<class RealType = double>
Howard Hinnantd6d11712010-05-20 15:11:46 +00001480class piecewise_constant_distribution
1481{
1482 // types
1483 typedef RealType result_type;
1484
1485 class param_type
1486 {
1487 public:
1488 typedef piecewise_constant_distribution distribution_type;
1489
1490 param_type();
1491 template<class InputIteratorB, class InputIteratorW>
1492 param_type(InputIteratorB firstB, InputIteratorB lastB,
1493 InputIteratorW firstW);
1494 template<class UnaryOperation>
1495 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1496 template<class UnaryOperation>
1497 param_type(size_t nw, result_type xmin, result_type xmax,
1498 UnaryOperation fw);
1499
1500 vector<result_type> intervals() const;
1501 vector<double> densities() const;
1502
1503 friend bool operator==(const param_type& x, const param_type& y);
1504 friend bool operator!=(const param_type& x, const param_type& y);
1505 };
1506
1507 // constructor and reset functions
1508 piecewise_constant_distribution();
1509 template<class InputIteratorB, class InputIteratorW>
1510 piecewise_constant_distribution(InputIteratorB firstB,
1511 InputIteratorB lastB,
1512 InputIteratorW firstW);
1513 template<class UnaryOperation>
1514 piecewise_constant_distribution(initializer_list<result_type> bl,
1515 UnaryOperation fw);
1516 template<class UnaryOperation>
1517 piecewise_constant_distribution(size_t nw, result_type xmin,
1518 result_type xmax, UnaryOperation fw);
1519 explicit piecewise_constant_distribution(const param_type& parm);
1520 void reset();
1521
1522 // generating functions
1523 template<class URNG> result_type operator()(URNG& g);
1524 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1525
1526 // property functions
1527 vector<result_type> intervals() const;
1528 vector<double> densities() const;
1529
1530 param_type param() const;
1531 void param(const param_type& parm);
1532
1533 result_type min() const;
1534 result_type max() const;
1535
1536 friend bool operator==(const piecewise_constant_distribution& x,
1537 const piecewise_constant_distribution& y);
1538 friend bool operator!=(const piecewise_constant_distribution& x,
1539 const piecewise_constant_distribution& y);
1540
1541 template <class charT, class traits>
1542 friend
1543 basic_ostream<charT, traits>&
1544 operator<<(basic_ostream<charT, traits>& os,
1545 const piecewise_constant_distribution& x);
1546
1547 template <class charT, class traits>
1548 friend
1549 basic_istream<charT, traits>&
1550 operator>>(basic_istream<charT, traits>& is,
1551 piecewise_constant_distribution& x);
1552};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553
1554template<class RealType = double>
1555 class piecewise_linear_distribution;
1556
1557} // std
1558*/
1559
1560#include <__config>
1561#include <cstddef>
1562#include <type_traits>
1563#include <initializer_list>
1564#include <cstdint>
1565#include <limits>
1566#include <algorithm>
Howard Hinnant551d8e42010-05-19 01:53:57 +00001567#include <numeric>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001568#include <vector>
1569#include <string>
1570#include <istream>
1571#include <ostream>
Howard Hinnant30a840f2010-05-12 17:08:57 +00001572#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573
1574#pragma GCC system_header
1575
1576_LIBCPP_BEGIN_NAMESPACE_STD
1577
1578// linear_congruential_engine
1579
1580template <unsigned long long __a, unsigned long long __c,
1581 unsigned long long __m, unsigned long long _M,
1582 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
1583struct __lce_ta;
1584
1585// 64
1586
1587template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1588struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1589{
1590 typedef unsigned long long result_type;
1591 static result_type next(result_type __x)
1592 {
1593 // Schrage's algorithm
1594 const result_type __q = __m / __a;
1595 const result_type __r = __m % __a;
1596 const result_type __t0 = __a * (__x % __q);
1597 const result_type __t1 = __r * (__x / __q);
1598 __x = __t0 + (__t0 < __t1) * __m - __t1;
1599 __x += __c - (__x >= __m - __c) * __m;
1600 return __x;
1601 }
1602};
1603
1604template <unsigned long long __a, unsigned long long __m>
1605struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1606{
1607 typedef unsigned long long result_type;
1608 static result_type next(result_type __x)
1609 {
1610 // Schrage's algorithm
1611 const result_type __q = __m / __a;
1612 const result_type __r = __m % __a;
1613 const result_type __t0 = __a * (__x % __q);
1614 const result_type __t1 = __r * (__x / __q);
1615 __x = __t0 + (__t0 < __t1) * __m - __t1;
1616 return __x;
1617 }
1618};
1619
1620template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1621struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1622{
1623 typedef unsigned long long result_type;
1624 static result_type next(result_type __x)
1625 {
1626 return (__a * __x + __c) % __m;
1627 }
1628};
1629
1630template <unsigned long long __a, unsigned long long __c>
1631struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1632{
1633 typedef unsigned long long result_type;
1634 static result_type next(result_type __x)
1635 {
1636 return __a * __x + __c;
1637 }
1638};
1639
1640// 32
1641
1642template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1643struct __lce_ta<_A, _C, _M, unsigned(~0), true>
1644{
1645 typedef unsigned result_type;
1646 static result_type next(result_type __x)
1647 {
1648 const result_type __a = static_cast<result_type>(_A);
1649 const result_type __c = static_cast<result_type>(_C);
1650 const result_type __m = static_cast<result_type>(_M);
1651 // Schrage's algorithm
1652 const result_type __q = __m / __a;
1653 const result_type __r = __m % __a;
1654 const result_type __t0 = __a * (__x % __q);
1655 const result_type __t1 = __r * (__x / __q);
1656 __x = __t0 + (__t0 < __t1) * __m - __t1;
1657 __x += __c - (__x >= __m - __c) * __m;
1658 return __x;
1659 }
1660};
1661
1662template <unsigned long long _A, unsigned long long _M>
1663struct __lce_ta<_A, 0, _M, unsigned(~0), true>
1664{
1665 typedef unsigned result_type;
1666 static result_type next(result_type __x)
1667 {
1668 const result_type __a = static_cast<result_type>(_A);
1669 const result_type __m = static_cast<result_type>(_M);
1670 // Schrage's algorithm
1671 const result_type __q = __m / __a;
1672 const result_type __r = __m % __a;
1673 const result_type __t0 = __a * (__x % __q);
1674 const result_type __t1 = __r * (__x / __q);
1675 __x = __t0 + (__t0 < __t1) * __m - __t1;
1676 return __x;
1677 }
1678};
1679
1680template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1681struct __lce_ta<_A, _C, _M, unsigned(~0), false>
1682{
1683 typedef unsigned result_type;
1684 static result_type next(result_type __x)
1685 {
1686 const result_type __a = static_cast<result_type>(_A);
1687 const result_type __c = static_cast<result_type>(_C);
1688 const result_type __m = static_cast<result_type>(_M);
1689 return (__a * __x + __c) % __m;
1690 }
1691};
1692
1693template <unsigned long long _A, unsigned long long _C>
1694struct __lce_ta<_A, _C, 0, unsigned(~0), false>
1695{
1696 typedef unsigned result_type;
1697 static result_type next(result_type __x)
1698 {
1699 const result_type __a = static_cast<result_type>(_A);
1700 const result_type __c = static_cast<result_type>(_C);
1701 return __a * __x + __c;
1702 }
1703};
1704
1705// 16
1706
1707template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1708struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1709{
1710 typedef unsigned short result_type;
1711 static result_type next(result_type __x)
1712 {
1713 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1714 }
1715};
1716
1717template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1718class linear_congruential_engine;
1719
1720template <class _CharT, class _Traits,
1721 class _U, _U _A, _U _C, _U _N>
1722basic_ostream<_CharT, _Traits>&
1723operator<<(basic_ostream<_CharT, _Traits>& __os,
1724 const linear_congruential_engine<_U, _A, _C, _N>&);
1725
1726template <class _CharT, class _Traits,
1727 class _U, _U _A, _U _C, _U _N>
1728basic_istream<_CharT, _Traits>&
1729operator>>(basic_istream<_CharT, _Traits>& __is,
1730 linear_congruential_engine<_U, _A, _C, _N>& __x);
1731
1732template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1733class linear_congruential_engine
1734{
1735public:
1736 // types
1737 typedef _UIntType result_type;
1738
1739private:
1740 result_type __x_;
1741
1742 static const result_type _M = result_type(~0);
1743
1744 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1745 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1746public:
1747 static const result_type _Min = __c == 0u ? 1u: 0u;
1748 static const result_type _Max = __m - 1u;
1749 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1750
1751 // engine characteristics
1752 static const/*expr*/ result_type multiplier = __a;
1753 static const/*expr*/ result_type increment = __c;
1754 static const/*expr*/ result_type modulus = __m;
1755 static const/*expr*/ result_type min() {return _Min;}
1756 static const/*expr*/ result_type max() {return _Max;}
1757 static const/*expr*/ result_type default_seed = 1u;
1758
1759 // constructors and seeding functions
1760 explicit linear_congruential_engine(result_type __s = default_seed)
1761 {seed(__s);}
1762 template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q)
1763 {seed(__q);}
1764 void seed(result_type __s = default_seed)
1765 {seed(integral_constant<bool, __m == 0>(),
1766 integral_constant<bool, __c == 0>(), __s);}
1767 template<class _Sseq>
1768 typename enable_if
1769 <
1770 !is_convertible<_Sseq, result_type>::value,
1771 void
1772 >::type
1773 seed(_Sseq& __q)
1774 {__seed(__q, integral_constant<unsigned,
1775 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1776 : (__m-1) / 0x100000000ull)>());}
1777
1778 // generating functions
1779 result_type operator()()
1780 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
1781 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1782
1783 friend bool operator==(const linear_congruential_engine& __x,
1784 const linear_congruential_engine& __y)
1785 {return __x.__x_ == __y.__x_;}
1786 friend bool operator!=(const linear_congruential_engine& __x,
1787 const linear_congruential_engine& __y)
1788 {return !(__x == __y);}
1789
1790private:
1791
1792 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
1793 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
1794 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1795 1 : __s % __m;}
1796 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1797
1798 template<class _Sseq>
1799 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1800 template<class _Sseq>
1801 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1802
1803 template <class _CharT, class _Traits,
1804 class _U, _U _A, _U _C, _U _N>
1805 friend
1806 basic_ostream<_CharT, _Traits>&
1807 operator<<(basic_ostream<_CharT, _Traits>& __os,
1808 const linear_congruential_engine<_U, _A, _C, _N>&);
1809
1810 template <class _CharT, class _Traits,
1811 class _U, _U _A, _U _C, _U _N>
1812 friend
1813 basic_istream<_CharT, _Traits>&
1814 operator>>(basic_istream<_CharT, _Traits>& __is,
1815 linear_congruential_engine<_U, _A, _C, _N>& __x);
1816};
1817
1818template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1819template<class _Sseq>
1820void
1821linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1822 integral_constant<unsigned, 1>)
1823{
1824 const unsigned __k = 1;
1825 uint32_t __ar[__k+3];
1826 __q.generate(__ar, __ar + __k + 3);
1827 result_type __s = static_cast<result_type>(__ar[3] % __m);
1828 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1829}
1830
1831template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1832template<class _Sseq>
1833void
1834linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1835 integral_constant<unsigned, 2>)
1836{
1837 const unsigned __k = 2;
1838 uint32_t __ar[__k+3];
1839 __q.generate(__ar, __ar + __k + 3);
1840 result_type __s = static_cast<result_type>((__ar[3] +
1841 (uint64_t)__ar[4] << 32) % __m);
1842 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1843}
1844
1845template <class _CharT, class _Traits>
1846class __save_flags
1847{
1848 typedef basic_ios<_CharT, _Traits> __stream_type;
1849 typedef typename __stream_type::fmtflags fmtflags;
1850
1851 __stream_type& __stream_;
1852 fmtflags __fmtflags_;
1853 _CharT __fill_;
1854
1855 __save_flags(const __save_flags&);
1856 __save_flags& operator=(const __save_flags&);
1857public:
1858 explicit __save_flags(__stream_type& __stream)
1859 : __stream_(__stream),
1860 __fmtflags_(__stream.flags()),
1861 __fill_(__stream.fill())
1862 {}
1863 ~__save_flags()
1864 {
1865 __stream_.flags(__fmtflags_);
1866 __stream_.fill(__fill_);
1867 }
1868};
1869
1870template <class _CharT, class _Traits,
1871 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1872inline
1873basic_ostream<_CharT, _Traits>&
1874operator<<(basic_ostream<_CharT, _Traits>& __os,
1875 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1876{
1877 __save_flags<_CharT, _Traits> _(__os);
1878 __os.flags(ios_base::dec | ios_base::left);
1879 __os.fill(__os.widen(' '));
1880 return __os << __x.__x_;
1881}
1882
1883template <class _CharT, class _Traits,
1884 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1885basic_istream<_CharT, _Traits>&
1886operator>>(basic_istream<_CharT, _Traits>& __is,
1887 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1888{
1889 __save_flags<_CharT, _Traits> _(__is);
1890 __is.flags(ios_base::dec | ios_base::skipws);
1891 _UIntType __t;
1892 __is >> __t;
1893 if (!__is.fail())
1894 __x.__x_ = __t;
1895 return __is;
1896}
1897
1898typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
1899 minstd_rand0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
1901 minstd_rand;
Howard Hinnantd6d11712010-05-20 15:11:46 +00001902typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903// mersenne_twister_engine
1904
1905template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1906 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1907 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1908class mersenne_twister_engine;
1909
1910template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1911 _UI _A, size_t _U, _UI _D, size_t _S,
1912 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1913bool
1914operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1915 _B, _T, _C, _L, _F>& __x,
1916 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1917 _B, _T, _C, _L, _F>& __y);
1918
1919template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1920 _UI _A, size_t _U, _UI _D, size_t _S,
1921 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1922bool
1923operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1924 _B, _T, _C, _L, _F>& __x,
1925 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1926 _B, _T, _C, _L, _F>& __y);
1927
1928template <class _CharT, class _Traits,
1929 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1930 _UI _A, size_t _U, _UI _D, size_t _S,
1931 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1932basic_ostream<_CharT, _Traits>&
1933operator<<(basic_ostream<_CharT, _Traits>& __os,
1934 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1935 _B, _T, _C, _L, _F>& __x);
1936
1937template <class _CharT, class _Traits,
1938 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1939 _UI _A, size_t _U, _UI _D, size_t _S,
1940 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1941basic_istream<_CharT, _Traits>&
1942operator>>(basic_istream<_CharT, _Traits>& __is,
1943 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1944 _B, _T, _C, _L, _F>& __x);
1945
1946template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1947 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1948 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1949class mersenne_twister_engine
1950{
1951public:
1952 // types
1953 typedef _UIntType result_type;
1954
1955private:
1956 result_type __x_[__n];
1957 size_t __i_;
1958
1959 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
1960 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
1961 static const result_type _Dt = numeric_limits<result_type>::digits;
1962 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
1963 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
1964 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
1965 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
1966 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
1967 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
1968 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
1969public:
1970 static const result_type _Min = 0;
1971 static const result_type _Max = __w == _Dt ? result_type(~0) :
1972 (result_type(1) << __w) - result_type(1);
1973 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
1974 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
1975 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
1976 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
1977 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
1978 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
1979
1980 // engine characteristics
1981 static const/*expr*/ size_t word_size = __w;
1982 static const/*expr*/ size_t state_size = __n;
1983 static const/*expr*/ size_t shift_size = __m;
1984 static const/*expr*/ size_t mask_bits = __r;
1985 static const/*expr*/ result_type xor_mask = __a;
1986 static const/*expr*/ size_t tempering_u = __u;
1987 static const/*expr*/ result_type tempering_d = __d;
1988 static const/*expr*/ size_t tempering_s = __s;
1989 static const/*expr*/ result_type tempering_b = __b;
1990 static const/*expr*/ size_t tempering_t = __t;
1991 static const/*expr*/ result_type tempering_c = __c;
1992 static const/*expr*/ size_t tempering_l = __l;
1993 static const/*expr*/ result_type initialization_multiplier = __f;
1994 static const/*expr*/ result_type min() { return _Min; }
1995 static const/*expr*/ result_type max() { return _Max; }
1996 static const/*expr*/ result_type default_seed = 5489u;
1997
1998 // constructors and seeding functions
1999 explicit mersenne_twister_engine(result_type __sd = default_seed)
2000 {seed(__sd);}
2001 template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q)
2002 {seed(__q);}
2003 void seed(result_type __sd = default_seed);
2004 template<class _Sseq>
2005 typename enable_if
2006 <
2007 !is_convertible<_Sseq, result_type>::value,
2008 void
2009 >::type
2010 seed(_Sseq& __q)
2011 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2012
2013 // generating functions
2014 result_type operator()();
2015 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2016
2017 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2018 _UI _A, size_t _U, _UI _D, size_t _S,
2019 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2020 friend
2021 bool
2022 operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2023 _B, _T, _C, _L, _F>& __x,
2024 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2025 _B, _T, _C, _L, _F>& __y);
2026
2027 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2028 _UI _A, size_t _U, _UI _D, size_t _S,
2029 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2030 friend
2031 bool
2032 operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2033 _B, _T, _C, _L, _F>& __x,
2034 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2035 _B, _T, _C, _L, _F>& __y);
2036
2037 template <class _CharT, class _Traits,
2038 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2039 _UI _A, size_t _U, _UI _D, size_t _S,
2040 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2041 friend
2042 basic_ostream<_CharT, _Traits>&
2043 operator<<(basic_ostream<_CharT, _Traits>& __os,
2044 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2045 _B, _T, _C, _L, _F>& __x);
2046
2047 template <class _CharT, class _Traits,
2048 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2049 _UI _A, size_t _U, _UI _D, size_t _S,
2050 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2051 friend
2052 basic_istream<_CharT, _Traits>&
2053 operator>>(basic_istream<_CharT, _Traits>& __is,
2054 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2055 _B, _T, _C, _L, _F>& __x);
2056private:
2057
2058 template<class _Sseq>
2059 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2060 template<class _Sseq>
2061 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2062
2063 template <size_t __count>
2064 static
2065 typename enable_if
2066 <
2067 __count < __w,
2068 result_type
2069 >::type
2070 __lshift(result_type __x) {return (__x << __count) & _Max;}
2071
2072 template <size_t __count>
2073 static
2074 typename enable_if
2075 <
2076 (__count >= __w),
2077 result_type
2078 >::type
2079 __lshift(result_type __x) {return result_type(0);}
2080
2081 template <size_t __count>
2082 static
2083 typename enable_if
2084 <
2085 __count < _Dt,
2086 result_type
2087 >::type
2088 __rshift(result_type __x) {return __x >> __count;}
2089
2090 template <size_t __count>
2091 static
2092 typename enable_if
2093 <
2094 (__count >= _Dt),
2095 result_type
2096 >::type
2097 __rshift(result_type __x) {return result_type(0);}
2098};
2099
2100template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2101 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2102 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2103void
2104mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2105 __t, __c, __l, __f>::seed(result_type __sd)
2106{ // __w >= 2
2107 __x_[0] = __sd & _Max;
2108 for (size_t __i = 1; __i < __n; ++__i)
2109 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2110 __i_ = 0;
2111}
2112
2113template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2114 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2115 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2116template<class _Sseq>
2117void
2118mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2119 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2120{
2121 const unsigned __k = 1;
2122 uint32_t __ar[__n * __k];
2123 __q.generate(__ar, __ar + __n * __k);
2124 for (size_t __i = 0; __i < __n; ++__i)
2125 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2126 const result_type __mask = __r == _Dt ? result_type(~0) :
2127 (result_type(1) << __r) - result_type(1);
2128 __i_ = 0;
2129 if ((__x_[0] & ~__mask) == 0)
2130 {
2131 for (size_t __i = 1; __i < __n; ++__i)
2132 if (__x_[__i] != 0)
2133 return;
2134 __x_[0] = _Max;
2135 }
2136}
2137
2138template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2139 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2140 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2141template<class _Sseq>
2142void
2143mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2144 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2145{
2146 const unsigned __k = 2;
2147 uint32_t __ar[__n * __k];
2148 __q.generate(__ar, __ar + __n * __k);
2149 for (size_t __i = 0; __i < __n; ++__i)
2150 __x_[__i] = static_cast<result_type>(
2151 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2152 const result_type __mask = __r == _Dt ? result_type(~0) :
2153 (result_type(1) << __r) - result_type(1);
2154 __i_ = 0;
2155 if ((__x_[0] & ~__mask) == 0)
2156 {
2157 for (size_t __i = 1; __i < __n; ++__i)
2158 if (__x_[__i] != 0)
2159 return;
2160 __x_[0] = _Max;
2161 }
2162}
2163
2164template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2165 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2166 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2167_UIntType
2168mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2169 __t, __c, __l, __f>::operator()()
2170{
2171 const size_t __j = (__i_ + 1) % __n;
2172 const result_type __mask = __r == _Dt ? result_type(~0) :
2173 (result_type(1) << __r) - result_type(1);
2174 const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
2175 const size_t __k = (__i_ + __m) % __n;
2176 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
2177 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2178 __i_ = __j;
2179 __z ^= __lshift<__s>(__z) & __b;
2180 __z ^= __lshift<__t>(__z) & __c;
2181 return __z ^ __rshift<__l>(__z);
2182}
2183
2184template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2185 _UI _A, size_t _U, _UI _D, size_t _S,
2186 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2187bool
2188operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2189 _B, _T, _C, _L, _F>& __x,
2190 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2191 _B, _T, _C, _L, _F>& __y)
2192{
2193 if (__x.__i_ == __y.__i_)
2194 return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
2195 if (__x.__i_ == 0 || __y.__i_ == 0)
2196 {
2197 size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
2198 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2199 __y.__x_ + __y.__i_))
2200 return false;
2201 if (__x.__i_ == 0)
2202 return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
2203 return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
2204 }
2205 if (__x.__i_ < __y.__i_)
2206 {
2207 size_t __j = _N - __y.__i_;
2208 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2209 __y.__x_ + __y.__i_))
2210 return false;
2211 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
2212 __y.__x_))
2213 return false;
2214 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
2215 __y.__x_ + (_N - (__x.__i_ + __j)));
2216 }
2217 size_t __j = _N - __x.__i_;
2218 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2219 __x.__x_ + __x.__i_))
2220 return false;
2221 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
2222 __x.__x_))
2223 return false;
2224 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
2225 __x.__x_ + (_N - (__y.__i_ + __j)));
2226}
2227
2228template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2229 _UI _A, size_t _U, _UI _D, size_t _S,
2230 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2231inline
2232bool
2233operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2234 _B, _T, _C, _L, _F>& __x,
2235 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2236 _B, _T, _C, _L, _F>& __y)
2237{
2238 return !(__x == __y);
2239}
2240
2241template <class _CharT, class _Traits,
2242 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2243 _UI _A, size_t _U, _UI _D, size_t _S,
2244 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2245basic_ostream<_CharT, _Traits>&
2246operator<<(basic_ostream<_CharT, _Traits>& __os,
2247 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2248 _B, _T, _C, _L, _F>& __x)
2249{
2250 __save_flags<_CharT, _Traits> _(__os);
2251 __os.flags(ios_base::dec | ios_base::left);
2252 _CharT __sp = __os.widen(' ');
2253 __os.fill(__sp);
2254 __os << __x.__x_[__x.__i_];
2255 for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
2256 __os << __sp << __x.__x_[__j];
2257 for (size_t __j = 0; __j < __x.__i_; ++__j)
2258 __os << __sp << __x.__x_[__j];
2259 return __os;
2260}
2261
2262template <class _CharT, class _Traits,
2263 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2264 _UI _A, size_t _U, _UI _D, size_t _S,
2265 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2266basic_istream<_CharT, _Traits>&
2267operator>>(basic_istream<_CharT, _Traits>& __is,
2268 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2269 _B, _T, _C, _L, _F>& __x)
2270{
2271 __save_flags<_CharT, _Traits> _(__is);
2272 __is.flags(ios_base::dec | ios_base::skipws);
2273 _UI __t[_N];
2274 for (size_t __i = 0; __i < _N; ++__i)
2275 __is >> __t[__i];
2276 if (!__is.fail())
2277 {
2278 for (size_t __i = 0; __i < _N; ++__i)
2279 __x.__x_[__i] = __t[__i];
2280 __x.__i_ = 0;
2281 }
2282 return __is;
2283}
2284
2285typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2286 0x9908b0df, 11, 0xffffffff,
2287 7, 0x9d2c5680,
2288 15, 0xefc60000,
2289 18, 1812433253> mt19937;
2290typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2291 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2292 17, 0x71d67fffeda60000ULL,
2293 37, 0xfff7eee000000000ULL,
2294 43, 6364136223846793005ULL> mt19937_64;
2295
2296// subtract_with_carry_engine
2297
2298template<class _UIntType, size_t __w, size_t __s, size_t __r>
2299class subtract_with_carry_engine;
2300
2301template<class _UI, size_t _W, size_t _S, size_t _R>
2302bool
2303operator==(
2304 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2305 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2306
2307template<class _UI, size_t _W, size_t _S, size_t _R>
2308bool
2309operator!=(
2310 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2311 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2312
2313template <class _CharT, class _Traits,
2314 class _UI, size_t _W, size_t _S, size_t _R>
2315basic_ostream<_CharT, _Traits>&
2316operator<<(basic_ostream<_CharT, _Traits>& __os,
2317 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2318
2319template <class _CharT, class _Traits,
2320 class _UI, size_t _W, size_t _S, size_t _R>
2321basic_istream<_CharT, _Traits>&
2322operator>>(basic_istream<_CharT, _Traits>& __is,
2323 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2324
2325template<class _UIntType, size_t __w, size_t __s, size_t __r>
2326class subtract_with_carry_engine
2327{
2328public:
2329 // types
2330 typedef _UIntType result_type;
2331
2332private:
2333 result_type __x_[__r];
2334 result_type __c_;
2335 size_t __i_;
2336
2337 static const result_type _Dt = numeric_limits<result_type>::digits;
2338 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2339 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2340 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2341 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2342public:
2343 static const result_type _Min = 0;
2344 static const result_type _Max = __w == _Dt ? result_type(~0) :
2345 (result_type(1) << __w) - result_type(1);
2346 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2347
2348 // engine characteristics
2349 static const/*expr*/ size_t word_size = __w;
2350 static const/*expr*/ size_t short_lag = __s;
2351 static const/*expr*/ size_t long_lag = __r;
2352 static const/*expr*/ result_type min() { return _Min; }
2353 static const/*expr*/ result_type max() { return _Max; }
2354 static const/*expr*/ result_type default_seed = 19780503u;
2355
2356 // constructors and seeding functions
2357 explicit subtract_with_carry_engine(result_type __sd = default_seed)
2358 {seed(__sd);}
2359 template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q)
2360 {seed(__q);}
2361 void seed(result_type __sd = default_seed)
2362 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2363 template<class _Sseq>
2364 typename enable_if
2365 <
2366 !is_convertible<_Sseq, result_type>::value,
2367 void
2368 >::type
2369 seed(_Sseq& __q)
2370 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2371
2372 // generating functions
2373 result_type operator()();
2374 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2375
2376 template<class _UI, size_t _W, size_t _S, size_t _R>
2377 friend
2378 bool
2379 operator==(
2380 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2381 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2382
2383 template<class _UI, size_t _W, size_t _S, size_t _R>
2384 friend
2385 bool
2386 operator!=(
2387 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2388 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2389
2390 template <class _CharT, class _Traits,
2391 class _UI, size_t _W, size_t _S, size_t _R>
2392 friend
2393 basic_ostream<_CharT, _Traits>&
2394 operator<<(basic_ostream<_CharT, _Traits>& __os,
2395 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2396
2397 template <class _CharT, class _Traits,
2398 class _UI, size_t _W, size_t _S, size_t _R>
2399 friend
2400 basic_istream<_CharT, _Traits>&
2401 operator>>(basic_istream<_CharT, _Traits>& __is,
2402 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2403
2404private:
2405
2406 void seed(result_type __sd, integral_constant<unsigned, 1>);
2407 void seed(result_type __sd, integral_constant<unsigned, 2>);
2408 template<class _Sseq>
2409 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2410 template<class _Sseq>
2411 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2412};
2413
2414template<class _UIntType, size_t __w, size_t __s, size_t __r>
2415void
2416subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2417 integral_constant<unsigned, 1>)
2418{
2419 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2420 __e(__sd == 0u ? default_seed : __sd);
2421 for (size_t __i = 0; __i < __r; ++__i)
2422 __x_[__i] = static_cast<result_type>(__e() & _Max);
2423 __c_ = __x_[__r-1] == 0;
2424 __i_ = 0;
2425}
2426
2427template<class _UIntType, size_t __w, size_t __s, size_t __r>
2428void
2429subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2430 integral_constant<unsigned, 2>)
2431{
2432 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2433 __e(__sd == 0u ? default_seed : __sd);
2434 for (size_t __i = 0; __i < __r; ++__i)
2435 __x_[__i] = static_cast<result_type>(
2436 (__e() + ((uint64_t)__e() << 32)) & _Max);
2437 __c_ = __x_[__r-1] == 0;
2438 __i_ = 0;
2439}
2440
2441template<class _UIntType, size_t __w, size_t __s, size_t __r>
2442template<class _Sseq>
2443void
2444subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2445 integral_constant<unsigned, 1>)
2446{
2447 const unsigned __k = 1;
2448 uint32_t __ar[__r * __k];
2449 __q.generate(__ar, __ar + __r * __k);
2450 for (size_t __i = 0; __i < __r; ++__i)
2451 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2452 __c_ = __x_[__r-1] == 0;
2453 __i_ = 0;
2454}
2455
2456template<class _UIntType, size_t __w, size_t __s, size_t __r>
2457template<class _Sseq>
2458void
2459subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2460 integral_constant<unsigned, 2>)
2461{
2462 const unsigned __k = 2;
2463 uint32_t __ar[__r * __k];
2464 __q.generate(__ar, __ar + __r * __k);
2465 for (size_t __i = 0; __i < __r; ++__i)
2466 __x_[__i] = static_cast<result_type>(
2467 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2468 __c_ = __x_[__r-1] == 0;
2469 __i_ = 0;
2470}
2471
2472template<class _UIntType, size_t __w, size_t __s, size_t __r>
2473_UIntType
2474subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2475{
2476 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2477 result_type& __xr = __x_[__i_];
2478 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2479 __xr = (__xs - __xr - __c_) & _Max;
2480 __c_ = __new_c;
2481 __i_ = (__i_ + 1) % __r;
2482 return __xr;
2483}
2484
2485template<class _UI, size_t _W, size_t _S, size_t _R>
2486bool
2487operator==(
2488 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2489 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2490{
2491 if (__x.__c_ != __y.__c_)
2492 return false;
2493 if (__x.__i_ == __y.__i_)
2494 return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
2495 if (__x.__i_ == 0 || __y.__i_ == 0)
2496 {
2497 size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
2498 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2499 __y.__x_ + __y.__i_))
2500 return false;
2501 if (__x.__i_ == 0)
2502 return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
2503 return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
2504 }
2505 if (__x.__i_ < __y.__i_)
2506 {
2507 size_t __j = _R - __y.__i_;
2508 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2509 __y.__x_ + __y.__i_))
2510 return false;
2511 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
2512 __y.__x_))
2513 return false;
2514 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
2515 __y.__x_ + (_R - (__x.__i_ + __j)));
2516 }
2517 size_t __j = _R - __x.__i_;
2518 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2519 __x.__x_ + __x.__i_))
2520 return false;
2521 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
2522 __x.__x_))
2523 return false;
2524 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
2525 __x.__x_ + (_R - (__y.__i_ + __j)));
2526}
2527
2528template<class _UI, size_t _W, size_t _S, size_t _R>
2529inline
2530bool
2531operator!=(
2532 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2533 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2534{
2535 return !(__x == __y);
2536}
2537
2538template <class _CharT, class _Traits,
2539 class _UI, size_t _W, size_t _S, size_t _R>
2540basic_ostream<_CharT, _Traits>&
2541operator<<(basic_ostream<_CharT, _Traits>& __os,
2542 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2543{
2544 __save_flags<_CharT, _Traits> _(__os);
2545 __os.flags(ios_base::dec | ios_base::left);
2546 _CharT __sp = __os.widen(' ');
2547 __os.fill(__sp);
2548 __os << __x.__x_[__x.__i_];
2549 for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
2550 __os << __sp << __x.__x_[__j];
2551 for (size_t __j = 0; __j < __x.__i_; ++__j)
2552 __os << __sp << __x.__x_[__j];
2553 __os << __sp << __x.__c_;
2554 return __os;
2555}
2556
2557template <class _CharT, class _Traits,
2558 class _UI, size_t _W, size_t _S, size_t _R>
2559basic_istream<_CharT, _Traits>&
2560operator>>(basic_istream<_CharT, _Traits>& __is,
2561 subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2562{
2563 __save_flags<_CharT, _Traits> _(__is);
2564 __is.flags(ios_base::dec | ios_base::skipws);
2565 _UI __t[_R+1];
2566 for (size_t __i = 0; __i < _R+1; ++__i)
2567 __is >> __t[__i];
2568 if (!__is.fail())
2569 {
2570 for (size_t __i = 0; __i < _R; ++__i)
2571 __x.__x_[__i] = __t[__i];
2572 __x.__c_ = __t[_R];
2573 __x.__i_ = 0;
2574 }
2575 return __is;
2576}
2577
2578typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2579typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2580
2581// discard_block_engine
2582
2583template<class _Engine, size_t __p, size_t __r>
2584class discard_block_engine
2585{
2586 _Engine __e_;
2587 int __n_;
2588
2589 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2590 static_assert(__r <= __p, "discard_block_engine invalid parameters");
2591public:
2592 // types
2593 typedef typename _Engine::result_type result_type;
2594
2595 // engine characteristics
2596 static const/*expr*/ size_t block_size = __p;
2597 static const/*expr*/ size_t used_block = __r;
2598
2599 // Temporary work around for lack of constexpr
2600 static const result_type _Min = _Engine::_Min;
2601 static const result_type _Max = _Engine::_Max;
2602
2603 static const/*expr*/ result_type min() { return _Engine::min(); }
2604 static const/*expr*/ result_type max() { return _Engine::max(); }
2605
2606 // constructors and seeding functions
2607 discard_block_engine() : __n_(0) {}
2608// explicit discard_block_engine(const _Engine& __e);
2609// explicit discard_block_engine(_Engine&& __e);
2610 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
2611 template<class _Sseq> explicit discard_block_engine(_Sseq& __q)
2612 : __e_(__q), __n_(0) {}
2613 void seed() {__e_.seed(); __n_ = 0;}
2614 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
2615 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
2616
2617 // generating functions
2618 result_type operator()();
2619 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2620
2621 // property functions
2622 const _Engine& base() const {return __e_;}
2623
2624 template<class _Eng, size_t _P, size_t _R>
2625 friend
2626 bool
2627 operator==(
2628 const discard_block_engine<_Eng, _P, _R>& __x,
2629 const discard_block_engine<_Eng, _P, _R>& __y);
2630
2631 template<class _Eng, size_t _P, size_t _R>
2632 friend
2633 bool
2634 operator!=(
2635 const discard_block_engine<_Eng, _P, _R>& __x,
2636 const discard_block_engine<_Eng, _P, _R>& __y);
2637
2638 template <class _CharT, class _Traits,
2639 class _Eng, size_t _P, size_t _R>
2640 friend
2641 basic_ostream<_CharT, _Traits>&
2642 operator<<(basic_ostream<_CharT, _Traits>& __os,
2643 const discard_block_engine<_Eng, _P, _R>& __x);
2644
2645 template <class _CharT, class _Traits,
2646 class _Eng, size_t _P, size_t _R>
2647 friend
2648 basic_istream<_CharT, _Traits>&
2649 operator>>(basic_istream<_CharT, _Traits>& __is,
2650 discard_block_engine<_Eng, _P, _R>& __x);
2651};
2652
2653template<class _Engine, size_t __p, size_t __r>
2654typename discard_block_engine<_Engine, __p, __r>::result_type
2655discard_block_engine<_Engine, __p, __r>::operator()()
2656{
2657 if (__n_ >= __r)
2658 {
2659 __e_.discard(__p - __r);
2660 __n_ = 0;
2661 }
2662 ++__n_;
2663 return __e_();
2664}
2665
2666template<class _Eng, size_t _P, size_t _R>
2667inline
2668bool
2669operator==(const discard_block_engine<_Eng, _P, _R>& __x,
2670 const discard_block_engine<_Eng, _P, _R>& __y)
2671{
2672 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2673}
2674
2675template<class _Eng, size_t _P, size_t _R>
2676inline
2677bool
2678operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
2679 const discard_block_engine<_Eng, _P, _R>& __y)
2680{
2681 return !(__x == __y);
2682}
2683
2684template <class _CharT, class _Traits,
2685 class _Eng, size_t _P, size_t _R>
2686basic_ostream<_CharT, _Traits>&
2687operator<<(basic_ostream<_CharT, _Traits>& __os,
2688 const discard_block_engine<_Eng, _P, _R>& __x)
2689{
2690 __save_flags<_CharT, _Traits> _(__os);
2691 __os.flags(ios_base::dec | ios_base::left);
2692 _CharT __sp = __os.widen(' ');
2693 __os.fill(__sp);
2694 return __os << __x.__e_ << __sp << __x.__n_;
2695}
2696
2697template <class _CharT, class _Traits,
2698 class _Eng, size_t _P, size_t _R>
2699basic_istream<_CharT, _Traits>&
2700operator>>(basic_istream<_CharT, _Traits>& __is,
2701 discard_block_engine<_Eng, _P, _R>& __x)
2702{
2703 __save_flags<_CharT, _Traits> _(__is);
2704 __is.flags(ios_base::dec | ios_base::skipws);
2705 _Eng __e;
2706 int __n;
2707 __is >> __e >> __n;
2708 if (!__is.fail())
2709 {
2710 __x.__e_ = __e;
2711 __x.__n_ = __n;
2712 }
2713 return __is;
2714}
2715
2716typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2717typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2718
2719// independent_bits_engine
2720
2721template <unsigned long long _X, size_t _R>
2722struct __log2_imp
2723{
2724 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
2725 : __log2_imp<_X, _R - 1>::value;
2726};
2727
2728template <unsigned long long _X>
2729struct __log2_imp<_X, 0>
2730{
2731 static const size_t value = 0;
2732};
2733
2734template <size_t _R>
2735struct __log2_imp<0, _R>
2736{
2737 static const size_t value = _R + 1;
2738};
2739
2740template <class _UI, _UI _X>
2741struct __log2
2742{
2743 static const size_t value = __log2_imp<_X,
2744 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2745};
2746
2747template<class _Engine, size_t __w, class _UIntType>
2748class independent_bits_engine
2749{
2750 template <class _UI, _UI _R0, size_t _W, size_t _M>
2751 class __get_n
2752 {
2753 static const size_t _Dt = numeric_limits<_UI>::digits;
2754 static const size_t _N = _W / _M + (_W % _M != 0);
2755 static const size_t _W0 = _W / _N;
2756 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
2757 public:
2758 static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
2759 };
2760public:
2761 // types
2762 typedef _UIntType result_type;
2763
2764private:
2765 _Engine __e_;
2766
2767 static const result_type _Dt = numeric_limits<result_type>::digits;
2768 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2769 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2770
2771 typedef typename _Engine::result_type _Engine_result_type;
2772 typedef typename conditional
2773 <
2774 sizeof(_Engine_result_type) <= sizeof(result_type),
2775 result_type,
2776 _Engine_result_type
2777 >::type _Working_result_type;
2778 // Temporary work around for lack of constexpr
2779 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2780 + _Working_result_type(1);
2781 static const size_t __m = __log2<_Working_result_type, _R>::value;
2782 static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
2783 static const size_t __w0 = __w / __n;
2784 static const size_t __n0 = __n - __w % __n;
2785 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2786 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2787 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2788 (_R >> __w0) << __w0;
2789 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2790 (_R >> (__w0+1)) << (__w0+1);
2791 static const _Engine_result_type __mask0 = __w0 > 0 ?
2792 _Engine_result_type(~0) >> (_EDt - __w0) :
2793 _Engine_result_type(0);
2794 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2795 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2796 _Engine_result_type(~0);
2797public:
2798 static const result_type _Min = 0;
2799 static const result_type _Max = __w == _Dt ? result_type(~0) :
2800 (result_type(1) << __w) - result_type(1);
2801 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2802
2803 // engine characteristics
2804 static const/*expr*/ result_type min() { return _Min; }
2805 static const/*expr*/ result_type max() { return _Max; }
2806
2807 // constructors and seeding functions
2808 independent_bits_engine() {}
2809// explicit independent_bits_engine(const _Engine& __e);
2810// explicit independent_bits_engine(_Engine&& __e);
2811 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
2812 template<class _Sseq> explicit independent_bits_engine(_Sseq& __q)
2813 : __e_(__q) {}
2814 void seed() {__e_.seed();}
2815 void seed(result_type __sd) {__e_.seed(__sd);}
2816 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);}
2817
2818 // generating functions
2819 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2820 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2821
2822 // property functions
2823 const _Engine& base() const {return __e_;}
2824
2825 template<class _Eng, size_t _W, class _UI>
2826 friend
2827 bool
2828 operator==(
2829 const independent_bits_engine<_Eng, _W, _UI>& __x,
2830 const independent_bits_engine<_Eng, _W, _UI>& __y);
2831
2832 template<class _Eng, size_t _W, class _UI>
2833 friend
2834 bool
2835 operator!=(
2836 const independent_bits_engine<_Eng, _W, _UI>& __x,
2837 const independent_bits_engine<_Eng, _W, _UI>& __y);
2838
2839 template <class _CharT, class _Traits,
2840 class _Eng, size_t _W, class _UI>
2841 friend
2842 basic_ostream<_CharT, _Traits>&
2843 operator<<(basic_ostream<_CharT, _Traits>& __os,
2844 const independent_bits_engine<_Eng, _W, _UI>& __x);
2845
2846 template <class _CharT, class _Traits,
2847 class _Eng, size_t _W, class _UI>
2848 friend
2849 basic_istream<_CharT, _Traits>&
2850 operator>>(basic_istream<_CharT, _Traits>& __is,
2851 independent_bits_engine<_Eng, _W, _UI>& __x);
2852
2853private:
2854 result_type __eval(false_type);
2855 result_type __eval(true_type);
2856
2857 template <size_t __count>
2858 static
2859 typename enable_if
2860 <
2861 __count < _Dt,
2862 result_type
2863 >::type
2864 __lshift(result_type __x) {return __x << __count;}
2865
2866 template <size_t __count>
2867 static
2868 typename enable_if
2869 <
2870 (__count >= _Dt),
2871 result_type
2872 >::type
2873 __lshift(result_type __x) {return result_type(0);}
2874};
2875
2876template<class _Engine, size_t __w, class _UIntType>
2877inline
2878_UIntType
2879independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
2880{
2881 return static_cast<result_type>(__e_() & __mask0);
2882}
2883
2884template<class _Engine, size_t __w, class _UIntType>
2885_UIntType
2886independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
2887{
2888 result_type _S = 0;
2889 for (size_t __k = 0; __k < __n0; ++__k)
2890 {
2891 _Engine_result_type __u;
2892 do
2893 {
2894 __u = __e_() - _Engine::min();
2895 } while (__u >= __y0);
2896 _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
2897 }
2898 for (size_t __k = __n0; __k < __n; ++__k)
2899 {
2900 _Engine_result_type __u;
2901 do
2902 {
2903 __u = __e_() - _Engine::min();
2904 } while (__u >= __y1);
2905 _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
2906 }
2907 return _S;
2908}
2909
2910template<class _Eng, size_t _W, class _UI>
2911inline
2912bool
2913operator==(
2914 const independent_bits_engine<_Eng, _W, _UI>& __x,
2915 const independent_bits_engine<_Eng, _W, _UI>& __y)
2916{
2917 return __x.base() == __y.base();
2918}
2919
2920template<class _Eng, size_t _W, class _UI>
2921inline
2922bool
2923operator!=(
2924 const independent_bits_engine<_Eng, _W, _UI>& __x,
2925 const independent_bits_engine<_Eng, _W, _UI>& __y)
2926{
2927 return !(__x == __y);
2928}
2929
2930template <class _CharT, class _Traits,
2931 class _Eng, size_t _W, class _UI>
2932basic_ostream<_CharT, _Traits>&
2933operator<<(basic_ostream<_CharT, _Traits>& __os,
2934 const independent_bits_engine<_Eng, _W, _UI>& __x)
2935{
2936 return __os << __x.base();
2937}
2938
2939template <class _CharT, class _Traits,
2940 class _Eng, size_t _W, class _UI>
2941basic_istream<_CharT, _Traits>&
2942operator>>(basic_istream<_CharT, _Traits>& __is,
2943 independent_bits_engine<_Eng, _W, _UI>& __x)
2944{
2945 _Eng __e;
2946 __is >> __e;
2947 if (!__is.fail())
2948 __x.__e_ = __e;
2949 return __is;
2950}
2951
2952// shuffle_order_engine
2953
2954template <uint64_t _Xp, uint64_t _Yp>
2955struct __ugcd
2956{
2957 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
2958};
2959
2960template <uint64_t _Xp>
2961struct __ugcd<_Xp, 0>
2962{
2963 static const uint64_t value = _Xp;
2964};
2965
2966template <uint64_t _N, uint64_t _D>
2967class __uratio
2968{
2969 static_assert(_D != 0, "__uratio divide by 0");
2970 static const uint64_t __gcd = __ugcd<_N, _D>::value;
2971public:
2972 static const uint64_t num = _N / __gcd;
2973 static const uint64_t den = _D / __gcd;
2974
2975 typedef __uratio<num, den> type;
2976};
2977
2978template<class _Engine, size_t __k>
2979class shuffle_order_engine
2980{
2981 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
2982public:
2983 // types
2984 typedef typename _Engine::result_type result_type;
2985
2986private:
2987 _Engine __e_;
2988 result_type _V_[__k];
2989 result_type _Y_;
2990
2991public:
2992 // engine characteristics
2993 static const/*expr*/ size_t table_size = __k;
2994
2995 static const result_type _Min = _Engine::_Min;
2996 static const result_type _Max = _Engine::_Max;
2997 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
2998 static const/*expr*/ result_type min() { return _Min; }
2999 static const/*expr*/ result_type max() { return _Max; }
3000
3001 static const unsigned long long _R = _Max - _Min + 1ull;
3002
3003 // constructors and seeding functions
3004 shuffle_order_engine() {__init();}
3005// explicit shuffle_order_engine(const _Engine& __e);
3006// explicit shuffle_order_engine(_Engine&& e);
3007 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
3008 template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q)
3009 : __e_(__q) {__init();}
3010 void seed() {__e_.seed(); __init();}
3011 void seed(result_type __sd) {__e_.seed(__sd); __init();}
3012 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();}
3013
3014 // generating functions
3015 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
3016 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3017
3018 // property functions
3019 const _Engine& base() const {return __e_;}
3020
3021private:
3022 template<class _Eng, size_t _K>
3023 friend
3024 bool
3025 operator==(
3026 const shuffle_order_engine<_Eng, _K>& __x,
3027 const shuffle_order_engine<_Eng, _K>& __y);
3028
3029 template<class _Eng, size_t _K>
3030 friend
3031 bool
3032 operator!=(
3033 const shuffle_order_engine<_Eng, _K>& __x,
3034 const shuffle_order_engine<_Eng, _K>& __y);
3035
3036 template <class _CharT, class _Traits,
3037 class _Eng, size_t _K>
3038 friend
3039 basic_ostream<_CharT, _Traits>&
3040 operator<<(basic_ostream<_CharT, _Traits>& __os,
3041 const shuffle_order_engine<_Eng, _K>& __x);
3042
3043 template <class _CharT, class _Traits,
3044 class _Eng, size_t _K>
3045 friend
3046 basic_istream<_CharT, _Traits>&
3047 operator>>(basic_istream<_CharT, _Traits>& __is,
3048 shuffle_order_engine<_Eng, _K>& __x);
3049
3050 void __init()
3051 {
3052 for (size_t __i = 0; __i < __k; ++__i)
3053 _V_[__i] = __e_();
3054 _Y_ = __e_();
3055 }
3056
3057 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
3058 result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
3059
3060 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
3061 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3062
3063 template <uint64_t _N, uint64_t _D>
3064 typename enable_if
3065 <
3066 (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
3067 result_type
3068 >::type
3069 __eval(__uratio<_N, _D>)
3070 {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
3071
3072 template <uint64_t _N, uint64_t _D>
3073 typename enable_if
3074 <
3075 __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
3076 result_type
3077 >::type
3078 __eval(__uratio<_N, _D>)
3079 {
3080 const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
3081 / __uratio<_N, _D>::den);
3082 _Y_ = _V_[__j];
3083 _V_[__j] = __e_();
3084 return _Y_;
3085 }
3086
3087 template <uint64_t __n, uint64_t __d>
3088 result_type __evalf()
3089 {
3090 const double _F = __d == 0 ?
3091 __n / (2. * 0x8000000000000000ull) :
3092 __n / (double)__d;
3093 const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
3094 _Y_ = _V_[__j];
3095 _V_[__j] = __e_();
3096 return _Y_;
3097 }
3098};
3099
3100template<class _Eng, size_t _K>
3101bool
3102operator==(
3103 const shuffle_order_engine<_Eng, _K>& __x,
3104 const shuffle_order_engine<_Eng, _K>& __y)
3105{
3106 return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
3107 __x.__e_ == __y.__e_;
3108}
3109
3110template<class _Eng, size_t _K>
3111inline
3112bool
3113operator!=(
3114 const shuffle_order_engine<_Eng, _K>& __x,
3115 const shuffle_order_engine<_Eng, _K>& __y)
3116{
3117 return !(__x == __y);
3118}
3119
3120template <class _CharT, class _Traits,
3121 class _Eng, size_t _K>
3122basic_ostream<_CharT, _Traits>&
3123operator<<(basic_ostream<_CharT, _Traits>& __os,
3124 const shuffle_order_engine<_Eng, _K>& __x)
3125{
3126 __save_flags<_CharT, _Traits> _(__os);
3127 __os.flags(ios_base::dec | ios_base::left);
3128 _CharT __sp = __os.widen(' ');
3129 __os.fill(__sp);
3130 __os << __x.__e_ << __sp << __x._V_[0];
3131 for (size_t __i = 1; __i < _K; ++__i)
3132 __os << __sp << __x._V_[__i];
3133 return __os << __sp << __x._Y_;
3134}
3135
3136template <class _CharT, class _Traits,
3137 class _Eng, size_t _K>
3138basic_istream<_CharT, _Traits>&
3139operator>>(basic_istream<_CharT, _Traits>& __is,
3140 shuffle_order_engine<_Eng, _K>& __x)
3141{
3142 typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
3143 __save_flags<_CharT, _Traits> _(__is);
3144 __is.flags(ios_base::dec | ios_base::skipws);
3145 _Eng __e;
3146 result_type _V[_K+1];
3147 __is >> __e;
3148 for (size_t __i = 0; __i < _K+1; ++__i)
3149 __is >> _V[__i];
3150 if (!__is.fail())
3151 {
3152 __x.__e_ = __e;
3153 for (size_t __i = 0; __i < _K; ++__i)
3154 __x._V_[__i] = _V[__i];
3155 __x._Y_ = _V[_K];
3156 }
3157 return __is;
3158}
3159
3160typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3161
3162// random_device
3163
3164class random_device
3165{
3166 int __f_;
3167public:
3168 // types
3169 typedef unsigned result_type;
3170
3171 // generator characteristics
3172 static const result_type _Min = 0;
3173 static const result_type _Max = 0xFFFFFFFFu;
3174
3175 static const/*expr*/ result_type min() { return _Min;}
3176 static const/*expr*/ result_type max() { return _Max;}
3177
3178 // constructors
3179 explicit random_device(const string& __token = "/dev/urandom");
3180 ~random_device();
3181
3182 // generating functions
3183 result_type operator()();
3184
3185 // property functions
3186 double entropy() const;
3187
3188private:
3189 // no copy functions
3190 random_device(const random_device&); // = delete;
3191 random_device& operator=(const random_device&); // = delete;
3192};
3193
3194// seed_seq
3195
3196class seed_seq
3197{
3198public:
3199 // types
3200 typedef uint32_t result_type;
3201
3202private:
3203 vector<result_type> __v_;
3204
3205 template<class _InputIterator>
3206 void init(_InputIterator __first, _InputIterator __last);
3207public:
3208 // constructors
3209 seed_seq() {}
3210 template<class _Tp>
3211 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
3212
3213 template<class _InputIterator>
3214 seed_seq(_InputIterator __first, _InputIterator __last)
3215 {init(__first, __last);}
3216
3217 // generating functions
3218 template<class _RandomAccessIterator>
3219 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3220
3221 // property functions
3222 size_t size() const {return __v_.size();}
3223 template<class _OutputIterator>
3224 void param(_OutputIterator __dest) const
3225 {_STD::copy(__v_.begin(), __v_.end(), __dest);}
3226
3227private:
3228 // no copy functions
3229 seed_seq(const seed_seq&); // = delete;
3230 void operator=(const seed_seq&); // = delete;
3231
3232 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
3233};
3234
3235template<class _InputIterator>
3236void
3237seed_seq::init(_InputIterator __first, _InputIterator __last)
3238{
3239 for (_InputIterator __s = __first; __s != __last; ++__s)
3240 __v_.push_back(*__s & 0xFFFFFFFF);
3241}
3242
3243template<class _RandomAccessIterator>
3244void
3245seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3246{
3247 if (__first != __last)
3248 {
3249 _STD::fill(__first, __last, 0x8b8b8b8b);
3250 const size_t __n = static_cast<size_t>(__last - __first);
3251 const size_t __s = __v_.size();
3252 const size_t __t = (__n >= 623) ? 11
3253 : (__n >= 68) ? 7
3254 : (__n >= 39) ? 5
3255 : (__n >= 7) ? 3
3256 : (__n - 1) / 2;
3257 const size_t __p = (__n - __t) / 2;
3258 const size_t __q = __p + __t;
3259 const size_t __m = _STD::max(__s + 1, __n);
3260 // __k = 0;
3261 {
3262 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
3263 ^ __first[__n - 1]);
3264 __first[__p] += __r;
3265 __r += __s;
3266 __first[__q] += __r;
3267 __first[0] = __r;
3268 }
3269 for (size_t __k = 1; __k <= __s; ++__k)
3270 {
3271 const size_t __kmodn = __k % __n;
3272 const size_t __kpmodn = (__k + __p) % __n;
3273 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3274 ^ __first[(__k - 1) % __n]);
3275 __first[__kpmodn] += __r;
3276 __r += __kmodn + __v_[__k-1];
3277 __first[(__k + __q) % __n] += __r;
3278 __first[__kmodn] = __r;
3279 }
3280 for (size_t __k = __s + 1; __k < __m; ++__k)
3281 {
3282 const size_t __kmodn = __k % __n;
3283 const size_t __kpmodn = (__k + __p) % __n;
3284 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3285 ^ __first[(__k - 1) % __n]);
3286 __first[__kpmodn] += __r;
3287 __r += __kmodn;
3288 __first[(__k + __q) % __n] += __r;
3289 __first[__kmodn] = __r;
3290 }
3291 for (size_t __k = __m; __k < __m + __n; ++__k)
3292 {
3293 const size_t __kmodn = __k % __n;
3294 const size_t __kpmodn = (__k + __p) % __n;
3295 result_type __r = 1566083941 * _T(__first[__kmodn] +
3296 __first[__kpmodn] +
3297 __first[(__k - 1) % __n]);
3298 __first[__kpmodn] ^= __r;
3299 __r -= __kmodn;
3300 __first[(__k + __q) % __n] ^= __r;
3301 __first[__kmodn] = __r;
3302 }
3303 }
3304}
3305
Howard Hinnant30a840f2010-05-12 17:08:57 +00003306// generate_canonical
3307
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003308template<class _RealType, size_t __bits, class _URNG>
3309_RealType
3310generate_canonical(_URNG& __g)
3311{
3312 const size_t _Dt = numeric_limits<_RealType>::digits;
3313 const size_t __b = _Dt < __bits ? _Dt : __bits;
3314 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
3315 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
3316 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
3317 _RealType __base = _R;
3318 _RealType _S = __g() - _URNG::_Min;
3319 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
3320 _S += (__g() - _URNG::_Min) * __base;
3321 return _S / __base;
3322}
3323
3324// __independent_bits_engine
3325
3326template<class _Engine, class _UIntType>
3327class __independent_bits_engine
3328{
3329public:
3330 // types
3331 typedef _UIntType result_type;
3332
3333private:
3334 typedef typename _Engine::result_type _Engine_result_type;
3335 typedef typename conditional
3336 <
3337 sizeof(_Engine_result_type) <= sizeof(result_type),
3338 result_type,
3339 _Engine_result_type
3340 >::type _Working_result_type;
3341
3342 _Engine& __e_;
3343 size_t __w_;
3344 size_t __w0_;
3345 size_t __n_;
3346 size_t __n0_;
3347 _Working_result_type __y0_;
3348 _Working_result_type __y1_;
3349 _Engine_result_type __mask0_;
3350 _Engine_result_type __mask1_;
3351
3352 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
3353 + _Working_result_type(1);
3354 static const size_t __m = __log2<_Working_result_type, _R>::value;
3355 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
3356 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
3357
3358public:
3359 // constructors and seeding functions
3360 __independent_bits_engine(_Engine& __e, size_t __w);
3361
3362 // generating functions
3363 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
3364
3365private:
3366 result_type __eval(false_type);
3367 result_type __eval(true_type);
3368};
3369
3370template<class _Engine, class _UIntType>
3371__independent_bits_engine<_Engine, _UIntType>
3372 ::__independent_bits_engine(_Engine& __e, size_t __w)
3373 : __e_(__e),
3374 __w_(__w)
3375{
3376 __n_ = __w_ / __m + (__w_ % __m != 0);
3377 __w0_ = __w_ / __n_;
3378 if (_R == 0)
3379 __y0_ = _R;
3380 else if (__w0_ < _WDt)
3381 __y0_ = (_R >> __w0_) << __w0_;
3382 else
3383 __y0_ = 0;
3384 if (_R - __y0_ > __y0_ / __n_)
3385 {
3386 ++__n_;
3387 __w0_ = __w_ / __n_;
3388 if (__w0_ < _WDt)
3389 __y0_ = (_R >> __w0_) << __w0_;
3390 else
3391 __y0_ = 0;
3392 }
3393 __n0_ = __n_ - __w_ % __n_;
3394 if (__w0_ < _WDt - 1)
3395 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
3396 else
3397 __y1_ = 0;
3398 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
3399 _Engine_result_type(0);
3400 __mask1_ = __w0_ < _EDt - 1 ?
3401 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
3402 _Engine_result_type(~0);
3403}
3404
3405template<class _Engine, class _UIntType>
3406inline
3407_UIntType
3408__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
3409{
3410 return static_cast<result_type>(__e_() & __mask0_);
3411}
3412
3413template<class _Engine, class _UIntType>
3414_UIntType
3415__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
3416{
3417 result_type _S = 0;
3418 for (size_t __k = 0; __k < __n0_; ++__k)
3419 {
3420 _Engine_result_type __u;
3421 do
3422 {
3423 __u = __e_() - _Engine::min();
3424 } while (__u >= __y0_);
3425 if (__w0_ < _EDt)
3426 _S <<= __w0_;
3427 else
3428 _S = 0;
3429 _S += __u & __mask0_;
3430 }
3431 for (size_t __k = __n0_; __k < __n_; ++__k)
3432 {
3433 _Engine_result_type __u;
3434 do
3435 {
3436 __u = __e_() - _Engine::min();
3437 } while (__u >= __y1_);
3438 if (__w0_ < _EDt - 1)
3439 _S <<= __w0_ + 1;
3440 else
3441 _S = 0;
3442 _S += __u & __mask1_;
3443 }
3444 return _S;
3445}
3446
Howard Hinnant30a840f2010-05-12 17:08:57 +00003447// uniform_int_distribution
3448
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003449template<class _IntType = int>
3450class uniform_int_distribution
3451{
3452public:
3453 // types
3454 typedef _IntType result_type;
3455
3456 class param_type
3457 {
3458 result_type __a_;
3459 result_type __b_;
3460 public:
3461 typedef uniform_int_distribution distribution_type;
3462
3463 explicit param_type(result_type __a = 0,
3464 result_type __b = numeric_limits<result_type>::max())
3465 : __a_(__a), __b_(__b) {}
3466
3467 result_type a() const {return __a_;}
3468 result_type b() const {return __b_;}
3469
3470 friend bool operator==(const param_type& __x, const param_type& __y)
3471 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3472 friend bool operator!=(const param_type& __x, const param_type& __y)
3473 {return !(__x == __y);}
3474 };
3475
3476private:
3477 param_type __p_;
3478
3479public:
3480 // constructors and reset functions
3481 explicit uniform_int_distribution(result_type __a = 0,
3482 result_type __b = numeric_limits<result_type>::max())
3483 : __p_(param_type(__a, __b)) {}
3484 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3485 void reset() {}
3486
3487 // generating functions
3488 template<class _URNG> result_type operator()(_URNG& __g)
3489 {return (*this)(__g, __p_);}
3490 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3491
3492 // property functions
3493 result_type a() const {return __p_.a();}
3494 result_type b() const {return __p_.b();}
3495
3496 param_type param() const {return __p_;}
3497 void param(const param_type& __p) {__p_ = __p;}
3498
3499 result_type min() const {return a();}
3500 result_type max() const {return b();}
3501
3502 friend bool operator==(const uniform_int_distribution& __x,
3503 const uniform_int_distribution& __y)
3504 {return __x.__p_ == __y.__p_;}
3505 friend bool operator!=(const uniform_int_distribution& __x,
3506 const uniform_int_distribution& __y)
3507 {return !(__x == __y);}
3508};
3509
3510template<class _IntType>
3511template<class _URNG>
3512typename uniform_int_distribution<_IntType>::result_type
3513uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3514{
3515 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3516 uint32_t, uint64_t>::type _UIntType;
3517 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
3518 if (_R == 1)
3519 return __p.a();
3520 const size_t _Dt = numeric_limits<_UIntType>::digits;
3521 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3522 if (_R == 0)
3523 return static_cast<result_type>(_Eng(__g, _Dt)());
3524 size_t __w = _Dt - __clz(_R) - 1;
3525 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
3526 ++__w;
3527 _Eng __e(__g, __w);
3528 _UIntType __u;
3529 do
3530 {
3531 __u = __e();
3532 } while (__u >= _R);
3533 return static_cast<result_type>(__u + __p.a());
3534}
3535
3536template <class _CharT, class _Traits, class _IT>
3537basic_ostream<_CharT, _Traits>&
3538operator<<(basic_ostream<_CharT, _Traits>& __os,
3539 const uniform_int_distribution<_IT>& __x)
3540{
3541 __save_flags<_CharT, _Traits> _(__os);
3542 __os.flags(ios_base::dec | ios_base::left);
3543 _CharT __sp = __os.widen(' ');
3544 __os.fill(__sp);
3545 return __os << __x.a() << __sp << __x.b();
3546}
3547
3548template <class _CharT, class _Traits, class _IT>
3549basic_istream<_CharT, _Traits>&
3550operator>>(basic_istream<_CharT, _Traits>& __is,
3551 uniform_int_distribution<_IT>& __x)
3552{
3553 typedef uniform_int_distribution<_IT> _Eng;
3554 typedef typename _Eng::result_type result_type;
3555 typedef typename _Eng::param_type param_type;
3556 __save_flags<_CharT, _Traits> _(__is);
3557 __is.flags(ios_base::dec | ios_base::skipws);
3558 result_type __a;
3559 result_type __b;
3560 __is >> __a >> __b;
3561 if (!__is.fail())
3562 __x.param(param_type(__a, __b));
3563 return __is;
3564}
3565
Howard Hinnant30a840f2010-05-12 17:08:57 +00003566// uniform_real_distribution
3567
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003568template<class _RealType = double>
3569class uniform_real_distribution
3570{
3571public:
3572 // types
3573 typedef _RealType result_type;
3574
3575 class param_type
3576 {
3577 result_type __a_;
3578 result_type __b_;
3579 public:
3580 typedef uniform_real_distribution distribution_type;
3581
3582 explicit param_type(result_type __a = 0,
3583 result_type __b = 1)
3584 : __a_(__a), __b_(__b) {}
3585
3586 result_type a() const {return __a_;}
3587 result_type b() const {return __b_;}
3588
3589 friend bool operator==(const param_type& __x, const param_type& __y)
3590 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3591 friend bool operator!=(const param_type& __x, const param_type& __y)
3592 {return !(__x == __y);}
3593 };
3594
3595private:
3596 param_type __p_;
3597
3598public:
3599 // constructors and reset functions
3600 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3601 : __p_(param_type(__a, __b)) {}
3602 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
3603 void reset() {}
3604
3605 // generating functions
3606 template<class _URNG> result_type operator()(_URNG& __g)
3607 {return (*this)(__g, __p_);}
3608 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3609
3610 // property functions
3611 result_type a() const {return __p_.a();}
3612 result_type b() const {return __p_.b();}
3613
3614 param_type param() const {return __p_;}
3615 void param(const param_type& __p) {__p_ = __p;}
3616
3617 result_type min() const {return a();}
3618 result_type max() const {return b();}
3619
3620 friend bool operator==(const uniform_real_distribution& __x,
3621 const uniform_real_distribution& __y)
3622 {return __x.__p_ == __y.__p_;}
3623 friend bool operator!=(const uniform_real_distribution& __x,
3624 const uniform_real_distribution& __y)
3625 {return !(__x == __y);}
3626};
3627
3628template<class _RealType>
3629template<class _URNG>
3630inline
3631typename uniform_real_distribution<_RealType>::result_type
3632uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3633{
3634 return (__p.b() - __p.a())
3635 * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
3636 + __p.a();
3637}
3638
3639template <class _CharT, class _Traits, class _RT>
3640basic_ostream<_CharT, _Traits>&
3641operator<<(basic_ostream<_CharT, _Traits>& __os,
3642 const uniform_real_distribution<_RT>& __x)
3643{
3644 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003645 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3646 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647 _CharT __sp = __os.widen(' ');
3648 __os.fill(__sp);
3649 return __os << __x.a() << __sp << __x.b();
3650}
3651
3652template <class _CharT, class _Traits, class _RT>
3653basic_istream<_CharT, _Traits>&
3654operator>>(basic_istream<_CharT, _Traits>& __is,
3655 uniform_real_distribution<_RT>& __x)
3656{
3657 typedef uniform_real_distribution<_RT> _Eng;
3658 typedef typename _Eng::result_type result_type;
3659 typedef typename _Eng::param_type param_type;
3660 __save_flags<_CharT, _Traits> _(__is);
3661 __is.flags(ios_base::dec | ios_base::skipws);
3662 result_type __a;
3663 result_type __b;
3664 __is >> __a >> __b;
3665 if (!__is.fail())
3666 __x.param(param_type(__a, __b));
3667 return __is;
3668}
3669
Howard Hinnant30a840f2010-05-12 17:08:57 +00003670// bernoulli_distribution
3671
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003672class bernoulli_distribution
3673{
3674public:
3675 // types
3676 typedef bool result_type;
3677
3678 class param_type
3679 {
3680 double __p_;
3681 public:
3682 typedef bernoulli_distribution distribution_type;
3683
3684 explicit param_type(double __p = 0.5) : __p_(__p) {}
3685
3686 double p() const {return __p_;}
3687
3688 friend bool operator==(const param_type& __x, const param_type& __y)
3689 {return __x.__p_ == __y.__p_;}
3690 friend bool operator!=(const param_type& __x, const param_type& __y)
3691 {return !(__x == __y);}
3692 };
3693
3694private:
3695 param_type __p_;
3696
3697public:
3698 // constructors and reset functions
3699 explicit bernoulli_distribution(double __p = 0.5)
3700 : __p_(param_type(__p)) {}
3701 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
3702 void reset() {}
3703
3704 // generating functions
3705 template<class _URNG> result_type operator()(_URNG& __g)
3706 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003707 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003708
3709 // property functions
3710 double p() const {return __p_.p();}
3711
3712 param_type param() const {return __p_;}
3713 void param(const param_type& __p) {__p_ = __p;}
3714
3715 result_type min() const {return false;}
3716 result_type max() const {return true;}
3717
3718 friend bool operator==(const bernoulli_distribution& __x,
3719 const bernoulli_distribution& __y)
3720 {return __x.__p_ == __y.__p_;}
3721 friend bool operator!=(const bernoulli_distribution& __x,
3722 const bernoulli_distribution& __y)
3723 {return !(__x == __y);}
3724};
3725
Howard Hinnant03aad812010-05-11 23:26:59 +00003726template<class _URNG>
3727inline
3728bernoulli_distribution::result_type
3729bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3730{
Howard Hinnantd6d11712010-05-20 15:11:46 +00003731 uniform_real_distribution<double> __gen;
3732 return __gen(__g) < __p.p();
Howard Hinnant03aad812010-05-11 23:26:59 +00003733}
3734
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003735template <class _CharT, class _Traits>
3736basic_ostream<_CharT, _Traits>&
3737operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3738{
3739 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003740 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3741 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003742 _CharT __sp = __os.widen(' ');
3743 __os.fill(__sp);
3744 return __os << __x.p();
3745}
3746
3747template <class _CharT, class _Traits>
3748basic_istream<_CharT, _Traits>&
3749operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3750{
3751 typedef bernoulli_distribution _Eng;
3752 typedef typename _Eng::param_type param_type;
3753 __save_flags<_CharT, _Traits> _(__is);
3754 __is.flags(ios_base::dec | ios_base::skipws);
3755 double __p;
3756 __is >> __p;
3757 if (!__is.fail())
3758 __x.param(param_type(__p));
3759 return __is;
3760}
3761
Howard Hinnant30a840f2010-05-12 17:08:57 +00003762// binomial_distribution
3763
Howard Hinnant03aad812010-05-11 23:26:59 +00003764template<class _IntType = int>
3765class binomial_distribution
3766{
3767public:
3768 // types
3769 typedef _IntType result_type;
3770
3771 class param_type
3772 {
3773 result_type __t_;
3774 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003775 double __pr_;
3776 double __odds_ratio_;
3777 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003778 public:
3779 typedef binomial_distribution distribution_type;
3780
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003781 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003782
3783 result_type t() const {return __t_;}
3784 double p() const {return __p_;}
3785
3786 friend bool operator==(const param_type& __x, const param_type& __y)
3787 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
3788 friend bool operator!=(const param_type& __x, const param_type& __y)
3789 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003790
3791 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003792 };
3793
3794private:
3795 param_type __p_;
3796
3797public:
3798 // constructors and reset functions
3799 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3800 : __p_(param_type(__t, __p)) {}
3801 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
3802 void reset() {}
3803
3804 // generating functions
3805 template<class _URNG> result_type operator()(_URNG& __g)
3806 {return (*this)(__g, __p_);}
3807 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3808
3809 // property functions
3810 result_type t() const {return __p_.t();}
3811 double p() const {return __p_.p();}
3812
3813 param_type param() const {return __p_;}
3814 void param(const param_type& __p) {__p_ = __p;}
3815
3816 result_type min() const {return 0;}
3817 result_type max() const {return t();}
3818
3819 friend bool operator==(const binomial_distribution& __x,
3820 const binomial_distribution& __y)
3821 {return __x.__p_ == __y.__p_;}
3822 friend bool operator!=(const binomial_distribution& __x,
3823 const binomial_distribution& __y)
3824 {return !(__x == __y);}
3825};
3826
3827template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003828binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3829 : __t_(__t), __p_(__p)
3830{
3831 if (0 < __p_ && __p_ < 1)
3832 {
3833 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
3834 __pr_ = _STD::exp(_STD::lgamma(__t_ + 1.) - _STD::lgamma(__r0_ + 1.) -
3835 _STD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _STD::log(__p_) +
3836 (__t_ - __r0_) * _STD::log(1 - __p_));
3837 __odds_ratio_ = __p_ / (1 - __p_);
3838 }
3839}
3840
3841template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00003842template<class _URNG>
3843_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003844binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00003845{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003846 if (__pr.__t_ == 0 || __pr.__p_ == 0)
3847 return 0;
3848 if (__pr.__p_ == 1)
3849 return __pr.__t_;
3850 uniform_real_distribution<double> __gen;
3851 double __u = __gen(__g) - __pr.__pr_;
3852 if (__u < 0)
3853 return __pr.__r0_;
3854 double __pu = __pr.__pr_;
3855 double __pd = __pu;
3856 result_type __ru = __pr.__r0_;
3857 result_type __rd = __ru;
3858 while (true)
3859 {
3860 if (__rd >= 1)
3861 {
3862 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3863 __u -= __pd;
3864 if (__u < 0)
3865 return __rd - 1;
3866 }
3867 --__rd;
3868 ++__ru;
3869 if (__ru <= __pr.__t_)
3870 {
3871 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3872 __u -= __pu;
3873 if (__u < 0)
3874 return __ru;
3875 }
3876 }
Howard Hinnant03aad812010-05-11 23:26:59 +00003877}
3878
3879template <class _CharT, class _Traits, class _IntType>
3880basic_ostream<_CharT, _Traits>&
3881operator<<(basic_ostream<_CharT, _Traits>& __os,
3882 const binomial_distribution<_IntType>& __x)
3883{
3884 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003885 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3886 ios_base::scientific);
Howard Hinnant03aad812010-05-11 23:26:59 +00003887 _CharT __sp = __os.widen(' ');
3888 __os.fill(__sp);
3889 return __os << __x.t() << __sp << __x.p();
3890}
3891
3892template <class _CharT, class _Traits, class _IntType>
3893basic_istream<_CharT, _Traits>&
3894operator>>(basic_istream<_CharT, _Traits>& __is,
3895 binomial_distribution<_IntType>& __x)
3896{
3897 typedef binomial_distribution<_IntType> _Eng;
3898 typedef typename _Eng::result_type result_type;
3899 typedef typename _Eng::param_type param_type;
3900 __save_flags<_CharT, _Traits> _(__is);
3901 __is.flags(ios_base::dec | ios_base::skipws);
3902 result_type __t;
3903 double __p;
3904 __is >> __t >> __p;
3905 if (!__is.fail())
3906 __x.param(param_type(__t, __p));
3907 return __is;
3908}
3909
Howard Hinnant30a840f2010-05-12 17:08:57 +00003910// exponential_distribution
3911
3912template<class _RealType = double>
3913class exponential_distribution
3914{
3915public:
3916 // types
3917 typedef _RealType result_type;
3918
3919 class param_type
3920 {
3921 result_type __lambda_;
3922 public:
3923 typedef exponential_distribution distribution_type;
3924
3925 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3926
3927 result_type lambda() const {return __lambda_;}
3928
3929 friend bool operator==(const param_type& __x, const param_type& __y)
3930 {return __x.__lambda_ == __y.__lambda_;}
3931 friend bool operator!=(const param_type& __x, const param_type& __y)
3932 {return !(__x == __y);}
3933 };
3934
3935private:
3936 param_type __p_;
3937
3938public:
3939 // constructors and reset functions
3940 explicit exponential_distribution(result_type __lambda = 1)
3941 : __p_(param_type(__lambda)) {}
3942 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
3943 void reset() {}
3944
3945 // generating functions
3946 template<class _URNG> result_type operator()(_URNG& __g)
3947 {return (*this)(__g, __p_);}
3948 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3949
3950 // property functions
3951 result_type lambda() const {return __p_.lambda();}
3952
3953 param_type param() const {return __p_;}
3954 void param(const param_type& __p) {__p_ = __p;}
3955
3956 result_type min() const {return 0;}
Howard Hinnantdf40dc62010-05-16 17:56:20 +00003957 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00003958
3959 friend bool operator==(const exponential_distribution& __x,
3960 const exponential_distribution& __y)
3961 {return __x.__p_ == __y.__p_;}
3962 friend bool operator!=(const exponential_distribution& __x,
3963 const exponential_distribution& __y)
3964 {return !(__x == __y);}
3965};
3966
3967template <class _RealType>
3968template<class _URNG>
3969_RealType
3970exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3971{
3972 return -_STD::log
3973 (
3974 result_type(1) -
3975 _STD::generate_canonical<result_type,
3976 numeric_limits<result_type>::digits>(__g)
3977 )
3978 / __p.lambda();
3979}
3980
3981template <class _CharT, class _Traits, class _RealType>
3982basic_ostream<_CharT, _Traits>&
3983operator<<(basic_ostream<_CharT, _Traits>& __os,
3984 const exponential_distribution<_RealType>& __x)
3985{
3986 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003987 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3988 ios_base::scientific);
Howard Hinnant30a840f2010-05-12 17:08:57 +00003989 return __os << __x.lambda();
3990}
3991
3992template <class _CharT, class _Traits, class _RealType>
3993basic_istream<_CharT, _Traits>&
3994operator>>(basic_istream<_CharT, _Traits>& __is,
3995 exponential_distribution<_RealType>& __x)
3996{
3997 typedef exponential_distribution<_RealType> _Eng;
3998 typedef typename _Eng::result_type result_type;
3999 typedef typename _Eng::param_type param_type;
4000 __save_flags<_CharT, _Traits> _(__is);
4001 __is.flags(ios_base::dec | ios_base::skipws);
4002 result_type __lambda;
4003 __is >> __lambda;
4004 if (!__is.fail())
4005 __x.param(param_type(__lambda));
4006 return __is;
4007}
4008
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004009// normal_distribution
4010
4011template<class _RealType = double>
4012class normal_distribution
4013{
4014public:
4015 // types
4016 typedef _RealType result_type;
4017
4018 class param_type
4019 {
4020 result_type __mean_;
4021 result_type __stddev_;
4022 public:
4023 typedef normal_distribution distribution_type;
4024
4025 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4026 : __mean_(__mean), __stddev_(__stddev) {}
4027
4028 result_type mean() const {return __mean_;}
4029 result_type stddev() const {return __stddev_;}
4030
4031 friend bool operator==(const param_type& __x, const param_type& __y)
4032 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
4033 friend bool operator!=(const param_type& __x, const param_type& __y)
4034 {return !(__x == __y);}
4035 };
4036
4037private:
4038 param_type __p_;
4039 result_type _V_;
4040 bool _V_hot_;
4041
4042public:
4043 // constructors and reset functions
4044 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4045 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
4046 explicit normal_distribution(const param_type& __p)
4047 : __p_(__p), _V_hot_(false) {}
4048 void reset() {_V_hot_ = false;}
4049
4050 // generating functions
4051 template<class _URNG> result_type operator()(_URNG& __g)
4052 {return (*this)(__g, __p_);}
4053 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4054
4055 // property functions
4056 result_type mean() const {return __p_.mean();}
4057 result_type stddev() const {return __p_.stddev();}
4058
4059 param_type param() const {return __p_;}
4060 void param(const param_type& __p) {__p_ = __p;}
4061
4062 result_type min() const {return -numeric_limits<result_type>::infinity();}
4063 result_type max() const {return numeric_limits<result_type>::infinity();}
4064
4065 friend bool operator==(const normal_distribution& __x,
4066 const normal_distribution& __y)
4067 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4068 (!__x._V_hot_ || __x._V_ == __y._V_);}
4069 friend bool operator!=(const normal_distribution& __x,
4070 const normal_distribution& __y)
4071 {return !(__x == __y);}
4072
4073 template <class _CharT, class _Traits, class _RT>
4074 friend
4075 basic_ostream<_CharT, _Traits>&
4076 operator<<(basic_ostream<_CharT, _Traits>& __os,
4077 const normal_distribution<_RT>& __x);
4078
4079 template <class _CharT, class _Traits, class _RT>
4080 friend
4081 basic_istream<_CharT, _Traits>&
4082 operator>>(basic_istream<_CharT, _Traits>& __is,
4083 normal_distribution<_RT>& __x);
4084};
4085
4086template <class _RealType>
4087template<class _URNG>
4088_RealType
4089normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4090{
4091 result_type _U;
4092 if (_V_hot_)
4093 {
4094 _V_hot_ = false;
4095 _U = _V_;
4096 }
4097 else
4098 {
4099 uniform_real_distribution<result_type> _Uni(-1, 1);
4100 result_type __u;
4101 result_type __v;
4102 result_type __s;
4103 do
4104 {
4105 __u = _Uni(__g);
4106 __v = _Uni(__g);
4107 __s = __u * __u + __v * __v;
4108 } while (__s > 1 || __s == 0);
4109 result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s);
4110 _V_ = __v * _F;
4111 _V_hot_ = true;
4112 _U = __u * _F;
4113 }
4114 return _U * __p.stddev() + __p.mean();
4115}
4116
4117template <class _CharT, class _Traits, class _RT>
4118basic_ostream<_CharT, _Traits>&
4119operator<<(basic_ostream<_CharT, _Traits>& __os,
4120 const normal_distribution<_RT>& __x)
4121{
4122 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004123 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4124 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004125 _CharT __sp = __os.widen(' ');
4126 __os.fill(__sp);
4127 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4128 if (__x._V_hot_)
4129 __os << __sp << __x._V_;
4130 return __os;
4131}
4132
4133template <class _CharT, class _Traits, class _RT>
4134basic_istream<_CharT, _Traits>&
4135operator>>(basic_istream<_CharT, _Traits>& __is,
4136 normal_distribution<_RT>& __x)
4137{
4138 typedef normal_distribution<_RT> _Eng;
4139 typedef typename _Eng::result_type result_type;
4140 typedef typename _Eng::param_type param_type;
4141 __save_flags<_CharT, _Traits> _(__is);
4142 __is.flags(ios_base::dec | ios_base::skipws);
4143 result_type __mean;
4144 result_type __stddev;
4145 result_type _V = 0;
4146 bool _V_hot = false;
4147 __is >> __mean >> __stddev >> _V_hot;
4148 if (_V_hot)
4149 __is >> _V;
4150 if (!__is.fail())
4151 {
4152 __x.param(param_type(__mean, __stddev));
4153 __x._V_hot_ = _V_hot;
4154 __x._V_ = _V;
4155 }
4156 return __is;
4157}
4158
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004159// lognormal_distribution
4160
4161template<class _RealType = double>
4162class lognormal_distribution
4163{
4164public:
4165 // types
4166 typedef _RealType result_type;
4167
4168 class param_type
4169 {
4170 normal_distribution<result_type> __nd_;
4171 public:
4172 typedef lognormal_distribution distribution_type;
4173
4174 explicit param_type(result_type __m = 0, result_type __s = 1)
4175 : __nd_(__m, __s) {}
4176
4177 result_type m() const {return __nd_.mean();}
4178 result_type s() const {return __nd_.stddev();}
4179
4180 friend bool operator==(const param_type& __x, const param_type& __y)
4181 {return __x.__nd_ == __y.__nd_;}
4182 friend bool operator!=(const param_type& __x, const param_type& __y)
4183 {return !(__x == __y);}
4184 friend class lognormal_distribution;
4185
4186 template <class _CharT, class _Traits, class _RT>
4187 friend
4188 basic_ostream<_CharT, _Traits>&
4189 operator<<(basic_ostream<_CharT, _Traits>& __os,
4190 const lognormal_distribution<_RT>& __x);
4191
4192 template <class _CharT, class _Traits, class _RT>
4193 friend
4194 basic_istream<_CharT, _Traits>&
4195 operator>>(basic_istream<_CharT, _Traits>& __is,
4196 lognormal_distribution<_RT>& __x);
4197 };
4198
4199private:
4200 param_type __p_;
4201
4202public:
4203 // constructor and reset functions
4204 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4205 : __p_(param_type(__m, __s)) {}
4206 explicit lognormal_distribution(const param_type& __p)
4207 : __p_(__p) {}
4208 void reset() {__p_.__nd_.reset();}
4209
4210 // generating functions
4211 template<class _URNG> result_type operator()(_URNG& __g)
4212 {return (*this)(__g, __p_);}
4213 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4214 {return _STD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
4215
4216 // property functions
4217 result_type m() const {return __p_.m();}
4218 result_type s() const {return __p_.s();}
4219
4220 param_type param() const {return __p_;}
Howard Hinnant551d8e42010-05-19 01:53:57 +00004221 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004222
4223 result_type min() const {return 0;}
4224 result_type max() const {return numeric_limits<result_type>::infinity();}
4225
4226 friend bool operator==(const lognormal_distribution& __x,
4227 const lognormal_distribution& __y)
4228 {return __x.__p_ == __y.__p_;}
4229 friend bool operator!=(const lognormal_distribution& __x,
4230 const lognormal_distribution& __y)
4231 {return !(__x == __y);}
4232
4233 template <class _CharT, class _Traits, class _RT>
4234 friend
4235 basic_ostream<_CharT, _Traits>&
4236 operator<<(basic_ostream<_CharT, _Traits>& __os,
4237 const lognormal_distribution<_RT>& __x);
4238
4239 template <class _CharT, class _Traits, class _RT>
4240 friend
4241 basic_istream<_CharT, _Traits>&
4242 operator>>(basic_istream<_CharT, _Traits>& __is,
4243 lognormal_distribution<_RT>& __x);
4244};
4245
4246template <class _CharT, class _Traits, class _RT>
4247inline
4248basic_ostream<_CharT, _Traits>&
4249operator<<(basic_ostream<_CharT, _Traits>& __os,
4250 const lognormal_distribution<_RT>& __x)
4251{
4252 return __os << __x.__p_.__nd_;
4253}
4254
4255template <class _CharT, class _Traits, class _RT>
4256inline
4257basic_istream<_CharT, _Traits>&
4258operator>>(basic_istream<_CharT, _Traits>& __is,
4259 lognormal_distribution<_RT>& __x)
4260{
4261 return __is >> __x.__p_.__nd_;
4262}
4263
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004264// poisson_distribution
4265
4266template<class _IntType = int>
4267class poisson_distribution
4268{
4269public:
4270 // types
4271 typedef _IntType result_type;
4272
4273 class param_type
4274 {
4275 double __mean_;
4276 double __s_;
4277 double __d_;
4278 double __l_;
4279 double __omega_;
4280 double __c0_;
4281 double __c1_;
4282 double __c2_;
4283 double __c3_;
4284 double __c_;
4285
4286 public:
4287 typedef poisson_distribution distribution_type;
4288
4289 explicit param_type(double __mean = 1.0);
4290
4291 double mean() const {return __mean_;}
4292
4293 friend bool operator==(const param_type& __x, const param_type& __y)
4294 {return __x.__mean_ == __y.__mean_;}
4295 friend bool operator!=(const param_type& __x, const param_type& __y)
4296 {return !(__x == __y);}
4297
4298 friend class poisson_distribution;
4299 };
4300
4301private:
4302 param_type __p_;
4303
4304public:
4305 // constructors and reset functions
4306 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
4307 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
4308 void reset() {}
4309
4310 // generating functions
4311 template<class _URNG> result_type operator()(_URNG& __g)
4312 {return (*this)(__g, __p_);}
4313 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4314
4315 // property functions
4316 double mean() const {return __p_.mean();}
4317
4318 param_type param() const {return __p_;}
4319 void param(const param_type& __p) {__p_ = __p;}
4320
4321 result_type min() const {return 0;}
4322 result_type max() const {return numeric_limits<result_type>::max();}
4323
4324 friend bool operator==(const poisson_distribution& __x,
4325 const poisson_distribution& __y)
4326 {return __x.__p_ == __y.__p_;}
4327 friend bool operator!=(const poisson_distribution& __x,
4328 const poisson_distribution& __y)
4329 {return !(__x == __y);}
4330};
4331
4332template<class _IntType>
4333poisson_distribution<_IntType>::param_type::param_type(double __mean)
4334 : __mean_(__mean)
4335{
4336 if (__mean_ < 10)
4337 {
4338 __s_ = 0;
4339 __d_ = 0;
4340 __l_ = _STD::exp(-__mean_);
4341 __omega_ = 0;
4342 __c3_ = 0;
4343 __c2_ = 0;
4344 __c1_ = 0;
4345 __c0_ = 0;
4346 __c_ = 0;
4347 }
4348 else
4349 {
4350 __s_ = _STD::sqrt(__mean_);
4351 __d_ = 6 * __mean_ * __mean_;
4352 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4353 __omega_ = .3989423 / __s_;
4354 double __b1_ = .4166667E-1 / __mean_;
4355 double __b2_ = .3 * __b1_ * __b1_;
4356 __c3_ = .1428571 * __b1_ * __b2_;
4357 __c2_ = __b2_ - 15. * __c3_;
4358 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4359 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4360 __c_ = .1069 / __mean_;
4361 }
4362}
4363
4364template <class _IntType>
4365template<class _URNG>
4366_IntType
4367poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4368{
4369 result_type __x;
4370 uniform_real_distribution<double> __urd;
4371 if (__pr.__mean_ <= 10)
4372 {
4373 __x = 0;
4374 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4375 __p *= __urd(__urng);
4376 }
4377 else
4378 {
4379 double __difmuk;
4380 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4381 double __u;
4382 if (__g > 0)
4383 {
4384 __x = static_cast<result_type>(__g);
4385 if (__x >= __pr.__l_)
4386 return __x;
4387 __difmuk = __pr.__mean_ - __x;
4388 __u = __urd(__urng);
4389 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4390 return __x;
4391 }
4392 exponential_distribution<double> __edist;
4393 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4394 {
4395 double __e;
4396 if (__using_exp_dist || __g < 0)
4397 {
4398 double __t;
4399 do
4400 {
4401 __e = __edist(__urng);
4402 __u = __urd(__urng);
4403 __u += __u - 1;
4404 __t = 1.8 + (__u < 0 ? -__e : __e);
4405 } while (__t <= -.6744);
4406 __x = __pr.__mean_ + __pr.__s_ * __t;
4407 __difmuk = __pr.__mean_ - __x;
4408 __using_exp_dist = true;
4409 }
4410 double __px;
4411 double __py;
4412 if (__x < 10)
4413 {
4414 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4415 40320, 362880};
4416 __px = -__pr.__mean_;
4417 __py = _STD::pow(__pr.__mean_, (double)__x) / __fac[__x];
4418 }
4419 else
4420 {
4421 double __del = .8333333E-1 / __x;
4422 __del -= 4.8 * __del * __del * __del;
4423 double __v = __difmuk / __x;
4424 if (_STD::abs(__v) > 0.25)
4425 __px = __x * _STD::log(1 + __v) - __difmuk - __del;
4426 else
4427 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4428 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4429 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
4430 __py = .3989423 / _STD::sqrt(__x);
4431 }
4432 double __r = (0.5 - __difmuk) / __pr.__s_;
4433 double __r2 = __r * __r;
4434 double __fx = -0.5 * __r2;
4435 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4436 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4437 if (__using_exp_dist)
4438 {
4439 if (__pr.__c_ * _STD::abs(__u) <= __py * _STD::exp(__px + __e) -
4440 __fy * _STD::exp(__fx + __e))
4441 break;
4442 }
4443 else
4444 {
4445 if (__fy - __u * __fy <= __py * _STD::exp(__px - __fx))
4446 break;
4447 }
4448 }
4449 }
4450 return __x;
4451}
4452
4453template <class _CharT, class _Traits, class _IntType>
4454basic_ostream<_CharT, _Traits>&
4455operator<<(basic_ostream<_CharT, _Traits>& __os,
4456 const poisson_distribution<_IntType>& __x)
4457{
4458 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004459 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4460 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004461 return __os << __x.mean();
4462}
4463
4464template <class _CharT, class _Traits, class _IntType>
4465basic_istream<_CharT, _Traits>&
4466operator>>(basic_istream<_CharT, _Traits>& __is,
4467 poisson_distribution<_IntType>& __x)
4468{
4469 typedef poisson_distribution<_IntType> _Eng;
4470 typedef typename _Eng::param_type param_type;
4471 __save_flags<_CharT, _Traits> _(__is);
4472 __is.flags(ios_base::dec | ios_base::skipws);
4473 double __mean;
4474 __is >> __mean;
4475 if (!__is.fail())
4476 __x.param(param_type(__mean));
4477 return __is;
4478}
4479
Howard Hinnant9de6e302010-05-16 01:09:02 +00004480// weibull_distribution
4481
4482template<class _RealType = double>
4483class weibull_distribution
4484{
4485public:
4486 // types
4487 typedef _RealType result_type;
4488
4489 class param_type
4490 {
4491 result_type __a_;
4492 result_type __b_;
4493 public:
4494 typedef weibull_distribution distribution_type;
4495
4496 explicit param_type(result_type __a = 1, result_type __b = 1)
4497 : __a_(__a), __b_(__b) {}
4498
4499 result_type a() const {return __a_;}
4500 result_type b() const {return __b_;}
4501
4502 friend bool operator==(const param_type& __x, const param_type& __y)
4503 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
4504 friend bool operator!=(const param_type& __x, const param_type& __y)
4505 {return !(__x == __y);}
4506 };
4507
4508private:
4509 param_type __p_;
4510
4511public:
4512 // constructor and reset functions
4513 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4514 : __p_(param_type(__a, __b)) {}
4515 explicit weibull_distribution(const param_type& __p)
4516 : __p_(__p) {}
4517 void reset() {}
4518
4519 // generating functions
4520 template<class _URNG> result_type operator()(_URNG& __g)
4521 {return (*this)(__g, __p_);}
4522 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4523 {return __p.b() *
4524 _STD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
4525
4526 // property functions
4527 result_type a() const {return __p_.a();}
4528 result_type b() const {return __p_.b();}
4529
4530 param_type param() const {return __p_;}
4531 void param(const param_type& __p) {__p_ = __p;}
4532
4533 result_type min() const {return 0;}
4534 result_type max() const {return numeric_limits<result_type>::infinity();}
4535
4536
4537 friend bool operator==(const weibull_distribution& __x,
4538 const weibull_distribution& __y)
4539 {return __x.__p_ == __y.__p_;}
4540 friend bool operator!=(const weibull_distribution& __x,
4541 const weibull_distribution& __y)
4542 {return !(__x == __y);}
4543};
4544
4545template <class _CharT, class _Traits, class _RT>
4546basic_ostream<_CharT, _Traits>&
4547operator<<(basic_ostream<_CharT, _Traits>& __os,
4548 const weibull_distribution<_RT>& __x)
4549{
4550 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004551 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4552 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004553 _CharT __sp = __os.widen(' ');
4554 __os.fill(__sp);
4555 __os << __x.a() << __sp << __x.b();
4556 return __os;
4557}
4558
4559template <class _CharT, class _Traits, class _RT>
4560basic_istream<_CharT, _Traits>&
4561operator>>(basic_istream<_CharT, _Traits>& __is,
4562 weibull_distribution<_RT>& __x)
4563{
4564 typedef weibull_distribution<_RT> _Eng;
4565 typedef typename _Eng::result_type result_type;
4566 typedef typename _Eng::param_type param_type;
4567 __save_flags<_CharT, _Traits> _(__is);
4568 __is.flags(ios_base::dec | ios_base::skipws);
4569 result_type __a;
4570 result_type __b;
4571 __is >> __a >> __b;
4572 if (!__is.fail())
4573 __x.param(param_type(__a, __b));
4574 return __is;
4575}
4576
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004577template<class _RealType = double>
4578class extreme_value_distribution
4579{
4580public:
4581 // types
4582 typedef _RealType result_type;
4583
4584 class param_type
4585 {
4586 result_type __a_;
4587 result_type __b_;
4588 public:
4589 typedef extreme_value_distribution distribution_type;
4590
4591 explicit param_type(result_type __a = 0, result_type __b = 1)
4592 : __a_(__a), __b_(__b) {}
4593
4594 result_type a() const {return __a_;}
4595 result_type b() const {return __b_;}
4596
4597 friend bool operator==(const param_type& __x, const param_type& __y)
4598 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
4599 friend bool operator!=(const param_type& __x, const param_type& __y)
4600 {return !(__x == __y);}
4601 };
4602
4603private:
4604 param_type __p_;
4605
4606public:
4607 // constructor and reset functions
4608 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4609 : __p_(param_type(__a, __b)) {}
4610 explicit extreme_value_distribution(const param_type& __p)
4611 : __p_(__p) {}
4612 void reset() {}
4613
4614 // generating functions
4615 template<class _URNG> result_type operator()(_URNG& __g)
4616 {return (*this)(__g, __p_);}
4617 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4618
4619 // property functions
4620 result_type a() const {return __p_.a();}
4621 result_type b() const {return __p_.b();}
4622
4623 param_type param() const {return __p_;}
4624 void param(const param_type& __p) {__p_ = __p;}
4625
4626 result_type min() const {return -numeric_limits<result_type>::infinity();}
4627 result_type max() const {return numeric_limits<result_type>::infinity();}
4628
4629 friend bool operator==(const extreme_value_distribution& __x,
4630 const extreme_value_distribution& __y)
4631 {return __x.__p_ == __y.__p_;}
4632 friend bool operator!=(const extreme_value_distribution& __x,
4633 const extreme_value_distribution& __y)
4634 {return !(__x == __y);}
4635};
4636
4637template<class _RealType>
4638template<class _URNG>
4639_RealType
4640extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4641{
4642 return __p.a() - __p.b() *
4643 _STD::log(-_STD::log(1-uniform_real_distribution<result_type>()(__g)));
4644}
4645
4646template <class _CharT, class _Traits, class _RT>
4647basic_ostream<_CharT, _Traits>&
4648operator<<(basic_ostream<_CharT, _Traits>& __os,
4649 const extreme_value_distribution<_RT>& __x)
4650{
4651 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004652 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4653 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004654 _CharT __sp = __os.widen(' ');
4655 __os.fill(__sp);
4656 __os << __x.a() << __sp << __x.b();
4657 return __os;
4658}
4659
4660template <class _CharT, class _Traits, class _RT>
4661basic_istream<_CharT, _Traits>&
4662operator>>(basic_istream<_CharT, _Traits>& __is,
4663 extreme_value_distribution<_RT>& __x)
4664{
4665 typedef extreme_value_distribution<_RT> _Eng;
4666 typedef typename _Eng::result_type result_type;
4667 typedef typename _Eng::param_type param_type;
4668 __save_flags<_CharT, _Traits> _(__is);
4669 __is.flags(ios_base::dec | ios_base::skipws);
4670 result_type __a;
4671 result_type __b;
4672 __is >> __a >> __b;
4673 if (!__is.fail())
4674 __x.param(param_type(__a, __b));
4675 return __is;
4676}
4677
Howard Hinnantc7c49132010-05-13 17:58:28 +00004678// gamma_distribution
4679
4680template<class _RealType = double>
4681class gamma_distribution
4682{
4683public:
4684 // types
4685 typedef _RealType result_type;
4686
4687 class param_type
4688 {
4689 result_type __alpha_;
4690 result_type __beta_;
4691 public:
4692 typedef gamma_distribution distribution_type;
4693
4694 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4695 : __alpha_(__alpha), __beta_(__beta) {}
4696
4697 result_type alpha() const {return __alpha_;}
4698 result_type beta() const {return __beta_;}
4699
4700 friend bool operator==(const param_type& __x, const param_type& __y)
4701 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
4702 friend bool operator!=(const param_type& __x, const param_type& __y)
4703 {return !(__x == __y);}
4704 };
4705
4706private:
4707 param_type __p_;
4708
4709public:
4710 // constructors and reset functions
4711 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4712 : __p_(param_type(__alpha, __beta)) {}
4713 explicit gamma_distribution(const param_type& __p)
4714 : __p_(__p) {}
4715 void reset() {}
4716
4717 // generating functions
4718 template<class _URNG> result_type operator()(_URNG& __g)
4719 {return (*this)(__g, __p_);}
4720 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4721
4722 // property functions
4723 result_type alpha() const {return __p_.alpha();}
4724 result_type beta() const {return __p_.beta();}
4725
4726 param_type param() const {return __p_;}
4727 void param(const param_type& __p) {__p_ = __p;}
4728
4729 result_type min() const {return 0;}
4730 result_type max() const {return numeric_limits<result_type>::infinity();}
4731
4732 friend bool operator==(const gamma_distribution& __x,
4733 const gamma_distribution& __y)
4734 {return __x.__p_ == __y.__p_;}
4735 friend bool operator!=(const gamma_distribution& __x,
4736 const gamma_distribution& __y)
4737 {return !(__x == __y);}
4738};
4739
4740template <class _RealType>
4741template<class _URNG>
4742_RealType
4743gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4744{
Howard Hinnantf417abe2010-05-14 18:43:10 +00004745 result_type __a = __p.alpha();
4746 uniform_real_distribution<result_type> __gen(0, 1);
4747 exponential_distribution<result_type> __egen;
4748 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00004749 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00004750 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004751 else if (__a > 1)
4752 {
4753 const result_type __b = __a - 1;
4754 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004755 while (true)
4756 {
4757 const result_type __u = __gen(__g);
4758 const result_type __v = __gen(__g);
4759 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004760 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004761 {
4762 const result_type __y = _STD::sqrt(__c / __w) *
4763 (__u - result_type(0.5));
4764 __x = __b + __y;
4765 if (__x >= 0)
4766 {
4767 const result_type __z = 64 * __w * __w * __w * __v * __v;
4768 if (__z <= 1 - 2 * __y * __y / __x)
4769 break;
4770 if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y))
4771 break;
4772 }
4773 }
4774 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00004775 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00004776 else // __a < 1
4777 {
4778 while (true)
4779 {
4780 const result_type __u = __gen(__g);
4781 const result_type __es = __egen(__g);
4782 if (__u <= 1 - __a)
4783 {
4784 __x = _STD::pow(__u, 1 / __a);
4785 if (__x <= __es)
4786 break;
4787 }
4788 else
4789 {
4790 const result_type __e = -_STD::log((1-__u)/__a);
4791 __x = _STD::pow(1 - __a + __a * __e, 1 / __a);
4792 if (__x <= __e + __es)
4793 break;
4794 }
4795 }
4796 }
4797 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00004798}
4799
4800template <class _CharT, class _Traits, class _RT>
4801basic_ostream<_CharT, _Traits>&
4802operator<<(basic_ostream<_CharT, _Traits>& __os,
4803 const gamma_distribution<_RT>& __x)
4804{
4805 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004806 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4807 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004808 _CharT __sp = __os.widen(' ');
4809 __os.fill(__sp);
4810 __os << __x.alpha() << __sp << __x.beta();
4811 return __os;
4812}
4813
4814template <class _CharT, class _Traits, class _RT>
4815basic_istream<_CharT, _Traits>&
4816operator>>(basic_istream<_CharT, _Traits>& __is,
4817 gamma_distribution<_RT>& __x)
4818{
4819 typedef gamma_distribution<_RT> _Eng;
4820 typedef typename _Eng::result_type result_type;
4821 typedef typename _Eng::param_type param_type;
4822 __save_flags<_CharT, _Traits> _(__is);
4823 __is.flags(ios_base::dec | ios_base::skipws);
4824 result_type __alpha;
4825 result_type __beta;
4826 __is >> __alpha >> __beta;
4827 if (!__is.fail())
4828 __x.param(param_type(__alpha, __beta));
4829 return __is;
4830}
Howard Hinnanta64111c2010-05-12 21:02:31 +00004831
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00004832// negative_binomial_distribution
4833
4834template<class _IntType = int>
4835class negative_binomial_distribution
4836{
4837public:
4838 // types
4839 typedef _IntType result_type;
4840
4841 class param_type
4842 {
4843 result_type __k_;
4844 double __p_;
4845 public:
4846 typedef negative_binomial_distribution distribution_type;
4847
4848 explicit param_type(result_type __k = 1, double __p = 0.5)
4849 : __k_(__k), __p_(__p) {}
4850
4851 result_type k() const {return __k_;}
4852 double p() const {return __p_;}
4853
4854 friend bool operator==(const param_type& __x, const param_type& __y)
4855 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
4856 friend bool operator!=(const param_type& __x, const param_type& __y)
4857 {return !(__x == __y);}
4858 };
4859
4860private:
4861 param_type __p_;
4862
4863public:
4864 // constructor and reset functions
4865 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
4866 : __p_(__k, __p) {}
4867 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
4868 void reset() {}
4869
4870 // generating functions
4871 template<class _URNG> result_type operator()(_URNG& __g)
4872 {return (*this)(__g, __p_);}
4873 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4874
4875 // property functions
4876 result_type k() const {return __p_.k();}
4877 double p() const {return __p_.p();}
4878
4879 param_type param() const {return __p_;}
4880 void param(const param_type& __p) {__p_ = __p;}
4881
4882 result_type min() const {return 0;}
4883 result_type max() const {return numeric_limits<result_type>::max();}
4884
4885 friend bool operator==(const negative_binomial_distribution& __x,
4886 const negative_binomial_distribution& __y)
4887 {return __x.__p_ == __y.__p_;}
4888 friend bool operator!=(const negative_binomial_distribution& __x,
4889 const negative_binomial_distribution& __y)
4890 {return !(__x == __y);}
4891};
4892
4893template <class _IntType>
4894template<class _URNG>
4895_IntType
4896negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4897{
4898 result_type __k = __pr.k();
4899 double __p = __pr.p();
4900 if (__k <= 21 * __p)
4901 {
4902 bernoulli_distribution __gen(__p);
4903 result_type __f = 0;
4904 result_type __s = 0;
4905 while (__s < __k)
4906 {
4907 if (__gen(__urng))
4908 ++__s;
4909 else
4910 ++__f;
4911 }
4912 return __f;
4913 }
4914 return poisson_distribution<result_type>(gamma_distribution<double>
4915 (__k, (1-__p)/__p)(__urng))(__urng);
4916}
4917
4918template <class _CharT, class _Traits, class _IntType>
4919basic_ostream<_CharT, _Traits>&
4920operator<<(basic_ostream<_CharT, _Traits>& __os,
4921 const negative_binomial_distribution<_IntType>& __x)
4922{
4923 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004924 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4925 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00004926 _CharT __sp = __os.widen(' ');
4927 __os.fill(__sp);
4928 return __os << __x.k() << __sp << __x.p();
4929}
4930
4931template <class _CharT, class _Traits, class _IntType>
4932basic_istream<_CharT, _Traits>&
4933operator>>(basic_istream<_CharT, _Traits>& __is,
4934 negative_binomial_distribution<_IntType>& __x)
4935{
4936 typedef negative_binomial_distribution<_IntType> _Eng;
4937 typedef typename _Eng::result_type result_type;
4938 typedef typename _Eng::param_type param_type;
4939 __save_flags<_CharT, _Traits> _(__is);
4940 __is.flags(ios_base::dec | ios_base::skipws);
4941 result_type __k;
4942 double __p;
4943 __is >> __k >> __p;
4944 if (!__is.fail())
4945 __x.param(param_type(__k, __p));
4946 return __is;
4947}
4948
Howard Hinnant34e8a572010-05-17 13:44:27 +00004949// geometric_distribution
4950
4951template<class _IntType = int>
4952class geometric_distribution
4953{
4954public:
4955 // types
4956 typedef _IntType result_type;
4957
4958 class param_type
4959 {
4960 double __p_;
4961 public:
4962 typedef geometric_distribution distribution_type;
4963
4964 explicit param_type(double __p = 0.5) : __p_(__p) {}
4965
4966 double p() const {return __p_;}
4967
4968 friend bool operator==(const param_type& __x, const param_type& __y)
4969 {return __x.__p_ == __y.__p_;}
4970 friend bool operator!=(const param_type& __x, const param_type& __y)
4971 {return !(__x == __y);}
4972 };
4973
4974private:
4975 param_type __p_;
4976
4977public:
4978 // constructors and reset functions
4979 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
4980 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
4981 void reset() {}
4982
4983 // generating functions
4984 template<class _URNG> result_type operator()(_URNG& __g)
4985 {return (*this)(__g, __p_);}
4986 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4987 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
4988
4989 // property functions
4990 double p() const {return __p_.p();}
4991
4992 param_type param() const {return __p_;}
4993 void param(const param_type& __p) {__p_ = __p;}
4994
4995 result_type min() const {return 0;}
4996 result_type max() const {return numeric_limits<result_type>::max();}
4997
4998 friend bool operator==(const geometric_distribution& __x,
4999 const geometric_distribution& __y)
5000 {return __x.__p_ == __y.__p_;}
5001 friend bool operator!=(const geometric_distribution& __x,
5002 const geometric_distribution& __y)
5003 {return !(__x == __y);}
5004};
5005
5006template <class _CharT, class _Traits, class _IntType>
5007basic_ostream<_CharT, _Traits>&
5008operator<<(basic_ostream<_CharT, _Traits>& __os,
5009 const geometric_distribution<_IntType>& __x)
5010{
5011 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005012 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5013 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005014 return __os << __x.p();
5015}
5016
5017template <class _CharT, class _Traits, class _IntType>
5018basic_istream<_CharT, _Traits>&
5019operator>>(basic_istream<_CharT, _Traits>& __is,
5020 geometric_distribution<_IntType>& __x)
5021{
5022 typedef geometric_distribution<_IntType> _Eng;
5023 typedef typename _Eng::param_type param_type;
5024 __save_flags<_CharT, _Traits> _(__is);
5025 __is.flags(ios_base::dec | ios_base::skipws);
5026 double __p;
5027 __is >> __p;
5028 if (!__is.fail())
5029 __x.param(param_type(__p));
5030 return __is;
5031}
5032
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005033// chi_squared_distribution
5034
5035template<class _RealType = double>
5036class chi_squared_distribution
5037{
5038public:
5039 // types
5040 typedef _RealType result_type;
5041
5042 class param_type
5043 {
5044 result_type __n_;
5045 public:
5046 typedef chi_squared_distribution distribution_type;
5047
5048 explicit param_type(result_type __n = 1) : __n_(__n) {}
5049
5050 result_type n() const {return __n_;}
5051
5052 friend bool operator==(const param_type& __x, const param_type& __y)
5053 {return __x.__n_ == __y.__n_;}
5054 friend bool operator!=(const param_type& __x, const param_type& __y)
5055 {return !(__x == __y);}
5056 };
5057
5058private:
5059 param_type __p_;
5060
5061public:
5062 // constructor and reset functions
5063 explicit chi_squared_distribution(result_type __n = 1)
5064 : __p_(param_type(__n)) {}
5065 explicit chi_squared_distribution(const param_type& __p)
5066 : __p_(__p) {}
5067 void reset() {}
5068
5069 // generating functions
5070 template<class _URNG> result_type operator()(_URNG& __g)
5071 {return (*this)(__g, __p_);}
5072 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
5073 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5074
5075 // property functions
5076 result_type n() const {return __p_.n();}
5077
5078 param_type param() const {return __p_;}
5079 void param(const param_type& __p) {__p_ = __p;}
5080
5081 result_type min() const {return 0;}
5082 result_type max() const {return numeric_limits<result_type>::infinity();}
5083
5084
5085 friend bool operator==(const chi_squared_distribution& __x,
5086 const chi_squared_distribution& __y)
5087 {return __x.__p_ == __y.__p_;}
5088 friend bool operator!=(const chi_squared_distribution& __x,
5089 const chi_squared_distribution& __y)
5090 {return !(__x == __y);}
5091};
5092
5093template <class _CharT, class _Traits, class _RT>
5094basic_ostream<_CharT, _Traits>&
5095operator<<(basic_ostream<_CharT, _Traits>& __os,
5096 const chi_squared_distribution<_RT>& __x)
5097{
5098 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005099 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5100 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005101 __os << __x.n();
5102 return __os;
5103}
5104
5105template <class _CharT, class _Traits, class _RT>
5106basic_istream<_CharT, _Traits>&
5107operator>>(basic_istream<_CharT, _Traits>& __is,
5108 chi_squared_distribution<_RT>& __x)
5109{
5110 typedef chi_squared_distribution<_RT> _Eng;
5111 typedef typename _Eng::result_type result_type;
5112 typedef typename _Eng::param_type param_type;
5113 __save_flags<_CharT, _Traits> _(__is);
5114 __is.flags(ios_base::dec | ios_base::skipws);
5115 result_type __n;
5116 __is >> __n;
5117 if (!__is.fail())
5118 __x.param(param_type(__n));
5119 return __is;
5120}
5121
Howard Hinnantd7d01132010-05-17 21:55:46 +00005122// cauchy_distribution
5123
5124template<class _RealType = double>
5125class cauchy_distribution
5126{
5127public:
5128 // types
5129 typedef _RealType result_type;
5130
5131 class param_type
5132 {
5133 result_type __a_;
5134 result_type __b_;
5135 public:
5136 typedef cauchy_distribution distribution_type;
5137
5138 explicit param_type(result_type __a = 0, result_type __b = 1)
5139 : __a_(__a), __b_(__b) {}
5140
5141 result_type a() const {return __a_;}
5142 result_type b() const {return __b_;}
5143
5144 friend bool operator==(const param_type& __x, const param_type& __y)
5145 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
5146 friend bool operator!=(const param_type& __x, const param_type& __y)
5147 {return !(__x == __y);}
5148 };
5149
5150private:
5151 param_type __p_;
5152
5153public:
5154 // constructor and reset functions
5155 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5156 : __p_(param_type(__a, __b)) {}
5157 explicit cauchy_distribution(const param_type& __p)
5158 : __p_(__p) {}
5159 void reset() {}
5160
5161 // generating functions
5162 template<class _URNG> result_type operator()(_URNG& __g)
5163 {return (*this)(__g, __p_);}
5164 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5165
5166 // property functions
5167 result_type a() const {return __p_.a();}
5168 result_type b() const {return __p_.b();}
5169
5170 param_type param() const {return __p_;}
5171 void param(const param_type& __p) {__p_ = __p;}
5172
5173 result_type min() const {return -numeric_limits<result_type>::infinity();}
5174 result_type max() const {return numeric_limits<result_type>::infinity();}
5175
5176 friend bool operator==(const cauchy_distribution& __x,
5177 const cauchy_distribution& __y)
5178 {return __x.__p_ == __y.__p_;}
5179 friend bool operator!=(const cauchy_distribution& __x,
5180 const cauchy_distribution& __y)
5181 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005182};
5183
5184template <class _RealType>
5185template<class _URNG>
5186inline
5187_RealType
5188cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5189{
5190 uniform_real_distribution<result_type> __gen;
5191 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
5192 return __p.a() + __p.b() * _STD::tan(3.1415926535897932384626433832795 * __gen(__g));
5193}
5194
5195template <class _CharT, class _Traits, class _RT>
5196basic_ostream<_CharT, _Traits>&
5197operator<<(basic_ostream<_CharT, _Traits>& __os,
5198 const cauchy_distribution<_RT>& __x)
5199{
5200 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005201 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5202 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005203 _CharT __sp = __os.widen(' ');
5204 __os.fill(__sp);
5205 __os << __x.a() << __sp << __x.b();
5206 return __os;
5207}
5208
5209template <class _CharT, class _Traits, class _RT>
5210basic_istream<_CharT, _Traits>&
5211operator>>(basic_istream<_CharT, _Traits>& __is,
5212 cauchy_distribution<_RT>& __x)
5213{
5214 typedef cauchy_distribution<_RT> _Eng;
5215 typedef typename _Eng::result_type result_type;
5216 typedef typename _Eng::param_type param_type;
5217 __save_flags<_CharT, _Traits> _(__is);
5218 __is.flags(ios_base::dec | ios_base::skipws);
5219 result_type __a;
5220 result_type __b;
5221 __is >> __a >> __b;
5222 if (!__is.fail())
5223 __x.param(param_type(__a, __b));
5224 return __is;
5225}
5226
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005227// fisher_f_distribution
5228
5229template<class _RealType = double>
5230class fisher_f_distribution
5231{
5232public:
5233 // types
5234 typedef _RealType result_type;
5235
5236 class param_type
5237 {
5238 result_type __m_;
5239 result_type __n_;
5240 public:
5241 typedef fisher_f_distribution distribution_type;
5242
5243 explicit param_type(result_type __m = 1, result_type __n = 1)
5244 : __m_(__m), __n_(__n) {}
5245
5246 result_type m() const {return __m_;}
5247 result_type n() const {return __n_;}
5248
5249 friend bool operator==(const param_type& __x, const param_type& __y)
5250 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
5251 friend bool operator!=(const param_type& __x, const param_type& __y)
5252 {return !(__x == __y);}
5253 };
5254
5255private:
5256 param_type __p_;
5257
5258public:
5259 // constructor and reset functions
5260 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5261 : __p_(param_type(__m, __n)) {}
5262 explicit fisher_f_distribution(const param_type& __p)
5263 : __p_(__p) {}
5264 void reset() {}
5265
5266 // generating functions
5267 template<class _URNG> result_type operator()(_URNG& __g)
5268 {return (*this)(__g, __p_);}
5269 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5270
5271 // property functions
5272 result_type m() const {return __p_.m();}
5273 result_type n() const {return __p_.n();}
5274
5275 param_type param() const {return __p_;}
5276 void param(const param_type& __p) {__p_ = __p;}
5277
5278 result_type min() const {return 0;}
5279 result_type max() const {return numeric_limits<result_type>::infinity();}
5280
5281 friend bool operator==(const fisher_f_distribution& __x,
5282 const fisher_f_distribution& __y)
5283 {return __x.__p_ == __y.__p_;}
5284 friend bool operator!=(const fisher_f_distribution& __x,
5285 const fisher_f_distribution& __y)
5286 {return !(__x == __y);}
5287};
5288
5289template <class _RealType>
5290template<class _URNG>
5291_RealType
5292fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5293{
5294 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5295 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5296 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5297}
5298
5299template <class _CharT, class _Traits, class _RT>
5300basic_ostream<_CharT, _Traits>&
5301operator<<(basic_ostream<_CharT, _Traits>& __os,
5302 const fisher_f_distribution<_RT>& __x)
5303{
5304 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005305 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5306 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005307 _CharT __sp = __os.widen(' ');
5308 __os.fill(__sp);
5309 __os << __x.m() << __sp << __x.n();
5310 return __os;
5311}
5312
5313template <class _CharT, class _Traits, class _RT>
5314basic_istream<_CharT, _Traits>&
5315operator>>(basic_istream<_CharT, _Traits>& __is,
5316 fisher_f_distribution<_RT>& __x)
5317{
5318 typedef fisher_f_distribution<_RT> _Eng;
5319 typedef typename _Eng::result_type result_type;
5320 typedef typename _Eng::param_type param_type;
5321 __save_flags<_CharT, _Traits> _(__is);
5322 __is.flags(ios_base::dec | ios_base::skipws);
5323 result_type __m;
5324 result_type __n;
5325 __is >> __m >> __n;
5326 if (!__is.fail())
5327 __x.param(param_type(__m, __n));
5328 return __is;
5329}
5330
Howard Hinnant551d8e42010-05-19 01:53:57 +00005331// student_t_distribution
5332
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005333template<class _RealType = double>
5334class student_t_distribution
5335{
5336public:
5337 // types
5338 typedef _RealType result_type;
5339
5340 class param_type
5341 {
5342 result_type __n_;
5343 public:
5344 typedef student_t_distribution distribution_type;
5345
5346 explicit param_type(result_type __n = 1) : __n_(__n) {}
5347
5348 result_type n() const {return __n_;}
5349
5350 friend bool operator==(const param_type& __x, const param_type& __y)
5351 {return __x.__n_ == __y.__n_;}
5352 friend bool operator!=(const param_type& __x, const param_type& __y)
5353 {return !(__x == __y);}
5354 };
5355
5356private:
5357 param_type __p_;
5358 normal_distribution<result_type> __nd_;
5359
5360public:
5361 // constructor and reset functions
5362 explicit student_t_distribution(result_type __n = 1)
5363 : __p_(param_type(__n)) {}
5364 explicit student_t_distribution(const param_type& __p)
5365 : __p_(__p) {}
5366 void reset() {__nd_.reset();}
5367
5368 // generating functions
5369 template<class _URNG> result_type operator()(_URNG& __g)
5370 {return (*this)(__g, __p_);}
5371 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5372
5373 // property functions
5374 result_type n() const {return __p_.n();}
5375
5376 param_type param() const {return __p_;}
Howard Hinnant551d8e42010-05-19 01:53:57 +00005377 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005378
5379 result_type min() const {return -numeric_limits<result_type>::infinity();}
5380 result_type max() const {return numeric_limits<result_type>::infinity();}
5381
5382 friend bool operator==(const student_t_distribution& __x,
5383 const student_t_distribution& __y)
5384 {return __x.__p_ == __y.__p_;}
5385 friend bool operator!=(const student_t_distribution& __x,
5386 const student_t_distribution& __y)
5387 {return !(__x == __y);}
5388};
5389
5390template <class _RealType>
5391template<class _URNG>
5392_RealType
5393student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5394{
5395 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
5396 return __nd_(__g) * _STD::sqrt(__p.n()/__gd(__g));
5397}
5398
5399template <class _CharT, class _Traits, class _RT>
5400basic_ostream<_CharT, _Traits>&
5401operator<<(basic_ostream<_CharT, _Traits>& __os,
5402 const student_t_distribution<_RT>& __x)
5403{
5404 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005405 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5406 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005407 __os << __x.n();
5408 return __os;
5409}
5410
5411template <class _CharT, class _Traits, class _RT>
5412basic_istream<_CharT, _Traits>&
5413operator>>(basic_istream<_CharT, _Traits>& __is,
5414 student_t_distribution<_RT>& __x)
5415{
5416 typedef student_t_distribution<_RT> _Eng;
5417 typedef typename _Eng::result_type result_type;
5418 typedef typename _Eng::param_type param_type;
5419 __save_flags<_CharT, _Traits> _(__is);
5420 __is.flags(ios_base::dec | ios_base::skipws);
5421 result_type __n;
5422 __is >> __n;
5423 if (!__is.fail())
5424 __x.param(param_type(__n));
5425 return __is;
5426}
5427
Howard Hinnant551d8e42010-05-19 01:53:57 +00005428// discrete_distribution
5429
5430template<class _IntType = int>
5431class discrete_distribution
5432{
5433public:
5434 // types
5435 typedef _IntType result_type;
5436
5437 class param_type
5438 {
5439 vector<double> __p_;
5440 public:
5441 typedef discrete_distribution distribution_type;
5442
5443 param_type() {}
5444 template<class _InputIterator>
5445 param_type(_InputIterator __f, _InputIterator __l)
5446 : __p_(__f, __l) {__init();}
5447 param_type(initializer_list<double> __wl)
5448 : __p_(__wl.begin(), __wl.end()) {__init();}
5449 template<class _UnaryOperation>
5450 param_type(size_t __nw, double __xmin, double __xmax,
5451 _UnaryOperation __fw);
5452
5453 vector<double> probabilities() const;
5454
5455 friend bool operator==(const param_type& __x, const param_type& __y)
5456 {return __x.__p_ == __y.__p_;}
5457 friend bool operator!=(const param_type& __x, const param_type& __y)
5458 {return !(__x == __y);}
5459
5460 private:
5461 void __init();
5462
5463 friend class discrete_distribution;
5464
5465 template <class _CharT, class _Traits, class _IT>
5466 friend
5467 basic_ostream<_CharT, _Traits>&
5468 operator<<(basic_ostream<_CharT, _Traits>& __os,
5469 const discrete_distribution<_IT>& __x);
5470
5471 template <class _CharT, class _Traits, class _IT>
5472 friend
5473 basic_istream<_CharT, _Traits>&
5474 operator>>(basic_istream<_CharT, _Traits>& __is,
5475 discrete_distribution<_IT>& __x);
5476 };
5477
5478private:
5479 param_type __p_;
5480
5481public:
5482 // constructor and reset functions
5483 discrete_distribution() {}
5484 template<class _InputIterator>
5485 discrete_distribution(_InputIterator __f, _InputIterator __l)
5486 : __p_(__f, __l) {}
5487 discrete_distribution(initializer_list<double> __wl)
5488 : __p_(__wl) {}
5489 template<class _UnaryOperation>
5490 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5491 _UnaryOperation __fw)
5492 : __p_(__nw, __xmin, __xmax, __fw) {}
5493 explicit discrete_distribution(const param_type& __p)
5494 : __p_(__p) {}
5495 void reset() {}
5496
5497 // generating functions
5498 template<class _URNG> result_type operator()(_URNG& __g)
5499 {return (*this)(__g, __p_);}
5500 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5501
5502 // property functions
5503 vector<double> probabilities() const {return __p_.probabilities();}
5504
5505 param_type param() const {return __p_;}
5506 void param(const param_type& __p) {__p_ = __p;}
5507
5508 result_type min() const {return 0;}
5509 result_type max() const {return __p_.__p_.size();}
5510
5511 friend bool operator==(const discrete_distribution& __x,
5512 const discrete_distribution& __y)
5513 {return __x.__p_ == __y.__p_;}
5514 friend bool operator!=(const discrete_distribution& __x,
5515 const discrete_distribution& __y)
5516 {return !(__x == __y);}
5517
5518 template <class _CharT, class _Traits, class _IT>
5519 friend
5520 basic_ostream<_CharT, _Traits>&
5521 operator<<(basic_ostream<_CharT, _Traits>& __os,
5522 const discrete_distribution<_IT>& __x);
5523
5524 template <class _CharT, class _Traits, class _IT>
5525 friend
5526 basic_istream<_CharT, _Traits>&
5527 operator>>(basic_istream<_CharT, _Traits>& __is,
5528 discrete_distribution<_IT>& __x);
5529};
5530
5531template<class _IntType>
5532template<class _UnaryOperation>
5533discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5534 double __xmin,
5535 double __xmax,
5536 _UnaryOperation __fw)
5537{
5538 if (__nw > 1)
5539 {
5540 __p_.reserve(__nw - 1);
5541 double __d = (__xmax - __xmin) / __nw;
5542 double __d2 = __d / 2;
5543 for (size_t __k = 0; __k < __nw; ++__k)
5544 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5545 __init();
5546 }
5547}
5548
5549template<class _IntType>
5550void
5551discrete_distribution<_IntType>::param_type::__init()
5552{
5553 if (!__p_.empty())
5554 {
5555 if (__p_.size() > 1)
5556 {
5557 double __s = _STD::accumulate(__p_.begin(), __p_.end(), 0.0);
5558 for (_STD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
5559 __i < __e; ++__i)
5560 *__i /= __s;
5561 vector<double> __t(__p_.size() - 1);
5562 _STD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
5563 swap(__p_, __t);
5564 }
5565 else
5566 {
5567 __p_.clear();
5568 __p_.shrink_to_fit();
5569 }
5570 }
5571}
5572
5573template<class _IntType>
5574vector<double>
5575discrete_distribution<_IntType>::param_type::probabilities() const
5576{
5577 size_t __n = __p_.size();
5578 _STD::vector<double> __p(__n+1);
5579 _STD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
5580 if (__n > 0)
5581 __p[__n] = 1 - __p_[__n-1];
5582 else
5583 __p[0] = 1;
5584 return __p;
5585}
5586
5587template<class _IntType>
5588template<class _URNG>
5589_IntType
5590discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
5591{
5592 uniform_real_distribution<double> __gen;
5593 return static_cast<_IntType>(
5594 _STD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
5595 __p.__p_.begin());
5596}
5597
5598template <class _CharT, class _Traits, class _IT>
5599basic_ostream<_CharT, _Traits>&
5600operator<<(basic_ostream<_CharT, _Traits>& __os,
5601 const discrete_distribution<_IT>& __x)
5602{
5603 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005604 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5605 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005606 _CharT __sp = __os.widen(' ');
5607 __os.fill(__sp);
5608 size_t __n = __x.__p_.__p_.size();
5609 __os << __n;
5610 for (size_t __i = 0; __i < __n; ++__i)
5611 __os << __sp << __x.__p_.__p_[__i];
5612 return __os;
5613}
5614
5615template <class _CharT, class _Traits, class _IT>
5616basic_istream<_CharT, _Traits>&
5617operator>>(basic_istream<_CharT, _Traits>& __is,
5618 discrete_distribution<_IT>& __x)
5619{
5620 typedef discrete_distribution<_IT> _Eng;
5621 typedef typename _Eng::result_type result_type;
5622 typedef typename _Eng::param_type param_type;
5623 __save_flags<_CharT, _Traits> _(__is);
5624 __is.flags(ios_base::dec | ios_base::skipws);
5625 size_t __n;
5626 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005627 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005628 for (size_t __i = 0; __i < __n; ++__i)
5629 __is >> __p[__i];
5630 if (!__is.fail())
5631 swap(__x.__p_.__p_, __p);
5632 return __is;
5633}
5634
Howard Hinnantd6d11712010-05-20 15:11:46 +00005635// piecewise_constant_distribution
5636
5637template<class _RealType = double>
5638class piecewise_constant_distribution
5639{
5640public:
5641 // types
5642 typedef _RealType result_type;
5643
5644 class param_type
5645 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005646 typedef typename common_type<double, result_type>::type __area_type;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005647 vector<result_type> __b_;
Howard Hinnant2a592542010-05-24 00:35:40 +00005648 vector<double> __densities_;
5649 vector<__area_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005650 public:
5651 typedef piecewise_constant_distribution distribution_type;
5652
5653 param_type();
5654 template<class _InputIteratorB, class _InputIteratorW>
5655 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5656 _InputIteratorW __fW);
5657 template<class _UnaryOperation>
5658 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
5659 template<class _UnaryOperation>
5660 param_type(size_t __nw, result_type __xmin, result_type __xmax,
5661 _UnaryOperation __fw);
5662
5663 vector<result_type> intervals() const {return __b_;}
Howard Hinnant2a592542010-05-24 00:35:40 +00005664 vector<double> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005665
5666 friend bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:40 +00005667 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005668 friend bool operator!=(const param_type& __x, const param_type& __y)
5669 {return !(__x == __y);}
5670
5671 private:
5672 void __init();
5673
5674 friend class piecewise_constant_distribution;
5675
5676 template <class _CharT, class _Traits, class _RT>
5677 friend
5678 basic_ostream<_CharT, _Traits>&
5679 operator<<(basic_ostream<_CharT, _Traits>& __os,
5680 const piecewise_constant_distribution<_RT>& __x);
5681
5682 template <class _CharT, class _Traits, class _RT>
5683 friend
5684 basic_istream<_CharT, _Traits>&
5685 operator>>(basic_istream<_CharT, _Traits>& __is,
5686 piecewise_constant_distribution<_RT>& __x);
5687 };
5688
5689private:
5690 param_type __p_;
5691
5692public:
5693 // constructor and reset functions
5694 piecewise_constant_distribution() {}
5695 template<class _InputIteratorB, class _InputIteratorW>
5696 piecewise_constant_distribution(_InputIteratorB __fB,
5697 _InputIteratorB __lB,
5698 _InputIteratorW __fW)
5699 : __p_(__fB, __lB, __fW) {}
5700
5701 template<class _UnaryOperation>
5702 piecewise_constant_distribution(initializer_list<result_type> __bl,
5703 _UnaryOperation __fw)
5704 : __p_(__bl, __fw) {}
5705
5706 template<class _UnaryOperation>
5707 piecewise_constant_distribution(size_t __nw, result_type __xmin,
5708 result_type __xmax, _UnaryOperation __fw)
5709 : __p_(__nw, __xmin, __xmax, __fw) {}
5710
5711 explicit piecewise_constant_distribution(const param_type& __p)
5712 : __p_(__p) {}
5713
5714 void reset() {}
5715
5716 // generating functions
5717 template<class _URNG> result_type operator()(_URNG& __g)
5718 {return (*this)(__g, __p_);}
5719 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5720
5721 // property functions
5722 vector<result_type> intervals() const {return __p_.intervals();}
5723 vector<double> densities() const {return __p_.densities();}
5724
5725 param_type param() const {return __p_;}
5726 void param(const param_type& __p) {__p_ = __p;}
5727
5728 result_type min() const {return __p_.__b_.front();}
5729 result_type max() const {return __p_.__b_.back();}
5730
5731 friend bool operator==(const piecewise_constant_distribution& __x,
5732 const piecewise_constant_distribution& __y)
5733 {return __x.__p_ == __y.__p_;}
5734 friend bool operator!=(const piecewise_constant_distribution& __x,
5735 const piecewise_constant_distribution& __y)
5736 {return !(__x == __y);}
5737
5738 template <class _CharT, class _Traits, class _RT>
5739 friend
5740 basic_ostream<_CharT, _Traits>&
5741 operator<<(basic_ostream<_CharT, _Traits>& __os,
5742 const piecewise_constant_distribution<_RT>& __x);
5743
5744 template <class _CharT, class _Traits, class _RT>
5745 friend
5746 basic_istream<_CharT, _Traits>&
5747 operator>>(basic_istream<_CharT, _Traits>& __is,
5748 piecewise_constant_distribution<_RT>& __x);
5749};
5750
5751template<class _RealType>
5752void
5753piecewise_constant_distribution<_RealType>::param_type::__init()
5754{
Howard Hinnant2a592542010-05-24 00:35:40 +00005755 // __densities_ contains non-normalized areas
5756 __area_type __total_area = _STD::accumulate(__densities_.begin(),
5757 __densities_.end(),
5758 __area_type());
5759 for (size_t __i = 0; __i < __densities_.size(); ++__i)
5760 __densities_[__i] /= __total_area;
5761 // __densities_ contains normalized areas
5762 __areas_.assign(__densities_.size(), __area_type());
5763 _STD::partial_sum(__densities_.begin(), __densities_.end() - 1,
5764 __areas_.begin() + 1);
5765 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
5766 __densities_.back() = 1 - __areas_.back(); // correct round off error
5767 for (size_t __i = 0; __i < __densities_.size(); ++__i)
5768 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
5769 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:46 +00005770}
5771
5772template<class _RealType>
5773piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:40 +00005774 : __b_(2),
5775 __densities_(1, 1.0)
Howard Hinnantd6d11712010-05-20 15:11:46 +00005776{
5777 __b_[1] = 1;
5778}
5779
5780template<class _RealType>
5781template<class _InputIteratorB, class _InputIteratorW>
5782piecewise_constant_distribution<_RealType>::param_type::param_type(
5783 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
5784 : __b_(__fB, __lB)
5785{
5786 if (__b_.size() < 2)
5787 {
5788 __b_.resize(2);
5789 __b_[0] = 0;
5790 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00005791 __densities_.assign(1, 1.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005792 }
5793 else
5794 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005795 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005796 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:40 +00005797 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005798 __init();
5799 }
5800}
5801
5802template<class _RealType>
5803template<class _UnaryOperation>
5804piecewise_constant_distribution<_RealType>::param_type::param_type(
5805 initializer_list<result_type> __bl, _UnaryOperation __fw)
5806 : __b_(__bl.begin(), __bl.end())
5807{
5808 if (__b_.size() < 2)
5809 {
5810 __b_.resize(2);
5811 __b_[0] = 0;
5812 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00005813 __densities_.assign(1, 1.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005814 }
5815 else
5816 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005817 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005818 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005819 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00005820 __init();
5821 }
5822}
5823
5824template<class _RealType>
5825template<class _UnaryOperation>
5826piecewise_constant_distribution<_RealType>::param_type::param_type(
5827 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
5828 : __b_(__nw == 0 ? 2 : __nw + 1)
5829{
5830 size_t __n = __b_.size() - 1;
5831 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:40 +00005832 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005833 for (size_t __i = 0; __i < __n; ++__i)
5834 {
5835 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:40 +00005836 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00005837 }
5838 __b_[__n] = __xmax;
5839 __init();
5840}
5841
5842template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00005843template<class _URNG>
5844_RealType
5845piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5846{
5847 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005848 result_type __u = _Gen()(__g);
Howard Hinnant2a592542010-05-24 00:35:40 +00005849 ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
5850 static_cast<double>(__u)) - __p.__areas_.begin() - 1;
5851 return static_cast<result_type>((__u - __p.__areas_[__k]) / __p.__densities_[__k]
5852 + __p.__b_[__k]);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005853}
5854
5855template <class _CharT, class _Traits, class _RT>
5856basic_ostream<_CharT, _Traits>&
5857operator<<(basic_ostream<_CharT, _Traits>& __os,
5858 const piecewise_constant_distribution<_RT>& __x)
5859{
5860 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005861 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5862 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005863 _CharT __sp = __os.widen(' ');
5864 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:40 +00005865 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00005866 __os << __n;
5867 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005868 __os << __sp << __x.__p_.__b_[__i];
5869 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00005870 __os << __sp << __n;
5871 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005872 __os << __sp << __x.__p_.__densities_[__i];
5873 __n = __x.__p_.__areas_.size();
5874 __os << __sp << __n;
5875 for (size_t __i = 0; __i < __n; ++__i)
5876 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00005877 return __os;
5878}
5879
5880template <class _CharT, class _Traits, class _RT>
5881basic_istream<_CharT, _Traits>&
5882operator>>(basic_istream<_CharT, _Traits>& __is,
5883 piecewise_constant_distribution<_RT>& __x)
5884{
5885 typedef piecewise_constant_distribution<_RT> _Eng;
5886 typedef typename _Eng::result_type result_type;
5887 typedef typename _Eng::param_type param_type;
Howard Hinnant2a592542010-05-24 00:35:40 +00005888 typedef typename param_type::__area_type __area_type;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005889 __save_flags<_CharT, _Traits> _(__is);
5890 __is.flags(ios_base::dec | ios_base::skipws);
5891 size_t __n;
5892 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005893 vector<result_type> __b(__n);
5894 for (size_t __i = 0; __i < __n; ++__i)
5895 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:40 +00005896 __is >> __n;
5897 vector<double> __densities(__n);
5898 for (size_t __i = 0; __i < __n; ++__i)
5899 __is >> __densities[__i];
5900 __is >> __n;
5901 vector<__area_type> __areas(__n);
5902 for (size_t __i = 0; __i < __n; ++__i)
5903 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00005904 if (!__is.fail())
5905 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00005906 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:40 +00005907 swap(__x.__p_.__densities_, __densities);
5908 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005909 }
5910 return __is;
5911}
5912
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005913_LIBCPP_END_NAMESPACE_STD
5914
5915#endif // _LIBCPP_RANDOM