blob: 86663afab9778e721a5bc864c09b00312b5994f5 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- random -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_RANDOM
12#define _LIBCPP_RANDOM
13
14/*
15 random synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22// Engines
23
24template <class UIntType, UIntType a, UIntType c, UIntType m>
25class linear_congruential_engine
26{
27public:
28 // types
29 typedef UIntType result_type;
30
31 // engine characteristics
32 static constexpr result_type multiplier = a;
33 static constexpr result_type increment = c;
34 static constexpr result_type modulus = m;
35 static constexpr result_type min() { return c == 0u ? 1u: 0u;}
36 static constexpr result_type max() { return m - 1u;}
37 static constexpr result_type default_seed = 1u;
38
39 // constructors and seeding functions
40 explicit linear_congruential_engine(result_type s = default_seed);
41 template<class Sseq> explicit linear_congruential_engine(Sseq& q);
42 void seed(result_type s = default_seed);
43 template<class Sseq> void seed(Sseq& q);
44
45 // generating functions
46 result_type operator()();
47 void discard(unsigned long long z);
48};
49
50template <class UIntType, UIntType a, UIntType c, UIntType m>
51bool
52operator==(const linear_congruential_engine<UIntType, a, c, m>& x,
53 const linear_congruential_engine<UIntType, a, c, m>& y);
54
55template <class UIntType, UIntType a, UIntType c, UIntType m>
56bool
57operator!=(const linear_congruential_engine<UIntType, a, c, m>& x,
58 const linear_congruential_engine<UIntType, a, c, m>& y);
59
60template <class charT, class traits,
61 class UIntType, UIntType a, UIntType c, UIntType m>
62basic_ostream<charT, traits>&
63operator<<(basic_ostream<charT, traits>& os,
64 const linear_congruential_engine<UIntType, a, c, m>& x);
65
66template <class charT, class traits,
67 class UIntType, UIntType a, UIntType c, UIntType m>
68basic_istream<charT, traits>&
69operator>>(basic_istream<charT, traits>& is,
70 linear_congruential_engine<UIntType, a, c, m>& x);
71
72template <class UIntType, size_t w, size_t n, size_t m, size_t r,
73 UIntType a, size_t u, UIntType d, size_t s,
74 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
75class mersenne_twister_engine
76{
77public:
78 // types
79 typedef UIntType result_type;
80
81 // engine characteristics
82 static constexpr size_t word_size = w;
83 static constexpr size_t state_size = n;
84 static constexpr size_t shift_size = m;
85 static constexpr size_t mask_bits = r;
86 static constexpr result_type xor_mask = a;
87 static constexpr size_t tempering_u = u;
88 static constexpr result_type tempering_d = d;
89 static constexpr size_t tempering_s = s;
90 static constexpr result_type tempering_b = b;
91 static constexpr size_t tempering_t = t;
92 static constexpr result_type tempering_c = c;
93 static constexpr size_t tempering_l = l;
94 static constexpr result_type initialization_multiplier = f;
95 static constexpr result_type min () { return 0; }
96 static constexpr result_type max() { return 2^w - 1; }
97 static constexpr result_type default_seed = 5489u;
98
99 // constructors and seeding functions
100 explicit mersenne_twister_engine(result_type value = default_seed);
101 template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
102 void seed(result_type value = default_seed);
103 template<class Sseq> void seed(Sseq& q);
104
105 // generating functions
106 result_type operator()();
107 void discard(unsigned long long z);
108};
109
110template <class UIntType, size_t w, size_t n, size_t m, size_t r,
111 UIntType a, size_t u, UIntType d, size_t s,
112 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
113bool
114operator==(
115 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
116 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
117
118template <class UIntType, size_t w, size_t n, size_t m, size_t r,
119 UIntType a, size_t u, UIntType d, size_t s,
120 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
121bool
122operator!=(
123 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
124 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
125
126template <class charT, class traits,
127 class UIntType, size_t w, size_t n, size_t m, size_t r,
128 UIntType a, size_t u, UIntType d, size_t s,
129 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
130basic_ostream<charT, traits>&
131operator<<(basic_ostream<charT, traits>& os,
132 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
133
134template <class charT, class traits,
135 class UIntType, size_t w, size_t n, size_t m, size_t r,
136 UIntType a, size_t u, UIntType d, size_t s,
137 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
138basic_istream<charT, traits>&
139operator>>(basic_istream<charT, traits>& is,
140 mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
141
142template<class UIntType, size_t w, size_t s, size_t r>
143class subtract_with_carry_engine
144{
145public:
146 // types
147 typedef UIntType result_type;
148
149 // engine characteristics
150 static constexpr size_t word_size = w;
151 static constexpr size_t short_lag = s;
152 static constexpr size_t long_lag = r;
153 static constexpr result_type min() { return 0; }
154 static constexpr result_type max() { return m-1; }
155 static constexpr result_type default_seed = 19780503u;
156
157 // constructors and seeding functions
158 explicit subtract_with_carry_engine(result_type value = default_seed);
159 template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
160 void seed(result_type value = default_seed);
161 template<class Sseq> void seed(Sseq& q);
162
163 // generating functions
164 result_type operator()();
165 void discard(unsigned long long z);
166};
167
168template<class UIntType, size_t w, size_t s, size_t r>
169bool
170operator==(
171 const subtract_with_carry_engine<UIntType, w, s, r>& x,
172 const subtract_with_carry_engine<UIntType, w, s, r>& y);
173
174template<class UIntType, size_t w, size_t s, size_t r>
175bool
176operator!=(
177 const subtract_with_carry_engine<UIntType, w, s, r>& x,
178 const subtract_with_carry_engine<UIntType, w, s, r>& y);
179
180template <class charT, class traits,
181 class UIntType, size_t w, size_t s, size_t r>
182basic_ostream<charT, traits>&
183operator<<(basic_ostream<charT, traits>& os,
184 const subtract_with_carry_engine<UIntType, w, s, r>& x);
185
186template <class charT, class traits,
187 class UIntType, size_t w, size_t s, size_t r>
188basic_istream<charT, traits>&
189operator>>(basic_istream<charT, traits>& is,
190 subtract_with_carry_engine<UIntType, w, s, r>& x);
191
192template<class Engine, size_t p, size_t r>
193class discard_block_engine
194{
195public:
196 // types
197 typedef typename Engine::result_type result_type;
198
199 // engine characteristics
200 static constexpr size_t block_size = p;
201 static constexpr size_t used_block = r;
202 static constexpr result_type min() { return Engine::min(); }
203 static constexpr result_type max() { return Engine::max(); }
204
205 // constructors and seeding functions
206 discard_block_engine();
207 explicit discard_block_engine(const Engine& e);
208 explicit discard_block_engine(Engine&& e);
209 explicit discard_block_engine(result_type s);
210 template<class Sseq> explicit discard_block_engine(Sseq& q);
211 void seed();
212 void seed(result_type s);
213 template<class Sseq> void seed(Sseq& q);
214
215 // generating functions
216 result_type operator()();
217 void discard(unsigned long long z);
218
219 // property functions
220 const Engine& base() const;
221};
222
223template<class Engine, size_t p, size_t r>
224bool
225operator==(
226 const discard_block_engine<Engine, p, r>& x,
227 const discard_block_engine<Engine, p, r>& y);
228
229template<class Engine, size_t p, size_t r>
230bool
231operator!=(
232 const discard_block_engine<Engine, p, r>& x,
233 const discard_block_engine<Engine, p, r>& y);
234
235template <class charT, class traits,
236 class Engine, size_t p, size_t r>
237basic_ostream<charT, traits>&
238operator<<(basic_ostream<charT, traits>& os,
239 const discard_block_engine<Engine, p, r>& x);
240
241template <class charT, class traits,
242 class Engine, size_t p, size_t r>
243basic_istream<charT, traits>&
244operator>>(basic_istream<charT, traits>& is,
245 discard_block_engine<Engine, p, r>& x);
246
247template<class Engine, size_t w, class UIntType>
248class independent_bits_engine
249{
250public:
251 // types
252 typedef UIntType result_type;
253
254 // engine characteristics
255 static constexpr result_type min() { return 0; }
256 static constexpr result_type max() { return 2^w - 1; }
257
258 // constructors and seeding functions
259 independent_bits_engine();
260 explicit independent_bits_engine(const Engine& e);
261 explicit independent_bits_engine(Engine&& e);
262 explicit independent_bits_engine(result_type s);
263 template<class Sseq> explicit independent_bits_engine(Sseq& q);
264 void seed();
265 void seed(result_type s);
266 template<class Sseq> void seed(Sseq& q);
267
268 // generating functions
269 result_type operator()(); void discard(unsigned long long z);
270
271 // property functions
272 const Engine& base() const;
273};
274
275template<class Engine, size_t w, class UIntType>
276bool
277operator==(
278 const independent_bits_engine<Engine, w, UIntType>& x,
279 const independent_bits_engine<Engine, w, UIntType>& y);
280
281template<class Engine, size_t w, class UIntType>
282bool
283operator!=(
284 const independent_bits_engine<Engine, w, UIntType>& x,
285 const independent_bits_engine<Engine, w, UIntType>& y);
286
287template <class charT, class traits,
288 class Engine, size_t w, class UIntType>
289basic_ostream<charT, traits>&
290operator<<(basic_ostream<charT, traits>& os,
291 const independent_bits_engine<Engine, w, UIntType>& x);
292
293template <class charT, class traits,
294 class Engine, size_t w, class UIntType>
295basic_istream<charT, traits>&
296operator>>(basic_istream<charT, traits>& is,
297 independent_bits_engine<Engine, w, UIntType>& x);
298
299template<class Engine, size_t k>
300class shuffle_order_engine
301{
302public:
303 // types
304 typedef typename Engine::result_type result_type;
305
306 // engine characteristics
307 static constexpr size_t table_size = k;
308 static constexpr result_type min() { return Engine::min; }
309 static constexpr result_type max() { return Engine::max; }
310
311 // constructors and seeding functions
312 shuffle_order_engine();
313 explicit shuffle_order_engine(const Engine& e);
314 explicit shuffle_order_engine(Engine&& e);
315 explicit shuffle_order_engine(result_type s);
316 template<class Sseq> explicit shuffle_order_engine(Sseq& q);
317 void seed();
318 void seed(result_type s);
319 template<class Sseq> void seed(Sseq& q);
320
321 // generating functions
322 result_type operator()();
323 void discard(unsigned long long z);
324
325 // property functions
326 const Engine& base() const;
327};
328
329template<class Engine, size_t k>
330bool
331operator==(
332 const shuffle_order_engine<Engine, k>& x,
333 const shuffle_order_engine<Engine, k>& y);
334
335template<class Engine, size_t k>
336bool
337operator!=(
338 const shuffle_order_engine<Engine, k>& x,
339 const shuffle_order_engine<Engine, k>& y);
340
341template <class charT, class traits,
342 class Engine, size_t k>
343basic_ostream<charT, traits>&
344operator<<(basic_ostream<charT, traits>& os,
345 const shuffle_order_engine<Engine, k>& x);
346
347template <class charT, class traits,
348 class Engine, size_t k>
349basic_istream<charT, traits>&
350operator>>(basic_istream<charT, traits>& is,
351 shuffle_order_engine<Engine, k>& x);
352
353typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
354 minstd_rand0;
355typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
356 minstd_rand;
357typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
358 0x9908b0df,
359 11, 0xffffffff,
360 7, 0x9d2c5680,
361 15, 0xefc60000,
362 18, 1812433253> mt19937;
363typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
364 0xb5026f5aa96619e9,
365 29, 0x5555555555555555,
366 17, 0x71d67fffeda60000,
367 37, 0xfff7eee000000000,
368 43, 6364136223846793005> mt19937_64;
369typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
370typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
371typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
372typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
373typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
Howard Hinnantd6d11712010-05-20 15:11:46 +0000374typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375
376// Generators
377
378class random_device
379{
380public:
381 // types
382 typedef unsigned int result_type;
383
384 // generator characteristics
385 static constexpr result_type min() { return numeric_limits<result_type>::min(); }
386 static constexpr result_type max() { return numeric_limits<result_type>::max(); }
387
388 // constructors
389 explicit random_device(const string& token = "/dev/urandom");
390
391 // generating functions
392 result_type operator()();
393
394 // property functions
395 double entropy() const;
396
397 // no copy functions
398 random_device(const random_device& ) = delete;
399 void operator=(const random_device& ) = delete;
400};
401
402// Utilities
403
404class seed_seq
405{
406public:
407 // types
408 typedef uint_least32_t result_type;
409
410 // constructors
411 seed_seq();
412 template<class T>
413 seed_seq(initializer_list<T> il);
414 template<class InputIterator>
415 seed_seq(InputIterator begin, InputIterator end);
416
417 // generating functions
418 template<class RandomAccessIterator>
419 void generate(RandomAccessIterator begin, RandomAccessIterator end);
420
421 // property functions
422 size_t size() const;
423 template<class OutputIterator>
424 void param(OutputIterator dest) const;
425
426 // no copy functions
427 seed_seq(const seed_seq&) = delete;
428 void operator=(const seed_seq& ) = delete;
429};
430
431template<class RealType, size_t bits, class URNG>
432 RealType generate_canonical(URNG& g);
433
434// Distributions
435
436template<class IntType = int>
437class uniform_int_distribution
438{
439public:
440 // types
441 typedef IntType result_type;
442
443 class param_type
444 {
445 public:
446 typedef uniform_int_distribution distribution_type;
447
448 explicit param_type(IntType a = 0,
449 IntType b = numeric_limits<IntType>::max());
450
451 result_type a() const;
452 result_type b() const;
453
454 friend bool operator==(const param_type& x, const param_type& y);
455 friend bool operator!=(const param_type& x, const param_type& y);
456 };
457
458 // constructors and reset functions
459 explicit uniform_int_distribution(IntType a = 0,
460 IntType b = numeric_limits<IntType>::max());
461 explicit uniform_int_distribution(const param_type& parm);
462 void reset();
463
464 // generating functions
465 template<class URNG> result_type operator()(URNG& g);
466 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
467
468 // property functions
469 result_type a() const;
470 result_type b() const;
471
472 param_type param() const;
473 void param(const param_type& parm);
474
475 result_type min() const;
476 result_type max() const;
477
478 friend bool operator==(const uniform_int_distribution& x,
479 const uniform_int_distribution& y);
480 friend bool operator!=(const uniform_int_distribution& x,
481 const uniform_int_distribution& y);
482
483 template <class charT, class traits>
484 friend
485 basic_ostream<charT, traits>&
486 operator<<(basic_ostream<charT, traits>& os,
487 const uniform_int_distribution& x);
488
489 template <class charT, class traits>
490 friend
491 basic_istream<charT, traits>&
492 operator>>(basic_istream<charT, traits>& is,
493 uniform_int_distribution& x);
494};
495
496template<class RealType = double>
497class uniform_real_distribution
498{
499public:
500 // types
501 typedef RealType result_type;
502
503 class param_type
504 {
505 public:
506 typedef uniform_real_distribution distribution_type;
507
508 explicit param_type(RealType a = 0,
509 RealType b = 1);
510
511 result_type a() const;
512 result_type b() const;
513
514 friend bool operator==(const param_type& x, const param_type& y);
515 friend bool operator!=(const param_type& x, const param_type& y);
516 };
517
518 // constructors and reset functions
519 explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);
520 explicit uniform_real_distribution(const param_type& parm);
521 void reset();
522
523 // generating functions
524 template<class URNG> result_type operator()(URNG& g);
525 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
526
527 // property functions
528 result_type a() const;
529 result_type b() const;
530
531 param_type param() const;
532 void param(const param_type& parm);
533
534 result_type min() const;
535 result_type max() const;
536
537 friend bool operator==(const uniform_real_distribution& x,
538 const uniform_real_distribution& y);
539 friend bool operator!=(const uniform_real_distribution& x,
540 const uniform_real_distribution& y);
541
542 template <class charT, class traits>
543 friend
544 basic_ostream<charT, traits>&
545 operator<<(basic_ostream<charT, traits>& os,
546 const uniform_real_distribution& x);
547
548 template <class charT, class traits>
549 friend
550 basic_istream<charT, traits>&
551 operator>>(basic_istream<charT, traits>& is,
552 uniform_real_distribution& x);
553};
554
555class bernoulli_distribution
556{
557public:
558 // types
559 typedef bool result_type;
560
561 class param_type
562 {
563 public:
564 typedef bernoulli_distribution distribution_type;
565
566 explicit param_type(double p = 0.5);
567
568 double p() const;
569
570 friend bool operator==(const param_type& x, const param_type& y);
571 friend bool operator!=(const param_type& x, const param_type& y);
572 };
573
574 // constructors and reset functions
575 explicit bernoulli_distribution(double p = 0.5);
576 explicit bernoulli_distribution(const param_type& parm);
577 void reset();
578
579 // generating functions
580 template<class URNG> result_type operator()(URNG& g);
581 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
582
583 // property functions
584 double p() const;
585
586 param_type param() const;
587 void param(const param_type& parm);
588
589 result_type min() const;
590 result_type max() const;
591
592 friend bool operator==(const bernoulli_distribution& x,
593 const bernoulli_distribution& y);
594 friend bool operator!=(const bernoulli_distribution& x,
595 const bernoulli_distribution& y);
596
597 template <class charT, class traits>
598 friend
599 basic_ostream<charT, traits>&
600 operator<<(basic_ostream<charT, traits>& os,
601 const bernoulli_distribution& x);
602
603 template <class charT, class traits>
604 friend
605 basic_istream<charT, traits>&
606 operator>>(basic_istream<charT, traits>& is,
607 bernoulli_distribution& x);
608};
609
610template<class IntType = int>
Howard Hinnant03aad812010-05-11 23:26:59 +0000611class binomial_distribution
612{
613public:
614 // types
615 typedef IntType result_type;
616
617 class param_type
618 {
619 public:
620 typedef binomial_distribution distribution_type;
621
622 explicit param_type(IntType t = 1, double p = 0.5);
623
624 IntType t() const;
625 double p() const;
626
627 friend bool operator==(const param_type& x, const param_type& y);
628 friend bool operator!=(const param_type& x, const param_type& y);
629 };
630
631 // constructors and reset functions
632 explicit binomial_distribution(IntType t = 1, double p = 0.5);
633 explicit binomial_distribution(const param_type& parm);
634 void reset();
635
636 // generating functions
637 template<class URNG> result_type operator()(URNG& g);
638 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
639
640 // property functions
641 IntType t() const;
642 double p() const;
643
644 param_type param() const;
645 void param(const param_type& parm);
646
647 result_type min() const;
648 result_type max() const;
649
650 friend bool operator==(const binomial_distribution& x,
651 const binomial_distribution& y);
652 friend bool operator!=(const binomial_distribution& x,
653 const binomial_distribution& y);
654
655 template <class charT, class traits>
656 friend
657 basic_ostream<charT, traits>&
658 operator<<(basic_ostream<charT, traits>& os,
659 const binomial_distribution& x);
660
661 template <class charT, class traits>
662 friend
663 basic_istream<charT, traits>&
664 operator>>(basic_istream<charT, traits>& is,
665 binomial_distribution& x);
666};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667
668template<class IntType = int>
Howard Hinnant34e8a572010-05-17 13:44:27 +0000669class geometric_distribution
670{
671public:
672 // types
673 typedef IntType result_type;
674
675 class param_type
676 {
677 public:
678 typedef geometric_distribution distribution_type;
679
680 explicit param_type(double p = 0.5);
681
682 double p() const;
683
684 friend bool operator==(const param_type& x, const param_type& y);
685 friend bool operator!=(const param_type& x, const param_type& y);
686 };
687
688 // constructors and reset functions
689 explicit geometric_distribution(double p = 0.5);
690 explicit geometric_distribution(const param_type& parm);
691 void reset();
692
693 // generating functions
694 template<class URNG> result_type operator()(URNG& g);
695 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
696
697 // property functions
698 double p() const;
699
700 param_type param() const;
701 void param(const param_type& parm);
702
703 result_type min() const;
704 result_type max() const;
705
706 friend bool operator==(const geometric_distribution& x,
707 const geometric_distribution& y);
708 friend bool operator!=(const geometric_distribution& x,
709 const geometric_distribution& y);
710
711 template <class charT, class traits>
712 friend
713 basic_ostream<charT, traits>&
714 operator<<(basic_ostream<charT, traits>& os,
715 const geometric_distribution& x);
716
717 template <class charT, class traits>
718 friend
719 basic_istream<charT, traits>&
720 operator>>(basic_istream<charT, traits>& is,
721 geometric_distribution& x);
722};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723
724template<class IntType = int>
Howard Hinnantf2fe5d52010-05-17 00:09:38 +0000725class negative_binomial_distribution
726{
727public:
728 // types
729 typedef IntType result_type;
730
731 class param_type
732 {
733 public:
734 typedef negative_binomial_distribution distribution_type;
735
736 explicit param_type(result_type k = 1, double p = 0.5);
737
738 result_type k() const;
739 double p() const;
740
741 friend bool operator==(const param_type& x, const param_type& y);
742 friend bool operator!=(const param_type& x, const param_type& y);
743 };
744
745 // constructor and reset functions
746 explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
747 explicit negative_binomial_distribution(const param_type& parm);
748 void reset();
749
750 // generating functions
751 template<class URNG> result_type operator()(URNG& g);
752 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
753
754 // property functions
755 result_type k() const;
756 double p() const;
757
758 param_type param() const;
759 void param(const param_type& parm);
760
761 result_type min() const;
762 result_type max() const;
763
764 friend bool operator==(const negative_binomial_distribution& x,
765 const negative_binomial_distribution& y);
766 friend bool operator!=(const negative_binomial_distribution& x,
767 const negative_binomial_distribution& y);
768
769 template <class charT, class traits>
770 friend
771 basic_ostream<charT, traits>&
772 operator<<(basic_ostream<charT, traits>& os,
773 const negative_binomial_distribution& x);
774
775 template <class charT, class traits>
776 friend
777 basic_istream<charT, traits>&
778 operator>>(basic_istream<charT, traits>& is,
779 negative_binomial_distribution& x);
780};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000781
782template<class IntType = int>
Howard Hinnant4ff556c2010-05-14 21:38:54 +0000783class poisson_distribution
784{
785public:
786 // types
787 typedef IntType result_type;
788
789 class param_type
790 {
791 public:
792 typedef poisson_distribution distribution_type;
793
794 explicit param_type(double mean = 1.0);
795
796 double mean() const;
797
798 friend bool operator==(const param_type& x, const param_type& y);
799 friend bool operator!=(const param_type& x, const param_type& y);
800 };
801
802 // constructors and reset functions
803 explicit poisson_distribution(double mean = 1.0);
804 explicit poisson_distribution(const param_type& parm);
805 void reset();
806
807 // generating functions
808 template<class URNG> result_type operator()(URNG& g);
809 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
810
811 // property functions
812 double mean() const;
813
814 param_type param() const;
815 void param(const param_type& parm);
816
817 result_type min() const;
818 result_type max() const;
819
820 friend bool operator==(const poisson_distribution& x,
821 const poisson_distribution& y);
822 friend bool operator!=(const poisson_distribution& x,
823 const poisson_distribution& y);
824
825 template <class charT, class traits>
826 friend
827 basic_ostream<charT, traits>&
828 operator<<(basic_ostream<charT, traits>& os,
829 const poisson_distribution& x);
830
831 template <class charT, class traits>
832 friend
833 basic_istream<charT, traits>&
834 operator>>(basic_istream<charT, traits>& is,
835 poisson_distribution& x);
836};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837
838template<class RealType = double>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000839class exponential_distribution
840{
841public:
842 // types
843 typedef RealType result_type;
844
845 class param_type
846 {
847 public:
848 typedef exponential_distribution distribution_type;
849
Howard Hinnanta64111c2010-05-12 21:02:31 +0000850 explicit param_type(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000851
Howard Hinnanta64111c2010-05-12 21:02:31 +0000852 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000853
854 friend bool operator==(const param_type& x, const param_type& y);
855 friend bool operator!=(const param_type& x, const param_type& y);
856 };
857
858 // constructors and reset functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000859 explicit exponential_distribution(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000860 explicit exponential_distribution(const param_type& parm);
861 void reset();
862
863 // generating functions
864 template<class URNG> result_type operator()(URNG& g);
865 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
866
867 // property functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000868 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000869
870 param_type param() const;
871 void param(const param_type& parm);
872
873 result_type min() const;
874 result_type max() const;
875
876 friend bool operator==(const exponential_distribution& x,
877 const exponential_distribution& y);
878 friend bool operator!=(const exponential_distribution& x,
879 const exponential_distribution& y);
880
881 template <class charT, class traits>
882 friend
883 basic_ostream<charT, traits>&
884 operator<<(basic_ostream<charT, traits>& os,
885 const exponential_distribution& x);
886
887 template <class charT, class traits>
888 friend
889 basic_istream<charT, traits>&
890 operator>>(basic_istream<charT, traits>& is,
891 exponential_distribution& x);
892};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893
894template<class RealType = double>
Howard Hinnantc7c49132010-05-13 17:58:28 +0000895class gamma_distribution
896{
897public:
898 // types
899 typedef RealType result_type;
900
901 class param_type
902 {
903 public:
904 typedef gamma_distribution distribution_type;
905
906 explicit param_type(result_type alpha = 1, result_type beta = 1);
907
908 result_type alpha() const;
909 result_type beta() const;
910
911 friend bool operator==(const param_type& x, const param_type& y);
912 friend bool operator!=(const param_type& x, const param_type& y);
913 };
914
915 // constructors and reset functions
916 explicit gamma_distribution(result_type alpha = 1, result_type beta = 1);
917 explicit gamma_distribution(const param_type& parm);
918 void reset();
919
920 // generating functions
921 template<class URNG> result_type operator()(URNG& g);
922 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
923
924 // property functions
925 result_type alpha() const;
926 result_type beta() const;
927
928 param_type param() const;
929 void param(const param_type& parm);
930
931 result_type min() const;
932 result_type max() const;
933
934 friend bool operator==(const gamma_distribution& x,
935 const gamma_distribution& y);
936 friend bool operator!=(const gamma_distribution& x,
937 const gamma_distribution& y);
938
939 template <class charT, class traits>
940 friend
941 basic_ostream<charT, traits>&
942 operator<<(basic_ostream<charT, traits>& os,
943 const gamma_distribution& x);
944
945 template <class charT, class traits>
946 friend
947 basic_istream<charT, traits>&
948 operator>>(basic_istream<charT, traits>& is,
949 gamma_distribution& x);
950};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951
952template<class RealType = double>
Howard Hinnant9de6e302010-05-16 01:09:02 +0000953class weibull_distribution
954{
955public:
956 // types
957 typedef RealType result_type;
958
959 class param_type
960 {
961 public:
962 typedef weibull_distribution distribution_type;
963
964 explicit param_type(result_type alpha = 1, result_type beta = 1);
965
966 result_type a() const;
967 result_type b() const;
968
969 friend bool operator==(const param_type& x, const param_type& y);
970 friend bool operator!=(const param_type& x, const param_type& y);
971 };
972
973 // constructor and reset functions
974 explicit weibull_distribution(result_type a = 1, result_type b = 1);
975 explicit weibull_distribution(const param_type& parm);
976 void reset();
977
978 // generating functions
979 template<class URNG> result_type operator()(URNG& g);
980 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
981
982 // property functions
983 result_type a() const;
984 result_type b() const;
985
986 param_type param() const;
987 void param(const param_type& parm);
988
989 result_type min() const;
990 result_type max() const;
991
Howard Hinnant9de6e302010-05-16 01:09:02 +0000992 friend bool operator==(const weibull_distribution& x,
993 const weibull_distribution& y);
994 friend bool operator!=(const weibull_distribution& x,
995 const weibull_distribution& y);
996
997 template <class charT, class traits>
998 friend
999 basic_ostream<charT, traits>&
1000 operator<<(basic_ostream<charT, traits>& os,
1001 const weibull_distribution& x);
1002
1003 template <class charT, class traits>
1004 friend
1005 basic_istream<charT, traits>&
1006 operator>>(basic_istream<charT, traits>& is,
1007 weibull_distribution& x);
1008};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009
1010template<class RealType = double>
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00001011class extreme_value_distribution
1012{
1013public:
1014 // types
1015 typedef RealType result_type;
1016
1017 class param_type
1018 {
1019 public:
1020 typedef extreme_value_distribution distribution_type;
1021
1022 explicit param_type(result_type a = 0, result_type b = 1);
1023
1024 result_type a() const;
1025 result_type b() const;
1026
1027 friend bool operator==(const param_type& x, const param_type& y);
1028 friend bool operator!=(const param_type& x, const param_type& y);
1029 };
1030
1031 // constructor and reset functions
1032 explicit extreme_value_distribution(result_type a = 0, result_type b = 1);
1033 explicit extreme_value_distribution(const param_type& parm);
1034 void reset();
1035
1036 // generating functions
1037 template<class URNG> result_type operator()(URNG& g);
1038 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1039
1040 // property functions
1041 result_type a() const;
1042 result_type b() const;
1043
1044 param_type param() const;
1045 void param(const param_type& parm);
1046
1047 result_type min() const;
1048 result_type max() const;
1049
1050 friend bool operator==(const extreme_value_distribution& x,
1051 const extreme_value_distribution& y);
1052 friend bool operator!=(const extreme_value_distribution& x,
1053 const extreme_value_distribution& y);
1054
1055 template <class charT, class traits>
1056 friend
1057 basic_ostream<charT, traits>&
1058 operator<<(basic_ostream<charT, traits>& os,
1059 const extreme_value_distribution& x);
1060
1061 template <class charT, class traits>
1062 friend
1063 basic_istream<charT, traits>&
1064 operator>>(basic_istream<charT, traits>& is,
1065 extreme_value_distribution& x);
1066};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067
1068template<class RealType = double>
Howard Hinnanta64111c2010-05-12 21:02:31 +00001069class normal_distribution
1070{
1071public:
1072 // types
1073 typedef RealType result_type;
1074
1075 class param_type
1076 {
1077 public:
1078 typedef normal_distribution distribution_type;
1079
1080 explicit param_type(result_type mean = 0, result_type stddev = 1);
1081
1082 result_type mean() const;
1083 result_type stddev() const;
1084
1085 friend bool operator==(const param_type& x, const param_type& y);
1086 friend bool operator!=(const param_type& x, const param_type& y);
1087 };
1088
1089 // constructors and reset functions
1090 explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
1091 explicit normal_distribution(const param_type& parm);
1092 void reset();
1093
1094 // generating functions
1095 template<class URNG> result_type operator()(URNG& g);
1096 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1097
1098 // property functions
1099 result_type mean() const;
1100 result_type stddev() const;
1101
1102 param_type param() const;
1103 void param(const param_type& parm);
1104
1105 result_type min() const;
1106 result_type max() const;
1107
1108 friend bool operator==(const normal_distribution& x,
1109 const normal_distribution& y);
1110 friend bool operator!=(const normal_distribution& x,
1111 const normal_distribution& y);
1112
1113 template <class charT, class traits>
1114 friend
1115 basic_ostream<charT, traits>&
1116 operator<<(basic_ostream<charT, traits>& os,
1117 const normal_distribution& x);
1118
1119 template <class charT, class traits>
1120 friend
1121 basic_istream<charT, traits>&
1122 operator>>(basic_istream<charT, traits>& is,
1123 normal_distribution& x);
1124};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125
1126template<class RealType = double>
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00001127class lognormal_distribution
1128{
1129public:
1130 // types
1131 typedef RealType result_type;
1132
1133 class param_type
1134 {
1135 public:
1136 typedef lognormal_distribution distribution_type;
1137
1138 explicit param_type(result_type m = 0, result_type s = 1);
1139
1140 result_type m() const;
1141 result_type s() const;
1142
1143 friend bool operator==(const param_type& x, const param_type& y);
1144 friend bool operator!=(const param_type& x, const param_type& y);
1145 };
1146
1147 // constructor and reset functions
1148 explicit lognormal_distribution(result_type m = 0, result_type s = 1);
1149 explicit lognormal_distribution(const param_type& parm);
1150 void reset();
1151
1152 // generating functions
1153 template<class URNG> result_type operator()(URNG& g);
1154 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1155
1156 // property functions
1157 result_type m() const;
1158 result_type s() const;
1159
1160 param_type param() const;
1161 void param(const param_type& parm);
1162
1163 result_type min() const;
1164 result_type max() const;
1165
1166 friend bool operator==(const lognormal_distribution& x,
1167 const lognormal_distribution& y);
1168 friend bool operator!=(const lognormal_distribution& x,
1169 const lognormal_distribution& y);
1170
1171 template <class charT, class traits>
1172 friend
1173 basic_ostream<charT, traits>&
1174 operator<<(basic_ostream<charT, traits>& os,
1175 const lognormal_distribution& x);
1176
1177 template <class charT, class traits>
1178 friend
1179 basic_istream<charT, traits>&
1180 operator>>(basic_istream<charT, traits>& is,
1181 lognormal_distribution& x);
1182};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183
1184template<class RealType = double>
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001185class chi_squared_distribution
1186{
1187public:
1188 // types
1189 typedef RealType result_type;
1190
1191 class param_type
1192 {
1193 public:
1194 typedef chi_squared_distribution distribution_type;
1195
1196 explicit param_type(result_type n = 1);
1197
1198 result_type n() const;
1199
1200 friend bool operator==(const param_type& x, const param_type& y);
1201 friend bool operator!=(const param_type& x, const param_type& y);
1202 };
1203
1204 // constructor and reset functions
1205 explicit chi_squared_distribution(result_type n = 1);
1206 explicit chi_squared_distribution(const param_type& parm);
1207 void reset();
1208
1209 // generating functions
1210 template<class URNG> result_type operator()(URNG& g);
1211 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1212
1213 // property functions
1214 result_type n() const;
1215
1216 param_type param() const;
1217 void param(const param_type& parm);
1218
1219 result_type min() const;
1220 result_type max() const;
1221
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001222 friend bool operator==(const chi_squared_distribution& x,
1223 const chi_squared_distribution& y);
1224 friend bool operator!=(const chi_squared_distribution& x,
1225 const chi_squared_distribution& y);
1226
1227 template <class charT, class traits>
1228 friend
1229 basic_ostream<charT, traits>&
1230 operator<<(basic_ostream<charT, traits>& os,
1231 const chi_squared_distribution& x);
1232
1233 template <class charT, class traits>
1234 friend
1235 basic_istream<charT, traits>&
1236 operator>>(basic_istream<charT, traits>& is,
1237 chi_squared_distribution& x);
1238};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239
1240template<class RealType = double>
Howard Hinnantd7d01132010-05-17 21:55:46 +00001241class cauchy_distribution
1242{
1243public:
1244 // types
1245 typedef RealType result_type;
1246
1247 class param_type
1248 {
1249 public:
1250 typedef cauchy_distribution distribution_type;
1251
1252 explicit param_type(result_type a = 0, result_type b = 1);
1253
1254 result_type a() const;
1255 result_type b() const;
1256
1257 friend bool operator==(const param_type& x, const param_type& y);
1258 friend bool operator!=(const param_type& x, const param_type& y);
1259 };
1260
1261 // constructor and reset functions
1262 explicit cauchy_distribution(result_type a = 0, result_type b = 1);
1263 explicit cauchy_distribution(const param_type& parm);
1264 void reset();
1265
1266 // generating functions
1267 template<class URNG> result_type operator()(URNG& g);
1268 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1269
1270 // property functions
1271 result_type a() const;
1272 result_type b() const;
1273
1274 param_type param() const;
1275 void param(const param_type& parm);
1276
1277 result_type min() const;
1278 result_type max() const;
1279
1280 friend bool operator==(const cauchy_distribution& x,
1281 const cauchy_distribution& y);
1282 friend bool operator!=(const cauchy_distribution& x,
1283 const cauchy_distribution& y);
1284
1285 template <class charT, class traits>
1286 friend
1287 basic_ostream<charT, traits>&
1288 operator<<(basic_ostream<charT, traits>& os,
1289 const cauchy_distribution& x);
1290
1291 template <class charT, class traits>
1292 friend
1293 basic_istream<charT, traits>&
1294 operator>>(basic_istream<charT, traits>& is,
1295 cauchy_distribution& x);
1296};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297
1298template<class RealType = double>
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001299class fisher_f_distribution
1300{
1301public:
1302 // types
1303 typedef RealType result_type;
1304
1305 class param_type
1306 {
1307 public:
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001308 typedef fisher_f_distribution distribution_type;
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001309
1310 explicit param_type(result_type m = 1, result_type n = 1);
1311
1312 result_type m() const;
1313 result_type n() const;
1314
1315 friend bool operator==(const param_type& x, const param_type& y);
1316 friend bool operator!=(const param_type& x, const param_type& y);
1317 };
1318
1319 // constructor and reset functions
1320 explicit fisher_f_distribution(result_type m = 1, result_type n = 1);
1321 explicit fisher_f_distribution(const param_type& parm);
1322 void reset();
1323
1324 // generating functions
1325 template<class URNG> result_type operator()(URNG& g);
1326 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1327
1328 // property functions
1329 result_type m() const;
1330 result_type n() const;
1331
1332 param_type param() const;
1333 void param(const param_type& parm);
1334
1335 result_type min() const;
1336 result_type max() const;
1337
1338 friend bool operator==(const fisher_f_distribution& x,
1339 const fisher_f_distribution& y);
1340 friend bool operator!=(const fisher_f_distribution& x,
1341 const fisher_f_distribution& y);
1342
1343 template <class charT, class traits>
1344 friend
1345 basic_ostream<charT, traits>&
1346 operator<<(basic_ostream<charT, traits>& os,
1347 const fisher_f_distribution& x);
1348
1349 template <class charT, class traits>
1350 friend
1351 basic_istream<charT, traits>&
1352 operator>>(basic_istream<charT, traits>& is,
1353 fisher_f_distribution& x);
1354};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355
1356template<class RealType = double>
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001357class student_t_distribution
1358{
1359public:
1360 // types
1361 typedef RealType result_type;
1362
1363 class param_type
1364 {
1365 public:
1366 typedef student_t_distribution distribution_type;
1367
1368 explicit param_type(result_type n = 1);
1369
1370 result_type n() const;
1371
1372 friend bool operator==(const param_type& x, const param_type& y);
1373 friend bool operator!=(const param_type& x, const param_type& y);
1374 };
1375
1376 // constructor and reset functions
1377 explicit student_t_distribution(result_type n = 1);
1378 explicit student_t_distribution(const param_type& parm);
1379 void reset();
1380
1381 // generating functions
1382 template<class URNG> result_type operator()(URNG& g);
1383 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1384
1385 // property functions
1386 result_type n() const;
1387
1388 param_type param() const;
1389 void param(const param_type& parm);
1390
1391 result_type min() const;
1392 result_type max() const;
1393
1394 friend bool operator==(const student_t_distribution& x,
1395 const student_t_distribution& y);
1396 friend bool operator!=(const student_t_distribution& x,
1397 const student_t_distribution& y);
1398
1399 template <class charT, class traits>
1400 friend
1401 basic_ostream<charT, traits>&
1402 operator<<(basic_ostream<charT, traits>& os,
1403 const student_t_distribution& x);
1404
1405 template <class charT, class traits>
1406 friend
1407 basic_istream<charT, traits>&
1408 operator>>(basic_istream<charT, traits>& is,
1409 student_t_distribution& x);
1410};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411
1412template<class IntType = int>
Howard Hinnant551d8e42010-05-19 01:53:57 +00001413class discrete_distribution
1414{
1415public:
1416 // types
1417 typedef IntType result_type;
1418
1419 class param_type
1420 {
1421 public:
1422 typedef discrete_distribution distribution_type;
1423
1424 param_type();
1425 template<class InputIterator>
1426 param_type(InputIterator firstW, InputIterator lastW);
1427 param_type(initializer_list<double> wl);
1428 template<class UnaryOperation>
1429 param_type(size_t nw, double xmin, double xmax, UnaryOperation fw);
1430
1431 vector<double> probabilities() const;
1432
1433 friend bool operator==(const param_type& x, const param_type& y);
1434 friend bool operator!=(const param_type& x, const param_type& y);
1435 };
1436
1437 // constructor and reset functions
1438 discrete_distribution();
1439 template<class InputIterator>
1440 discrete_distribution(InputIterator firstW, InputIterator lastW);
1441 discrete_distribution(initializer_list<double> wl);
1442 template<class UnaryOperation>
1443 discrete_distribution(size_t nw, double xmin, double xmax,
1444 UnaryOperation fw);
1445 explicit discrete_distribution(const param_type& parm);
1446 void reset();
1447
1448 // generating functions
1449 template<class URNG> result_type operator()(URNG& g);
1450 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1451
1452 // property functions
1453 vector<double> probabilities() const;
1454
1455 param_type param() const;
1456 void param(const param_type& parm);
1457
1458 result_type min() const;
1459 result_type max() const;
1460
1461 friend bool operator==(const discrete_distribution& x,
1462 const discrete_distribution& y);
1463 friend bool operator!=(const discrete_distribution& x,
1464 const discrete_distribution& y);
1465
1466 template <class charT, class traits>
1467 friend
1468 basic_ostream<charT, traits>&
1469 operator<<(basic_ostream<charT, traits>& os,
1470 const discrete_distribution& x);
1471
1472 template <class charT, class traits>
1473 friend
1474 basic_istream<charT, traits>&
1475 operator>>(basic_istream<charT, traits>& is,
1476 discrete_distribution& x);
1477};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478
1479template<class RealType = double>
Howard Hinnantd6d11712010-05-20 15:11:46 +00001480class piecewise_constant_distribution
1481{
1482 // types
1483 typedef RealType result_type;
1484
1485 class param_type
1486 {
1487 public:
1488 typedef piecewise_constant_distribution distribution_type;
1489
1490 param_type();
1491 template<class InputIteratorB, class InputIteratorW>
1492 param_type(InputIteratorB firstB, InputIteratorB lastB,
1493 InputIteratorW firstW);
1494 template<class UnaryOperation>
1495 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1496 template<class UnaryOperation>
1497 param_type(size_t nw, result_type xmin, result_type xmax,
1498 UnaryOperation fw);
1499
1500 vector<result_type> intervals() const;
1501 vector<double> densities() const;
1502
1503 friend bool operator==(const param_type& x, const param_type& y);
1504 friend bool operator!=(const param_type& x, const param_type& y);
1505 };
1506
1507 // constructor and reset functions
1508 piecewise_constant_distribution();
1509 template<class InputIteratorB, class InputIteratorW>
1510 piecewise_constant_distribution(InputIteratorB firstB,
1511 InputIteratorB lastB,
1512 InputIteratorW firstW);
1513 template<class UnaryOperation>
1514 piecewise_constant_distribution(initializer_list<result_type> bl,
1515 UnaryOperation fw);
1516 template<class UnaryOperation>
1517 piecewise_constant_distribution(size_t nw, result_type xmin,
1518 result_type xmax, UnaryOperation fw);
1519 explicit piecewise_constant_distribution(const param_type& parm);
1520 void reset();
1521
1522 // generating functions
1523 template<class URNG> result_type operator()(URNG& g);
1524 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1525
1526 // property functions
1527 vector<result_type> intervals() const;
1528 vector<double> densities() const;
1529
1530 param_type param() const;
1531 void param(const param_type& parm);
1532
1533 result_type min() const;
1534 result_type max() const;
1535
1536 friend bool operator==(const piecewise_constant_distribution& x,
1537 const piecewise_constant_distribution& y);
1538 friend bool operator!=(const piecewise_constant_distribution& x,
1539 const piecewise_constant_distribution& y);
1540
1541 template <class charT, class traits>
1542 friend
1543 basic_ostream<charT, traits>&
1544 operator<<(basic_ostream<charT, traits>& os,
1545 const piecewise_constant_distribution& x);
1546
1547 template <class charT, class traits>
1548 friend
1549 basic_istream<charT, traits>&
1550 operator>>(basic_istream<charT, traits>& is,
1551 piecewise_constant_distribution& x);
1552};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553
1554template<class RealType = double>
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);
1588
1589 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);
1624
1625 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>&);
1885
1886 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);
2103
2104 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);
2460
2461 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);
2467
2468 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);
2474
2475 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) {}
2688#ifdef _LIBCPP_MOVE
2689 explicit discard_block_engine(_Engine&& __e)
2690 : __e_(_STD::move(__e)), __n_(0) {}
2691#endif
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);
2720
2721 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);
2727
2728 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);
2734
2735 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) {}
2875#ifdef _LIBCPP_MOVE
2876 explicit independent_bits_engine(_Engine&& __e)
2877 : __e_(_STD::move(__e)) {}
2878#endif
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);
2907
2908 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);
2914
2915 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);
2921
2922 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;
3070
3071 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();}
3083#ifdef _LIBCPP_MOVE
3084 explicit shuffle_order_engine(_Engine&& __e)
3085 : __e_(_STD::move(__e)) {__init();}
3086#endif
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);
3116
3117 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);
3123
3124 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);
3130
3131 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());}
3300
3301 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);
3958
3959 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);
4071
4072 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);
4118
4119 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
4416
4417 friend bool operator==(const weibull_distribution& __x,
4418 const weibull_distribution& __y)
4419 {return __x.__p_ == __y.__p_;}
4420 friend bool operator!=(const weibull_distribution& __x,
4421 const weibull_distribution& __y)
4422 {return !(__x == __y);}
4423};
4424
4425template <class _CharT, class _Traits, class _RT>
4426basic_ostream<_CharT, _Traits>&
4427operator<<(basic_ostream<_CharT, _Traits>& __os,
4428 const weibull_distribution<_RT>& __x)
4429{
4430 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004431 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4432 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004433 _CharT __sp = __os.widen(' ');
4434 __os.fill(__sp);
4435 __os << __x.a() << __sp << __x.b();
4436 return __os;
4437}
4438
4439template <class _CharT, class _Traits, class _RT>
4440basic_istream<_CharT, _Traits>&
4441operator>>(basic_istream<_CharT, _Traits>& __is,
4442 weibull_distribution<_RT>& __x)
4443{
4444 typedef weibull_distribution<_RT> _Eng;
4445 typedef typename _Eng::result_type result_type;
4446 typedef typename _Eng::param_type param_type;
4447 __save_flags<_CharT, _Traits> _(__is);
4448 __is.flags(ios_base::dec | ios_base::skipws);
4449 result_type __a;
4450 result_type __b;
4451 __is >> __a >> __b;
4452 if (!__is.fail())
4453 __x.param(param_type(__a, __b));
4454 return __is;
4455}
4456
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004457template<class _RealType = double>
4458class extreme_value_distribution
4459{
4460public:
4461 // types
4462 typedef _RealType result_type;
4463
4464 class param_type
4465 {
4466 result_type __a_;
4467 result_type __b_;
4468 public:
4469 typedef extreme_value_distribution distribution_type;
4470
4471 explicit param_type(result_type __a = 0, result_type __b = 1)
4472 : __a_(__a), __b_(__b) {}
4473
4474 result_type a() const {return __a_;}
4475 result_type b() const {return __b_;}
4476
4477 friend bool operator==(const param_type& __x, const param_type& __y)
4478 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
4479 friend bool operator!=(const param_type& __x, const param_type& __y)
4480 {return !(__x == __y);}
4481 };
4482
4483private:
4484 param_type __p_;
4485
4486public:
4487 // constructor and reset functions
4488 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4489 : __p_(param_type(__a, __b)) {}
4490 explicit extreme_value_distribution(const param_type& __p)
4491 : __p_(__p) {}
4492 void reset() {}
4493
4494 // generating functions
4495 template<class _URNG> result_type operator()(_URNG& __g)
4496 {return (*this)(__g, __p_);}
4497 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4498
4499 // property functions
4500 result_type a() const {return __p_.a();}
4501 result_type b() const {return __p_.b();}
4502
4503 param_type param() const {return __p_;}
4504 void param(const param_type& __p) {__p_ = __p;}
4505
4506 result_type min() const {return -numeric_limits<result_type>::infinity();}
4507 result_type max() const {return numeric_limits<result_type>::infinity();}
4508
4509 friend bool operator==(const extreme_value_distribution& __x,
4510 const extreme_value_distribution& __y)
4511 {return __x.__p_ == __y.__p_;}
4512 friend bool operator!=(const extreme_value_distribution& __x,
4513 const extreme_value_distribution& __y)
4514 {return !(__x == __y);}
4515};
4516
4517template<class _RealType>
4518template<class _URNG>
4519_RealType
4520extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4521{
4522 return __p.a() - __p.b() *
4523 _STD::log(-_STD::log(1-uniform_real_distribution<result_type>()(__g)));
4524}
4525
4526template <class _CharT, class _Traits, class _RT>
4527basic_ostream<_CharT, _Traits>&
4528operator<<(basic_ostream<_CharT, _Traits>& __os,
4529 const extreme_value_distribution<_RT>& __x)
4530{
4531 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004532 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4533 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004534 _CharT __sp = __os.widen(' ');
4535 __os.fill(__sp);
4536 __os << __x.a() << __sp << __x.b();
4537 return __os;
4538}
4539
4540template <class _CharT, class _Traits, class _RT>
4541basic_istream<_CharT, _Traits>&
4542operator>>(basic_istream<_CharT, _Traits>& __is,
4543 extreme_value_distribution<_RT>& __x)
4544{
4545 typedef extreme_value_distribution<_RT> _Eng;
4546 typedef typename _Eng::result_type result_type;
4547 typedef typename _Eng::param_type param_type;
4548 __save_flags<_CharT, _Traits> _(__is);
4549 __is.flags(ios_base::dec | ios_base::skipws);
4550 result_type __a;
4551 result_type __b;
4552 __is >> __a >> __b;
4553 if (!__is.fail())
4554 __x.param(param_type(__a, __b));
4555 return __is;
4556}
4557
Howard Hinnantc7c49132010-05-13 17:58:28 +00004558// gamma_distribution
4559
4560template<class _RealType = double>
4561class gamma_distribution
4562{
4563public:
4564 // types
4565 typedef _RealType result_type;
4566
4567 class param_type
4568 {
4569 result_type __alpha_;
4570 result_type __beta_;
4571 public:
4572 typedef gamma_distribution distribution_type;
4573
4574 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4575 : __alpha_(__alpha), __beta_(__beta) {}
4576
4577 result_type alpha() const {return __alpha_;}
4578 result_type beta() const {return __beta_;}
4579
4580 friend bool operator==(const param_type& __x, const param_type& __y)
4581 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
4582 friend bool operator!=(const param_type& __x, const param_type& __y)
4583 {return !(__x == __y);}
4584 };
4585
4586private:
4587 param_type __p_;
4588
4589public:
4590 // constructors and reset functions
4591 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4592 : __p_(param_type(__alpha, __beta)) {}
4593 explicit gamma_distribution(const param_type& __p)
4594 : __p_(__p) {}
4595 void reset() {}
4596
4597 // generating functions
4598 template<class _URNG> result_type operator()(_URNG& __g)
4599 {return (*this)(__g, __p_);}
4600 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4601
4602 // property functions
4603 result_type alpha() const {return __p_.alpha();}
4604 result_type beta() const {return __p_.beta();}
4605
4606 param_type param() const {return __p_;}
4607 void param(const param_type& __p) {__p_ = __p;}
4608
4609 result_type min() const {return 0;}
4610 result_type max() const {return numeric_limits<result_type>::infinity();}
4611
4612 friend bool operator==(const gamma_distribution& __x,
4613 const gamma_distribution& __y)
4614 {return __x.__p_ == __y.__p_;}
4615 friend bool operator!=(const gamma_distribution& __x,
4616 const gamma_distribution& __y)
4617 {return !(__x == __y);}
4618};
4619
4620template <class _RealType>
4621template<class _URNG>
4622_RealType
4623gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4624{
Howard Hinnantf417abe2010-05-14 18:43:10 +00004625 result_type __a = __p.alpha();
4626 uniform_real_distribution<result_type> __gen(0, 1);
4627 exponential_distribution<result_type> __egen;
4628 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00004629 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00004630 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004631 else if (__a > 1)
4632 {
4633 const result_type __b = __a - 1;
4634 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004635 while (true)
4636 {
4637 const result_type __u = __gen(__g);
4638 const result_type __v = __gen(__g);
4639 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004640 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004641 {
4642 const result_type __y = _STD::sqrt(__c / __w) *
4643 (__u - result_type(0.5));
4644 __x = __b + __y;
4645 if (__x >= 0)
4646 {
4647 const result_type __z = 64 * __w * __w * __w * __v * __v;
4648 if (__z <= 1 - 2 * __y * __y / __x)
4649 break;
4650 if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y))
4651 break;
4652 }
4653 }
4654 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00004655 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00004656 else // __a < 1
4657 {
4658 while (true)
4659 {
4660 const result_type __u = __gen(__g);
4661 const result_type __es = __egen(__g);
4662 if (__u <= 1 - __a)
4663 {
4664 __x = _STD::pow(__u, 1 / __a);
4665 if (__x <= __es)
4666 break;
4667 }
4668 else
4669 {
4670 const result_type __e = -_STD::log((1-__u)/__a);
4671 __x = _STD::pow(1 - __a + __a * __e, 1 / __a);
4672 if (__x <= __e + __es)
4673 break;
4674 }
4675 }
4676 }
4677 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00004678}
4679
4680template <class _CharT, class _Traits, class _RT>
4681basic_ostream<_CharT, _Traits>&
4682operator<<(basic_ostream<_CharT, _Traits>& __os,
4683 const gamma_distribution<_RT>& __x)
4684{
4685 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004686 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4687 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004688 _CharT __sp = __os.widen(' ');
4689 __os.fill(__sp);
4690 __os << __x.alpha() << __sp << __x.beta();
4691 return __os;
4692}
4693
4694template <class _CharT, class _Traits, class _RT>
4695basic_istream<_CharT, _Traits>&
4696operator>>(basic_istream<_CharT, _Traits>& __is,
4697 gamma_distribution<_RT>& __x)
4698{
4699 typedef gamma_distribution<_RT> _Eng;
4700 typedef typename _Eng::result_type result_type;
4701 typedef typename _Eng::param_type param_type;
4702 __save_flags<_CharT, _Traits> _(__is);
4703 __is.flags(ios_base::dec | ios_base::skipws);
4704 result_type __alpha;
4705 result_type __beta;
4706 __is >> __alpha >> __beta;
4707 if (!__is.fail())
4708 __x.param(param_type(__alpha, __beta));
4709 return __is;
4710}
Howard Hinnanta64111c2010-05-12 21:02:31 +00004711
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00004712// negative_binomial_distribution
4713
4714template<class _IntType = int>
4715class negative_binomial_distribution
4716{
4717public:
4718 // types
4719 typedef _IntType result_type;
4720
4721 class param_type
4722 {
4723 result_type __k_;
4724 double __p_;
4725 public:
4726 typedef negative_binomial_distribution distribution_type;
4727
4728 explicit param_type(result_type __k = 1, double __p = 0.5)
4729 : __k_(__k), __p_(__p) {}
4730
4731 result_type k() const {return __k_;}
4732 double p() const {return __p_;}
4733
4734 friend bool operator==(const param_type& __x, const param_type& __y)
4735 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
4736 friend bool operator!=(const param_type& __x, const param_type& __y)
4737 {return !(__x == __y);}
4738 };
4739
4740private:
4741 param_type __p_;
4742
4743public:
4744 // constructor and reset functions
4745 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
4746 : __p_(__k, __p) {}
4747 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
4748 void reset() {}
4749
4750 // generating functions
4751 template<class _URNG> result_type operator()(_URNG& __g)
4752 {return (*this)(__g, __p_);}
4753 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4754
4755 // property functions
4756 result_type k() const {return __p_.k();}
4757 double p() const {return __p_.p();}
4758
4759 param_type param() const {return __p_;}
4760 void param(const param_type& __p) {__p_ = __p;}
4761
4762 result_type min() const {return 0;}
4763 result_type max() const {return numeric_limits<result_type>::max();}
4764
4765 friend bool operator==(const negative_binomial_distribution& __x,
4766 const negative_binomial_distribution& __y)
4767 {return __x.__p_ == __y.__p_;}
4768 friend bool operator!=(const negative_binomial_distribution& __x,
4769 const negative_binomial_distribution& __y)
4770 {return !(__x == __y);}
4771};
4772
4773template <class _IntType>
4774template<class _URNG>
4775_IntType
4776negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4777{
4778 result_type __k = __pr.k();
4779 double __p = __pr.p();
4780 if (__k <= 21 * __p)
4781 {
4782 bernoulli_distribution __gen(__p);
4783 result_type __f = 0;
4784 result_type __s = 0;
4785 while (__s < __k)
4786 {
4787 if (__gen(__urng))
4788 ++__s;
4789 else
4790 ++__f;
4791 }
4792 return __f;
4793 }
4794 return poisson_distribution<result_type>(gamma_distribution<double>
4795 (__k, (1-__p)/__p)(__urng))(__urng);
4796}
4797
4798template <class _CharT, class _Traits, class _IntType>
4799basic_ostream<_CharT, _Traits>&
4800operator<<(basic_ostream<_CharT, _Traits>& __os,
4801 const negative_binomial_distribution<_IntType>& __x)
4802{
4803 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004804 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4805 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00004806 _CharT __sp = __os.widen(' ');
4807 __os.fill(__sp);
4808 return __os << __x.k() << __sp << __x.p();
4809}
4810
4811template <class _CharT, class _Traits, class _IntType>
4812basic_istream<_CharT, _Traits>&
4813operator>>(basic_istream<_CharT, _Traits>& __is,
4814 negative_binomial_distribution<_IntType>& __x)
4815{
4816 typedef negative_binomial_distribution<_IntType> _Eng;
4817 typedef typename _Eng::result_type result_type;
4818 typedef typename _Eng::param_type param_type;
4819 __save_flags<_CharT, _Traits> _(__is);
4820 __is.flags(ios_base::dec | ios_base::skipws);
4821 result_type __k;
4822 double __p;
4823 __is >> __k >> __p;
4824 if (!__is.fail())
4825 __x.param(param_type(__k, __p));
4826 return __is;
4827}
4828
Howard Hinnant34e8a572010-05-17 13:44:27 +00004829// geometric_distribution
4830
4831template<class _IntType = int>
4832class geometric_distribution
4833{
4834public:
4835 // types
4836 typedef _IntType result_type;
4837
4838 class param_type
4839 {
4840 double __p_;
4841 public:
4842 typedef geometric_distribution distribution_type;
4843
4844 explicit param_type(double __p = 0.5) : __p_(__p) {}
4845
4846 double p() const {return __p_;}
4847
4848 friend bool operator==(const param_type& __x, const param_type& __y)
4849 {return __x.__p_ == __y.__p_;}
4850 friend bool operator!=(const param_type& __x, const param_type& __y)
4851 {return !(__x == __y);}
4852 };
4853
4854private:
4855 param_type __p_;
4856
4857public:
4858 // constructors and reset functions
4859 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
4860 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
4861 void reset() {}
4862
4863 // generating functions
4864 template<class _URNG> result_type operator()(_URNG& __g)
4865 {return (*this)(__g, __p_);}
4866 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4867 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
4868
4869 // property functions
4870 double p() const {return __p_.p();}
4871
4872 param_type param() const {return __p_;}
4873 void param(const param_type& __p) {__p_ = __p;}
4874
4875 result_type min() const {return 0;}
4876 result_type max() const {return numeric_limits<result_type>::max();}
4877
4878 friend bool operator==(const geometric_distribution& __x,
4879 const geometric_distribution& __y)
4880 {return __x.__p_ == __y.__p_;}
4881 friend bool operator!=(const geometric_distribution& __x,
4882 const geometric_distribution& __y)
4883 {return !(__x == __y);}
4884};
4885
4886template <class _CharT, class _Traits, class _IntType>
4887basic_ostream<_CharT, _Traits>&
4888operator<<(basic_ostream<_CharT, _Traits>& __os,
4889 const geometric_distribution<_IntType>& __x)
4890{
4891 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004892 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4893 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:27 +00004894 return __os << __x.p();
4895}
4896
4897template <class _CharT, class _Traits, class _IntType>
4898basic_istream<_CharT, _Traits>&
4899operator>>(basic_istream<_CharT, _Traits>& __is,
4900 geometric_distribution<_IntType>& __x)
4901{
4902 typedef geometric_distribution<_IntType> _Eng;
4903 typedef typename _Eng::param_type param_type;
4904 __save_flags<_CharT, _Traits> _(__is);
4905 __is.flags(ios_base::dec | ios_base::skipws);
4906 double __p;
4907 __is >> __p;
4908 if (!__is.fail())
4909 __x.param(param_type(__p));
4910 return __is;
4911}
4912
Howard Hinnant97dc2f32010-05-15 23:36:00 +00004913// chi_squared_distribution
4914
4915template<class _RealType = double>
4916class chi_squared_distribution
4917{
4918public:
4919 // types
4920 typedef _RealType result_type;
4921
4922 class param_type
4923 {
4924 result_type __n_;
4925 public:
4926 typedef chi_squared_distribution distribution_type;
4927
4928 explicit param_type(result_type __n = 1) : __n_(__n) {}
4929
4930 result_type n() const {return __n_;}
4931
4932 friend bool operator==(const param_type& __x, const param_type& __y)
4933 {return __x.__n_ == __y.__n_;}
4934 friend bool operator!=(const param_type& __x, const param_type& __y)
4935 {return !(__x == __y);}
4936 };
4937
4938private:
4939 param_type __p_;
4940
4941public:
4942 // constructor and reset functions
4943 explicit chi_squared_distribution(result_type __n = 1)
4944 : __p_(param_type(__n)) {}
4945 explicit chi_squared_distribution(const param_type& __p)
4946 : __p_(__p) {}
4947 void reset() {}
4948
4949 // generating functions
4950 template<class _URNG> result_type operator()(_URNG& __g)
4951 {return (*this)(__g, __p_);}
4952 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4953 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
4954
4955 // property functions
4956 result_type n() const {return __p_.n();}
4957
4958 param_type param() const {return __p_;}
4959 void param(const param_type& __p) {__p_ = __p;}
4960
4961 result_type min() const {return 0;}
4962 result_type max() const {return numeric_limits<result_type>::infinity();}
4963
4964
4965 friend bool operator==(const chi_squared_distribution& __x,
4966 const chi_squared_distribution& __y)
4967 {return __x.__p_ == __y.__p_;}
4968 friend bool operator!=(const chi_squared_distribution& __x,
4969 const chi_squared_distribution& __y)
4970 {return !(__x == __y);}
4971};
4972
4973template <class _CharT, class _Traits, class _RT>
4974basic_ostream<_CharT, _Traits>&
4975operator<<(basic_ostream<_CharT, _Traits>& __os,
4976 const chi_squared_distribution<_RT>& __x)
4977{
4978 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004979 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4980 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00004981 __os << __x.n();
4982 return __os;
4983}
4984
4985template <class _CharT, class _Traits, class _RT>
4986basic_istream<_CharT, _Traits>&
4987operator>>(basic_istream<_CharT, _Traits>& __is,
4988 chi_squared_distribution<_RT>& __x)
4989{
4990 typedef chi_squared_distribution<_RT> _Eng;
4991 typedef typename _Eng::result_type result_type;
4992 typedef typename _Eng::param_type param_type;
4993 __save_flags<_CharT, _Traits> _(__is);
4994 __is.flags(ios_base::dec | ios_base::skipws);
4995 result_type __n;
4996 __is >> __n;
4997 if (!__is.fail())
4998 __x.param(param_type(__n));
4999 return __is;
5000}
5001
Howard Hinnantd7d01132010-05-17 21:55:46 +00005002// cauchy_distribution
5003
5004template<class _RealType = double>
5005class cauchy_distribution
5006{
5007public:
5008 // types
5009 typedef _RealType result_type;
5010
5011 class param_type
5012 {
5013 result_type __a_;
5014 result_type __b_;
5015 public:
5016 typedef cauchy_distribution distribution_type;
5017
5018 explicit param_type(result_type __a = 0, result_type __b = 1)
5019 : __a_(__a), __b_(__b) {}
5020
5021 result_type a() const {return __a_;}
5022 result_type b() const {return __b_;}
5023
5024 friend bool operator==(const param_type& __x, const param_type& __y)
5025 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
5026 friend bool operator!=(const param_type& __x, const param_type& __y)
5027 {return !(__x == __y);}
5028 };
5029
5030private:
5031 param_type __p_;
5032
5033public:
5034 // constructor and reset functions
5035 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5036 : __p_(param_type(__a, __b)) {}
5037 explicit cauchy_distribution(const param_type& __p)
5038 : __p_(__p) {}
5039 void reset() {}
5040
5041 // generating functions
5042 template<class _URNG> result_type operator()(_URNG& __g)
5043 {return (*this)(__g, __p_);}
5044 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5045
5046 // property functions
5047 result_type a() const {return __p_.a();}
5048 result_type b() const {return __p_.b();}
5049
5050 param_type param() const {return __p_;}
5051 void param(const param_type& __p) {__p_ = __p;}
5052
5053 result_type min() const {return -numeric_limits<result_type>::infinity();}
5054 result_type max() const {return numeric_limits<result_type>::infinity();}
5055
5056 friend bool operator==(const cauchy_distribution& __x,
5057 const cauchy_distribution& __y)
5058 {return __x.__p_ == __y.__p_;}
5059 friend bool operator!=(const cauchy_distribution& __x,
5060 const cauchy_distribution& __y)
5061 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005062};
5063
5064template <class _RealType>
5065template<class _URNG>
5066inline
5067_RealType
5068cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5069{
5070 uniform_real_distribution<result_type> __gen;
5071 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
5072 return __p.a() + __p.b() * _STD::tan(3.1415926535897932384626433832795 * __gen(__g));
5073}
5074
5075template <class _CharT, class _Traits, class _RT>
5076basic_ostream<_CharT, _Traits>&
5077operator<<(basic_ostream<_CharT, _Traits>& __os,
5078 const cauchy_distribution<_RT>& __x)
5079{
5080 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005081 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5082 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005083 _CharT __sp = __os.widen(' ');
5084 __os.fill(__sp);
5085 __os << __x.a() << __sp << __x.b();
5086 return __os;
5087}
5088
5089template <class _CharT, class _Traits, class _RT>
5090basic_istream<_CharT, _Traits>&
5091operator>>(basic_istream<_CharT, _Traits>& __is,
5092 cauchy_distribution<_RT>& __x)
5093{
5094 typedef cauchy_distribution<_RT> _Eng;
5095 typedef typename _Eng::result_type result_type;
5096 typedef typename _Eng::param_type param_type;
5097 __save_flags<_CharT, _Traits> _(__is);
5098 __is.flags(ios_base::dec | ios_base::skipws);
5099 result_type __a;
5100 result_type __b;
5101 __is >> __a >> __b;
5102 if (!__is.fail())
5103 __x.param(param_type(__a, __b));
5104 return __is;
5105}
5106
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005107// fisher_f_distribution
5108
5109template<class _RealType = double>
5110class fisher_f_distribution
5111{
5112public:
5113 // types
5114 typedef _RealType result_type;
5115
5116 class param_type
5117 {
5118 result_type __m_;
5119 result_type __n_;
5120 public:
5121 typedef fisher_f_distribution distribution_type;
5122
5123 explicit param_type(result_type __m = 1, result_type __n = 1)
5124 : __m_(__m), __n_(__n) {}
5125
5126 result_type m() const {return __m_;}
5127 result_type n() const {return __n_;}
5128
5129 friend bool operator==(const param_type& __x, const param_type& __y)
5130 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
5131 friend bool operator!=(const param_type& __x, const param_type& __y)
5132 {return !(__x == __y);}
5133 };
5134
5135private:
5136 param_type __p_;
5137
5138public:
5139 // constructor and reset functions
5140 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5141 : __p_(param_type(__m, __n)) {}
5142 explicit fisher_f_distribution(const param_type& __p)
5143 : __p_(__p) {}
5144 void reset() {}
5145
5146 // generating functions
5147 template<class _URNG> result_type operator()(_URNG& __g)
5148 {return (*this)(__g, __p_);}
5149 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5150
5151 // property functions
5152 result_type m() const {return __p_.m();}
5153 result_type n() const {return __p_.n();}
5154
5155 param_type param() const {return __p_;}
5156 void param(const param_type& __p) {__p_ = __p;}
5157
5158 result_type min() const {return 0;}
5159 result_type max() const {return numeric_limits<result_type>::infinity();}
5160
5161 friend bool operator==(const fisher_f_distribution& __x,
5162 const fisher_f_distribution& __y)
5163 {return __x.__p_ == __y.__p_;}
5164 friend bool operator!=(const fisher_f_distribution& __x,
5165 const fisher_f_distribution& __y)
5166 {return !(__x == __y);}
5167};
5168
5169template <class _RealType>
5170template<class _URNG>
5171_RealType
5172fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5173{
5174 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5175 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5176 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5177}
5178
5179template <class _CharT, class _Traits, class _RT>
5180basic_ostream<_CharT, _Traits>&
5181operator<<(basic_ostream<_CharT, _Traits>& __os,
5182 const fisher_f_distribution<_RT>& __x)
5183{
5184 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005185 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5186 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005187 _CharT __sp = __os.widen(' ');
5188 __os.fill(__sp);
5189 __os << __x.m() << __sp << __x.n();
5190 return __os;
5191}
5192
5193template <class _CharT, class _Traits, class _RT>
5194basic_istream<_CharT, _Traits>&
5195operator>>(basic_istream<_CharT, _Traits>& __is,
5196 fisher_f_distribution<_RT>& __x)
5197{
5198 typedef fisher_f_distribution<_RT> _Eng;
5199 typedef typename _Eng::result_type result_type;
5200 typedef typename _Eng::param_type param_type;
5201 __save_flags<_CharT, _Traits> _(__is);
5202 __is.flags(ios_base::dec | ios_base::skipws);
5203 result_type __m;
5204 result_type __n;
5205 __is >> __m >> __n;
5206 if (!__is.fail())
5207 __x.param(param_type(__m, __n));
5208 return __is;
5209}
5210
Howard Hinnant551d8e42010-05-19 01:53:57 +00005211// student_t_distribution
5212
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005213template<class _RealType = double>
5214class student_t_distribution
5215{
5216public:
5217 // types
5218 typedef _RealType result_type;
5219
5220 class param_type
5221 {
5222 result_type __n_;
5223 public:
5224 typedef student_t_distribution distribution_type;
5225
5226 explicit param_type(result_type __n = 1) : __n_(__n) {}
5227
5228 result_type n() const {return __n_;}
5229
5230 friend bool operator==(const param_type& __x, const param_type& __y)
5231 {return __x.__n_ == __y.__n_;}
5232 friend bool operator!=(const param_type& __x, const param_type& __y)
5233 {return !(__x == __y);}
5234 };
5235
5236private:
5237 param_type __p_;
5238 normal_distribution<result_type> __nd_;
5239
5240public:
5241 // constructor and reset functions
5242 explicit student_t_distribution(result_type __n = 1)
5243 : __p_(param_type(__n)) {}
5244 explicit student_t_distribution(const param_type& __p)
5245 : __p_(__p) {}
5246 void reset() {__nd_.reset();}
5247
5248 // generating functions
5249 template<class _URNG> result_type operator()(_URNG& __g)
5250 {return (*this)(__g, __p_);}
5251 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5252
5253 // property functions
5254 result_type n() const {return __p_.n();}
5255
5256 param_type param() const {return __p_;}
Howard Hinnant551d8e42010-05-19 01:53:57 +00005257 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005258
5259 result_type min() const {return -numeric_limits<result_type>::infinity();}
5260 result_type max() const {return numeric_limits<result_type>::infinity();}
5261
5262 friend bool operator==(const student_t_distribution& __x,
5263 const student_t_distribution& __y)
5264 {return __x.__p_ == __y.__p_;}
5265 friend bool operator!=(const student_t_distribution& __x,
5266 const student_t_distribution& __y)
5267 {return !(__x == __y);}
5268};
5269
5270template <class _RealType>
5271template<class _URNG>
5272_RealType
5273student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5274{
5275 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
5276 return __nd_(__g) * _STD::sqrt(__p.n()/__gd(__g));
5277}
5278
5279template <class _CharT, class _Traits, class _RT>
5280basic_ostream<_CharT, _Traits>&
5281operator<<(basic_ostream<_CharT, _Traits>& __os,
5282 const student_t_distribution<_RT>& __x)
5283{
5284 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005285 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5286 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005287 __os << __x.n();
5288 return __os;
5289}
5290
5291template <class _CharT, class _Traits, class _RT>
5292basic_istream<_CharT, _Traits>&
5293operator>>(basic_istream<_CharT, _Traits>& __is,
5294 student_t_distribution<_RT>& __x)
5295{
5296 typedef student_t_distribution<_RT> _Eng;
5297 typedef typename _Eng::result_type result_type;
5298 typedef typename _Eng::param_type param_type;
5299 __save_flags<_CharT, _Traits> _(__is);
5300 __is.flags(ios_base::dec | ios_base::skipws);
5301 result_type __n;
5302 __is >> __n;
5303 if (!__is.fail())
5304 __x.param(param_type(__n));
5305 return __is;
5306}
5307
Howard Hinnant551d8e42010-05-19 01:53:57 +00005308// discrete_distribution
5309
5310template<class _IntType = int>
5311class discrete_distribution
5312{
5313public:
5314 // types
5315 typedef _IntType result_type;
5316
5317 class param_type
5318 {
5319 vector<double> __p_;
5320 public:
5321 typedef discrete_distribution distribution_type;
5322
5323 param_type() {}
5324 template<class _InputIterator>
5325 param_type(_InputIterator __f, _InputIterator __l)
5326 : __p_(__f, __l) {__init();}
5327 param_type(initializer_list<double> __wl)
5328 : __p_(__wl.begin(), __wl.end()) {__init();}
5329 template<class _UnaryOperation>
5330 param_type(size_t __nw, double __xmin, double __xmax,
5331 _UnaryOperation __fw);
5332
5333 vector<double> probabilities() const;
5334
5335 friend bool operator==(const param_type& __x, const param_type& __y)
5336 {return __x.__p_ == __y.__p_;}
5337 friend bool operator!=(const param_type& __x, const param_type& __y)
5338 {return !(__x == __y);}
5339
5340 private:
5341 void __init();
5342
5343 friend class discrete_distribution;
5344
5345 template <class _CharT, class _Traits, class _IT>
5346 friend
5347 basic_ostream<_CharT, _Traits>&
5348 operator<<(basic_ostream<_CharT, _Traits>& __os,
5349 const discrete_distribution<_IT>& __x);
5350
5351 template <class _CharT, class _Traits, class _IT>
5352 friend
5353 basic_istream<_CharT, _Traits>&
5354 operator>>(basic_istream<_CharT, _Traits>& __is,
5355 discrete_distribution<_IT>& __x);
5356 };
5357
5358private:
5359 param_type __p_;
5360
5361public:
5362 // constructor and reset functions
5363 discrete_distribution() {}
5364 template<class _InputIterator>
5365 discrete_distribution(_InputIterator __f, _InputIterator __l)
5366 : __p_(__f, __l) {}
5367 discrete_distribution(initializer_list<double> __wl)
5368 : __p_(__wl) {}
5369 template<class _UnaryOperation>
5370 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5371 _UnaryOperation __fw)
5372 : __p_(__nw, __xmin, __xmax, __fw) {}
5373 explicit discrete_distribution(const param_type& __p)
5374 : __p_(__p) {}
5375 void reset() {}
5376
5377 // generating functions
5378 template<class _URNG> result_type operator()(_URNG& __g)
5379 {return (*this)(__g, __p_);}
5380 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5381
5382 // property functions
5383 vector<double> probabilities() const {return __p_.probabilities();}
5384
5385 param_type param() const {return __p_;}
5386 void param(const param_type& __p) {__p_ = __p;}
5387
5388 result_type min() const {return 0;}
5389 result_type max() const {return __p_.__p_.size();}
5390
5391 friend bool operator==(const discrete_distribution& __x,
5392 const discrete_distribution& __y)
5393 {return __x.__p_ == __y.__p_;}
5394 friend bool operator!=(const discrete_distribution& __x,
5395 const discrete_distribution& __y)
5396 {return !(__x == __y);}
5397
5398 template <class _CharT, class _Traits, class _IT>
5399 friend
5400 basic_ostream<_CharT, _Traits>&
5401 operator<<(basic_ostream<_CharT, _Traits>& __os,
5402 const discrete_distribution<_IT>& __x);
5403
5404 template <class _CharT, class _Traits, class _IT>
5405 friend
5406 basic_istream<_CharT, _Traits>&
5407 operator>>(basic_istream<_CharT, _Traits>& __is,
5408 discrete_distribution<_IT>& __x);
5409};
5410
5411template<class _IntType>
5412template<class _UnaryOperation>
5413discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5414 double __xmin,
5415 double __xmax,
5416 _UnaryOperation __fw)
5417{
5418 if (__nw > 1)
5419 {
5420 __p_.reserve(__nw - 1);
5421 double __d = (__xmax - __xmin) / __nw;
5422 double __d2 = __d / 2;
5423 for (size_t __k = 0; __k < __nw; ++__k)
5424 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5425 __init();
5426 }
5427}
5428
5429template<class _IntType>
5430void
5431discrete_distribution<_IntType>::param_type::__init()
5432{
5433 if (!__p_.empty())
5434 {
5435 if (__p_.size() > 1)
5436 {
5437 double __s = _STD::accumulate(__p_.begin(), __p_.end(), 0.0);
5438 for (_STD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
5439 __i < __e; ++__i)
5440 *__i /= __s;
5441 vector<double> __t(__p_.size() - 1);
5442 _STD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
5443 swap(__p_, __t);
5444 }
5445 else
5446 {
5447 __p_.clear();
5448 __p_.shrink_to_fit();
5449 }
5450 }
5451}
5452
5453template<class _IntType>
5454vector<double>
5455discrete_distribution<_IntType>::param_type::probabilities() const
5456{
5457 size_t __n = __p_.size();
5458 _STD::vector<double> __p(__n+1);
5459 _STD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
5460 if (__n > 0)
5461 __p[__n] = 1 - __p_[__n-1];
5462 else
5463 __p[0] = 1;
5464 return __p;
5465}
5466
5467template<class _IntType>
5468template<class _URNG>
5469_IntType
5470discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
5471{
5472 uniform_real_distribution<double> __gen;
5473 return static_cast<_IntType>(
5474 _STD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
5475 __p.__p_.begin());
5476}
5477
5478template <class _CharT, class _Traits, class _IT>
5479basic_ostream<_CharT, _Traits>&
5480operator<<(basic_ostream<_CharT, _Traits>& __os,
5481 const discrete_distribution<_IT>& __x)
5482{
5483 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005484 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5485 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005486 _CharT __sp = __os.widen(' ');
5487 __os.fill(__sp);
5488 size_t __n = __x.__p_.__p_.size();
5489 __os << __n;
5490 for (size_t __i = 0; __i < __n; ++__i)
5491 __os << __sp << __x.__p_.__p_[__i];
5492 return __os;
5493}
5494
5495template <class _CharT, class _Traits, class _IT>
5496basic_istream<_CharT, _Traits>&
5497operator>>(basic_istream<_CharT, _Traits>& __is,
5498 discrete_distribution<_IT>& __x)
5499{
5500 typedef discrete_distribution<_IT> _Eng;
5501 typedef typename _Eng::result_type result_type;
5502 typedef typename _Eng::param_type param_type;
5503 __save_flags<_CharT, _Traits> _(__is);
5504 __is.flags(ios_base::dec | ios_base::skipws);
5505 size_t __n;
5506 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005507 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005508 for (size_t __i = 0; __i < __n; ++__i)
5509 __is >> __p[__i];
5510 if (!__is.fail())
5511 swap(__x.__p_.__p_, __p);
5512 return __is;
5513}
5514
Howard Hinnantd6d11712010-05-20 15:11:46 +00005515// piecewise_constant_distribution
5516
5517template<class _RealType = double>
5518class piecewise_constant_distribution
5519{
5520public:
5521 // types
5522 typedef _RealType result_type;
5523
5524 class param_type
5525 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005526 typedef typename common_type<double, result_type>::type __area_type;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005527 vector<result_type> __b_;
Howard Hinnant2a592542010-05-24 00:35:40 +00005528 vector<double> __densities_;
5529 vector<__area_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005530 public:
5531 typedef piecewise_constant_distribution distribution_type;
5532
5533 param_type();
5534 template<class _InputIteratorB, class _InputIteratorW>
5535 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5536 _InputIteratorW __fW);
5537 template<class _UnaryOperation>
5538 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
5539 template<class _UnaryOperation>
5540 param_type(size_t __nw, result_type __xmin, result_type __xmax,
5541 _UnaryOperation __fw);
5542
5543 vector<result_type> intervals() const {return __b_;}
Howard Hinnant2a592542010-05-24 00:35:40 +00005544 vector<double> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005545
5546 friend bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:40 +00005547 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005548 friend bool operator!=(const param_type& __x, const param_type& __y)
5549 {return !(__x == __y);}
5550
5551 private:
5552 void __init();
5553
5554 friend class piecewise_constant_distribution;
5555
5556 template <class _CharT, class _Traits, class _RT>
5557 friend
5558 basic_ostream<_CharT, _Traits>&
5559 operator<<(basic_ostream<_CharT, _Traits>& __os,
5560 const piecewise_constant_distribution<_RT>& __x);
5561
5562 template <class _CharT, class _Traits, class _RT>
5563 friend
5564 basic_istream<_CharT, _Traits>&
5565 operator>>(basic_istream<_CharT, _Traits>& __is,
5566 piecewise_constant_distribution<_RT>& __x);
5567 };
5568
5569private:
5570 param_type __p_;
5571
5572public:
5573 // constructor and reset functions
5574 piecewise_constant_distribution() {}
5575 template<class _InputIteratorB, class _InputIteratorW>
5576 piecewise_constant_distribution(_InputIteratorB __fB,
5577 _InputIteratorB __lB,
5578 _InputIteratorW __fW)
5579 : __p_(__fB, __lB, __fW) {}
5580
5581 template<class _UnaryOperation>
5582 piecewise_constant_distribution(initializer_list<result_type> __bl,
5583 _UnaryOperation __fw)
5584 : __p_(__bl, __fw) {}
5585
5586 template<class _UnaryOperation>
5587 piecewise_constant_distribution(size_t __nw, result_type __xmin,
5588 result_type __xmax, _UnaryOperation __fw)
5589 : __p_(__nw, __xmin, __xmax, __fw) {}
5590
5591 explicit piecewise_constant_distribution(const param_type& __p)
5592 : __p_(__p) {}
5593
5594 void reset() {}
5595
5596 // generating functions
5597 template<class _URNG> result_type operator()(_URNG& __g)
5598 {return (*this)(__g, __p_);}
5599 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5600
5601 // property functions
5602 vector<result_type> intervals() const {return __p_.intervals();}
5603 vector<double> densities() const {return __p_.densities();}
5604
5605 param_type param() const {return __p_;}
5606 void param(const param_type& __p) {__p_ = __p;}
5607
5608 result_type min() const {return __p_.__b_.front();}
5609 result_type max() const {return __p_.__b_.back();}
5610
5611 friend bool operator==(const piecewise_constant_distribution& __x,
5612 const piecewise_constant_distribution& __y)
5613 {return __x.__p_ == __y.__p_;}
5614 friend bool operator!=(const piecewise_constant_distribution& __x,
5615 const piecewise_constant_distribution& __y)
5616 {return !(__x == __y);}
5617
5618 template <class _CharT, class _Traits, class _RT>
5619 friend
5620 basic_ostream<_CharT, _Traits>&
5621 operator<<(basic_ostream<_CharT, _Traits>& __os,
5622 const piecewise_constant_distribution<_RT>& __x);
5623
5624 template <class _CharT, class _Traits, class _RT>
5625 friend
5626 basic_istream<_CharT, _Traits>&
5627 operator>>(basic_istream<_CharT, _Traits>& __is,
5628 piecewise_constant_distribution<_RT>& __x);
5629};
5630
5631template<class _RealType>
5632void
5633piecewise_constant_distribution<_RealType>::param_type::__init()
5634{
Howard Hinnant2a592542010-05-24 00:35:40 +00005635 // __densities_ contains non-normalized areas
5636 __area_type __total_area = _STD::accumulate(__densities_.begin(),
5637 __densities_.end(),
5638 __area_type());
5639 for (size_t __i = 0; __i < __densities_.size(); ++__i)
5640 __densities_[__i] /= __total_area;
5641 // __densities_ contains normalized areas
5642 __areas_.assign(__densities_.size(), __area_type());
5643 _STD::partial_sum(__densities_.begin(), __densities_.end() - 1,
5644 __areas_.begin() + 1);
5645 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
5646 __densities_.back() = 1 - __areas_.back(); // correct round off error
5647 for (size_t __i = 0; __i < __densities_.size(); ++__i)
5648 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
5649 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:46 +00005650}
5651
5652template<class _RealType>
5653piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:40 +00005654 : __b_(2),
Howard Hinnant54305402010-05-25 00:27:34 +00005655 __densities_(1, 1.0),
5656 __areas_(1, 0.0)
Howard Hinnantd6d11712010-05-20 15:11:46 +00005657{
5658 __b_[1] = 1;
5659}
5660
5661template<class _RealType>
5662template<class _InputIteratorB, class _InputIteratorW>
5663piecewise_constant_distribution<_RealType>::param_type::param_type(
5664 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
5665 : __b_(__fB, __lB)
5666{
5667 if (__b_.size() < 2)
5668 {
5669 __b_.resize(2);
5670 __b_[0] = 0;
5671 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00005672 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00005673 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005674 }
5675 else
5676 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005677 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005678 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:40 +00005679 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005680 __init();
5681 }
5682}
5683
5684template<class _RealType>
5685template<class _UnaryOperation>
5686piecewise_constant_distribution<_RealType>::param_type::param_type(
5687 initializer_list<result_type> __bl, _UnaryOperation __fw)
5688 : __b_(__bl.begin(), __bl.end())
5689{
5690 if (__b_.size() < 2)
5691 {
5692 __b_.resize(2);
5693 __b_[0] = 0;
5694 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00005695 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00005696 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005697 }
5698 else
5699 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005700 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005701 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005702 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00005703 __init();
5704 }
5705}
5706
5707template<class _RealType>
5708template<class _UnaryOperation>
5709piecewise_constant_distribution<_RealType>::param_type::param_type(
5710 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
5711 : __b_(__nw == 0 ? 2 : __nw + 1)
5712{
5713 size_t __n = __b_.size() - 1;
5714 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:40 +00005715 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005716 for (size_t __i = 0; __i < __n; ++__i)
5717 {
5718 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:40 +00005719 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00005720 }
5721 __b_[__n] = __xmax;
5722 __init();
5723}
5724
5725template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00005726template<class _URNG>
5727_RealType
5728piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5729{
5730 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005731 result_type __u = _Gen()(__g);
Howard Hinnant2a592542010-05-24 00:35:40 +00005732 ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
5733 static_cast<double>(__u)) - __p.__areas_.begin() - 1;
5734 return static_cast<result_type>((__u - __p.__areas_[__k]) / __p.__densities_[__k]
5735 + __p.__b_[__k]);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005736}
5737
5738template <class _CharT, class _Traits, class _RT>
5739basic_ostream<_CharT, _Traits>&
5740operator<<(basic_ostream<_CharT, _Traits>& __os,
5741 const piecewise_constant_distribution<_RT>& __x)
5742{
5743 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005744 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5745 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005746 _CharT __sp = __os.widen(' ');
5747 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:40 +00005748 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00005749 __os << __n;
5750 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005751 __os << __sp << __x.__p_.__b_[__i];
5752 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00005753 __os << __sp << __n;
5754 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005755 __os << __sp << __x.__p_.__densities_[__i];
5756 __n = __x.__p_.__areas_.size();
5757 __os << __sp << __n;
5758 for (size_t __i = 0; __i < __n; ++__i)
5759 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00005760 return __os;
5761}
5762
5763template <class _CharT, class _Traits, class _RT>
5764basic_istream<_CharT, _Traits>&
5765operator>>(basic_istream<_CharT, _Traits>& __is,
5766 piecewise_constant_distribution<_RT>& __x)
5767{
5768 typedef piecewise_constant_distribution<_RT> _Eng;
5769 typedef typename _Eng::result_type result_type;
5770 typedef typename _Eng::param_type param_type;
Howard Hinnant2a592542010-05-24 00:35:40 +00005771 typedef typename param_type::__area_type __area_type;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005772 __save_flags<_CharT, _Traits> _(__is);
5773 __is.flags(ios_base::dec | ios_base::skipws);
5774 size_t __n;
5775 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005776 vector<result_type> __b(__n);
5777 for (size_t __i = 0; __i < __n; ++__i)
5778 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:40 +00005779 __is >> __n;
5780 vector<double> __densities(__n);
5781 for (size_t __i = 0; __i < __n; ++__i)
5782 __is >> __densities[__i];
5783 __is >> __n;
5784 vector<__area_type> __areas(__n);
5785 for (size_t __i = 0; __i < __n; ++__i)
5786 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00005787 if (!__is.fail())
5788 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00005789 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:40 +00005790 swap(__x.__p_.__densities_, __densities);
5791 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005792 }
5793 return __is;
5794}
5795
Howard Hinnant54305402010-05-25 00:27:34 +00005796// piecewise_linear_distribution
5797
5798template<class _RealType = double>
5799class piecewise_linear_distribution
5800{
5801public:
5802 // types
5803 typedef _RealType result_type;
5804
5805 class param_type
5806 {
5807 typedef typename common_type<double, result_type>::type __area_type;
5808 vector<result_type> __b_;
5809 vector<double> __densities_;
5810 vector<__area_type> __areas_;
5811 public:
5812 typedef piecewise_linear_distribution distribution_type;
5813
5814 param_type();
5815 template<class _InputIteratorB, class _InputIteratorW>
5816 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5817 _InputIteratorW __fW);
5818 template<class _UnaryOperation>
5819 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
5820 template<class _UnaryOperation>
5821 param_type(size_t __nw, result_type __xmin, result_type __xmax,
5822 _UnaryOperation __fw);
5823
5824 vector<result_type> intervals() const {return __b_;}
5825 vector<double> densities() const {return __densities_;}
5826
5827 friend bool operator==(const param_type& __x, const param_type& __y)
5828 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
5829 friend bool operator!=(const param_type& __x, const param_type& __y)
5830 {return !(__x == __y);}
5831
5832 private:
5833 void __init();
5834
5835 friend class piecewise_linear_distribution;
5836
5837 template <class _CharT, class _Traits, class _RT>
5838 friend
5839 basic_ostream<_CharT, _Traits>&
5840 operator<<(basic_ostream<_CharT, _Traits>& __os,
5841 const piecewise_linear_distribution<_RT>& __x);
5842
5843 template <class _CharT, class _Traits, class _RT>
5844 friend
5845 basic_istream<_CharT, _Traits>&
5846 operator>>(basic_istream<_CharT, _Traits>& __is,
5847 piecewise_linear_distribution<_RT>& __x);
5848 };
5849
5850private:
5851 param_type __p_;
5852
5853public:
5854 // constructor and reset functions
5855 piecewise_linear_distribution() {}
5856 template<class _InputIteratorB, class _InputIteratorW>
5857 piecewise_linear_distribution(_InputIteratorB __fB,
5858 _InputIteratorB __lB,
5859 _InputIteratorW __fW)
5860 : __p_(__fB, __lB, __fW) {}
5861
5862 template<class _UnaryOperation>
5863 piecewise_linear_distribution(initializer_list<result_type> __bl,
5864 _UnaryOperation __fw)
5865 : __p_(__bl, __fw) {}
5866
5867 template<class _UnaryOperation>
5868 piecewise_linear_distribution(size_t __nw, result_type __xmin,
5869 result_type __xmax, _UnaryOperation __fw)
5870 : __p_(__nw, __xmin, __xmax, __fw) {}
5871
5872 explicit piecewise_linear_distribution(const param_type& __p)
5873 : __p_(__p) {}
5874
5875 void reset() {}
5876
5877 // generating functions
5878 template<class _URNG> result_type operator()(_URNG& __g)
5879 {return (*this)(__g, __p_);}
5880 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5881
5882 // property functions
5883 vector<result_type> intervals() const {return __p_.intervals();}
5884 vector<double> densities() const {return __p_.densities();}
5885
5886 param_type param() const {return __p_;}
5887 void param(const param_type& __p) {__p_ = __p;}
5888
5889 result_type min() const {return __p_.__b_.front();}
5890 result_type max() const {return __p_.__b_.back();}
5891
5892 friend bool operator==(const piecewise_linear_distribution& __x,
5893 const piecewise_linear_distribution& __y)
5894 {return __x.__p_ == __y.__p_;}
5895 friend bool operator!=(const piecewise_linear_distribution& __x,
5896 const piecewise_linear_distribution& __y)
5897 {return !(__x == __y);}
5898
5899 template <class _CharT, class _Traits, class _RT>
5900 friend
5901 basic_ostream<_CharT, _Traits>&
5902 operator<<(basic_ostream<_CharT, _Traits>& __os,
5903 const piecewise_linear_distribution<_RT>& __x);
5904
5905 template <class _CharT, class _Traits, class _RT>
5906 friend
5907 basic_istream<_CharT, _Traits>&
5908 operator>>(basic_istream<_CharT, _Traits>& __is,
5909 piecewise_linear_distribution<_RT>& __x);
5910};
5911
5912template<class _RealType>
5913void
5914piecewise_linear_distribution<_RealType>::param_type::__init()
5915{
5916 __areas_.assign(__densities_.size() - 1, __area_type());
5917 __area_type _S = 0;
5918 for (size_t __i = 0; __i < __areas_.size(); ++__i)
5919 {
5920 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
5921 (__b_[__i+1] - __b_[__i]) * .5;
5922 _S += __areas_[__i];
5923 }
5924 for (size_t __i = __areas_.size(); __i > 1;)
5925 {
5926 --__i;
5927 __areas_[__i] = __areas_[__i-1] / _S;
5928 }
5929 __areas_[0] = 0;
5930 for (size_t __i = 1; __i < __areas_.size(); ++__i)
5931 __areas_[__i] += __areas_[__i-1];
5932 for (size_t __i = 0; __i < __densities_.size(); ++__i)
5933 __densities_[__i] /= _S;
5934}
5935
5936template<class _RealType>
5937piecewise_linear_distribution<_RealType>::param_type::param_type()
5938 : __b_(2),
5939 __densities_(2, 1.0),
5940 __areas_(1, 0.0)
5941{
5942 __b_[1] = 1;
5943}
5944
5945template<class _RealType>
5946template<class _InputIteratorB, class _InputIteratorW>
5947piecewise_linear_distribution<_RealType>::param_type::param_type(
5948 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
5949 : __b_(__fB, __lB)
5950{
5951 if (__b_.size() < 2)
5952 {
5953 __b_.resize(2);
5954 __b_[0] = 0;
5955 __b_[1] = 1;
5956 __densities_.assign(2, 1.0);
5957 __areas_.assign(1, 0.0);
5958 }
5959 else
5960 {
5961 __densities_.reserve(__b_.size());
5962 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
5963 __densities_.push_back(*__fW);
5964 __init();
5965 }
5966}
5967
5968template<class _RealType>
5969template<class _UnaryOperation>
5970piecewise_linear_distribution<_RealType>::param_type::param_type(
5971 initializer_list<result_type> __bl, _UnaryOperation __fw)
5972 : __b_(__bl.begin(), __bl.end())
5973{
5974 if (__b_.size() < 2)
5975 {
5976 __b_.resize(2);
5977 __b_[0] = 0;
5978 __b_[1] = 1;
5979 __densities_.assign(2, 1.0);
5980 __areas_.assign(1, 0.0);
5981 }
5982 else
5983 {
5984 __densities_.reserve(__b_.size());
5985 for (size_t __i = 0; __i < __b_.size(); ++__i)
5986 __densities_.push_back(__fw(__b_[__i]));
5987 __init();
5988 }
5989}
5990
5991template<class _RealType>
5992template<class _UnaryOperation>
5993piecewise_linear_distribution<_RealType>::param_type::param_type(
5994 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
5995 : __b_(__nw == 0 ? 2 : __nw + 1)
5996{
5997 size_t __n = __b_.size() - 1;
5998 result_type __d = (__xmax - __xmin) / __n;
5999 __densities_.reserve(__b_.size());
6000 for (size_t __i = 0; __i < __n; ++__i)
6001 {
6002 __b_[__i] = __xmin + __i * __d;
6003 __densities_.push_back(__fw(__b_[__i]));
6004 }
6005 __b_[__n] = __xmax;
6006 __densities_.push_back(__fw(__b_[__n]));
6007 __init();
6008}
6009
6010template<class _RealType>
6011template<class _URNG>
6012_RealType
6013piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6014{
6015 typedef uniform_real_distribution<result_type> _Gen;
6016 result_type __u = _Gen()(__g);
6017 ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
6018 static_cast<double>(__u)) - __p.__areas_.begin() - 1;
6019 __u -= __p.__areas_[__k];
6020 const double __dk = __p.__densities_[__k];
6021 const double __dk1 = __p.__densities_[__k+1];
6022 const double __deltad = __dk1 - __dk;
6023 const result_type __bk = __p.__b_[__k];
6024 if (__deltad == 0)
6025 return static_cast<result_type>(__u / __dk + __bk);
6026 const result_type __bk1 = __p.__b_[__k+1];
6027 const result_type __deltab = __bk1 - __bk;
6028 return static_cast<result_type>((__bk * __dk1 - __bk1 * __dk +
6029 _STD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
6030 __deltad);
6031}
6032
6033template <class _CharT, class _Traits, class _RT>
6034basic_ostream<_CharT, _Traits>&
6035operator<<(basic_ostream<_CharT, _Traits>& __os,
6036 const piecewise_linear_distribution<_RT>& __x)
6037{
6038 __save_flags<_CharT, _Traits> _(__os);
6039 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6040 ios_base::scientific);
6041 _CharT __sp = __os.widen(' ');
6042 __os.fill(__sp);
6043 size_t __n = __x.__p_.__b_.size();
6044 __os << __n;
6045 for (size_t __i = 0; __i < __n; ++__i)
6046 __os << __sp << __x.__p_.__b_[__i];
6047 __n = __x.__p_.__densities_.size();
6048 __os << __sp << __n;
6049 for (size_t __i = 0; __i < __n; ++__i)
6050 __os << __sp << __x.__p_.__densities_[__i];
6051 __n = __x.__p_.__areas_.size();
6052 __os << __sp << __n;
6053 for (size_t __i = 0; __i < __n; ++__i)
6054 __os << __sp << __x.__p_.__areas_[__i];
6055 return __os;
6056}
6057
6058template <class _CharT, class _Traits, class _RT>
6059basic_istream<_CharT, _Traits>&
6060operator>>(basic_istream<_CharT, _Traits>& __is,
6061 piecewise_linear_distribution<_RT>& __x)
6062{
6063 typedef piecewise_linear_distribution<_RT> _Eng;
6064 typedef typename _Eng::result_type result_type;
6065 typedef typename _Eng::param_type param_type;
6066 typedef typename param_type::__area_type __area_type;
6067 __save_flags<_CharT, _Traits> _(__is);
6068 __is.flags(ios_base::dec | ios_base::skipws);
6069 size_t __n;
6070 __is >> __n;
6071 vector<result_type> __b(__n);
6072 for (size_t __i = 0; __i < __n; ++__i)
6073 __is >> __b[__i];
6074 __is >> __n;
6075 vector<double> __densities(__n);
6076 for (size_t __i = 0; __i < __n; ++__i)
6077 __is >> __densities[__i];
6078 __is >> __n;
6079 vector<__area_type> __areas(__n);
6080 for (size_t __i = 0; __i < __n; ++__i)
6081 __is >> __areas[__i];
6082 if (!__is.fail())
6083 {
6084 swap(__x.__p_.__b_, __b);
6085 swap(__x.__p_.__densities_, __densities);
6086 swap(__x.__p_.__areas_, __areas);
6087 }
6088 return __is;
6089}
6090
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00006091_LIBCPP_END_NAMESPACE_STD
6092
6093#endif // _LIBCPP_RANDOM