blob: 5de738ad907b83418d7074768872ac400741162a [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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000488
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489 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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000547
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548 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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000602
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603 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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000660
Howard Hinnant03aad812010-05-11 23:26:59 +0000661 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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000716
Howard Hinnant34e8a572010-05-17 13:44:27 +0000717 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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000774
Howard Hinnantf2fe5d52010-05-17 00:09:38 +0000775 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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000830
Howard Hinnant4ff556c2010-05-14 21:38:54 +0000831 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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000886
Howard Hinnant30a840f2010-05-12 17:08:57 +0000887 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);
Howard Hinnant324bb032010-08-22 00:02:43 +0000944
Howard Hinnantc7c49132010-05-13 17:58:28 +0000945 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);
Howard Hinnant324bb032010-08-22 00:02:43 +00001002
Howard Hinnant9de6e302010-05-16 01:09:02 +00001003 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);
Howard Hinnant324bb032010-08-22 00:02:43 +00001060
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00001061 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);
Howard Hinnant324bb032010-08-22 00:02:43 +00001118
Howard Hinnanta64111c2010-05-12 21:02:31 +00001119 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);
Howard Hinnant324bb032010-08-22 00:02:43 +00001176
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00001177 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);
Howard Hinnant324bb032010-08-22 00:02:43 +00001232
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001233 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);
Howard Hinnant324bb032010-08-22 00:02:43 +00001290
Howard Hinnantd7d01132010-05-17 21:55:46 +00001291 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);
Howard Hinnant324bb032010-08-22 00:02:43 +00001348
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001349 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);
Howard Hinnant324bb032010-08-22 00:02:43 +00001404
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001405 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);
Howard Hinnant324bb032010-08-22 00:02:43 +00001471
Howard Hinnant551d8e42010-05-19 01:53:57 +00001472 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);
Howard Hinnant324bb032010-08-22 00:02:43 +00001546
Howard Hinnantd6d11712010-05-20 15:11:46 +00001547 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>
Howard Hinnant54305402010-05-25 00:27:34 +00001555class piecewise_linear_distribution
1556{
1557 // types
1558 typedef RealType result_type;
1559
1560 class param_type
1561 {
1562 public:
1563 typedef piecewise_linear_distribution distribution_type;
1564
1565 param_type();
1566 template<class InputIteratorB, class InputIteratorW>
1567 param_type(InputIteratorB firstB, InputIteratorB lastB,
1568 InputIteratorW firstW);
1569 template<class UnaryOperation>
1570 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1571 template<class UnaryOperation>
1572 param_type(size_t nw, result_type xmin, result_type xmax,
1573 UnaryOperation fw);
1574
1575 vector<result_type> intervals() const;
1576 vector<double> densities() const;
1577
1578 friend bool operator==(const param_type& x, const param_type& y);
1579 friend bool operator!=(const param_type& x, const param_type& y);
1580 };
1581
1582 // constructor and reset functions
1583 piecewise_linear_distribution();
1584 template<class InputIteratorB, class InputIteratorW>
1585 piecewise_linear_distribution(InputIteratorB firstB,
1586 InputIteratorB lastB,
1587 InputIteratorW firstW);
Howard Hinnant324bb032010-08-22 00:02:43 +00001588
Howard Hinnant54305402010-05-25 00:27:34 +00001589 template<class UnaryOperation>
1590 piecewise_linear_distribution(initializer_list<result_type> bl,
1591 UnaryOperation fw);
1592
1593 template<class UnaryOperation>
1594 piecewise_linear_distribution(size_t nw, result_type xmin,
1595 result_type xmax, UnaryOperation fw);
1596
1597 explicit piecewise_linear_distribution(const param_type& parm);
1598 void reset();
1599
1600 // generating functions
1601 template<class URNG> result_type operator()(URNG& g);
1602 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1603
1604 // property functions
1605 vector<result_type> intervals() const;
1606 vector<double> densities() const;
1607
1608 param_type param() const;
1609 void param(const param_type& parm);
1610
1611 result_type min() const;
1612 result_type max() const;
1613
1614 friend bool operator==(const piecewise_linear_distribution& x,
1615 const piecewise_linear_distribution& y);
1616 friend bool operator!=(const piecewise_linear_distribution& x,
1617 const piecewise_linear_distribution& y);
1618
1619 template <class charT, class traits>
1620 friend
1621 basic_ostream<charT, traits>&
1622 operator<<(basic_ostream<charT, traits>& os,
1623 const piecewise_linear_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001624
Howard Hinnant54305402010-05-25 00:27:34 +00001625 template <class charT, class traits>
1626 friend
1627 basic_istream<charT, traits>&
1628 operator>>(basic_istream<charT, traits>& is,
1629 piecewise_linear_distribution& x);
1630};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631
1632} // std
1633*/
1634
1635#include <__config>
1636#include <cstddef>
1637#include <type_traits>
1638#include <initializer_list>
1639#include <cstdint>
1640#include <limits>
1641#include <algorithm>
Howard Hinnant551d8e42010-05-19 01:53:57 +00001642#include <numeric>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643#include <vector>
1644#include <string>
1645#include <istream>
1646#include <ostream>
Howard Hinnant30a840f2010-05-12 17:08:57 +00001647#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648
1649#pragma GCC system_header
1650
1651_LIBCPP_BEGIN_NAMESPACE_STD
1652
1653// linear_congruential_engine
1654
1655template <unsigned long long __a, unsigned long long __c,
1656 unsigned long long __m, unsigned long long _M,
1657 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
1658struct __lce_ta;
1659
1660// 64
1661
1662template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1663struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1664{
1665 typedef unsigned long long result_type;
1666 static result_type next(result_type __x)
1667 {
1668 // Schrage's algorithm
1669 const result_type __q = __m / __a;
1670 const result_type __r = __m % __a;
1671 const result_type __t0 = __a * (__x % __q);
1672 const result_type __t1 = __r * (__x / __q);
1673 __x = __t0 + (__t0 < __t1) * __m - __t1;
1674 __x += __c - (__x >= __m - __c) * __m;
1675 return __x;
1676 }
1677};
1678
1679template <unsigned long long __a, unsigned long long __m>
1680struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1681{
1682 typedef unsigned long long result_type;
1683 static result_type next(result_type __x)
1684 {
1685 // Schrage's algorithm
1686 const result_type __q = __m / __a;
1687 const result_type __r = __m % __a;
1688 const result_type __t0 = __a * (__x % __q);
1689 const result_type __t1 = __r * (__x / __q);
1690 __x = __t0 + (__t0 < __t1) * __m - __t1;
1691 return __x;
1692 }
1693};
1694
1695template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1696struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1697{
1698 typedef unsigned long long result_type;
1699 static result_type next(result_type __x)
1700 {
1701 return (__a * __x + __c) % __m;
1702 }
1703};
1704
1705template <unsigned long long __a, unsigned long long __c>
1706struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1707{
1708 typedef unsigned long long result_type;
1709 static result_type next(result_type __x)
1710 {
1711 return __a * __x + __c;
1712 }
1713};
1714
1715// 32
1716
1717template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1718struct __lce_ta<_A, _C, _M, unsigned(~0), true>
1719{
1720 typedef unsigned result_type;
1721 static result_type next(result_type __x)
1722 {
1723 const result_type __a = static_cast<result_type>(_A);
1724 const result_type __c = static_cast<result_type>(_C);
1725 const result_type __m = static_cast<result_type>(_M);
1726 // Schrage's algorithm
1727 const result_type __q = __m / __a;
1728 const result_type __r = __m % __a;
1729 const result_type __t0 = __a * (__x % __q);
1730 const result_type __t1 = __r * (__x / __q);
1731 __x = __t0 + (__t0 < __t1) * __m - __t1;
1732 __x += __c - (__x >= __m - __c) * __m;
1733 return __x;
1734 }
1735};
1736
1737template <unsigned long long _A, unsigned long long _M>
1738struct __lce_ta<_A, 0, _M, unsigned(~0), true>
1739{
1740 typedef unsigned result_type;
1741 static result_type next(result_type __x)
1742 {
1743 const result_type __a = static_cast<result_type>(_A);
1744 const result_type __m = static_cast<result_type>(_M);
1745 // Schrage's algorithm
1746 const result_type __q = __m / __a;
1747 const result_type __r = __m % __a;
1748 const result_type __t0 = __a * (__x % __q);
1749 const result_type __t1 = __r * (__x / __q);
1750 __x = __t0 + (__t0 < __t1) * __m - __t1;
1751 return __x;
1752 }
1753};
1754
1755template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1756struct __lce_ta<_A, _C, _M, unsigned(~0), false>
1757{
1758 typedef unsigned result_type;
1759 static result_type next(result_type __x)
1760 {
1761 const result_type __a = static_cast<result_type>(_A);
1762 const result_type __c = static_cast<result_type>(_C);
1763 const result_type __m = static_cast<result_type>(_M);
1764 return (__a * __x + __c) % __m;
1765 }
1766};
1767
1768template <unsigned long long _A, unsigned long long _C>
1769struct __lce_ta<_A, _C, 0, unsigned(~0), false>
1770{
1771 typedef unsigned result_type;
1772 static result_type next(result_type __x)
1773 {
1774 const result_type __a = static_cast<result_type>(_A);
1775 const result_type __c = static_cast<result_type>(_C);
1776 return __a * __x + __c;
1777 }
1778};
1779
1780// 16
1781
1782template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1783struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1784{
1785 typedef unsigned short result_type;
1786 static result_type next(result_type __x)
1787 {
1788 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1789 }
1790};
1791
1792template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1793class linear_congruential_engine;
1794
1795template <class _CharT, class _Traits,
1796 class _U, _U _A, _U _C, _U _N>
1797basic_ostream<_CharT, _Traits>&
1798operator<<(basic_ostream<_CharT, _Traits>& __os,
1799 const linear_congruential_engine<_U, _A, _C, _N>&);
1800
1801template <class _CharT, class _Traits,
1802 class _U, _U _A, _U _C, _U _N>
1803basic_istream<_CharT, _Traits>&
1804operator>>(basic_istream<_CharT, _Traits>& __is,
1805 linear_congruential_engine<_U, _A, _C, _N>& __x);
1806
1807template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1808class linear_congruential_engine
1809{
1810public:
1811 // types
1812 typedef _UIntType result_type;
1813
1814private:
1815 result_type __x_;
1816
1817 static const result_type _M = result_type(~0);
1818
1819 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1820 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1821public:
1822 static const result_type _Min = __c == 0u ? 1u: 0u;
1823 static const result_type _Max = __m - 1u;
1824 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1825
1826 // engine characteristics
1827 static const/*expr*/ result_type multiplier = __a;
1828 static const/*expr*/ result_type increment = __c;
1829 static const/*expr*/ result_type modulus = __m;
1830 static const/*expr*/ result_type min() {return _Min;}
1831 static const/*expr*/ result_type max() {return _Max;}
1832 static const/*expr*/ result_type default_seed = 1u;
1833
1834 // constructors and seeding functions
1835 explicit linear_congruential_engine(result_type __s = default_seed)
1836 {seed(__s);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00001837 template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q,
1838 typename enable_if<!is_convertible<_Sseq, result_type>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839 {seed(__q);}
1840 void seed(result_type __s = default_seed)
1841 {seed(integral_constant<bool, __m == 0>(),
1842 integral_constant<bool, __c == 0>(), __s);}
1843 template<class _Sseq>
1844 typename enable_if
1845 <
1846 !is_convertible<_Sseq, result_type>::value,
1847 void
1848 >::type
1849 seed(_Sseq& __q)
1850 {__seed(__q, integral_constant<unsigned,
1851 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1852 : (__m-1) / 0x100000000ull)>());}
1853
1854 // generating functions
1855 result_type operator()()
1856 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
1857 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1858
1859 friend bool operator==(const linear_congruential_engine& __x,
1860 const linear_congruential_engine& __y)
1861 {return __x.__x_ == __y.__x_;}
1862 friend bool operator!=(const linear_congruential_engine& __x,
1863 const linear_congruential_engine& __y)
1864 {return !(__x == __y);}
1865
1866private:
1867
1868 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
1869 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
1870 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1871 1 : __s % __m;}
1872 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1873
1874 template<class _Sseq>
1875 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1876 template<class _Sseq>
1877 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1878
1879 template <class _CharT, class _Traits,
1880 class _U, _U _A, _U _C, _U _N>
1881 friend
1882 basic_ostream<_CharT, _Traits>&
1883 operator<<(basic_ostream<_CharT, _Traits>& __os,
1884 const linear_congruential_engine<_U, _A, _C, _N>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001885
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886 template <class _CharT, class _Traits,
1887 class _U, _U _A, _U _C, _U _N>
1888 friend
1889 basic_istream<_CharT, _Traits>&
1890 operator>>(basic_istream<_CharT, _Traits>& __is,
1891 linear_congruential_engine<_U, _A, _C, _N>& __x);
1892};
1893
1894template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1895template<class _Sseq>
1896void
1897linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1898 integral_constant<unsigned, 1>)
1899{
1900 const unsigned __k = 1;
1901 uint32_t __ar[__k+3];
1902 __q.generate(__ar, __ar + __k + 3);
1903 result_type __s = static_cast<result_type>(__ar[3] % __m);
1904 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1905}
1906
1907template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1908template<class _Sseq>
1909void
1910linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1911 integral_constant<unsigned, 2>)
1912{
1913 const unsigned __k = 2;
1914 uint32_t __ar[__k+3];
1915 __q.generate(__ar, __ar + __k + 3);
1916 result_type __s = static_cast<result_type>((__ar[3] +
1917 (uint64_t)__ar[4] << 32) % __m);
1918 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1919}
1920
1921template <class _CharT, class _Traits>
1922class __save_flags
1923{
1924 typedef basic_ios<_CharT, _Traits> __stream_type;
1925 typedef typename __stream_type::fmtflags fmtflags;
1926
1927 __stream_type& __stream_;
1928 fmtflags __fmtflags_;
1929 _CharT __fill_;
1930
1931 __save_flags(const __save_flags&);
1932 __save_flags& operator=(const __save_flags&);
1933public:
1934 explicit __save_flags(__stream_type& __stream)
1935 : __stream_(__stream),
1936 __fmtflags_(__stream.flags()),
1937 __fill_(__stream.fill())
1938 {}
1939 ~__save_flags()
1940 {
1941 __stream_.flags(__fmtflags_);
1942 __stream_.fill(__fill_);
1943 }
1944};
1945
1946template <class _CharT, class _Traits,
1947 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1948inline
1949basic_ostream<_CharT, _Traits>&
1950operator<<(basic_ostream<_CharT, _Traits>& __os,
1951 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1952{
1953 __save_flags<_CharT, _Traits> _(__os);
1954 __os.flags(ios_base::dec | ios_base::left);
1955 __os.fill(__os.widen(' '));
1956 return __os << __x.__x_;
1957}
1958
1959template <class _CharT, class _Traits,
1960 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1961basic_istream<_CharT, _Traits>&
1962operator>>(basic_istream<_CharT, _Traits>& __is,
1963 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1964{
1965 __save_flags<_CharT, _Traits> _(__is);
1966 __is.flags(ios_base::dec | ios_base::skipws);
1967 _UIntType __t;
1968 __is >> __t;
1969 if (!__is.fail())
1970 __x.__x_ = __t;
1971 return __is;
1972}
1973
1974typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
1975 minstd_rand0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
1977 minstd_rand;
Howard Hinnantd6d11712010-05-20 15:11:46 +00001978typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979// mersenne_twister_engine
1980
1981template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1982 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1983 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1984class mersenne_twister_engine;
1985
1986template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1987 _UI _A, size_t _U, _UI _D, size_t _S,
1988 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1989bool
1990operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1991 _B, _T, _C, _L, _F>& __x,
1992 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1993 _B, _T, _C, _L, _F>& __y);
1994
1995template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1996 _UI _A, size_t _U, _UI _D, size_t _S,
1997 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1998bool
1999operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2000 _B, _T, _C, _L, _F>& __x,
2001 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2002 _B, _T, _C, _L, _F>& __y);
2003
2004template <class _CharT, class _Traits,
2005 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2006 _UI _A, size_t _U, _UI _D, size_t _S,
2007 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2008basic_ostream<_CharT, _Traits>&
2009operator<<(basic_ostream<_CharT, _Traits>& __os,
2010 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2011 _B, _T, _C, _L, _F>& __x);
2012
2013template <class _CharT, class _Traits,
2014 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2015 _UI _A, size_t _U, _UI _D, size_t _S,
2016 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2017basic_istream<_CharT, _Traits>&
2018operator>>(basic_istream<_CharT, _Traits>& __is,
2019 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2020 _B, _T, _C, _L, _F>& __x);
2021
2022template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2023 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2024 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2025class mersenne_twister_engine
2026{
2027public:
2028 // types
2029 typedef _UIntType result_type;
2030
2031private:
2032 result_type __x_[__n];
2033 size_t __i_;
2034
2035 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2036 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
2037 static const result_type _Dt = numeric_limits<result_type>::digits;
2038 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2039 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2040 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2041 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2042 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2043 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2044 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2045public:
2046 static const result_type _Min = 0;
2047 static const result_type _Max = __w == _Dt ? result_type(~0) :
2048 (result_type(1) << __w) - result_type(1);
2049 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2050 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2051 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2052 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2053 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2054 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2055
2056 // engine characteristics
2057 static const/*expr*/ size_t word_size = __w;
2058 static const/*expr*/ size_t state_size = __n;
2059 static const/*expr*/ size_t shift_size = __m;
2060 static const/*expr*/ size_t mask_bits = __r;
2061 static const/*expr*/ result_type xor_mask = __a;
2062 static const/*expr*/ size_t tempering_u = __u;
2063 static const/*expr*/ result_type tempering_d = __d;
2064 static const/*expr*/ size_t tempering_s = __s;
2065 static const/*expr*/ result_type tempering_b = __b;
2066 static const/*expr*/ size_t tempering_t = __t;
2067 static const/*expr*/ result_type tempering_c = __c;
2068 static const/*expr*/ size_t tempering_l = __l;
2069 static const/*expr*/ result_type initialization_multiplier = __f;
2070 static const/*expr*/ result_type min() { return _Min; }
2071 static const/*expr*/ result_type max() { return _Max; }
2072 static const/*expr*/ result_type default_seed = 5489u;
2073
2074 // constructors and seeding functions
2075 explicit mersenne_twister_engine(result_type __sd = default_seed)
2076 {seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002077 template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q,
2078 typename enable_if<!is_convertible<_Sseq, result_type>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079 {seed(__q);}
2080 void seed(result_type __sd = default_seed);
2081 template<class _Sseq>
2082 typename enable_if
2083 <
2084 !is_convertible<_Sseq, result_type>::value,
2085 void
2086 >::type
2087 seed(_Sseq& __q)
2088 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2089
2090 // generating functions
2091 result_type operator()();
2092 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2093
2094 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2095 _UI _A, size_t _U, _UI _D, size_t _S,
2096 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2097 friend
2098 bool
2099 operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2100 _B, _T, _C, _L, _F>& __x,
2101 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2102 _B, _T, _C, _L, _F>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002103
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002104 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2105 _UI _A, size_t _U, _UI _D, size_t _S,
2106 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2107 friend
2108 bool
2109 operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2110 _B, _T, _C, _L, _F>& __x,
2111 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2112 _B, _T, _C, _L, _F>& __y);
2113
2114 template <class _CharT, class _Traits,
2115 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2116 _UI _A, size_t _U, _UI _D, size_t _S,
2117 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2118 friend
2119 basic_ostream<_CharT, _Traits>&
2120 operator<<(basic_ostream<_CharT, _Traits>& __os,
2121 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2122 _B, _T, _C, _L, _F>& __x);
2123
2124 template <class _CharT, class _Traits,
2125 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2126 _UI _A, size_t _U, _UI _D, size_t _S,
2127 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2128 friend
2129 basic_istream<_CharT, _Traits>&
2130 operator>>(basic_istream<_CharT, _Traits>& __is,
2131 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2132 _B, _T, _C, _L, _F>& __x);
2133private:
2134
2135 template<class _Sseq>
2136 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2137 template<class _Sseq>
2138 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2139
2140 template <size_t __count>
2141 static
2142 typename enable_if
2143 <
2144 __count < __w,
2145 result_type
2146 >::type
2147 __lshift(result_type __x) {return (__x << __count) & _Max;}
2148
2149 template <size_t __count>
2150 static
2151 typename enable_if
2152 <
2153 (__count >= __w),
2154 result_type
2155 >::type
2156 __lshift(result_type __x) {return result_type(0);}
2157
2158 template <size_t __count>
2159 static
2160 typename enable_if
2161 <
2162 __count < _Dt,
2163 result_type
2164 >::type
2165 __rshift(result_type __x) {return __x >> __count;}
2166
2167 template <size_t __count>
2168 static
2169 typename enable_if
2170 <
2171 (__count >= _Dt),
2172 result_type
2173 >::type
2174 __rshift(result_type __x) {return result_type(0);}
2175};
2176
2177template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2178 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2179 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2180void
2181mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2182 __t, __c, __l, __f>::seed(result_type __sd)
2183{ // __w >= 2
2184 __x_[0] = __sd & _Max;
2185 for (size_t __i = 1; __i < __n; ++__i)
2186 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2187 __i_ = 0;
2188}
2189
2190template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2191 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2192 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2193template<class _Sseq>
2194void
2195mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2196 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2197{
2198 const unsigned __k = 1;
2199 uint32_t __ar[__n * __k];
2200 __q.generate(__ar, __ar + __n * __k);
2201 for (size_t __i = 0; __i < __n; ++__i)
2202 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2203 const result_type __mask = __r == _Dt ? result_type(~0) :
2204 (result_type(1) << __r) - result_type(1);
2205 __i_ = 0;
2206 if ((__x_[0] & ~__mask) == 0)
2207 {
2208 for (size_t __i = 1; __i < __n; ++__i)
2209 if (__x_[__i] != 0)
2210 return;
2211 __x_[0] = _Max;
2212 }
2213}
2214
2215template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2216 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2217 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2218template<class _Sseq>
2219void
2220mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2221 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2222{
2223 const unsigned __k = 2;
2224 uint32_t __ar[__n * __k];
2225 __q.generate(__ar, __ar + __n * __k);
2226 for (size_t __i = 0; __i < __n; ++__i)
2227 __x_[__i] = static_cast<result_type>(
2228 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2229 const result_type __mask = __r == _Dt ? result_type(~0) :
2230 (result_type(1) << __r) - result_type(1);
2231 __i_ = 0;
2232 if ((__x_[0] & ~__mask) == 0)
2233 {
2234 for (size_t __i = 1; __i < __n; ++__i)
2235 if (__x_[__i] != 0)
2236 return;
2237 __x_[0] = _Max;
2238 }
2239}
2240
2241template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2242 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2243 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2244_UIntType
2245mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2246 __t, __c, __l, __f>::operator()()
2247{
2248 const size_t __j = (__i_ + 1) % __n;
2249 const result_type __mask = __r == _Dt ? result_type(~0) :
2250 (result_type(1) << __r) - result_type(1);
2251 const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
2252 const size_t __k = (__i_ + __m) % __n;
2253 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
2254 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2255 __i_ = __j;
2256 __z ^= __lshift<__s>(__z) & __b;
2257 __z ^= __lshift<__t>(__z) & __c;
2258 return __z ^ __rshift<__l>(__z);
2259}
2260
2261template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2262 _UI _A, size_t _U, _UI _D, size_t _S,
2263 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2264bool
2265operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2266 _B, _T, _C, _L, _F>& __x,
2267 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2268 _B, _T, _C, _L, _F>& __y)
2269{
2270 if (__x.__i_ == __y.__i_)
2271 return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
2272 if (__x.__i_ == 0 || __y.__i_ == 0)
2273 {
2274 size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
2275 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2276 __y.__x_ + __y.__i_))
2277 return false;
2278 if (__x.__i_ == 0)
2279 return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
2280 return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
2281 }
2282 if (__x.__i_ < __y.__i_)
2283 {
2284 size_t __j = _N - __y.__i_;
2285 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2286 __y.__x_ + __y.__i_))
2287 return false;
2288 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
2289 __y.__x_))
2290 return false;
2291 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
2292 __y.__x_ + (_N - (__x.__i_ + __j)));
2293 }
2294 size_t __j = _N - __x.__i_;
2295 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2296 __x.__x_ + __x.__i_))
2297 return false;
2298 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
2299 __x.__x_))
2300 return false;
2301 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
2302 __x.__x_ + (_N - (__y.__i_ + __j)));
2303}
2304
2305template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2306 _UI _A, size_t _U, _UI _D, size_t _S,
2307 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2308inline
2309bool
2310operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2311 _B, _T, _C, _L, _F>& __x,
2312 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2313 _B, _T, _C, _L, _F>& __y)
2314{
2315 return !(__x == __y);
2316}
2317
2318template <class _CharT, class _Traits,
2319 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2320 _UI _A, size_t _U, _UI _D, size_t _S,
2321 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2322basic_ostream<_CharT, _Traits>&
2323operator<<(basic_ostream<_CharT, _Traits>& __os,
2324 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2325 _B, _T, _C, _L, _F>& __x)
2326{
2327 __save_flags<_CharT, _Traits> _(__os);
2328 __os.flags(ios_base::dec | ios_base::left);
2329 _CharT __sp = __os.widen(' ');
2330 __os.fill(__sp);
2331 __os << __x.__x_[__x.__i_];
2332 for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
2333 __os << __sp << __x.__x_[__j];
2334 for (size_t __j = 0; __j < __x.__i_; ++__j)
2335 __os << __sp << __x.__x_[__j];
2336 return __os;
2337}
2338
2339template <class _CharT, class _Traits,
2340 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2341 _UI _A, size_t _U, _UI _D, size_t _S,
2342 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2343basic_istream<_CharT, _Traits>&
2344operator>>(basic_istream<_CharT, _Traits>& __is,
2345 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2346 _B, _T, _C, _L, _F>& __x)
2347{
2348 __save_flags<_CharT, _Traits> _(__is);
2349 __is.flags(ios_base::dec | ios_base::skipws);
2350 _UI __t[_N];
2351 for (size_t __i = 0; __i < _N; ++__i)
2352 __is >> __t[__i];
2353 if (!__is.fail())
2354 {
2355 for (size_t __i = 0; __i < _N; ++__i)
2356 __x.__x_[__i] = __t[__i];
2357 __x.__i_ = 0;
2358 }
2359 return __is;
2360}
2361
2362typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2363 0x9908b0df, 11, 0xffffffff,
2364 7, 0x9d2c5680,
2365 15, 0xefc60000,
2366 18, 1812433253> mt19937;
2367typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2368 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2369 17, 0x71d67fffeda60000ULL,
2370 37, 0xfff7eee000000000ULL,
2371 43, 6364136223846793005ULL> mt19937_64;
2372
2373// subtract_with_carry_engine
2374
2375template<class _UIntType, size_t __w, size_t __s, size_t __r>
2376class subtract_with_carry_engine;
2377
2378template<class _UI, size_t _W, size_t _S, size_t _R>
2379bool
2380operator==(
2381 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2382 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2383
2384template<class _UI, size_t _W, size_t _S, size_t _R>
2385bool
2386operator!=(
2387 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2388 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2389
2390template <class _CharT, class _Traits,
2391 class _UI, size_t _W, size_t _S, size_t _R>
2392basic_ostream<_CharT, _Traits>&
2393operator<<(basic_ostream<_CharT, _Traits>& __os,
2394 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2395
2396template <class _CharT, class _Traits,
2397 class _UI, size_t _W, size_t _S, size_t _R>
2398basic_istream<_CharT, _Traits>&
2399operator>>(basic_istream<_CharT, _Traits>& __is,
2400 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2401
2402template<class _UIntType, size_t __w, size_t __s, size_t __r>
2403class subtract_with_carry_engine
2404{
2405public:
2406 // types
2407 typedef _UIntType result_type;
2408
2409private:
2410 result_type __x_[__r];
2411 result_type __c_;
2412 size_t __i_;
2413
2414 static const result_type _Dt = numeric_limits<result_type>::digits;
2415 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2416 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2417 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2418 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2419public:
2420 static const result_type _Min = 0;
2421 static const result_type _Max = __w == _Dt ? result_type(~0) :
2422 (result_type(1) << __w) - result_type(1);
2423 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2424
2425 // engine characteristics
2426 static const/*expr*/ size_t word_size = __w;
2427 static const/*expr*/ size_t short_lag = __s;
2428 static const/*expr*/ size_t long_lag = __r;
2429 static const/*expr*/ result_type min() { return _Min; }
2430 static const/*expr*/ result_type max() { return _Max; }
2431 static const/*expr*/ result_type default_seed = 19780503u;
2432
2433 // constructors and seeding functions
2434 explicit subtract_with_carry_engine(result_type __sd = default_seed)
2435 {seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002436 template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q,
2437 typename enable_if<!is_convertible<_Sseq, result_type>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002438 {seed(__q);}
2439 void seed(result_type __sd = default_seed)
2440 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2441 template<class _Sseq>
2442 typename enable_if
2443 <
2444 !is_convertible<_Sseq, result_type>::value,
2445 void
2446 >::type
2447 seed(_Sseq& __q)
2448 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2449
2450 // generating functions
2451 result_type operator()();
2452 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2453
2454 template<class _UI, size_t _W, size_t _S, size_t _R>
2455 friend
2456 bool
2457 operator==(
2458 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2459 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002460
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002461 template<class _UI, size_t _W, size_t _S, size_t _R>
2462 friend
2463 bool
2464 operator!=(
2465 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2466 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002467
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002468 template <class _CharT, class _Traits,
2469 class _UI, size_t _W, size_t _S, size_t _R>
2470 friend
2471 basic_ostream<_CharT, _Traits>&
2472 operator<<(basic_ostream<_CharT, _Traits>& __os,
2473 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002474
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002475 template <class _CharT, class _Traits,
2476 class _UI, size_t _W, size_t _S, size_t _R>
2477 friend
2478 basic_istream<_CharT, _Traits>&
2479 operator>>(basic_istream<_CharT, _Traits>& __is,
2480 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2481
2482private:
2483
2484 void seed(result_type __sd, integral_constant<unsigned, 1>);
2485 void seed(result_type __sd, integral_constant<unsigned, 2>);
2486 template<class _Sseq>
2487 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2488 template<class _Sseq>
2489 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2490};
2491
2492template<class _UIntType, size_t __w, size_t __s, size_t __r>
2493void
2494subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2495 integral_constant<unsigned, 1>)
2496{
2497 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2498 __e(__sd == 0u ? default_seed : __sd);
2499 for (size_t __i = 0; __i < __r; ++__i)
2500 __x_[__i] = static_cast<result_type>(__e() & _Max);
2501 __c_ = __x_[__r-1] == 0;
2502 __i_ = 0;
2503}
2504
2505template<class _UIntType, size_t __w, size_t __s, size_t __r>
2506void
2507subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2508 integral_constant<unsigned, 2>)
2509{
2510 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2511 __e(__sd == 0u ? default_seed : __sd);
2512 for (size_t __i = 0; __i < __r; ++__i)
2513 __x_[__i] = static_cast<result_type>(
2514 (__e() + ((uint64_t)__e() << 32)) & _Max);
2515 __c_ = __x_[__r-1] == 0;
2516 __i_ = 0;
2517}
2518
2519template<class _UIntType, size_t __w, size_t __s, size_t __r>
2520template<class _Sseq>
2521void
2522subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2523 integral_constant<unsigned, 1>)
2524{
2525 const unsigned __k = 1;
2526 uint32_t __ar[__r * __k];
2527 __q.generate(__ar, __ar + __r * __k);
2528 for (size_t __i = 0; __i < __r; ++__i)
2529 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2530 __c_ = __x_[__r-1] == 0;
2531 __i_ = 0;
2532}
2533
2534template<class _UIntType, size_t __w, size_t __s, size_t __r>
2535template<class _Sseq>
2536void
2537subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2538 integral_constant<unsigned, 2>)
2539{
2540 const unsigned __k = 2;
2541 uint32_t __ar[__r * __k];
2542 __q.generate(__ar, __ar + __r * __k);
2543 for (size_t __i = 0; __i < __r; ++__i)
2544 __x_[__i] = static_cast<result_type>(
2545 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2546 __c_ = __x_[__r-1] == 0;
2547 __i_ = 0;
2548}
2549
2550template<class _UIntType, size_t __w, size_t __s, size_t __r>
2551_UIntType
2552subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2553{
2554 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2555 result_type& __xr = __x_[__i_];
2556 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2557 __xr = (__xs - __xr - __c_) & _Max;
2558 __c_ = __new_c;
2559 __i_ = (__i_ + 1) % __r;
2560 return __xr;
2561}
2562
2563template<class _UI, size_t _W, size_t _S, size_t _R>
2564bool
2565operator==(
2566 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2567 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2568{
2569 if (__x.__c_ != __y.__c_)
2570 return false;
2571 if (__x.__i_ == __y.__i_)
2572 return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
2573 if (__x.__i_ == 0 || __y.__i_ == 0)
2574 {
2575 size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
2576 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2577 __y.__x_ + __y.__i_))
2578 return false;
2579 if (__x.__i_ == 0)
2580 return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
2581 return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
2582 }
2583 if (__x.__i_ < __y.__i_)
2584 {
2585 size_t __j = _R - __y.__i_;
2586 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2587 __y.__x_ + __y.__i_))
2588 return false;
2589 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
2590 __y.__x_))
2591 return false;
2592 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
2593 __y.__x_ + (_R - (__x.__i_ + __j)));
2594 }
2595 size_t __j = _R - __x.__i_;
2596 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2597 __x.__x_ + __x.__i_))
2598 return false;
2599 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
2600 __x.__x_))
2601 return false;
2602 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
2603 __x.__x_ + (_R - (__y.__i_ + __j)));
2604}
2605
2606template<class _UI, size_t _W, size_t _S, size_t _R>
2607inline
2608bool
2609operator!=(
2610 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2611 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2612{
2613 return !(__x == __y);
2614}
2615
2616template <class _CharT, class _Traits,
2617 class _UI, size_t _W, size_t _S, size_t _R>
2618basic_ostream<_CharT, _Traits>&
2619operator<<(basic_ostream<_CharT, _Traits>& __os,
2620 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2621{
2622 __save_flags<_CharT, _Traits> _(__os);
2623 __os.flags(ios_base::dec | ios_base::left);
2624 _CharT __sp = __os.widen(' ');
2625 __os.fill(__sp);
2626 __os << __x.__x_[__x.__i_];
2627 for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
2628 __os << __sp << __x.__x_[__j];
2629 for (size_t __j = 0; __j < __x.__i_; ++__j)
2630 __os << __sp << __x.__x_[__j];
2631 __os << __sp << __x.__c_;
2632 return __os;
2633}
2634
2635template <class _CharT, class _Traits,
2636 class _UI, size_t _W, size_t _S, size_t _R>
2637basic_istream<_CharT, _Traits>&
2638operator>>(basic_istream<_CharT, _Traits>& __is,
2639 subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2640{
2641 __save_flags<_CharT, _Traits> _(__is);
2642 __is.flags(ios_base::dec | ios_base::skipws);
2643 _UI __t[_R+1];
2644 for (size_t __i = 0; __i < _R+1; ++__i)
2645 __is >> __t[__i];
2646 if (!__is.fail())
2647 {
2648 for (size_t __i = 0; __i < _R; ++__i)
2649 __x.__x_[__i] = __t[__i];
2650 __x.__c_ = __t[_R];
2651 __x.__i_ = 0;
2652 }
2653 return __is;
2654}
2655
2656typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2657typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2658
2659// discard_block_engine
2660
2661template<class _Engine, size_t __p, size_t __r>
2662class discard_block_engine
2663{
2664 _Engine __e_;
2665 int __n_;
2666
2667 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2668 static_assert(__r <= __p, "discard_block_engine invalid parameters");
2669public:
2670 // types
2671 typedef typename _Engine::result_type result_type;
2672
2673 // engine characteristics
2674 static const/*expr*/ size_t block_size = __p;
2675 static const/*expr*/ size_t used_block = __r;
2676
2677 // Temporary work around for lack of constexpr
2678 static const result_type _Min = _Engine::_Min;
2679 static const result_type _Max = _Engine::_Max;
2680
2681 static const/*expr*/ result_type min() { return _Engine::min(); }
2682 static const/*expr*/ result_type max() { return _Engine::max(); }
2683
2684 // constructors and seeding functions
2685 discard_block_engine() : __n_(0) {}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002686 explicit discard_block_engine(const _Engine& __e)
2687 : __e_(__e), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002688#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3ec31842010-05-28 15:49:54 +00002689 explicit discard_block_engine(_Engine&& __e)
2690 : __e_(_STD::move(__e)), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002691#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002692 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002693 template<class _Sseq> explicit discard_block_engine(_Sseq& __q,
2694 typename enable_if<!is_convertible<_Sseq, result_type>::value &&
2695 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002696 : __e_(__q), __n_(0) {}
2697 void seed() {__e_.seed(); __n_ = 0;}
2698 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002699 template<class _Sseq>
2700 typename enable_if
2701 <
2702 !is_convertible<_Sseq, result_type>::value,
2703 void
2704 >::type
2705 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706
2707 // generating functions
2708 result_type operator()();
2709 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2710
2711 // property functions
2712 const _Engine& base() const {return __e_;}
2713
2714 template<class _Eng, size_t _P, size_t _R>
2715 friend
2716 bool
2717 operator==(
2718 const discard_block_engine<_Eng, _P, _R>& __x,
2719 const discard_block_engine<_Eng, _P, _R>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002720
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002721 template<class _Eng, size_t _P, size_t _R>
2722 friend
2723 bool
2724 operator!=(
2725 const discard_block_engine<_Eng, _P, _R>& __x,
2726 const discard_block_engine<_Eng, _P, _R>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002727
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728 template <class _CharT, class _Traits,
2729 class _Eng, size_t _P, size_t _R>
2730 friend
2731 basic_ostream<_CharT, _Traits>&
2732 operator<<(basic_ostream<_CharT, _Traits>& __os,
2733 const discard_block_engine<_Eng, _P, _R>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002734
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002735 template <class _CharT, class _Traits,
2736 class _Eng, size_t _P, size_t _R>
2737 friend
2738 basic_istream<_CharT, _Traits>&
2739 operator>>(basic_istream<_CharT, _Traits>& __is,
2740 discard_block_engine<_Eng, _P, _R>& __x);
2741};
2742
2743template<class _Engine, size_t __p, size_t __r>
2744typename discard_block_engine<_Engine, __p, __r>::result_type
2745discard_block_engine<_Engine, __p, __r>::operator()()
2746{
2747 if (__n_ >= __r)
2748 {
2749 __e_.discard(__p - __r);
2750 __n_ = 0;
2751 }
2752 ++__n_;
2753 return __e_();
2754}
2755
2756template<class _Eng, size_t _P, size_t _R>
2757inline
2758bool
2759operator==(const discard_block_engine<_Eng, _P, _R>& __x,
2760 const discard_block_engine<_Eng, _P, _R>& __y)
2761{
2762 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2763}
2764
2765template<class _Eng, size_t _P, size_t _R>
2766inline
2767bool
2768operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
2769 const discard_block_engine<_Eng, _P, _R>& __y)
2770{
2771 return !(__x == __y);
2772}
2773
2774template <class _CharT, class _Traits,
2775 class _Eng, size_t _P, size_t _R>
2776basic_ostream<_CharT, _Traits>&
2777operator<<(basic_ostream<_CharT, _Traits>& __os,
2778 const discard_block_engine<_Eng, _P, _R>& __x)
2779{
2780 __save_flags<_CharT, _Traits> _(__os);
2781 __os.flags(ios_base::dec | ios_base::left);
2782 _CharT __sp = __os.widen(' ');
2783 __os.fill(__sp);
2784 return __os << __x.__e_ << __sp << __x.__n_;
2785}
2786
2787template <class _CharT, class _Traits,
2788 class _Eng, size_t _P, size_t _R>
2789basic_istream<_CharT, _Traits>&
2790operator>>(basic_istream<_CharT, _Traits>& __is,
2791 discard_block_engine<_Eng, _P, _R>& __x)
2792{
2793 __save_flags<_CharT, _Traits> _(__is);
2794 __is.flags(ios_base::dec | ios_base::skipws);
2795 _Eng __e;
2796 int __n;
2797 __is >> __e >> __n;
2798 if (!__is.fail())
2799 {
2800 __x.__e_ = __e;
2801 __x.__n_ = __n;
2802 }
2803 return __is;
2804}
2805
2806typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2807typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2808
2809// independent_bits_engine
2810
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002811template<class _Engine, size_t __w, class _UIntType>
2812class independent_bits_engine
2813{
2814 template <class _UI, _UI _R0, size_t _W, size_t _M>
2815 class __get_n
2816 {
2817 static const size_t _Dt = numeric_limits<_UI>::digits;
2818 static const size_t _N = _W / _M + (_W % _M != 0);
2819 static const size_t _W0 = _W / _N;
2820 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
2821 public:
2822 static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
2823 };
2824public:
2825 // types
2826 typedef _UIntType result_type;
2827
2828private:
2829 _Engine __e_;
2830
2831 static const result_type _Dt = numeric_limits<result_type>::digits;
2832 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2833 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2834
2835 typedef typename _Engine::result_type _Engine_result_type;
2836 typedef typename conditional
2837 <
2838 sizeof(_Engine_result_type) <= sizeof(result_type),
2839 result_type,
2840 _Engine_result_type
2841 >::type _Working_result_type;
2842 // Temporary work around for lack of constexpr
2843 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2844 + _Working_result_type(1);
2845 static const size_t __m = __log2<_Working_result_type, _R>::value;
2846 static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
2847 static const size_t __w0 = __w / __n;
2848 static const size_t __n0 = __n - __w % __n;
2849 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2850 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2851 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2852 (_R >> __w0) << __w0;
2853 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2854 (_R >> (__w0+1)) << (__w0+1);
2855 static const _Engine_result_type __mask0 = __w0 > 0 ?
2856 _Engine_result_type(~0) >> (_EDt - __w0) :
2857 _Engine_result_type(0);
2858 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2859 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2860 _Engine_result_type(~0);
2861public:
2862 static const result_type _Min = 0;
2863 static const result_type _Max = __w == _Dt ? result_type(~0) :
2864 (result_type(1) << __w) - result_type(1);
2865 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2866
2867 // engine characteristics
2868 static const/*expr*/ result_type min() { return _Min; }
2869 static const/*expr*/ result_type max() { return _Max; }
2870
2871 // constructors and seeding functions
2872 independent_bits_engine() {}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002873 explicit independent_bits_engine(const _Engine& __e)
2874 : __e_(__e) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002875#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3ec31842010-05-28 15:49:54 +00002876 explicit independent_bits_engine(_Engine&& __e)
2877 : __e_(_STD::move(__e)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002878#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002879 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002880 template<class _Sseq> explicit independent_bits_engine(_Sseq& __q,
2881 typename enable_if<!is_convertible<_Sseq, result_type>::value &&
2882 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002883 : __e_(__q) {}
2884 void seed() {__e_.seed();}
2885 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002886 template<class _Sseq>
2887 typename enable_if
2888 <
2889 !is_convertible<_Sseq, result_type>::value,
2890 void
2891 >::type
2892 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002893
2894 // generating functions
2895 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2896 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2897
2898 // property functions
2899 const _Engine& base() const {return __e_;}
2900
2901 template<class _Eng, size_t _W, class _UI>
2902 friend
2903 bool
2904 operator==(
2905 const independent_bits_engine<_Eng, _W, _UI>& __x,
2906 const independent_bits_engine<_Eng, _W, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002907
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002908 template<class _Eng, size_t _W, class _UI>
2909 friend
2910 bool
2911 operator!=(
2912 const independent_bits_engine<_Eng, _W, _UI>& __x,
2913 const independent_bits_engine<_Eng, _W, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002914
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915 template <class _CharT, class _Traits,
2916 class _Eng, size_t _W, class _UI>
2917 friend
2918 basic_ostream<_CharT, _Traits>&
2919 operator<<(basic_ostream<_CharT, _Traits>& __os,
2920 const independent_bits_engine<_Eng, _W, _UI>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002921
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922 template <class _CharT, class _Traits,
2923 class _Eng, size_t _W, class _UI>
2924 friend
2925 basic_istream<_CharT, _Traits>&
2926 operator>>(basic_istream<_CharT, _Traits>& __is,
2927 independent_bits_engine<_Eng, _W, _UI>& __x);
2928
2929private:
2930 result_type __eval(false_type);
2931 result_type __eval(true_type);
2932
2933 template <size_t __count>
2934 static
2935 typename enable_if
2936 <
2937 __count < _Dt,
2938 result_type
2939 >::type
2940 __lshift(result_type __x) {return __x << __count;}
2941
2942 template <size_t __count>
2943 static
2944 typename enable_if
2945 <
2946 (__count >= _Dt),
2947 result_type
2948 >::type
2949 __lshift(result_type __x) {return result_type(0);}
2950};
2951
2952template<class _Engine, size_t __w, class _UIntType>
2953inline
2954_UIntType
2955independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
2956{
2957 return static_cast<result_type>(__e_() & __mask0);
2958}
2959
2960template<class _Engine, size_t __w, class _UIntType>
2961_UIntType
2962independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
2963{
2964 result_type _S = 0;
2965 for (size_t __k = 0; __k < __n0; ++__k)
2966 {
2967 _Engine_result_type __u;
2968 do
2969 {
2970 __u = __e_() - _Engine::min();
2971 } while (__u >= __y0);
2972 _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
2973 }
2974 for (size_t __k = __n0; __k < __n; ++__k)
2975 {
2976 _Engine_result_type __u;
2977 do
2978 {
2979 __u = __e_() - _Engine::min();
2980 } while (__u >= __y1);
2981 _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
2982 }
2983 return _S;
2984}
2985
2986template<class _Eng, size_t _W, class _UI>
2987inline
2988bool
2989operator==(
2990 const independent_bits_engine<_Eng, _W, _UI>& __x,
2991 const independent_bits_engine<_Eng, _W, _UI>& __y)
2992{
2993 return __x.base() == __y.base();
2994}
2995
2996template<class _Eng, size_t _W, class _UI>
2997inline
2998bool
2999operator!=(
3000 const independent_bits_engine<_Eng, _W, _UI>& __x,
3001 const independent_bits_engine<_Eng, _W, _UI>& __y)
3002{
3003 return !(__x == __y);
3004}
3005
3006template <class _CharT, class _Traits,
3007 class _Eng, size_t _W, class _UI>
3008basic_ostream<_CharT, _Traits>&
3009operator<<(basic_ostream<_CharT, _Traits>& __os,
3010 const independent_bits_engine<_Eng, _W, _UI>& __x)
3011{
3012 return __os << __x.base();
3013}
3014
3015template <class _CharT, class _Traits,
3016 class _Eng, size_t _W, class _UI>
3017basic_istream<_CharT, _Traits>&
3018operator>>(basic_istream<_CharT, _Traits>& __is,
3019 independent_bits_engine<_Eng, _W, _UI>& __x)
3020{
3021 _Eng __e;
3022 __is >> __e;
3023 if (!__is.fail())
3024 __x.__e_ = __e;
3025 return __is;
3026}
3027
3028// shuffle_order_engine
3029
3030template <uint64_t _Xp, uint64_t _Yp>
3031struct __ugcd
3032{
3033 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
3034};
3035
3036template <uint64_t _Xp>
3037struct __ugcd<_Xp, 0>
3038{
3039 static const uint64_t value = _Xp;
3040};
3041
3042template <uint64_t _N, uint64_t _D>
3043class __uratio
3044{
3045 static_assert(_D != 0, "__uratio divide by 0");
3046 static const uint64_t __gcd = __ugcd<_N, _D>::value;
3047public:
3048 static const uint64_t num = _N / __gcd;
3049 static const uint64_t den = _D / __gcd;
3050
3051 typedef __uratio<num, den> type;
3052};
3053
3054template<class _Engine, size_t __k>
3055class shuffle_order_engine
3056{
3057 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3058public:
3059 // types
3060 typedef typename _Engine::result_type result_type;
3061
3062private:
3063 _Engine __e_;
3064 result_type _V_[__k];
3065 result_type _Y_;
3066
3067public:
3068 // engine characteristics
3069 static const/*expr*/ size_t table_size = __k;
Howard Hinnant324bb032010-08-22 00:02:43 +00003070
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003071 static const result_type _Min = _Engine::_Min;
3072 static const result_type _Max = _Engine::_Max;
3073 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
3074 static const/*expr*/ result_type min() { return _Min; }
3075 static const/*expr*/ result_type max() { return _Max; }
3076
3077 static const unsigned long long _R = _Max - _Min + 1ull;
3078
3079 // constructors and seeding functions
3080 shuffle_order_engine() {__init();}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003081 explicit shuffle_order_engine(const _Engine& __e)
3082 : __e_(__e) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003083#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3ec31842010-05-28 15:49:54 +00003084 explicit shuffle_order_engine(_Engine&& __e)
3085 : __e_(_STD::move(__e)) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003086#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003087 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003088 template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q,
3089 typename enable_if<!is_convertible<_Sseq, result_type>::value &&
3090 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003091 : __e_(__q) {__init();}
3092 void seed() {__e_.seed(); __init();}
3093 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003094 template<class _Sseq>
3095 typename enable_if
3096 <
3097 !is_convertible<_Sseq, result_type>::value,
3098 void
3099 >::type
3100 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003101
3102 // generating functions
3103 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
3104 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3105
3106 // property functions
3107 const _Engine& base() const {return __e_;}
3108
3109private:
3110 template<class _Eng, size_t _K>
3111 friend
3112 bool
3113 operator==(
3114 const shuffle_order_engine<_Eng, _K>& __x,
3115 const shuffle_order_engine<_Eng, _K>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003116
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003117 template<class _Eng, size_t _K>
3118 friend
3119 bool
3120 operator!=(
3121 const shuffle_order_engine<_Eng, _K>& __x,
3122 const shuffle_order_engine<_Eng, _K>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003123
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003124 template <class _CharT, class _Traits,
3125 class _Eng, size_t _K>
3126 friend
3127 basic_ostream<_CharT, _Traits>&
3128 operator<<(basic_ostream<_CharT, _Traits>& __os,
3129 const shuffle_order_engine<_Eng, _K>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003130
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003131 template <class _CharT, class _Traits,
3132 class _Eng, size_t _K>
3133 friend
3134 basic_istream<_CharT, _Traits>&
3135 operator>>(basic_istream<_CharT, _Traits>& __is,
3136 shuffle_order_engine<_Eng, _K>& __x);
3137
3138 void __init()
3139 {
3140 for (size_t __i = 0; __i < __k; ++__i)
3141 _V_[__i] = __e_();
3142 _Y_ = __e_();
3143 }
3144
3145 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
3146 result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
3147
3148 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
3149 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3150
3151 template <uint64_t _N, uint64_t _D>
3152 typename enable_if
3153 <
3154 (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
3155 result_type
3156 >::type
3157 __eval(__uratio<_N, _D>)
3158 {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
3159
3160 template <uint64_t _N, uint64_t _D>
3161 typename enable_if
3162 <
3163 __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
3164 result_type
3165 >::type
3166 __eval(__uratio<_N, _D>)
3167 {
3168 const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
3169 / __uratio<_N, _D>::den);
3170 _Y_ = _V_[__j];
3171 _V_[__j] = __e_();
3172 return _Y_;
3173 }
3174
3175 template <uint64_t __n, uint64_t __d>
3176 result_type __evalf()
3177 {
3178 const double _F = __d == 0 ?
3179 __n / (2. * 0x8000000000000000ull) :
3180 __n / (double)__d;
3181 const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
3182 _Y_ = _V_[__j];
3183 _V_[__j] = __e_();
3184 return _Y_;
3185 }
3186};
3187
3188template<class _Eng, size_t _K>
3189bool
3190operator==(
3191 const shuffle_order_engine<_Eng, _K>& __x,
3192 const shuffle_order_engine<_Eng, _K>& __y)
3193{
3194 return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
3195 __x.__e_ == __y.__e_;
3196}
3197
3198template<class _Eng, size_t _K>
3199inline
3200bool
3201operator!=(
3202 const shuffle_order_engine<_Eng, _K>& __x,
3203 const shuffle_order_engine<_Eng, _K>& __y)
3204{
3205 return !(__x == __y);
3206}
3207
3208template <class _CharT, class _Traits,
3209 class _Eng, size_t _K>
3210basic_ostream<_CharT, _Traits>&
3211operator<<(basic_ostream<_CharT, _Traits>& __os,
3212 const shuffle_order_engine<_Eng, _K>& __x)
3213{
3214 __save_flags<_CharT, _Traits> _(__os);
3215 __os.flags(ios_base::dec | ios_base::left);
3216 _CharT __sp = __os.widen(' ');
3217 __os.fill(__sp);
3218 __os << __x.__e_ << __sp << __x._V_[0];
3219 for (size_t __i = 1; __i < _K; ++__i)
3220 __os << __sp << __x._V_[__i];
3221 return __os << __sp << __x._Y_;
3222}
3223
3224template <class _CharT, class _Traits,
3225 class _Eng, size_t _K>
3226basic_istream<_CharT, _Traits>&
3227operator>>(basic_istream<_CharT, _Traits>& __is,
3228 shuffle_order_engine<_Eng, _K>& __x)
3229{
3230 typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
3231 __save_flags<_CharT, _Traits> _(__is);
3232 __is.flags(ios_base::dec | ios_base::skipws);
3233 _Eng __e;
3234 result_type _V[_K+1];
3235 __is >> __e;
3236 for (size_t __i = 0; __i < _K+1; ++__i)
3237 __is >> _V[__i];
3238 if (!__is.fail())
3239 {
3240 __x.__e_ = __e;
3241 for (size_t __i = 0; __i < _K; ++__i)
3242 __x._V_[__i] = _V[__i];
3243 __x._Y_ = _V[_K];
3244 }
3245 return __is;
3246}
3247
3248typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3249
3250// random_device
3251
3252class random_device
3253{
3254 int __f_;
3255public:
3256 // types
3257 typedef unsigned result_type;
3258
3259 // generator characteristics
3260 static const result_type _Min = 0;
3261 static const result_type _Max = 0xFFFFFFFFu;
3262
3263 static const/*expr*/ result_type min() { return _Min;}
3264 static const/*expr*/ result_type max() { return _Max;}
3265
3266 // constructors
3267 explicit random_device(const string& __token = "/dev/urandom");
3268 ~random_device();
3269
3270 // generating functions
3271 result_type operator()();
3272
3273 // property functions
3274 double entropy() const;
3275
3276private:
3277 // no copy functions
3278 random_device(const random_device&); // = delete;
3279 random_device& operator=(const random_device&); // = delete;
3280};
3281
3282// seed_seq
3283
3284class seed_seq
3285{
3286public:
3287 // types
3288 typedef uint32_t result_type;
3289
3290private:
3291 vector<result_type> __v_;
3292
3293 template<class _InputIterator>
3294 void init(_InputIterator __first, _InputIterator __last);
3295public:
3296 // constructors
3297 seed_seq() {}
3298 template<class _Tp>
3299 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Howard Hinnant324bb032010-08-22 00:02:43 +00003300
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003301 template<class _InputIterator>
3302 seed_seq(_InputIterator __first, _InputIterator __last)
3303 {init(__first, __last);}
3304
3305 // generating functions
3306 template<class _RandomAccessIterator>
3307 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3308
3309 // property functions
3310 size_t size() const {return __v_.size();}
3311 template<class _OutputIterator>
3312 void param(_OutputIterator __dest) const
3313 {_STD::copy(__v_.begin(), __v_.end(), __dest);}
3314
3315private:
3316 // no copy functions
3317 seed_seq(const seed_seq&); // = delete;
3318 void operator=(const seed_seq&); // = delete;
3319
3320 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
3321};
3322
3323template<class _InputIterator>
3324void
3325seed_seq::init(_InputIterator __first, _InputIterator __last)
3326{
3327 for (_InputIterator __s = __first; __s != __last; ++__s)
3328 __v_.push_back(*__s & 0xFFFFFFFF);
3329}
3330
3331template<class _RandomAccessIterator>
3332void
3333seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3334{
3335 if (__first != __last)
3336 {
3337 _STD::fill(__first, __last, 0x8b8b8b8b);
3338 const size_t __n = static_cast<size_t>(__last - __first);
3339 const size_t __s = __v_.size();
3340 const size_t __t = (__n >= 623) ? 11
3341 : (__n >= 68) ? 7
3342 : (__n >= 39) ? 5
3343 : (__n >= 7) ? 3
3344 : (__n - 1) / 2;
3345 const size_t __p = (__n - __t) / 2;
3346 const size_t __q = __p + __t;
3347 const size_t __m = _STD::max(__s + 1, __n);
3348 // __k = 0;
3349 {
3350 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
3351 ^ __first[__n - 1]);
3352 __first[__p] += __r;
3353 __r += __s;
3354 __first[__q] += __r;
3355 __first[0] = __r;
3356 }
3357 for (size_t __k = 1; __k <= __s; ++__k)
3358 {
3359 const size_t __kmodn = __k % __n;
3360 const size_t __kpmodn = (__k + __p) % __n;
3361 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3362 ^ __first[(__k - 1) % __n]);
3363 __first[__kpmodn] += __r;
3364 __r += __kmodn + __v_[__k-1];
3365 __first[(__k + __q) % __n] += __r;
3366 __first[__kmodn] = __r;
3367 }
3368 for (size_t __k = __s + 1; __k < __m; ++__k)
3369 {
3370 const size_t __kmodn = __k % __n;
3371 const size_t __kpmodn = (__k + __p) % __n;
3372 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3373 ^ __first[(__k - 1) % __n]);
3374 __first[__kpmodn] += __r;
3375 __r += __kmodn;
3376 __first[(__k + __q) % __n] += __r;
3377 __first[__kmodn] = __r;
3378 }
3379 for (size_t __k = __m; __k < __m + __n; ++__k)
3380 {
3381 const size_t __kmodn = __k % __n;
3382 const size_t __kpmodn = (__k + __p) % __n;
3383 result_type __r = 1566083941 * _T(__first[__kmodn] +
3384 __first[__kpmodn] +
3385 __first[(__k - 1) % __n]);
3386 __first[__kpmodn] ^= __r;
3387 __r -= __kmodn;
3388 __first[(__k + __q) % __n] ^= __r;
3389 __first[__kmodn] = __r;
3390 }
3391 }
3392}
3393
Howard Hinnant30a840f2010-05-12 17:08:57 +00003394// generate_canonical
3395
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003396template<class _RealType, size_t __bits, class _URNG>
3397_RealType
3398generate_canonical(_URNG& __g)
3399{
3400 const size_t _Dt = numeric_limits<_RealType>::digits;
3401 const size_t __b = _Dt < __bits ? _Dt : __bits;
3402 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
3403 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
3404 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
3405 _RealType __base = _R;
3406 _RealType _S = __g() - _URNG::_Min;
3407 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
3408 _S += (__g() - _URNG::_Min) * __base;
3409 return _S / __base;
3410}
3411
Howard Hinnant30a840f2010-05-12 17:08:57 +00003412// uniform_int_distribution
3413
Howard Hinnantc3267212010-05-26 17:49:34 +00003414// in <algorithm>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003415
3416template <class _CharT, class _Traits, class _IT>
3417basic_ostream<_CharT, _Traits>&
3418operator<<(basic_ostream<_CharT, _Traits>& __os,
3419 const uniform_int_distribution<_IT>& __x)
3420{
3421 __save_flags<_CharT, _Traits> _(__os);
3422 __os.flags(ios_base::dec | ios_base::left);
3423 _CharT __sp = __os.widen(' ');
3424 __os.fill(__sp);
3425 return __os << __x.a() << __sp << __x.b();
3426}
3427
3428template <class _CharT, class _Traits, class _IT>
3429basic_istream<_CharT, _Traits>&
3430operator>>(basic_istream<_CharT, _Traits>& __is,
3431 uniform_int_distribution<_IT>& __x)
3432{
3433 typedef uniform_int_distribution<_IT> _Eng;
3434 typedef typename _Eng::result_type result_type;
3435 typedef typename _Eng::param_type param_type;
3436 __save_flags<_CharT, _Traits> _(__is);
3437 __is.flags(ios_base::dec | ios_base::skipws);
3438 result_type __a;
3439 result_type __b;
3440 __is >> __a >> __b;
3441 if (!__is.fail())
3442 __x.param(param_type(__a, __b));
3443 return __is;
3444}
3445
Howard Hinnant30a840f2010-05-12 17:08:57 +00003446// uniform_real_distribution
3447
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003448template<class _RealType = double>
3449class uniform_real_distribution
3450{
3451public:
3452 // types
3453 typedef _RealType result_type;
3454
3455 class param_type
3456 {
3457 result_type __a_;
3458 result_type __b_;
3459 public:
3460 typedef uniform_real_distribution distribution_type;
3461
3462 explicit param_type(result_type __a = 0,
3463 result_type __b = 1)
3464 : __a_(__a), __b_(__b) {}
3465
3466 result_type a() const {return __a_;}
3467 result_type b() const {return __b_;}
3468
3469 friend bool operator==(const param_type& __x, const param_type& __y)
3470 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3471 friend bool operator!=(const param_type& __x, const param_type& __y)
3472 {return !(__x == __y);}
3473 };
3474
3475private:
3476 param_type __p_;
3477
3478public:
3479 // constructors and reset functions
3480 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3481 : __p_(param_type(__a, __b)) {}
3482 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
3483 void reset() {}
3484
3485 // generating functions
3486 template<class _URNG> result_type operator()(_URNG& __g)
3487 {return (*this)(__g, __p_);}
3488 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3489
3490 // property functions
3491 result_type a() const {return __p_.a();}
3492 result_type b() const {return __p_.b();}
3493
3494 param_type param() const {return __p_;}
3495 void param(const param_type& __p) {__p_ = __p;}
3496
3497 result_type min() const {return a();}
3498 result_type max() const {return b();}
3499
3500 friend bool operator==(const uniform_real_distribution& __x,
3501 const uniform_real_distribution& __y)
3502 {return __x.__p_ == __y.__p_;}
3503 friend bool operator!=(const uniform_real_distribution& __x,
3504 const uniform_real_distribution& __y)
3505 {return !(__x == __y);}
3506};
3507
3508template<class _RealType>
3509template<class _URNG>
3510inline
3511typename uniform_real_distribution<_RealType>::result_type
3512uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3513{
3514 return (__p.b() - __p.a())
3515 * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
3516 + __p.a();
3517}
3518
3519template <class _CharT, class _Traits, class _RT>
3520basic_ostream<_CharT, _Traits>&
3521operator<<(basic_ostream<_CharT, _Traits>& __os,
3522 const uniform_real_distribution<_RT>& __x)
3523{
3524 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003525 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3526 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003527 _CharT __sp = __os.widen(' ');
3528 __os.fill(__sp);
3529 return __os << __x.a() << __sp << __x.b();
3530}
3531
3532template <class _CharT, class _Traits, class _RT>
3533basic_istream<_CharT, _Traits>&
3534operator>>(basic_istream<_CharT, _Traits>& __is,
3535 uniform_real_distribution<_RT>& __x)
3536{
3537 typedef uniform_real_distribution<_RT> _Eng;
3538 typedef typename _Eng::result_type result_type;
3539 typedef typename _Eng::param_type param_type;
3540 __save_flags<_CharT, _Traits> _(__is);
3541 __is.flags(ios_base::dec | ios_base::skipws);
3542 result_type __a;
3543 result_type __b;
3544 __is >> __a >> __b;
3545 if (!__is.fail())
3546 __x.param(param_type(__a, __b));
3547 return __is;
3548}
3549
Howard Hinnant30a840f2010-05-12 17:08:57 +00003550// bernoulli_distribution
3551
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003552class bernoulli_distribution
3553{
3554public:
3555 // types
3556 typedef bool result_type;
3557
3558 class param_type
3559 {
3560 double __p_;
3561 public:
3562 typedef bernoulli_distribution distribution_type;
3563
3564 explicit param_type(double __p = 0.5) : __p_(__p) {}
3565
3566 double p() const {return __p_;}
3567
3568 friend bool operator==(const param_type& __x, const param_type& __y)
3569 {return __x.__p_ == __y.__p_;}
3570 friend bool operator!=(const param_type& __x, const param_type& __y)
3571 {return !(__x == __y);}
3572 };
3573
3574private:
3575 param_type __p_;
3576
3577public:
3578 // constructors and reset functions
3579 explicit bernoulli_distribution(double __p = 0.5)
3580 : __p_(param_type(__p)) {}
3581 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
3582 void reset() {}
3583
3584 // generating functions
3585 template<class _URNG> result_type operator()(_URNG& __g)
3586 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003587 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003588
3589 // property functions
3590 double p() const {return __p_.p();}
3591
3592 param_type param() const {return __p_;}
3593 void param(const param_type& __p) {__p_ = __p;}
3594
3595 result_type min() const {return false;}
3596 result_type max() const {return true;}
3597
3598 friend bool operator==(const bernoulli_distribution& __x,
3599 const bernoulli_distribution& __y)
3600 {return __x.__p_ == __y.__p_;}
3601 friend bool operator!=(const bernoulli_distribution& __x,
3602 const bernoulli_distribution& __y)
3603 {return !(__x == __y);}
3604};
3605
Howard Hinnant03aad812010-05-11 23:26:59 +00003606template<class _URNG>
3607inline
3608bernoulli_distribution::result_type
3609bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3610{
Howard Hinnantd6d11712010-05-20 15:11:46 +00003611 uniform_real_distribution<double> __gen;
3612 return __gen(__g) < __p.p();
Howard Hinnant03aad812010-05-11 23:26:59 +00003613}
3614
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003615template <class _CharT, class _Traits>
3616basic_ostream<_CharT, _Traits>&
3617operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3618{
3619 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003620 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3621 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003622 _CharT __sp = __os.widen(' ');
3623 __os.fill(__sp);
3624 return __os << __x.p();
3625}
3626
3627template <class _CharT, class _Traits>
3628basic_istream<_CharT, _Traits>&
3629operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3630{
3631 typedef bernoulli_distribution _Eng;
3632 typedef typename _Eng::param_type param_type;
3633 __save_flags<_CharT, _Traits> _(__is);
3634 __is.flags(ios_base::dec | ios_base::skipws);
3635 double __p;
3636 __is >> __p;
3637 if (!__is.fail())
3638 __x.param(param_type(__p));
3639 return __is;
3640}
3641
Howard Hinnant30a840f2010-05-12 17:08:57 +00003642// binomial_distribution
3643
Howard Hinnant03aad812010-05-11 23:26:59 +00003644template<class _IntType = int>
3645class binomial_distribution
3646{
3647public:
3648 // types
3649 typedef _IntType result_type;
3650
3651 class param_type
3652 {
3653 result_type __t_;
3654 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003655 double __pr_;
3656 double __odds_ratio_;
3657 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003658 public:
3659 typedef binomial_distribution distribution_type;
3660
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003661 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003662
3663 result_type t() const {return __t_;}
3664 double p() const {return __p_;}
3665
3666 friend bool operator==(const param_type& __x, const param_type& __y)
3667 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
3668 friend bool operator!=(const param_type& __x, const param_type& __y)
3669 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003670
3671 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003672 };
3673
3674private:
3675 param_type __p_;
3676
3677public:
3678 // constructors and reset functions
3679 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3680 : __p_(param_type(__t, __p)) {}
3681 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
3682 void reset() {}
3683
3684 // generating functions
3685 template<class _URNG> result_type operator()(_URNG& __g)
3686 {return (*this)(__g, __p_);}
3687 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3688
3689 // property functions
3690 result_type t() const {return __p_.t();}
3691 double p() const {return __p_.p();}
3692
3693 param_type param() const {return __p_;}
3694 void param(const param_type& __p) {__p_ = __p;}
3695
3696 result_type min() const {return 0;}
3697 result_type max() const {return t();}
3698
3699 friend bool operator==(const binomial_distribution& __x,
3700 const binomial_distribution& __y)
3701 {return __x.__p_ == __y.__p_;}
3702 friend bool operator!=(const binomial_distribution& __x,
3703 const binomial_distribution& __y)
3704 {return !(__x == __y);}
3705};
3706
3707template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003708binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3709 : __t_(__t), __p_(__p)
3710{
3711 if (0 < __p_ && __p_ < 1)
3712 {
3713 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
3714 __pr_ = _STD::exp(_STD::lgamma(__t_ + 1.) - _STD::lgamma(__r0_ + 1.) -
3715 _STD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _STD::log(__p_) +
3716 (__t_ - __r0_) * _STD::log(1 - __p_));
3717 __odds_ratio_ = __p_ / (1 - __p_);
3718 }
3719}
3720
3721template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00003722template<class _URNG>
3723_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003724binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00003725{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003726 if (__pr.__t_ == 0 || __pr.__p_ == 0)
3727 return 0;
3728 if (__pr.__p_ == 1)
3729 return __pr.__t_;
3730 uniform_real_distribution<double> __gen;
3731 double __u = __gen(__g) - __pr.__pr_;
3732 if (__u < 0)
3733 return __pr.__r0_;
3734 double __pu = __pr.__pr_;
3735 double __pd = __pu;
3736 result_type __ru = __pr.__r0_;
3737 result_type __rd = __ru;
3738 while (true)
3739 {
3740 if (__rd >= 1)
3741 {
3742 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3743 __u -= __pd;
3744 if (__u < 0)
3745 return __rd - 1;
3746 }
3747 --__rd;
3748 ++__ru;
3749 if (__ru <= __pr.__t_)
3750 {
3751 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3752 __u -= __pu;
3753 if (__u < 0)
3754 return __ru;
3755 }
3756 }
Howard Hinnant03aad812010-05-11 23:26:59 +00003757}
3758
3759template <class _CharT, class _Traits, class _IntType>
3760basic_ostream<_CharT, _Traits>&
3761operator<<(basic_ostream<_CharT, _Traits>& __os,
3762 const binomial_distribution<_IntType>& __x)
3763{
3764 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003765 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3766 ios_base::scientific);
Howard Hinnant03aad812010-05-11 23:26:59 +00003767 _CharT __sp = __os.widen(' ');
3768 __os.fill(__sp);
3769 return __os << __x.t() << __sp << __x.p();
3770}
3771
3772template <class _CharT, class _Traits, class _IntType>
3773basic_istream<_CharT, _Traits>&
3774operator>>(basic_istream<_CharT, _Traits>& __is,
3775 binomial_distribution<_IntType>& __x)
3776{
3777 typedef binomial_distribution<_IntType> _Eng;
3778 typedef typename _Eng::result_type result_type;
3779 typedef typename _Eng::param_type param_type;
3780 __save_flags<_CharT, _Traits> _(__is);
3781 __is.flags(ios_base::dec | ios_base::skipws);
3782 result_type __t;
3783 double __p;
3784 __is >> __t >> __p;
3785 if (!__is.fail())
3786 __x.param(param_type(__t, __p));
3787 return __is;
3788}
3789
Howard Hinnant30a840f2010-05-12 17:08:57 +00003790// exponential_distribution
3791
3792template<class _RealType = double>
3793class exponential_distribution
3794{
3795public:
3796 // types
3797 typedef _RealType result_type;
3798
3799 class param_type
3800 {
3801 result_type __lambda_;
3802 public:
3803 typedef exponential_distribution distribution_type;
3804
3805 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3806
3807 result_type lambda() const {return __lambda_;}
3808
3809 friend bool operator==(const param_type& __x, const param_type& __y)
3810 {return __x.__lambda_ == __y.__lambda_;}
3811 friend bool operator!=(const param_type& __x, const param_type& __y)
3812 {return !(__x == __y);}
3813 };
3814
3815private:
3816 param_type __p_;
3817
3818public:
3819 // constructors and reset functions
3820 explicit exponential_distribution(result_type __lambda = 1)
3821 : __p_(param_type(__lambda)) {}
3822 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
3823 void reset() {}
3824
3825 // generating functions
3826 template<class _URNG> result_type operator()(_URNG& __g)
3827 {return (*this)(__g, __p_);}
3828 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3829
3830 // property functions
3831 result_type lambda() const {return __p_.lambda();}
3832
3833 param_type param() const {return __p_;}
3834 void param(const param_type& __p) {__p_ = __p;}
3835
3836 result_type min() const {return 0;}
Howard Hinnantdf40dc62010-05-16 17:56:20 +00003837 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00003838
3839 friend bool operator==(const exponential_distribution& __x,
3840 const exponential_distribution& __y)
3841 {return __x.__p_ == __y.__p_;}
3842 friend bool operator!=(const exponential_distribution& __x,
3843 const exponential_distribution& __y)
3844 {return !(__x == __y);}
3845};
3846
3847template <class _RealType>
3848template<class _URNG>
3849_RealType
3850exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3851{
3852 return -_STD::log
3853 (
3854 result_type(1) -
3855 _STD::generate_canonical<result_type,
3856 numeric_limits<result_type>::digits>(__g)
3857 )
3858 / __p.lambda();
3859}
3860
3861template <class _CharT, class _Traits, class _RealType>
3862basic_ostream<_CharT, _Traits>&
3863operator<<(basic_ostream<_CharT, _Traits>& __os,
3864 const exponential_distribution<_RealType>& __x)
3865{
3866 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003867 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3868 ios_base::scientific);
Howard Hinnant30a840f2010-05-12 17:08:57 +00003869 return __os << __x.lambda();
3870}
3871
3872template <class _CharT, class _Traits, class _RealType>
3873basic_istream<_CharT, _Traits>&
3874operator>>(basic_istream<_CharT, _Traits>& __is,
3875 exponential_distribution<_RealType>& __x)
3876{
3877 typedef exponential_distribution<_RealType> _Eng;
3878 typedef typename _Eng::result_type result_type;
3879 typedef typename _Eng::param_type param_type;
3880 __save_flags<_CharT, _Traits> _(__is);
3881 __is.flags(ios_base::dec | ios_base::skipws);
3882 result_type __lambda;
3883 __is >> __lambda;
3884 if (!__is.fail())
3885 __x.param(param_type(__lambda));
3886 return __is;
3887}
3888
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003889// normal_distribution
3890
3891template<class _RealType = double>
3892class normal_distribution
3893{
3894public:
3895 // types
3896 typedef _RealType result_type;
3897
3898 class param_type
3899 {
3900 result_type __mean_;
3901 result_type __stddev_;
3902 public:
3903 typedef normal_distribution distribution_type;
3904
3905 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
3906 : __mean_(__mean), __stddev_(__stddev) {}
3907
3908 result_type mean() const {return __mean_;}
3909 result_type stddev() const {return __stddev_;}
3910
3911 friend bool operator==(const param_type& __x, const param_type& __y)
3912 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
3913 friend bool operator!=(const param_type& __x, const param_type& __y)
3914 {return !(__x == __y);}
3915 };
3916
3917private:
3918 param_type __p_;
3919 result_type _V_;
3920 bool _V_hot_;
3921
3922public:
3923 // constructors and reset functions
3924 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
3925 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
3926 explicit normal_distribution(const param_type& __p)
3927 : __p_(__p), _V_hot_(false) {}
3928 void reset() {_V_hot_ = false;}
3929
3930 // generating functions
3931 template<class _URNG> result_type operator()(_URNG& __g)
3932 {return (*this)(__g, __p_);}
3933 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3934
3935 // property functions
3936 result_type mean() const {return __p_.mean();}
3937 result_type stddev() const {return __p_.stddev();}
3938
3939 param_type param() const {return __p_;}
3940 void param(const param_type& __p) {__p_ = __p;}
3941
3942 result_type min() const {return -numeric_limits<result_type>::infinity();}
3943 result_type max() const {return numeric_limits<result_type>::infinity();}
3944
3945 friend bool operator==(const normal_distribution& __x,
3946 const normal_distribution& __y)
3947 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
3948 (!__x._V_hot_ || __x._V_ == __y._V_);}
3949 friend bool operator!=(const normal_distribution& __x,
3950 const normal_distribution& __y)
3951 {return !(__x == __y);}
3952
3953 template <class _CharT, class _Traits, class _RT>
3954 friend
3955 basic_ostream<_CharT, _Traits>&
3956 operator<<(basic_ostream<_CharT, _Traits>& __os,
3957 const normal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003958
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003959 template <class _CharT, class _Traits, class _RT>
3960 friend
3961 basic_istream<_CharT, _Traits>&
3962 operator>>(basic_istream<_CharT, _Traits>& __is,
3963 normal_distribution<_RT>& __x);
3964};
3965
3966template <class _RealType>
3967template<class _URNG>
3968_RealType
3969normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3970{
3971 result_type _U;
3972 if (_V_hot_)
3973 {
3974 _V_hot_ = false;
3975 _U = _V_;
3976 }
3977 else
3978 {
3979 uniform_real_distribution<result_type> _Uni(-1, 1);
3980 result_type __u;
3981 result_type __v;
3982 result_type __s;
3983 do
3984 {
3985 __u = _Uni(__g);
3986 __v = _Uni(__g);
3987 __s = __u * __u + __v * __v;
3988 } while (__s > 1 || __s == 0);
3989 result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s);
3990 _V_ = __v * _F;
3991 _V_hot_ = true;
3992 _U = __u * _F;
3993 }
3994 return _U * __p.stddev() + __p.mean();
3995}
3996
3997template <class _CharT, class _Traits, class _RT>
3998basic_ostream<_CharT, _Traits>&
3999operator<<(basic_ostream<_CharT, _Traits>& __os,
4000 const normal_distribution<_RT>& __x)
4001{
4002 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004003 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4004 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004005 _CharT __sp = __os.widen(' ');
4006 __os.fill(__sp);
4007 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4008 if (__x._V_hot_)
4009 __os << __sp << __x._V_;
4010 return __os;
4011}
4012
4013template <class _CharT, class _Traits, class _RT>
4014basic_istream<_CharT, _Traits>&
4015operator>>(basic_istream<_CharT, _Traits>& __is,
4016 normal_distribution<_RT>& __x)
4017{
4018 typedef normal_distribution<_RT> _Eng;
4019 typedef typename _Eng::result_type result_type;
4020 typedef typename _Eng::param_type param_type;
4021 __save_flags<_CharT, _Traits> _(__is);
4022 __is.flags(ios_base::dec | ios_base::skipws);
4023 result_type __mean;
4024 result_type __stddev;
4025 result_type _V = 0;
4026 bool _V_hot = false;
4027 __is >> __mean >> __stddev >> _V_hot;
4028 if (_V_hot)
4029 __is >> _V;
4030 if (!__is.fail())
4031 {
4032 __x.param(param_type(__mean, __stddev));
4033 __x._V_hot_ = _V_hot;
4034 __x._V_ = _V;
4035 }
4036 return __is;
4037}
4038
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004039// lognormal_distribution
4040
4041template<class _RealType = double>
4042class lognormal_distribution
4043{
4044public:
4045 // types
4046 typedef _RealType result_type;
4047
4048 class param_type
4049 {
4050 normal_distribution<result_type> __nd_;
4051 public:
4052 typedef lognormal_distribution distribution_type;
4053
4054 explicit param_type(result_type __m = 0, result_type __s = 1)
4055 : __nd_(__m, __s) {}
4056
4057 result_type m() const {return __nd_.mean();}
4058 result_type s() const {return __nd_.stddev();}
4059
4060 friend bool operator==(const param_type& __x, const param_type& __y)
4061 {return __x.__nd_ == __y.__nd_;}
4062 friend bool operator!=(const param_type& __x, const param_type& __y)
4063 {return !(__x == __y);}
4064 friend class lognormal_distribution;
4065
4066 template <class _CharT, class _Traits, class _RT>
4067 friend
4068 basic_ostream<_CharT, _Traits>&
4069 operator<<(basic_ostream<_CharT, _Traits>& __os,
4070 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004071
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004072 template <class _CharT, class _Traits, class _RT>
4073 friend
4074 basic_istream<_CharT, _Traits>&
4075 operator>>(basic_istream<_CharT, _Traits>& __is,
4076 lognormal_distribution<_RT>& __x);
4077 };
4078
4079private:
4080 param_type __p_;
4081
4082public:
4083 // constructor and reset functions
4084 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4085 : __p_(param_type(__m, __s)) {}
4086 explicit lognormal_distribution(const param_type& __p)
4087 : __p_(__p) {}
4088 void reset() {__p_.__nd_.reset();}
4089
4090 // generating functions
4091 template<class _URNG> result_type operator()(_URNG& __g)
4092 {return (*this)(__g, __p_);}
4093 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4094 {return _STD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
4095
4096 // property functions
4097 result_type m() const {return __p_.m();}
4098 result_type s() const {return __p_.s();}
4099
4100 param_type param() const {return __p_;}
Howard Hinnant551d8e42010-05-19 01:53:57 +00004101 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004102
4103 result_type min() const {return 0;}
4104 result_type max() const {return numeric_limits<result_type>::infinity();}
4105
4106 friend bool operator==(const lognormal_distribution& __x,
4107 const lognormal_distribution& __y)
4108 {return __x.__p_ == __y.__p_;}
4109 friend bool operator!=(const lognormal_distribution& __x,
4110 const lognormal_distribution& __y)
4111 {return !(__x == __y);}
4112
4113 template <class _CharT, class _Traits, class _RT>
4114 friend
4115 basic_ostream<_CharT, _Traits>&
4116 operator<<(basic_ostream<_CharT, _Traits>& __os,
4117 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004118
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004119 template <class _CharT, class _Traits, class _RT>
4120 friend
4121 basic_istream<_CharT, _Traits>&
4122 operator>>(basic_istream<_CharT, _Traits>& __is,
4123 lognormal_distribution<_RT>& __x);
4124};
4125
4126template <class _CharT, class _Traits, class _RT>
4127inline
4128basic_ostream<_CharT, _Traits>&
4129operator<<(basic_ostream<_CharT, _Traits>& __os,
4130 const lognormal_distribution<_RT>& __x)
4131{
4132 return __os << __x.__p_.__nd_;
4133}
4134
4135template <class _CharT, class _Traits, class _RT>
4136inline
4137basic_istream<_CharT, _Traits>&
4138operator>>(basic_istream<_CharT, _Traits>& __is,
4139 lognormal_distribution<_RT>& __x)
4140{
4141 return __is >> __x.__p_.__nd_;
4142}
4143
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004144// poisson_distribution
4145
4146template<class _IntType = int>
4147class poisson_distribution
4148{
4149public:
4150 // types
4151 typedef _IntType result_type;
4152
4153 class param_type
4154 {
4155 double __mean_;
4156 double __s_;
4157 double __d_;
4158 double __l_;
4159 double __omega_;
4160 double __c0_;
4161 double __c1_;
4162 double __c2_;
4163 double __c3_;
4164 double __c_;
4165
4166 public:
4167 typedef poisson_distribution distribution_type;
4168
4169 explicit param_type(double __mean = 1.0);
4170
4171 double mean() const {return __mean_;}
4172
4173 friend bool operator==(const param_type& __x, const param_type& __y)
4174 {return __x.__mean_ == __y.__mean_;}
4175 friend bool operator!=(const param_type& __x, const param_type& __y)
4176 {return !(__x == __y);}
4177
4178 friend class poisson_distribution;
4179 };
4180
4181private:
4182 param_type __p_;
4183
4184public:
4185 // constructors and reset functions
4186 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
4187 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
4188 void reset() {}
4189
4190 // generating functions
4191 template<class _URNG> result_type operator()(_URNG& __g)
4192 {return (*this)(__g, __p_);}
4193 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4194
4195 // property functions
4196 double mean() const {return __p_.mean();}
4197
4198 param_type param() const {return __p_;}
4199 void param(const param_type& __p) {__p_ = __p;}
4200
4201 result_type min() const {return 0;}
4202 result_type max() const {return numeric_limits<result_type>::max();}
4203
4204 friend bool operator==(const poisson_distribution& __x,
4205 const poisson_distribution& __y)
4206 {return __x.__p_ == __y.__p_;}
4207 friend bool operator!=(const poisson_distribution& __x,
4208 const poisson_distribution& __y)
4209 {return !(__x == __y);}
4210};
4211
4212template<class _IntType>
4213poisson_distribution<_IntType>::param_type::param_type(double __mean)
4214 : __mean_(__mean)
4215{
4216 if (__mean_ < 10)
4217 {
4218 __s_ = 0;
4219 __d_ = 0;
4220 __l_ = _STD::exp(-__mean_);
4221 __omega_ = 0;
4222 __c3_ = 0;
4223 __c2_ = 0;
4224 __c1_ = 0;
4225 __c0_ = 0;
4226 __c_ = 0;
4227 }
4228 else
4229 {
4230 __s_ = _STD::sqrt(__mean_);
4231 __d_ = 6 * __mean_ * __mean_;
4232 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4233 __omega_ = .3989423 / __s_;
4234 double __b1_ = .4166667E-1 / __mean_;
4235 double __b2_ = .3 * __b1_ * __b1_;
4236 __c3_ = .1428571 * __b1_ * __b2_;
4237 __c2_ = __b2_ - 15. * __c3_;
4238 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4239 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4240 __c_ = .1069 / __mean_;
4241 }
4242}
4243
4244template <class _IntType>
4245template<class _URNG>
4246_IntType
4247poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4248{
4249 result_type __x;
4250 uniform_real_distribution<double> __urd;
4251 if (__pr.__mean_ <= 10)
4252 {
4253 __x = 0;
4254 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4255 __p *= __urd(__urng);
4256 }
4257 else
4258 {
4259 double __difmuk;
4260 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4261 double __u;
4262 if (__g > 0)
4263 {
4264 __x = static_cast<result_type>(__g);
4265 if (__x >= __pr.__l_)
4266 return __x;
4267 __difmuk = __pr.__mean_ - __x;
4268 __u = __urd(__urng);
4269 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4270 return __x;
4271 }
4272 exponential_distribution<double> __edist;
4273 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4274 {
4275 double __e;
4276 if (__using_exp_dist || __g < 0)
4277 {
4278 double __t;
4279 do
4280 {
4281 __e = __edist(__urng);
4282 __u = __urd(__urng);
4283 __u += __u - 1;
4284 __t = 1.8 + (__u < 0 ? -__e : __e);
4285 } while (__t <= -.6744);
4286 __x = __pr.__mean_ + __pr.__s_ * __t;
4287 __difmuk = __pr.__mean_ - __x;
4288 __using_exp_dist = true;
4289 }
4290 double __px;
4291 double __py;
4292 if (__x < 10)
4293 {
4294 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4295 40320, 362880};
4296 __px = -__pr.__mean_;
4297 __py = _STD::pow(__pr.__mean_, (double)__x) / __fac[__x];
4298 }
4299 else
4300 {
4301 double __del = .8333333E-1 / __x;
4302 __del -= 4.8 * __del * __del * __del;
4303 double __v = __difmuk / __x;
4304 if (_STD::abs(__v) > 0.25)
4305 __px = __x * _STD::log(1 + __v) - __difmuk - __del;
4306 else
4307 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4308 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4309 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
4310 __py = .3989423 / _STD::sqrt(__x);
4311 }
4312 double __r = (0.5 - __difmuk) / __pr.__s_;
4313 double __r2 = __r * __r;
4314 double __fx = -0.5 * __r2;
4315 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4316 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4317 if (__using_exp_dist)
4318 {
4319 if (__pr.__c_ * _STD::abs(__u) <= __py * _STD::exp(__px + __e) -
4320 __fy * _STD::exp(__fx + __e))
4321 break;
4322 }
4323 else
4324 {
4325 if (__fy - __u * __fy <= __py * _STD::exp(__px - __fx))
4326 break;
4327 }
4328 }
4329 }
4330 return __x;
4331}
4332
4333template <class _CharT, class _Traits, class _IntType>
4334basic_ostream<_CharT, _Traits>&
4335operator<<(basic_ostream<_CharT, _Traits>& __os,
4336 const poisson_distribution<_IntType>& __x)
4337{
4338 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004339 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4340 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004341 return __os << __x.mean();
4342}
4343
4344template <class _CharT, class _Traits, class _IntType>
4345basic_istream<_CharT, _Traits>&
4346operator>>(basic_istream<_CharT, _Traits>& __is,
4347 poisson_distribution<_IntType>& __x)
4348{
4349 typedef poisson_distribution<_IntType> _Eng;
4350 typedef typename _Eng::param_type param_type;
4351 __save_flags<_CharT, _Traits> _(__is);
4352 __is.flags(ios_base::dec | ios_base::skipws);
4353 double __mean;
4354 __is >> __mean;
4355 if (!__is.fail())
4356 __x.param(param_type(__mean));
4357 return __is;
4358}
4359
Howard Hinnant9de6e302010-05-16 01:09:02 +00004360// weibull_distribution
4361
4362template<class _RealType = double>
4363class weibull_distribution
4364{
4365public:
4366 // types
4367 typedef _RealType result_type;
4368
4369 class param_type
4370 {
4371 result_type __a_;
4372 result_type __b_;
4373 public:
4374 typedef weibull_distribution distribution_type;
4375
4376 explicit param_type(result_type __a = 1, result_type __b = 1)
4377 : __a_(__a), __b_(__b) {}
4378
4379 result_type a() const {return __a_;}
4380 result_type b() const {return __b_;}
4381
4382 friend bool operator==(const param_type& __x, const param_type& __y)
4383 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
4384 friend bool operator!=(const param_type& __x, const param_type& __y)
4385 {return !(__x == __y);}
4386 };
4387
4388private:
4389 param_type __p_;
4390
4391public:
4392 // constructor and reset functions
4393 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4394 : __p_(param_type(__a, __b)) {}
4395 explicit weibull_distribution(const param_type& __p)
4396 : __p_(__p) {}
4397 void reset() {}
4398
4399 // generating functions
4400 template<class _URNG> result_type operator()(_URNG& __g)
4401 {return (*this)(__g, __p_);}
4402 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4403 {return __p.b() *
4404 _STD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
4405
4406 // property functions
4407 result_type a() const {return __p_.a();}
4408 result_type b() const {return __p_.b();}
4409
4410 param_type param() const {return __p_;}
4411 void param(const param_type& __p) {__p_ = __p;}
4412
4413 result_type min() const {return 0;}
4414 result_type max() const {return numeric_limits<result_type>::infinity();}
4415
Howard Hinnant9de6e302010-05-16 01:09:02 +00004416 friend bool operator==(const weibull_distribution& __x,
4417 const weibull_distribution& __y)
4418 {return __x.__p_ == __y.__p_;}
4419 friend bool operator!=(const weibull_distribution& __x,
4420 const weibull_distribution& __y)
4421 {return !(__x == __y);}
4422};
4423
4424template <class _CharT, class _Traits, class _RT>
4425basic_ostream<_CharT, _Traits>&
4426operator<<(basic_ostream<_CharT, _Traits>& __os,
4427 const weibull_distribution<_RT>& __x)
4428{
4429 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004430 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4431 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004432 _CharT __sp = __os.widen(' ');
4433 __os.fill(__sp);
4434 __os << __x.a() << __sp << __x.b();
4435 return __os;
4436}
4437
4438template <class _CharT, class _Traits, class _RT>
4439basic_istream<_CharT, _Traits>&
4440operator>>(basic_istream<_CharT, _Traits>& __is,
4441 weibull_distribution<_RT>& __x)
4442{
4443 typedef weibull_distribution<_RT> _Eng;
4444 typedef typename _Eng::result_type result_type;
4445 typedef typename _Eng::param_type param_type;
4446 __save_flags<_CharT, _Traits> _(__is);
4447 __is.flags(ios_base::dec | ios_base::skipws);
4448 result_type __a;
4449 result_type __b;
4450 __is >> __a >> __b;
4451 if (!__is.fail())
4452 __x.param(param_type(__a, __b));
4453 return __is;
4454}
4455
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004456template<class _RealType = double>
4457class extreme_value_distribution
4458{
4459public:
4460 // types
4461 typedef _RealType result_type;
4462
4463 class param_type
4464 {
4465 result_type __a_;
4466 result_type __b_;
4467 public:
4468 typedef extreme_value_distribution distribution_type;
4469
4470 explicit param_type(result_type __a = 0, result_type __b = 1)
4471 : __a_(__a), __b_(__b) {}
4472
4473 result_type a() const {return __a_;}
4474 result_type b() const {return __b_;}
4475
4476 friend bool operator==(const param_type& __x, const param_type& __y)
4477 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
4478 friend bool operator!=(const param_type& __x, const param_type& __y)
4479 {return !(__x == __y);}
4480 };
4481
4482private:
4483 param_type __p_;
4484
4485public:
4486 // constructor and reset functions
4487 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4488 : __p_(param_type(__a, __b)) {}
4489 explicit extreme_value_distribution(const param_type& __p)
4490 : __p_(__p) {}
4491 void reset() {}
4492
4493 // generating functions
4494 template<class _URNG> result_type operator()(_URNG& __g)
4495 {return (*this)(__g, __p_);}
4496 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4497
4498 // property functions
4499 result_type a() const {return __p_.a();}
4500 result_type b() const {return __p_.b();}
4501
4502 param_type param() const {return __p_;}
4503 void param(const param_type& __p) {__p_ = __p;}
4504
4505 result_type min() const {return -numeric_limits<result_type>::infinity();}
4506 result_type max() const {return numeric_limits<result_type>::infinity();}
4507
4508 friend bool operator==(const extreme_value_distribution& __x,
4509 const extreme_value_distribution& __y)
4510 {return __x.__p_ == __y.__p_;}
4511 friend bool operator!=(const extreme_value_distribution& __x,
4512 const extreme_value_distribution& __y)
4513 {return !(__x == __y);}
4514};
4515
4516template<class _RealType>
4517template<class _URNG>
4518_RealType
4519extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4520{
4521 return __p.a() - __p.b() *
4522 _STD::log(-_STD::log(1-uniform_real_distribution<result_type>()(__g)));
4523}
4524
4525template <class _CharT, class _Traits, class _RT>
4526basic_ostream<_CharT, _Traits>&
4527operator<<(basic_ostream<_CharT, _Traits>& __os,
4528 const extreme_value_distribution<_RT>& __x)
4529{
4530 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004531 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4532 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004533 _CharT __sp = __os.widen(' ');
4534 __os.fill(__sp);
4535 __os << __x.a() << __sp << __x.b();
4536 return __os;
4537}
4538
4539template <class _CharT, class _Traits, class _RT>
4540basic_istream<_CharT, _Traits>&
4541operator>>(basic_istream<_CharT, _Traits>& __is,
4542 extreme_value_distribution<_RT>& __x)
4543{
4544 typedef extreme_value_distribution<_RT> _Eng;
4545 typedef typename _Eng::result_type result_type;
4546 typedef typename _Eng::param_type param_type;
4547 __save_flags<_CharT, _Traits> _(__is);
4548 __is.flags(ios_base::dec | ios_base::skipws);
4549 result_type __a;
4550 result_type __b;
4551 __is >> __a >> __b;
4552 if (!__is.fail())
4553 __x.param(param_type(__a, __b));
4554 return __is;
4555}
4556
Howard Hinnantc7c49132010-05-13 17:58:28 +00004557// gamma_distribution
4558
4559template<class _RealType = double>
4560class gamma_distribution
4561{
4562public:
4563 // types
4564 typedef _RealType result_type;
4565
4566 class param_type
4567 {
4568 result_type __alpha_;
4569 result_type __beta_;
4570 public:
4571 typedef gamma_distribution distribution_type;
4572
4573 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4574 : __alpha_(__alpha), __beta_(__beta) {}
4575
4576 result_type alpha() const {return __alpha_;}
4577 result_type beta() const {return __beta_;}
4578
4579 friend bool operator==(const param_type& __x, const param_type& __y)
4580 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
4581 friend bool operator!=(const param_type& __x, const param_type& __y)
4582 {return !(__x == __y);}
4583 };
4584
4585private:
4586 param_type __p_;
4587
4588public:
4589 // constructors and reset functions
4590 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4591 : __p_(param_type(__alpha, __beta)) {}
4592 explicit gamma_distribution(const param_type& __p)
4593 : __p_(__p) {}
4594 void reset() {}
4595
4596 // generating functions
4597 template<class _URNG> result_type operator()(_URNG& __g)
4598 {return (*this)(__g, __p_);}
4599 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4600
4601 // property functions
4602 result_type alpha() const {return __p_.alpha();}
4603 result_type beta() const {return __p_.beta();}
4604
4605 param_type param() const {return __p_;}
4606 void param(const param_type& __p) {__p_ = __p;}
4607
4608 result_type min() const {return 0;}
Howard Hinnant324bb032010-08-22 00:02:43 +00004609 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantc7c49132010-05-13 17:58:28 +00004610
4611 friend bool operator==(const gamma_distribution& __x,
4612 const gamma_distribution& __y)
4613 {return __x.__p_ == __y.__p_;}
4614 friend bool operator!=(const gamma_distribution& __x,
4615 const gamma_distribution& __y)
4616 {return !(__x == __y);}
4617};
4618
4619template <class _RealType>
4620template<class _URNG>
4621_RealType
4622gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4623{
Howard Hinnantf417abe2010-05-14 18:43:10 +00004624 result_type __a = __p.alpha();
4625 uniform_real_distribution<result_type> __gen(0, 1);
4626 exponential_distribution<result_type> __egen;
4627 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00004628 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00004629 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004630 else if (__a > 1)
4631 {
4632 const result_type __b = __a - 1;
4633 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004634 while (true)
4635 {
4636 const result_type __u = __gen(__g);
4637 const result_type __v = __gen(__g);
4638 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004639 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004640 {
4641 const result_type __y = _STD::sqrt(__c / __w) *
4642 (__u - result_type(0.5));
4643 __x = __b + __y;
4644 if (__x >= 0)
4645 {
4646 const result_type __z = 64 * __w * __w * __w * __v * __v;
4647 if (__z <= 1 - 2 * __y * __y / __x)
4648 break;
4649 if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y))
4650 break;
4651 }
4652 }
4653 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00004654 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00004655 else // __a < 1
4656 {
4657 while (true)
4658 {
4659 const result_type __u = __gen(__g);
4660 const result_type __es = __egen(__g);
4661 if (__u <= 1 - __a)
4662 {
4663 __x = _STD::pow(__u, 1 / __a);
4664 if (__x <= __es)
4665 break;
4666 }
4667 else
4668 {
4669 const result_type __e = -_STD::log((1-__u)/__a);
4670 __x = _STD::pow(1 - __a + __a * __e, 1 / __a);
4671 if (__x <= __e + __es)
4672 break;
4673 }
4674 }
4675 }
4676 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00004677}
4678
4679template <class _CharT, class _Traits, class _RT>
4680basic_ostream<_CharT, _Traits>&
4681operator<<(basic_ostream<_CharT, _Traits>& __os,
4682 const gamma_distribution<_RT>& __x)
4683{
4684 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004685 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4686 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004687 _CharT __sp = __os.widen(' ');
4688 __os.fill(__sp);
4689 __os << __x.alpha() << __sp << __x.beta();
4690 return __os;
4691}
4692
4693template <class _CharT, class _Traits, class _RT>
4694basic_istream<_CharT, _Traits>&
4695operator>>(basic_istream<_CharT, _Traits>& __is,
4696 gamma_distribution<_RT>& __x)
4697{
4698 typedef gamma_distribution<_RT> _Eng;
4699 typedef typename _Eng::result_type result_type;
4700 typedef typename _Eng::param_type param_type;
4701 __save_flags<_CharT, _Traits> _(__is);
4702 __is.flags(ios_base::dec | ios_base::skipws);
4703 result_type __alpha;
4704 result_type __beta;
4705 __is >> __alpha >> __beta;
4706 if (!__is.fail())
4707 __x.param(param_type(__alpha, __beta));
4708 return __is;
4709}
Howard Hinnanta64111c2010-05-12 21:02:31 +00004710
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00004711// negative_binomial_distribution
4712
4713template<class _IntType = int>
4714class negative_binomial_distribution
4715{
4716public:
4717 // types
4718 typedef _IntType result_type;
4719
4720 class param_type
4721 {
4722 result_type __k_;
4723 double __p_;
4724 public:
4725 typedef negative_binomial_distribution distribution_type;
4726
4727 explicit param_type(result_type __k = 1, double __p = 0.5)
4728 : __k_(__k), __p_(__p) {}
4729
4730 result_type k() const {return __k_;}
4731 double p() const {return __p_;}
4732
4733 friend bool operator==(const param_type& __x, const param_type& __y)
4734 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
4735 friend bool operator!=(const param_type& __x, const param_type& __y)
4736 {return !(__x == __y);}
4737 };
4738
4739private:
4740 param_type __p_;
4741
4742public:
4743 // constructor and reset functions
4744 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
4745 : __p_(__k, __p) {}
4746 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
4747 void reset() {}
4748
4749 // generating functions
4750 template<class _URNG> result_type operator()(_URNG& __g)
4751 {return (*this)(__g, __p_);}
4752 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4753
4754 // property functions
4755 result_type k() const {return __p_.k();}
4756 double p() const {return __p_.p();}
4757
4758 param_type param() const {return __p_;}
4759 void param(const param_type& __p) {__p_ = __p;}
4760
4761 result_type min() const {return 0;}
4762 result_type max() const {return numeric_limits<result_type>::max();}
4763
4764 friend bool operator==(const negative_binomial_distribution& __x,
4765 const negative_binomial_distribution& __y)
4766 {return __x.__p_ == __y.__p_;}
4767 friend bool operator!=(const negative_binomial_distribution& __x,
4768 const negative_binomial_distribution& __y)
4769 {return !(__x == __y);}
4770};
4771
4772template <class _IntType>
4773template<class _URNG>
4774_IntType
4775negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4776{
4777 result_type __k = __pr.k();
4778 double __p = __pr.p();
4779 if (__k <= 21 * __p)
4780 {
4781 bernoulli_distribution __gen(__p);
4782 result_type __f = 0;
4783 result_type __s = 0;
4784 while (__s < __k)
4785 {
4786 if (__gen(__urng))
4787 ++__s;
4788 else
4789 ++__f;
4790 }
4791 return __f;
4792 }
4793 return poisson_distribution<result_type>(gamma_distribution<double>
4794 (__k, (1-__p)/__p)(__urng))(__urng);
4795}
4796
4797template <class _CharT, class _Traits, class _IntType>
4798basic_ostream<_CharT, _Traits>&
4799operator<<(basic_ostream<_CharT, _Traits>& __os,
4800 const negative_binomial_distribution<_IntType>& __x)
4801{
4802 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004803 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4804 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00004805 _CharT __sp = __os.widen(' ');
4806 __os.fill(__sp);
4807 return __os << __x.k() << __sp << __x.p();
4808}
4809
4810template <class _CharT, class _Traits, class _IntType>
4811basic_istream<_CharT, _Traits>&
4812operator>>(basic_istream<_CharT, _Traits>& __is,
4813 negative_binomial_distribution<_IntType>& __x)
4814{
4815 typedef negative_binomial_distribution<_IntType> _Eng;
4816 typedef typename _Eng::result_type result_type;
4817 typedef typename _Eng::param_type param_type;
4818 __save_flags<_CharT, _Traits> _(__is);
4819 __is.flags(ios_base::dec | ios_base::skipws);
4820 result_type __k;
4821 double __p;
4822 __is >> __k >> __p;
4823 if (!__is.fail())
4824 __x.param(param_type(__k, __p));
4825 return __is;
4826}
4827
Howard Hinnant34e8a572010-05-17 13:44:27 +00004828// geometric_distribution
4829
4830template<class _IntType = int>
4831class geometric_distribution
4832{
4833public:
4834 // types
4835 typedef _IntType result_type;
4836
4837 class param_type
4838 {
4839 double __p_;
4840 public:
4841 typedef geometric_distribution distribution_type;
4842
4843 explicit param_type(double __p = 0.5) : __p_(__p) {}
4844
4845 double p() const {return __p_;}
4846
4847 friend bool operator==(const param_type& __x, const param_type& __y)
4848 {return __x.__p_ == __y.__p_;}
4849 friend bool operator!=(const param_type& __x, const param_type& __y)
4850 {return !(__x == __y);}
4851 };
4852
4853private:
4854 param_type __p_;
4855
4856public:
4857 // constructors and reset functions
4858 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
4859 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
4860 void reset() {}
4861
4862 // generating functions
4863 template<class _URNG> result_type operator()(_URNG& __g)
4864 {return (*this)(__g, __p_);}
4865 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4866 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
4867
4868 // property functions
4869 double p() const {return __p_.p();}
4870
4871 param_type param() const {return __p_;}
4872 void param(const param_type& __p) {__p_ = __p;}
4873
4874 result_type min() const {return 0;}
4875 result_type max() const {return numeric_limits<result_type>::max();}
4876
4877 friend bool operator==(const geometric_distribution& __x,
4878 const geometric_distribution& __y)
4879 {return __x.__p_ == __y.__p_;}
4880 friend bool operator!=(const geometric_distribution& __x,
4881 const geometric_distribution& __y)
4882 {return !(__x == __y);}
4883};
4884
4885template <class _CharT, class _Traits, class _IntType>
4886basic_ostream<_CharT, _Traits>&
4887operator<<(basic_ostream<_CharT, _Traits>& __os,
4888 const geometric_distribution<_IntType>& __x)
4889{
4890 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004891 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4892 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:27 +00004893 return __os << __x.p();
4894}
4895
4896template <class _CharT, class _Traits, class _IntType>
4897basic_istream<_CharT, _Traits>&
4898operator>>(basic_istream<_CharT, _Traits>& __is,
4899 geometric_distribution<_IntType>& __x)
4900{
4901 typedef geometric_distribution<_IntType> _Eng;
4902 typedef typename _Eng::param_type param_type;
4903 __save_flags<_CharT, _Traits> _(__is);
4904 __is.flags(ios_base::dec | ios_base::skipws);
4905 double __p;
4906 __is >> __p;
4907 if (!__is.fail())
4908 __x.param(param_type(__p));
4909 return __is;
4910}
4911
Howard Hinnant97dc2f32010-05-15 23:36:00 +00004912// chi_squared_distribution
4913
4914template<class _RealType = double>
4915class chi_squared_distribution
4916{
4917public:
4918 // types
4919 typedef _RealType result_type;
4920
4921 class param_type
4922 {
4923 result_type __n_;
4924 public:
4925 typedef chi_squared_distribution distribution_type;
4926
4927 explicit param_type(result_type __n = 1) : __n_(__n) {}
4928
4929 result_type n() const {return __n_;}
4930
4931 friend bool operator==(const param_type& __x, const param_type& __y)
4932 {return __x.__n_ == __y.__n_;}
4933 friend bool operator!=(const param_type& __x, const param_type& __y)
4934 {return !(__x == __y);}
4935 };
4936
4937private:
4938 param_type __p_;
4939
4940public:
4941 // constructor and reset functions
4942 explicit chi_squared_distribution(result_type __n = 1)
4943 : __p_(param_type(__n)) {}
4944 explicit chi_squared_distribution(const param_type& __p)
4945 : __p_(__p) {}
4946 void reset() {}
4947
4948 // generating functions
4949 template<class _URNG> result_type operator()(_URNG& __g)
4950 {return (*this)(__g, __p_);}
4951 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4952 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
4953
4954 // property functions
4955 result_type n() const {return __p_.n();}
4956
4957 param_type param() const {return __p_;}
4958 void param(const param_type& __p) {__p_ = __p;}
4959
4960 result_type min() const {return 0;}
Howard Hinnant324bb032010-08-22 00:02:43 +00004961 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant97dc2f32010-05-15 23:36:00 +00004962
4963 friend bool operator==(const chi_squared_distribution& __x,
4964 const chi_squared_distribution& __y)
4965 {return __x.__p_ == __y.__p_;}
4966 friend bool operator!=(const chi_squared_distribution& __x,
4967 const chi_squared_distribution& __y)
4968 {return !(__x == __y);}
4969};
4970
4971template <class _CharT, class _Traits, class _RT>
4972basic_ostream<_CharT, _Traits>&
4973operator<<(basic_ostream<_CharT, _Traits>& __os,
4974 const chi_squared_distribution<_RT>& __x)
4975{
4976 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004977 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4978 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00004979 __os << __x.n();
4980 return __os;
4981}
4982
4983template <class _CharT, class _Traits, class _RT>
4984basic_istream<_CharT, _Traits>&
4985operator>>(basic_istream<_CharT, _Traits>& __is,
4986 chi_squared_distribution<_RT>& __x)
4987{
4988 typedef chi_squared_distribution<_RT> _Eng;
4989 typedef typename _Eng::result_type result_type;
4990 typedef typename _Eng::param_type param_type;
4991 __save_flags<_CharT, _Traits> _(__is);
4992 __is.flags(ios_base::dec | ios_base::skipws);
4993 result_type __n;
4994 __is >> __n;
4995 if (!__is.fail())
4996 __x.param(param_type(__n));
4997 return __is;
4998}
4999
Howard Hinnantd7d01132010-05-17 21:55:46 +00005000// cauchy_distribution
5001
5002template<class _RealType = double>
5003class cauchy_distribution
5004{
5005public:
5006 // types
5007 typedef _RealType result_type;
5008
5009 class param_type
5010 {
5011 result_type __a_;
5012 result_type __b_;
5013 public:
5014 typedef cauchy_distribution distribution_type;
5015
5016 explicit param_type(result_type __a = 0, result_type __b = 1)
5017 : __a_(__a), __b_(__b) {}
5018
5019 result_type a() const {return __a_;}
5020 result_type b() const {return __b_;}
5021
5022 friend bool operator==(const param_type& __x, const param_type& __y)
5023 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
5024 friend bool operator!=(const param_type& __x, const param_type& __y)
5025 {return !(__x == __y);}
5026 };
5027
5028private:
5029 param_type __p_;
5030
5031public:
5032 // constructor and reset functions
5033 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5034 : __p_(param_type(__a, __b)) {}
5035 explicit cauchy_distribution(const param_type& __p)
5036 : __p_(__p) {}
5037 void reset() {}
5038
5039 // generating functions
5040 template<class _URNG> result_type operator()(_URNG& __g)
5041 {return (*this)(__g, __p_);}
5042 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5043
5044 // property functions
5045 result_type a() const {return __p_.a();}
5046 result_type b() const {return __p_.b();}
5047
5048 param_type param() const {return __p_;}
5049 void param(const param_type& __p) {__p_ = __p;}
5050
5051 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnant324bb032010-08-22 00:02:43 +00005052 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005053
5054 friend bool operator==(const cauchy_distribution& __x,
5055 const cauchy_distribution& __y)
5056 {return __x.__p_ == __y.__p_;}
5057 friend bool operator!=(const cauchy_distribution& __x,
5058 const cauchy_distribution& __y)
5059 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005060};
5061
5062template <class _RealType>
5063template<class _URNG>
5064inline
5065_RealType
5066cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5067{
5068 uniform_real_distribution<result_type> __gen;
5069 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
5070 return __p.a() + __p.b() * _STD::tan(3.1415926535897932384626433832795 * __gen(__g));
5071}
5072
5073template <class _CharT, class _Traits, class _RT>
5074basic_ostream<_CharT, _Traits>&
5075operator<<(basic_ostream<_CharT, _Traits>& __os,
5076 const cauchy_distribution<_RT>& __x)
5077{
5078 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005079 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5080 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005081 _CharT __sp = __os.widen(' ');
5082 __os.fill(__sp);
5083 __os << __x.a() << __sp << __x.b();
5084 return __os;
5085}
5086
5087template <class _CharT, class _Traits, class _RT>
5088basic_istream<_CharT, _Traits>&
5089operator>>(basic_istream<_CharT, _Traits>& __is,
5090 cauchy_distribution<_RT>& __x)
5091{
5092 typedef cauchy_distribution<_RT> _Eng;
5093 typedef typename _Eng::result_type result_type;
5094 typedef typename _Eng::param_type param_type;
5095 __save_flags<_CharT, _Traits> _(__is);
5096 __is.flags(ios_base::dec | ios_base::skipws);
5097 result_type __a;
5098 result_type __b;
5099 __is >> __a >> __b;
5100 if (!__is.fail())
5101 __x.param(param_type(__a, __b));
5102 return __is;
5103}
5104
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005105// fisher_f_distribution
5106
5107template<class _RealType = double>
5108class fisher_f_distribution
5109{
5110public:
5111 // types
5112 typedef _RealType result_type;
5113
5114 class param_type
5115 {
5116 result_type __m_;
5117 result_type __n_;
5118 public:
5119 typedef fisher_f_distribution distribution_type;
5120
5121 explicit param_type(result_type __m = 1, result_type __n = 1)
5122 : __m_(__m), __n_(__n) {}
5123
5124 result_type m() const {return __m_;}
5125 result_type n() const {return __n_;}
5126
5127 friend bool operator==(const param_type& __x, const param_type& __y)
5128 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
5129 friend bool operator!=(const param_type& __x, const param_type& __y)
5130 {return !(__x == __y);}
5131 };
5132
5133private:
5134 param_type __p_;
5135
5136public:
5137 // constructor and reset functions
5138 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5139 : __p_(param_type(__m, __n)) {}
5140 explicit fisher_f_distribution(const param_type& __p)
5141 : __p_(__p) {}
5142 void reset() {}
5143
5144 // generating functions
5145 template<class _URNG> result_type operator()(_URNG& __g)
5146 {return (*this)(__g, __p_);}
5147 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5148
5149 // property functions
5150 result_type m() const {return __p_.m();}
5151 result_type n() const {return __p_.n();}
5152
5153 param_type param() const {return __p_;}
5154 void param(const param_type& __p) {__p_ = __p;}
5155
5156 result_type min() const {return 0;}
Howard Hinnant324bb032010-08-22 00:02:43 +00005157 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005158
5159 friend bool operator==(const fisher_f_distribution& __x,
5160 const fisher_f_distribution& __y)
5161 {return __x.__p_ == __y.__p_;}
5162 friend bool operator!=(const fisher_f_distribution& __x,
5163 const fisher_f_distribution& __y)
5164 {return !(__x == __y);}
5165};
5166
5167template <class _RealType>
5168template<class _URNG>
5169_RealType
5170fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5171{
5172 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5173 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5174 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5175}
5176
5177template <class _CharT, class _Traits, class _RT>
5178basic_ostream<_CharT, _Traits>&
5179operator<<(basic_ostream<_CharT, _Traits>& __os,
5180 const fisher_f_distribution<_RT>& __x)
5181{
5182 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005183 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5184 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005185 _CharT __sp = __os.widen(' ');
5186 __os.fill(__sp);
5187 __os << __x.m() << __sp << __x.n();
5188 return __os;
5189}
5190
5191template <class _CharT, class _Traits, class _RT>
5192basic_istream<_CharT, _Traits>&
5193operator>>(basic_istream<_CharT, _Traits>& __is,
5194 fisher_f_distribution<_RT>& __x)
5195{
5196 typedef fisher_f_distribution<_RT> _Eng;
5197 typedef typename _Eng::result_type result_type;
5198 typedef typename _Eng::param_type param_type;
5199 __save_flags<_CharT, _Traits> _(__is);
5200 __is.flags(ios_base::dec | ios_base::skipws);
5201 result_type __m;
5202 result_type __n;
5203 __is >> __m >> __n;
5204 if (!__is.fail())
5205 __x.param(param_type(__m, __n));
5206 return __is;
5207}
5208
Howard Hinnant551d8e42010-05-19 01:53:57 +00005209// student_t_distribution
5210
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005211template<class _RealType = double>
5212class student_t_distribution
5213{
5214public:
5215 // types
5216 typedef _RealType result_type;
5217
5218 class param_type
5219 {
5220 result_type __n_;
5221 public:
5222 typedef student_t_distribution distribution_type;
5223
5224 explicit param_type(result_type __n = 1) : __n_(__n) {}
5225
5226 result_type n() const {return __n_;}
5227
5228 friend bool operator==(const param_type& __x, const param_type& __y)
5229 {return __x.__n_ == __y.__n_;}
5230 friend bool operator!=(const param_type& __x, const param_type& __y)
5231 {return !(__x == __y);}
5232 };
5233
5234private:
5235 param_type __p_;
5236 normal_distribution<result_type> __nd_;
5237
5238public:
5239 // constructor and reset functions
5240 explicit student_t_distribution(result_type __n = 1)
5241 : __p_(param_type(__n)) {}
5242 explicit student_t_distribution(const param_type& __p)
5243 : __p_(__p) {}
5244 void reset() {__nd_.reset();}
5245
5246 // generating functions
5247 template<class _URNG> result_type operator()(_URNG& __g)
5248 {return (*this)(__g, __p_);}
5249 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5250
5251 // property functions
5252 result_type n() const {return __p_.n();}
5253
5254 param_type param() const {return __p_;}
Howard Hinnant551d8e42010-05-19 01:53:57 +00005255 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005256
5257 result_type min() const {return -numeric_limits<result_type>::infinity();}
5258 result_type max() const {return numeric_limits<result_type>::infinity();}
5259
5260 friend bool operator==(const student_t_distribution& __x,
5261 const student_t_distribution& __y)
5262 {return __x.__p_ == __y.__p_;}
5263 friend bool operator!=(const student_t_distribution& __x,
5264 const student_t_distribution& __y)
5265 {return !(__x == __y);}
5266};
5267
5268template <class _RealType>
5269template<class _URNG>
5270_RealType
5271student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5272{
5273 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
5274 return __nd_(__g) * _STD::sqrt(__p.n()/__gd(__g));
5275}
5276
5277template <class _CharT, class _Traits, class _RT>
5278basic_ostream<_CharT, _Traits>&
5279operator<<(basic_ostream<_CharT, _Traits>& __os,
5280 const student_t_distribution<_RT>& __x)
5281{
5282 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005283 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5284 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005285 __os << __x.n();
5286 return __os;
5287}
5288
5289template <class _CharT, class _Traits, class _RT>
5290basic_istream<_CharT, _Traits>&
5291operator>>(basic_istream<_CharT, _Traits>& __is,
5292 student_t_distribution<_RT>& __x)
5293{
5294 typedef student_t_distribution<_RT> _Eng;
5295 typedef typename _Eng::result_type result_type;
5296 typedef typename _Eng::param_type param_type;
5297 __save_flags<_CharT, _Traits> _(__is);
5298 __is.flags(ios_base::dec | ios_base::skipws);
5299 result_type __n;
5300 __is >> __n;
5301 if (!__is.fail())
5302 __x.param(param_type(__n));
5303 return __is;
5304}
5305
Howard Hinnant551d8e42010-05-19 01:53:57 +00005306// discrete_distribution
5307
5308template<class _IntType = int>
5309class discrete_distribution
5310{
5311public:
5312 // types
5313 typedef _IntType result_type;
5314
5315 class param_type
5316 {
5317 vector<double> __p_;
5318 public:
5319 typedef discrete_distribution distribution_type;
5320
5321 param_type() {}
5322 template<class _InputIterator>
5323 param_type(_InputIterator __f, _InputIterator __l)
5324 : __p_(__f, __l) {__init();}
5325 param_type(initializer_list<double> __wl)
5326 : __p_(__wl.begin(), __wl.end()) {__init();}
5327 template<class _UnaryOperation>
5328 param_type(size_t __nw, double __xmin, double __xmax,
5329 _UnaryOperation __fw);
5330
5331 vector<double> probabilities() const;
5332
5333 friend bool operator==(const param_type& __x, const param_type& __y)
5334 {return __x.__p_ == __y.__p_;}
5335 friend bool operator!=(const param_type& __x, const param_type& __y)
5336 {return !(__x == __y);}
5337
5338 private:
5339 void __init();
5340
5341 friend class discrete_distribution;
5342
5343 template <class _CharT, class _Traits, class _IT>
5344 friend
5345 basic_ostream<_CharT, _Traits>&
5346 operator<<(basic_ostream<_CharT, _Traits>& __os,
5347 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005348
Howard Hinnant551d8e42010-05-19 01:53:57 +00005349 template <class _CharT, class _Traits, class _IT>
5350 friend
5351 basic_istream<_CharT, _Traits>&
5352 operator>>(basic_istream<_CharT, _Traits>& __is,
5353 discrete_distribution<_IT>& __x);
5354 };
5355
5356private:
5357 param_type __p_;
5358
5359public:
5360 // constructor and reset functions
5361 discrete_distribution() {}
5362 template<class _InputIterator>
5363 discrete_distribution(_InputIterator __f, _InputIterator __l)
5364 : __p_(__f, __l) {}
5365 discrete_distribution(initializer_list<double> __wl)
5366 : __p_(__wl) {}
5367 template<class _UnaryOperation>
5368 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5369 _UnaryOperation __fw)
5370 : __p_(__nw, __xmin, __xmax, __fw) {}
5371 explicit discrete_distribution(const param_type& __p)
5372 : __p_(__p) {}
5373 void reset() {}
5374
5375 // generating functions
5376 template<class _URNG> result_type operator()(_URNG& __g)
5377 {return (*this)(__g, __p_);}
5378 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5379
5380 // property functions
5381 vector<double> probabilities() const {return __p_.probabilities();}
5382
5383 param_type param() const {return __p_;}
5384 void param(const param_type& __p) {__p_ = __p;}
5385
5386 result_type min() const {return 0;}
5387 result_type max() const {return __p_.__p_.size();}
5388
5389 friend bool operator==(const discrete_distribution& __x,
5390 const discrete_distribution& __y)
5391 {return __x.__p_ == __y.__p_;}
5392 friend bool operator!=(const discrete_distribution& __x,
5393 const discrete_distribution& __y)
5394 {return !(__x == __y);}
5395
5396 template <class _CharT, class _Traits, class _IT>
5397 friend
5398 basic_ostream<_CharT, _Traits>&
5399 operator<<(basic_ostream<_CharT, _Traits>& __os,
5400 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005401
Howard Hinnant551d8e42010-05-19 01:53:57 +00005402 template <class _CharT, class _Traits, class _IT>
5403 friend
5404 basic_istream<_CharT, _Traits>&
5405 operator>>(basic_istream<_CharT, _Traits>& __is,
5406 discrete_distribution<_IT>& __x);
5407};
5408
5409template<class _IntType>
5410template<class _UnaryOperation>
5411discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5412 double __xmin,
5413 double __xmax,
5414 _UnaryOperation __fw)
5415{
5416 if (__nw > 1)
5417 {
5418 __p_.reserve(__nw - 1);
5419 double __d = (__xmax - __xmin) / __nw;
5420 double __d2 = __d / 2;
5421 for (size_t __k = 0; __k < __nw; ++__k)
5422 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5423 __init();
5424 }
5425}
5426
5427template<class _IntType>
5428void
5429discrete_distribution<_IntType>::param_type::__init()
5430{
5431 if (!__p_.empty())
5432 {
5433 if (__p_.size() > 1)
5434 {
5435 double __s = _STD::accumulate(__p_.begin(), __p_.end(), 0.0);
5436 for (_STD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
5437 __i < __e; ++__i)
5438 *__i /= __s;
5439 vector<double> __t(__p_.size() - 1);
5440 _STD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
5441 swap(__p_, __t);
5442 }
5443 else
5444 {
5445 __p_.clear();
5446 __p_.shrink_to_fit();
5447 }
5448 }
5449}
5450
5451template<class _IntType>
5452vector<double>
5453discrete_distribution<_IntType>::param_type::probabilities() const
5454{
5455 size_t __n = __p_.size();
5456 _STD::vector<double> __p(__n+1);
5457 _STD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
5458 if (__n > 0)
5459 __p[__n] = 1 - __p_[__n-1];
5460 else
5461 __p[0] = 1;
5462 return __p;
5463}
5464
5465template<class _IntType>
5466template<class _URNG>
5467_IntType
5468discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
5469{
5470 uniform_real_distribution<double> __gen;
5471 return static_cast<_IntType>(
5472 _STD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
5473 __p.__p_.begin());
5474}
5475
5476template <class _CharT, class _Traits, class _IT>
5477basic_ostream<_CharT, _Traits>&
5478operator<<(basic_ostream<_CharT, _Traits>& __os,
5479 const discrete_distribution<_IT>& __x)
5480{
5481 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005482 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5483 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005484 _CharT __sp = __os.widen(' ');
5485 __os.fill(__sp);
5486 size_t __n = __x.__p_.__p_.size();
5487 __os << __n;
5488 for (size_t __i = 0; __i < __n; ++__i)
5489 __os << __sp << __x.__p_.__p_[__i];
5490 return __os;
5491}
5492
5493template <class _CharT, class _Traits, class _IT>
5494basic_istream<_CharT, _Traits>&
5495operator>>(basic_istream<_CharT, _Traits>& __is,
5496 discrete_distribution<_IT>& __x)
5497{
5498 typedef discrete_distribution<_IT> _Eng;
5499 typedef typename _Eng::result_type result_type;
5500 typedef typename _Eng::param_type param_type;
5501 __save_flags<_CharT, _Traits> _(__is);
5502 __is.flags(ios_base::dec | ios_base::skipws);
5503 size_t __n;
5504 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005505 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005506 for (size_t __i = 0; __i < __n; ++__i)
5507 __is >> __p[__i];
5508 if (!__is.fail())
5509 swap(__x.__p_.__p_, __p);
5510 return __is;
5511}
5512
Howard Hinnantd6d11712010-05-20 15:11:46 +00005513// piecewise_constant_distribution
5514
5515template<class _RealType = double>
5516class piecewise_constant_distribution
5517{
5518public:
5519 // types
5520 typedef _RealType result_type;
5521
5522 class param_type
5523 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005524 typedef typename common_type<double, result_type>::type __area_type;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005525 vector<result_type> __b_;
Howard Hinnant2a592542010-05-24 00:35:40 +00005526 vector<double> __densities_;
5527 vector<__area_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005528 public:
5529 typedef piecewise_constant_distribution distribution_type;
5530
5531 param_type();
5532 template<class _InputIteratorB, class _InputIteratorW>
5533 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5534 _InputIteratorW __fW);
5535 template<class _UnaryOperation>
5536 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
5537 template<class _UnaryOperation>
5538 param_type(size_t __nw, result_type __xmin, result_type __xmax,
5539 _UnaryOperation __fw);
5540
5541 vector<result_type> intervals() const {return __b_;}
Howard Hinnant2a592542010-05-24 00:35:40 +00005542 vector<double> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005543
5544 friend bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:40 +00005545 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005546 friend bool operator!=(const param_type& __x, const param_type& __y)
5547 {return !(__x == __y);}
5548
5549 private:
5550 void __init();
5551
5552 friend class piecewise_constant_distribution;
5553
5554 template <class _CharT, class _Traits, class _RT>
5555 friend
5556 basic_ostream<_CharT, _Traits>&
5557 operator<<(basic_ostream<_CharT, _Traits>& __os,
5558 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005559
Howard Hinnantd6d11712010-05-20 15:11:46 +00005560 template <class _CharT, class _Traits, class _RT>
5561 friend
5562 basic_istream<_CharT, _Traits>&
5563 operator>>(basic_istream<_CharT, _Traits>& __is,
5564 piecewise_constant_distribution<_RT>& __x);
5565 };
5566
5567private:
5568 param_type __p_;
5569
5570public:
5571 // constructor and reset functions
5572 piecewise_constant_distribution() {}
5573 template<class _InputIteratorB, class _InputIteratorW>
5574 piecewise_constant_distribution(_InputIteratorB __fB,
5575 _InputIteratorB __lB,
5576 _InputIteratorW __fW)
5577 : __p_(__fB, __lB, __fW) {}
5578
5579 template<class _UnaryOperation>
5580 piecewise_constant_distribution(initializer_list<result_type> __bl,
5581 _UnaryOperation __fw)
5582 : __p_(__bl, __fw) {}
5583
5584 template<class _UnaryOperation>
5585 piecewise_constant_distribution(size_t __nw, result_type __xmin,
5586 result_type __xmax, _UnaryOperation __fw)
5587 : __p_(__nw, __xmin, __xmax, __fw) {}
5588
5589 explicit piecewise_constant_distribution(const param_type& __p)
5590 : __p_(__p) {}
5591
5592 void reset() {}
5593
5594 // generating functions
5595 template<class _URNG> result_type operator()(_URNG& __g)
5596 {return (*this)(__g, __p_);}
5597 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5598
5599 // property functions
5600 vector<result_type> intervals() const {return __p_.intervals();}
5601 vector<double> densities() const {return __p_.densities();}
5602
5603 param_type param() const {return __p_;}
5604 void param(const param_type& __p) {__p_ = __p;}
5605
5606 result_type min() const {return __p_.__b_.front();}
5607 result_type max() const {return __p_.__b_.back();}
5608
5609 friend bool operator==(const piecewise_constant_distribution& __x,
5610 const piecewise_constant_distribution& __y)
5611 {return __x.__p_ == __y.__p_;}
5612 friend bool operator!=(const piecewise_constant_distribution& __x,
5613 const piecewise_constant_distribution& __y)
5614 {return !(__x == __y);}
5615
5616 template <class _CharT, class _Traits, class _RT>
5617 friend
5618 basic_ostream<_CharT, _Traits>&
5619 operator<<(basic_ostream<_CharT, _Traits>& __os,
5620 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005621
Howard Hinnantd6d11712010-05-20 15:11:46 +00005622 template <class _CharT, class _Traits, class _RT>
5623 friend
5624 basic_istream<_CharT, _Traits>&
5625 operator>>(basic_istream<_CharT, _Traits>& __is,
5626 piecewise_constant_distribution<_RT>& __x);
5627};
5628
5629template<class _RealType>
5630void
5631piecewise_constant_distribution<_RealType>::param_type::__init()
5632{
Howard Hinnant2a592542010-05-24 00:35:40 +00005633 // __densities_ contains non-normalized areas
5634 __area_type __total_area = _STD::accumulate(__densities_.begin(),
5635 __densities_.end(),
Howard Hinnant324bb032010-08-22 00:02:43 +00005636 __area_type());
Howard Hinnant2a592542010-05-24 00:35:40 +00005637 for (size_t __i = 0; __i < __densities_.size(); ++__i)
5638 __densities_[__i] /= __total_area;
5639 // __densities_ contains normalized areas
5640 __areas_.assign(__densities_.size(), __area_type());
5641 _STD::partial_sum(__densities_.begin(), __densities_.end() - 1,
5642 __areas_.begin() + 1);
5643 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
5644 __densities_.back() = 1 - __areas_.back(); // correct round off error
5645 for (size_t __i = 0; __i < __densities_.size(); ++__i)
5646 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
5647 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:46 +00005648}
5649
5650template<class _RealType>
5651piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:40 +00005652 : __b_(2),
Howard Hinnant54305402010-05-25 00:27:34 +00005653 __densities_(1, 1.0),
5654 __areas_(1, 0.0)
Howard Hinnantd6d11712010-05-20 15:11:46 +00005655{
5656 __b_[1] = 1;
5657}
5658
5659template<class _RealType>
5660template<class _InputIteratorB, class _InputIteratorW>
5661piecewise_constant_distribution<_RealType>::param_type::param_type(
5662 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
5663 : __b_(__fB, __lB)
5664{
5665 if (__b_.size() < 2)
5666 {
5667 __b_.resize(2);
5668 __b_[0] = 0;
5669 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00005670 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00005671 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005672 }
5673 else
5674 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005675 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005676 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:40 +00005677 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005678 __init();
5679 }
5680}
5681
5682template<class _RealType>
5683template<class _UnaryOperation>
5684piecewise_constant_distribution<_RealType>::param_type::param_type(
5685 initializer_list<result_type> __bl, _UnaryOperation __fw)
5686 : __b_(__bl.begin(), __bl.end())
5687{
5688 if (__b_.size() < 2)
5689 {
5690 __b_.resize(2);
5691 __b_[0] = 0;
5692 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00005693 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00005694 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005695 }
5696 else
5697 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005698 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005699 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005700 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00005701 __init();
5702 }
5703}
5704
5705template<class _RealType>
5706template<class _UnaryOperation>
5707piecewise_constant_distribution<_RealType>::param_type::param_type(
5708 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
5709 : __b_(__nw == 0 ? 2 : __nw + 1)
5710{
5711 size_t __n = __b_.size() - 1;
5712 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:40 +00005713 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005714 for (size_t __i = 0; __i < __n; ++__i)
5715 {
5716 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:40 +00005717 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00005718 }
5719 __b_[__n] = __xmax;
5720 __init();
5721}
5722
5723template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00005724template<class _URNG>
5725_RealType
5726piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5727{
5728 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005729 result_type __u = _Gen()(__g);
Howard Hinnant2a592542010-05-24 00:35:40 +00005730 ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
5731 static_cast<double>(__u)) - __p.__areas_.begin() - 1;
5732 return static_cast<result_type>((__u - __p.__areas_[__k]) / __p.__densities_[__k]
5733 + __p.__b_[__k]);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005734}
5735
5736template <class _CharT, class _Traits, class _RT>
5737basic_ostream<_CharT, _Traits>&
5738operator<<(basic_ostream<_CharT, _Traits>& __os,
5739 const piecewise_constant_distribution<_RT>& __x)
5740{
5741 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005742 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5743 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005744 _CharT __sp = __os.widen(' ');
5745 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:40 +00005746 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00005747 __os << __n;
5748 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005749 __os << __sp << __x.__p_.__b_[__i];
5750 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00005751 __os << __sp << __n;
5752 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005753 __os << __sp << __x.__p_.__densities_[__i];
5754 __n = __x.__p_.__areas_.size();
5755 __os << __sp << __n;
5756 for (size_t __i = 0; __i < __n; ++__i)
5757 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00005758 return __os;
5759}
5760
5761template <class _CharT, class _Traits, class _RT>
5762basic_istream<_CharT, _Traits>&
5763operator>>(basic_istream<_CharT, _Traits>& __is,
5764 piecewise_constant_distribution<_RT>& __x)
5765{
5766 typedef piecewise_constant_distribution<_RT> _Eng;
5767 typedef typename _Eng::result_type result_type;
5768 typedef typename _Eng::param_type param_type;
Howard Hinnant2a592542010-05-24 00:35:40 +00005769 typedef typename param_type::__area_type __area_type;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005770 __save_flags<_CharT, _Traits> _(__is);
5771 __is.flags(ios_base::dec | ios_base::skipws);
5772 size_t __n;
5773 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005774 vector<result_type> __b(__n);
5775 for (size_t __i = 0; __i < __n; ++__i)
5776 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:40 +00005777 __is >> __n;
5778 vector<double> __densities(__n);
5779 for (size_t __i = 0; __i < __n; ++__i)
5780 __is >> __densities[__i];
5781 __is >> __n;
5782 vector<__area_type> __areas(__n);
5783 for (size_t __i = 0; __i < __n; ++__i)
5784 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00005785 if (!__is.fail())
5786 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00005787 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:40 +00005788 swap(__x.__p_.__densities_, __densities);
5789 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005790 }
5791 return __is;
5792}
5793
Howard Hinnant54305402010-05-25 00:27:34 +00005794// piecewise_linear_distribution
5795
5796template<class _RealType = double>
5797class piecewise_linear_distribution
5798{
5799public:
5800 // types
5801 typedef _RealType result_type;
5802
5803 class param_type
5804 {
5805 typedef typename common_type<double, result_type>::type __area_type;
5806 vector<result_type> __b_;
5807 vector<double> __densities_;
5808 vector<__area_type> __areas_;
5809 public:
5810 typedef piecewise_linear_distribution distribution_type;
5811
5812 param_type();
5813 template<class _InputIteratorB, class _InputIteratorW>
5814 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5815 _InputIteratorW __fW);
5816 template<class _UnaryOperation>
5817 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
5818 template<class _UnaryOperation>
5819 param_type(size_t __nw, result_type __xmin, result_type __xmax,
5820 _UnaryOperation __fw);
5821
5822 vector<result_type> intervals() const {return __b_;}
5823 vector<double> densities() const {return __densities_;}
5824
5825 friend bool operator==(const param_type& __x, const param_type& __y)
5826 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
5827 friend bool operator!=(const param_type& __x, const param_type& __y)
5828 {return !(__x == __y);}
5829
5830 private:
5831 void __init();
5832
5833 friend class piecewise_linear_distribution;
5834
5835 template <class _CharT, class _Traits, class _RT>
5836 friend
5837 basic_ostream<_CharT, _Traits>&
5838 operator<<(basic_ostream<_CharT, _Traits>& __os,
5839 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005840
Howard Hinnant54305402010-05-25 00:27:34 +00005841 template <class _CharT, class _Traits, class _RT>
5842 friend
5843 basic_istream<_CharT, _Traits>&
5844 operator>>(basic_istream<_CharT, _Traits>& __is,
5845 piecewise_linear_distribution<_RT>& __x);
5846 };
5847
5848private:
5849 param_type __p_;
5850
5851public:
5852 // constructor and reset functions
5853 piecewise_linear_distribution() {}
5854 template<class _InputIteratorB, class _InputIteratorW>
5855 piecewise_linear_distribution(_InputIteratorB __fB,
5856 _InputIteratorB __lB,
5857 _InputIteratorW __fW)
5858 : __p_(__fB, __lB, __fW) {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005859
Howard Hinnant54305402010-05-25 00:27:34 +00005860 template<class _UnaryOperation>
5861 piecewise_linear_distribution(initializer_list<result_type> __bl,
5862 _UnaryOperation __fw)
5863 : __p_(__bl, __fw) {}
5864
5865 template<class _UnaryOperation>
5866 piecewise_linear_distribution(size_t __nw, result_type __xmin,
5867 result_type __xmax, _UnaryOperation __fw)
5868 : __p_(__nw, __xmin, __xmax, __fw) {}
5869
5870 explicit piecewise_linear_distribution(const param_type& __p)
5871 : __p_(__p) {}
5872
5873 void reset() {}
5874
5875 // generating functions
5876 template<class _URNG> result_type operator()(_URNG& __g)
5877 {return (*this)(__g, __p_);}
5878 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5879
5880 // property functions
5881 vector<result_type> intervals() const {return __p_.intervals();}
5882 vector<double> densities() const {return __p_.densities();}
5883
5884 param_type param() const {return __p_;}
5885 void param(const param_type& __p) {__p_ = __p;}
5886
5887 result_type min() const {return __p_.__b_.front();}
5888 result_type max() const {return __p_.__b_.back();}
5889
5890 friend bool operator==(const piecewise_linear_distribution& __x,
5891 const piecewise_linear_distribution& __y)
5892 {return __x.__p_ == __y.__p_;}
5893 friend bool operator!=(const piecewise_linear_distribution& __x,
5894 const piecewise_linear_distribution& __y)
5895 {return !(__x == __y);}
5896
5897 template <class _CharT, class _Traits, class _RT>
5898 friend
5899 basic_ostream<_CharT, _Traits>&
5900 operator<<(basic_ostream<_CharT, _Traits>& __os,
5901 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005902
Howard Hinnant54305402010-05-25 00:27:34 +00005903 template <class _CharT, class _Traits, class _RT>
5904 friend
5905 basic_istream<_CharT, _Traits>&
5906 operator>>(basic_istream<_CharT, _Traits>& __is,
5907 piecewise_linear_distribution<_RT>& __x);
5908};
5909
5910template<class _RealType>
5911void
5912piecewise_linear_distribution<_RealType>::param_type::__init()
5913{
5914 __areas_.assign(__densities_.size() - 1, __area_type());
5915 __area_type _S = 0;
5916 for (size_t __i = 0; __i < __areas_.size(); ++__i)
5917 {
5918 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
5919 (__b_[__i+1] - __b_[__i]) * .5;
5920 _S += __areas_[__i];
5921 }
5922 for (size_t __i = __areas_.size(); __i > 1;)
5923 {
5924 --__i;
5925 __areas_[__i] = __areas_[__i-1] / _S;
5926 }
5927 __areas_[0] = 0;
5928 for (size_t __i = 1; __i < __areas_.size(); ++__i)
5929 __areas_[__i] += __areas_[__i-1];
5930 for (size_t __i = 0; __i < __densities_.size(); ++__i)
5931 __densities_[__i] /= _S;
5932}
5933
5934template<class _RealType>
5935piecewise_linear_distribution<_RealType>::param_type::param_type()
5936 : __b_(2),
5937 __densities_(2, 1.0),
5938 __areas_(1, 0.0)
5939{
5940 __b_[1] = 1;
5941}
5942
5943template<class _RealType>
5944template<class _InputIteratorB, class _InputIteratorW>
5945piecewise_linear_distribution<_RealType>::param_type::param_type(
5946 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
5947 : __b_(__fB, __lB)
5948{
5949 if (__b_.size() < 2)
5950 {
5951 __b_.resize(2);
5952 __b_[0] = 0;
5953 __b_[1] = 1;
5954 __densities_.assign(2, 1.0);
5955 __areas_.assign(1, 0.0);
5956 }
5957 else
5958 {
5959 __densities_.reserve(__b_.size());
5960 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
5961 __densities_.push_back(*__fW);
5962 __init();
5963 }
5964}
5965
5966template<class _RealType>
5967template<class _UnaryOperation>
5968piecewise_linear_distribution<_RealType>::param_type::param_type(
5969 initializer_list<result_type> __bl, _UnaryOperation __fw)
5970 : __b_(__bl.begin(), __bl.end())
5971{
5972 if (__b_.size() < 2)
5973 {
5974 __b_.resize(2);
5975 __b_[0] = 0;
5976 __b_[1] = 1;
5977 __densities_.assign(2, 1.0);
5978 __areas_.assign(1, 0.0);
5979 }
5980 else
5981 {
5982 __densities_.reserve(__b_.size());
5983 for (size_t __i = 0; __i < __b_.size(); ++__i)
5984 __densities_.push_back(__fw(__b_[__i]));
5985 __init();
5986 }
5987}
5988
5989template<class _RealType>
5990template<class _UnaryOperation>
5991piecewise_linear_distribution<_RealType>::param_type::param_type(
5992 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
5993 : __b_(__nw == 0 ? 2 : __nw + 1)
5994{
5995 size_t __n = __b_.size() - 1;
5996 result_type __d = (__xmax - __xmin) / __n;
5997 __densities_.reserve(__b_.size());
5998 for (size_t __i = 0; __i < __n; ++__i)
5999 {
6000 __b_[__i] = __xmin + __i * __d;
6001 __densities_.push_back(__fw(__b_[__i]));
6002 }
6003 __b_[__n] = __xmax;
6004 __densities_.push_back(__fw(__b_[__n]));
6005 __init();
6006}
6007
6008template<class _RealType>
6009template<class _URNG>
6010_RealType
6011piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6012{
6013 typedef uniform_real_distribution<result_type> _Gen;
6014 result_type __u = _Gen()(__g);
6015 ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
6016 static_cast<double>(__u)) - __p.__areas_.begin() - 1;
6017 __u -= __p.__areas_[__k];
6018 const double __dk = __p.__densities_[__k];
6019 const double __dk1 = __p.__densities_[__k+1];
6020 const double __deltad = __dk1 - __dk;
6021 const result_type __bk = __p.__b_[__k];
6022 if (__deltad == 0)
6023 return static_cast<result_type>(__u / __dk + __bk);
6024 const result_type __bk1 = __p.__b_[__k+1];
6025 const result_type __deltab = __bk1 - __bk;
Howard Hinnant324bb032010-08-22 00:02:43 +00006026 return static_cast<result_type>((__bk * __dk1 - __bk1 * __dk +
Howard Hinnant54305402010-05-25 00:27:34 +00006027 _STD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
6028 __deltad);
6029}
6030
6031template <class _CharT, class _Traits, class _RT>
6032basic_ostream<_CharT, _Traits>&
6033operator<<(basic_ostream<_CharT, _Traits>& __os,
6034 const piecewise_linear_distribution<_RT>& __x)
6035{
6036 __save_flags<_CharT, _Traits> _(__os);
6037 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6038 ios_base::scientific);
6039 _CharT __sp = __os.widen(' ');
6040 __os.fill(__sp);
6041 size_t __n = __x.__p_.__b_.size();
6042 __os << __n;
6043 for (size_t __i = 0; __i < __n; ++__i)
6044 __os << __sp << __x.__p_.__b_[__i];
6045 __n = __x.__p_.__densities_.size();
6046 __os << __sp << __n;
6047 for (size_t __i = 0; __i < __n; ++__i)
6048 __os << __sp << __x.__p_.__densities_[__i];
6049 __n = __x.__p_.__areas_.size();
6050 __os << __sp << __n;
6051 for (size_t __i = 0; __i < __n; ++__i)
6052 __os << __sp << __x.__p_.__areas_[__i];
6053 return __os;
6054}
6055
6056template <class _CharT, class _Traits, class _RT>
6057basic_istream<_CharT, _Traits>&
6058operator>>(basic_istream<_CharT, _Traits>& __is,
6059 piecewise_linear_distribution<_RT>& __x)
6060{
6061 typedef piecewise_linear_distribution<_RT> _Eng;
6062 typedef typename _Eng::result_type result_type;
6063 typedef typename _Eng::param_type param_type;
6064 typedef typename param_type::__area_type __area_type;
6065 __save_flags<_CharT, _Traits> _(__is);
6066 __is.flags(ios_base::dec | ios_base::skipws);
6067 size_t __n;
6068 __is >> __n;
6069 vector<result_type> __b(__n);
6070 for (size_t __i = 0; __i < __n; ++__i)
6071 __is >> __b[__i];
6072 __is >> __n;
6073 vector<double> __densities(__n);
6074 for (size_t __i = 0; __i < __n; ++__i)
6075 __is >> __densities[__i];
6076 __is >> __n;
6077 vector<__area_type> __areas(__n);
6078 for (size_t __i = 0; __i < __n; ++__i)
6079 __is >> __areas[__i];
6080 if (!__is.fail())
6081 {
6082 swap(__x.__p_.__b_, __b);
6083 swap(__x.__p_.__densities_, __densities);
6084 swap(__x.__p_.__areas_, __areas);
6085 }
6086 return __is;
6087}
6088
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00006089_LIBCPP_END_NAMESPACE_STD
6090
6091#endif // _LIBCPP_RANDOM