blob: 49646e908624fbff333d42e61015f4eaf24b34fe [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//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_RANDOM
12#define _LIBCPP_RANDOM
13
14/*
15 random synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22// Engines
23
24template <class UIntType, UIntType a, UIntType c, UIntType m>
25class linear_congruential_engine
26{
27public:
28 // types
29 typedef UIntType result_type;
30
31 // engine characteristics
32 static constexpr result_type multiplier = a;
33 static constexpr result_type increment = c;
34 static constexpr result_type modulus = m;
35 static constexpr result_type min() { return c == 0u ? 1u: 0u;}
36 static constexpr result_type max() { return m - 1u;}
37 static constexpr result_type default_seed = 1u;
38
39 // constructors and seeding functions
40 explicit linear_congruential_engine(result_type s = default_seed);
41 template<class Sseq> explicit linear_congruential_engine(Sseq& q);
42 void seed(result_type s = default_seed);
43 template<class Sseq> void seed(Sseq& q);
44
45 // generating functions
46 result_type operator()();
47 void discard(unsigned long long z);
48};
49
50template <class UIntType, UIntType a, UIntType c, UIntType m>
51bool
52operator==(const linear_congruential_engine<UIntType, a, c, m>& x,
53 const linear_congruential_engine<UIntType, a, c, m>& y);
54
55template <class UIntType, UIntType a, UIntType c, UIntType m>
56bool
57operator!=(const linear_congruential_engine<UIntType, a, c, m>& x,
58 const linear_congruential_engine<UIntType, a, c, m>& y);
59
60template <class charT, class traits,
61 class UIntType, UIntType a, UIntType c, UIntType m>
62basic_ostream<charT, traits>&
63operator<<(basic_ostream<charT, traits>& os,
64 const linear_congruential_engine<UIntType, a, c, m>& x);
65
66template <class charT, class traits,
67 class UIntType, UIntType a, UIntType c, UIntType m>
68basic_istream<charT, traits>&
69operator>>(basic_istream<charT, traits>& is,
70 linear_congruential_engine<UIntType, a, c, m>& x);
71
72template <class UIntType, size_t w, size_t n, size_t m, size_t r,
73 UIntType a, size_t u, UIntType d, size_t s,
74 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
75class mersenne_twister_engine
76{
77public:
78 // types
79 typedef UIntType result_type;
80
81 // engine characteristics
82 static constexpr size_t word_size = w;
83 static constexpr size_t state_size = n;
84 static constexpr size_t shift_size = m;
85 static constexpr size_t mask_bits = r;
86 static constexpr result_type xor_mask = a;
87 static constexpr size_t tempering_u = u;
88 static constexpr result_type tempering_d = d;
89 static constexpr size_t tempering_s = s;
90 static constexpr result_type tempering_b = b;
91 static constexpr size_t tempering_t = t;
92 static constexpr result_type tempering_c = c;
93 static constexpr size_t tempering_l = l;
94 static constexpr result_type initialization_multiplier = f;
95 static constexpr result_type min () { return 0; }
96 static constexpr result_type max() { return 2^w - 1; }
97 static constexpr result_type default_seed = 5489u;
98
99 // constructors and seeding functions
100 explicit mersenne_twister_engine(result_type value = default_seed);
101 template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
102 void seed(result_type value = default_seed);
103 template<class Sseq> void seed(Sseq& q);
104
105 // generating functions
106 result_type operator()();
107 void discard(unsigned long long z);
108};
109
110template <class UIntType, size_t w, size_t n, size_t m, size_t r,
111 UIntType a, size_t u, UIntType d, size_t s,
112 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
113bool
114operator==(
115 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
116 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
117
118template <class UIntType, size_t w, size_t n, size_t m, size_t r,
119 UIntType a, size_t u, UIntType d, size_t s,
120 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
121bool
122operator!=(
123 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
124 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
125
126template <class charT, class traits,
127 class UIntType, size_t w, size_t n, size_t m, size_t r,
128 UIntType a, size_t u, UIntType d, size_t s,
129 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
130basic_ostream<charT, traits>&
131operator<<(basic_ostream<charT, traits>& os,
132 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
133
134template <class charT, class traits,
135 class UIntType, size_t w, size_t n, size_t m, size_t r,
136 UIntType a, size_t u, UIntType d, size_t s,
137 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
138basic_istream<charT, traits>&
139operator>>(basic_istream<charT, traits>& is,
140 mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
141
142template<class UIntType, size_t w, size_t s, size_t r>
143class subtract_with_carry_engine
144{
145public:
146 // types
147 typedef UIntType result_type;
148
149 // engine characteristics
150 static constexpr size_t word_size = w;
151 static constexpr size_t short_lag = s;
152 static constexpr size_t long_lag = r;
153 static constexpr result_type min() { return 0; }
154 static constexpr result_type max() { return m-1; }
155 static constexpr result_type default_seed = 19780503u;
156
157 // constructors and seeding functions
158 explicit subtract_with_carry_engine(result_type value = default_seed);
159 template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
160 void seed(result_type value = default_seed);
161 template<class Sseq> void seed(Sseq& q);
162
163 // generating functions
164 result_type operator()();
165 void discard(unsigned long long z);
166};
167
168template<class UIntType, size_t w, size_t s, size_t r>
169bool
170operator==(
171 const subtract_with_carry_engine<UIntType, w, s, r>& x,
172 const subtract_with_carry_engine<UIntType, w, s, r>& y);
173
174template<class UIntType, size_t w, size_t s, size_t r>
175bool
176operator!=(
177 const subtract_with_carry_engine<UIntType, w, s, r>& x,
178 const subtract_with_carry_engine<UIntType, w, s, r>& y);
179
180template <class charT, class traits,
181 class UIntType, size_t w, size_t s, size_t r>
182basic_ostream<charT, traits>&
183operator<<(basic_ostream<charT, traits>& os,
184 const subtract_with_carry_engine<UIntType, w, s, r>& x);
185
186template <class charT, class traits,
187 class UIntType, size_t w, size_t s, size_t r>
188basic_istream<charT, traits>&
189operator>>(basic_istream<charT, traits>& is,
190 subtract_with_carry_engine<UIntType, w, s, r>& x);
191
192template<class Engine, size_t p, size_t r>
193class discard_block_engine
194{
195public:
196 // types
197 typedef typename Engine::result_type result_type;
198
199 // engine characteristics
200 static constexpr size_t block_size = p;
201 static constexpr size_t used_block = r;
202 static constexpr result_type min() { return Engine::min(); }
203 static constexpr result_type max() { return Engine::max(); }
204
205 // constructors and seeding functions
206 discard_block_engine();
207 explicit discard_block_engine(const Engine& e);
208 explicit discard_block_engine(Engine&& e);
209 explicit discard_block_engine(result_type s);
210 template<class Sseq> explicit discard_block_engine(Sseq& q);
211 void seed();
212 void seed(result_type s);
213 template<class Sseq> void seed(Sseq& q);
214
215 // generating functions
216 result_type operator()();
217 void discard(unsigned long long z);
218
219 // property functions
220 const Engine& base() const;
221};
222
223template<class Engine, size_t p, size_t r>
224bool
225operator==(
226 const discard_block_engine<Engine, p, r>& x,
227 const discard_block_engine<Engine, p, r>& y);
228
229template<class Engine, size_t p, size_t r>
230bool
231operator!=(
232 const discard_block_engine<Engine, p, r>& x,
233 const discard_block_engine<Engine, p, r>& y);
234
235template <class charT, class traits,
236 class Engine, size_t p, size_t r>
237basic_ostream<charT, traits>&
238operator<<(basic_ostream<charT, traits>& os,
239 const discard_block_engine<Engine, p, r>& x);
240
241template <class charT, class traits,
242 class Engine, size_t p, size_t r>
243basic_istream<charT, traits>&
244operator>>(basic_istream<charT, traits>& is,
245 discard_block_engine<Engine, p, r>& x);
246
247template<class Engine, size_t w, class UIntType>
248class independent_bits_engine
249{
250public:
251 // types
252 typedef UIntType result_type;
253
254 // engine characteristics
255 static constexpr result_type min() { return 0; }
256 static constexpr result_type max() { return 2^w - 1; }
257
258 // constructors and seeding functions
259 independent_bits_engine();
260 explicit independent_bits_engine(const Engine& e);
261 explicit independent_bits_engine(Engine&& e);
262 explicit independent_bits_engine(result_type s);
263 template<class Sseq> explicit independent_bits_engine(Sseq& q);
264 void seed();
265 void seed(result_type s);
266 template<class Sseq> void seed(Sseq& q);
267
268 // generating functions
269 result_type operator()(); void discard(unsigned long long z);
270
271 // property functions
272 const Engine& base() const;
273};
274
275template<class Engine, size_t w, class UIntType>
276bool
277operator==(
278 const independent_bits_engine<Engine, w, UIntType>& x,
279 const independent_bits_engine<Engine, w, UIntType>& y);
280
281template<class Engine, size_t w, class UIntType>
282bool
283operator!=(
284 const independent_bits_engine<Engine, w, UIntType>& x,
285 const independent_bits_engine<Engine, w, UIntType>& y);
286
287template <class charT, class traits,
288 class Engine, size_t w, class UIntType>
289basic_ostream<charT, traits>&
290operator<<(basic_ostream<charT, traits>& os,
291 const independent_bits_engine<Engine, w, UIntType>& x);
292
293template <class charT, class traits,
294 class Engine, size_t w, class UIntType>
295basic_istream<charT, traits>&
296operator>>(basic_istream<charT, traits>& is,
297 independent_bits_engine<Engine, w, UIntType>& x);
298
299template<class Engine, size_t k>
300class shuffle_order_engine
301{
302public:
303 // types
304 typedef typename Engine::result_type result_type;
305
306 // engine characteristics
307 static constexpr size_t table_size = k;
308 static constexpr result_type min() { return Engine::min; }
309 static constexpr result_type max() { return Engine::max; }
310
311 // constructors and seeding functions
312 shuffle_order_engine();
313 explicit shuffle_order_engine(const Engine& e);
314 explicit shuffle_order_engine(Engine&& e);
315 explicit shuffle_order_engine(result_type s);
316 template<class Sseq> explicit shuffle_order_engine(Sseq& q);
317 void seed();
318 void seed(result_type s);
319 template<class Sseq> void seed(Sseq& q);
320
321 // generating functions
322 result_type operator()();
323 void discard(unsigned long long z);
324
325 // property functions
326 const Engine& base() const;
327};
328
329template<class Engine, size_t k>
330bool
331operator==(
332 const shuffle_order_engine<Engine, k>& x,
333 const shuffle_order_engine<Engine, k>& y);
334
335template<class Engine, size_t k>
336bool
337operator!=(
338 const shuffle_order_engine<Engine, k>& x,
339 const shuffle_order_engine<Engine, k>& y);
340
341template <class charT, class traits,
342 class Engine, size_t k>
343basic_ostream<charT, traits>&
344operator<<(basic_ostream<charT, traits>& os,
345 const shuffle_order_engine<Engine, k>& x);
346
347template <class charT, class traits,
348 class Engine, size_t k>
349basic_istream<charT, traits>&
350operator>>(basic_istream<charT, traits>& is,
351 shuffle_order_engine<Engine, k>& x);
352
353typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
354 minstd_rand0;
355typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
356 minstd_rand;
357typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
358 0x9908b0df,
359 11, 0xffffffff,
360 7, 0x9d2c5680,
361 15, 0xefc60000,
362 18, 1812433253> mt19937;
363typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
364 0xb5026f5aa96619e9,
365 29, 0x5555555555555555,
366 17, 0x71d67fffeda60000,
367 37, 0xfff7eee000000000,
368 43, 6364136223846793005> mt19937_64;
369typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
370typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
371typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
372typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
373typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
Howard Hinnantd6d11712010-05-20 15:11:46 +0000374typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375
376// Generators
377
378class random_device
379{
380public:
381 // types
382 typedef unsigned int result_type;
383
384 // generator characteristics
385 static constexpr result_type min() { return numeric_limits<result_type>::min(); }
386 static constexpr result_type max() { return numeric_limits<result_type>::max(); }
387
388 // constructors
389 explicit random_device(const string& token = "/dev/urandom");
390
391 // generating functions
392 result_type operator()();
393
394 // property functions
395 double entropy() const;
396
397 // no copy functions
398 random_device(const random_device& ) = delete;
399 void operator=(const random_device& ) = delete;
400};
401
402// Utilities
403
404class seed_seq
405{
406public:
407 // types
408 typedef uint_least32_t result_type;
409
410 // constructors
411 seed_seq();
412 template<class T>
413 seed_seq(initializer_list<T> il);
414 template<class InputIterator>
415 seed_seq(InputIterator begin, InputIterator end);
416
417 // generating functions
418 template<class RandomAccessIterator>
419 void generate(RandomAccessIterator begin, RandomAccessIterator end);
420
421 // property functions
422 size_t size() const;
423 template<class OutputIterator>
424 void param(OutputIterator dest) const;
425
426 // no copy functions
427 seed_seq(const seed_seq&) = delete;
428 void operator=(const seed_seq& ) = delete;
429};
430
431template<class RealType, size_t bits, class URNG>
432 RealType generate_canonical(URNG& g);
433
434// Distributions
435
436template<class IntType = int>
437class uniform_int_distribution
438{
439public:
440 // types
441 typedef IntType result_type;
442
443 class param_type
444 {
445 public:
446 typedef uniform_int_distribution distribution_type;
447
448 explicit param_type(IntType a = 0,
449 IntType b = numeric_limits<IntType>::max());
450
451 result_type a() const;
452 result_type b() const;
453
454 friend bool operator==(const param_type& x, const param_type& y);
455 friend bool operator!=(const param_type& x, const param_type& y);
456 };
457
458 // constructors and reset functions
459 explicit uniform_int_distribution(IntType a = 0,
460 IntType b = numeric_limits<IntType>::max());
461 explicit uniform_int_distribution(const param_type& parm);
462 void reset();
463
464 // generating functions
465 template<class URNG> result_type operator()(URNG& g);
466 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
467
468 // property functions
469 result_type a() const;
470 result_type b() const;
471
472 param_type param() const;
473 void param(const param_type& parm);
474
475 result_type min() const;
476 result_type max() const;
477
478 friend bool operator==(const uniform_int_distribution& x,
479 const uniform_int_distribution& y);
480 friend bool operator!=(const uniform_int_distribution& x,
481 const uniform_int_distribution& y);
482
483 template <class charT, class traits>
484 friend
485 basic_ostream<charT, traits>&
486 operator<<(basic_ostream<charT, traits>& os,
487 const uniform_int_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000488
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489 template <class charT, class traits>
490 friend
491 basic_istream<charT, traits>&
492 operator>>(basic_istream<charT, traits>& is,
493 uniform_int_distribution& x);
494};
495
496template<class RealType = double>
497class uniform_real_distribution
498{
499public:
500 // types
501 typedef RealType result_type;
502
503 class param_type
504 {
505 public:
506 typedef uniform_real_distribution distribution_type;
507
508 explicit param_type(RealType a = 0,
509 RealType b = 1);
510
511 result_type a() const;
512 result_type b() const;
513
514 friend bool operator==(const param_type& x, const param_type& y);
515 friend bool operator!=(const param_type& x, const param_type& y);
516 };
517
518 // constructors and reset functions
519 explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);
520 explicit uniform_real_distribution(const param_type& parm);
521 void reset();
522
523 // generating functions
524 template<class URNG> result_type operator()(URNG& g);
525 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
526
527 // property functions
528 result_type a() const;
529 result_type b() const;
530
531 param_type param() const;
532 void param(const param_type& parm);
533
534 result_type min() const;
535 result_type max() const;
536
537 friend bool operator==(const uniform_real_distribution& x,
538 const uniform_real_distribution& y);
539 friend bool operator!=(const uniform_real_distribution& x,
540 const uniform_real_distribution& y);
541
542 template <class charT, class traits>
543 friend
544 basic_ostream<charT, traits>&
545 operator<<(basic_ostream<charT, traits>& os,
546 const uniform_real_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000547
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548 template <class charT, class traits>
549 friend
550 basic_istream<charT, traits>&
551 operator>>(basic_istream<charT, traits>& is,
552 uniform_real_distribution& x);
553};
554
555class bernoulli_distribution
556{
557public:
558 // types
559 typedef bool result_type;
560
561 class param_type
562 {
563 public:
564 typedef bernoulli_distribution distribution_type;
565
566 explicit param_type(double p = 0.5);
567
568 double p() const;
569
570 friend bool operator==(const param_type& x, const param_type& y);
571 friend bool operator!=(const param_type& x, const param_type& y);
572 };
573
574 // constructors and reset functions
575 explicit bernoulli_distribution(double p = 0.5);
576 explicit bernoulli_distribution(const param_type& parm);
577 void reset();
578
579 // generating functions
580 template<class URNG> result_type operator()(URNG& g);
581 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
582
583 // property functions
584 double p() const;
585
586 param_type param() const;
587 void param(const param_type& parm);
588
589 result_type min() const;
590 result_type max() const;
591
592 friend bool operator==(const bernoulli_distribution& x,
593 const bernoulli_distribution& y);
594 friend bool operator!=(const bernoulli_distribution& x,
595 const bernoulli_distribution& y);
596
597 template <class charT, class traits>
598 friend
599 basic_ostream<charT, traits>&
600 operator<<(basic_ostream<charT, traits>& os,
601 const bernoulli_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000602
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603 template <class charT, class traits>
604 friend
605 basic_istream<charT, traits>&
606 operator>>(basic_istream<charT, traits>& is,
607 bernoulli_distribution& x);
608};
609
610template<class IntType = int>
Howard Hinnant03aad812010-05-11 23:26:59 +0000611class binomial_distribution
612{
613public:
614 // types
615 typedef IntType result_type;
616
617 class param_type
618 {
619 public:
620 typedef binomial_distribution distribution_type;
621
622 explicit param_type(IntType t = 1, double p = 0.5);
623
624 IntType t() const;
625 double p() const;
626
627 friend bool operator==(const param_type& x, const param_type& y);
628 friend bool operator!=(const param_type& x, const param_type& y);
629 };
630
631 // constructors and reset functions
632 explicit binomial_distribution(IntType t = 1, double p = 0.5);
633 explicit binomial_distribution(const param_type& parm);
634 void reset();
635
636 // generating functions
637 template<class URNG> result_type operator()(URNG& g);
638 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
639
640 // property functions
641 IntType t() const;
642 double p() const;
643
644 param_type param() const;
645 void param(const param_type& parm);
646
647 result_type min() const;
648 result_type max() const;
649
650 friend bool operator==(const binomial_distribution& x,
651 const binomial_distribution& y);
652 friend bool operator!=(const binomial_distribution& x,
653 const binomial_distribution& y);
654
655 template <class charT, class traits>
656 friend
657 basic_ostream<charT, traits>&
658 operator<<(basic_ostream<charT, traits>& os,
659 const binomial_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000660
Howard Hinnant03aad812010-05-11 23:26:59 +0000661 template <class charT, class traits>
662 friend
663 basic_istream<charT, traits>&
664 operator>>(basic_istream<charT, traits>& is,
665 binomial_distribution& x);
666};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667
668template<class IntType = int>
Howard Hinnant34e8a572010-05-17 13:44:27 +0000669class geometric_distribution
670{
671public:
672 // types
673 typedef IntType result_type;
674
675 class param_type
676 {
677 public:
678 typedef geometric_distribution distribution_type;
679
680 explicit param_type(double p = 0.5);
681
682 double p() const;
683
684 friend bool operator==(const param_type& x, const param_type& y);
685 friend bool operator!=(const param_type& x, const param_type& y);
686 };
687
688 // constructors and reset functions
689 explicit geometric_distribution(double p = 0.5);
690 explicit geometric_distribution(const param_type& parm);
691 void reset();
692
693 // generating functions
694 template<class URNG> result_type operator()(URNG& g);
695 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
696
697 // property functions
698 double p() const;
699
700 param_type param() const;
701 void param(const param_type& parm);
702
703 result_type min() const;
704 result_type max() const;
705
706 friend bool operator==(const geometric_distribution& x,
707 const geometric_distribution& y);
708 friend bool operator!=(const geometric_distribution& x,
709 const geometric_distribution& y);
710
711 template <class charT, class traits>
712 friend
713 basic_ostream<charT, traits>&
714 operator<<(basic_ostream<charT, traits>& os,
715 const geometric_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000716
Howard Hinnant34e8a572010-05-17 13:44:27 +0000717 template <class charT, class traits>
718 friend
719 basic_istream<charT, traits>&
720 operator>>(basic_istream<charT, traits>& is,
721 geometric_distribution& x);
722};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723
724template<class IntType = int>
Howard Hinnantf2fe5d52010-05-17 00:09:38 +0000725class negative_binomial_distribution
726{
727public:
728 // types
729 typedef IntType result_type;
730
731 class param_type
732 {
733 public:
734 typedef negative_binomial_distribution distribution_type;
735
736 explicit param_type(result_type k = 1, double p = 0.5);
737
738 result_type k() const;
739 double p() const;
740
741 friend bool operator==(const param_type& x, const param_type& y);
742 friend bool operator!=(const param_type& x, const param_type& y);
743 };
744
745 // constructor and reset functions
746 explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
747 explicit negative_binomial_distribution(const param_type& parm);
748 void reset();
749
750 // generating functions
751 template<class URNG> result_type operator()(URNG& g);
752 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
753
754 // property functions
755 result_type k() const;
756 double p() const;
757
758 param_type param() const;
759 void param(const param_type& parm);
760
761 result_type min() const;
762 result_type max() const;
763
764 friend bool operator==(const negative_binomial_distribution& x,
765 const negative_binomial_distribution& y);
766 friend bool operator!=(const negative_binomial_distribution& x,
767 const negative_binomial_distribution& y);
768
769 template <class charT, class traits>
770 friend
771 basic_ostream<charT, traits>&
772 operator<<(basic_ostream<charT, traits>& os,
773 const negative_binomial_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000774
Howard Hinnantf2fe5d52010-05-17 00:09:38 +0000775 template <class charT, class traits>
776 friend
777 basic_istream<charT, traits>&
778 operator>>(basic_istream<charT, traits>& is,
779 negative_binomial_distribution& x);
780};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000781
782template<class IntType = int>
Howard Hinnant4ff556c2010-05-14 21:38:54 +0000783class poisson_distribution
784{
785public:
786 // types
787 typedef IntType result_type;
788
789 class param_type
790 {
791 public:
792 typedef poisson_distribution distribution_type;
793
794 explicit param_type(double mean = 1.0);
795
796 double mean() const;
797
798 friend bool operator==(const param_type& x, const param_type& y);
799 friend bool operator!=(const param_type& x, const param_type& y);
800 };
801
802 // constructors and reset functions
803 explicit poisson_distribution(double mean = 1.0);
804 explicit poisson_distribution(const param_type& parm);
805 void reset();
806
807 // generating functions
808 template<class URNG> result_type operator()(URNG& g);
809 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
810
811 // property functions
812 double mean() const;
813
814 param_type param() const;
815 void param(const param_type& parm);
816
817 result_type min() const;
818 result_type max() const;
819
820 friend bool operator==(const poisson_distribution& x,
821 const poisson_distribution& y);
822 friend bool operator!=(const poisson_distribution& x,
823 const poisson_distribution& y);
824
825 template <class charT, class traits>
826 friend
827 basic_ostream<charT, traits>&
828 operator<<(basic_ostream<charT, traits>& os,
829 const poisson_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000830
Howard Hinnant4ff556c2010-05-14 21:38:54 +0000831 template <class charT, class traits>
832 friend
833 basic_istream<charT, traits>&
834 operator>>(basic_istream<charT, traits>& is,
835 poisson_distribution& x);
836};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837
838template<class RealType = double>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000839class exponential_distribution
840{
841public:
842 // types
843 typedef RealType result_type;
844
845 class param_type
846 {
847 public:
848 typedef exponential_distribution distribution_type;
849
Howard Hinnanta64111c2010-05-12 21:02:31 +0000850 explicit param_type(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000851
Howard Hinnanta64111c2010-05-12 21:02:31 +0000852 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000853
854 friend bool operator==(const param_type& x, const param_type& y);
855 friend bool operator!=(const param_type& x, const param_type& y);
856 };
857
858 // constructors and reset functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000859 explicit exponential_distribution(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000860 explicit exponential_distribution(const param_type& parm);
861 void reset();
862
863 // generating functions
864 template<class URNG> result_type operator()(URNG& g);
865 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
866
867 // property functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000868 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000869
870 param_type param() const;
871 void param(const param_type& parm);
872
873 result_type min() const;
874 result_type max() const;
875
876 friend bool operator==(const exponential_distribution& x,
877 const exponential_distribution& y);
878 friend bool operator!=(const exponential_distribution& x,
879 const exponential_distribution& y);
880
881 template <class charT, class traits>
882 friend
883 basic_ostream<charT, traits>&
884 operator<<(basic_ostream<charT, traits>& os,
885 const exponential_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000886
Howard Hinnant30a840f2010-05-12 17:08:57 +0000887 template <class charT, class traits>
888 friend
889 basic_istream<charT, traits>&
890 operator>>(basic_istream<charT, traits>& is,
891 exponential_distribution& x);
892};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893
894template<class RealType = double>
Howard Hinnantc7c49132010-05-13 17:58:28 +0000895class gamma_distribution
896{
897public:
898 // types
899 typedef RealType result_type;
900
901 class param_type
902 {
903 public:
904 typedef gamma_distribution distribution_type;
905
906 explicit param_type(result_type alpha = 1, result_type beta = 1);
907
908 result_type alpha() const;
909 result_type beta() const;
910
911 friend bool operator==(const param_type& x, const param_type& y);
912 friend bool operator!=(const param_type& x, const param_type& y);
913 };
914
915 // constructors and reset functions
916 explicit gamma_distribution(result_type alpha = 1, result_type beta = 1);
917 explicit gamma_distribution(const param_type& parm);
918 void reset();
919
920 // generating functions
921 template<class URNG> result_type operator()(URNG& g);
922 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
923
924 // property functions
925 result_type alpha() const;
926 result_type beta() const;
927
928 param_type param() const;
929 void param(const param_type& parm);
930
931 result_type min() const;
932 result_type max() const;
933
934 friend bool operator==(const gamma_distribution& x,
935 const gamma_distribution& y);
936 friend bool operator!=(const gamma_distribution& x,
937 const gamma_distribution& y);
938
939 template <class charT, class traits>
940 friend
941 basic_ostream<charT, traits>&
942 operator<<(basic_ostream<charT, traits>& os,
943 const gamma_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +0000944
Howard Hinnantc7c49132010-05-13 17:58:28 +0000945 template <class charT, class traits>
946 friend
947 basic_istream<charT, traits>&
948 operator>>(basic_istream<charT, traits>& is,
949 gamma_distribution& x);
950};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951
952template<class RealType = double>
Howard Hinnant9de6e302010-05-16 01:09:02 +0000953class weibull_distribution
954{
955public:
956 // types
957 typedef RealType result_type;
958
959 class param_type
960 {
961 public:
962 typedef weibull_distribution distribution_type;
963
964 explicit param_type(result_type alpha = 1, result_type beta = 1);
965
966 result_type a() const;
967 result_type b() const;
968
969 friend bool operator==(const param_type& x, const param_type& y);
970 friend bool operator!=(const param_type& x, const param_type& y);
971 };
972
973 // constructor and reset functions
974 explicit weibull_distribution(result_type a = 1, result_type b = 1);
975 explicit weibull_distribution(const param_type& parm);
976 void reset();
977
978 // generating functions
979 template<class URNG> result_type operator()(URNG& g);
980 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
981
982 // property functions
983 result_type a() const;
984 result_type b() const;
985
986 param_type param() const;
987 void param(const param_type& parm);
988
989 result_type min() const;
990 result_type max() const;
991
Howard Hinnant9de6e302010-05-16 01:09:02 +0000992 friend bool operator==(const weibull_distribution& x,
993 const weibull_distribution& y);
994 friend bool operator!=(const weibull_distribution& x,
995 const weibull_distribution& y);
996
997 template <class charT, class traits>
998 friend
999 basic_ostream<charT, traits>&
1000 operator<<(basic_ostream<charT, traits>& os,
1001 const weibull_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001002
Howard Hinnant9de6e302010-05-16 01:09:02 +00001003 template <class charT, class traits>
1004 friend
1005 basic_istream<charT, traits>&
1006 operator>>(basic_istream<charT, traits>& is,
1007 weibull_distribution& x);
1008};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009
1010template<class RealType = double>
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00001011class extreme_value_distribution
1012{
1013public:
1014 // types
1015 typedef RealType result_type;
1016
1017 class param_type
1018 {
1019 public:
1020 typedef extreme_value_distribution distribution_type;
1021
1022 explicit param_type(result_type a = 0, result_type b = 1);
1023
1024 result_type a() const;
1025 result_type b() const;
1026
1027 friend bool operator==(const param_type& x, const param_type& y);
1028 friend bool operator!=(const param_type& x, const param_type& y);
1029 };
1030
1031 // constructor and reset functions
1032 explicit extreme_value_distribution(result_type a = 0, result_type b = 1);
1033 explicit extreme_value_distribution(const param_type& parm);
1034 void reset();
1035
1036 // generating functions
1037 template<class URNG> result_type operator()(URNG& g);
1038 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1039
1040 // property functions
1041 result_type a() const;
1042 result_type b() const;
1043
1044 param_type param() const;
1045 void param(const param_type& parm);
1046
1047 result_type min() const;
1048 result_type max() const;
1049
1050 friend bool operator==(const extreme_value_distribution& x,
1051 const extreme_value_distribution& y);
1052 friend bool operator!=(const extreme_value_distribution& x,
1053 const extreme_value_distribution& y);
1054
1055 template <class charT, class traits>
1056 friend
1057 basic_ostream<charT, traits>&
1058 operator<<(basic_ostream<charT, traits>& os,
1059 const extreme_value_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001060
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00001061 template <class charT, class traits>
1062 friend
1063 basic_istream<charT, traits>&
1064 operator>>(basic_istream<charT, traits>& is,
1065 extreme_value_distribution& x);
1066};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067
1068template<class RealType = double>
Howard Hinnanta64111c2010-05-12 21:02:31 +00001069class normal_distribution
1070{
1071public:
1072 // types
1073 typedef RealType result_type;
1074
1075 class param_type
1076 {
1077 public:
1078 typedef normal_distribution distribution_type;
1079
1080 explicit param_type(result_type mean = 0, result_type stddev = 1);
1081
1082 result_type mean() const;
1083 result_type stddev() const;
1084
1085 friend bool operator==(const param_type& x, const param_type& y);
1086 friend bool operator!=(const param_type& x, const param_type& y);
1087 };
1088
1089 // constructors and reset functions
1090 explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
1091 explicit normal_distribution(const param_type& parm);
1092 void reset();
1093
1094 // generating functions
1095 template<class URNG> result_type operator()(URNG& g);
1096 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1097
1098 // property functions
1099 result_type mean() const;
1100 result_type stddev() const;
1101
1102 param_type param() const;
1103 void param(const param_type& parm);
1104
1105 result_type min() const;
1106 result_type max() const;
1107
1108 friend bool operator==(const normal_distribution& x,
1109 const normal_distribution& y);
1110 friend bool operator!=(const normal_distribution& x,
1111 const normal_distribution& y);
1112
1113 template <class charT, class traits>
1114 friend
1115 basic_ostream<charT, traits>&
1116 operator<<(basic_ostream<charT, traits>& os,
1117 const normal_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001118
Howard Hinnanta64111c2010-05-12 21:02:31 +00001119 template <class charT, class traits>
1120 friend
1121 basic_istream<charT, traits>&
1122 operator>>(basic_istream<charT, traits>& is,
1123 normal_distribution& x);
1124};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125
1126template<class RealType = double>
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00001127class lognormal_distribution
1128{
1129public:
1130 // types
1131 typedef RealType result_type;
1132
1133 class param_type
1134 {
1135 public:
1136 typedef lognormal_distribution distribution_type;
1137
1138 explicit param_type(result_type m = 0, result_type s = 1);
1139
1140 result_type m() const;
1141 result_type s() const;
1142
1143 friend bool operator==(const param_type& x, const param_type& y);
1144 friend bool operator!=(const param_type& x, const param_type& y);
1145 };
1146
1147 // constructor and reset functions
1148 explicit lognormal_distribution(result_type m = 0, result_type s = 1);
1149 explicit lognormal_distribution(const param_type& parm);
1150 void reset();
1151
1152 // generating functions
1153 template<class URNG> result_type operator()(URNG& g);
1154 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1155
1156 // property functions
1157 result_type m() const;
1158 result_type s() const;
1159
1160 param_type param() const;
1161 void param(const param_type& parm);
1162
1163 result_type min() const;
1164 result_type max() const;
1165
1166 friend bool operator==(const lognormal_distribution& x,
1167 const lognormal_distribution& y);
1168 friend bool operator!=(const lognormal_distribution& x,
1169 const lognormal_distribution& y);
1170
1171 template <class charT, class traits>
1172 friend
1173 basic_ostream<charT, traits>&
1174 operator<<(basic_ostream<charT, traits>& os,
1175 const lognormal_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001176
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00001177 template <class charT, class traits>
1178 friend
1179 basic_istream<charT, traits>&
1180 operator>>(basic_istream<charT, traits>& is,
1181 lognormal_distribution& x);
1182};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183
1184template<class RealType = double>
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001185class chi_squared_distribution
1186{
1187public:
1188 // types
1189 typedef RealType result_type;
1190
1191 class param_type
1192 {
1193 public:
1194 typedef chi_squared_distribution distribution_type;
1195
1196 explicit param_type(result_type n = 1);
1197
1198 result_type n() const;
1199
1200 friend bool operator==(const param_type& x, const param_type& y);
1201 friend bool operator!=(const param_type& x, const param_type& y);
1202 };
1203
1204 // constructor and reset functions
1205 explicit chi_squared_distribution(result_type n = 1);
1206 explicit chi_squared_distribution(const param_type& parm);
1207 void reset();
1208
1209 // generating functions
1210 template<class URNG> result_type operator()(URNG& g);
1211 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1212
1213 // property functions
1214 result_type n() const;
1215
1216 param_type param() const;
1217 void param(const param_type& parm);
1218
1219 result_type min() const;
1220 result_type max() const;
1221
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001222 friend bool operator==(const chi_squared_distribution& x,
1223 const chi_squared_distribution& y);
1224 friend bool operator!=(const chi_squared_distribution& x,
1225 const chi_squared_distribution& y);
1226
1227 template <class charT, class traits>
1228 friend
1229 basic_ostream<charT, traits>&
1230 operator<<(basic_ostream<charT, traits>& os,
1231 const chi_squared_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001232
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001233 template <class charT, class traits>
1234 friend
1235 basic_istream<charT, traits>&
1236 operator>>(basic_istream<charT, traits>& is,
1237 chi_squared_distribution& x);
1238};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239
1240template<class RealType = double>
Howard Hinnantd7d01132010-05-17 21:55:46 +00001241class cauchy_distribution
1242{
1243public:
1244 // types
1245 typedef RealType result_type;
1246
1247 class param_type
1248 {
1249 public:
1250 typedef cauchy_distribution distribution_type;
1251
1252 explicit param_type(result_type a = 0, result_type b = 1);
1253
1254 result_type a() const;
1255 result_type b() const;
1256
1257 friend bool operator==(const param_type& x, const param_type& y);
1258 friend bool operator!=(const param_type& x, const param_type& y);
1259 };
1260
1261 // constructor and reset functions
1262 explicit cauchy_distribution(result_type a = 0, result_type b = 1);
1263 explicit cauchy_distribution(const param_type& parm);
1264 void reset();
1265
1266 // generating functions
1267 template<class URNG> result_type operator()(URNG& g);
1268 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1269
1270 // property functions
1271 result_type a() const;
1272 result_type b() const;
1273
1274 param_type param() const;
1275 void param(const param_type& parm);
1276
1277 result_type min() const;
1278 result_type max() const;
1279
1280 friend bool operator==(const cauchy_distribution& x,
1281 const cauchy_distribution& y);
1282 friend bool operator!=(const cauchy_distribution& x,
1283 const cauchy_distribution& y);
1284
1285 template <class charT, class traits>
1286 friend
1287 basic_ostream<charT, traits>&
1288 operator<<(basic_ostream<charT, traits>& os,
1289 const cauchy_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001290
Howard Hinnantd7d01132010-05-17 21:55:46 +00001291 template <class charT, class traits>
1292 friend
1293 basic_istream<charT, traits>&
1294 operator>>(basic_istream<charT, traits>& is,
1295 cauchy_distribution& x);
1296};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297
1298template<class RealType = double>
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001299class fisher_f_distribution
1300{
1301public:
1302 // types
1303 typedef RealType result_type;
1304
1305 class param_type
1306 {
1307 public:
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001308 typedef fisher_f_distribution distribution_type;
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001309
1310 explicit param_type(result_type m = 1, result_type n = 1);
1311
1312 result_type m() const;
1313 result_type n() const;
1314
1315 friend bool operator==(const param_type& x, const param_type& y);
1316 friend bool operator!=(const param_type& x, const param_type& y);
1317 };
1318
1319 // constructor and reset functions
1320 explicit fisher_f_distribution(result_type m = 1, result_type n = 1);
1321 explicit fisher_f_distribution(const param_type& parm);
1322 void reset();
1323
1324 // generating functions
1325 template<class URNG> result_type operator()(URNG& g);
1326 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1327
1328 // property functions
1329 result_type m() const;
1330 result_type n() const;
1331
1332 param_type param() const;
1333 void param(const param_type& parm);
1334
1335 result_type min() const;
1336 result_type max() const;
1337
1338 friend bool operator==(const fisher_f_distribution& x,
1339 const fisher_f_distribution& y);
1340 friend bool operator!=(const fisher_f_distribution& x,
1341 const fisher_f_distribution& y);
1342
1343 template <class charT, class traits>
1344 friend
1345 basic_ostream<charT, traits>&
1346 operator<<(basic_ostream<charT, traits>& os,
1347 const fisher_f_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001348
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001349 template <class charT, class traits>
1350 friend
1351 basic_istream<charT, traits>&
1352 operator>>(basic_istream<charT, traits>& is,
1353 fisher_f_distribution& x);
1354};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355
1356template<class RealType = double>
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001357class student_t_distribution
1358{
1359public:
1360 // types
1361 typedef RealType result_type;
1362
1363 class param_type
1364 {
1365 public:
1366 typedef student_t_distribution distribution_type;
1367
1368 explicit param_type(result_type n = 1);
1369
1370 result_type n() const;
1371
1372 friend bool operator==(const param_type& x, const param_type& y);
1373 friend bool operator!=(const param_type& x, const param_type& y);
1374 };
1375
1376 // constructor and reset functions
1377 explicit student_t_distribution(result_type n = 1);
1378 explicit student_t_distribution(const param_type& parm);
1379 void reset();
1380
1381 // generating functions
1382 template<class URNG> result_type operator()(URNG& g);
1383 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1384
1385 // property functions
1386 result_type n() const;
1387
1388 param_type param() const;
1389 void param(const param_type& parm);
1390
1391 result_type min() const;
1392 result_type max() const;
1393
1394 friend bool operator==(const student_t_distribution& x,
1395 const student_t_distribution& y);
1396 friend bool operator!=(const student_t_distribution& x,
1397 const student_t_distribution& y);
1398
1399 template <class charT, class traits>
1400 friend
1401 basic_ostream<charT, traits>&
1402 operator<<(basic_ostream<charT, traits>& os,
1403 const student_t_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001404
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001405 template <class charT, class traits>
1406 friend
1407 basic_istream<charT, traits>&
1408 operator>>(basic_istream<charT, traits>& is,
1409 student_t_distribution& x);
1410};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411
1412template<class IntType = int>
Howard Hinnant551d8e42010-05-19 01:53:57 +00001413class discrete_distribution
1414{
1415public:
1416 // types
1417 typedef IntType result_type;
1418
1419 class param_type
1420 {
1421 public:
1422 typedef discrete_distribution distribution_type;
1423
1424 param_type();
1425 template<class InputIterator>
1426 param_type(InputIterator firstW, InputIterator lastW);
1427 param_type(initializer_list<double> wl);
1428 template<class UnaryOperation>
1429 param_type(size_t nw, double xmin, double xmax, UnaryOperation fw);
1430
1431 vector<double> probabilities() const;
1432
1433 friend bool operator==(const param_type& x, const param_type& y);
1434 friend bool operator!=(const param_type& x, const param_type& y);
1435 };
1436
1437 // constructor and reset functions
1438 discrete_distribution();
1439 template<class InputIterator>
1440 discrete_distribution(InputIterator firstW, InputIterator lastW);
1441 discrete_distribution(initializer_list<double> wl);
1442 template<class UnaryOperation>
1443 discrete_distribution(size_t nw, double xmin, double xmax,
1444 UnaryOperation fw);
1445 explicit discrete_distribution(const param_type& parm);
1446 void reset();
1447
1448 // generating functions
1449 template<class URNG> result_type operator()(URNG& g);
1450 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1451
1452 // property functions
1453 vector<double> probabilities() const;
1454
1455 param_type param() const;
1456 void param(const param_type& parm);
1457
1458 result_type min() const;
1459 result_type max() const;
1460
1461 friend bool operator==(const discrete_distribution& x,
1462 const discrete_distribution& y);
1463 friend bool operator!=(const discrete_distribution& x,
1464 const discrete_distribution& y);
1465
1466 template <class charT, class traits>
1467 friend
1468 basic_ostream<charT, traits>&
1469 operator<<(basic_ostream<charT, traits>& os,
1470 const discrete_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001471
Howard Hinnant551d8e42010-05-19 01:53:57 +00001472 template <class charT, class traits>
1473 friend
1474 basic_istream<charT, traits>&
1475 operator>>(basic_istream<charT, traits>& is,
1476 discrete_distribution& x);
1477};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478
1479template<class RealType = double>
Howard Hinnantd6d11712010-05-20 15:11:46 +00001480class piecewise_constant_distribution
1481{
1482 // types
1483 typedef RealType result_type;
1484
1485 class param_type
1486 {
1487 public:
1488 typedef piecewise_constant_distribution distribution_type;
1489
1490 param_type();
1491 template<class InputIteratorB, class InputIteratorW>
1492 param_type(InputIteratorB firstB, InputIteratorB lastB,
1493 InputIteratorW firstW);
1494 template<class UnaryOperation>
1495 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1496 template<class UnaryOperation>
1497 param_type(size_t nw, result_type xmin, result_type xmax,
1498 UnaryOperation fw);
1499
1500 vector<result_type> intervals() const;
Howard Hinnant995676a2010-11-18 17:34:48 +00001501 vector<result_type> densities() const;
Howard Hinnantd6d11712010-05-20 15:11:46 +00001502
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;
Howard Hinnant995676a2010-11-18 17:34:48 +00001528 vector<result_type> densities() const;
Howard Hinnantd6d11712010-05-20 15:11:46 +00001529
1530 param_type param() const;
1531 void param(const param_type& parm);
1532
1533 result_type min() const;
1534 result_type max() const;
1535
1536 friend bool operator==(const piecewise_constant_distribution& x,
1537 const piecewise_constant_distribution& y);
1538 friend bool operator!=(const piecewise_constant_distribution& x,
1539 const piecewise_constant_distribution& y);
1540
1541 template <class charT, class traits>
1542 friend
1543 basic_ostream<charT, traits>&
1544 operator<<(basic_ostream<charT, traits>& os,
1545 const piecewise_constant_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001546
Howard Hinnantd6d11712010-05-20 15:11:46 +00001547 template <class charT, class traits>
1548 friend
1549 basic_istream<charT, traits>&
1550 operator>>(basic_istream<charT, traits>& is,
1551 piecewise_constant_distribution& x);
1552};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553
1554template<class RealType = double>
Howard Hinnant54305402010-05-25 00:27:34 +00001555class piecewise_linear_distribution
1556{
1557 // types
1558 typedef RealType result_type;
1559
1560 class param_type
1561 {
1562 public:
1563 typedef piecewise_linear_distribution distribution_type;
1564
1565 param_type();
1566 template<class InputIteratorB, class InputIteratorW>
1567 param_type(InputIteratorB firstB, InputIteratorB lastB,
1568 InputIteratorW firstW);
1569 template<class UnaryOperation>
1570 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1571 template<class UnaryOperation>
1572 param_type(size_t nw, result_type xmin, result_type xmax,
1573 UnaryOperation fw);
1574
1575 vector<result_type> intervals() const;
Howard Hinnant995676a2010-11-18 17:34:48 +00001576 vector<result_type> densities() const;
Howard Hinnant54305402010-05-25 00:27:34 +00001577
1578 friend bool operator==(const param_type& x, const param_type& y);
1579 friend bool operator!=(const param_type& x, const param_type& y);
1580 };
1581
1582 // constructor and reset functions
1583 piecewise_linear_distribution();
1584 template<class InputIteratorB, class InputIteratorW>
1585 piecewise_linear_distribution(InputIteratorB firstB,
1586 InputIteratorB lastB,
1587 InputIteratorW firstW);
Howard Hinnant324bb032010-08-22 00:02:43 +00001588
Howard Hinnant54305402010-05-25 00:27:34 +00001589 template<class UnaryOperation>
1590 piecewise_linear_distribution(initializer_list<result_type> bl,
1591 UnaryOperation fw);
1592
1593 template<class UnaryOperation>
1594 piecewise_linear_distribution(size_t nw, result_type xmin,
1595 result_type xmax, UnaryOperation fw);
1596
1597 explicit piecewise_linear_distribution(const param_type& parm);
1598 void reset();
1599
1600 // generating functions
1601 template<class URNG> result_type operator()(URNG& g);
1602 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1603
1604 // property functions
1605 vector<result_type> intervals() const;
Howard Hinnant995676a2010-11-18 17:34:48 +00001606 vector<result_type> densities() const;
Howard Hinnant54305402010-05-25 00:27:34 +00001607
1608 param_type param() const;
1609 void param(const param_type& parm);
1610
1611 result_type min() const;
1612 result_type max() const;
1613
1614 friend bool operator==(const piecewise_linear_distribution& x,
1615 const piecewise_linear_distribution& y);
1616 friend bool operator!=(const piecewise_linear_distribution& x,
1617 const piecewise_linear_distribution& y);
1618
1619 template <class charT, class traits>
1620 friend
1621 basic_ostream<charT, traits>&
1622 operator<<(basic_ostream<charT, traits>& os,
1623 const piecewise_linear_distribution& x);
Howard Hinnant324bb032010-08-22 00:02:43 +00001624
Howard Hinnant54305402010-05-25 00:27:34 +00001625 template <class charT, class traits>
1626 friend
1627 basic_istream<charT, traits>&
1628 operator>>(basic_istream<charT, traits>& is,
1629 piecewise_linear_distribution& x);
1630};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631
1632} // std
1633*/
1634
1635#include <__config>
1636#include <cstddef>
1637#include <type_traits>
1638#include <initializer_list>
1639#include <cstdint>
1640#include <limits>
1641#include <algorithm>
Howard Hinnant551d8e42010-05-19 01:53:57 +00001642#include <numeric>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643#include <vector>
1644#include <string>
1645#include <istream>
1646#include <ostream>
Howard Hinnant30a840f2010-05-12 17:08:57 +00001647#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648
1649#pragma GCC system_header
1650
1651_LIBCPP_BEGIN_NAMESPACE_STD
1652
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001653// __is_seed_sequence
1654
1655template <class _Sseq, class _Engine>
1656struct __is_seed_sequence
1657{
1658 static const bool value =
1659 !is_convertible<_Sseq, typename _Engine::result_type>::value &&
1660 !is_same<typename remove_cv<_Sseq>::type, _Engine>::value;
1661};
1662
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663// linear_congruential_engine
1664
1665template <unsigned long long __a, unsigned long long __c,
1666 unsigned long long __m, unsigned long long _M,
1667 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
1668struct __lce_ta;
1669
1670// 64
1671
1672template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1673struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1674{
1675 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677 static result_type next(result_type __x)
1678 {
1679 // Schrage's algorithm
1680 const result_type __q = __m / __a;
1681 const result_type __r = __m % __a;
1682 const result_type __t0 = __a * (__x % __q);
1683 const result_type __t1 = __r * (__x / __q);
1684 __x = __t0 + (__t0 < __t1) * __m - __t1;
1685 __x += __c - (__x >= __m - __c) * __m;
1686 return __x;
1687 }
1688};
1689
1690template <unsigned long long __a, unsigned long long __m>
1691struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1692{
1693 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001695 static result_type next(result_type __x)
1696 {
1697 // Schrage's algorithm
1698 const result_type __q = __m / __a;
1699 const result_type __r = __m % __a;
1700 const result_type __t0 = __a * (__x % __q);
1701 const result_type __t1 = __r * (__x / __q);
1702 __x = __t0 + (__t0 < __t1) * __m - __t1;
1703 return __x;
1704 }
1705};
1706
1707template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1708struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1709{
1710 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001712 static result_type next(result_type __x)
1713 {
1714 return (__a * __x + __c) % __m;
1715 }
1716};
1717
1718template <unsigned long long __a, unsigned long long __c>
1719struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1720{
1721 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001723 static result_type next(result_type __x)
1724 {
1725 return __a * __x + __c;
1726 }
1727};
1728
1729// 32
1730
1731template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1732struct __lce_ta<_A, _C, _M, unsigned(~0), true>
1733{
1734 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736 static result_type next(result_type __x)
1737 {
1738 const result_type __a = static_cast<result_type>(_A);
1739 const result_type __c = static_cast<result_type>(_C);
1740 const result_type __m = static_cast<result_type>(_M);
1741 // Schrage's algorithm
1742 const result_type __q = __m / __a;
1743 const result_type __r = __m % __a;
1744 const result_type __t0 = __a * (__x % __q);
1745 const result_type __t1 = __r * (__x / __q);
1746 __x = __t0 + (__t0 < __t1) * __m - __t1;
1747 __x += __c - (__x >= __m - __c) * __m;
1748 return __x;
1749 }
1750};
1751
1752template <unsigned long long _A, unsigned long long _M>
1753struct __lce_ta<_A, 0, _M, unsigned(~0), true>
1754{
1755 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757 static result_type next(result_type __x)
1758 {
1759 const result_type __a = static_cast<result_type>(_A);
1760 const result_type __m = static_cast<result_type>(_M);
1761 // Schrage's algorithm
1762 const result_type __q = __m / __a;
1763 const result_type __r = __m % __a;
1764 const result_type __t0 = __a * (__x % __q);
1765 const result_type __t1 = __r * (__x / __q);
1766 __x = __t0 + (__t0 < __t1) * __m - __t1;
1767 return __x;
1768 }
1769};
1770
1771template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1772struct __lce_ta<_A, _C, _M, unsigned(~0), false>
1773{
1774 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001776 static result_type next(result_type __x)
1777 {
1778 const result_type __a = static_cast<result_type>(_A);
1779 const result_type __c = static_cast<result_type>(_C);
1780 const result_type __m = static_cast<result_type>(_M);
1781 return (__a * __x + __c) % __m;
1782 }
1783};
1784
1785template <unsigned long long _A, unsigned long long _C>
1786struct __lce_ta<_A, _C, 0, unsigned(~0), false>
1787{
1788 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790 static result_type next(result_type __x)
1791 {
1792 const result_type __a = static_cast<result_type>(_A);
1793 const result_type __c = static_cast<result_type>(_C);
1794 return __a * __x + __c;
1795 }
1796};
1797
1798// 16
1799
1800template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1801struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1802{
1803 typedef unsigned short result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 static result_type next(result_type __x)
1806 {
1807 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1808 }
1809};
1810
1811template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1812class linear_congruential_engine;
1813
1814template <class _CharT, class _Traits,
1815 class _U, _U _A, _U _C, _U _N>
1816basic_ostream<_CharT, _Traits>&
1817operator<<(basic_ostream<_CharT, _Traits>& __os,
1818 const linear_congruential_engine<_U, _A, _C, _N>&);
1819
1820template <class _CharT, class _Traits,
1821 class _U, _U _A, _U _C, _U _N>
1822basic_istream<_CharT, _Traits>&
1823operator>>(basic_istream<_CharT, _Traits>& __is,
1824 linear_congruential_engine<_U, _A, _C, _N>& __x);
1825
1826template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001827class _LIBCPP_VISIBLE linear_congruential_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001828{
1829public:
1830 // types
1831 typedef _UIntType result_type;
1832
1833private:
1834 result_type __x_;
1835
1836 static const result_type _M = result_type(~0);
1837
1838 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1839 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1840public:
1841 static const result_type _Min = __c == 0u ? 1u: 0u;
1842 static const result_type _Max = __m - 1u;
1843 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1844
1845 // engine characteristics
1846 static const/*expr*/ result_type multiplier = __a;
1847 static const/*expr*/ result_type increment = __c;
1848 static const/*expr*/ result_type modulus = __m;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850 static const/*expr*/ result_type min() {return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852 static const/*expr*/ result_type max() {return _Max;}
1853 static const/*expr*/ result_type default_seed = 1u;
1854
1855 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857 explicit linear_congruential_engine(result_type __s = default_seed)
1858 {seed(__s);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00001859 template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q,
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001861 typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864 void seed(result_type __s = default_seed)
1865 {seed(integral_constant<bool, __m == 0>(),
1866 integral_constant<bool, __c == 0>(), __s);}
1867 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869 typename enable_if
1870 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001871 __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001872 void
1873 >::type
1874 seed(_Sseq& __q)
1875 {__seed(__q, integral_constant<unsigned,
1876 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1877 : (__m-1) / 0x100000000ull)>());}
1878
1879 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001881 result_type operator()()
1882 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001884 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1885
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001886 friend _LIBCPP_INLINE_VISIBILITY
1887 bool operator==(const linear_congruential_engine& __x,
1888 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889 {return __x.__x_ == __y.__x_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001890 friend _LIBCPP_INLINE_VISIBILITY
1891 bool operator!=(const linear_congruential_engine& __x,
1892 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001893 {return !(__x == __y);}
1894
1895private:
1896
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001902 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1903 1 : __s % __m;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1906
1907 template<class _Sseq>
1908 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1909 template<class _Sseq>
1910 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1911
1912 template <class _CharT, class _Traits,
1913 class _U, _U _A, _U _C, _U _N>
1914 friend
1915 basic_ostream<_CharT, _Traits>&
1916 operator<<(basic_ostream<_CharT, _Traits>& __os,
1917 const linear_congruential_engine<_U, _A, _C, _N>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001918
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001919 template <class _CharT, class _Traits,
1920 class _U, _U _A, _U _C, _U _N>
1921 friend
1922 basic_istream<_CharT, _Traits>&
1923 operator>>(basic_istream<_CharT, _Traits>& __is,
1924 linear_congruential_engine<_U, _A, _C, _N>& __x);
1925};
1926
1927template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1928template<class _Sseq>
1929void
1930linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1931 integral_constant<unsigned, 1>)
1932{
1933 const unsigned __k = 1;
1934 uint32_t __ar[__k+3];
1935 __q.generate(__ar, __ar + __k + 3);
1936 result_type __s = static_cast<result_type>(__ar[3] % __m);
1937 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1938}
1939
1940template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1941template<class _Sseq>
1942void
1943linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1944 integral_constant<unsigned, 2>)
1945{
1946 const unsigned __k = 2;
1947 uint32_t __ar[__k+3];
1948 __q.generate(__ar, __ar + __k + 3);
1949 result_type __s = static_cast<result_type>((__ar[3] +
1950 (uint64_t)__ar[4] << 32) % __m);
1951 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1952}
1953
1954template <class _CharT, class _Traits>
1955class __save_flags
1956{
1957 typedef basic_ios<_CharT, _Traits> __stream_type;
1958 typedef typename __stream_type::fmtflags fmtflags;
1959
1960 __stream_type& __stream_;
1961 fmtflags __fmtflags_;
1962 _CharT __fill_;
1963
1964 __save_flags(const __save_flags&);
1965 __save_flags& operator=(const __save_flags&);
1966public:
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 explicit __save_flags(__stream_type& __stream)
1969 : __stream_(__stream),
1970 __fmtflags_(__stream.flags()),
1971 __fill_(__stream.fill())
1972 {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001974 ~__save_flags()
1975 {
1976 __stream_.flags(__fmtflags_);
1977 __stream_.fill(__fill_);
1978 }
1979};
1980
1981template <class _CharT, class _Traits,
1982 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001983inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001984basic_ostream<_CharT, _Traits>&
1985operator<<(basic_ostream<_CharT, _Traits>& __os,
1986 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1987{
1988 __save_flags<_CharT, _Traits> _(__os);
1989 __os.flags(ios_base::dec | ios_base::left);
1990 __os.fill(__os.widen(' '));
1991 return __os << __x.__x_;
1992}
1993
1994template <class _CharT, class _Traits,
1995 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1996basic_istream<_CharT, _Traits>&
1997operator>>(basic_istream<_CharT, _Traits>& __is,
1998 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1999{
2000 __save_flags<_CharT, _Traits> _(__is);
2001 __is.flags(ios_base::dec | ios_base::skipws);
2002 _UIntType __t;
2003 __is >> __t;
2004 if (!__is.fail())
2005 __x.__x_ = __t;
2006 return __is;
2007}
2008
2009typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2010 minstd_rand0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002011typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2012 minstd_rand;
Howard Hinnantd6d11712010-05-20 15:11:46 +00002013typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002014// mersenne_twister_engine
2015
2016template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2017 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2018 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2019class mersenne_twister_engine;
2020
2021template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2022 _UI _A, size_t _U, _UI _D, size_t _S,
2023 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2024bool
2025operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2026 _B, _T, _C, _L, _F>& __x,
2027 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2028 _B, _T, _C, _L, _F>& __y);
2029
2030template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2031 _UI _A, size_t _U, _UI _D, size_t _S,
2032 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2033bool
2034operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2035 _B, _T, _C, _L, _F>& __x,
2036 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2037 _B, _T, _C, _L, _F>& __y);
2038
2039template <class _CharT, class _Traits,
2040 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2041 _UI _A, size_t _U, _UI _D, size_t _S,
2042 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2043basic_ostream<_CharT, _Traits>&
2044operator<<(basic_ostream<_CharT, _Traits>& __os,
2045 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2046 _B, _T, _C, _L, _F>& __x);
2047
2048template <class _CharT, class _Traits,
2049 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2050 _UI _A, size_t _U, _UI _D, size_t _S,
2051 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2052basic_istream<_CharT, _Traits>&
2053operator>>(basic_istream<_CharT, _Traits>& __is,
2054 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2055 _B, _T, _C, _L, _F>& __x);
2056
2057template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2058 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2059 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002060class _LIBCPP_VISIBLE mersenne_twister_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061{
2062public:
2063 // types
2064 typedef _UIntType result_type;
2065
2066private:
2067 result_type __x_[__n];
2068 size_t __i_;
2069
2070 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2071 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
2072 static const result_type _Dt = numeric_limits<result_type>::digits;
2073 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2074 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2075 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2076 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2077 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2078 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2079 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2080public:
2081 static const result_type _Min = 0;
2082 static const result_type _Max = __w == _Dt ? result_type(~0) :
2083 (result_type(1) << __w) - result_type(1);
2084 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2085 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2086 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2087 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2088 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2089 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2090
2091 // engine characteristics
2092 static const/*expr*/ size_t word_size = __w;
2093 static const/*expr*/ size_t state_size = __n;
2094 static const/*expr*/ size_t shift_size = __m;
2095 static const/*expr*/ size_t mask_bits = __r;
2096 static const/*expr*/ result_type xor_mask = __a;
2097 static const/*expr*/ size_t tempering_u = __u;
2098 static const/*expr*/ result_type tempering_d = __d;
2099 static const/*expr*/ size_t tempering_s = __s;
2100 static const/*expr*/ result_type tempering_b = __b;
2101 static const/*expr*/ size_t tempering_t = __t;
2102 static const/*expr*/ result_type tempering_c = __c;
2103 static const/*expr*/ size_t tempering_l = __l;
2104 static const/*expr*/ result_type initialization_multiplier = __f;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106 static const/*expr*/ result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002108 static const/*expr*/ result_type max() { return _Max; }
2109 static const/*expr*/ result_type default_seed = 5489u;
2110
2111 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113 explicit mersenne_twister_engine(result_type __sd = default_seed)
2114 {seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002115 template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q,
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002117 typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 {seed(__q);}
2119 void seed(result_type __sd = default_seed);
2120 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122 typename enable_if
2123 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002124 __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125 void
2126 >::type
2127 seed(_Sseq& __q)
2128 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2129
2130 // generating functions
2131 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002133 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2134
2135 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2136 _UI _A, size_t _U, _UI _D, size_t _S,
2137 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2138 friend
2139 bool
2140 operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2141 _B, _T, _C, _L, _F>& __x,
2142 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2143 _B, _T, _C, _L, _F>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002144
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002145 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2146 _UI _A, size_t _U, _UI _D, size_t _S,
2147 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2148 friend
2149 bool
2150 operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2151 _B, _T, _C, _L, _F>& __x,
2152 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2153 _B, _T, _C, _L, _F>& __y);
2154
2155 template <class _CharT, class _Traits,
2156 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2157 _UI _A, size_t _U, _UI _D, size_t _S,
2158 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2159 friend
2160 basic_ostream<_CharT, _Traits>&
2161 operator<<(basic_ostream<_CharT, _Traits>& __os,
2162 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2163 _B, _T, _C, _L, _F>& __x);
2164
2165 template <class _CharT, class _Traits,
2166 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2167 _UI _A, size_t _U, _UI _D, size_t _S,
2168 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2169 friend
2170 basic_istream<_CharT, _Traits>&
2171 operator>>(basic_istream<_CharT, _Traits>& __is,
2172 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2173 _B, _T, _C, _L, _F>& __x);
2174private:
2175
2176 template<class _Sseq>
2177 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2178 template<class _Sseq>
2179 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2180
2181 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002183 static
2184 typename enable_if
2185 <
2186 __count < __w,
2187 result_type
2188 >::type
2189 __lshift(result_type __x) {return (__x << __count) & _Max;}
2190
2191 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193 static
2194 typename enable_if
2195 <
2196 (__count >= __w),
2197 result_type
2198 >::type
2199 __lshift(result_type __x) {return result_type(0);}
2200
2201 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002203 static
2204 typename enable_if
2205 <
2206 __count < _Dt,
2207 result_type
2208 >::type
2209 __rshift(result_type __x) {return __x >> __count;}
2210
2211 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002213 static
2214 typename enable_if
2215 <
2216 (__count >= _Dt),
2217 result_type
2218 >::type
2219 __rshift(result_type __x) {return result_type(0);}
2220};
2221
2222template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2223 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2224 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2225void
2226mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2227 __t, __c, __l, __f>::seed(result_type __sd)
2228{ // __w >= 2
2229 __x_[0] = __sd & _Max;
2230 for (size_t __i = 1; __i < __n; ++__i)
2231 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2232 __i_ = 0;
2233}
2234
2235template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2236 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2237 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2238template<class _Sseq>
2239void
2240mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2241 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2242{
2243 const unsigned __k = 1;
2244 uint32_t __ar[__n * __k];
2245 __q.generate(__ar, __ar + __n * __k);
2246 for (size_t __i = 0; __i < __n; ++__i)
2247 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2248 const result_type __mask = __r == _Dt ? result_type(~0) :
2249 (result_type(1) << __r) - result_type(1);
2250 __i_ = 0;
2251 if ((__x_[0] & ~__mask) == 0)
2252 {
2253 for (size_t __i = 1; __i < __n; ++__i)
2254 if (__x_[__i] != 0)
2255 return;
2256 __x_[0] = _Max;
2257 }
2258}
2259
2260template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2261 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2262 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2263template<class _Sseq>
2264void
2265mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2266 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2267{
2268 const unsigned __k = 2;
2269 uint32_t __ar[__n * __k];
2270 __q.generate(__ar, __ar + __n * __k);
2271 for (size_t __i = 0; __i < __n; ++__i)
2272 __x_[__i] = static_cast<result_type>(
2273 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2274 const result_type __mask = __r == _Dt ? result_type(~0) :
2275 (result_type(1) << __r) - result_type(1);
2276 __i_ = 0;
2277 if ((__x_[0] & ~__mask) == 0)
2278 {
2279 for (size_t __i = 1; __i < __n; ++__i)
2280 if (__x_[__i] != 0)
2281 return;
2282 __x_[0] = _Max;
2283 }
2284}
2285
2286template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2287 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2288 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2289_UIntType
2290mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2291 __t, __c, __l, __f>::operator()()
2292{
2293 const size_t __j = (__i_ + 1) % __n;
2294 const result_type __mask = __r == _Dt ? result_type(~0) :
2295 (result_type(1) << __r) - result_type(1);
2296 const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
2297 const size_t __k = (__i_ + __m) % __n;
2298 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
2299 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2300 __i_ = __j;
2301 __z ^= __lshift<__s>(__z) & __b;
2302 __z ^= __lshift<__t>(__z) & __c;
2303 return __z ^ __rshift<__l>(__z);
2304}
2305
2306template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2307 _UI _A, size_t _U, _UI _D, size_t _S,
2308 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
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 if (__x.__i_ == __y.__i_)
2316 return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
2317 if (__x.__i_ == 0 || __y.__i_ == 0)
2318 {
2319 size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
2320 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2321 __y.__x_ + __y.__i_))
2322 return false;
2323 if (__x.__i_ == 0)
2324 return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
2325 return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
2326 }
2327 if (__x.__i_ < __y.__i_)
2328 {
2329 size_t __j = _N - __y.__i_;
2330 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2331 __y.__x_ + __y.__i_))
2332 return false;
2333 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
2334 __y.__x_))
2335 return false;
2336 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
2337 __y.__x_ + (_N - (__x.__i_ + __j)));
2338 }
2339 size_t __j = _N - __x.__i_;
2340 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2341 __x.__x_ + __x.__i_))
2342 return false;
2343 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
2344 __x.__x_))
2345 return false;
2346 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
2347 __x.__x_ + (_N - (__y.__i_ + __j)));
2348}
2349
2350template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2351 _UI _A, size_t _U, _UI _D, size_t _S,
2352 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002353inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354bool
2355operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2356 _B, _T, _C, _L, _F>& __x,
2357 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2358 _B, _T, _C, _L, _F>& __y)
2359{
2360 return !(__x == __y);
2361}
2362
2363template <class _CharT, class _Traits,
2364 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2365 _UI _A, size_t _U, _UI _D, size_t _S,
2366 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2367basic_ostream<_CharT, _Traits>&
2368operator<<(basic_ostream<_CharT, _Traits>& __os,
2369 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2370 _B, _T, _C, _L, _F>& __x)
2371{
2372 __save_flags<_CharT, _Traits> _(__os);
2373 __os.flags(ios_base::dec | ios_base::left);
2374 _CharT __sp = __os.widen(' ');
2375 __os.fill(__sp);
2376 __os << __x.__x_[__x.__i_];
2377 for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
2378 __os << __sp << __x.__x_[__j];
2379 for (size_t __j = 0; __j < __x.__i_; ++__j)
2380 __os << __sp << __x.__x_[__j];
2381 return __os;
2382}
2383
2384template <class _CharT, class _Traits,
2385 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2386 _UI _A, size_t _U, _UI _D, size_t _S,
2387 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2388basic_istream<_CharT, _Traits>&
2389operator>>(basic_istream<_CharT, _Traits>& __is,
2390 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2391 _B, _T, _C, _L, _F>& __x)
2392{
2393 __save_flags<_CharT, _Traits> _(__is);
2394 __is.flags(ios_base::dec | ios_base::skipws);
2395 _UI __t[_N];
2396 for (size_t __i = 0; __i < _N; ++__i)
2397 __is >> __t[__i];
2398 if (!__is.fail())
2399 {
2400 for (size_t __i = 0; __i < _N; ++__i)
2401 __x.__x_[__i] = __t[__i];
2402 __x.__i_ = 0;
2403 }
2404 return __is;
2405}
2406
2407typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2408 0x9908b0df, 11, 0xffffffff,
2409 7, 0x9d2c5680,
2410 15, 0xefc60000,
2411 18, 1812433253> mt19937;
2412typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2413 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2414 17, 0x71d67fffeda60000ULL,
2415 37, 0xfff7eee000000000ULL,
2416 43, 6364136223846793005ULL> mt19937_64;
2417
2418// subtract_with_carry_engine
2419
2420template<class _UIntType, size_t __w, size_t __s, size_t __r>
2421class subtract_with_carry_engine;
2422
2423template<class _UI, size_t _W, size_t _S, size_t _R>
2424bool
2425operator==(
2426 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2427 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2428
2429template<class _UI, size_t _W, size_t _S, size_t _R>
2430bool
2431operator!=(
2432 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2433 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2434
2435template <class _CharT, class _Traits,
2436 class _UI, size_t _W, size_t _S, size_t _R>
2437basic_ostream<_CharT, _Traits>&
2438operator<<(basic_ostream<_CharT, _Traits>& __os,
2439 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2440
2441template <class _CharT, class _Traits,
2442 class _UI, size_t _W, size_t _S, size_t _R>
2443basic_istream<_CharT, _Traits>&
2444operator>>(basic_istream<_CharT, _Traits>& __is,
2445 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2446
2447template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002448class _LIBCPP_VISIBLE subtract_with_carry_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449{
2450public:
2451 // types
2452 typedef _UIntType result_type;
2453
2454private:
2455 result_type __x_[__r];
2456 result_type __c_;
2457 size_t __i_;
2458
2459 static const result_type _Dt = numeric_limits<result_type>::digits;
2460 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2461 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2462 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2463 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2464public:
2465 static const result_type _Min = 0;
2466 static const result_type _Max = __w == _Dt ? result_type(~0) :
2467 (result_type(1) << __w) - result_type(1);
2468 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2469
2470 // engine characteristics
2471 static const/*expr*/ size_t word_size = __w;
2472 static const/*expr*/ size_t short_lag = __s;
2473 static const/*expr*/ size_t long_lag = __r;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002475 static const/*expr*/ result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002477 static const/*expr*/ result_type max() { return _Max; }
2478 static const/*expr*/ result_type default_seed = 19780503u;
2479
2480 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002482 explicit subtract_with_carry_engine(result_type __sd = default_seed)
2483 {seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002484 template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q,
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002486 typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002487 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002489 void seed(result_type __sd = default_seed)
2490 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2491 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002493 typename enable_if
2494 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002495 __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002496 void
2497 >::type
2498 seed(_Sseq& __q)
2499 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2500
2501 // generating functions
2502 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002503 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002504 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2505
2506 template<class _UI, size_t _W, size_t _S, size_t _R>
2507 friend
2508 bool
2509 operator==(
2510 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2511 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002512
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002513 template<class _UI, size_t _W, size_t _S, size_t _R>
2514 friend
2515 bool
2516 operator!=(
2517 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2518 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002519
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520 template <class _CharT, class _Traits,
2521 class _UI, size_t _W, size_t _S, size_t _R>
2522 friend
2523 basic_ostream<_CharT, _Traits>&
2524 operator<<(basic_ostream<_CharT, _Traits>& __os,
2525 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002526
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527 template <class _CharT, class _Traits,
2528 class _UI, size_t _W, size_t _S, size_t _R>
2529 friend
2530 basic_istream<_CharT, _Traits>&
2531 operator>>(basic_istream<_CharT, _Traits>& __is,
2532 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2533
2534private:
2535
2536 void seed(result_type __sd, integral_constant<unsigned, 1>);
2537 void seed(result_type __sd, integral_constant<unsigned, 2>);
2538 template<class _Sseq>
2539 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2540 template<class _Sseq>
2541 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2542};
2543
2544template<class _UIntType, size_t __w, size_t __s, size_t __r>
2545void
2546subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2547 integral_constant<unsigned, 1>)
2548{
2549 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2550 __e(__sd == 0u ? default_seed : __sd);
2551 for (size_t __i = 0; __i < __r; ++__i)
2552 __x_[__i] = static_cast<result_type>(__e() & _Max);
2553 __c_ = __x_[__r-1] == 0;
2554 __i_ = 0;
2555}
2556
2557template<class _UIntType, size_t __w, size_t __s, size_t __r>
2558void
2559subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2560 integral_constant<unsigned, 2>)
2561{
2562 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2563 __e(__sd == 0u ? default_seed : __sd);
2564 for (size_t __i = 0; __i < __r; ++__i)
2565 __x_[__i] = static_cast<result_type>(
2566 (__e() + ((uint64_t)__e() << 32)) & _Max);
2567 __c_ = __x_[__r-1] == 0;
2568 __i_ = 0;
2569}
2570
2571template<class _UIntType, size_t __w, size_t __s, size_t __r>
2572template<class _Sseq>
2573void
2574subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2575 integral_constant<unsigned, 1>)
2576{
2577 const unsigned __k = 1;
2578 uint32_t __ar[__r * __k];
2579 __q.generate(__ar, __ar + __r * __k);
2580 for (size_t __i = 0; __i < __r; ++__i)
2581 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2582 __c_ = __x_[__r-1] == 0;
2583 __i_ = 0;
2584}
2585
2586template<class _UIntType, size_t __w, size_t __s, size_t __r>
2587template<class _Sseq>
2588void
2589subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2590 integral_constant<unsigned, 2>)
2591{
2592 const unsigned __k = 2;
2593 uint32_t __ar[__r * __k];
2594 __q.generate(__ar, __ar + __r * __k);
2595 for (size_t __i = 0; __i < __r; ++__i)
2596 __x_[__i] = static_cast<result_type>(
2597 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2598 __c_ = __x_[__r-1] == 0;
2599 __i_ = 0;
2600}
2601
2602template<class _UIntType, size_t __w, size_t __s, size_t __r>
2603_UIntType
2604subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2605{
2606 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2607 result_type& __xr = __x_[__i_];
2608 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2609 __xr = (__xs - __xr - __c_) & _Max;
2610 __c_ = __new_c;
2611 __i_ = (__i_ + 1) % __r;
2612 return __xr;
2613}
2614
2615template<class _UI, size_t _W, size_t _S, size_t _R>
2616bool
2617operator==(
2618 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2619 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2620{
2621 if (__x.__c_ != __y.__c_)
2622 return false;
2623 if (__x.__i_ == __y.__i_)
2624 return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
2625 if (__x.__i_ == 0 || __y.__i_ == 0)
2626 {
2627 size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
2628 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2629 __y.__x_ + __y.__i_))
2630 return false;
2631 if (__x.__i_ == 0)
2632 return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
2633 return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
2634 }
2635 if (__x.__i_ < __y.__i_)
2636 {
2637 size_t __j = _R - __y.__i_;
2638 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2639 __y.__x_ + __y.__i_))
2640 return false;
2641 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
2642 __y.__x_))
2643 return false;
2644 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
2645 __y.__x_ + (_R - (__x.__i_ + __j)));
2646 }
2647 size_t __j = _R - __x.__i_;
2648 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2649 __x.__x_ + __x.__i_))
2650 return false;
2651 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
2652 __x.__x_))
2653 return false;
2654 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
2655 __x.__x_ + (_R - (__y.__i_ + __j)));
2656}
2657
2658template<class _UI, size_t _W, size_t _S, size_t _R>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002659inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660bool
2661operator!=(
2662 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2663 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2664{
2665 return !(__x == __y);
2666}
2667
2668template <class _CharT, class _Traits,
2669 class _UI, size_t _W, size_t _S, size_t _R>
2670basic_ostream<_CharT, _Traits>&
2671operator<<(basic_ostream<_CharT, _Traits>& __os,
2672 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2673{
2674 __save_flags<_CharT, _Traits> _(__os);
2675 __os.flags(ios_base::dec | ios_base::left);
2676 _CharT __sp = __os.widen(' ');
2677 __os.fill(__sp);
2678 __os << __x.__x_[__x.__i_];
2679 for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
2680 __os << __sp << __x.__x_[__j];
2681 for (size_t __j = 0; __j < __x.__i_; ++__j)
2682 __os << __sp << __x.__x_[__j];
2683 __os << __sp << __x.__c_;
2684 return __os;
2685}
2686
2687template <class _CharT, class _Traits,
2688 class _UI, size_t _W, size_t _S, size_t _R>
2689basic_istream<_CharT, _Traits>&
2690operator>>(basic_istream<_CharT, _Traits>& __is,
2691 subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2692{
2693 __save_flags<_CharT, _Traits> _(__is);
2694 __is.flags(ios_base::dec | ios_base::skipws);
2695 _UI __t[_R+1];
2696 for (size_t __i = 0; __i < _R+1; ++__i)
2697 __is >> __t[__i];
2698 if (!__is.fail())
2699 {
2700 for (size_t __i = 0; __i < _R; ++__i)
2701 __x.__x_[__i] = __t[__i];
2702 __x.__c_ = __t[_R];
2703 __x.__i_ = 0;
2704 }
2705 return __is;
2706}
2707
2708typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2709typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2710
2711// discard_block_engine
2712
2713template<class _Engine, size_t __p, size_t __r>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002714class _LIBCPP_VISIBLE discard_block_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715{
2716 _Engine __e_;
2717 int __n_;
2718
2719 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2720 static_assert(__r <= __p, "discard_block_engine invalid parameters");
2721public:
2722 // types
2723 typedef typename _Engine::result_type result_type;
2724
2725 // engine characteristics
2726 static const/*expr*/ size_t block_size = __p;
2727 static const/*expr*/ size_t used_block = __r;
2728
2729 // Temporary work around for lack of constexpr
2730 static const result_type _Min = _Engine::_Min;
2731 static const result_type _Max = _Engine::_Max;
2732
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002734 static const/*expr*/ result_type min() { return _Engine::min(); }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736 static const/*expr*/ result_type max() { return _Engine::max(); }
2737
2738 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740 discard_block_engine() : __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002742 explicit discard_block_engine(const _Engine& __e)
2743 : __e_(__e), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002744#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002746 explicit discard_block_engine(_Engine&& __e)
2747 : __e_(_STD::move(__e)), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002748#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002750 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002751 template<class _Sseq>
2752 _LIBCPP_INLINE_VISIBILITY
2753 explicit discard_block_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002754 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00002755 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002756 : __e_(__q), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002758 void seed() {__e_.seed(); __n_ = 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002760 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002761 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002763 typename enable_if
2764 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002765 __is_seed_sequence<_Sseq, discard_block_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00002766 void
2767 >::type
2768 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002769
2770 // generating functions
2771 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002773 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2774
2775 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002777 const _Engine& base() const {return __e_;}
2778
2779 template<class _Eng, size_t _P, size_t _R>
2780 friend
2781 bool
2782 operator==(
2783 const discard_block_engine<_Eng, _P, _R>& __x,
2784 const discard_block_engine<_Eng, _P, _R>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002785
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002786 template<class _Eng, size_t _P, size_t _R>
2787 friend
2788 bool
2789 operator!=(
2790 const discard_block_engine<_Eng, _P, _R>& __x,
2791 const discard_block_engine<_Eng, _P, _R>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002792
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793 template <class _CharT, class _Traits,
2794 class _Eng, size_t _P, size_t _R>
2795 friend
2796 basic_ostream<_CharT, _Traits>&
2797 operator<<(basic_ostream<_CharT, _Traits>& __os,
2798 const discard_block_engine<_Eng, _P, _R>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002799
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002800 template <class _CharT, class _Traits,
2801 class _Eng, size_t _P, size_t _R>
2802 friend
2803 basic_istream<_CharT, _Traits>&
2804 operator>>(basic_istream<_CharT, _Traits>& __is,
2805 discard_block_engine<_Eng, _P, _R>& __x);
2806};
2807
2808template<class _Engine, size_t __p, size_t __r>
2809typename discard_block_engine<_Engine, __p, __r>::result_type
2810discard_block_engine<_Engine, __p, __r>::operator()()
2811{
2812 if (__n_ >= __r)
2813 {
2814 __e_.discard(__p - __r);
2815 __n_ = 0;
2816 }
2817 ++__n_;
2818 return __e_();
2819}
2820
2821template<class _Eng, size_t _P, size_t _R>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002822inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823bool
2824operator==(const discard_block_engine<_Eng, _P, _R>& __x,
2825 const discard_block_engine<_Eng, _P, _R>& __y)
2826{
2827 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2828}
2829
2830template<class _Eng, size_t _P, size_t _R>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002831inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002832bool
2833operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
2834 const discard_block_engine<_Eng, _P, _R>& __y)
2835{
2836 return !(__x == __y);
2837}
2838
2839template <class _CharT, class _Traits,
2840 class _Eng, size_t _P, size_t _R>
2841basic_ostream<_CharT, _Traits>&
2842operator<<(basic_ostream<_CharT, _Traits>& __os,
2843 const discard_block_engine<_Eng, _P, _R>& __x)
2844{
2845 __save_flags<_CharT, _Traits> _(__os);
2846 __os.flags(ios_base::dec | ios_base::left);
2847 _CharT __sp = __os.widen(' ');
2848 __os.fill(__sp);
2849 return __os << __x.__e_ << __sp << __x.__n_;
2850}
2851
2852template <class _CharT, class _Traits,
2853 class _Eng, size_t _P, size_t _R>
2854basic_istream<_CharT, _Traits>&
2855operator>>(basic_istream<_CharT, _Traits>& __is,
2856 discard_block_engine<_Eng, _P, _R>& __x)
2857{
2858 __save_flags<_CharT, _Traits> _(__is);
2859 __is.flags(ios_base::dec | ios_base::skipws);
2860 _Eng __e;
2861 int __n;
2862 __is >> __e >> __n;
2863 if (!__is.fail())
2864 {
2865 __x.__e_ = __e;
2866 __x.__n_ = __n;
2867 }
2868 return __is;
2869}
2870
2871typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2872typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2873
2874// independent_bits_engine
2875
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002876template<class _Engine, size_t __w, class _UIntType>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002877class _LIBCPP_VISIBLE independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002878{
2879 template <class _UI, _UI _R0, size_t _W, size_t _M>
2880 class __get_n
2881 {
2882 static const size_t _Dt = numeric_limits<_UI>::digits;
2883 static const size_t _N = _W / _M + (_W % _M != 0);
2884 static const size_t _W0 = _W / _N;
2885 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
2886 public:
2887 static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
2888 };
2889public:
2890 // types
2891 typedef _UIntType result_type;
2892
2893private:
2894 _Engine __e_;
2895
2896 static const result_type _Dt = numeric_limits<result_type>::digits;
2897 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2898 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2899
2900 typedef typename _Engine::result_type _Engine_result_type;
2901 typedef typename conditional
2902 <
2903 sizeof(_Engine_result_type) <= sizeof(result_type),
2904 result_type,
2905 _Engine_result_type
2906 >::type _Working_result_type;
2907 // Temporary work around for lack of constexpr
2908 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2909 + _Working_result_type(1);
2910 static const size_t __m = __log2<_Working_result_type, _R>::value;
2911 static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
2912 static const size_t __w0 = __w / __n;
2913 static const size_t __n0 = __n - __w % __n;
2914 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2915 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2916 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2917 (_R >> __w0) << __w0;
2918 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2919 (_R >> (__w0+1)) << (__w0+1);
2920 static const _Engine_result_type __mask0 = __w0 > 0 ?
2921 _Engine_result_type(~0) >> (_EDt - __w0) :
2922 _Engine_result_type(0);
2923 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2924 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2925 _Engine_result_type(~0);
2926public:
2927 static const result_type _Min = 0;
2928 static const result_type _Max = __w == _Dt ? result_type(~0) :
2929 (result_type(1) << __w) - result_type(1);
2930 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2931
2932 // engine characteristics
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002934 static const/*expr*/ result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002936 static const/*expr*/ result_type max() { return _Max; }
2937
2938 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002940 independent_bits_engine() {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002942 explicit independent_bits_engine(const _Engine& __e)
2943 : __e_(__e) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002944#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002946 explicit independent_bits_engine(_Engine&& __e)
2947 : __e_(_STD::move(__e)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002948#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002950 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002951 template<class _Sseq> explicit independent_bits_engine(_Sseq& __q,
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002952 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002953 typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00002954 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002955 : __e_(__q) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002957 void seed() {__e_.seed();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002959 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002960 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002962 typename enable_if
2963 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002964 __is_seed_sequence<_Sseq, independent_bits_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00002965 void
2966 >::type
2967 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002968
2969 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002971 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002973 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2974
2975 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002977 const _Engine& base() const {return __e_;}
2978
2979 template<class _Eng, size_t _W, class _UI>
2980 friend
2981 bool
2982 operator==(
2983 const independent_bits_engine<_Eng, _W, _UI>& __x,
2984 const independent_bits_engine<_Eng, _W, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002985
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002986 template<class _Eng, size_t _W, class _UI>
2987 friend
2988 bool
2989 operator!=(
2990 const independent_bits_engine<_Eng, _W, _UI>& __x,
2991 const independent_bits_engine<_Eng, _W, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002992
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002993 template <class _CharT, class _Traits,
2994 class _Eng, size_t _W, class _UI>
2995 friend
2996 basic_ostream<_CharT, _Traits>&
2997 operator<<(basic_ostream<_CharT, _Traits>& __os,
2998 const independent_bits_engine<_Eng, _W, _UI>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002999
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003000 template <class _CharT, class _Traits,
3001 class _Eng, size_t _W, class _UI>
3002 friend
3003 basic_istream<_CharT, _Traits>&
3004 operator>>(basic_istream<_CharT, _Traits>& __is,
3005 independent_bits_engine<_Eng, _W, _UI>& __x);
3006
3007private:
3008 result_type __eval(false_type);
3009 result_type __eval(true_type);
3010
3011 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003012 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003013 static
3014 typename enable_if
3015 <
3016 __count < _Dt,
3017 result_type
3018 >::type
3019 __lshift(result_type __x) {return __x << __count;}
3020
3021 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003023 static
3024 typename enable_if
3025 <
3026 (__count >= _Dt),
3027 result_type
3028 >::type
3029 __lshift(result_type __x) {return result_type(0);}
3030};
3031
3032template<class _Engine, size_t __w, class _UIntType>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003033inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003034_UIntType
3035independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
3036{
3037 return static_cast<result_type>(__e_() & __mask0);
3038}
3039
3040template<class _Engine, size_t __w, class _UIntType>
3041_UIntType
3042independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
3043{
3044 result_type _S = 0;
3045 for (size_t __k = 0; __k < __n0; ++__k)
3046 {
3047 _Engine_result_type __u;
3048 do
3049 {
3050 __u = __e_() - _Engine::min();
3051 } while (__u >= __y0);
3052 _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
3053 }
3054 for (size_t __k = __n0; __k < __n; ++__k)
3055 {
3056 _Engine_result_type __u;
3057 do
3058 {
3059 __u = __e_() - _Engine::min();
3060 } while (__u >= __y1);
3061 _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
3062 }
3063 return _S;
3064}
3065
3066template<class _Eng, size_t _W, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003067inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003068bool
3069operator==(
3070 const independent_bits_engine<_Eng, _W, _UI>& __x,
3071 const independent_bits_engine<_Eng, _W, _UI>& __y)
3072{
3073 return __x.base() == __y.base();
3074}
3075
3076template<class _Eng, size_t _W, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003077inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003078bool
3079operator!=(
3080 const independent_bits_engine<_Eng, _W, _UI>& __x,
3081 const independent_bits_engine<_Eng, _W, _UI>& __y)
3082{
3083 return !(__x == __y);
3084}
3085
3086template <class _CharT, class _Traits,
3087 class _Eng, size_t _W, class _UI>
3088basic_ostream<_CharT, _Traits>&
3089operator<<(basic_ostream<_CharT, _Traits>& __os,
3090 const independent_bits_engine<_Eng, _W, _UI>& __x)
3091{
3092 return __os << __x.base();
3093}
3094
3095template <class _CharT, class _Traits,
3096 class _Eng, size_t _W, class _UI>
3097basic_istream<_CharT, _Traits>&
3098operator>>(basic_istream<_CharT, _Traits>& __is,
3099 independent_bits_engine<_Eng, _W, _UI>& __x)
3100{
3101 _Eng __e;
3102 __is >> __e;
3103 if (!__is.fail())
3104 __x.__e_ = __e;
3105 return __is;
3106}
3107
3108// shuffle_order_engine
3109
3110template <uint64_t _Xp, uint64_t _Yp>
3111struct __ugcd
3112{
3113 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
3114};
3115
3116template <uint64_t _Xp>
3117struct __ugcd<_Xp, 0>
3118{
3119 static const uint64_t value = _Xp;
3120};
3121
3122template <uint64_t _N, uint64_t _D>
3123class __uratio
3124{
3125 static_assert(_D != 0, "__uratio divide by 0");
3126 static const uint64_t __gcd = __ugcd<_N, _D>::value;
3127public:
3128 static const uint64_t num = _N / __gcd;
3129 static const uint64_t den = _D / __gcd;
3130
3131 typedef __uratio<num, den> type;
3132};
3133
3134template<class _Engine, size_t __k>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003135class _LIBCPP_VISIBLE shuffle_order_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003136{
3137 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3138public:
3139 // types
3140 typedef typename _Engine::result_type result_type;
3141
3142private:
3143 _Engine __e_;
3144 result_type _V_[__k];
3145 result_type _Y_;
3146
3147public:
3148 // engine characteristics
3149 static const/*expr*/ size_t table_size = __k;
Howard Hinnant324bb032010-08-22 00:02:43 +00003150
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003151 static const result_type _Min = _Engine::_Min;
3152 static const result_type _Max = _Engine::_Max;
3153 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003155 static const/*expr*/ result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003157 static const/*expr*/ result_type max() { return _Max; }
3158
3159 static const unsigned long long _R = _Max - _Min + 1ull;
3160
3161 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003163 shuffle_order_engine() {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003165 explicit shuffle_order_engine(const _Engine& __e)
3166 : __e_(__e) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003167#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003169 explicit shuffle_order_engine(_Engine&& __e)
3170 : __e_(_STD::move(__e)) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003171#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003173 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003174 template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q,
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003176 typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00003177 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003178 : __e_(__q) {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003180 void seed() {__e_.seed(); __init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003182 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003183 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003185 typename enable_if
3186 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003187 __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00003188 void
3189 >::type
3190 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003191
3192 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003194 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003195 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003196 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3197
3198 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003200 const _Engine& base() const {return __e_;}
3201
3202private:
3203 template<class _Eng, size_t _K>
3204 friend
3205 bool
3206 operator==(
3207 const shuffle_order_engine<_Eng, _K>& __x,
3208 const shuffle_order_engine<_Eng, _K>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003209
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003210 template<class _Eng, size_t _K>
3211 friend
3212 bool
3213 operator!=(
3214 const shuffle_order_engine<_Eng, _K>& __x,
3215 const shuffle_order_engine<_Eng, _K>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003216
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003217 template <class _CharT, class _Traits,
3218 class _Eng, size_t _K>
3219 friend
3220 basic_ostream<_CharT, _Traits>&
3221 operator<<(basic_ostream<_CharT, _Traits>& __os,
3222 const shuffle_order_engine<_Eng, _K>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003223
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003224 template <class _CharT, class _Traits,
3225 class _Eng, size_t _K>
3226 friend
3227 basic_istream<_CharT, _Traits>&
3228 operator>>(basic_istream<_CharT, _Traits>& __is,
3229 shuffle_order_engine<_Eng, _K>& __x);
3230
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003232 void __init()
3233 {
3234 for (size_t __i = 0; __i < __k; ++__i)
3235 _V_[__i] = __e_();
3236 _Y_ = __e_();
3237 }
3238
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003240 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003242 result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
3243
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003245 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003247 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3248
3249 template <uint64_t _N, uint64_t _D>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003251 typename enable_if
3252 <
3253 (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
3254 result_type
3255 >::type
3256 __eval(__uratio<_N, _D>)
3257 {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
3258
3259 template <uint64_t _N, uint64_t _D>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003261 typename enable_if
3262 <
3263 __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
3264 result_type
3265 >::type
3266 __eval(__uratio<_N, _D>)
3267 {
3268 const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
3269 / __uratio<_N, _D>::den);
3270 _Y_ = _V_[__j];
3271 _V_[__j] = __e_();
3272 return _Y_;
3273 }
3274
3275 template <uint64_t __n, uint64_t __d>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003277 result_type __evalf()
3278 {
3279 const double _F = __d == 0 ?
3280 __n / (2. * 0x8000000000000000ull) :
3281 __n / (double)__d;
3282 const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
3283 _Y_ = _V_[__j];
3284 _V_[__j] = __e_();
3285 return _Y_;
3286 }
3287};
3288
3289template<class _Eng, size_t _K>
3290bool
3291operator==(
3292 const shuffle_order_engine<_Eng, _K>& __x,
3293 const shuffle_order_engine<_Eng, _K>& __y)
3294{
3295 return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
3296 __x.__e_ == __y.__e_;
3297}
3298
3299template<class _Eng, size_t _K>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003300inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003301bool
3302operator!=(
3303 const shuffle_order_engine<_Eng, _K>& __x,
3304 const shuffle_order_engine<_Eng, _K>& __y)
3305{
3306 return !(__x == __y);
3307}
3308
3309template <class _CharT, class _Traits,
3310 class _Eng, size_t _K>
3311basic_ostream<_CharT, _Traits>&
3312operator<<(basic_ostream<_CharT, _Traits>& __os,
3313 const shuffle_order_engine<_Eng, _K>& __x)
3314{
3315 __save_flags<_CharT, _Traits> _(__os);
3316 __os.flags(ios_base::dec | ios_base::left);
3317 _CharT __sp = __os.widen(' ');
3318 __os.fill(__sp);
3319 __os << __x.__e_ << __sp << __x._V_[0];
3320 for (size_t __i = 1; __i < _K; ++__i)
3321 __os << __sp << __x._V_[__i];
3322 return __os << __sp << __x._Y_;
3323}
3324
3325template <class _CharT, class _Traits,
3326 class _Eng, size_t _K>
3327basic_istream<_CharT, _Traits>&
3328operator>>(basic_istream<_CharT, _Traits>& __is,
3329 shuffle_order_engine<_Eng, _K>& __x)
3330{
3331 typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
3332 __save_flags<_CharT, _Traits> _(__is);
3333 __is.flags(ios_base::dec | ios_base::skipws);
3334 _Eng __e;
3335 result_type _V[_K+1];
3336 __is >> __e;
3337 for (size_t __i = 0; __i < _K+1; ++__i)
3338 __is >> _V[__i];
3339 if (!__is.fail())
3340 {
3341 __x.__e_ = __e;
3342 for (size_t __i = 0; __i < _K; ++__i)
3343 __x._V_[__i] = _V[__i];
3344 __x._Y_ = _V[_K];
3345 }
3346 return __is;
3347}
3348
3349typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3350
3351// random_device
3352
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003353class _LIBCPP_VISIBLE random_device
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003354{
3355 int __f_;
3356public:
3357 // types
3358 typedef unsigned result_type;
3359
3360 // generator characteristics
3361 static const result_type _Min = 0;
3362 static const result_type _Max = 0xFFFFFFFFu;
3363
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003365 static const/*expr*/ result_type min() { return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003367 static const/*expr*/ result_type max() { return _Max;}
3368
3369 // constructors
3370 explicit random_device(const string& __token = "/dev/urandom");
3371 ~random_device();
3372
3373 // generating functions
3374 result_type operator()();
3375
3376 // property functions
3377 double entropy() const;
3378
3379private:
3380 // no copy functions
3381 random_device(const random_device&); // = delete;
3382 random_device& operator=(const random_device&); // = delete;
3383};
3384
3385// seed_seq
3386
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003387class _LIBCPP_VISIBLE seed_seq
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003388{
3389public:
3390 // types
3391 typedef uint32_t result_type;
3392
3393private:
3394 vector<result_type> __v_;
3395
3396 template<class _InputIterator>
3397 void init(_InputIterator __first, _InputIterator __last);
3398public:
3399 // constructors
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003401 seed_seq() {}
3402 template<class _Tp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003404 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Howard Hinnant324bb032010-08-22 00:02:43 +00003405
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003406 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003408 seed_seq(_InputIterator __first, _InputIterator __last)
3409 {init(__first, __last);}
3410
3411 // generating functions
3412 template<class _RandomAccessIterator>
3413 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3414
3415 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003417 size_t size() const {return __v_.size();}
3418 template<class _OutputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003420 void param(_OutputIterator __dest) const
3421 {_STD::copy(__v_.begin(), __v_.end(), __dest);}
3422
3423private:
3424 // no copy functions
3425 seed_seq(const seed_seq&); // = delete;
3426 void operator=(const seed_seq&); // = delete;
3427
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003429 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
3430};
3431
3432template<class _InputIterator>
3433void
3434seed_seq::init(_InputIterator __first, _InputIterator __last)
3435{
3436 for (_InputIterator __s = __first; __s != __last; ++__s)
3437 __v_.push_back(*__s & 0xFFFFFFFF);
3438}
3439
3440template<class _RandomAccessIterator>
3441void
3442seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3443{
3444 if (__first != __last)
3445 {
3446 _STD::fill(__first, __last, 0x8b8b8b8b);
3447 const size_t __n = static_cast<size_t>(__last - __first);
3448 const size_t __s = __v_.size();
3449 const size_t __t = (__n >= 623) ? 11
3450 : (__n >= 68) ? 7
3451 : (__n >= 39) ? 5
3452 : (__n >= 7) ? 3
3453 : (__n - 1) / 2;
3454 const size_t __p = (__n - __t) / 2;
3455 const size_t __q = __p + __t;
3456 const size_t __m = _STD::max(__s + 1, __n);
3457 // __k = 0;
3458 {
3459 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
3460 ^ __first[__n - 1]);
3461 __first[__p] += __r;
3462 __r += __s;
3463 __first[__q] += __r;
3464 __first[0] = __r;
3465 }
3466 for (size_t __k = 1; __k <= __s; ++__k)
3467 {
3468 const size_t __kmodn = __k % __n;
3469 const size_t __kpmodn = (__k + __p) % __n;
3470 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3471 ^ __first[(__k - 1) % __n]);
3472 __first[__kpmodn] += __r;
3473 __r += __kmodn + __v_[__k-1];
3474 __first[(__k + __q) % __n] += __r;
3475 __first[__kmodn] = __r;
3476 }
3477 for (size_t __k = __s + 1; __k < __m; ++__k)
3478 {
3479 const size_t __kmodn = __k % __n;
3480 const size_t __kpmodn = (__k + __p) % __n;
3481 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3482 ^ __first[(__k - 1) % __n]);
3483 __first[__kpmodn] += __r;
3484 __r += __kmodn;
3485 __first[(__k + __q) % __n] += __r;
3486 __first[__kmodn] = __r;
3487 }
3488 for (size_t __k = __m; __k < __m + __n; ++__k)
3489 {
3490 const size_t __kmodn = __k % __n;
3491 const size_t __kpmodn = (__k + __p) % __n;
3492 result_type __r = 1566083941 * _T(__first[__kmodn] +
3493 __first[__kpmodn] +
3494 __first[(__k - 1) % __n]);
3495 __first[__kpmodn] ^= __r;
3496 __r -= __kmodn;
3497 __first[(__k + __q) % __n] ^= __r;
3498 __first[__kmodn] = __r;
3499 }
3500 }
3501}
3502
Howard Hinnant30a840f2010-05-12 17:08:57 +00003503// generate_canonical
3504
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003505template<class _RealType, size_t __bits, class _URNG>
3506_RealType
3507generate_canonical(_URNG& __g)
3508{
3509 const size_t _Dt = numeric_limits<_RealType>::digits;
3510 const size_t __b = _Dt < __bits ? _Dt : __bits;
3511 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
3512 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
3513 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
3514 _RealType __base = _R;
3515 _RealType _S = __g() - _URNG::_Min;
3516 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
3517 _S += (__g() - _URNG::_Min) * __base;
3518 return _S / __base;
3519}
3520
Howard Hinnant30a840f2010-05-12 17:08:57 +00003521// uniform_int_distribution
3522
Howard Hinnantc3267212010-05-26 17:49:34 +00003523// in <algorithm>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003524
3525template <class _CharT, class _Traits, class _IT>
3526basic_ostream<_CharT, _Traits>&
3527operator<<(basic_ostream<_CharT, _Traits>& __os,
3528 const uniform_int_distribution<_IT>& __x)
3529{
3530 __save_flags<_CharT, _Traits> _(__os);
3531 __os.flags(ios_base::dec | ios_base::left);
3532 _CharT __sp = __os.widen(' ');
3533 __os.fill(__sp);
3534 return __os << __x.a() << __sp << __x.b();
3535}
3536
3537template <class _CharT, class _Traits, class _IT>
3538basic_istream<_CharT, _Traits>&
3539operator>>(basic_istream<_CharT, _Traits>& __is,
3540 uniform_int_distribution<_IT>& __x)
3541{
3542 typedef uniform_int_distribution<_IT> _Eng;
3543 typedef typename _Eng::result_type result_type;
3544 typedef typename _Eng::param_type param_type;
3545 __save_flags<_CharT, _Traits> _(__is);
3546 __is.flags(ios_base::dec | ios_base::skipws);
3547 result_type __a;
3548 result_type __b;
3549 __is >> __a >> __b;
3550 if (!__is.fail())
3551 __x.param(param_type(__a, __b));
3552 return __is;
3553}
3554
Howard Hinnant30a840f2010-05-12 17:08:57 +00003555// uniform_real_distribution
3556
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003557template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003558class _LIBCPP_VISIBLE uniform_real_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003559{
3560public:
3561 // types
3562 typedef _RealType result_type;
3563
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003564 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003565 {
3566 result_type __a_;
3567 result_type __b_;
3568 public:
3569 typedef uniform_real_distribution distribution_type;
3570
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003572 explicit param_type(result_type __a = 0,
3573 result_type __b = 1)
3574 : __a_(__a), __b_(__b) {}
3575
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003577 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003579 result_type b() const {return __b_;}
3580
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003581 friend _LIBCPP_INLINE_VISIBILITY
3582 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003583 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003584 friend _LIBCPP_INLINE_VISIBILITY
3585 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586 {return !(__x == __y);}
3587 };
3588
3589private:
3590 param_type __p_;
3591
3592public:
3593 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003595 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3596 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003598 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600 void reset() {}
3601
3602 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003603 template<class _URNG>
3604 _LIBCPP_INLINE_VISIBILITY
3605 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003606 {return (*this)(__g, __p_);}
3607 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3608
3609 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003611 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003613 result_type b() const {return __p_.b();}
3614
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003616 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003618 void param(const param_type& __p) {__p_ = __p;}
3619
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003621 result_type min() const {return a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623 result_type max() const {return b();}
3624
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003625 friend _LIBCPP_INLINE_VISIBILITY
3626 bool operator==(const uniform_real_distribution& __x,
3627 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003628 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003629 friend _LIBCPP_INLINE_VISIBILITY
3630 bool operator!=(const uniform_real_distribution& __x,
3631 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632 {return !(__x == __y);}
3633};
3634
3635template<class _RealType>
3636template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003637inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003638typename uniform_real_distribution<_RealType>::result_type
3639uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3640{
3641 return (__p.b() - __p.a())
3642 * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
3643 + __p.a();
3644}
3645
3646template <class _CharT, class _Traits, class _RT>
3647basic_ostream<_CharT, _Traits>&
3648operator<<(basic_ostream<_CharT, _Traits>& __os,
3649 const uniform_real_distribution<_RT>& __x)
3650{
3651 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003652 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3653 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003654 _CharT __sp = __os.widen(' ');
3655 __os.fill(__sp);
3656 return __os << __x.a() << __sp << __x.b();
3657}
3658
3659template <class _CharT, class _Traits, class _RT>
3660basic_istream<_CharT, _Traits>&
3661operator>>(basic_istream<_CharT, _Traits>& __is,
3662 uniform_real_distribution<_RT>& __x)
3663{
3664 typedef uniform_real_distribution<_RT> _Eng;
3665 typedef typename _Eng::result_type result_type;
3666 typedef typename _Eng::param_type param_type;
3667 __save_flags<_CharT, _Traits> _(__is);
3668 __is.flags(ios_base::dec | ios_base::skipws);
3669 result_type __a;
3670 result_type __b;
3671 __is >> __a >> __b;
3672 if (!__is.fail())
3673 __x.param(param_type(__a, __b));
3674 return __is;
3675}
3676
Howard Hinnant30a840f2010-05-12 17:08:57 +00003677// bernoulli_distribution
3678
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003679class _LIBCPP_VISIBLE bernoulli_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003680{
3681public:
3682 // types
3683 typedef bool result_type;
3684
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003685 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686 {
3687 double __p_;
3688 public:
3689 typedef bernoulli_distribution distribution_type;
3690
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003692 explicit param_type(double __p = 0.5) : __p_(__p) {}
3693
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003695 double p() const {return __p_;}
3696
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003697 friend _LIBCPP_INLINE_VISIBILITY
3698 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003699 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003700 friend _LIBCPP_INLINE_VISIBILITY
3701 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003702 {return !(__x == __y);}
3703 };
3704
3705private:
3706 param_type __p_;
3707
3708public:
3709 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003711 explicit bernoulli_distribution(double __p = 0.5)
3712 : __p_(param_type(__p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003714 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003716 void reset() {}
3717
3718 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003719 template<class _URNG>
3720 _LIBCPP_INLINE_VISIBILITY
3721 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003722 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003723 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003724
3725 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003727 double p() const {return __p_.p();}
3728
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003730 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003732 void param(const param_type& __p) {__p_ = __p;}
3733
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003735 result_type min() const {return false;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003737 result_type max() const {return true;}
3738
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003739 friend _LIBCPP_INLINE_VISIBILITY
3740 bool operator==(const bernoulli_distribution& __x,
3741 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003742 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003743 friend _LIBCPP_INLINE_VISIBILITY
3744 bool operator!=(const bernoulli_distribution& __x,
3745 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003746 {return !(__x == __y);}
3747};
3748
Howard Hinnant03aad812010-05-11 23:26:59 +00003749template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003750inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003751bernoulli_distribution::result_type
3752bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3753{
Howard Hinnantd6d11712010-05-20 15:11:46 +00003754 uniform_real_distribution<double> __gen;
3755 return __gen(__g) < __p.p();
Howard Hinnant03aad812010-05-11 23:26:59 +00003756}
3757
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758template <class _CharT, class _Traits>
3759basic_ostream<_CharT, _Traits>&
3760operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3761{
3762 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003763 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3764 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003765 _CharT __sp = __os.widen(' ');
3766 __os.fill(__sp);
3767 return __os << __x.p();
3768}
3769
3770template <class _CharT, class _Traits>
3771basic_istream<_CharT, _Traits>&
3772operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3773{
3774 typedef bernoulli_distribution _Eng;
3775 typedef typename _Eng::param_type param_type;
3776 __save_flags<_CharT, _Traits> _(__is);
3777 __is.flags(ios_base::dec | ios_base::skipws);
3778 double __p;
3779 __is >> __p;
3780 if (!__is.fail())
3781 __x.param(param_type(__p));
3782 return __is;
3783}
3784
Howard Hinnant30a840f2010-05-12 17:08:57 +00003785// binomial_distribution
3786
Howard Hinnant03aad812010-05-11 23:26:59 +00003787template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003788class _LIBCPP_VISIBLE binomial_distribution
Howard Hinnant03aad812010-05-11 23:26:59 +00003789{
3790public:
3791 // types
3792 typedef _IntType result_type;
3793
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003794 class _LIBCPP_VISIBLE param_type
Howard Hinnant03aad812010-05-11 23:26:59 +00003795 {
3796 result_type __t_;
3797 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003798 double __pr_;
3799 double __odds_ratio_;
3800 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003801 public:
3802 typedef binomial_distribution distribution_type;
3803
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003804 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003805
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003807 result_type t() const {return __t_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003809 double p() const {return __p_;}
3810
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003811 friend _LIBCPP_INLINE_VISIBILITY
3812 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003813 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003814 friend _LIBCPP_INLINE_VISIBILITY
3815 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003816 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003817
3818 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003819 };
3820
3821private:
3822 param_type __p_;
3823
3824public:
3825 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003827 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3828 : __p_(param_type(__t, __p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003830 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003832 void reset() {}
3833
3834 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003835 template<class _URNG>
3836 _LIBCPP_INLINE_VISIBILITY
3837 result_type operator()(_URNG& __g)
Howard Hinnant03aad812010-05-11 23:26:59 +00003838 {return (*this)(__g, __p_);}
3839 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3840
3841 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003843 result_type t() const {return __p_.t();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003845 double p() const {return __p_.p();}
3846
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003848 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003850 void param(const param_type& __p) {__p_ = __p;}
3851
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003853 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003855 result_type max() const {return t();}
3856
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003857 friend _LIBCPP_INLINE_VISIBILITY
3858 bool operator==(const binomial_distribution& __x,
3859 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003860 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003861 friend _LIBCPP_INLINE_VISIBILITY
3862 bool operator!=(const binomial_distribution& __x,
3863 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003864 {return !(__x == __y);}
3865};
3866
3867template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003868binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3869 : __t_(__t), __p_(__p)
3870{
3871 if (0 < __p_ && __p_ < 1)
3872 {
3873 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
3874 __pr_ = _STD::exp(_STD::lgamma(__t_ + 1.) - _STD::lgamma(__r0_ + 1.) -
3875 _STD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _STD::log(__p_) +
3876 (__t_ - __r0_) * _STD::log(1 - __p_));
3877 __odds_ratio_ = __p_ / (1 - __p_);
3878 }
3879}
3880
3881template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00003882template<class _URNG>
3883_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003884binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00003885{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003886 if (__pr.__t_ == 0 || __pr.__p_ == 0)
3887 return 0;
3888 if (__pr.__p_ == 1)
3889 return __pr.__t_;
3890 uniform_real_distribution<double> __gen;
3891 double __u = __gen(__g) - __pr.__pr_;
3892 if (__u < 0)
3893 return __pr.__r0_;
3894 double __pu = __pr.__pr_;
3895 double __pd = __pu;
3896 result_type __ru = __pr.__r0_;
3897 result_type __rd = __ru;
3898 while (true)
3899 {
3900 if (__rd >= 1)
3901 {
3902 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3903 __u -= __pd;
3904 if (__u < 0)
3905 return __rd - 1;
3906 }
3907 --__rd;
3908 ++__ru;
3909 if (__ru <= __pr.__t_)
3910 {
3911 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3912 __u -= __pu;
3913 if (__u < 0)
3914 return __ru;
3915 }
3916 }
Howard Hinnant03aad812010-05-11 23:26:59 +00003917}
3918
3919template <class _CharT, class _Traits, class _IntType>
3920basic_ostream<_CharT, _Traits>&
3921operator<<(basic_ostream<_CharT, _Traits>& __os,
3922 const binomial_distribution<_IntType>& __x)
3923{
3924 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003925 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3926 ios_base::scientific);
Howard Hinnant03aad812010-05-11 23:26:59 +00003927 _CharT __sp = __os.widen(' ');
3928 __os.fill(__sp);
3929 return __os << __x.t() << __sp << __x.p();
3930}
3931
3932template <class _CharT, class _Traits, class _IntType>
3933basic_istream<_CharT, _Traits>&
3934operator>>(basic_istream<_CharT, _Traits>& __is,
3935 binomial_distribution<_IntType>& __x)
3936{
3937 typedef binomial_distribution<_IntType> _Eng;
3938 typedef typename _Eng::result_type result_type;
3939 typedef typename _Eng::param_type param_type;
3940 __save_flags<_CharT, _Traits> _(__is);
3941 __is.flags(ios_base::dec | ios_base::skipws);
3942 result_type __t;
3943 double __p;
3944 __is >> __t >> __p;
3945 if (!__is.fail())
3946 __x.param(param_type(__t, __p));
3947 return __is;
3948}
3949
Howard Hinnant30a840f2010-05-12 17:08:57 +00003950// exponential_distribution
3951
3952template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003953class _LIBCPP_VISIBLE exponential_distribution
Howard Hinnant30a840f2010-05-12 17:08:57 +00003954{
3955public:
3956 // types
3957 typedef _RealType result_type;
3958
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003959 class _LIBCPP_VISIBLE param_type
Howard Hinnant30a840f2010-05-12 17:08:57 +00003960 {
3961 result_type __lambda_;
3962 public:
3963 typedef exponential_distribution distribution_type;
3964
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003965 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003966 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3967
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003969 result_type lambda() const {return __lambda_;}
3970
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003971 friend _LIBCPP_INLINE_VISIBILITY
3972 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00003973 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003974 friend _LIBCPP_INLINE_VISIBILITY
3975 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00003976 {return !(__x == __y);}
3977 };
3978
3979private:
3980 param_type __p_;
3981
3982public:
3983 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003984 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003985 explicit exponential_distribution(result_type __lambda = 1)
3986 : __p_(param_type(__lambda)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003988 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003990 void reset() {}
3991
3992 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003993 template<class _URNG>
3994 _LIBCPP_INLINE_VISIBILITY
3995 result_type operator()(_URNG& __g)
Howard Hinnant30a840f2010-05-12 17:08:57 +00003996 {return (*this)(__g, __p_);}
3997 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3998
3999 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004001 result_type lambda() const {return __p_.lambda();}
4002
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004004 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004005 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004006 void param(const param_type& __p) {__p_ = __p;}
4007
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004009 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf40dc62010-05-16 17:56:20 +00004011 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00004012
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004013 friend _LIBCPP_INLINE_VISIBILITY
4014 bool operator==(const exponential_distribution& __x,
4015 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004016 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004017 friend _LIBCPP_INLINE_VISIBILITY
4018 bool operator!=(const exponential_distribution& __x,
4019 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004020 {return !(__x == __y);}
4021};
4022
4023template <class _RealType>
4024template<class _URNG>
4025_RealType
4026exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4027{
4028 return -_STD::log
4029 (
4030 result_type(1) -
4031 _STD::generate_canonical<result_type,
4032 numeric_limits<result_type>::digits>(__g)
4033 )
4034 / __p.lambda();
4035}
4036
4037template <class _CharT, class _Traits, class _RealType>
4038basic_ostream<_CharT, _Traits>&
4039operator<<(basic_ostream<_CharT, _Traits>& __os,
4040 const exponential_distribution<_RealType>& __x)
4041{
4042 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004043 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4044 ios_base::scientific);
Howard Hinnant30a840f2010-05-12 17:08:57 +00004045 return __os << __x.lambda();
4046}
4047
4048template <class _CharT, class _Traits, class _RealType>
4049basic_istream<_CharT, _Traits>&
4050operator>>(basic_istream<_CharT, _Traits>& __is,
4051 exponential_distribution<_RealType>& __x)
4052{
4053 typedef exponential_distribution<_RealType> _Eng;
4054 typedef typename _Eng::result_type result_type;
4055 typedef typename _Eng::param_type param_type;
4056 __save_flags<_CharT, _Traits> _(__is);
4057 __is.flags(ios_base::dec | ios_base::skipws);
4058 result_type __lambda;
4059 __is >> __lambda;
4060 if (!__is.fail())
4061 __x.param(param_type(__lambda));
4062 return __is;
4063}
4064
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004065// normal_distribution
4066
4067template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004068class _LIBCPP_VISIBLE normal_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004069{
4070public:
4071 // types
4072 typedef _RealType result_type;
4073
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004074 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004075 {
4076 result_type __mean_;
4077 result_type __stddev_;
4078 public:
4079 typedef normal_distribution distribution_type;
4080
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004082 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4083 : __mean_(__mean), __stddev_(__stddev) {}
4084
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004085 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004086 result_type mean() const {return __mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004088 result_type stddev() const {return __stddev_;}
4089
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004090 friend _LIBCPP_INLINE_VISIBILITY
4091 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004092 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004093 friend _LIBCPP_INLINE_VISIBILITY
4094 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004095 {return !(__x == __y);}
4096 };
4097
4098private:
4099 param_type __p_;
4100 result_type _V_;
4101 bool _V_hot_;
4102
4103public:
4104 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004106 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4107 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004109 explicit normal_distribution(const param_type& __p)
4110 : __p_(__p), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004112 void reset() {_V_hot_ = false;}
4113
4114 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004115 template<class _URNG>
4116 _LIBCPP_INLINE_VISIBILITY
4117 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004118 {return (*this)(__g, __p_);}
4119 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4120
4121 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004122 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004123 result_type mean() const {return __p_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004125 result_type stddev() const {return __p_.stddev();}
4126
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004128 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004130 void param(const param_type& __p) {__p_ = __p;}
4131
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004133 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004135 result_type max() const {return numeric_limits<result_type>::infinity();}
4136
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004137 friend _LIBCPP_INLINE_VISIBILITY
4138 bool operator==(const normal_distribution& __x,
4139 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004140 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4141 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004142 friend _LIBCPP_INLINE_VISIBILITY
4143 bool operator!=(const normal_distribution& __x,
4144 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004145 {return !(__x == __y);}
4146
4147 template <class _CharT, class _Traits, class _RT>
4148 friend
4149 basic_ostream<_CharT, _Traits>&
4150 operator<<(basic_ostream<_CharT, _Traits>& __os,
4151 const normal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004152
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004153 template <class _CharT, class _Traits, class _RT>
4154 friend
4155 basic_istream<_CharT, _Traits>&
4156 operator>>(basic_istream<_CharT, _Traits>& __is,
4157 normal_distribution<_RT>& __x);
4158};
4159
4160template <class _RealType>
4161template<class _URNG>
4162_RealType
4163normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4164{
4165 result_type _U;
4166 if (_V_hot_)
4167 {
4168 _V_hot_ = false;
4169 _U = _V_;
4170 }
4171 else
4172 {
4173 uniform_real_distribution<result_type> _Uni(-1, 1);
4174 result_type __u;
4175 result_type __v;
4176 result_type __s;
4177 do
4178 {
4179 __u = _Uni(__g);
4180 __v = _Uni(__g);
4181 __s = __u * __u + __v * __v;
4182 } while (__s > 1 || __s == 0);
4183 result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s);
4184 _V_ = __v * _F;
4185 _V_hot_ = true;
4186 _U = __u * _F;
4187 }
4188 return _U * __p.stddev() + __p.mean();
4189}
4190
4191template <class _CharT, class _Traits, class _RT>
4192basic_ostream<_CharT, _Traits>&
4193operator<<(basic_ostream<_CharT, _Traits>& __os,
4194 const normal_distribution<_RT>& __x)
4195{
4196 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004197 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4198 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004199 _CharT __sp = __os.widen(' ');
4200 __os.fill(__sp);
4201 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4202 if (__x._V_hot_)
4203 __os << __sp << __x._V_;
4204 return __os;
4205}
4206
4207template <class _CharT, class _Traits, class _RT>
4208basic_istream<_CharT, _Traits>&
4209operator>>(basic_istream<_CharT, _Traits>& __is,
4210 normal_distribution<_RT>& __x)
4211{
4212 typedef normal_distribution<_RT> _Eng;
4213 typedef typename _Eng::result_type result_type;
4214 typedef typename _Eng::param_type param_type;
4215 __save_flags<_CharT, _Traits> _(__is);
4216 __is.flags(ios_base::dec | ios_base::skipws);
4217 result_type __mean;
4218 result_type __stddev;
4219 result_type _V = 0;
4220 bool _V_hot = false;
4221 __is >> __mean >> __stddev >> _V_hot;
4222 if (_V_hot)
4223 __is >> _V;
4224 if (!__is.fail())
4225 {
4226 __x.param(param_type(__mean, __stddev));
4227 __x._V_hot_ = _V_hot;
4228 __x._V_ = _V;
4229 }
4230 return __is;
4231}
4232
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004233// lognormal_distribution
4234
4235template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004236class _LIBCPP_VISIBLE lognormal_distribution
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004237{
4238public:
4239 // types
4240 typedef _RealType result_type;
4241
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004242 class _LIBCPP_VISIBLE param_type
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004243 {
4244 normal_distribution<result_type> __nd_;
4245 public:
4246 typedef lognormal_distribution distribution_type;
4247
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004249 explicit param_type(result_type __m = 0, result_type __s = 1)
4250 : __nd_(__m, __s) {}
4251
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004253 result_type m() const {return __nd_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004255 result_type s() const {return __nd_.stddev();}
4256
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004257 friend _LIBCPP_INLINE_VISIBILITY
4258 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004259 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004260 friend _LIBCPP_INLINE_VISIBILITY
4261 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004262 {return !(__x == __y);}
4263 friend class lognormal_distribution;
4264
4265 template <class _CharT, class _Traits, class _RT>
4266 friend
4267 basic_ostream<_CharT, _Traits>&
4268 operator<<(basic_ostream<_CharT, _Traits>& __os,
4269 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004270
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004271 template <class _CharT, class _Traits, class _RT>
4272 friend
4273 basic_istream<_CharT, _Traits>&
4274 operator>>(basic_istream<_CharT, _Traits>& __is,
4275 lognormal_distribution<_RT>& __x);
4276 };
4277
4278private:
4279 param_type __p_;
4280
4281public:
4282 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004284 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4285 : __p_(param_type(__m, __s)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004287 explicit lognormal_distribution(const param_type& __p)
4288 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004290 void reset() {__p_.__nd_.reset();}
4291
4292 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004293 template<class _URNG>
4294 _LIBCPP_INLINE_VISIBILITY
4295 result_type operator()(_URNG& __g)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004296 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004297 template<class _URNG>
4298 _LIBCPP_INLINE_VISIBILITY
4299 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004300 {return _STD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
4301
4302 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004304 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004306 result_type s() const {return __p_.s();}
4307
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004309 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00004311 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004312
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004314 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004316 result_type max() const {return numeric_limits<result_type>::infinity();}
4317
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004318 friend _LIBCPP_INLINE_VISIBILITY
4319 bool operator==(const lognormal_distribution& __x,
4320 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004321 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004322 friend _LIBCPP_INLINE_VISIBILITY
4323 bool operator!=(const lognormal_distribution& __x,
4324 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004325 {return !(__x == __y);}
4326
4327 template <class _CharT, class _Traits, class _RT>
4328 friend
4329 basic_ostream<_CharT, _Traits>&
4330 operator<<(basic_ostream<_CharT, _Traits>& __os,
4331 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004332
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004333 template <class _CharT, class _Traits, class _RT>
4334 friend
4335 basic_istream<_CharT, _Traits>&
4336 operator>>(basic_istream<_CharT, _Traits>& __is,
4337 lognormal_distribution<_RT>& __x);
4338};
4339
4340template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004341inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004342basic_ostream<_CharT, _Traits>&
4343operator<<(basic_ostream<_CharT, _Traits>& __os,
4344 const lognormal_distribution<_RT>& __x)
4345{
4346 return __os << __x.__p_.__nd_;
4347}
4348
4349template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004350inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004351basic_istream<_CharT, _Traits>&
4352operator>>(basic_istream<_CharT, _Traits>& __is,
4353 lognormal_distribution<_RT>& __x)
4354{
4355 return __is >> __x.__p_.__nd_;
4356}
4357
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004358// poisson_distribution
4359
4360template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004361class _LIBCPP_VISIBLE poisson_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004362{
4363public:
4364 // types
4365 typedef _IntType result_type;
4366
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004367 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004368 {
4369 double __mean_;
4370 double __s_;
4371 double __d_;
4372 double __l_;
4373 double __omega_;
4374 double __c0_;
4375 double __c1_;
4376 double __c2_;
4377 double __c3_;
4378 double __c_;
4379
4380 public:
4381 typedef poisson_distribution distribution_type;
4382
4383 explicit param_type(double __mean = 1.0);
4384
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004386 double mean() const {return __mean_;}
4387
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004388 friend _LIBCPP_INLINE_VISIBILITY
4389 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004390 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004391 friend _LIBCPP_INLINE_VISIBILITY
4392 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004393 {return !(__x == __y);}
4394
4395 friend class poisson_distribution;
4396 };
4397
4398private:
4399 param_type __p_;
4400
4401public:
4402 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004404 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004406 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004408 void reset() {}
4409
4410 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004411 template<class _URNG>
4412 _LIBCPP_INLINE_VISIBILITY
4413 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004414 {return (*this)(__g, __p_);}
4415 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4416
4417 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004419 double mean() const {return __p_.mean();}
4420
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004422 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004424 void param(const param_type& __p) {__p_ = __p;}
4425
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004427 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004429 result_type max() const {return numeric_limits<result_type>::max();}
4430
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004431 friend _LIBCPP_INLINE_VISIBILITY
4432 bool operator==(const poisson_distribution& __x,
4433 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004434 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004435 friend _LIBCPP_INLINE_VISIBILITY
4436 bool operator!=(const poisson_distribution& __x,
4437 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004438 {return !(__x == __y);}
4439};
4440
4441template<class _IntType>
4442poisson_distribution<_IntType>::param_type::param_type(double __mean)
4443 : __mean_(__mean)
4444{
4445 if (__mean_ < 10)
4446 {
4447 __s_ = 0;
4448 __d_ = 0;
4449 __l_ = _STD::exp(-__mean_);
4450 __omega_ = 0;
4451 __c3_ = 0;
4452 __c2_ = 0;
4453 __c1_ = 0;
4454 __c0_ = 0;
4455 __c_ = 0;
4456 }
4457 else
4458 {
4459 __s_ = _STD::sqrt(__mean_);
4460 __d_ = 6 * __mean_ * __mean_;
4461 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4462 __omega_ = .3989423 / __s_;
4463 double __b1_ = .4166667E-1 / __mean_;
4464 double __b2_ = .3 * __b1_ * __b1_;
4465 __c3_ = .1428571 * __b1_ * __b2_;
4466 __c2_ = __b2_ - 15. * __c3_;
4467 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4468 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4469 __c_ = .1069 / __mean_;
4470 }
4471}
4472
4473template <class _IntType>
4474template<class _URNG>
4475_IntType
4476poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4477{
4478 result_type __x;
4479 uniform_real_distribution<double> __urd;
Howard Hinnant75f76952011-04-14 15:59:22 +00004480 if (__pr.__mean_ < 10)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004481 {
4482 __x = 0;
4483 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4484 __p *= __urd(__urng);
4485 }
4486 else
4487 {
4488 double __difmuk;
4489 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4490 double __u;
4491 if (__g > 0)
4492 {
4493 __x = static_cast<result_type>(__g);
4494 if (__x >= __pr.__l_)
4495 return __x;
4496 __difmuk = __pr.__mean_ - __x;
4497 __u = __urd(__urng);
4498 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4499 return __x;
4500 }
4501 exponential_distribution<double> __edist;
4502 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4503 {
4504 double __e;
4505 if (__using_exp_dist || __g < 0)
4506 {
4507 double __t;
4508 do
4509 {
4510 __e = __edist(__urng);
4511 __u = __urd(__urng);
4512 __u += __u - 1;
4513 __t = 1.8 + (__u < 0 ? -__e : __e);
4514 } while (__t <= -.6744);
4515 __x = __pr.__mean_ + __pr.__s_ * __t;
4516 __difmuk = __pr.__mean_ - __x;
4517 __using_exp_dist = true;
4518 }
4519 double __px;
4520 double __py;
4521 if (__x < 10)
4522 {
4523 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4524 40320, 362880};
4525 __px = -__pr.__mean_;
4526 __py = _STD::pow(__pr.__mean_, (double)__x) / __fac[__x];
4527 }
4528 else
4529 {
4530 double __del = .8333333E-1 / __x;
4531 __del -= 4.8 * __del * __del * __del;
4532 double __v = __difmuk / __x;
4533 if (_STD::abs(__v) > 0.25)
4534 __px = __x * _STD::log(1 + __v) - __difmuk - __del;
4535 else
4536 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4537 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4538 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
4539 __py = .3989423 / _STD::sqrt(__x);
4540 }
4541 double __r = (0.5 - __difmuk) / __pr.__s_;
4542 double __r2 = __r * __r;
4543 double __fx = -0.5 * __r2;
4544 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4545 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4546 if (__using_exp_dist)
4547 {
4548 if (__pr.__c_ * _STD::abs(__u) <= __py * _STD::exp(__px + __e) -
4549 __fy * _STD::exp(__fx + __e))
4550 break;
4551 }
4552 else
4553 {
4554 if (__fy - __u * __fy <= __py * _STD::exp(__px - __fx))
4555 break;
4556 }
4557 }
4558 }
4559 return __x;
4560}
4561
4562template <class _CharT, class _Traits, class _IntType>
4563basic_ostream<_CharT, _Traits>&
4564operator<<(basic_ostream<_CharT, _Traits>& __os,
4565 const poisson_distribution<_IntType>& __x)
4566{
4567 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004568 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4569 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004570 return __os << __x.mean();
4571}
4572
4573template <class _CharT, class _Traits, class _IntType>
4574basic_istream<_CharT, _Traits>&
4575operator>>(basic_istream<_CharT, _Traits>& __is,
4576 poisson_distribution<_IntType>& __x)
4577{
4578 typedef poisson_distribution<_IntType> _Eng;
4579 typedef typename _Eng::param_type param_type;
4580 __save_flags<_CharT, _Traits> _(__is);
4581 __is.flags(ios_base::dec | ios_base::skipws);
4582 double __mean;
4583 __is >> __mean;
4584 if (!__is.fail())
4585 __x.param(param_type(__mean));
4586 return __is;
4587}
4588
Howard Hinnant9de6e302010-05-16 01:09:02 +00004589// weibull_distribution
4590
4591template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004592class _LIBCPP_VISIBLE weibull_distribution
Howard Hinnant9de6e302010-05-16 01:09:02 +00004593{
4594public:
4595 // types
4596 typedef _RealType result_type;
4597
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004598 class _LIBCPP_VISIBLE param_type
Howard Hinnant9de6e302010-05-16 01:09:02 +00004599 {
4600 result_type __a_;
4601 result_type __b_;
4602 public:
4603 typedef weibull_distribution distribution_type;
4604
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004606 explicit param_type(result_type __a = 1, result_type __b = 1)
4607 : __a_(__a), __b_(__b) {}
4608
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004610 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004612 result_type b() const {return __b_;}
4613
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004614 friend _LIBCPP_INLINE_VISIBILITY
4615 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004616 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004617 friend _LIBCPP_INLINE_VISIBILITY
4618 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004619 {return !(__x == __y);}
4620 };
4621
4622private:
4623 param_type __p_;
4624
4625public:
4626 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004628 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4629 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004631 explicit weibull_distribution(const param_type& __p)
4632 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004634 void reset() {}
4635
4636 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004637 template<class _URNG>
4638 _LIBCPP_INLINE_VISIBILITY
4639 result_type operator()(_URNG& __g)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004640 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004641 template<class _URNG>
4642 _LIBCPP_INLINE_VISIBILITY
4643 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004644 {return __p.b() *
4645 _STD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
4646
4647 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004649 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004651 result_type b() const {return __p_.b();}
4652
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004654 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004656 void param(const param_type& __p) {__p_ = __p;}
4657
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004659 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004661 result_type max() const {return numeric_limits<result_type>::infinity();}
4662
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004663 friend _LIBCPP_INLINE_VISIBILITY
4664 bool operator==(const weibull_distribution& __x,
4665 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004666 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004667 friend _LIBCPP_INLINE_VISIBILITY
4668 bool operator!=(const weibull_distribution& __x,
4669 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004670 {return !(__x == __y);}
4671};
4672
4673template <class _CharT, class _Traits, class _RT>
4674basic_ostream<_CharT, _Traits>&
4675operator<<(basic_ostream<_CharT, _Traits>& __os,
4676 const weibull_distribution<_RT>& __x)
4677{
4678 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004679 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4680 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004681 _CharT __sp = __os.widen(' ');
4682 __os.fill(__sp);
4683 __os << __x.a() << __sp << __x.b();
4684 return __os;
4685}
4686
4687template <class _CharT, class _Traits, class _RT>
4688basic_istream<_CharT, _Traits>&
4689operator>>(basic_istream<_CharT, _Traits>& __is,
4690 weibull_distribution<_RT>& __x)
4691{
4692 typedef weibull_distribution<_RT> _Eng;
4693 typedef typename _Eng::result_type result_type;
4694 typedef typename _Eng::param_type param_type;
4695 __save_flags<_CharT, _Traits> _(__is);
4696 __is.flags(ios_base::dec | ios_base::skipws);
4697 result_type __a;
4698 result_type __b;
4699 __is >> __a >> __b;
4700 if (!__is.fail())
4701 __x.param(param_type(__a, __b));
4702 return __is;
4703}
4704
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004705template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004706class _LIBCPP_VISIBLE extreme_value_distribution
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004707{
4708public:
4709 // types
4710 typedef _RealType result_type;
4711
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004712 class _LIBCPP_VISIBLE param_type
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004713 {
4714 result_type __a_;
4715 result_type __b_;
4716 public:
4717 typedef extreme_value_distribution distribution_type;
4718
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004720 explicit param_type(result_type __a = 0, result_type __b = 1)
4721 : __a_(__a), __b_(__b) {}
4722
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004724 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004726 result_type b() const {return __b_;}
4727
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004728 friend _LIBCPP_INLINE_VISIBILITY
4729 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004730 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004731 friend _LIBCPP_INLINE_VISIBILITY
4732 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004733 {return !(__x == __y);}
4734 };
4735
4736private:
4737 param_type __p_;
4738
4739public:
4740 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004742 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4743 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004745 explicit extreme_value_distribution(const param_type& __p)
4746 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004748 void reset() {}
4749
4750 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004751 template<class _URNG>
4752 _LIBCPP_INLINE_VISIBILITY
4753 result_type operator()(_URNG& __g)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004754 {return (*this)(__g, __p_);}
4755 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4756
4757 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004759 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004761 result_type b() const {return __p_.b();}
4762
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004764 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004766 void param(const param_type& __p) {__p_ = __p;}
4767
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004769 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004771 result_type max() const {return numeric_limits<result_type>::infinity();}
4772
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004773 friend _LIBCPP_INLINE_VISIBILITY
4774 bool operator==(const extreme_value_distribution& __x,
4775 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004776 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004777 friend _LIBCPP_INLINE_VISIBILITY
4778 bool operator!=(const extreme_value_distribution& __x,
4779 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004780 {return !(__x == __y);}
4781};
4782
4783template<class _RealType>
4784template<class _URNG>
4785_RealType
4786extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4787{
4788 return __p.a() - __p.b() *
4789 _STD::log(-_STD::log(1-uniform_real_distribution<result_type>()(__g)));
4790}
4791
4792template <class _CharT, class _Traits, class _RT>
4793basic_ostream<_CharT, _Traits>&
4794operator<<(basic_ostream<_CharT, _Traits>& __os,
4795 const extreme_value_distribution<_RT>& __x)
4796{
4797 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004798 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4799 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004800 _CharT __sp = __os.widen(' ');
4801 __os.fill(__sp);
4802 __os << __x.a() << __sp << __x.b();
4803 return __os;
4804}
4805
4806template <class _CharT, class _Traits, class _RT>
4807basic_istream<_CharT, _Traits>&
4808operator>>(basic_istream<_CharT, _Traits>& __is,
4809 extreme_value_distribution<_RT>& __x)
4810{
4811 typedef extreme_value_distribution<_RT> _Eng;
4812 typedef typename _Eng::result_type result_type;
4813 typedef typename _Eng::param_type param_type;
4814 __save_flags<_CharT, _Traits> _(__is);
4815 __is.flags(ios_base::dec | ios_base::skipws);
4816 result_type __a;
4817 result_type __b;
4818 __is >> __a >> __b;
4819 if (!__is.fail())
4820 __x.param(param_type(__a, __b));
4821 return __is;
4822}
4823
Howard Hinnantc7c49132010-05-13 17:58:28 +00004824// gamma_distribution
4825
4826template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004827class _LIBCPP_VISIBLE gamma_distribution
Howard Hinnantc7c49132010-05-13 17:58:28 +00004828{
4829public:
4830 // types
4831 typedef _RealType result_type;
4832
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004833 class _LIBCPP_VISIBLE param_type
Howard Hinnantc7c49132010-05-13 17:58:28 +00004834 {
4835 result_type __alpha_;
4836 result_type __beta_;
4837 public:
4838 typedef gamma_distribution distribution_type;
4839
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004841 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4842 : __alpha_(__alpha), __beta_(__beta) {}
4843
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004845 result_type alpha() const {return __alpha_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004847 result_type beta() const {return __beta_;}
4848
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004849 friend _LIBCPP_INLINE_VISIBILITY
4850 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004851 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004852 friend _LIBCPP_INLINE_VISIBILITY
4853 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004854 {return !(__x == __y);}
4855 };
4856
4857private:
4858 param_type __p_;
4859
4860public:
4861 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004863 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4864 : __p_(param_type(__alpha, __beta)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004866 explicit gamma_distribution(const param_type& __p)
4867 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004869 void reset() {}
4870
4871 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004872 template<class _URNG>
4873 _LIBCPP_INLINE_VISIBILITY
4874 result_type operator()(_URNG& __g)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004875 {return (*this)(__g, __p_);}
4876 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4877
4878 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004880 result_type alpha() const {return __p_.alpha();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004882 result_type beta() const {return __p_.beta();}
4883
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004885 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004887 void param(const param_type& __p) {__p_ = __p;}
4888
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004890 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004892 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantc7c49132010-05-13 17:58:28 +00004893
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004894 friend _LIBCPP_INLINE_VISIBILITY
4895 bool operator==(const gamma_distribution& __x,
4896 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004897 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004898 friend _LIBCPP_INLINE_VISIBILITY
4899 bool operator!=(const gamma_distribution& __x,
4900 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004901 {return !(__x == __y);}
4902};
4903
4904template <class _RealType>
4905template<class _URNG>
4906_RealType
4907gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4908{
Howard Hinnantf417abe2010-05-14 18:43:10 +00004909 result_type __a = __p.alpha();
4910 uniform_real_distribution<result_type> __gen(0, 1);
4911 exponential_distribution<result_type> __egen;
4912 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00004913 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00004914 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004915 else if (__a > 1)
4916 {
4917 const result_type __b = __a - 1;
4918 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004919 while (true)
4920 {
4921 const result_type __u = __gen(__g);
4922 const result_type __v = __gen(__g);
4923 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004924 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004925 {
4926 const result_type __y = _STD::sqrt(__c / __w) *
4927 (__u - result_type(0.5));
4928 __x = __b + __y;
4929 if (__x >= 0)
4930 {
4931 const result_type __z = 64 * __w * __w * __w * __v * __v;
4932 if (__z <= 1 - 2 * __y * __y / __x)
4933 break;
4934 if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y))
4935 break;
4936 }
4937 }
4938 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00004939 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00004940 else // __a < 1
4941 {
4942 while (true)
4943 {
4944 const result_type __u = __gen(__g);
4945 const result_type __es = __egen(__g);
4946 if (__u <= 1 - __a)
4947 {
4948 __x = _STD::pow(__u, 1 / __a);
4949 if (__x <= __es)
4950 break;
4951 }
4952 else
4953 {
4954 const result_type __e = -_STD::log((1-__u)/__a);
4955 __x = _STD::pow(1 - __a + __a * __e, 1 / __a);
4956 if (__x <= __e + __es)
4957 break;
4958 }
4959 }
4960 }
4961 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00004962}
4963
4964template <class _CharT, class _Traits, class _RT>
4965basic_ostream<_CharT, _Traits>&
4966operator<<(basic_ostream<_CharT, _Traits>& __os,
4967 const gamma_distribution<_RT>& __x)
4968{
4969 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004970 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4971 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004972 _CharT __sp = __os.widen(' ');
4973 __os.fill(__sp);
4974 __os << __x.alpha() << __sp << __x.beta();
4975 return __os;
4976}
4977
4978template <class _CharT, class _Traits, class _RT>
4979basic_istream<_CharT, _Traits>&
4980operator>>(basic_istream<_CharT, _Traits>& __is,
4981 gamma_distribution<_RT>& __x)
4982{
4983 typedef gamma_distribution<_RT> _Eng;
4984 typedef typename _Eng::result_type result_type;
4985 typedef typename _Eng::param_type param_type;
4986 __save_flags<_CharT, _Traits> _(__is);
4987 __is.flags(ios_base::dec | ios_base::skipws);
4988 result_type __alpha;
4989 result_type __beta;
4990 __is >> __alpha >> __beta;
4991 if (!__is.fail())
4992 __x.param(param_type(__alpha, __beta));
4993 return __is;
4994}
Howard Hinnanta64111c2010-05-12 21:02:31 +00004995
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00004996// negative_binomial_distribution
4997
4998template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004999class _LIBCPP_VISIBLE negative_binomial_distribution
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005000{
5001public:
5002 // types
5003 typedef _IntType result_type;
5004
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005005 class _LIBCPP_VISIBLE param_type
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005006 {
5007 result_type __k_;
5008 double __p_;
5009 public:
5010 typedef negative_binomial_distribution distribution_type;
5011
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005012 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005013 explicit param_type(result_type __k = 1, double __p = 0.5)
5014 : __k_(__k), __p_(__p) {}
5015
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005017 result_type k() const {return __k_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005019 double p() const {return __p_;}
5020
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005021 friend _LIBCPP_INLINE_VISIBILITY
5022 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005023 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005024 friend _LIBCPP_INLINE_VISIBILITY
5025 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005026 {return !(__x == __y);}
5027 };
5028
5029private:
5030 param_type __p_;
5031
5032public:
5033 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005035 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
5036 : __p_(__k, __p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005038 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005040 void reset() {}
5041
5042 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005043 template<class _URNG>
5044 _LIBCPP_INLINE_VISIBILITY
5045 result_type operator()(_URNG& __g)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005046 {return (*this)(__g, __p_);}
5047 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5048
5049 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005051 result_type k() const {return __p_.k();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005053 double p() const {return __p_.p();}
5054
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005056 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005058 void param(const param_type& __p) {__p_ = __p;}
5059
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005061 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005063 result_type max() const {return numeric_limits<result_type>::max();}
5064
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005065 friend _LIBCPP_INLINE_VISIBILITY
5066 bool operator==(const negative_binomial_distribution& __x,
5067 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005068 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005069 friend _LIBCPP_INLINE_VISIBILITY
5070 bool operator!=(const negative_binomial_distribution& __x,
5071 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005072 {return !(__x == __y);}
5073};
5074
5075template <class _IntType>
5076template<class _URNG>
5077_IntType
5078negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5079{
5080 result_type __k = __pr.k();
5081 double __p = __pr.p();
5082 if (__k <= 21 * __p)
5083 {
5084 bernoulli_distribution __gen(__p);
5085 result_type __f = 0;
5086 result_type __s = 0;
5087 while (__s < __k)
5088 {
5089 if (__gen(__urng))
5090 ++__s;
5091 else
5092 ++__f;
5093 }
5094 return __f;
5095 }
5096 return poisson_distribution<result_type>(gamma_distribution<double>
5097 (__k, (1-__p)/__p)(__urng))(__urng);
5098}
5099
5100template <class _CharT, class _Traits, class _IntType>
5101basic_ostream<_CharT, _Traits>&
5102operator<<(basic_ostream<_CharT, _Traits>& __os,
5103 const negative_binomial_distribution<_IntType>& __x)
5104{
5105 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005106 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5107 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005108 _CharT __sp = __os.widen(' ');
5109 __os.fill(__sp);
5110 return __os << __x.k() << __sp << __x.p();
5111}
5112
5113template <class _CharT, class _Traits, class _IntType>
5114basic_istream<_CharT, _Traits>&
5115operator>>(basic_istream<_CharT, _Traits>& __is,
5116 negative_binomial_distribution<_IntType>& __x)
5117{
5118 typedef negative_binomial_distribution<_IntType> _Eng;
5119 typedef typename _Eng::result_type result_type;
5120 typedef typename _Eng::param_type param_type;
5121 __save_flags<_CharT, _Traits> _(__is);
5122 __is.flags(ios_base::dec | ios_base::skipws);
5123 result_type __k;
5124 double __p;
5125 __is >> __k >> __p;
5126 if (!__is.fail())
5127 __x.param(param_type(__k, __p));
5128 return __is;
5129}
5130
Howard Hinnant34e8a572010-05-17 13:44:27 +00005131// geometric_distribution
5132
5133template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005134class _LIBCPP_VISIBLE geometric_distribution
Howard Hinnant34e8a572010-05-17 13:44:27 +00005135{
5136public:
5137 // types
5138 typedef _IntType result_type;
5139
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005140 class _LIBCPP_VISIBLE param_type
Howard Hinnant34e8a572010-05-17 13:44:27 +00005141 {
5142 double __p_;
5143 public:
5144 typedef geometric_distribution distribution_type;
5145
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005147 explicit param_type(double __p = 0.5) : __p_(__p) {}
5148
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005150 double p() const {return __p_;}
5151
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005152 friend _LIBCPP_INLINE_VISIBILITY
5153 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005154 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005155 friend _LIBCPP_INLINE_VISIBILITY
5156 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005157 {return !(__x == __y);}
5158 };
5159
5160private:
5161 param_type __p_;
5162
5163public:
5164 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005166 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005168 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005170 void reset() {}
5171
5172 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005173 template<class _URNG>
5174 _LIBCPP_INLINE_VISIBILITY
5175 result_type operator()(_URNG& __g)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005176 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005177 template<class _URNG>
5178 _LIBCPP_INLINE_VISIBILITY
5179 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005180 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5181
5182 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005184 double p() const {return __p_.p();}
5185
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005187 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005189 void param(const param_type& __p) {__p_ = __p;}
5190
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005192 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005194 result_type max() const {return numeric_limits<result_type>::max();}
5195
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005196 friend _LIBCPP_INLINE_VISIBILITY
5197 bool operator==(const geometric_distribution& __x,
5198 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005199 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005200 friend _LIBCPP_INLINE_VISIBILITY
5201 bool operator!=(const geometric_distribution& __x,
5202 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005203 {return !(__x == __y);}
5204};
5205
5206template <class _CharT, class _Traits, class _IntType>
5207basic_ostream<_CharT, _Traits>&
5208operator<<(basic_ostream<_CharT, _Traits>& __os,
5209 const geometric_distribution<_IntType>& __x)
5210{
5211 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005212 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5213 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005214 return __os << __x.p();
5215}
5216
5217template <class _CharT, class _Traits, class _IntType>
5218basic_istream<_CharT, _Traits>&
5219operator>>(basic_istream<_CharT, _Traits>& __is,
5220 geometric_distribution<_IntType>& __x)
5221{
5222 typedef geometric_distribution<_IntType> _Eng;
5223 typedef typename _Eng::param_type param_type;
5224 __save_flags<_CharT, _Traits> _(__is);
5225 __is.flags(ios_base::dec | ios_base::skipws);
5226 double __p;
5227 __is >> __p;
5228 if (!__is.fail())
5229 __x.param(param_type(__p));
5230 return __is;
5231}
5232
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005233// chi_squared_distribution
5234
5235template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005236class _LIBCPP_VISIBLE chi_squared_distribution
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005237{
5238public:
5239 // types
5240 typedef _RealType result_type;
5241
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005242 class _LIBCPP_VISIBLE param_type
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005243 {
5244 result_type __n_;
5245 public:
5246 typedef chi_squared_distribution distribution_type;
5247
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005249 explicit param_type(result_type __n = 1) : __n_(__n) {}
5250
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005252 result_type n() const {return __n_;}
5253
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005254 friend _LIBCPP_INLINE_VISIBILITY
5255 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005256 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005257 friend _LIBCPP_INLINE_VISIBILITY
5258 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005259 {return !(__x == __y);}
5260 };
5261
5262private:
5263 param_type __p_;
5264
5265public:
5266 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005268 explicit chi_squared_distribution(result_type __n = 1)
5269 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005271 explicit chi_squared_distribution(const param_type& __p)
5272 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005274 void reset() {}
5275
5276 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005277 template<class _URNG>
5278 _LIBCPP_INLINE_VISIBILITY
5279 result_type operator()(_URNG& __g)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005280 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005281 template<class _URNG>
5282 _LIBCPP_INLINE_VISIBILITY
5283 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005284 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5285
5286 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005288 result_type n() const {return __p_.n();}
5289
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005291 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005293 void param(const param_type& __p) {__p_ = __p;}
5294
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005296 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005298 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005299
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005300 friend _LIBCPP_INLINE_VISIBILITY
5301 bool operator==(const chi_squared_distribution& __x,
5302 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005303 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005304 friend _LIBCPP_INLINE_VISIBILITY
5305 bool operator!=(const chi_squared_distribution& __x,
5306 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005307 {return !(__x == __y);}
5308};
5309
5310template <class _CharT, class _Traits, class _RT>
5311basic_ostream<_CharT, _Traits>&
5312operator<<(basic_ostream<_CharT, _Traits>& __os,
5313 const chi_squared_distribution<_RT>& __x)
5314{
5315 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005316 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5317 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005318 __os << __x.n();
5319 return __os;
5320}
5321
5322template <class _CharT, class _Traits, class _RT>
5323basic_istream<_CharT, _Traits>&
5324operator>>(basic_istream<_CharT, _Traits>& __is,
5325 chi_squared_distribution<_RT>& __x)
5326{
5327 typedef chi_squared_distribution<_RT> _Eng;
5328 typedef typename _Eng::result_type result_type;
5329 typedef typename _Eng::param_type param_type;
5330 __save_flags<_CharT, _Traits> _(__is);
5331 __is.flags(ios_base::dec | ios_base::skipws);
5332 result_type __n;
5333 __is >> __n;
5334 if (!__is.fail())
5335 __x.param(param_type(__n));
5336 return __is;
5337}
5338
Howard Hinnantd7d01132010-05-17 21:55:46 +00005339// cauchy_distribution
5340
5341template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005342class _LIBCPP_VISIBLE cauchy_distribution
Howard Hinnantd7d01132010-05-17 21:55:46 +00005343{
5344public:
5345 // types
5346 typedef _RealType result_type;
5347
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005348 class _LIBCPP_VISIBLE param_type
Howard Hinnantd7d01132010-05-17 21:55:46 +00005349 {
5350 result_type __a_;
5351 result_type __b_;
5352 public:
5353 typedef cauchy_distribution distribution_type;
5354
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005356 explicit param_type(result_type __a = 0, result_type __b = 1)
5357 : __a_(__a), __b_(__b) {}
5358
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005360 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005362 result_type b() const {return __b_;}
5363
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005364 friend _LIBCPP_INLINE_VISIBILITY
5365 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005366 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005367 friend _LIBCPP_INLINE_VISIBILITY
5368 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005369 {return !(__x == __y);}
5370 };
5371
5372private:
5373 param_type __p_;
5374
5375public:
5376 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005378 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5379 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005381 explicit cauchy_distribution(const param_type& __p)
5382 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005384 void reset() {}
5385
5386 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005387 template<class _URNG>
5388 _LIBCPP_INLINE_VISIBILITY
5389 result_type operator()(_URNG& __g)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005390 {return (*this)(__g, __p_);}
5391 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5392
5393 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005395 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005397 result_type b() const {return __p_.b();}
5398
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005400 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005402 void param(const param_type& __p) {__p_ = __p;}
5403
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005405 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005407 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005408
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005409 friend _LIBCPP_INLINE_VISIBILITY
5410 bool operator==(const cauchy_distribution& __x,
5411 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005412 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005413 friend _LIBCPP_INLINE_VISIBILITY
5414 bool operator!=(const cauchy_distribution& __x,
5415 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005416 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005417};
5418
5419template <class _RealType>
5420template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005421inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005422_RealType
5423cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5424{
5425 uniform_real_distribution<result_type> __gen;
5426 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
5427 return __p.a() + __p.b() * _STD::tan(3.1415926535897932384626433832795 * __gen(__g));
5428}
5429
5430template <class _CharT, class _Traits, class _RT>
5431basic_ostream<_CharT, _Traits>&
5432operator<<(basic_ostream<_CharT, _Traits>& __os,
5433 const cauchy_distribution<_RT>& __x)
5434{
5435 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005436 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5437 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005438 _CharT __sp = __os.widen(' ');
5439 __os.fill(__sp);
5440 __os << __x.a() << __sp << __x.b();
5441 return __os;
5442}
5443
5444template <class _CharT, class _Traits, class _RT>
5445basic_istream<_CharT, _Traits>&
5446operator>>(basic_istream<_CharT, _Traits>& __is,
5447 cauchy_distribution<_RT>& __x)
5448{
5449 typedef cauchy_distribution<_RT> _Eng;
5450 typedef typename _Eng::result_type result_type;
5451 typedef typename _Eng::param_type param_type;
5452 __save_flags<_CharT, _Traits> _(__is);
5453 __is.flags(ios_base::dec | ios_base::skipws);
5454 result_type __a;
5455 result_type __b;
5456 __is >> __a >> __b;
5457 if (!__is.fail())
5458 __x.param(param_type(__a, __b));
5459 return __is;
5460}
5461
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005462// fisher_f_distribution
5463
5464template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005465class _LIBCPP_VISIBLE fisher_f_distribution
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005466{
5467public:
5468 // types
5469 typedef _RealType result_type;
5470
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005471 class _LIBCPP_VISIBLE param_type
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005472 {
5473 result_type __m_;
5474 result_type __n_;
5475 public:
5476 typedef fisher_f_distribution distribution_type;
5477
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005479 explicit param_type(result_type __m = 1, result_type __n = 1)
5480 : __m_(__m), __n_(__n) {}
5481
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005483 result_type m() const {return __m_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005485 result_type n() const {return __n_;}
5486
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005487 friend _LIBCPP_INLINE_VISIBILITY
5488 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005489 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005490 friend _LIBCPP_INLINE_VISIBILITY
5491 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005492 {return !(__x == __y);}
5493 };
5494
5495private:
5496 param_type __p_;
5497
5498public:
5499 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005501 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5502 : __p_(param_type(__m, __n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005503 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005504 explicit fisher_f_distribution(const param_type& __p)
5505 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005507 void reset() {}
5508
5509 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005510 template<class _URNG>
5511 _LIBCPP_INLINE_VISIBILITY
5512 result_type operator()(_URNG& __g)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005513 {return (*this)(__g, __p_);}
5514 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5515
5516 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005517 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005518 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005519 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005520 result_type n() const {return __p_.n();}
5521
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005523 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005525 void param(const param_type& __p) {__p_ = __p;}
5526
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005528 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005530 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005531
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005532 friend _LIBCPP_INLINE_VISIBILITY
5533 bool operator==(const fisher_f_distribution& __x,
5534 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005535 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005536 friend _LIBCPP_INLINE_VISIBILITY
5537 bool operator!=(const fisher_f_distribution& __x,
5538 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005539 {return !(__x == __y);}
5540};
5541
5542template <class _RealType>
5543template<class _URNG>
5544_RealType
5545fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5546{
5547 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5548 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5549 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5550}
5551
5552template <class _CharT, class _Traits, class _RT>
5553basic_ostream<_CharT, _Traits>&
5554operator<<(basic_ostream<_CharT, _Traits>& __os,
5555 const fisher_f_distribution<_RT>& __x)
5556{
5557 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005558 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5559 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005560 _CharT __sp = __os.widen(' ');
5561 __os.fill(__sp);
5562 __os << __x.m() << __sp << __x.n();
5563 return __os;
5564}
5565
5566template <class _CharT, class _Traits, class _RT>
5567basic_istream<_CharT, _Traits>&
5568operator>>(basic_istream<_CharT, _Traits>& __is,
5569 fisher_f_distribution<_RT>& __x)
5570{
5571 typedef fisher_f_distribution<_RT> _Eng;
5572 typedef typename _Eng::result_type result_type;
5573 typedef typename _Eng::param_type param_type;
5574 __save_flags<_CharT, _Traits> _(__is);
5575 __is.flags(ios_base::dec | ios_base::skipws);
5576 result_type __m;
5577 result_type __n;
5578 __is >> __m >> __n;
5579 if (!__is.fail())
5580 __x.param(param_type(__m, __n));
5581 return __is;
5582}
5583
Howard Hinnant551d8e42010-05-19 01:53:57 +00005584// student_t_distribution
5585
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005586template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005587class _LIBCPP_VISIBLE student_t_distribution
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005588{
5589public:
5590 // types
5591 typedef _RealType result_type;
5592
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005593 class _LIBCPP_VISIBLE param_type
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005594 {
5595 result_type __n_;
5596 public:
5597 typedef student_t_distribution distribution_type;
5598
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005600 explicit param_type(result_type __n = 1) : __n_(__n) {}
5601
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005603 result_type n() const {return __n_;}
5604
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005605 friend _LIBCPP_INLINE_VISIBILITY
5606 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005607 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005608 friend _LIBCPP_INLINE_VISIBILITY
5609 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005610 {return !(__x == __y);}
5611 };
5612
5613private:
5614 param_type __p_;
5615 normal_distribution<result_type> __nd_;
5616
5617public:
5618 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005620 explicit student_t_distribution(result_type __n = 1)
5621 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005623 explicit student_t_distribution(const param_type& __p)
5624 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005626 void reset() {__nd_.reset();}
5627
5628 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005629 template<class _URNG>
5630 _LIBCPP_INLINE_VISIBILITY
5631 result_type operator()(_URNG& __g)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005632 {return (*this)(__g, __p_);}
5633 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5634
5635 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005637 result_type n() const {return __p_.n();}
5638
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005640 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005642 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005643
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005645 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005647 result_type max() const {return numeric_limits<result_type>::infinity();}
5648
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005649 friend _LIBCPP_INLINE_VISIBILITY
5650 bool operator==(const student_t_distribution& __x,
5651 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005652 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005653 friend _LIBCPP_INLINE_VISIBILITY
5654 bool operator!=(const student_t_distribution& __x,
5655 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005656 {return !(__x == __y);}
5657};
5658
5659template <class _RealType>
5660template<class _URNG>
5661_RealType
5662student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5663{
5664 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
5665 return __nd_(__g) * _STD::sqrt(__p.n()/__gd(__g));
5666}
5667
5668template <class _CharT, class _Traits, class _RT>
5669basic_ostream<_CharT, _Traits>&
5670operator<<(basic_ostream<_CharT, _Traits>& __os,
5671 const student_t_distribution<_RT>& __x)
5672{
5673 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005674 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5675 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005676 __os << __x.n();
5677 return __os;
5678}
5679
5680template <class _CharT, class _Traits, class _RT>
5681basic_istream<_CharT, _Traits>&
5682operator>>(basic_istream<_CharT, _Traits>& __is,
5683 student_t_distribution<_RT>& __x)
5684{
5685 typedef student_t_distribution<_RT> _Eng;
5686 typedef typename _Eng::result_type result_type;
5687 typedef typename _Eng::param_type param_type;
5688 __save_flags<_CharT, _Traits> _(__is);
5689 __is.flags(ios_base::dec | ios_base::skipws);
5690 result_type __n;
5691 __is >> __n;
5692 if (!__is.fail())
5693 __x.param(param_type(__n));
5694 return __is;
5695}
5696
Howard Hinnant551d8e42010-05-19 01:53:57 +00005697// discrete_distribution
5698
5699template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005700class _LIBCPP_VISIBLE discrete_distribution
Howard Hinnant551d8e42010-05-19 01:53:57 +00005701{
5702public:
5703 // types
5704 typedef _IntType result_type;
5705
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005706 class _LIBCPP_VISIBLE param_type
Howard Hinnant551d8e42010-05-19 01:53:57 +00005707 {
5708 vector<double> __p_;
5709 public:
5710 typedef discrete_distribution distribution_type;
5711
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005713 param_type() {}
5714 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005716 param_type(_InputIterator __f, _InputIterator __l)
5717 : __p_(__f, __l) {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005718 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005719 param_type(initializer_list<double> __wl)
5720 : __p_(__wl.begin(), __wl.end()) {__init();}
5721 template<class _UnaryOperation>
5722 param_type(size_t __nw, double __xmin, double __xmax,
5723 _UnaryOperation __fw);
5724
5725 vector<double> probabilities() const;
5726
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005727 friend _LIBCPP_INLINE_VISIBILITY
5728 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005729 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005730 friend _LIBCPP_INLINE_VISIBILITY
5731 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005732 {return !(__x == __y);}
5733
5734 private:
5735 void __init();
5736
5737 friend class discrete_distribution;
5738
5739 template <class _CharT, class _Traits, class _IT>
5740 friend
5741 basic_ostream<_CharT, _Traits>&
5742 operator<<(basic_ostream<_CharT, _Traits>& __os,
5743 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005744
Howard Hinnant551d8e42010-05-19 01:53:57 +00005745 template <class _CharT, class _Traits, class _IT>
5746 friend
5747 basic_istream<_CharT, _Traits>&
5748 operator>>(basic_istream<_CharT, _Traits>& __is,
5749 discrete_distribution<_IT>& __x);
5750 };
5751
5752private:
5753 param_type __p_;
5754
5755public:
5756 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005758 discrete_distribution() {}
5759 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005761 discrete_distribution(_InputIterator __f, _InputIterator __l)
5762 : __p_(__f, __l) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005764 discrete_distribution(initializer_list<double> __wl)
5765 : __p_(__wl) {}
5766 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005768 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5769 _UnaryOperation __fw)
5770 : __p_(__nw, __xmin, __xmax, __fw) {}
5771 explicit discrete_distribution(const param_type& __p)
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005773 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005775 void reset() {}
5776
5777 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005778 template<class _URNG>
5779 _LIBCPP_INLINE_VISIBILITY
5780 result_type operator()(_URNG& __g)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005781 {return (*this)(__g, __p_);}
5782 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5783
5784 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005786 vector<double> probabilities() const {return __p_.probabilities();}
5787
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005789 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005791 void param(const param_type& __p) {__p_ = __p;}
5792
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005794 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005796 result_type max() const {return __p_.__p_.size();}
5797
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005798 friend _LIBCPP_INLINE_VISIBILITY
5799 bool operator==(const discrete_distribution& __x,
5800 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005801 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005802 friend _LIBCPP_INLINE_VISIBILITY
5803 bool operator!=(const discrete_distribution& __x,
5804 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005805 {return !(__x == __y);}
5806
5807 template <class _CharT, class _Traits, class _IT>
5808 friend
5809 basic_ostream<_CharT, _Traits>&
5810 operator<<(basic_ostream<_CharT, _Traits>& __os,
5811 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005812
Howard Hinnant551d8e42010-05-19 01:53:57 +00005813 template <class _CharT, class _Traits, class _IT>
5814 friend
5815 basic_istream<_CharT, _Traits>&
5816 operator>>(basic_istream<_CharT, _Traits>& __is,
5817 discrete_distribution<_IT>& __x);
5818};
5819
5820template<class _IntType>
5821template<class _UnaryOperation>
5822discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5823 double __xmin,
5824 double __xmax,
5825 _UnaryOperation __fw)
5826{
5827 if (__nw > 1)
5828 {
5829 __p_.reserve(__nw - 1);
5830 double __d = (__xmax - __xmin) / __nw;
5831 double __d2 = __d / 2;
5832 for (size_t __k = 0; __k < __nw; ++__k)
5833 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5834 __init();
5835 }
5836}
5837
5838template<class _IntType>
5839void
5840discrete_distribution<_IntType>::param_type::__init()
5841{
5842 if (!__p_.empty())
5843 {
5844 if (__p_.size() > 1)
5845 {
5846 double __s = _STD::accumulate(__p_.begin(), __p_.end(), 0.0);
5847 for (_STD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
5848 __i < __e; ++__i)
5849 *__i /= __s;
5850 vector<double> __t(__p_.size() - 1);
5851 _STD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
5852 swap(__p_, __t);
5853 }
5854 else
5855 {
5856 __p_.clear();
5857 __p_.shrink_to_fit();
5858 }
5859 }
5860}
5861
5862template<class _IntType>
5863vector<double>
5864discrete_distribution<_IntType>::param_type::probabilities() const
5865{
5866 size_t __n = __p_.size();
5867 _STD::vector<double> __p(__n+1);
5868 _STD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
5869 if (__n > 0)
5870 __p[__n] = 1 - __p_[__n-1];
5871 else
5872 __p[0] = 1;
5873 return __p;
5874}
5875
5876template<class _IntType>
5877template<class _URNG>
5878_IntType
5879discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
5880{
5881 uniform_real_distribution<double> __gen;
5882 return static_cast<_IntType>(
5883 _STD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
5884 __p.__p_.begin());
5885}
5886
5887template <class _CharT, class _Traits, class _IT>
5888basic_ostream<_CharT, _Traits>&
5889operator<<(basic_ostream<_CharT, _Traits>& __os,
5890 const discrete_distribution<_IT>& __x)
5891{
5892 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005893 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5894 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005895 _CharT __sp = __os.widen(' ');
5896 __os.fill(__sp);
5897 size_t __n = __x.__p_.__p_.size();
5898 __os << __n;
5899 for (size_t __i = 0; __i < __n; ++__i)
5900 __os << __sp << __x.__p_.__p_[__i];
5901 return __os;
5902}
5903
5904template <class _CharT, class _Traits, class _IT>
5905basic_istream<_CharT, _Traits>&
5906operator>>(basic_istream<_CharT, _Traits>& __is,
5907 discrete_distribution<_IT>& __x)
5908{
5909 typedef discrete_distribution<_IT> _Eng;
5910 typedef typename _Eng::result_type result_type;
5911 typedef typename _Eng::param_type param_type;
5912 __save_flags<_CharT, _Traits> _(__is);
5913 __is.flags(ios_base::dec | ios_base::skipws);
5914 size_t __n;
5915 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005916 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005917 for (size_t __i = 0; __i < __n; ++__i)
5918 __is >> __p[__i];
5919 if (!__is.fail())
5920 swap(__x.__p_.__p_, __p);
5921 return __is;
5922}
5923
Howard Hinnantd6d11712010-05-20 15:11:46 +00005924// piecewise_constant_distribution
5925
5926template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005927class _LIBCPP_VISIBLE piecewise_constant_distribution
Howard Hinnantd6d11712010-05-20 15:11:46 +00005928{
5929public:
5930 // types
5931 typedef _RealType result_type;
5932
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005933 class _LIBCPP_VISIBLE param_type
Howard Hinnantd6d11712010-05-20 15:11:46 +00005934 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00005935 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00005936 vector<result_type> __densities_;
5937 vector<result_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005938 public:
5939 typedef piecewise_constant_distribution distribution_type;
5940
5941 param_type();
5942 template<class _InputIteratorB, class _InputIteratorW>
5943 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5944 _InputIteratorW __fW);
5945 template<class _UnaryOperation>
5946 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
5947 template<class _UnaryOperation>
5948 param_type(size_t __nw, result_type __xmin, result_type __xmax,
5949 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00005950 param_type & operator=(const param_type& __rhs);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005951
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005952 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00005953 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00005955 vector<result_type> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005956
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005957 friend _LIBCPP_INLINE_VISIBILITY
5958 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:40 +00005959 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005960 friend _LIBCPP_INLINE_VISIBILITY
5961 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00005962 {return !(__x == __y);}
5963
5964 private:
5965 void __init();
5966
5967 friend class piecewise_constant_distribution;
5968
5969 template <class _CharT, class _Traits, class _RT>
5970 friend
5971 basic_ostream<_CharT, _Traits>&
5972 operator<<(basic_ostream<_CharT, _Traits>& __os,
5973 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005974
Howard Hinnantd6d11712010-05-20 15:11:46 +00005975 template <class _CharT, class _Traits, class _RT>
5976 friend
5977 basic_istream<_CharT, _Traits>&
5978 operator>>(basic_istream<_CharT, _Traits>& __is,
5979 piecewise_constant_distribution<_RT>& __x);
5980 };
5981
5982private:
5983 param_type __p_;
5984
5985public:
5986 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00005988 piecewise_constant_distribution() {}
5989 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00005991 piecewise_constant_distribution(_InputIteratorB __fB,
5992 _InputIteratorB __lB,
5993 _InputIteratorW __fW)
5994 : __p_(__fB, __lB, __fW) {}
5995
5996 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00005998 piecewise_constant_distribution(initializer_list<result_type> __bl,
5999 _UnaryOperation __fw)
6000 : __p_(__bl, __fw) {}
6001
6002 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006004 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6005 result_type __xmax, _UnaryOperation __fw)
6006 : __p_(__nw, __xmin, __xmax, __fw) {}
6007
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006009 explicit piecewise_constant_distribution(const param_type& __p)
6010 : __p_(__p) {}
6011
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006012 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006013 void reset() {}
6014
6015 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006016 template<class _URNG>
6017 _LIBCPP_INLINE_VISIBILITY
6018 result_type operator()(_URNG& __g)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006019 {return (*this)(__g, __p_);}
6020 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6021
6022 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006024 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006026 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnantd6d11712010-05-20 15:11:46 +00006027
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006028 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006029 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006031 void param(const param_type& __p) {__p_ = __p;}
6032
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006034 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006036 result_type max() const {return __p_.__b_.back();}
6037
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006038 friend _LIBCPP_INLINE_VISIBILITY
6039 bool operator==(const piecewise_constant_distribution& __x,
6040 const piecewise_constant_distribution& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006041 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006042 friend _LIBCPP_INLINE_VISIBILITY
6043 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnantd6d11712010-05-20 15:11:46 +00006044 const piecewise_constant_distribution& __y)
6045 {return !(__x == __y);}
6046
6047 template <class _CharT, class _Traits, class _RT>
6048 friend
6049 basic_ostream<_CharT, _Traits>&
6050 operator<<(basic_ostream<_CharT, _Traits>& __os,
6051 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006052
Howard Hinnantd6d11712010-05-20 15:11:46 +00006053 template <class _CharT, class _Traits, class _RT>
6054 friend
6055 basic_istream<_CharT, _Traits>&
6056 operator>>(basic_istream<_CharT, _Traits>& __is,
6057 piecewise_constant_distribution<_RT>& __x);
6058};
6059
6060template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006061typename piecewise_constant_distribution<_RealType>::param_type &
6062piecewise_constant_distribution<_RealType>::param_type::operator=
6063 (const param_type& __rhs)
6064{
6065// These can throw
6066 __b_.reserve (__rhs.__b_.size ());
6067 __densities_.reserve(__rhs.__densities_.size());
6068 __areas_.reserve (__rhs.__areas_.size());
6069
6070// These can not throw
6071 __b_ = __rhs.__b_;
6072 __densities_ = __rhs.__densities_;
6073 __areas_ = __rhs.__areas_;
6074 return *this;
6075}
6076
6077template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006078void
6079piecewise_constant_distribution<_RealType>::param_type::__init()
6080{
Howard Hinnant2a592542010-05-24 00:35:40 +00006081 // __densities_ contains non-normalized areas
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006082 result_type __total_area = _STD::accumulate(__densities_.begin(),
Howard Hinnant2a592542010-05-24 00:35:40 +00006083 __densities_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006084 result_type());
Howard Hinnant2a592542010-05-24 00:35:40 +00006085 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6086 __densities_[__i] /= __total_area;
6087 // __densities_ contains normalized areas
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006088 __areas_.assign(__densities_.size(), result_type());
Howard Hinnant2a592542010-05-24 00:35:40 +00006089 _STD::partial_sum(__densities_.begin(), __densities_.end() - 1,
6090 __areas_.begin() + 1);
6091 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6092 __densities_.back() = 1 - __areas_.back(); // correct round off error
6093 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6094 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6095 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:46 +00006096}
6097
6098template<class _RealType>
6099piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:40 +00006100 : __b_(2),
Howard Hinnant54305402010-05-25 00:27:34 +00006101 __densities_(1, 1.0),
6102 __areas_(1, 0.0)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006103{
6104 __b_[1] = 1;
6105}
6106
6107template<class _RealType>
6108template<class _InputIteratorB, class _InputIteratorW>
6109piecewise_constant_distribution<_RealType>::param_type::param_type(
6110 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6111 : __b_(__fB, __lB)
6112{
6113 if (__b_.size() < 2)
6114 {
6115 __b_.resize(2);
6116 __b_[0] = 0;
6117 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006118 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006119 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006120 }
6121 else
6122 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006123 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006124 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:40 +00006125 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006126 __init();
6127 }
6128}
6129
6130template<class _RealType>
6131template<class _UnaryOperation>
6132piecewise_constant_distribution<_RealType>::param_type::param_type(
6133 initializer_list<result_type> __bl, _UnaryOperation __fw)
6134 : __b_(__bl.begin(), __bl.end())
6135{
6136 if (__b_.size() < 2)
6137 {
6138 __b_.resize(2);
6139 __b_[0] = 0;
6140 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006141 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006142 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006143 }
6144 else
6145 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006146 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006147 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006148 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006149 __init();
6150 }
6151}
6152
6153template<class _RealType>
6154template<class _UnaryOperation>
6155piecewise_constant_distribution<_RealType>::param_type::param_type(
6156 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6157 : __b_(__nw == 0 ? 2 : __nw + 1)
6158{
6159 size_t __n = __b_.size() - 1;
6160 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:40 +00006161 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006162 for (size_t __i = 0; __i < __n; ++__i)
6163 {
6164 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:40 +00006165 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006166 }
6167 __b_[__n] = __xmax;
6168 __init();
6169}
6170
6171template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006172template<class _URNG>
6173_RealType
6174piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6175{
6176 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006177 result_type __u = _Gen()(__g);
Howard Hinnant2a592542010-05-24 00:35:40 +00006178 ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006179 __u) - __p.__areas_.begin() - 1;
6180 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006181}
6182
6183template <class _CharT, class _Traits, class _RT>
6184basic_ostream<_CharT, _Traits>&
6185operator<<(basic_ostream<_CharT, _Traits>& __os,
6186 const piecewise_constant_distribution<_RT>& __x)
6187{
6188 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00006189 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6190 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006191 _CharT __sp = __os.widen(' ');
6192 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:40 +00006193 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006194 __os << __n;
6195 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006196 __os << __sp << __x.__p_.__b_[__i];
6197 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006198 __os << __sp << __n;
6199 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006200 __os << __sp << __x.__p_.__densities_[__i];
6201 __n = __x.__p_.__areas_.size();
6202 __os << __sp << __n;
6203 for (size_t __i = 0; __i < __n; ++__i)
6204 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006205 return __os;
6206}
6207
6208template <class _CharT, class _Traits, class _RT>
6209basic_istream<_CharT, _Traits>&
6210operator>>(basic_istream<_CharT, _Traits>& __is,
6211 piecewise_constant_distribution<_RT>& __x)
6212{
6213 typedef piecewise_constant_distribution<_RT> _Eng;
6214 typedef typename _Eng::result_type result_type;
6215 typedef typename _Eng::param_type param_type;
6216 __save_flags<_CharT, _Traits> _(__is);
6217 __is.flags(ios_base::dec | ios_base::skipws);
6218 size_t __n;
6219 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006220 vector<result_type> __b(__n);
6221 for (size_t __i = 0; __i < __n; ++__i)
6222 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:40 +00006223 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006224 vector<result_type> __densities(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006225 for (size_t __i = 0; __i < __n; ++__i)
6226 __is >> __densities[__i];
6227 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006228 vector<result_type> __areas(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006229 for (size_t __i = 0; __i < __n; ++__i)
6230 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006231 if (!__is.fail())
6232 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00006233 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:40 +00006234 swap(__x.__p_.__densities_, __densities);
6235 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006236 }
6237 return __is;
6238}
6239
Howard Hinnant54305402010-05-25 00:27:34 +00006240// piecewise_linear_distribution
6241
6242template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006243class _LIBCPP_VISIBLE piecewise_linear_distribution
Howard Hinnant54305402010-05-25 00:27:34 +00006244{
6245public:
6246 // types
6247 typedef _RealType result_type;
6248
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006249 class _LIBCPP_VISIBLE param_type
Howard Hinnant54305402010-05-25 00:27:34 +00006250 {
Howard Hinnant54305402010-05-25 00:27:34 +00006251 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006252 vector<result_type> __densities_;
6253 vector<result_type> __areas_;
Howard Hinnant54305402010-05-25 00:27:34 +00006254 public:
6255 typedef piecewise_linear_distribution distribution_type;
6256
6257 param_type();
6258 template<class _InputIteratorB, class _InputIteratorW>
6259 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6260 _InputIteratorW __fW);
6261 template<class _UnaryOperation>
6262 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
6263 template<class _UnaryOperation>
6264 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6265 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006266 param_type & operator=(const param_type& __rhs);
6267
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006269 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006271 vector<result_type> densities() const {return __densities_;}
Howard Hinnant54305402010-05-25 00:27:34 +00006272
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006273 friend _LIBCPP_INLINE_VISIBILITY
6274 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006275 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006276 friend _LIBCPP_INLINE_VISIBILITY
6277 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006278 {return !(__x == __y);}
6279
6280 private:
6281 void __init();
6282
6283 friend class piecewise_linear_distribution;
6284
6285 template <class _CharT, class _Traits, class _RT>
6286 friend
6287 basic_ostream<_CharT, _Traits>&
6288 operator<<(basic_ostream<_CharT, _Traits>& __os,
6289 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006290
Howard Hinnant54305402010-05-25 00:27:34 +00006291 template <class _CharT, class _Traits, class _RT>
6292 friend
6293 basic_istream<_CharT, _Traits>&
6294 operator>>(basic_istream<_CharT, _Traits>& __is,
6295 piecewise_linear_distribution<_RT>& __x);
6296 };
6297
6298private:
6299 param_type __p_;
6300
6301public:
6302 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006304 piecewise_linear_distribution() {}
6305 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006307 piecewise_linear_distribution(_InputIteratorB __fB,
6308 _InputIteratorB __lB,
6309 _InputIteratorW __fW)
6310 : __p_(__fB, __lB, __fW) {}
Howard Hinnant324bb032010-08-22 00:02:43 +00006311
Howard Hinnant54305402010-05-25 00:27:34 +00006312 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006314 piecewise_linear_distribution(initializer_list<result_type> __bl,
6315 _UnaryOperation __fw)
6316 : __p_(__bl, __fw) {}
6317
6318 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006320 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6321 result_type __xmax, _UnaryOperation __fw)
6322 : __p_(__nw, __xmin, __xmax, __fw) {}
6323
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006325 explicit piecewise_linear_distribution(const param_type& __p)
6326 : __p_(__p) {}
6327
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006329 void reset() {}
6330
6331 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006332 template<class _URNG>
6333 _LIBCPP_INLINE_VISIBILITY
6334 result_type operator()(_URNG& __g)
Howard Hinnant54305402010-05-25 00:27:34 +00006335 {return (*this)(__g, __p_);}
6336 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6337
6338 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006340 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006342 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant54305402010-05-25 00:27:34 +00006343
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006345 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006347 void param(const param_type& __p) {__p_ = __p;}
6348
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006350 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006352 result_type max() const {return __p_.__b_.back();}
6353
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006354 friend _LIBCPP_INLINE_VISIBILITY
6355 bool operator==(const piecewise_linear_distribution& __x,
6356 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006357 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006358 friend _LIBCPP_INLINE_VISIBILITY
6359 bool operator!=(const piecewise_linear_distribution& __x,
6360 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006361 {return !(__x == __y);}
6362
6363 template <class _CharT, class _Traits, class _RT>
6364 friend
6365 basic_ostream<_CharT, _Traits>&
6366 operator<<(basic_ostream<_CharT, _Traits>& __os,
6367 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006368
Howard Hinnant54305402010-05-25 00:27:34 +00006369 template <class _CharT, class _Traits, class _RT>
6370 friend
6371 basic_istream<_CharT, _Traits>&
6372 operator>>(basic_istream<_CharT, _Traits>& __is,
6373 piecewise_linear_distribution<_RT>& __x);
6374};
6375
6376template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006377typename piecewise_linear_distribution<_RealType>::param_type &
6378piecewise_linear_distribution<_RealType>::param_type::operator=
6379 (const param_type& __rhs)
6380{
6381// These can throw
6382 __b_.reserve (__rhs.__b_.size ());
6383 __densities_.reserve(__rhs.__densities_.size());
6384 __areas_.reserve (__rhs.__areas_.size());
6385
6386// These can not throw
6387 __b_ = __rhs.__b_;
6388 __densities_ = __rhs.__densities_;
6389 __areas_ = __rhs.__areas_;
6390 return *this;
6391}
6392
6393
6394template<class _RealType>
Howard Hinnant54305402010-05-25 00:27:34 +00006395void
6396piecewise_linear_distribution<_RealType>::param_type::__init()
6397{
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006398 __areas_.assign(__densities_.size() - 1, result_type());
6399 result_type _S = 0;
Howard Hinnant54305402010-05-25 00:27:34 +00006400 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6401 {
6402 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6403 (__b_[__i+1] - __b_[__i]) * .5;
6404 _S += __areas_[__i];
6405 }
6406 for (size_t __i = __areas_.size(); __i > 1;)
6407 {
6408 --__i;
6409 __areas_[__i] = __areas_[__i-1] / _S;
6410 }
6411 __areas_[0] = 0;
6412 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6413 __areas_[__i] += __areas_[__i-1];
6414 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6415 __densities_[__i] /= _S;
6416}
6417
6418template<class _RealType>
6419piecewise_linear_distribution<_RealType>::param_type::param_type()
6420 : __b_(2),
6421 __densities_(2, 1.0),
6422 __areas_(1, 0.0)
6423{
6424 __b_[1] = 1;
6425}
6426
6427template<class _RealType>
6428template<class _InputIteratorB, class _InputIteratorW>
6429piecewise_linear_distribution<_RealType>::param_type::param_type(
6430 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6431 : __b_(__fB, __lB)
6432{
6433 if (__b_.size() < 2)
6434 {
6435 __b_.resize(2);
6436 __b_[0] = 0;
6437 __b_[1] = 1;
6438 __densities_.assign(2, 1.0);
6439 __areas_.assign(1, 0.0);
6440 }
6441 else
6442 {
6443 __densities_.reserve(__b_.size());
6444 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6445 __densities_.push_back(*__fW);
6446 __init();
6447 }
6448}
6449
6450template<class _RealType>
6451template<class _UnaryOperation>
6452piecewise_linear_distribution<_RealType>::param_type::param_type(
6453 initializer_list<result_type> __bl, _UnaryOperation __fw)
6454 : __b_(__bl.begin(), __bl.end())
6455{
6456 if (__b_.size() < 2)
6457 {
6458 __b_.resize(2);
6459 __b_[0] = 0;
6460 __b_[1] = 1;
6461 __densities_.assign(2, 1.0);
6462 __areas_.assign(1, 0.0);
6463 }
6464 else
6465 {
6466 __densities_.reserve(__b_.size());
6467 for (size_t __i = 0; __i < __b_.size(); ++__i)
6468 __densities_.push_back(__fw(__b_[__i]));
6469 __init();
6470 }
6471}
6472
6473template<class _RealType>
6474template<class _UnaryOperation>
6475piecewise_linear_distribution<_RealType>::param_type::param_type(
6476 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6477 : __b_(__nw == 0 ? 2 : __nw + 1)
6478{
6479 size_t __n = __b_.size() - 1;
6480 result_type __d = (__xmax - __xmin) / __n;
6481 __densities_.reserve(__b_.size());
6482 for (size_t __i = 0; __i < __n; ++__i)
6483 {
6484 __b_[__i] = __xmin + __i * __d;
6485 __densities_.push_back(__fw(__b_[__i]));
6486 }
6487 __b_[__n] = __xmax;
6488 __densities_.push_back(__fw(__b_[__n]));
6489 __init();
6490}
6491
6492template<class _RealType>
6493template<class _URNG>
6494_RealType
6495piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6496{
6497 typedef uniform_real_distribution<result_type> _Gen;
6498 result_type __u = _Gen()(__g);
6499 ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006500 __u) - __p.__areas_.begin() - 1;
Howard Hinnant54305402010-05-25 00:27:34 +00006501 __u -= __p.__areas_[__k];
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006502 const result_type __dk = __p.__densities_[__k];
6503 const result_type __dk1 = __p.__densities_[__k+1];
6504 const result_type __deltad = __dk1 - __dk;
Howard Hinnant54305402010-05-25 00:27:34 +00006505 const result_type __bk = __p.__b_[__k];
6506 if (__deltad == 0)
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006507 return __u / __dk + __bk;
Howard Hinnant54305402010-05-25 00:27:34 +00006508 const result_type __bk1 = __p.__b_[__k+1];
6509 const result_type __deltab = __bk1 - __bk;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006510 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnant54305402010-05-25 00:27:34 +00006511 _STD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006512 __deltad;
Howard Hinnant54305402010-05-25 00:27:34 +00006513}
6514
6515template <class _CharT, class _Traits, class _RT>
6516basic_ostream<_CharT, _Traits>&
6517operator<<(basic_ostream<_CharT, _Traits>& __os,
6518 const piecewise_linear_distribution<_RT>& __x)
6519{
6520 __save_flags<_CharT, _Traits> _(__os);
6521 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6522 ios_base::scientific);
6523 _CharT __sp = __os.widen(' ');
6524 __os.fill(__sp);
6525 size_t __n = __x.__p_.__b_.size();
6526 __os << __n;
6527 for (size_t __i = 0; __i < __n; ++__i)
6528 __os << __sp << __x.__p_.__b_[__i];
6529 __n = __x.__p_.__densities_.size();
6530 __os << __sp << __n;
6531 for (size_t __i = 0; __i < __n; ++__i)
6532 __os << __sp << __x.__p_.__densities_[__i];
6533 __n = __x.__p_.__areas_.size();
6534 __os << __sp << __n;
6535 for (size_t __i = 0; __i < __n; ++__i)
6536 __os << __sp << __x.__p_.__areas_[__i];
6537 return __os;
6538}
6539
6540template <class _CharT, class _Traits, class _RT>
6541basic_istream<_CharT, _Traits>&
6542operator>>(basic_istream<_CharT, _Traits>& __is,
6543 piecewise_linear_distribution<_RT>& __x)
6544{
6545 typedef piecewise_linear_distribution<_RT> _Eng;
6546 typedef typename _Eng::result_type result_type;
6547 typedef typename _Eng::param_type param_type;
Howard Hinnant54305402010-05-25 00:27:34 +00006548 __save_flags<_CharT, _Traits> _(__is);
6549 __is.flags(ios_base::dec | ios_base::skipws);
6550 size_t __n;
6551 __is >> __n;
6552 vector<result_type> __b(__n);
6553 for (size_t __i = 0; __i < __n; ++__i)
6554 __is >> __b[__i];
6555 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006556 vector<result_type> __densities(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006557 for (size_t __i = 0; __i < __n; ++__i)
6558 __is >> __densities[__i];
6559 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006560 vector<result_type> __areas(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006561 for (size_t __i = 0; __i < __n; ++__i)
6562 __is >> __areas[__i];
6563 if (!__is.fail())
6564 {
6565 swap(__x.__p_.__b_, __b);
6566 swap(__x.__p_.__densities_, __densities);
6567 swap(__x.__p_.__areas_, __areas);
6568 }
6569 return __is;
6570}
6571
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00006572_LIBCPP_END_NAMESPACE_STD
6573
6574#endif // _LIBCPP_RANDOM