blob: ac7b04dbc07490df1780d042c76b79377549df7f [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_)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002316 return _VSTD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002317 if (__x.__i_ == 0 || __y.__i_ == 0)
2318 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002319 size_t __j = _VSTD::min(_N - __x.__i_, _N - __y.__i_);
2320 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002321 __y.__x_ + __y.__i_))
2322 return false;
2323 if (__x.__i_ == 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002324 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
2325 return _VSTD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002326 }
2327 if (__x.__i_ < __y.__i_)
2328 {
2329 size_t __j = _N - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002330 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331 __y.__x_ + __y.__i_))
2332 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002333 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002334 __y.__x_))
2335 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002336 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002337 __y.__x_ + (_N - (__x.__i_ + __j)));
2338 }
2339 size_t __j = _N - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002340 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341 __x.__x_ + __x.__i_))
2342 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002343 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 __x.__x_))
2345 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002346 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347 __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_)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002624 return _VSTD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002625 if (__x.__i_ == 0 || __y.__i_ == 0)
2626 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002627 size_t __j = _VSTD::min(_R - __x.__i_, _R - __y.__i_);
2628 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002629 __y.__x_ + __y.__i_))
2630 return false;
2631 if (__x.__i_ == 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002632 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
2633 return _VSTD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002634 }
2635 if (__x.__i_ < __y.__i_)
2636 {
2637 size_t __j = _R - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002638 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639 __y.__x_ + __y.__i_))
2640 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002641 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002642 __y.__x_))
2643 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002644 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645 __y.__x_ + (_R - (__x.__i_ + __j)));
2646 }
2647 size_t __j = _R - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002648 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649 __x.__x_ + __x.__i_))
2650 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002651 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652 __x.__x_))
2653 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002654 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002655 __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)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002747 : __e_(_VSTD::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)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002947 : __e_(_VSTD::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)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003170 : __e_(_VSTD::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{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003295 return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003296 __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() {}
Howard Hinnante3e32912011-08-12 21:56:02 +00003402#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003403 template<class _Tp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003405 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00003406#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant324bb032010-08-22 00:02:43 +00003407
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003408 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003410 seed_seq(_InputIterator __first, _InputIterator __last)
3411 {init(__first, __last);}
3412
3413 // generating functions
3414 template<class _RandomAccessIterator>
3415 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3416
3417 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003419 size_t size() const {return __v_.size();}
3420 template<class _OutputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003422 void param(_OutputIterator __dest) const
Howard Hinnant0949eed2011-06-30 21:18:19 +00003423 {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003424
3425private:
3426 // no copy functions
3427 seed_seq(const seed_seq&); // = delete;
3428 void operator=(const seed_seq&); // = delete;
3429
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003431 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
3432};
3433
3434template<class _InputIterator>
3435void
3436seed_seq::init(_InputIterator __first, _InputIterator __last)
3437{
3438 for (_InputIterator __s = __first; __s != __last; ++__s)
3439 __v_.push_back(*__s & 0xFFFFFFFF);
3440}
3441
3442template<class _RandomAccessIterator>
3443void
3444seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3445{
3446 if (__first != __last)
3447 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003448 _VSTD::fill(__first, __last, 0x8b8b8b8b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003449 const size_t __n = static_cast<size_t>(__last - __first);
3450 const size_t __s = __v_.size();
3451 const size_t __t = (__n >= 623) ? 11
3452 : (__n >= 68) ? 7
3453 : (__n >= 39) ? 5
3454 : (__n >= 7) ? 3
3455 : (__n - 1) / 2;
3456 const size_t __p = (__n - __t) / 2;
3457 const size_t __q = __p + __t;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003458 const size_t __m = _VSTD::max(__s + 1, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003459 // __k = 0;
3460 {
3461 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
3462 ^ __first[__n - 1]);
3463 __first[__p] += __r;
3464 __r += __s;
3465 __first[__q] += __r;
3466 __first[0] = __r;
3467 }
3468 for (size_t __k = 1; __k <= __s; ++__k)
3469 {
3470 const size_t __kmodn = __k % __n;
3471 const size_t __kpmodn = (__k + __p) % __n;
3472 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3473 ^ __first[(__k - 1) % __n]);
3474 __first[__kpmodn] += __r;
3475 __r += __kmodn + __v_[__k-1];
3476 __first[(__k + __q) % __n] += __r;
3477 __first[__kmodn] = __r;
3478 }
3479 for (size_t __k = __s + 1; __k < __m; ++__k)
3480 {
3481 const size_t __kmodn = __k % __n;
3482 const size_t __kpmodn = (__k + __p) % __n;
3483 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3484 ^ __first[(__k - 1) % __n]);
3485 __first[__kpmodn] += __r;
3486 __r += __kmodn;
3487 __first[(__k + __q) % __n] += __r;
3488 __first[__kmodn] = __r;
3489 }
3490 for (size_t __k = __m; __k < __m + __n; ++__k)
3491 {
3492 const size_t __kmodn = __k % __n;
3493 const size_t __kpmodn = (__k + __p) % __n;
3494 result_type __r = 1566083941 * _T(__first[__kmodn] +
3495 __first[__kpmodn] +
3496 __first[(__k - 1) % __n]);
3497 __first[__kpmodn] ^= __r;
3498 __r -= __kmodn;
3499 __first[(__k + __q) % __n] ^= __r;
3500 __first[__kmodn] = __r;
3501 }
3502 }
3503}
3504
Howard Hinnant30a840f2010-05-12 17:08:57 +00003505// generate_canonical
3506
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003507template<class _RealType, size_t __bits, class _URNG>
3508_RealType
3509generate_canonical(_URNG& __g)
3510{
3511 const size_t _Dt = numeric_limits<_RealType>::digits;
3512 const size_t __b = _Dt < __bits ? _Dt : __bits;
3513 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
3514 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
3515 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
3516 _RealType __base = _R;
3517 _RealType _S = __g() - _URNG::_Min;
3518 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
3519 _S += (__g() - _URNG::_Min) * __base;
3520 return _S / __base;
3521}
3522
Howard Hinnant30a840f2010-05-12 17:08:57 +00003523// uniform_int_distribution
3524
Howard Hinnantc3267212010-05-26 17:49:34 +00003525// in <algorithm>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003526
3527template <class _CharT, class _Traits, class _IT>
3528basic_ostream<_CharT, _Traits>&
3529operator<<(basic_ostream<_CharT, _Traits>& __os,
3530 const uniform_int_distribution<_IT>& __x)
3531{
3532 __save_flags<_CharT, _Traits> _(__os);
3533 __os.flags(ios_base::dec | ios_base::left);
3534 _CharT __sp = __os.widen(' ');
3535 __os.fill(__sp);
3536 return __os << __x.a() << __sp << __x.b();
3537}
3538
3539template <class _CharT, class _Traits, class _IT>
3540basic_istream<_CharT, _Traits>&
3541operator>>(basic_istream<_CharT, _Traits>& __is,
3542 uniform_int_distribution<_IT>& __x)
3543{
3544 typedef uniform_int_distribution<_IT> _Eng;
3545 typedef typename _Eng::result_type result_type;
3546 typedef typename _Eng::param_type param_type;
3547 __save_flags<_CharT, _Traits> _(__is);
3548 __is.flags(ios_base::dec | ios_base::skipws);
3549 result_type __a;
3550 result_type __b;
3551 __is >> __a >> __b;
3552 if (!__is.fail())
3553 __x.param(param_type(__a, __b));
3554 return __is;
3555}
3556
Howard Hinnant30a840f2010-05-12 17:08:57 +00003557// uniform_real_distribution
3558
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003559template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003560class _LIBCPP_VISIBLE uniform_real_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003561{
3562public:
3563 // types
3564 typedef _RealType result_type;
3565
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003566 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003567 {
3568 result_type __a_;
3569 result_type __b_;
3570 public:
3571 typedef uniform_real_distribution distribution_type;
3572
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003574 explicit param_type(result_type __a = 0,
3575 result_type __b = 1)
3576 : __a_(__a), __b_(__b) {}
3577
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003579 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003581 result_type b() const {return __b_;}
3582
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003583 friend _LIBCPP_INLINE_VISIBILITY
3584 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003585 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003586 friend _LIBCPP_INLINE_VISIBILITY
3587 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003588 {return !(__x == __y);}
3589 };
3590
3591private:
3592 param_type __p_;
3593
3594public:
3595 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003597 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3598 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003602 void reset() {}
3603
3604 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003605 template<class _URNG>
3606 _LIBCPP_INLINE_VISIBILITY
3607 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003608 {return (*this)(__g, __p_);}
3609 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3610
3611 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003613 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003614 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003615 result_type b() const {return __p_.b();}
3616
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003618 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003620 void param(const param_type& __p) {__p_ = __p;}
3621
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623 result_type min() const {return a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003625 result_type max() const {return b();}
3626
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003627 friend _LIBCPP_INLINE_VISIBILITY
3628 bool operator==(const uniform_real_distribution& __x,
3629 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003631 friend _LIBCPP_INLINE_VISIBILITY
3632 bool operator!=(const uniform_real_distribution& __x,
3633 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003634 {return !(__x == __y);}
3635};
3636
3637template<class _RealType>
3638template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003639inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003640typename uniform_real_distribution<_RealType>::result_type
3641uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3642{
3643 return (__p.b() - __p.a())
Howard Hinnant0949eed2011-06-30 21:18:19 +00003644 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645 + __p.a();
3646}
3647
3648template <class _CharT, class _Traits, class _RT>
3649basic_ostream<_CharT, _Traits>&
3650operator<<(basic_ostream<_CharT, _Traits>& __os,
3651 const uniform_real_distribution<_RT>& __x)
3652{
3653 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003654 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3655 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656 _CharT __sp = __os.widen(' ');
3657 __os.fill(__sp);
3658 return __os << __x.a() << __sp << __x.b();
3659}
3660
3661template <class _CharT, class _Traits, class _RT>
3662basic_istream<_CharT, _Traits>&
3663operator>>(basic_istream<_CharT, _Traits>& __is,
3664 uniform_real_distribution<_RT>& __x)
3665{
3666 typedef uniform_real_distribution<_RT> _Eng;
3667 typedef typename _Eng::result_type result_type;
3668 typedef typename _Eng::param_type param_type;
3669 __save_flags<_CharT, _Traits> _(__is);
3670 __is.flags(ios_base::dec | ios_base::skipws);
3671 result_type __a;
3672 result_type __b;
3673 __is >> __a >> __b;
3674 if (!__is.fail())
3675 __x.param(param_type(__a, __b));
3676 return __is;
3677}
3678
Howard Hinnant30a840f2010-05-12 17:08:57 +00003679// bernoulli_distribution
3680
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003681class _LIBCPP_VISIBLE bernoulli_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003682{
3683public:
3684 // types
3685 typedef bool result_type;
3686
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003687 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003688 {
3689 double __p_;
3690 public:
3691 typedef bernoulli_distribution distribution_type;
3692
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003694 explicit param_type(double __p = 0.5) : __p_(__p) {}
3695
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003697 double p() const {return __p_;}
3698
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003699 friend _LIBCPP_INLINE_VISIBILITY
3700 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003701 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003702 friend _LIBCPP_INLINE_VISIBILITY
3703 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003704 {return !(__x == __y);}
3705 };
3706
3707private:
3708 param_type __p_;
3709
3710public:
3711 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003713 explicit bernoulli_distribution(double __p = 0.5)
3714 : __p_(param_type(__p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003716 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003718 void reset() {}
3719
3720 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003721 template<class _URNG>
3722 _LIBCPP_INLINE_VISIBILITY
3723 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003724 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003725 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003726
3727 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003729 double p() const {return __p_.p();}
3730
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003732 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003734 void param(const param_type& __p) {__p_ = __p;}
3735
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003737 result_type min() const {return false;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003739 result_type max() const {return true;}
3740
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003741 friend _LIBCPP_INLINE_VISIBILITY
3742 bool operator==(const bernoulli_distribution& __x,
3743 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003744 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003745 friend _LIBCPP_INLINE_VISIBILITY
3746 bool operator!=(const bernoulli_distribution& __x,
3747 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003748 {return !(__x == __y);}
3749};
3750
Howard Hinnant03aad812010-05-11 23:26:59 +00003751template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003752inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003753bernoulli_distribution::result_type
3754bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3755{
Howard Hinnantd6d11712010-05-20 15:11:46 +00003756 uniform_real_distribution<double> __gen;
3757 return __gen(__g) < __p.p();
Howard Hinnant03aad812010-05-11 23:26:59 +00003758}
3759
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003760template <class _CharT, class _Traits>
3761basic_ostream<_CharT, _Traits>&
3762operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3763{
3764 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003765 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3766 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003767 _CharT __sp = __os.widen(' ');
3768 __os.fill(__sp);
3769 return __os << __x.p();
3770}
3771
3772template <class _CharT, class _Traits>
3773basic_istream<_CharT, _Traits>&
3774operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3775{
3776 typedef bernoulli_distribution _Eng;
3777 typedef typename _Eng::param_type param_type;
3778 __save_flags<_CharT, _Traits> _(__is);
3779 __is.flags(ios_base::dec | ios_base::skipws);
3780 double __p;
3781 __is >> __p;
3782 if (!__is.fail())
3783 __x.param(param_type(__p));
3784 return __is;
3785}
3786
Howard Hinnant30a840f2010-05-12 17:08:57 +00003787// binomial_distribution
3788
Howard Hinnant03aad812010-05-11 23:26:59 +00003789template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003790class _LIBCPP_VISIBLE binomial_distribution
Howard Hinnant03aad812010-05-11 23:26:59 +00003791{
3792public:
3793 // types
3794 typedef _IntType result_type;
3795
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003796 class _LIBCPP_VISIBLE param_type
Howard Hinnant03aad812010-05-11 23:26:59 +00003797 {
3798 result_type __t_;
3799 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003800 double __pr_;
3801 double __odds_ratio_;
3802 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003803 public:
3804 typedef binomial_distribution distribution_type;
3805
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003806 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003807
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003809 result_type t() const {return __t_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003811 double p() const {return __p_;}
3812
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003813 friend _LIBCPP_INLINE_VISIBILITY
3814 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003815 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003816 friend _LIBCPP_INLINE_VISIBILITY
3817 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003818 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003819
3820 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003821 };
3822
3823private:
3824 param_type __p_;
3825
3826public:
3827 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003829 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3830 : __p_(param_type(__t, __p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003832 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003834 void reset() {}
3835
3836 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003837 template<class _URNG>
3838 _LIBCPP_INLINE_VISIBILITY
3839 result_type operator()(_URNG& __g)
Howard Hinnant03aad812010-05-11 23:26:59 +00003840 {return (*this)(__g, __p_);}
3841 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3842
3843 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003845 result_type t() const {return __p_.t();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003847 double p() const {return __p_.p();}
3848
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003850 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003852 void param(const param_type& __p) {__p_ = __p;}
3853
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003855 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003857 result_type max() const {return t();}
3858
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003859 friend _LIBCPP_INLINE_VISIBILITY
3860 bool operator==(const binomial_distribution& __x,
3861 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003862 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003863 friend _LIBCPP_INLINE_VISIBILITY
3864 bool operator!=(const binomial_distribution& __x,
3865 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003866 {return !(__x == __y);}
3867};
3868
3869template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003870binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3871 : __t_(__t), __p_(__p)
3872{
3873 if (0 < __p_ && __p_ < 1)
3874 {
3875 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003876 __pr_ = _VSTD::exp(_VSTD::lgamma(__t_ + 1.) - _VSTD::lgamma(__r0_ + 1.) -
3877 _VSTD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
3878 (__t_ - __r0_) * _VSTD::log(1 - __p_));
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003879 __odds_ratio_ = __p_ / (1 - __p_);
3880 }
3881}
3882
3883template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00003884template<class _URNG>
3885_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003886binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00003887{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003888 if (__pr.__t_ == 0 || __pr.__p_ == 0)
3889 return 0;
3890 if (__pr.__p_ == 1)
3891 return __pr.__t_;
3892 uniform_real_distribution<double> __gen;
3893 double __u = __gen(__g) - __pr.__pr_;
3894 if (__u < 0)
3895 return __pr.__r0_;
3896 double __pu = __pr.__pr_;
3897 double __pd = __pu;
3898 result_type __ru = __pr.__r0_;
3899 result_type __rd = __ru;
3900 while (true)
3901 {
3902 if (__rd >= 1)
3903 {
3904 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3905 __u -= __pd;
3906 if (__u < 0)
3907 return __rd - 1;
3908 }
3909 --__rd;
3910 ++__ru;
3911 if (__ru <= __pr.__t_)
3912 {
3913 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3914 __u -= __pu;
3915 if (__u < 0)
3916 return __ru;
3917 }
3918 }
Howard Hinnant03aad812010-05-11 23:26:59 +00003919}
3920
3921template <class _CharT, class _Traits, class _IntType>
3922basic_ostream<_CharT, _Traits>&
3923operator<<(basic_ostream<_CharT, _Traits>& __os,
3924 const binomial_distribution<_IntType>& __x)
3925{
3926 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003927 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3928 ios_base::scientific);
Howard Hinnant03aad812010-05-11 23:26:59 +00003929 _CharT __sp = __os.widen(' ');
3930 __os.fill(__sp);
3931 return __os << __x.t() << __sp << __x.p();
3932}
3933
3934template <class _CharT, class _Traits, class _IntType>
3935basic_istream<_CharT, _Traits>&
3936operator>>(basic_istream<_CharT, _Traits>& __is,
3937 binomial_distribution<_IntType>& __x)
3938{
3939 typedef binomial_distribution<_IntType> _Eng;
3940 typedef typename _Eng::result_type result_type;
3941 typedef typename _Eng::param_type param_type;
3942 __save_flags<_CharT, _Traits> _(__is);
3943 __is.flags(ios_base::dec | ios_base::skipws);
3944 result_type __t;
3945 double __p;
3946 __is >> __t >> __p;
3947 if (!__is.fail())
3948 __x.param(param_type(__t, __p));
3949 return __is;
3950}
3951
Howard Hinnant30a840f2010-05-12 17:08:57 +00003952// exponential_distribution
3953
3954template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003955class _LIBCPP_VISIBLE exponential_distribution
Howard Hinnant30a840f2010-05-12 17:08:57 +00003956{
3957public:
3958 // types
3959 typedef _RealType result_type;
3960
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003961 class _LIBCPP_VISIBLE param_type
Howard Hinnant30a840f2010-05-12 17:08:57 +00003962 {
3963 result_type __lambda_;
3964 public:
3965 typedef exponential_distribution distribution_type;
3966
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003968 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3969
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003971 result_type lambda() const {return __lambda_;}
3972
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003973 friend _LIBCPP_INLINE_VISIBILITY
3974 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00003975 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003976 friend _LIBCPP_INLINE_VISIBILITY
3977 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00003978 {return !(__x == __y);}
3979 };
3980
3981private:
3982 param_type __p_;
3983
3984public:
3985 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003987 explicit exponential_distribution(result_type __lambda = 1)
3988 : __p_(param_type(__lambda)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003990 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003992 void reset() {}
3993
3994 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003995 template<class _URNG>
3996 _LIBCPP_INLINE_VISIBILITY
3997 result_type operator()(_URNG& __g)
Howard Hinnant30a840f2010-05-12 17:08:57 +00003998 {return (*this)(__g, __p_);}
3999 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4000
4001 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004003 result_type lambda() const {return __p_.lambda();}
4004
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004005 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004006 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004008 void param(const param_type& __p) {__p_ = __p;}
4009
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004011 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004012 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf40dc62010-05-16 17:56:20 +00004013 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00004014
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004015 friend _LIBCPP_INLINE_VISIBILITY
4016 bool operator==(const exponential_distribution& __x,
4017 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004018 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004019 friend _LIBCPP_INLINE_VISIBILITY
4020 bool operator!=(const exponential_distribution& __x,
4021 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004022 {return !(__x == __y);}
4023};
4024
4025template <class _RealType>
4026template<class _URNG>
4027_RealType
4028exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4029{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004030 return -_VSTD::log
Howard Hinnant30a840f2010-05-12 17:08:57 +00004031 (
4032 result_type(1) -
Howard Hinnant0949eed2011-06-30 21:18:19 +00004033 _VSTD::generate_canonical<result_type,
Howard Hinnant30a840f2010-05-12 17:08:57 +00004034 numeric_limits<result_type>::digits>(__g)
4035 )
4036 / __p.lambda();
4037}
4038
4039template <class _CharT, class _Traits, class _RealType>
4040basic_ostream<_CharT, _Traits>&
4041operator<<(basic_ostream<_CharT, _Traits>& __os,
4042 const exponential_distribution<_RealType>& __x)
4043{
4044 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004045 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4046 ios_base::scientific);
Howard Hinnant30a840f2010-05-12 17:08:57 +00004047 return __os << __x.lambda();
4048}
4049
4050template <class _CharT, class _Traits, class _RealType>
4051basic_istream<_CharT, _Traits>&
4052operator>>(basic_istream<_CharT, _Traits>& __is,
4053 exponential_distribution<_RealType>& __x)
4054{
4055 typedef exponential_distribution<_RealType> _Eng;
4056 typedef typename _Eng::result_type result_type;
4057 typedef typename _Eng::param_type param_type;
4058 __save_flags<_CharT, _Traits> _(__is);
4059 __is.flags(ios_base::dec | ios_base::skipws);
4060 result_type __lambda;
4061 __is >> __lambda;
4062 if (!__is.fail())
4063 __x.param(param_type(__lambda));
4064 return __is;
4065}
4066
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004067// normal_distribution
4068
4069template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004070class _LIBCPP_VISIBLE normal_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004071{
4072public:
4073 // types
4074 typedef _RealType result_type;
4075
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004076 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004077 {
4078 result_type __mean_;
4079 result_type __stddev_;
4080 public:
4081 typedef normal_distribution distribution_type;
4082
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004084 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4085 : __mean_(__mean), __stddev_(__stddev) {}
4086
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004088 result_type mean() const {return __mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004090 result_type stddev() const {return __stddev_;}
4091
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004092 friend _LIBCPP_INLINE_VISIBILITY
4093 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004094 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004095 friend _LIBCPP_INLINE_VISIBILITY
4096 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004097 {return !(__x == __y);}
4098 };
4099
4100private:
4101 param_type __p_;
4102 result_type _V_;
4103 bool _V_hot_;
4104
4105public:
4106 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004108 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4109 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004111 explicit normal_distribution(const param_type& __p)
4112 : __p_(__p), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004114 void reset() {_V_hot_ = false;}
4115
4116 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004117 template<class _URNG>
4118 _LIBCPP_INLINE_VISIBILITY
4119 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004120 {return (*this)(__g, __p_);}
4121 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4122
4123 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004125 result_type mean() const {return __p_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004127 result_type stddev() const {return __p_.stddev();}
4128
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004130 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004132 void param(const param_type& __p) {__p_ = __p;}
4133
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004135 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004137 result_type max() const {return numeric_limits<result_type>::infinity();}
4138
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004139 friend _LIBCPP_INLINE_VISIBILITY
4140 bool operator==(const normal_distribution& __x,
4141 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004142 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4143 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004144 friend _LIBCPP_INLINE_VISIBILITY
4145 bool operator!=(const normal_distribution& __x,
4146 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004147 {return !(__x == __y);}
4148
4149 template <class _CharT, class _Traits, class _RT>
4150 friend
4151 basic_ostream<_CharT, _Traits>&
4152 operator<<(basic_ostream<_CharT, _Traits>& __os,
4153 const normal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004154
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004155 template <class _CharT, class _Traits, class _RT>
4156 friend
4157 basic_istream<_CharT, _Traits>&
4158 operator>>(basic_istream<_CharT, _Traits>& __is,
4159 normal_distribution<_RT>& __x);
4160};
4161
4162template <class _RealType>
4163template<class _URNG>
4164_RealType
4165normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4166{
4167 result_type _U;
4168 if (_V_hot_)
4169 {
4170 _V_hot_ = false;
4171 _U = _V_;
4172 }
4173 else
4174 {
4175 uniform_real_distribution<result_type> _Uni(-1, 1);
4176 result_type __u;
4177 result_type __v;
4178 result_type __s;
4179 do
4180 {
4181 __u = _Uni(__g);
4182 __v = _Uni(__g);
4183 __s = __u * __u + __v * __v;
4184 } while (__s > 1 || __s == 0);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004185 result_type _F = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004186 _V_ = __v * _F;
4187 _V_hot_ = true;
4188 _U = __u * _F;
4189 }
4190 return _U * __p.stddev() + __p.mean();
4191}
4192
4193template <class _CharT, class _Traits, class _RT>
4194basic_ostream<_CharT, _Traits>&
4195operator<<(basic_ostream<_CharT, _Traits>& __os,
4196 const normal_distribution<_RT>& __x)
4197{
4198 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004199 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4200 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004201 _CharT __sp = __os.widen(' ');
4202 __os.fill(__sp);
4203 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4204 if (__x._V_hot_)
4205 __os << __sp << __x._V_;
4206 return __os;
4207}
4208
4209template <class _CharT, class _Traits, class _RT>
4210basic_istream<_CharT, _Traits>&
4211operator>>(basic_istream<_CharT, _Traits>& __is,
4212 normal_distribution<_RT>& __x)
4213{
4214 typedef normal_distribution<_RT> _Eng;
4215 typedef typename _Eng::result_type result_type;
4216 typedef typename _Eng::param_type param_type;
4217 __save_flags<_CharT, _Traits> _(__is);
4218 __is.flags(ios_base::dec | ios_base::skipws);
4219 result_type __mean;
4220 result_type __stddev;
4221 result_type _V = 0;
4222 bool _V_hot = false;
4223 __is >> __mean >> __stddev >> _V_hot;
4224 if (_V_hot)
4225 __is >> _V;
4226 if (!__is.fail())
4227 {
4228 __x.param(param_type(__mean, __stddev));
4229 __x._V_hot_ = _V_hot;
4230 __x._V_ = _V;
4231 }
4232 return __is;
4233}
4234
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004235// lognormal_distribution
4236
4237template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004238class _LIBCPP_VISIBLE lognormal_distribution
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004239{
4240public:
4241 // types
4242 typedef _RealType result_type;
4243
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004244 class _LIBCPP_VISIBLE param_type
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004245 {
4246 normal_distribution<result_type> __nd_;
4247 public:
4248 typedef lognormal_distribution distribution_type;
4249
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004251 explicit param_type(result_type __m = 0, result_type __s = 1)
4252 : __nd_(__m, __s) {}
4253
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004255 result_type m() const {return __nd_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004257 result_type s() const {return __nd_.stddev();}
4258
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004259 friend _LIBCPP_INLINE_VISIBILITY
4260 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004261 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004262 friend _LIBCPP_INLINE_VISIBILITY
4263 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004264 {return !(__x == __y);}
4265 friend class lognormal_distribution;
4266
4267 template <class _CharT, class _Traits, class _RT>
4268 friend
4269 basic_ostream<_CharT, _Traits>&
4270 operator<<(basic_ostream<_CharT, _Traits>& __os,
4271 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004272
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004273 template <class _CharT, class _Traits, class _RT>
4274 friend
4275 basic_istream<_CharT, _Traits>&
4276 operator>>(basic_istream<_CharT, _Traits>& __is,
4277 lognormal_distribution<_RT>& __x);
4278 };
4279
4280private:
4281 param_type __p_;
4282
4283public:
4284 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004286 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4287 : __p_(param_type(__m, __s)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004289 explicit lognormal_distribution(const param_type& __p)
4290 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004292 void reset() {__p_.__nd_.reset();}
4293
4294 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004295 template<class _URNG>
4296 _LIBCPP_INLINE_VISIBILITY
4297 result_type operator()(_URNG& __g)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004298 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004299 template<class _URNG>
4300 _LIBCPP_INLINE_VISIBILITY
4301 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004302 {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004303
4304 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004306 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004308 result_type s() const {return __p_.s();}
4309
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004311 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00004313 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004314
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004316 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004318 result_type max() const {return numeric_limits<result_type>::infinity();}
4319
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004320 friend _LIBCPP_INLINE_VISIBILITY
4321 bool operator==(const lognormal_distribution& __x,
4322 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004323 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004324 friend _LIBCPP_INLINE_VISIBILITY
4325 bool operator!=(const lognormal_distribution& __x,
4326 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004327 {return !(__x == __y);}
4328
4329 template <class _CharT, class _Traits, class _RT>
4330 friend
4331 basic_ostream<_CharT, _Traits>&
4332 operator<<(basic_ostream<_CharT, _Traits>& __os,
4333 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004334
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004335 template <class _CharT, class _Traits, class _RT>
4336 friend
4337 basic_istream<_CharT, _Traits>&
4338 operator>>(basic_istream<_CharT, _Traits>& __is,
4339 lognormal_distribution<_RT>& __x);
4340};
4341
4342template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004343inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004344basic_ostream<_CharT, _Traits>&
4345operator<<(basic_ostream<_CharT, _Traits>& __os,
4346 const lognormal_distribution<_RT>& __x)
4347{
4348 return __os << __x.__p_.__nd_;
4349}
4350
4351template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004352inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004353basic_istream<_CharT, _Traits>&
4354operator>>(basic_istream<_CharT, _Traits>& __is,
4355 lognormal_distribution<_RT>& __x)
4356{
4357 return __is >> __x.__p_.__nd_;
4358}
4359
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004360// poisson_distribution
4361
4362template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004363class _LIBCPP_VISIBLE poisson_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004364{
4365public:
4366 // types
4367 typedef _IntType result_type;
4368
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004369 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004370 {
4371 double __mean_;
4372 double __s_;
4373 double __d_;
4374 double __l_;
4375 double __omega_;
4376 double __c0_;
4377 double __c1_;
4378 double __c2_;
4379 double __c3_;
4380 double __c_;
4381
4382 public:
4383 typedef poisson_distribution distribution_type;
4384
4385 explicit param_type(double __mean = 1.0);
4386
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004388 double mean() const {return __mean_;}
4389
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004390 friend _LIBCPP_INLINE_VISIBILITY
4391 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004392 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004393 friend _LIBCPP_INLINE_VISIBILITY
4394 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004395 {return !(__x == __y);}
4396
4397 friend class poisson_distribution;
4398 };
4399
4400private:
4401 param_type __p_;
4402
4403public:
4404 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004406 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004408 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004410 void reset() {}
4411
4412 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004413 template<class _URNG>
4414 _LIBCPP_INLINE_VISIBILITY
4415 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004416 {return (*this)(__g, __p_);}
4417 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4418
4419 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004421 double mean() const {return __p_.mean();}
4422
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004424 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004426 void param(const param_type& __p) {__p_ = __p;}
4427
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004429 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004431 result_type max() const {return numeric_limits<result_type>::max();}
4432
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004433 friend _LIBCPP_INLINE_VISIBILITY
4434 bool operator==(const poisson_distribution& __x,
4435 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004436 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004437 friend _LIBCPP_INLINE_VISIBILITY
4438 bool operator!=(const poisson_distribution& __x,
4439 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004440 {return !(__x == __y);}
4441};
4442
4443template<class _IntType>
4444poisson_distribution<_IntType>::param_type::param_type(double __mean)
4445 : __mean_(__mean)
4446{
4447 if (__mean_ < 10)
4448 {
4449 __s_ = 0;
4450 __d_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004451 __l_ = _VSTD::exp(-__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004452 __omega_ = 0;
4453 __c3_ = 0;
4454 __c2_ = 0;
4455 __c1_ = 0;
4456 __c0_ = 0;
4457 __c_ = 0;
4458 }
4459 else
4460 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004461 __s_ = _VSTD::sqrt(__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004462 __d_ = 6 * __mean_ * __mean_;
4463 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4464 __omega_ = .3989423 / __s_;
4465 double __b1_ = .4166667E-1 / __mean_;
4466 double __b2_ = .3 * __b1_ * __b1_;
4467 __c3_ = .1428571 * __b1_ * __b2_;
4468 __c2_ = __b2_ - 15. * __c3_;
4469 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4470 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4471 __c_ = .1069 / __mean_;
4472 }
4473}
4474
4475template <class _IntType>
4476template<class _URNG>
4477_IntType
4478poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4479{
4480 result_type __x;
4481 uniform_real_distribution<double> __urd;
Howard Hinnant75f76952011-04-14 15:59:22 +00004482 if (__pr.__mean_ < 10)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004483 {
4484 __x = 0;
4485 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4486 __p *= __urd(__urng);
4487 }
4488 else
4489 {
4490 double __difmuk;
4491 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4492 double __u;
4493 if (__g > 0)
4494 {
4495 __x = static_cast<result_type>(__g);
4496 if (__x >= __pr.__l_)
4497 return __x;
4498 __difmuk = __pr.__mean_ - __x;
4499 __u = __urd(__urng);
4500 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4501 return __x;
4502 }
4503 exponential_distribution<double> __edist;
4504 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4505 {
4506 double __e;
4507 if (__using_exp_dist || __g < 0)
4508 {
4509 double __t;
4510 do
4511 {
4512 __e = __edist(__urng);
4513 __u = __urd(__urng);
4514 __u += __u - 1;
4515 __t = 1.8 + (__u < 0 ? -__e : __e);
4516 } while (__t <= -.6744);
4517 __x = __pr.__mean_ + __pr.__s_ * __t;
4518 __difmuk = __pr.__mean_ - __x;
4519 __using_exp_dist = true;
4520 }
4521 double __px;
4522 double __py;
4523 if (__x < 10)
4524 {
4525 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4526 40320, 362880};
4527 __px = -__pr.__mean_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004528 __py = _VSTD::pow(__pr.__mean_, (double)__x) / __fac[__x];
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004529 }
4530 else
4531 {
4532 double __del = .8333333E-1 / __x;
4533 __del -= 4.8 * __del * __del * __del;
4534 double __v = __difmuk / __x;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004535 if (_VSTD::abs(__v) > 0.25)
4536 __px = __x * _VSTD::log(1 + __v) - __difmuk - __del;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004537 else
4538 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4539 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4540 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004541 __py = .3989423 / _VSTD::sqrt(__x);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004542 }
4543 double __r = (0.5 - __difmuk) / __pr.__s_;
4544 double __r2 = __r * __r;
4545 double __fx = -0.5 * __r2;
4546 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4547 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4548 if (__using_exp_dist)
4549 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004550 if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4551 __fy * _VSTD::exp(__fx + __e))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004552 break;
4553 }
4554 else
4555 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004556 if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004557 break;
4558 }
4559 }
4560 }
4561 return __x;
4562}
4563
4564template <class _CharT, class _Traits, class _IntType>
4565basic_ostream<_CharT, _Traits>&
4566operator<<(basic_ostream<_CharT, _Traits>& __os,
4567 const poisson_distribution<_IntType>& __x)
4568{
4569 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004570 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4571 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004572 return __os << __x.mean();
4573}
4574
4575template <class _CharT, class _Traits, class _IntType>
4576basic_istream<_CharT, _Traits>&
4577operator>>(basic_istream<_CharT, _Traits>& __is,
4578 poisson_distribution<_IntType>& __x)
4579{
4580 typedef poisson_distribution<_IntType> _Eng;
4581 typedef typename _Eng::param_type param_type;
4582 __save_flags<_CharT, _Traits> _(__is);
4583 __is.flags(ios_base::dec | ios_base::skipws);
4584 double __mean;
4585 __is >> __mean;
4586 if (!__is.fail())
4587 __x.param(param_type(__mean));
4588 return __is;
4589}
4590
Howard Hinnant9de6e302010-05-16 01:09:02 +00004591// weibull_distribution
4592
4593template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004594class _LIBCPP_VISIBLE weibull_distribution
Howard Hinnant9de6e302010-05-16 01:09:02 +00004595{
4596public:
4597 // types
4598 typedef _RealType result_type;
4599
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004600 class _LIBCPP_VISIBLE param_type
Howard Hinnant9de6e302010-05-16 01:09:02 +00004601 {
4602 result_type __a_;
4603 result_type __b_;
4604 public:
4605 typedef weibull_distribution distribution_type;
4606
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004608 explicit param_type(result_type __a = 1, result_type __b = 1)
4609 : __a_(__a), __b_(__b) {}
4610
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004612 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004614 result_type b() const {return __b_;}
4615
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004616 friend _LIBCPP_INLINE_VISIBILITY
4617 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004618 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004619 friend _LIBCPP_INLINE_VISIBILITY
4620 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004621 {return !(__x == __y);}
4622 };
4623
4624private:
4625 param_type __p_;
4626
4627public:
4628 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004630 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4631 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004633 explicit weibull_distribution(const param_type& __p)
4634 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004636 void reset() {}
4637
4638 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004639 template<class _URNG>
4640 _LIBCPP_INLINE_VISIBILITY
4641 result_type operator()(_URNG& __g)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004642 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004643 template<class _URNG>
4644 _LIBCPP_INLINE_VISIBILITY
4645 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004646 {return __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004647 _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
Howard Hinnant9de6e302010-05-16 01:09:02 +00004648
4649 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004651 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004653 result_type b() const {return __p_.b();}
4654
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004656 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004658 void param(const param_type& __p) {__p_ = __p;}
4659
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004661 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004663 result_type max() const {return numeric_limits<result_type>::infinity();}
4664
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004665 friend _LIBCPP_INLINE_VISIBILITY
4666 bool operator==(const weibull_distribution& __x,
4667 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004668 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004669 friend _LIBCPP_INLINE_VISIBILITY
4670 bool operator!=(const weibull_distribution& __x,
4671 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004672 {return !(__x == __y);}
4673};
4674
4675template <class _CharT, class _Traits, class _RT>
4676basic_ostream<_CharT, _Traits>&
4677operator<<(basic_ostream<_CharT, _Traits>& __os,
4678 const weibull_distribution<_RT>& __x)
4679{
4680 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004681 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4682 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004683 _CharT __sp = __os.widen(' ');
4684 __os.fill(__sp);
4685 __os << __x.a() << __sp << __x.b();
4686 return __os;
4687}
4688
4689template <class _CharT, class _Traits, class _RT>
4690basic_istream<_CharT, _Traits>&
4691operator>>(basic_istream<_CharT, _Traits>& __is,
4692 weibull_distribution<_RT>& __x)
4693{
4694 typedef weibull_distribution<_RT> _Eng;
4695 typedef typename _Eng::result_type result_type;
4696 typedef typename _Eng::param_type param_type;
4697 __save_flags<_CharT, _Traits> _(__is);
4698 __is.flags(ios_base::dec | ios_base::skipws);
4699 result_type __a;
4700 result_type __b;
4701 __is >> __a >> __b;
4702 if (!__is.fail())
4703 __x.param(param_type(__a, __b));
4704 return __is;
4705}
4706
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004707template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004708class _LIBCPP_VISIBLE extreme_value_distribution
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004709{
4710public:
4711 // types
4712 typedef _RealType result_type;
4713
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004714 class _LIBCPP_VISIBLE param_type
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004715 {
4716 result_type __a_;
4717 result_type __b_;
4718 public:
4719 typedef extreme_value_distribution distribution_type;
4720
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004722 explicit param_type(result_type __a = 0, result_type __b = 1)
4723 : __a_(__a), __b_(__b) {}
4724
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004726 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004728 result_type b() const {return __b_;}
4729
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004730 friend _LIBCPP_INLINE_VISIBILITY
4731 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004732 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004733 friend _LIBCPP_INLINE_VISIBILITY
4734 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004735 {return !(__x == __y);}
4736 };
4737
4738private:
4739 param_type __p_;
4740
4741public:
4742 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004744 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4745 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004747 explicit extreme_value_distribution(const param_type& __p)
4748 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004750 void reset() {}
4751
4752 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004753 template<class _URNG>
4754 _LIBCPP_INLINE_VISIBILITY
4755 result_type operator()(_URNG& __g)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004756 {return (*this)(__g, __p_);}
4757 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4758
4759 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004761 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004763 result_type b() const {return __p_.b();}
4764
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004766 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004768 void param(const param_type& __p) {__p_ = __p;}
4769
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004771 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004773 result_type max() const {return numeric_limits<result_type>::infinity();}
4774
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004775 friend _LIBCPP_INLINE_VISIBILITY
4776 bool operator==(const extreme_value_distribution& __x,
4777 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004778 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004779 friend _LIBCPP_INLINE_VISIBILITY
4780 bool operator!=(const extreme_value_distribution& __x,
4781 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004782 {return !(__x == __y);}
4783};
4784
4785template<class _RealType>
4786template<class _URNG>
4787_RealType
4788extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4789{
4790 return __p.a() - __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004791 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004792}
4793
4794template <class _CharT, class _Traits, class _RT>
4795basic_ostream<_CharT, _Traits>&
4796operator<<(basic_ostream<_CharT, _Traits>& __os,
4797 const extreme_value_distribution<_RT>& __x)
4798{
4799 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004800 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4801 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004802 _CharT __sp = __os.widen(' ');
4803 __os.fill(__sp);
4804 __os << __x.a() << __sp << __x.b();
4805 return __os;
4806}
4807
4808template <class _CharT, class _Traits, class _RT>
4809basic_istream<_CharT, _Traits>&
4810operator>>(basic_istream<_CharT, _Traits>& __is,
4811 extreme_value_distribution<_RT>& __x)
4812{
4813 typedef extreme_value_distribution<_RT> _Eng;
4814 typedef typename _Eng::result_type result_type;
4815 typedef typename _Eng::param_type param_type;
4816 __save_flags<_CharT, _Traits> _(__is);
4817 __is.flags(ios_base::dec | ios_base::skipws);
4818 result_type __a;
4819 result_type __b;
4820 __is >> __a >> __b;
4821 if (!__is.fail())
4822 __x.param(param_type(__a, __b));
4823 return __is;
4824}
4825
Howard Hinnantc7c49132010-05-13 17:58:28 +00004826// gamma_distribution
4827
4828template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004829class _LIBCPP_VISIBLE gamma_distribution
Howard Hinnantc7c49132010-05-13 17:58:28 +00004830{
4831public:
4832 // types
4833 typedef _RealType result_type;
4834
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004835 class _LIBCPP_VISIBLE param_type
Howard Hinnantc7c49132010-05-13 17:58:28 +00004836 {
4837 result_type __alpha_;
4838 result_type __beta_;
4839 public:
4840 typedef gamma_distribution distribution_type;
4841
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004843 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4844 : __alpha_(__alpha), __beta_(__beta) {}
4845
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004847 result_type alpha() const {return __alpha_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004849 result_type beta() const {return __beta_;}
4850
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004851 friend _LIBCPP_INLINE_VISIBILITY
4852 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004853 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004854 friend _LIBCPP_INLINE_VISIBILITY
4855 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004856 {return !(__x == __y);}
4857 };
4858
4859private:
4860 param_type __p_;
4861
4862public:
4863 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004865 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4866 : __p_(param_type(__alpha, __beta)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004868 explicit gamma_distribution(const param_type& __p)
4869 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004870 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004871 void reset() {}
4872
4873 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004874 template<class _URNG>
4875 _LIBCPP_INLINE_VISIBILITY
4876 result_type operator()(_URNG& __g)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004877 {return (*this)(__g, __p_);}
4878 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4879
4880 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004882 result_type alpha() const {return __p_.alpha();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004884 result_type beta() const {return __p_.beta();}
4885
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004887 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004889 void param(const param_type& __p) {__p_ = __p;}
4890
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004892 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004894 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantc7c49132010-05-13 17:58:28 +00004895
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004896 friend _LIBCPP_INLINE_VISIBILITY
4897 bool operator==(const gamma_distribution& __x,
4898 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004899 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004900 friend _LIBCPP_INLINE_VISIBILITY
4901 bool operator!=(const gamma_distribution& __x,
4902 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004903 {return !(__x == __y);}
4904};
4905
4906template <class _RealType>
4907template<class _URNG>
4908_RealType
4909gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4910{
Howard Hinnantf417abe2010-05-14 18:43:10 +00004911 result_type __a = __p.alpha();
4912 uniform_real_distribution<result_type> __gen(0, 1);
4913 exponential_distribution<result_type> __egen;
4914 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00004915 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00004916 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004917 else if (__a > 1)
4918 {
4919 const result_type __b = __a - 1;
4920 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004921 while (true)
4922 {
4923 const result_type __u = __gen(__g);
4924 const result_type __v = __gen(__g);
4925 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004926 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004927 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004928 const result_type __y = _VSTD::sqrt(__c / __w) *
Howard Hinnantc7c49132010-05-13 17:58:28 +00004929 (__u - result_type(0.5));
4930 __x = __b + __y;
4931 if (__x >= 0)
4932 {
4933 const result_type __z = 64 * __w * __w * __w * __v * __v;
4934 if (__z <= 1 - 2 * __y * __y / __x)
4935 break;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004936 if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
Howard Hinnantc7c49132010-05-13 17:58:28 +00004937 break;
4938 }
4939 }
4940 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00004941 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00004942 else // __a < 1
4943 {
4944 while (true)
4945 {
4946 const result_type __u = __gen(__g);
4947 const result_type __es = __egen(__g);
4948 if (__u <= 1 - __a)
4949 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004950 __x = _VSTD::pow(__u, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004951 if (__x <= __es)
4952 break;
4953 }
4954 else
4955 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004956 const result_type __e = -_VSTD::log((1-__u)/__a);
4957 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004958 if (__x <= __e + __es)
4959 break;
4960 }
4961 }
4962 }
4963 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00004964}
4965
4966template <class _CharT, class _Traits, class _RT>
4967basic_ostream<_CharT, _Traits>&
4968operator<<(basic_ostream<_CharT, _Traits>& __os,
4969 const gamma_distribution<_RT>& __x)
4970{
4971 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004972 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4973 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004974 _CharT __sp = __os.widen(' ');
4975 __os.fill(__sp);
4976 __os << __x.alpha() << __sp << __x.beta();
4977 return __os;
4978}
4979
4980template <class _CharT, class _Traits, class _RT>
4981basic_istream<_CharT, _Traits>&
4982operator>>(basic_istream<_CharT, _Traits>& __is,
4983 gamma_distribution<_RT>& __x)
4984{
4985 typedef gamma_distribution<_RT> _Eng;
4986 typedef typename _Eng::result_type result_type;
4987 typedef typename _Eng::param_type param_type;
4988 __save_flags<_CharT, _Traits> _(__is);
4989 __is.flags(ios_base::dec | ios_base::skipws);
4990 result_type __alpha;
4991 result_type __beta;
4992 __is >> __alpha >> __beta;
4993 if (!__is.fail())
4994 __x.param(param_type(__alpha, __beta));
4995 return __is;
4996}
Howard Hinnanta64111c2010-05-12 21:02:31 +00004997
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00004998// negative_binomial_distribution
4999
5000template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005001class _LIBCPP_VISIBLE negative_binomial_distribution
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005002{
5003public:
5004 // types
5005 typedef _IntType result_type;
5006
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005007 class _LIBCPP_VISIBLE param_type
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005008 {
5009 result_type __k_;
5010 double __p_;
5011 public:
5012 typedef negative_binomial_distribution distribution_type;
5013
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005015 explicit param_type(result_type __k = 1, double __p = 0.5)
5016 : __k_(__k), __p_(__p) {}
5017
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005019 result_type k() const {return __k_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005021 double p() const {return __p_;}
5022
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005023 friend _LIBCPP_INLINE_VISIBILITY
5024 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005025 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005026 friend _LIBCPP_INLINE_VISIBILITY
5027 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005028 {return !(__x == __y);}
5029 };
5030
5031private:
5032 param_type __p_;
5033
5034public:
5035 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005037 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
5038 : __p_(__k, __p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005040 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005042 void reset() {}
5043
5044 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005045 template<class _URNG>
5046 _LIBCPP_INLINE_VISIBILITY
5047 result_type operator()(_URNG& __g)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005048 {return (*this)(__g, __p_);}
5049 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5050
5051 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005053 result_type k() const {return __p_.k();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005055 double p() const {return __p_.p();}
5056
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005058 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005060 void param(const param_type& __p) {__p_ = __p;}
5061
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005063 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005065 result_type max() const {return numeric_limits<result_type>::max();}
5066
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005067 friend _LIBCPP_INLINE_VISIBILITY
5068 bool operator==(const negative_binomial_distribution& __x,
5069 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005070 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005071 friend _LIBCPP_INLINE_VISIBILITY
5072 bool operator!=(const negative_binomial_distribution& __x,
5073 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005074 {return !(__x == __y);}
5075};
5076
5077template <class _IntType>
5078template<class _URNG>
5079_IntType
5080negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5081{
5082 result_type __k = __pr.k();
5083 double __p = __pr.p();
5084 if (__k <= 21 * __p)
5085 {
5086 bernoulli_distribution __gen(__p);
5087 result_type __f = 0;
5088 result_type __s = 0;
5089 while (__s < __k)
5090 {
5091 if (__gen(__urng))
5092 ++__s;
5093 else
5094 ++__f;
5095 }
5096 return __f;
5097 }
5098 return poisson_distribution<result_type>(gamma_distribution<double>
5099 (__k, (1-__p)/__p)(__urng))(__urng);
5100}
5101
5102template <class _CharT, class _Traits, class _IntType>
5103basic_ostream<_CharT, _Traits>&
5104operator<<(basic_ostream<_CharT, _Traits>& __os,
5105 const negative_binomial_distribution<_IntType>& __x)
5106{
5107 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005108 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5109 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005110 _CharT __sp = __os.widen(' ');
5111 __os.fill(__sp);
5112 return __os << __x.k() << __sp << __x.p();
5113}
5114
5115template <class _CharT, class _Traits, class _IntType>
5116basic_istream<_CharT, _Traits>&
5117operator>>(basic_istream<_CharT, _Traits>& __is,
5118 negative_binomial_distribution<_IntType>& __x)
5119{
5120 typedef negative_binomial_distribution<_IntType> _Eng;
5121 typedef typename _Eng::result_type result_type;
5122 typedef typename _Eng::param_type param_type;
5123 __save_flags<_CharT, _Traits> _(__is);
5124 __is.flags(ios_base::dec | ios_base::skipws);
5125 result_type __k;
5126 double __p;
5127 __is >> __k >> __p;
5128 if (!__is.fail())
5129 __x.param(param_type(__k, __p));
5130 return __is;
5131}
5132
Howard Hinnant34e8a572010-05-17 13:44:27 +00005133// geometric_distribution
5134
5135template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005136class _LIBCPP_VISIBLE geometric_distribution
Howard Hinnant34e8a572010-05-17 13:44:27 +00005137{
5138public:
5139 // types
5140 typedef _IntType result_type;
5141
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005142 class _LIBCPP_VISIBLE param_type
Howard Hinnant34e8a572010-05-17 13:44:27 +00005143 {
5144 double __p_;
5145 public:
5146 typedef geometric_distribution distribution_type;
5147
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005149 explicit param_type(double __p = 0.5) : __p_(__p) {}
5150
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005151 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005152 double p() const {return __p_;}
5153
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005154 friend _LIBCPP_INLINE_VISIBILITY
5155 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005156 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005157 friend _LIBCPP_INLINE_VISIBILITY
5158 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005159 {return !(__x == __y);}
5160 };
5161
5162private:
5163 param_type __p_;
5164
5165public:
5166 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005168 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005170 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005172 void reset() {}
5173
5174 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005175 template<class _URNG>
5176 _LIBCPP_INLINE_VISIBILITY
5177 result_type operator()(_URNG& __g)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005178 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005179 template<class _URNG>
5180 _LIBCPP_INLINE_VISIBILITY
5181 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005182 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5183
5184 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005186 double p() const {return __p_.p();}
5187
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005189 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005191 void param(const param_type& __p) {__p_ = __p;}
5192
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005194 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005195 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005196 result_type max() const {return numeric_limits<result_type>::max();}
5197
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005198 friend _LIBCPP_INLINE_VISIBILITY
5199 bool operator==(const geometric_distribution& __x,
5200 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005201 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005202 friend _LIBCPP_INLINE_VISIBILITY
5203 bool operator!=(const geometric_distribution& __x,
5204 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005205 {return !(__x == __y);}
5206};
5207
5208template <class _CharT, class _Traits, class _IntType>
5209basic_ostream<_CharT, _Traits>&
5210operator<<(basic_ostream<_CharT, _Traits>& __os,
5211 const geometric_distribution<_IntType>& __x)
5212{
5213 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005214 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5215 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005216 return __os << __x.p();
5217}
5218
5219template <class _CharT, class _Traits, class _IntType>
5220basic_istream<_CharT, _Traits>&
5221operator>>(basic_istream<_CharT, _Traits>& __is,
5222 geometric_distribution<_IntType>& __x)
5223{
5224 typedef geometric_distribution<_IntType> _Eng;
5225 typedef typename _Eng::param_type param_type;
5226 __save_flags<_CharT, _Traits> _(__is);
5227 __is.flags(ios_base::dec | ios_base::skipws);
5228 double __p;
5229 __is >> __p;
5230 if (!__is.fail())
5231 __x.param(param_type(__p));
5232 return __is;
5233}
5234
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005235// chi_squared_distribution
5236
5237template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005238class _LIBCPP_VISIBLE chi_squared_distribution
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005239{
5240public:
5241 // types
5242 typedef _RealType result_type;
5243
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005244 class _LIBCPP_VISIBLE param_type
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005245 {
5246 result_type __n_;
5247 public:
5248 typedef chi_squared_distribution distribution_type;
5249
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005251 explicit param_type(result_type __n = 1) : __n_(__n) {}
5252
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005254 result_type n() const {return __n_;}
5255
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005256 friend _LIBCPP_INLINE_VISIBILITY
5257 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005258 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005259 friend _LIBCPP_INLINE_VISIBILITY
5260 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005261 {return !(__x == __y);}
5262 };
5263
5264private:
5265 param_type __p_;
5266
5267public:
5268 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005270 explicit chi_squared_distribution(result_type __n = 1)
5271 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005273 explicit chi_squared_distribution(const param_type& __p)
5274 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005276 void reset() {}
5277
5278 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005279 template<class _URNG>
5280 _LIBCPP_INLINE_VISIBILITY
5281 result_type operator()(_URNG& __g)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005282 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005283 template<class _URNG>
5284 _LIBCPP_INLINE_VISIBILITY
5285 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005286 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5287
5288 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005290 result_type n() const {return __p_.n();}
5291
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005293 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005295 void param(const param_type& __p) {__p_ = __p;}
5296
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005298 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005300 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005301
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005302 friend _LIBCPP_INLINE_VISIBILITY
5303 bool operator==(const chi_squared_distribution& __x,
5304 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005305 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005306 friend _LIBCPP_INLINE_VISIBILITY
5307 bool operator!=(const chi_squared_distribution& __x,
5308 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005309 {return !(__x == __y);}
5310};
5311
5312template <class _CharT, class _Traits, class _RT>
5313basic_ostream<_CharT, _Traits>&
5314operator<<(basic_ostream<_CharT, _Traits>& __os,
5315 const chi_squared_distribution<_RT>& __x)
5316{
5317 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005318 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5319 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005320 __os << __x.n();
5321 return __os;
5322}
5323
5324template <class _CharT, class _Traits, class _RT>
5325basic_istream<_CharT, _Traits>&
5326operator>>(basic_istream<_CharT, _Traits>& __is,
5327 chi_squared_distribution<_RT>& __x)
5328{
5329 typedef chi_squared_distribution<_RT> _Eng;
5330 typedef typename _Eng::result_type result_type;
5331 typedef typename _Eng::param_type param_type;
5332 __save_flags<_CharT, _Traits> _(__is);
5333 __is.flags(ios_base::dec | ios_base::skipws);
5334 result_type __n;
5335 __is >> __n;
5336 if (!__is.fail())
5337 __x.param(param_type(__n));
5338 return __is;
5339}
5340
Howard Hinnantd7d01132010-05-17 21:55:46 +00005341// cauchy_distribution
5342
5343template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005344class _LIBCPP_VISIBLE cauchy_distribution
Howard Hinnantd7d01132010-05-17 21:55:46 +00005345{
5346public:
5347 // types
5348 typedef _RealType result_type;
5349
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005350 class _LIBCPP_VISIBLE param_type
Howard Hinnantd7d01132010-05-17 21:55:46 +00005351 {
5352 result_type __a_;
5353 result_type __b_;
5354 public:
5355 typedef cauchy_distribution distribution_type;
5356
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005358 explicit param_type(result_type __a = 0, result_type __b = 1)
5359 : __a_(__a), __b_(__b) {}
5360
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005362 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005364 result_type b() const {return __b_;}
5365
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005366 friend _LIBCPP_INLINE_VISIBILITY
5367 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005368 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005369 friend _LIBCPP_INLINE_VISIBILITY
5370 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005371 {return !(__x == __y);}
5372 };
5373
5374private:
5375 param_type __p_;
5376
5377public:
5378 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005380 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5381 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005383 explicit cauchy_distribution(const param_type& __p)
5384 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005386 void reset() {}
5387
5388 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005389 template<class _URNG>
5390 _LIBCPP_INLINE_VISIBILITY
5391 result_type operator()(_URNG& __g)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005392 {return (*this)(__g, __p_);}
5393 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5394
5395 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005397 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005399 result_type b() const {return __p_.b();}
5400
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005402 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005404 void param(const param_type& __p) {__p_ = __p;}
5405
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005407 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005409 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005410
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005411 friend _LIBCPP_INLINE_VISIBILITY
5412 bool operator==(const cauchy_distribution& __x,
5413 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005414 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005415 friend _LIBCPP_INLINE_VISIBILITY
5416 bool operator!=(const cauchy_distribution& __x,
5417 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005418 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005419};
5420
5421template <class _RealType>
5422template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005423inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005424_RealType
5425cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5426{
5427 uniform_real_distribution<result_type> __gen;
5428 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
Howard Hinnant0949eed2011-06-30 21:18:19 +00005429 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
Howard Hinnantd7d01132010-05-17 21:55:46 +00005430}
5431
5432template <class _CharT, class _Traits, class _RT>
5433basic_ostream<_CharT, _Traits>&
5434operator<<(basic_ostream<_CharT, _Traits>& __os,
5435 const cauchy_distribution<_RT>& __x)
5436{
5437 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005438 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5439 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005440 _CharT __sp = __os.widen(' ');
5441 __os.fill(__sp);
5442 __os << __x.a() << __sp << __x.b();
5443 return __os;
5444}
5445
5446template <class _CharT, class _Traits, class _RT>
5447basic_istream<_CharT, _Traits>&
5448operator>>(basic_istream<_CharT, _Traits>& __is,
5449 cauchy_distribution<_RT>& __x)
5450{
5451 typedef cauchy_distribution<_RT> _Eng;
5452 typedef typename _Eng::result_type result_type;
5453 typedef typename _Eng::param_type param_type;
5454 __save_flags<_CharT, _Traits> _(__is);
5455 __is.flags(ios_base::dec | ios_base::skipws);
5456 result_type __a;
5457 result_type __b;
5458 __is >> __a >> __b;
5459 if (!__is.fail())
5460 __x.param(param_type(__a, __b));
5461 return __is;
5462}
5463
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005464// fisher_f_distribution
5465
5466template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005467class _LIBCPP_VISIBLE fisher_f_distribution
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005468{
5469public:
5470 // types
5471 typedef _RealType result_type;
5472
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005473 class _LIBCPP_VISIBLE param_type
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005474 {
5475 result_type __m_;
5476 result_type __n_;
5477 public:
5478 typedef fisher_f_distribution distribution_type;
5479
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005481 explicit param_type(result_type __m = 1, result_type __n = 1)
5482 : __m_(__m), __n_(__n) {}
5483
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005485 result_type m() const {return __m_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005487 result_type n() const {return __n_;}
5488
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005489 friend _LIBCPP_INLINE_VISIBILITY
5490 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005491 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005492 friend _LIBCPP_INLINE_VISIBILITY
5493 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005494 {return !(__x == __y);}
5495 };
5496
5497private:
5498 param_type __p_;
5499
5500public:
5501 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005503 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5504 : __p_(param_type(__m, __n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005506 explicit fisher_f_distribution(const param_type& __p)
5507 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005509 void reset() {}
5510
5511 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005512 template<class _URNG>
5513 _LIBCPP_INLINE_VISIBILITY
5514 result_type operator()(_URNG& __g)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005515 {return (*this)(__g, __p_);}
5516 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5517
5518 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005519 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005520 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005522 result_type n() const {return __p_.n();}
5523
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005525 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005526 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005527 void param(const param_type& __p) {__p_ = __p;}
5528
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005530 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005532 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005533
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005534 friend _LIBCPP_INLINE_VISIBILITY
5535 bool operator==(const fisher_f_distribution& __x,
5536 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005537 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005538 friend _LIBCPP_INLINE_VISIBILITY
5539 bool operator!=(const fisher_f_distribution& __x,
5540 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005541 {return !(__x == __y);}
5542};
5543
5544template <class _RealType>
5545template<class _URNG>
5546_RealType
5547fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5548{
5549 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5550 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5551 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5552}
5553
5554template <class _CharT, class _Traits, class _RT>
5555basic_ostream<_CharT, _Traits>&
5556operator<<(basic_ostream<_CharT, _Traits>& __os,
5557 const fisher_f_distribution<_RT>& __x)
5558{
5559 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005560 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5561 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005562 _CharT __sp = __os.widen(' ');
5563 __os.fill(__sp);
5564 __os << __x.m() << __sp << __x.n();
5565 return __os;
5566}
5567
5568template <class _CharT, class _Traits, class _RT>
5569basic_istream<_CharT, _Traits>&
5570operator>>(basic_istream<_CharT, _Traits>& __is,
5571 fisher_f_distribution<_RT>& __x)
5572{
5573 typedef fisher_f_distribution<_RT> _Eng;
5574 typedef typename _Eng::result_type result_type;
5575 typedef typename _Eng::param_type param_type;
5576 __save_flags<_CharT, _Traits> _(__is);
5577 __is.flags(ios_base::dec | ios_base::skipws);
5578 result_type __m;
5579 result_type __n;
5580 __is >> __m >> __n;
5581 if (!__is.fail())
5582 __x.param(param_type(__m, __n));
5583 return __is;
5584}
5585
Howard Hinnant551d8e42010-05-19 01:53:57 +00005586// student_t_distribution
5587
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005588template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005589class _LIBCPP_VISIBLE student_t_distribution
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005590{
5591public:
5592 // types
5593 typedef _RealType result_type;
5594
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005595 class _LIBCPP_VISIBLE param_type
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005596 {
5597 result_type __n_;
5598 public:
5599 typedef student_t_distribution distribution_type;
5600
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005602 explicit param_type(result_type __n = 1) : __n_(__n) {}
5603
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005605 result_type n() const {return __n_;}
5606
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005607 friend _LIBCPP_INLINE_VISIBILITY
5608 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005609 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005610 friend _LIBCPP_INLINE_VISIBILITY
5611 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005612 {return !(__x == __y);}
5613 };
5614
5615private:
5616 param_type __p_;
5617 normal_distribution<result_type> __nd_;
5618
5619public:
5620 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005622 explicit student_t_distribution(result_type __n = 1)
5623 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005625 explicit student_t_distribution(const param_type& __p)
5626 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005628 void reset() {__nd_.reset();}
5629
5630 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005631 template<class _URNG>
5632 _LIBCPP_INLINE_VISIBILITY
5633 result_type operator()(_URNG& __g)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005634 {return (*this)(__g, __p_);}
5635 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5636
5637 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005639 result_type n() const {return __p_.n();}
5640
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005642 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005644 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005645
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005647 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005649 result_type max() const {return numeric_limits<result_type>::infinity();}
5650
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005651 friend _LIBCPP_INLINE_VISIBILITY
5652 bool operator==(const student_t_distribution& __x,
5653 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005654 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005655 friend _LIBCPP_INLINE_VISIBILITY
5656 bool operator!=(const student_t_distribution& __x,
5657 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005658 {return !(__x == __y);}
5659};
5660
5661template <class _RealType>
5662template<class _URNG>
5663_RealType
5664student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5665{
5666 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005667 return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005668}
5669
5670template <class _CharT, class _Traits, class _RT>
5671basic_ostream<_CharT, _Traits>&
5672operator<<(basic_ostream<_CharT, _Traits>& __os,
5673 const student_t_distribution<_RT>& __x)
5674{
5675 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005676 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5677 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005678 __os << __x.n();
5679 return __os;
5680}
5681
5682template <class _CharT, class _Traits, class _RT>
5683basic_istream<_CharT, _Traits>&
5684operator>>(basic_istream<_CharT, _Traits>& __is,
5685 student_t_distribution<_RT>& __x)
5686{
5687 typedef student_t_distribution<_RT> _Eng;
5688 typedef typename _Eng::result_type result_type;
5689 typedef typename _Eng::param_type param_type;
5690 __save_flags<_CharT, _Traits> _(__is);
5691 __is.flags(ios_base::dec | ios_base::skipws);
5692 result_type __n;
5693 __is >> __n;
5694 if (!__is.fail())
5695 __x.param(param_type(__n));
5696 return __is;
5697}
5698
Howard Hinnant551d8e42010-05-19 01:53:57 +00005699// discrete_distribution
5700
5701template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005702class _LIBCPP_VISIBLE discrete_distribution
Howard Hinnant551d8e42010-05-19 01:53:57 +00005703{
5704public:
5705 // types
5706 typedef _IntType result_type;
5707
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005708 class _LIBCPP_VISIBLE param_type
Howard Hinnant551d8e42010-05-19 01:53:57 +00005709 {
5710 vector<double> __p_;
5711 public:
5712 typedef discrete_distribution distribution_type;
5713
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005715 param_type() {}
5716 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005718 param_type(_InputIterator __f, _InputIterator __l)
5719 : __p_(__f, __l) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005720#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005722 param_type(initializer_list<double> __wl)
5723 : __p_(__wl.begin(), __wl.end()) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005724#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005725 template<class _UnaryOperation>
5726 param_type(size_t __nw, double __xmin, double __xmax,
5727 _UnaryOperation __fw);
5728
5729 vector<double> probabilities() const;
5730
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005731 friend _LIBCPP_INLINE_VISIBILITY
5732 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005733 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005734 friend _LIBCPP_INLINE_VISIBILITY
5735 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005736 {return !(__x == __y);}
5737
5738 private:
5739 void __init();
5740
5741 friend class discrete_distribution;
5742
5743 template <class _CharT, class _Traits, class _IT>
5744 friend
5745 basic_ostream<_CharT, _Traits>&
5746 operator<<(basic_ostream<_CharT, _Traits>& __os,
5747 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005748
Howard Hinnant551d8e42010-05-19 01:53:57 +00005749 template <class _CharT, class _Traits, class _IT>
5750 friend
5751 basic_istream<_CharT, _Traits>&
5752 operator>>(basic_istream<_CharT, _Traits>& __is,
5753 discrete_distribution<_IT>& __x);
5754 };
5755
5756private:
5757 param_type __p_;
5758
5759public:
5760 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005762 discrete_distribution() {}
5763 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005765 discrete_distribution(_InputIterator __f, _InputIterator __l)
5766 : __p_(__f, __l) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005767#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005769 discrete_distribution(initializer_list<double> __wl)
5770 : __p_(__wl) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005771#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005772 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005774 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5775 _UnaryOperation __fw)
5776 : __p_(__nw, __xmin, __xmax, __fw) {}
5777 explicit discrete_distribution(const param_type& __p)
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005779 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005781 void reset() {}
5782
5783 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005784 template<class _URNG>
5785 _LIBCPP_INLINE_VISIBILITY
5786 result_type operator()(_URNG& __g)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005787 {return (*this)(__g, __p_);}
5788 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5789
5790 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005792 vector<double> probabilities() const {return __p_.probabilities();}
5793
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005795 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005797 void param(const param_type& __p) {__p_ = __p;}
5798
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005800 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005802 result_type max() const {return __p_.__p_.size();}
5803
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005804 friend _LIBCPP_INLINE_VISIBILITY
5805 bool operator==(const discrete_distribution& __x,
5806 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005807 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005808 friend _LIBCPP_INLINE_VISIBILITY
5809 bool operator!=(const discrete_distribution& __x,
5810 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005811 {return !(__x == __y);}
5812
5813 template <class _CharT, class _Traits, class _IT>
5814 friend
5815 basic_ostream<_CharT, _Traits>&
5816 operator<<(basic_ostream<_CharT, _Traits>& __os,
5817 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005818
Howard Hinnant551d8e42010-05-19 01:53:57 +00005819 template <class _CharT, class _Traits, class _IT>
5820 friend
5821 basic_istream<_CharT, _Traits>&
5822 operator>>(basic_istream<_CharT, _Traits>& __is,
5823 discrete_distribution<_IT>& __x);
5824};
5825
5826template<class _IntType>
5827template<class _UnaryOperation>
5828discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5829 double __xmin,
5830 double __xmax,
5831 _UnaryOperation __fw)
5832{
5833 if (__nw > 1)
5834 {
5835 __p_.reserve(__nw - 1);
5836 double __d = (__xmax - __xmin) / __nw;
5837 double __d2 = __d / 2;
5838 for (size_t __k = 0; __k < __nw; ++__k)
5839 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5840 __init();
5841 }
5842}
5843
5844template<class _IntType>
5845void
5846discrete_distribution<_IntType>::param_type::__init()
5847{
5848 if (!__p_.empty())
5849 {
5850 if (__p_.size() > 1)
5851 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005852 double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
5853 for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
Howard Hinnant551d8e42010-05-19 01:53:57 +00005854 __i < __e; ++__i)
5855 *__i /= __s;
5856 vector<double> __t(__p_.size() - 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005857 _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00005858 swap(__p_, __t);
5859 }
5860 else
5861 {
5862 __p_.clear();
5863 __p_.shrink_to_fit();
5864 }
5865 }
5866}
5867
5868template<class _IntType>
5869vector<double>
5870discrete_distribution<_IntType>::param_type::probabilities() const
5871{
5872 size_t __n = __p_.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00005873 _VSTD::vector<double> __p(__n+1);
5874 _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00005875 if (__n > 0)
5876 __p[__n] = 1 - __p_[__n-1];
5877 else
5878 __p[0] = 1;
5879 return __p;
5880}
5881
5882template<class _IntType>
5883template<class _URNG>
5884_IntType
5885discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
5886{
5887 uniform_real_distribution<double> __gen;
5888 return static_cast<_IntType>(
Howard Hinnant0949eed2011-06-30 21:18:19 +00005889 _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
Howard Hinnant551d8e42010-05-19 01:53:57 +00005890 __p.__p_.begin());
5891}
5892
5893template <class _CharT, class _Traits, class _IT>
5894basic_ostream<_CharT, _Traits>&
5895operator<<(basic_ostream<_CharT, _Traits>& __os,
5896 const discrete_distribution<_IT>& __x)
5897{
5898 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005899 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5900 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005901 _CharT __sp = __os.widen(' ');
5902 __os.fill(__sp);
5903 size_t __n = __x.__p_.__p_.size();
5904 __os << __n;
5905 for (size_t __i = 0; __i < __n; ++__i)
5906 __os << __sp << __x.__p_.__p_[__i];
5907 return __os;
5908}
5909
5910template <class _CharT, class _Traits, class _IT>
5911basic_istream<_CharT, _Traits>&
5912operator>>(basic_istream<_CharT, _Traits>& __is,
5913 discrete_distribution<_IT>& __x)
5914{
5915 typedef discrete_distribution<_IT> _Eng;
5916 typedef typename _Eng::result_type result_type;
5917 typedef typename _Eng::param_type param_type;
5918 __save_flags<_CharT, _Traits> _(__is);
5919 __is.flags(ios_base::dec | ios_base::skipws);
5920 size_t __n;
5921 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005922 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005923 for (size_t __i = 0; __i < __n; ++__i)
5924 __is >> __p[__i];
5925 if (!__is.fail())
5926 swap(__x.__p_.__p_, __p);
5927 return __is;
5928}
5929
Howard Hinnantd6d11712010-05-20 15:11:46 +00005930// piecewise_constant_distribution
5931
5932template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005933class _LIBCPP_VISIBLE piecewise_constant_distribution
Howard Hinnantd6d11712010-05-20 15:11:46 +00005934{
5935public:
5936 // types
5937 typedef _RealType result_type;
5938
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005939 class _LIBCPP_VISIBLE param_type
Howard Hinnantd6d11712010-05-20 15:11:46 +00005940 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00005941 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00005942 vector<result_type> __densities_;
5943 vector<result_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005944 public:
5945 typedef piecewise_constant_distribution distribution_type;
5946
5947 param_type();
5948 template<class _InputIteratorB, class _InputIteratorW>
5949 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5950 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00005951#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00005952 template<class _UnaryOperation>
5953 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00005954#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00005955 template<class _UnaryOperation>
5956 param_type(size_t __nw, result_type __xmin, result_type __xmax,
5957 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00005958 param_type & operator=(const param_type& __rhs);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005959
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00005961 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00005963 vector<result_type> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005964
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005965 friend _LIBCPP_INLINE_VISIBILITY
5966 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:40 +00005967 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005968 friend _LIBCPP_INLINE_VISIBILITY
5969 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00005970 {return !(__x == __y);}
5971
5972 private:
5973 void __init();
5974
5975 friend class piecewise_constant_distribution;
5976
5977 template <class _CharT, class _Traits, class _RT>
5978 friend
5979 basic_ostream<_CharT, _Traits>&
5980 operator<<(basic_ostream<_CharT, _Traits>& __os,
5981 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005982
Howard Hinnantd6d11712010-05-20 15:11:46 +00005983 template <class _CharT, class _Traits, class _RT>
5984 friend
5985 basic_istream<_CharT, _Traits>&
5986 operator>>(basic_istream<_CharT, _Traits>& __is,
5987 piecewise_constant_distribution<_RT>& __x);
5988 };
5989
5990private:
5991 param_type __p_;
5992
5993public:
5994 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00005996 piecewise_constant_distribution() {}
5997 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00005999 piecewise_constant_distribution(_InputIteratorB __fB,
6000 _InputIteratorB __lB,
6001 _InputIteratorW __fW)
6002 : __p_(__fB, __lB, __fW) {}
6003
Howard Hinnante3e32912011-08-12 21:56:02 +00006004#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006005 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006007 piecewise_constant_distribution(initializer_list<result_type> __bl,
6008 _UnaryOperation __fw)
6009 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006010#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006011
6012 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006014 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6015 result_type __xmax, _UnaryOperation __fw)
6016 : __p_(__nw, __xmin, __xmax, __fw) {}
6017
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006019 explicit piecewise_constant_distribution(const param_type& __p)
6020 : __p_(__p) {}
6021
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006023 void reset() {}
6024
6025 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006026 template<class _URNG>
6027 _LIBCPP_INLINE_VISIBILITY
6028 result_type operator()(_URNG& __g)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006029 {return (*this)(__g, __p_);}
6030 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6031
6032 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006034 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006036 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnantd6d11712010-05-20 15:11:46 +00006037
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006039 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006041 void param(const param_type& __p) {__p_ = __p;}
6042
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006044 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006046 result_type max() const {return __p_.__b_.back();}
6047
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006048 friend _LIBCPP_INLINE_VISIBILITY
6049 bool operator==(const piecewise_constant_distribution& __x,
6050 const piecewise_constant_distribution& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006051 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006052 friend _LIBCPP_INLINE_VISIBILITY
6053 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnantd6d11712010-05-20 15:11:46 +00006054 const piecewise_constant_distribution& __y)
6055 {return !(__x == __y);}
6056
6057 template <class _CharT, class _Traits, class _RT>
6058 friend
6059 basic_ostream<_CharT, _Traits>&
6060 operator<<(basic_ostream<_CharT, _Traits>& __os,
6061 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006062
Howard Hinnantd6d11712010-05-20 15:11:46 +00006063 template <class _CharT, class _Traits, class _RT>
6064 friend
6065 basic_istream<_CharT, _Traits>&
6066 operator>>(basic_istream<_CharT, _Traits>& __is,
6067 piecewise_constant_distribution<_RT>& __x);
6068};
6069
6070template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006071typename piecewise_constant_distribution<_RealType>::param_type &
6072piecewise_constant_distribution<_RealType>::param_type::operator=
6073 (const param_type& __rhs)
6074{
6075// These can throw
6076 __b_.reserve (__rhs.__b_.size ());
6077 __densities_.reserve(__rhs.__densities_.size());
6078 __areas_.reserve (__rhs.__areas_.size());
6079
6080// These can not throw
6081 __b_ = __rhs.__b_;
6082 __densities_ = __rhs.__densities_;
6083 __areas_ = __rhs.__areas_;
6084 return *this;
6085}
6086
6087template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006088void
6089piecewise_constant_distribution<_RealType>::param_type::__init()
6090{
Howard Hinnant2a592542010-05-24 00:35:40 +00006091 // __densities_ contains non-normalized areas
Howard Hinnant0949eed2011-06-30 21:18:19 +00006092 result_type __total_area = _VSTD::accumulate(__densities_.begin(),
Howard Hinnant2a592542010-05-24 00:35:40 +00006093 __densities_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006094 result_type());
Howard Hinnant2a592542010-05-24 00:35:40 +00006095 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6096 __densities_[__i] /= __total_area;
6097 // __densities_ contains normalized areas
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006098 __areas_.assign(__densities_.size(), result_type());
Howard Hinnant0949eed2011-06-30 21:18:19 +00006099 _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
Howard Hinnant2a592542010-05-24 00:35:40 +00006100 __areas_.begin() + 1);
6101 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6102 __densities_.back() = 1 - __areas_.back(); // correct round off error
6103 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6104 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6105 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:46 +00006106}
6107
6108template<class _RealType>
6109piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:40 +00006110 : __b_(2),
Howard Hinnant54305402010-05-25 00:27:34 +00006111 __densities_(1, 1.0),
6112 __areas_(1, 0.0)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006113{
6114 __b_[1] = 1;
6115}
6116
6117template<class _RealType>
6118template<class _InputIteratorB, class _InputIteratorW>
6119piecewise_constant_distribution<_RealType>::param_type::param_type(
6120 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6121 : __b_(__fB, __lB)
6122{
6123 if (__b_.size() < 2)
6124 {
6125 __b_.resize(2);
6126 __b_[0] = 0;
6127 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006128 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006129 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006130 }
6131 else
6132 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006133 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006134 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:40 +00006135 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006136 __init();
6137 }
6138}
6139
Howard Hinnante3e32912011-08-12 21:56:02 +00006140#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6141
Howard Hinnantd6d11712010-05-20 15:11:46 +00006142template<class _RealType>
6143template<class _UnaryOperation>
6144piecewise_constant_distribution<_RealType>::param_type::param_type(
6145 initializer_list<result_type> __bl, _UnaryOperation __fw)
6146 : __b_(__bl.begin(), __bl.end())
6147{
6148 if (__b_.size() < 2)
6149 {
6150 __b_.resize(2);
6151 __b_[0] = 0;
6152 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006153 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006154 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006155 }
6156 else
6157 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006158 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006159 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006160 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006161 __init();
6162 }
6163}
6164
Howard Hinnante3e32912011-08-12 21:56:02 +00006165#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6166
Howard Hinnantd6d11712010-05-20 15:11:46 +00006167template<class _RealType>
6168template<class _UnaryOperation>
6169piecewise_constant_distribution<_RealType>::param_type::param_type(
6170 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6171 : __b_(__nw == 0 ? 2 : __nw + 1)
6172{
6173 size_t __n = __b_.size() - 1;
6174 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:40 +00006175 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006176 for (size_t __i = 0; __i < __n; ++__i)
6177 {
6178 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:40 +00006179 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006180 }
6181 __b_[__n] = __xmax;
6182 __init();
6183}
6184
6185template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006186template<class _URNG>
6187_RealType
6188piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6189{
6190 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006191 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006192 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006193 __u) - __p.__areas_.begin() - 1;
6194 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006195}
6196
6197template <class _CharT, class _Traits, class _RT>
6198basic_ostream<_CharT, _Traits>&
6199operator<<(basic_ostream<_CharT, _Traits>& __os,
6200 const piecewise_constant_distribution<_RT>& __x)
6201{
6202 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00006203 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6204 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006205 _CharT __sp = __os.widen(' ');
6206 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:40 +00006207 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006208 __os << __n;
6209 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006210 __os << __sp << __x.__p_.__b_[__i];
6211 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006212 __os << __sp << __n;
6213 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006214 __os << __sp << __x.__p_.__densities_[__i];
6215 __n = __x.__p_.__areas_.size();
6216 __os << __sp << __n;
6217 for (size_t __i = 0; __i < __n; ++__i)
6218 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006219 return __os;
6220}
6221
6222template <class _CharT, class _Traits, class _RT>
6223basic_istream<_CharT, _Traits>&
6224operator>>(basic_istream<_CharT, _Traits>& __is,
6225 piecewise_constant_distribution<_RT>& __x)
6226{
6227 typedef piecewise_constant_distribution<_RT> _Eng;
6228 typedef typename _Eng::result_type result_type;
6229 typedef typename _Eng::param_type param_type;
6230 __save_flags<_CharT, _Traits> _(__is);
6231 __is.flags(ios_base::dec | ios_base::skipws);
6232 size_t __n;
6233 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006234 vector<result_type> __b(__n);
6235 for (size_t __i = 0; __i < __n; ++__i)
6236 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:40 +00006237 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006238 vector<result_type> __densities(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006239 for (size_t __i = 0; __i < __n; ++__i)
6240 __is >> __densities[__i];
6241 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006242 vector<result_type> __areas(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006243 for (size_t __i = 0; __i < __n; ++__i)
6244 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006245 if (!__is.fail())
6246 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00006247 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:40 +00006248 swap(__x.__p_.__densities_, __densities);
6249 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006250 }
6251 return __is;
6252}
6253
Howard Hinnant54305402010-05-25 00:27:34 +00006254// piecewise_linear_distribution
6255
6256template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006257class _LIBCPP_VISIBLE piecewise_linear_distribution
Howard Hinnant54305402010-05-25 00:27:34 +00006258{
6259public:
6260 // types
6261 typedef _RealType result_type;
6262
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006263 class _LIBCPP_VISIBLE param_type
Howard Hinnant54305402010-05-25 00:27:34 +00006264 {
Howard Hinnant54305402010-05-25 00:27:34 +00006265 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006266 vector<result_type> __densities_;
6267 vector<result_type> __areas_;
Howard Hinnant54305402010-05-25 00:27:34 +00006268 public:
6269 typedef piecewise_linear_distribution distribution_type;
6270
6271 param_type();
6272 template<class _InputIteratorB, class _InputIteratorW>
6273 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6274 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00006275#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006276 template<class _UnaryOperation>
6277 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00006278#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006279 template<class _UnaryOperation>
6280 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6281 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006282 param_type & operator=(const param_type& __rhs);
6283
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006285 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006287 vector<result_type> densities() const {return __densities_;}
Howard Hinnant54305402010-05-25 00:27:34 +00006288
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006289 friend _LIBCPP_INLINE_VISIBILITY
6290 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006291 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006292 friend _LIBCPP_INLINE_VISIBILITY
6293 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006294 {return !(__x == __y);}
6295
6296 private:
6297 void __init();
6298
6299 friend class piecewise_linear_distribution;
6300
6301 template <class _CharT, class _Traits, class _RT>
6302 friend
6303 basic_ostream<_CharT, _Traits>&
6304 operator<<(basic_ostream<_CharT, _Traits>& __os,
6305 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006306
Howard Hinnant54305402010-05-25 00:27:34 +00006307 template <class _CharT, class _Traits, class _RT>
6308 friend
6309 basic_istream<_CharT, _Traits>&
6310 operator>>(basic_istream<_CharT, _Traits>& __is,
6311 piecewise_linear_distribution<_RT>& __x);
6312 };
6313
6314private:
6315 param_type __p_;
6316
6317public:
6318 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006320 piecewise_linear_distribution() {}
6321 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006323 piecewise_linear_distribution(_InputIteratorB __fB,
6324 _InputIteratorB __lB,
6325 _InputIteratorW __fW)
6326 : __p_(__fB, __lB, __fW) {}
Howard Hinnant324bb032010-08-22 00:02:43 +00006327
Howard Hinnante3e32912011-08-12 21:56:02 +00006328#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006329 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006331 piecewise_linear_distribution(initializer_list<result_type> __bl,
6332 _UnaryOperation __fw)
6333 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006334#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006335
6336 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006338 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6339 result_type __xmax, _UnaryOperation __fw)
6340 : __p_(__nw, __xmin, __xmax, __fw) {}
6341
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006343 explicit piecewise_linear_distribution(const param_type& __p)
6344 : __p_(__p) {}
6345
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006347 void reset() {}
6348
6349 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006350 template<class _URNG>
6351 _LIBCPP_INLINE_VISIBILITY
6352 result_type operator()(_URNG& __g)
Howard Hinnant54305402010-05-25 00:27:34 +00006353 {return (*this)(__g, __p_);}
6354 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6355
6356 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006358 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006360 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant54305402010-05-25 00:27:34 +00006361
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006363 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006365 void param(const param_type& __p) {__p_ = __p;}
6366
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006368 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006370 result_type max() const {return __p_.__b_.back();}
6371
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006372 friend _LIBCPP_INLINE_VISIBILITY
6373 bool operator==(const piecewise_linear_distribution& __x,
6374 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006375 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006376 friend _LIBCPP_INLINE_VISIBILITY
6377 bool operator!=(const piecewise_linear_distribution& __x,
6378 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006379 {return !(__x == __y);}
6380
6381 template <class _CharT, class _Traits, class _RT>
6382 friend
6383 basic_ostream<_CharT, _Traits>&
6384 operator<<(basic_ostream<_CharT, _Traits>& __os,
6385 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006386
Howard Hinnant54305402010-05-25 00:27:34 +00006387 template <class _CharT, class _Traits, class _RT>
6388 friend
6389 basic_istream<_CharT, _Traits>&
6390 operator>>(basic_istream<_CharT, _Traits>& __is,
6391 piecewise_linear_distribution<_RT>& __x);
6392};
6393
6394template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006395typename piecewise_linear_distribution<_RealType>::param_type &
6396piecewise_linear_distribution<_RealType>::param_type::operator=
6397 (const param_type& __rhs)
6398{
6399// These can throw
6400 __b_.reserve (__rhs.__b_.size ());
6401 __densities_.reserve(__rhs.__densities_.size());
6402 __areas_.reserve (__rhs.__areas_.size());
6403
6404// These can not throw
6405 __b_ = __rhs.__b_;
6406 __densities_ = __rhs.__densities_;
6407 __areas_ = __rhs.__areas_;
6408 return *this;
6409}
6410
6411
6412template<class _RealType>
Howard Hinnant54305402010-05-25 00:27:34 +00006413void
6414piecewise_linear_distribution<_RealType>::param_type::__init()
6415{
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006416 __areas_.assign(__densities_.size() - 1, result_type());
6417 result_type _S = 0;
Howard Hinnant54305402010-05-25 00:27:34 +00006418 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6419 {
6420 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6421 (__b_[__i+1] - __b_[__i]) * .5;
6422 _S += __areas_[__i];
6423 }
6424 for (size_t __i = __areas_.size(); __i > 1;)
6425 {
6426 --__i;
6427 __areas_[__i] = __areas_[__i-1] / _S;
6428 }
6429 __areas_[0] = 0;
6430 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6431 __areas_[__i] += __areas_[__i-1];
6432 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6433 __densities_[__i] /= _S;
6434}
6435
6436template<class _RealType>
6437piecewise_linear_distribution<_RealType>::param_type::param_type()
6438 : __b_(2),
6439 __densities_(2, 1.0),
6440 __areas_(1, 0.0)
6441{
6442 __b_[1] = 1;
6443}
6444
6445template<class _RealType>
6446template<class _InputIteratorB, class _InputIteratorW>
6447piecewise_linear_distribution<_RealType>::param_type::param_type(
6448 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6449 : __b_(__fB, __lB)
6450{
6451 if (__b_.size() < 2)
6452 {
6453 __b_.resize(2);
6454 __b_[0] = 0;
6455 __b_[1] = 1;
6456 __densities_.assign(2, 1.0);
6457 __areas_.assign(1, 0.0);
6458 }
6459 else
6460 {
6461 __densities_.reserve(__b_.size());
6462 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6463 __densities_.push_back(*__fW);
6464 __init();
6465 }
6466}
6467
Howard Hinnante3e32912011-08-12 21:56:02 +00006468#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6469
Howard Hinnant54305402010-05-25 00:27:34 +00006470template<class _RealType>
6471template<class _UnaryOperation>
6472piecewise_linear_distribution<_RealType>::param_type::param_type(
6473 initializer_list<result_type> __bl, _UnaryOperation __fw)
6474 : __b_(__bl.begin(), __bl.end())
6475{
6476 if (__b_.size() < 2)
6477 {
6478 __b_.resize(2);
6479 __b_[0] = 0;
6480 __b_[1] = 1;
6481 __densities_.assign(2, 1.0);
6482 __areas_.assign(1, 0.0);
6483 }
6484 else
6485 {
6486 __densities_.reserve(__b_.size());
6487 for (size_t __i = 0; __i < __b_.size(); ++__i)
6488 __densities_.push_back(__fw(__b_[__i]));
6489 __init();
6490 }
6491}
6492
Howard Hinnante3e32912011-08-12 21:56:02 +00006493#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6494
Howard Hinnant54305402010-05-25 00:27:34 +00006495template<class _RealType>
6496template<class _UnaryOperation>
6497piecewise_linear_distribution<_RealType>::param_type::param_type(
6498 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6499 : __b_(__nw == 0 ? 2 : __nw + 1)
6500{
6501 size_t __n = __b_.size() - 1;
6502 result_type __d = (__xmax - __xmin) / __n;
6503 __densities_.reserve(__b_.size());
6504 for (size_t __i = 0; __i < __n; ++__i)
6505 {
6506 __b_[__i] = __xmin + __i * __d;
6507 __densities_.push_back(__fw(__b_[__i]));
6508 }
6509 __b_[__n] = __xmax;
6510 __densities_.push_back(__fw(__b_[__n]));
6511 __init();
6512}
6513
6514template<class _RealType>
6515template<class _URNG>
6516_RealType
6517piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6518{
6519 typedef uniform_real_distribution<result_type> _Gen;
6520 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006521 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006522 __u) - __p.__areas_.begin() - 1;
Howard Hinnant54305402010-05-25 00:27:34 +00006523 __u -= __p.__areas_[__k];
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006524 const result_type __dk = __p.__densities_[__k];
6525 const result_type __dk1 = __p.__densities_[__k+1];
6526 const result_type __deltad = __dk1 - __dk;
Howard Hinnant54305402010-05-25 00:27:34 +00006527 const result_type __bk = __p.__b_[__k];
6528 if (__deltad == 0)
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006529 return __u / __dk + __bk;
Howard Hinnant54305402010-05-25 00:27:34 +00006530 const result_type __bk1 = __p.__b_[__k+1];
6531 const result_type __deltab = __bk1 - __bk;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006532 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnant0949eed2011-06-30 21:18:19 +00006533 _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006534 __deltad;
Howard Hinnant54305402010-05-25 00:27:34 +00006535}
6536
6537template <class _CharT, class _Traits, class _RT>
6538basic_ostream<_CharT, _Traits>&
6539operator<<(basic_ostream<_CharT, _Traits>& __os,
6540 const piecewise_linear_distribution<_RT>& __x)
6541{
6542 __save_flags<_CharT, _Traits> _(__os);
6543 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6544 ios_base::scientific);
6545 _CharT __sp = __os.widen(' ');
6546 __os.fill(__sp);
6547 size_t __n = __x.__p_.__b_.size();
6548 __os << __n;
6549 for (size_t __i = 0; __i < __n; ++__i)
6550 __os << __sp << __x.__p_.__b_[__i];
6551 __n = __x.__p_.__densities_.size();
6552 __os << __sp << __n;
6553 for (size_t __i = 0; __i < __n; ++__i)
6554 __os << __sp << __x.__p_.__densities_[__i];
6555 __n = __x.__p_.__areas_.size();
6556 __os << __sp << __n;
6557 for (size_t __i = 0; __i < __n; ++__i)
6558 __os << __sp << __x.__p_.__areas_[__i];
6559 return __os;
6560}
6561
6562template <class _CharT, class _Traits, class _RT>
6563basic_istream<_CharT, _Traits>&
6564operator>>(basic_istream<_CharT, _Traits>& __is,
6565 piecewise_linear_distribution<_RT>& __x)
6566{
6567 typedef piecewise_linear_distribution<_RT> _Eng;
6568 typedef typename _Eng::result_type result_type;
6569 typedef typename _Eng::param_type param_type;
Howard Hinnant54305402010-05-25 00:27:34 +00006570 __save_flags<_CharT, _Traits> _(__is);
6571 __is.flags(ios_base::dec | ios_base::skipws);
6572 size_t __n;
6573 __is >> __n;
6574 vector<result_type> __b(__n);
6575 for (size_t __i = 0; __i < __n; ++__i)
6576 __is >> __b[__i];
6577 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006578 vector<result_type> __densities(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006579 for (size_t __i = 0; __i < __n; ++__i)
6580 __is >> __densities[__i];
6581 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006582 vector<result_type> __areas(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006583 for (size_t __i = 0; __i < __n; ++__i)
6584 __is >> __areas[__i];
6585 if (!__is.fail())
6586 {
6587 swap(__x.__p_.__b_, __b);
6588 swap(__x.__p_.__densities_, __densities);
6589 swap(__x.__p_.__areas_, __areas);
6590 }
6591 return __is;
6592}
6593
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00006594_LIBCPP_END_NAMESPACE_STD
6595
6596#endif // _LIBCPP_RANDOM