blob: 7099af5ea86674ac7961e104e3f7a1c6b460abbc [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
Howard Hinnant66c6f972011-11-29 16:45:27 +00001649#include <__undef_min_max>
1650
Howard Hinnant08e17472011-10-17 20:05:10 +00001651#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +00001653#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001654
1655_LIBCPP_BEGIN_NAMESPACE_STD
1656
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001657// __is_seed_sequence
1658
1659template <class _Sseq, class _Engine>
1660struct __is_seed_sequence
1661{
1662 static const bool value =
1663 !is_convertible<_Sseq, typename _Engine::result_type>::value &&
1664 !is_same<typename remove_cv<_Sseq>::type, _Engine>::value;
1665};
1666
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667// linear_congruential_engine
1668
1669template <unsigned long long __a, unsigned long long __c,
Howard Hinnant99968442011-11-29 18:15:50 +00001670 unsigned long long __m, unsigned long long _Mp,
1671 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_Mp-__c)/__a)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001672struct __lce_ta;
1673
1674// 64
1675
1676template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1677struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1678{
1679 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681 static result_type next(result_type __x)
1682 {
1683 // Schrage's algorithm
1684 const result_type __q = __m / __a;
1685 const result_type __r = __m % __a;
1686 const result_type __t0 = __a * (__x % __q);
1687 const result_type __t1 = __r * (__x / __q);
1688 __x = __t0 + (__t0 < __t1) * __m - __t1;
1689 __x += __c - (__x >= __m - __c) * __m;
1690 return __x;
1691 }
1692};
1693
1694template <unsigned long long __a, unsigned long long __m>
1695struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1696{
1697 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699 static result_type next(result_type __x)
1700 {
1701 // Schrage's algorithm
1702 const result_type __q = __m / __a;
1703 const result_type __r = __m % __a;
1704 const result_type __t0 = __a * (__x % __q);
1705 const result_type __t1 = __r * (__x / __q);
1706 __x = __t0 + (__t0 < __t1) * __m - __t1;
1707 return __x;
1708 }
1709};
1710
1711template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1712struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1713{
1714 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001716 static result_type next(result_type __x)
1717 {
1718 return (__a * __x + __c) % __m;
1719 }
1720};
1721
1722template <unsigned long long __a, unsigned long long __c>
1723struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1724{
1725 typedef unsigned long long result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001727 static result_type next(result_type __x)
1728 {
1729 return __a * __x + __c;
1730 }
1731};
1732
1733// 32
1734
Howard Hinnant99968442011-11-29 18:15:50 +00001735template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1736struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001737{
1738 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 static result_type next(result_type __x)
1741 {
Howard Hinnant99968442011-11-29 18:15:50 +00001742 const result_type __a = static_cast<result_type>(_Ap);
1743 const result_type __c = static_cast<result_type>(_Cp);
1744 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745 // Schrage's algorithm
1746 const result_type __q = __m / __a;
1747 const result_type __r = __m % __a;
1748 const result_type __t0 = __a * (__x % __q);
1749 const result_type __t1 = __r * (__x / __q);
1750 __x = __t0 + (__t0 < __t1) * __m - __t1;
1751 __x += __c - (__x >= __m - __c) * __m;
1752 return __x;
1753 }
1754};
1755
Howard Hinnant99968442011-11-29 18:15:50 +00001756template <unsigned long long _Ap, unsigned long long _Mp>
1757struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758{
1759 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 static result_type next(result_type __x)
1762 {
Howard Hinnant99968442011-11-29 18:15:50 +00001763 const result_type __a = static_cast<result_type>(_Ap);
1764 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765 // Schrage's algorithm
1766 const result_type __q = __m / __a;
1767 const result_type __r = __m % __a;
1768 const result_type __t0 = __a * (__x % __q);
1769 const result_type __t1 = __r * (__x / __q);
1770 __x = __t0 + (__t0 < __t1) * __m - __t1;
1771 return __x;
1772 }
1773};
1774
Howard Hinnant99968442011-11-29 18:15:50 +00001775template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1776struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), false>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777{
1778 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001780 static result_type next(result_type __x)
1781 {
Howard Hinnant99968442011-11-29 18:15:50 +00001782 const result_type __a = static_cast<result_type>(_Ap);
1783 const result_type __c = static_cast<result_type>(_Cp);
1784 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001785 return (__a * __x + __c) % __m;
1786 }
1787};
1788
Howard Hinnant99968442011-11-29 18:15:50 +00001789template <unsigned long long _Ap, unsigned long long _Cp>
1790struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791{
1792 typedef unsigned result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794 static result_type next(result_type __x)
1795 {
Howard Hinnant99968442011-11-29 18:15:50 +00001796 const result_type __a = static_cast<result_type>(_Ap);
1797 const result_type __c = static_cast<result_type>(_Cp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001798 return __a * __x + __c;
1799 }
1800};
1801
1802// 16
1803
1804template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1805struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1806{
1807 typedef unsigned short result_type;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001809 static result_type next(result_type __x)
1810 {
1811 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1812 }
1813};
1814
1815template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1816class linear_congruential_engine;
1817
1818template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001819 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820basic_ostream<_CharT, _Traits>&
1821operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00001822 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823
1824template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001825 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826basic_istream<_CharT, _Traits>&
1827operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00001828 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829
1830template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001831class _LIBCPP_VISIBLE linear_congruential_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832{
1833public:
1834 // types
1835 typedef _UIntType result_type;
1836
1837private:
1838 result_type __x_;
1839
Howard Hinnant99968442011-11-29 18:15:50 +00001840 static const result_type _Mp = result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841
1842 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1843 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1844public:
1845 static const result_type _Min = __c == 0u ? 1u: 0u;
1846 static const result_type _Max = __m - 1u;
1847 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1848
1849 // engine characteristics
1850 static const/*expr*/ result_type multiplier = __a;
1851 static const/*expr*/ result_type increment = __c;
1852 static const/*expr*/ result_type modulus = __m;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 static const/*expr*/ result_type min() {return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001856 static const/*expr*/ result_type max() {return _Max;}
1857 static const/*expr*/ result_type default_seed = 1u;
1858
1859 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 explicit linear_congruential_engine(result_type __s = default_seed)
1862 {seed(__s);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00001863 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001865 explicit linear_congruential_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001866 typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001867 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869 void seed(result_type __s = default_seed)
1870 {seed(integral_constant<bool, __m == 0>(),
1871 integral_constant<bool, __c == 0>(), __s);}
1872 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874 typename enable_if
1875 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001876 __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877 void
1878 >::type
1879 seed(_Sseq& __q)
1880 {__seed(__q, integral_constant<unsigned,
1881 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1882 : (__m-1) / 0x100000000ull)>());}
1883
1884 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886 result_type operator()()
Howard Hinnant99968442011-11-29 18:15:50 +00001887 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1890
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001891 friend _LIBCPP_INLINE_VISIBILITY
1892 bool operator==(const linear_congruential_engine& __x,
1893 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894 {return __x.__x_ == __y.__x_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001895 friend _LIBCPP_INLINE_VISIBILITY
1896 bool operator!=(const linear_congruential_engine& __x,
1897 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 {return !(__x == __y);}
1899
1900private:
1901
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1908 1 : __s % __m;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001910 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1911
1912 template<class _Sseq>
1913 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1914 template<class _Sseq>
1915 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1916
1917 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001918 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001919 friend
1920 basic_ostream<_CharT, _Traits>&
1921 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00001922 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001923
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001925 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001926 friend
1927 basic_istream<_CharT, _Traits>&
1928 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00001929 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930};
1931
1932template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1933template<class _Sseq>
1934void
1935linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1936 integral_constant<unsigned, 1>)
1937{
1938 const unsigned __k = 1;
1939 uint32_t __ar[__k+3];
1940 __q.generate(__ar, __ar + __k + 3);
1941 result_type __s = static_cast<result_type>(__ar[3] % __m);
1942 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1943}
1944
1945template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1946template<class _Sseq>
1947void
1948linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1949 integral_constant<unsigned, 2>)
1950{
1951 const unsigned __k = 2;
1952 uint32_t __ar[__k+3];
1953 __q.generate(__ar, __ar + __k + 3);
1954 result_type __s = static_cast<result_type>((__ar[3] +
1955 (uint64_t)__ar[4] << 32) % __m);
1956 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1957}
1958
1959template <class _CharT, class _Traits>
1960class __save_flags
1961{
1962 typedef basic_ios<_CharT, _Traits> __stream_type;
1963 typedef typename __stream_type::fmtflags fmtflags;
1964
1965 __stream_type& __stream_;
1966 fmtflags __fmtflags_;
1967 _CharT __fill_;
1968
1969 __save_flags(const __save_flags&);
1970 __save_flags& operator=(const __save_flags&);
1971public:
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001973 explicit __save_flags(__stream_type& __stream)
1974 : __stream_(__stream),
1975 __fmtflags_(__stream.flags()),
1976 __fill_(__stream.fill())
1977 {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 ~__save_flags()
1980 {
1981 __stream_.flags(__fmtflags_);
1982 __stream_.fill(__fill_);
1983 }
1984};
1985
1986template <class _CharT, class _Traits,
1987 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001988inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001989basic_ostream<_CharT, _Traits>&
1990operator<<(basic_ostream<_CharT, _Traits>& __os,
1991 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1992{
1993 __save_flags<_CharT, _Traits> _(__os);
1994 __os.flags(ios_base::dec | ios_base::left);
1995 __os.fill(__os.widen(' '));
1996 return __os << __x.__x_;
1997}
1998
1999template <class _CharT, class _Traits,
2000 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2001basic_istream<_CharT, _Traits>&
2002operator>>(basic_istream<_CharT, _Traits>& __is,
2003 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2004{
2005 __save_flags<_CharT, _Traits> _(__is);
2006 __is.flags(ios_base::dec | ios_base::skipws);
2007 _UIntType __t;
2008 __is >> __t;
2009 if (!__is.fail())
2010 __x.__x_ = __t;
2011 return __is;
2012}
2013
2014typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2015 minstd_rand0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2017 minstd_rand;
Howard Hinnantd6d11712010-05-20 15:11:46 +00002018typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019// mersenne_twister_engine
2020
2021template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2022 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2023 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2024class mersenne_twister_engine;
2025
Howard Hinnant99968442011-11-29 18:15:50 +00002026template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2027 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2028 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002029bool
Howard Hinnant99968442011-11-29 18:15:50 +00002030operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2031 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2032 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2033 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034
Howard Hinnant99968442011-11-29 18:15:50 +00002035template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2036 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2037 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038bool
Howard Hinnant99968442011-11-29 18:15:50 +00002039operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2040 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2041 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2042 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043
2044template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002045 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2046 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2047 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048basic_ostream<_CharT, _Traits>&
2049operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002050 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2051 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052
2053template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002054 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2055 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2056 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002057basic_istream<_CharT, _Traits>&
2058operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002059 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2060 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061
2062template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2063 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2064 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002065class _LIBCPP_VISIBLE mersenne_twister_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002066{
2067public:
2068 // types
2069 typedef _UIntType result_type;
2070
2071private:
2072 result_type __x_[__n];
2073 size_t __i_;
2074
2075 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2076 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
2077 static const result_type _Dt = numeric_limits<result_type>::digits;
2078 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2079 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2080 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2081 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2082 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2083 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2084 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2085public:
2086 static const result_type _Min = 0;
2087 static const result_type _Max = __w == _Dt ? result_type(~0) :
2088 (result_type(1) << __w) - result_type(1);
2089 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2090 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2091 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2092 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2093 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2094 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2095
2096 // engine characteristics
2097 static const/*expr*/ size_t word_size = __w;
2098 static const/*expr*/ size_t state_size = __n;
2099 static const/*expr*/ size_t shift_size = __m;
2100 static const/*expr*/ size_t mask_bits = __r;
2101 static const/*expr*/ result_type xor_mask = __a;
2102 static const/*expr*/ size_t tempering_u = __u;
2103 static const/*expr*/ result_type tempering_d = __d;
2104 static const/*expr*/ size_t tempering_s = __s;
2105 static const/*expr*/ result_type tempering_b = __b;
2106 static const/*expr*/ size_t tempering_t = __t;
2107 static const/*expr*/ result_type tempering_c = __c;
2108 static const/*expr*/ size_t tempering_l = __l;
2109 static const/*expr*/ result_type initialization_multiplier = __f;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111 static const/*expr*/ result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113 static const/*expr*/ result_type max() { return _Max; }
2114 static const/*expr*/ result_type default_seed = 5489u;
2115
2116 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 explicit mersenne_twister_engine(result_type __sd = default_seed)
2119 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002120 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002122 explicit mersenne_twister_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002123 typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124 {seed(__q);}
2125 void seed(result_type __sd = default_seed);
2126 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 typename enable_if
2129 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002130 __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 void
2132 >::type
2133 seed(_Sseq& __q)
2134 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2135
2136 // generating functions
2137 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002139 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2140
Howard Hinnant99968442011-11-29 18:15:50 +00002141 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2142 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2143 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 friend
2145 bool
Howard Hinnant99968442011-11-29 18:15:50 +00002146 operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2147 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2148 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2149 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002150
Howard Hinnant99968442011-11-29 18:15:50 +00002151 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2152 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2153 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002154 friend
2155 bool
Howard Hinnant99968442011-11-29 18:15:50 +00002156 operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2157 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2158 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2159 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160
2161 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002162 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2163 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2164 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165 friend
2166 basic_ostream<_CharT, _Traits>&
2167 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002168 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2169 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170
2171 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002172 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2173 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2174 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 friend
2176 basic_istream<_CharT, _Traits>&
2177 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002178 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2179 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180private:
2181
2182 template<class _Sseq>
2183 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2184 template<class _Sseq>
2185 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2186
2187 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002189 static
2190 typename enable_if
2191 <
2192 __count < __w,
2193 result_type
2194 >::type
2195 __lshift(result_type __x) {return (__x << __count) & _Max;}
2196
2197 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199 static
2200 typename enable_if
2201 <
2202 (__count >= __w),
2203 result_type
2204 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00002205 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206
2207 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002209 static
2210 typename enable_if
2211 <
2212 __count < _Dt,
2213 result_type
2214 >::type
2215 __rshift(result_type __x) {return __x >> __count;}
2216
2217 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219 static
2220 typename enable_if
2221 <
2222 (__count >= _Dt),
2223 result_type
2224 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00002225 __rshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002226};
2227
2228template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2229 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2230 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2231void
2232mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2233 __t, __c, __l, __f>::seed(result_type __sd)
2234{ // __w >= 2
2235 __x_[0] = __sd & _Max;
2236 for (size_t __i = 1; __i < __n; ++__i)
2237 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2238 __i_ = 0;
2239}
2240
2241template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2242 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2243 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2244template<class _Sseq>
2245void
2246mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2247 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2248{
2249 const unsigned __k = 1;
2250 uint32_t __ar[__n * __k];
2251 __q.generate(__ar, __ar + __n * __k);
2252 for (size_t __i = 0; __i < __n; ++__i)
2253 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2254 const result_type __mask = __r == _Dt ? result_type(~0) :
2255 (result_type(1) << __r) - result_type(1);
2256 __i_ = 0;
2257 if ((__x_[0] & ~__mask) == 0)
2258 {
2259 for (size_t __i = 1; __i < __n; ++__i)
2260 if (__x_[__i] != 0)
2261 return;
2262 __x_[0] = _Max;
2263 }
2264}
2265
2266template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2267 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2268 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2269template<class _Sseq>
2270void
2271mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2272 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2273{
2274 const unsigned __k = 2;
2275 uint32_t __ar[__n * __k];
2276 __q.generate(__ar, __ar + __n * __k);
2277 for (size_t __i = 0; __i < __n; ++__i)
2278 __x_[__i] = static_cast<result_type>(
2279 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2280 const result_type __mask = __r == _Dt ? result_type(~0) :
2281 (result_type(1) << __r) - result_type(1);
2282 __i_ = 0;
2283 if ((__x_[0] & ~__mask) == 0)
2284 {
2285 for (size_t __i = 1; __i < __n; ++__i)
2286 if (__x_[__i] != 0)
2287 return;
2288 __x_[0] = _Max;
2289 }
2290}
2291
2292template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2293 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2294 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2295_UIntType
2296mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2297 __t, __c, __l, __f>::operator()()
2298{
2299 const size_t __j = (__i_ + 1) % __n;
2300 const result_type __mask = __r == _Dt ? result_type(~0) :
2301 (result_type(1) << __r) - result_type(1);
Howard Hinnant99968442011-11-29 18:15:50 +00002302 const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 const size_t __k = (__i_ + __m) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00002304 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002305 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2306 __i_ = __j;
2307 __z ^= __lshift<__s>(__z) & __b;
2308 __z ^= __lshift<__t>(__z) & __c;
2309 return __z ^ __rshift<__l>(__z);
2310}
2311
Howard Hinnant99968442011-11-29 18:15:50 +00002312template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2313 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2314 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002315bool
Howard Hinnant99968442011-11-29 18:15:50 +00002316operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2317 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2318 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2319 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320{
2321 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:50 +00002322 return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002323 if (__x.__i_ == 0 || __y.__i_ == 0)
2324 {
Howard Hinnant99968442011-11-29 18:15:50 +00002325 size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002326 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002327 __y.__x_ + __y.__i_))
2328 return false;
2329 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00002330 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
2331 return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002332 }
2333 if (__x.__i_ < __y.__i_)
2334 {
Howard Hinnant99968442011-11-29 18:15:50 +00002335 size_t __j = _Np - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002336 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002337 __y.__x_ + __y.__i_))
2338 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002339 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002340 __y.__x_))
2341 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002342 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002343 __y.__x_ + (_Np - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 }
Howard Hinnant99968442011-11-29 18:15:50 +00002345 size_t __j = _Np - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002346 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347 __x.__x_ + __x.__i_))
2348 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002349 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350 __x.__x_))
2351 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002352 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002353 __x.__x_ + (_Np - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354}
2355
Howard Hinnant99968442011-11-29 18:15:50 +00002356template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2357 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2358 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002359inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002360bool
Howard Hinnant99968442011-11-29 18:15:50 +00002361operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2362 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2363 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2364 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365{
2366 return !(__x == __y);
2367}
2368
2369template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002370 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2371 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2372 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002373basic_ostream<_CharT, _Traits>&
2374operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002375 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2376 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002377{
2378 __save_flags<_CharT, _Traits> _(__os);
2379 __os.flags(ios_base::dec | ios_base::left);
2380 _CharT __sp = __os.widen(' ');
2381 __os.fill(__sp);
2382 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:50 +00002383 for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002384 __os << __sp << __x.__x_[__j];
2385 for (size_t __j = 0; __j < __x.__i_; ++__j)
2386 __os << __sp << __x.__x_[__j];
2387 return __os;
2388}
2389
2390template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002391 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2392 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2393 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002394basic_istream<_CharT, _Traits>&
2395operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002396 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2397 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398{
2399 __save_flags<_CharT, _Traits> _(__is);
2400 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:50 +00002401 _UI __t[_Np];
2402 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002403 __is >> __t[__i];
2404 if (!__is.fail())
2405 {
Howard Hinnant99968442011-11-29 18:15:50 +00002406 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407 __x.__x_[__i] = __t[__i];
2408 __x.__i_ = 0;
2409 }
2410 return __is;
2411}
2412
2413typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2414 0x9908b0df, 11, 0xffffffff,
2415 7, 0x9d2c5680,
2416 15, 0xefc60000,
2417 18, 1812433253> mt19937;
2418typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2419 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2420 17, 0x71d67fffeda60000ULL,
2421 37, 0xfff7eee000000000ULL,
2422 43, 6364136223846793005ULL> mt19937_64;
2423
2424// subtract_with_carry_engine
2425
2426template<class _UIntType, size_t __w, size_t __s, size_t __r>
2427class subtract_with_carry_engine;
2428
Howard Hinnant99968442011-11-29 18:15:50 +00002429template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002430bool
2431operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002432 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2433 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002434
Howard Hinnant99968442011-11-29 18:15:50 +00002435template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436bool
2437operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002438 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2439 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440
2441template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002442 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443basic_ostream<_CharT, _Traits>&
2444operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002445 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002446
2447template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002448 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449basic_istream<_CharT, _Traits>&
2450operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002451 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452
2453template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002454class _LIBCPP_VISIBLE subtract_with_carry_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455{
2456public:
2457 // types
2458 typedef _UIntType result_type;
2459
2460private:
2461 result_type __x_[__r];
2462 result_type __c_;
2463 size_t __i_;
2464
2465 static const result_type _Dt = numeric_limits<result_type>::digits;
2466 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2467 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2468 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2469 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2470public:
2471 static const result_type _Min = 0;
2472 static const result_type _Max = __w == _Dt ? result_type(~0) :
2473 (result_type(1) << __w) - result_type(1);
2474 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2475
2476 // engine characteristics
2477 static const/*expr*/ size_t word_size = __w;
2478 static const/*expr*/ size_t short_lag = __s;
2479 static const/*expr*/ size_t long_lag = __r;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002481 static const/*expr*/ result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002483 static const/*expr*/ result_type max() { return _Max; }
2484 static const/*expr*/ result_type default_seed = 19780503u;
2485
2486 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002488 explicit subtract_with_carry_engine(result_type __sd = default_seed)
2489 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002490 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002492 explicit subtract_with_carry_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002493 typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002496 void seed(result_type __sd = default_seed)
2497 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2498 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002500 typename enable_if
2501 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002502 __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503 void
2504 >::type
2505 seed(_Sseq& __q)
2506 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2507
2508 // generating functions
2509 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002511 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2512
Howard Hinnant99968442011-11-29 18:15:50 +00002513 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514 friend
2515 bool
2516 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002517 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2518 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002519
Howard Hinnant99968442011-11-29 18:15:50 +00002520 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002521 friend
2522 bool
2523 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002524 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2525 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002526
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002528 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529 friend
2530 basic_ostream<_CharT, _Traits>&
2531 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002532 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002533
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002534 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002535 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536 friend
2537 basic_istream<_CharT, _Traits>&
2538 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002539 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540
2541private:
2542
2543 void seed(result_type __sd, integral_constant<unsigned, 1>);
2544 void seed(result_type __sd, integral_constant<unsigned, 2>);
2545 template<class _Sseq>
2546 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2547 template<class _Sseq>
2548 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2549};
2550
2551template<class _UIntType, size_t __w, size_t __s, size_t __r>
2552void
2553subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2554 integral_constant<unsigned, 1>)
2555{
2556 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2557 __e(__sd == 0u ? default_seed : __sd);
2558 for (size_t __i = 0; __i < __r; ++__i)
2559 __x_[__i] = static_cast<result_type>(__e() & _Max);
2560 __c_ = __x_[__r-1] == 0;
2561 __i_ = 0;
2562}
2563
2564template<class _UIntType, size_t __w, size_t __s, size_t __r>
2565void
2566subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2567 integral_constant<unsigned, 2>)
2568{
2569 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2570 __e(__sd == 0u ? default_seed : __sd);
2571 for (size_t __i = 0; __i < __r; ++__i)
Howard Hinnant43edf2d2011-08-15 17:22:22 +00002572 {
2573 result_type __e0 = __e();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 __x_[__i] = static_cast<result_type>(
Howard Hinnant43edf2d2011-08-15 17:22:22 +00002575 (__e0 + ((uint64_t)__e() << 32)) & _Max);
2576 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577 __c_ = __x_[__r-1] == 0;
2578 __i_ = 0;
2579}
2580
2581template<class _UIntType, size_t __w, size_t __s, size_t __r>
2582template<class _Sseq>
2583void
2584subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2585 integral_constant<unsigned, 1>)
2586{
2587 const unsigned __k = 1;
2588 uint32_t __ar[__r * __k];
2589 __q.generate(__ar, __ar + __r * __k);
2590 for (size_t __i = 0; __i < __r; ++__i)
2591 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2592 __c_ = __x_[__r-1] == 0;
2593 __i_ = 0;
2594}
2595
2596template<class _UIntType, size_t __w, size_t __s, size_t __r>
2597template<class _Sseq>
2598void
2599subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2600 integral_constant<unsigned, 2>)
2601{
2602 const unsigned __k = 2;
2603 uint32_t __ar[__r * __k];
2604 __q.generate(__ar, __ar + __r * __k);
2605 for (size_t __i = 0; __i < __r; ++__i)
2606 __x_[__i] = static_cast<result_type>(
2607 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2608 __c_ = __x_[__r-1] == 0;
2609 __i_ = 0;
2610}
2611
2612template<class _UIntType, size_t __w, size_t __s, size_t __r>
2613_UIntType
2614subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2615{
2616 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2617 result_type& __xr = __x_[__i_];
2618 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2619 __xr = (__xs - __xr - __c_) & _Max;
2620 __c_ = __new_c;
2621 __i_ = (__i_ + 1) % __r;
2622 return __xr;
2623}
2624
Howard Hinnant99968442011-11-29 18:15:50 +00002625template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626bool
2627operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002628 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2629 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630{
2631 if (__x.__c_ != __y.__c_)
2632 return false;
2633 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:50 +00002634 return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002635 if (__x.__i_ == 0 || __y.__i_ == 0)
2636 {
Howard Hinnant99968442011-11-29 18:15:50 +00002637 size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __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;
2641 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00002642 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
2643 return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644 }
2645 if (__x.__i_ < __y.__i_)
2646 {
Howard Hinnant99968442011-11-29 18:15:50 +00002647 size_t __j = _Rp - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002648 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649 __y.__x_ + __y.__i_))
2650 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002651 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652 __y.__x_))
2653 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002654 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002655 __y.__x_ + (_Rp - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002656 }
Howard Hinnant99968442011-11-29 18:15:50 +00002657 size_t __j = _Rp - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002658 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002659 __x.__x_ + __x.__i_))
2660 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002661 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002662 __x.__x_))
2663 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002664 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002665 __x.__x_ + (_Rp - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666}
2667
Howard Hinnant99968442011-11-29 18:15:50 +00002668template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002669inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002670bool
2671operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002672 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2673 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002674{
2675 return !(__x == __y);
2676}
2677
2678template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002679 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002680basic_ostream<_CharT, _Traits>&
2681operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002682 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683{
2684 __save_flags<_CharT, _Traits> _(__os);
2685 __os.flags(ios_base::dec | ios_base::left);
2686 _CharT __sp = __os.widen(' ');
2687 __os.fill(__sp);
2688 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:50 +00002689 for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690 __os << __sp << __x.__x_[__j];
2691 for (size_t __j = 0; __j < __x.__i_; ++__j)
2692 __os << __sp << __x.__x_[__j];
2693 __os << __sp << __x.__c_;
2694 return __os;
2695}
2696
2697template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002698 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699basic_istream<_CharT, _Traits>&
2700operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002701 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702{
2703 __save_flags<_CharT, _Traits> _(__is);
2704 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:50 +00002705 _UI __t[_Rp+1];
2706 for (size_t __i = 0; __i < _Rp+1; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707 __is >> __t[__i];
2708 if (!__is.fail())
2709 {
Howard Hinnant99968442011-11-29 18:15:50 +00002710 for (size_t __i = 0; __i < _Rp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711 __x.__x_[__i] = __t[__i];
Howard Hinnant99968442011-11-29 18:15:50 +00002712 __x.__c_ = __t[_Rp];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713 __x.__i_ = 0;
2714 }
2715 return __is;
2716}
2717
2718typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2719typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2720
2721// discard_block_engine
2722
2723template<class _Engine, size_t __p, size_t __r>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002724class _LIBCPP_VISIBLE discard_block_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002725{
2726 _Engine __e_;
2727 int __n_;
2728
2729 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2730 static_assert(__r <= __p, "discard_block_engine invalid parameters");
2731public:
2732 // types
2733 typedef typename _Engine::result_type result_type;
2734
2735 // engine characteristics
2736 static const/*expr*/ size_t block_size = __p;
2737 static const/*expr*/ size_t used_block = __r;
2738
2739 // Temporary work around for lack of constexpr
2740 static const result_type _Min = _Engine::_Min;
2741 static const result_type _Max = _Engine::_Max;
2742
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744 static const/*expr*/ result_type min() { return _Engine::min(); }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746 static const/*expr*/ result_type max() { return _Engine::max(); }
2747
2748 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002750 discard_block_engine() : __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002752 explicit discard_block_engine(const _Engine& __e)
2753 : __e_(__e), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002754#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002756 explicit discard_block_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002757 : __e_(_VSTD::move(__e)), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002758#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002760 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002761 template<class _Sseq>
2762 _LIBCPP_INLINE_VISIBILITY
2763 explicit discard_block_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002764 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00002765 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766 : __e_(__q), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768 void seed() {__e_.seed(); __n_ = 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002771 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002773 typename enable_if
2774 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002775 __is_seed_sequence<_Sseq, discard_block_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00002776 void
2777 >::type
2778 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002779
2780 // generating functions
2781 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2784
2785 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 const _Engine& base() const {return __e_;}
2788
Howard Hinnant99968442011-11-29 18:15:50 +00002789 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002790 friend
2791 bool
2792 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002793 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2794 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002795
Howard Hinnant99968442011-11-29 18:15:50 +00002796 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797 friend
2798 bool
2799 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002800 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2801 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002802
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002803 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002804 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002805 friend
2806 basic_ostream<_CharT, _Traits>&
2807 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002808 const discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002809
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002810 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002811 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002812 friend
2813 basic_istream<_CharT, _Traits>&
2814 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002815 discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002816};
2817
2818template<class _Engine, size_t __p, size_t __r>
2819typename discard_block_engine<_Engine, __p, __r>::result_type
2820discard_block_engine<_Engine, __p, __r>::operator()()
2821{
2822 if (__n_ >= __r)
2823 {
2824 __e_.discard(__p - __r);
2825 __n_ = 0;
2826 }
2827 ++__n_;
2828 return __e_();
2829}
2830
Howard Hinnant99968442011-11-29 18:15:50 +00002831template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002832inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833bool
Howard Hinnant99968442011-11-29 18:15:50 +00002834operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2835 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002836{
2837 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2838}
2839
Howard Hinnant99968442011-11-29 18:15:50 +00002840template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002841inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002842bool
Howard Hinnant99968442011-11-29 18:15:50 +00002843operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2844 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002845{
2846 return !(__x == __y);
2847}
2848
2849template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002850 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002851basic_ostream<_CharT, _Traits>&
2852operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002853 const discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002854{
2855 __save_flags<_CharT, _Traits> _(__os);
2856 __os.flags(ios_base::dec | ios_base::left);
2857 _CharT __sp = __os.widen(' ');
2858 __os.fill(__sp);
2859 return __os << __x.__e_ << __sp << __x.__n_;
2860}
2861
2862template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002863 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864basic_istream<_CharT, _Traits>&
2865operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002866 discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002867{
2868 __save_flags<_CharT, _Traits> _(__is);
2869 __is.flags(ios_base::dec | ios_base::skipws);
2870 _Eng __e;
2871 int __n;
2872 __is >> __e >> __n;
2873 if (!__is.fail())
2874 {
2875 __x.__e_ = __e;
2876 __x.__n_ = __n;
2877 }
2878 return __is;
2879}
2880
2881typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2882typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2883
2884// independent_bits_engine
2885
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002886template<class _Engine, size_t __w, class _UIntType>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002887class _LIBCPP_VISIBLE independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002888{
Howard Hinnant99968442011-11-29 18:15:50 +00002889 template <class _UI, _UI _R0, size_t _Wp, size_t _Mp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002890 class __get_n
2891 {
2892 static const size_t _Dt = numeric_limits<_UI>::digits;
Howard Hinnant99968442011-11-29 18:15:50 +00002893 static const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
2894 static const size_t _W0 = _Wp / _Np;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002895 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
2896 public:
Howard Hinnant99968442011-11-29 18:15:50 +00002897 static const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002898 };
2899public:
2900 // types
2901 typedef _UIntType result_type;
2902
2903private:
2904 _Engine __e_;
2905
2906 static const result_type _Dt = numeric_limits<result_type>::digits;
2907 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2908 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2909
2910 typedef typename _Engine::result_type _Engine_result_type;
2911 typedef typename conditional
2912 <
2913 sizeof(_Engine_result_type) <= sizeof(result_type),
2914 result_type,
2915 _Engine_result_type
2916 >::type _Working_result_type;
2917 // Temporary work around for lack of constexpr
Howard Hinnant99968442011-11-29 18:15:50 +00002918 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002919 + _Working_result_type(1);
Howard Hinnant99968442011-11-29 18:15:50 +00002920 static const size_t __m = __log2<_Working_result_type, _Rp>::value;
2921 static const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922 static const size_t __w0 = __w / __n;
2923 static const size_t __n0 = __n - __w % __n;
2924 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2925 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2926 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
Howard Hinnant99968442011-11-29 18:15:50 +00002927 (_Rp >> __w0) << __w0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002928 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
Howard Hinnant99968442011-11-29 18:15:50 +00002929 (_Rp >> (__w0+1)) << (__w0+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002930 static const _Engine_result_type __mask0 = __w0 > 0 ?
2931 _Engine_result_type(~0) >> (_EDt - __w0) :
2932 _Engine_result_type(0);
2933 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2934 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2935 _Engine_result_type(~0);
2936public:
2937 static const result_type _Min = 0;
2938 static const result_type _Max = __w == _Dt ? result_type(~0) :
2939 (result_type(1) << __w) - result_type(1);
2940 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2941
2942 // engine characteristics
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002944 static const/*expr*/ result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002946 static const/*expr*/ result_type max() { return _Max; }
2947
2948 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002950 independent_bits_engine() {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002952 explicit independent_bits_engine(const _Engine& __e)
2953 : __e_(__e) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002954#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002956 explicit independent_bits_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002957 : __e_(_VSTD::move(__e)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002958#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002960 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002961 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002963 explicit independent_bits_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002964 typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00002965 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002966 : __e_(__q) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002968 void seed() {__e_.seed();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002969 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002970 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002971 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002973 typename enable_if
2974 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002975 __is_seed_sequence<_Sseq, independent_bits_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00002976 void
2977 >::type
2978 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002979
2980 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002981 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002982 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002984 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2985
2986 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002988 const _Engine& base() const {return __e_;}
2989
Howard Hinnant99968442011-11-29 18:15:50 +00002990 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002991 friend
2992 bool
2993 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002994 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
2995 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002996
Howard Hinnant99968442011-11-29 18:15:50 +00002997 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002998 friend
2999 bool
3000 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003001 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3002 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003003
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003004 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003005 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003006 friend
3007 basic_ostream<_CharT, _Traits>&
3008 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003009 const independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003010
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003011 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003012 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003013 friend
3014 basic_istream<_CharT, _Traits>&
3015 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003016 independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003017
3018private:
3019 result_type __eval(false_type);
3020 result_type __eval(true_type);
3021
3022 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003024 static
3025 typename enable_if
3026 <
3027 __count < _Dt,
3028 result_type
3029 >::type
3030 __lshift(result_type __x) {return __x << __count;}
3031
3032 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003034 static
3035 typename enable_if
3036 <
3037 (__count >= _Dt),
3038 result_type
3039 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00003040 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003041};
3042
3043template<class _Engine, size_t __w, class _UIntType>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003044inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003045_UIntType
3046independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
3047{
3048 return static_cast<result_type>(__e_() & __mask0);
3049}
3050
3051template<class _Engine, size_t __w, class _UIntType>
3052_UIntType
3053independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
3054{
Howard Hinnant99968442011-11-29 18:15:50 +00003055 result_type _Sp = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003056 for (size_t __k = 0; __k < __n0; ++__k)
3057 {
3058 _Engine_result_type __u;
3059 do
3060 {
3061 __u = __e_() - _Engine::min();
3062 } while (__u >= __y0);
Howard Hinnant99968442011-11-29 18:15:50 +00003063 _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003064 }
3065 for (size_t __k = __n0; __k < __n; ++__k)
3066 {
3067 _Engine_result_type __u;
3068 do
3069 {
3070 __u = __e_() - _Engine::min();
3071 } while (__u >= __y1);
Howard Hinnant99968442011-11-29 18:15:50 +00003072 _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003073 }
Howard Hinnant99968442011-11-29 18:15:50 +00003074 return _Sp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003075}
3076
Howard Hinnant99968442011-11-29 18:15:50 +00003077template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003078inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003079bool
3080operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003081 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3082 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003083{
3084 return __x.base() == __y.base();
3085}
3086
Howard Hinnant99968442011-11-29 18:15:50 +00003087template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003088inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003089bool
3090operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003091 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3092 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003093{
3094 return !(__x == __y);
3095}
3096
3097template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003098 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003099basic_ostream<_CharT, _Traits>&
3100operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003101 const independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003102{
3103 return __os << __x.base();
3104}
3105
3106template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003107 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003108basic_istream<_CharT, _Traits>&
3109operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003110 independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003111{
3112 _Eng __e;
3113 __is >> __e;
3114 if (!__is.fail())
3115 __x.__e_ = __e;
3116 return __is;
3117}
3118
3119// shuffle_order_engine
3120
3121template <uint64_t _Xp, uint64_t _Yp>
3122struct __ugcd
3123{
3124 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
3125};
3126
3127template <uint64_t _Xp>
3128struct __ugcd<_Xp, 0>
3129{
3130 static const uint64_t value = _Xp;
3131};
3132
Howard Hinnant99968442011-11-29 18:15:50 +00003133template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003134class __uratio
3135{
Howard Hinnant99968442011-11-29 18:15:50 +00003136 static_assert(_Dp != 0, "__uratio divide by 0");
3137 static const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003138public:
Howard Hinnant99968442011-11-29 18:15:50 +00003139 static const uint64_t num = _Np / __gcd;
3140 static const uint64_t den = _Dp / __gcd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003141
3142 typedef __uratio<num, den> type;
3143};
3144
3145template<class _Engine, size_t __k>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003146class _LIBCPP_VISIBLE shuffle_order_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003147{
3148 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3149public:
3150 // types
3151 typedef typename _Engine::result_type result_type;
3152
3153private:
3154 _Engine __e_;
3155 result_type _V_[__k];
3156 result_type _Y_;
3157
3158public:
3159 // engine characteristics
3160 static const/*expr*/ size_t table_size = __k;
Howard Hinnant324bb032010-08-22 00:02:43 +00003161
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003162 static const result_type _Min = _Engine::_Min;
3163 static const result_type _Max = _Engine::_Max;
3164 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003166 static const/*expr*/ result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003168 static const/*expr*/ result_type max() { return _Max; }
3169
Howard Hinnant99968442011-11-29 18:15:50 +00003170 static const unsigned long long _Rp = _Max - _Min + 1ull;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003171
3172 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003174 shuffle_order_engine() {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003176 explicit shuffle_order_engine(const _Engine& __e)
3177 : __e_(__e) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003178#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003180 explicit shuffle_order_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003181 : __e_(_VSTD::move(__e)) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003182#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003184 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnantec3773c2011-12-01 20:21:04 +00003185 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00003187 explicit shuffle_order_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003188 typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00003189 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003190 : __e_(__q) {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003192 void seed() {__e_.seed(); __init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003194 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003195 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003197 typename enable_if
3198 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003199 __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00003200 void
3201 >::type
3202 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003203
3204 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003205 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003206 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003208 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3209
3210 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003212 const _Engine& base() const {return __e_;}
3213
3214private:
Howard Hinnant99968442011-11-29 18:15:50 +00003215 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003216 friend
3217 bool
3218 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003219 const shuffle_order_engine<_Eng, _Kp>& __x,
3220 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003221
Howard Hinnant99968442011-11-29 18:15:50 +00003222 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003223 friend
3224 bool
3225 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003226 const shuffle_order_engine<_Eng, _Kp>& __x,
3227 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003228
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003229 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003230 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003231 friend
3232 basic_ostream<_CharT, _Traits>&
3233 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003234 const shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003235
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003236 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003237 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003238 friend
3239 basic_istream<_CharT, _Traits>&
3240 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003241 shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003242
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003244 void __init()
3245 {
3246 for (size_t __i = 0; __i < __k; ++__i)
3247 _V_[__i] = __e_();
3248 _Y_ = __e_();
3249 }
3250
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003252 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003254 result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003255
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003257 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003259 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3260
Howard Hinnant99968442011-11-29 18:15:50 +00003261 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003263 typename enable_if
3264 <
Howard Hinnant99968442011-11-29 18:15:50 +00003265 (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003266 result_type
3267 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00003268 __eval(__uratio<_Np, _Dp>)
3269 {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003270
Howard Hinnant99968442011-11-29 18:15:50 +00003271 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003273 typename enable_if
3274 <
Howard Hinnant99968442011-11-29 18:15:50 +00003275 __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003276 result_type
3277 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00003278 __eval(__uratio<_Np, _Dp>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003279 {
Howard Hinnant99968442011-11-29 18:15:50 +00003280 const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
3281 / __uratio<_Np, _Dp>::den);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003282 _Y_ = _V_[__j];
3283 _V_[__j] = __e_();
3284 return _Y_;
3285 }
3286
3287 template <uint64_t __n, uint64_t __d>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003289 result_type __evalf()
3290 {
Howard Hinnant99968442011-11-29 18:15:50 +00003291 const double _Fp = __d == 0 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003292 __n / (2. * 0x8000000000000000ull) :
3293 __n / (double)__d;
Howard Hinnant99968442011-11-29 18:15:50 +00003294 const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003295 _Y_ = _V_[__j];
3296 _V_[__j] = __e_();
3297 return _Y_;
3298 }
3299};
3300
Howard Hinnant99968442011-11-29 18:15:50 +00003301template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003302bool
3303operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003304 const shuffle_order_engine<_Eng, _Kp>& __x,
3305 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003306{
Howard Hinnant99968442011-11-29 18:15:50 +00003307 return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003308 __x.__e_ == __y.__e_;
3309}
3310
Howard Hinnant99968442011-11-29 18:15:50 +00003311template<class _Eng, size_t _Kp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003312inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003313bool
3314operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003315 const shuffle_order_engine<_Eng, _Kp>& __x,
3316 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003317{
3318 return !(__x == __y);
3319}
3320
3321template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003322 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003323basic_ostream<_CharT, _Traits>&
3324operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003325 const shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003326{
3327 __save_flags<_CharT, _Traits> _(__os);
3328 __os.flags(ios_base::dec | ios_base::left);
3329 _CharT __sp = __os.widen(' ');
3330 __os.fill(__sp);
3331 __os << __x.__e_ << __sp << __x._V_[0];
Howard Hinnant99968442011-11-29 18:15:50 +00003332 for (size_t __i = 1; __i < _Kp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003333 __os << __sp << __x._V_[__i];
3334 return __os << __sp << __x._Y_;
3335}
3336
3337template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003338 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003339basic_istream<_CharT, _Traits>&
3340operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003341 shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003342{
Howard Hinnant99968442011-11-29 18:15:50 +00003343 typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003344 __save_flags<_CharT, _Traits> _(__is);
3345 __is.flags(ios_base::dec | ios_base::skipws);
3346 _Eng __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003347 result_type _Vp[_Kp+1];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003348 __is >> __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003349 for (size_t __i = 0; __i < _Kp+1; ++__i)
3350 __is >> _Vp[__i];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003351 if (!__is.fail())
3352 {
3353 __x.__e_ = __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003354 for (size_t __i = 0; __i < _Kp; ++__i)
3355 __x._V_[__i] = _Vp[__i];
3356 __x._Y_ = _Vp[_Kp];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003357 }
3358 return __is;
3359}
3360
3361typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3362
3363// random_device
3364
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003365class _LIBCPP_VISIBLE random_device
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003366{
3367 int __f_;
3368public:
3369 // types
3370 typedef unsigned result_type;
3371
3372 // generator characteristics
3373 static const result_type _Min = 0;
3374 static const result_type _Max = 0xFFFFFFFFu;
3375
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003377 static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003379 static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003380
3381 // constructors
3382 explicit random_device(const string& __token = "/dev/urandom");
3383 ~random_device();
3384
3385 // generating functions
3386 result_type operator()();
3387
3388 // property functions
3389 double entropy() const;
3390
3391private:
3392 // no copy functions
3393 random_device(const random_device&); // = delete;
3394 random_device& operator=(const random_device&); // = delete;
3395};
3396
3397// seed_seq
3398
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003399class _LIBCPP_VISIBLE seed_seq
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003400{
3401public:
3402 // types
3403 typedef uint32_t result_type;
3404
3405private:
3406 vector<result_type> __v_;
3407
3408 template<class _InputIterator>
3409 void init(_InputIterator __first, _InputIterator __last);
3410public:
3411 // constructors
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003413 seed_seq() {}
Howard Hinnante3e32912011-08-12 21:56:02 +00003414#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003415 template<class _Tp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003417 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00003418#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant324bb032010-08-22 00:02:43 +00003419
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003420 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003422 seed_seq(_InputIterator __first, _InputIterator __last)
3423 {init(__first, __last);}
3424
3425 // generating functions
3426 template<class _RandomAccessIterator>
3427 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3428
3429 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003431 size_t size() const {return __v_.size();}
3432 template<class _OutputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003434 void param(_OutputIterator __dest) const
Howard Hinnant0949eed2011-06-30 21:18:19 +00003435 {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436
3437private:
3438 // no copy functions
3439 seed_seq(const seed_seq&); // = delete;
3440 void operator=(const seed_seq&); // = delete;
3441
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003443 static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003444};
3445
3446template<class _InputIterator>
3447void
3448seed_seq::init(_InputIterator __first, _InputIterator __last)
3449{
3450 for (_InputIterator __s = __first; __s != __last; ++__s)
3451 __v_.push_back(*__s & 0xFFFFFFFF);
3452}
3453
3454template<class _RandomAccessIterator>
3455void
3456seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3457{
3458 if (__first != __last)
3459 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003460 _VSTD::fill(__first, __last, 0x8b8b8b8b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003461 const size_t __n = static_cast<size_t>(__last - __first);
3462 const size_t __s = __v_.size();
3463 const size_t __t = (__n >= 623) ? 11
3464 : (__n >= 68) ? 7
3465 : (__n >= 39) ? 5
3466 : (__n >= 7) ? 3
3467 : (__n - 1) / 2;
3468 const size_t __p = (__n - __t) / 2;
3469 const size_t __q = __p + __t;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003470 const size_t __m = _VSTD::max(__s + 1, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003471 // __k = 0;
3472 {
Howard Hinnant99968442011-11-29 18:15:50 +00003473 result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003474 ^ __first[__n - 1]);
3475 __first[__p] += __r;
3476 __r += __s;
3477 __first[__q] += __r;
3478 __first[0] = __r;
3479 }
3480 for (size_t __k = 1; __k <= __s; ++__k)
3481 {
3482 const size_t __kmodn = __k % __n;
3483 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003484 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003485 ^ __first[(__k - 1) % __n]);
3486 __first[__kpmodn] += __r;
3487 __r += __kmodn + __v_[__k-1];
3488 __first[(__k + __q) % __n] += __r;
3489 __first[__kmodn] = __r;
3490 }
3491 for (size_t __k = __s + 1; __k < __m; ++__k)
3492 {
3493 const size_t __kmodn = __k % __n;
3494 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003495 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003496 ^ __first[(__k - 1) % __n]);
3497 __first[__kpmodn] += __r;
3498 __r += __kmodn;
3499 __first[(__k + __q) % __n] += __r;
3500 __first[__kmodn] = __r;
3501 }
3502 for (size_t __k = __m; __k < __m + __n; ++__k)
3503 {
3504 const size_t __kmodn = __k % __n;
3505 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003506 result_type __r = 1566083941 * _Tp(__first[__kmodn] +
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003507 __first[__kpmodn] +
3508 __first[(__k - 1) % __n]);
3509 __first[__kpmodn] ^= __r;
3510 __r -= __kmodn;
3511 __first[(__k + __q) % __n] ^= __r;
3512 __first[__kmodn] = __r;
3513 }
3514 }
3515}
3516
Howard Hinnant30a840f2010-05-12 17:08:57 +00003517// generate_canonical
3518
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003519template<class _RealType, size_t __bits, class _URNG>
3520_RealType
3521generate_canonical(_URNG& __g)
3522{
3523 const size_t _Dt = numeric_limits<_RealType>::digits;
3524 const size_t __b = _Dt < __bits ? _Dt : __bits;
3525 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
3526 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
Howard Hinnant99968442011-11-29 18:15:50 +00003527 const _RealType _Rp = _URNG::_Max - _URNG::_Min + _RealType(1);
3528 _RealType __base = _Rp;
3529 _RealType _Sp = __g() - _URNG::_Min;
3530 for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp)
3531 _Sp += (__g() - _URNG::_Min) * __base;
3532 return _Sp / __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003533}
3534
Howard Hinnant30a840f2010-05-12 17:08:57 +00003535// uniform_int_distribution
3536
Howard Hinnantc3267212010-05-26 17:49:34 +00003537// in <algorithm>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003538
3539template <class _CharT, class _Traits, class _IT>
3540basic_ostream<_CharT, _Traits>&
3541operator<<(basic_ostream<_CharT, _Traits>& __os,
3542 const uniform_int_distribution<_IT>& __x)
3543{
3544 __save_flags<_CharT, _Traits> _(__os);
3545 __os.flags(ios_base::dec | ios_base::left);
3546 _CharT __sp = __os.widen(' ');
3547 __os.fill(__sp);
3548 return __os << __x.a() << __sp << __x.b();
3549}
3550
3551template <class _CharT, class _Traits, class _IT>
3552basic_istream<_CharT, _Traits>&
3553operator>>(basic_istream<_CharT, _Traits>& __is,
3554 uniform_int_distribution<_IT>& __x)
3555{
3556 typedef uniform_int_distribution<_IT> _Eng;
3557 typedef typename _Eng::result_type result_type;
3558 typedef typename _Eng::param_type param_type;
3559 __save_flags<_CharT, _Traits> _(__is);
3560 __is.flags(ios_base::dec | ios_base::skipws);
3561 result_type __a;
3562 result_type __b;
3563 __is >> __a >> __b;
3564 if (!__is.fail())
3565 __x.param(param_type(__a, __b));
3566 return __is;
3567}
3568
Howard Hinnant30a840f2010-05-12 17:08:57 +00003569// uniform_real_distribution
3570
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003571template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003572class _LIBCPP_VISIBLE uniform_real_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003573{
3574public:
3575 // types
3576 typedef _RealType result_type;
3577
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003578 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003579 {
3580 result_type __a_;
3581 result_type __b_;
3582 public:
3583 typedef uniform_real_distribution distribution_type;
3584
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586 explicit param_type(result_type __a = 0,
3587 result_type __b = 1)
3588 : __a_(__a), __b_(__b) {}
3589
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003591 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003593 result_type b() const {return __b_;}
3594
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003595 friend _LIBCPP_INLINE_VISIBILITY
3596 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003597 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003598 friend _LIBCPP_INLINE_VISIBILITY
3599 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600 {return !(__x == __y);}
3601 };
3602
3603private:
3604 param_type __p_;
3605
3606public:
3607 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003609 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3610 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003612 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003614 void reset() {}
3615
3616 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003617 template<class _URNG>
3618 _LIBCPP_INLINE_VISIBILITY
3619 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003620 {return (*this)(__g, __p_);}
3621 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3622
3623 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003625 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003627 result_type b() const {return __p_.b();}
3628
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632 void param(const param_type& __p) {__p_ = __p;}
3633
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003634 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003635 result_type min() const {return a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003637 result_type max() const {return b();}
3638
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003639 friend _LIBCPP_INLINE_VISIBILITY
3640 bool operator==(const uniform_real_distribution& __x,
3641 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003642 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003643 friend _LIBCPP_INLINE_VISIBILITY
3644 bool operator!=(const uniform_real_distribution& __x,
3645 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003646 {return !(__x == __y);}
3647};
3648
3649template<class _RealType>
3650template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003651inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003652typename uniform_real_distribution<_RealType>::result_type
3653uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3654{
3655 return (__p.b() - __p.a())
Howard Hinnant0949eed2011-06-30 21:18:19 +00003656 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657 + __p.a();
3658}
3659
3660template <class _CharT, class _Traits, class _RT>
3661basic_ostream<_CharT, _Traits>&
3662operator<<(basic_ostream<_CharT, _Traits>& __os,
3663 const uniform_real_distribution<_RT>& __x)
3664{
3665 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003666 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3667 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003668 _CharT __sp = __os.widen(' ');
3669 __os.fill(__sp);
3670 return __os << __x.a() << __sp << __x.b();
3671}
3672
3673template <class _CharT, class _Traits, class _RT>
3674basic_istream<_CharT, _Traits>&
3675operator>>(basic_istream<_CharT, _Traits>& __is,
3676 uniform_real_distribution<_RT>& __x)
3677{
3678 typedef uniform_real_distribution<_RT> _Eng;
3679 typedef typename _Eng::result_type result_type;
3680 typedef typename _Eng::param_type param_type;
3681 __save_flags<_CharT, _Traits> _(__is);
3682 __is.flags(ios_base::dec | ios_base::skipws);
3683 result_type __a;
3684 result_type __b;
3685 __is >> __a >> __b;
3686 if (!__is.fail())
3687 __x.param(param_type(__a, __b));
3688 return __is;
3689}
3690
Howard Hinnant30a840f2010-05-12 17:08:57 +00003691// bernoulli_distribution
3692
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003693class _LIBCPP_VISIBLE bernoulli_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003694{
3695public:
3696 // types
3697 typedef bool result_type;
3698
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003699 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003700 {
3701 double __p_;
3702 public:
3703 typedef bernoulli_distribution distribution_type;
3704
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003706 explicit param_type(double __p = 0.5) : __p_(__p) {}
3707
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003709 double p() const {return __p_;}
3710
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003711 friend _LIBCPP_INLINE_VISIBILITY
3712 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003713 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003714 friend _LIBCPP_INLINE_VISIBILITY
3715 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003716 {return !(__x == __y);}
3717 };
3718
3719private:
3720 param_type __p_;
3721
3722public:
3723 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003725 explicit bernoulli_distribution(double __p = 0.5)
3726 : __p_(param_type(__p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003730 void reset() {}
3731
3732 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003733 template<class _URNG>
3734 _LIBCPP_INLINE_VISIBILITY
3735 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003736 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003737 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003738
3739 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003741 double p() const {return __p_.p();}
3742
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003744 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003746 void param(const param_type& __p) {__p_ = __p;}
3747
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003749 result_type min() const {return false;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751 result_type max() const {return true;}
3752
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003753 friend _LIBCPP_INLINE_VISIBILITY
3754 bool operator==(const bernoulli_distribution& __x,
3755 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003756 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003757 friend _LIBCPP_INLINE_VISIBILITY
3758 bool operator!=(const bernoulli_distribution& __x,
3759 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003760 {return !(__x == __y);}
3761};
3762
Howard Hinnant03aad812010-05-11 23:26:59 +00003763template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003764inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003765bernoulli_distribution::result_type
3766bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3767{
Howard Hinnantd6d11712010-05-20 15:11:46 +00003768 uniform_real_distribution<double> __gen;
3769 return __gen(__g) < __p.p();
Howard Hinnant03aad812010-05-11 23:26:59 +00003770}
3771
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003772template <class _CharT, class _Traits>
3773basic_ostream<_CharT, _Traits>&
3774operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3775{
3776 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003777 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3778 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003779 _CharT __sp = __os.widen(' ');
3780 __os.fill(__sp);
3781 return __os << __x.p();
3782}
3783
3784template <class _CharT, class _Traits>
3785basic_istream<_CharT, _Traits>&
3786operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3787{
3788 typedef bernoulli_distribution _Eng;
3789 typedef typename _Eng::param_type param_type;
3790 __save_flags<_CharT, _Traits> _(__is);
3791 __is.flags(ios_base::dec | ios_base::skipws);
3792 double __p;
3793 __is >> __p;
3794 if (!__is.fail())
3795 __x.param(param_type(__p));
3796 return __is;
3797}
3798
Howard Hinnant30a840f2010-05-12 17:08:57 +00003799// binomial_distribution
3800
Howard Hinnant03aad812010-05-11 23:26:59 +00003801template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003802class _LIBCPP_VISIBLE binomial_distribution
Howard Hinnant03aad812010-05-11 23:26:59 +00003803{
3804public:
3805 // types
3806 typedef _IntType result_type;
3807
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003808 class _LIBCPP_VISIBLE param_type
Howard Hinnant03aad812010-05-11 23:26:59 +00003809 {
3810 result_type __t_;
3811 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003812 double __pr_;
3813 double __odds_ratio_;
3814 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003815 public:
3816 typedef binomial_distribution distribution_type;
3817
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003818 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003819
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003821 result_type t() const {return __t_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003823 double p() const {return __p_;}
3824
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003825 friend _LIBCPP_INLINE_VISIBILITY
3826 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003827 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003828 friend _LIBCPP_INLINE_VISIBILITY
3829 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003830 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003831
3832 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003833 };
3834
3835private:
3836 param_type __p_;
3837
3838public:
3839 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003841 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3842 : __p_(param_type(__t, __p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003844 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003846 void reset() {}
3847
3848 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003849 template<class _URNG>
3850 _LIBCPP_INLINE_VISIBILITY
3851 result_type operator()(_URNG& __g)
Howard Hinnant03aad812010-05-11 23:26:59 +00003852 {return (*this)(__g, __p_);}
3853 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3854
3855 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003857 result_type t() const {return __p_.t();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003859 double p() const {return __p_.p();}
3860
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003862 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003864 void param(const param_type& __p) {__p_ = __p;}
3865
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003867 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003869 result_type max() const {return t();}
3870
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003871 friend _LIBCPP_INLINE_VISIBILITY
3872 bool operator==(const binomial_distribution& __x,
3873 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003874 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003875 friend _LIBCPP_INLINE_VISIBILITY
3876 bool operator!=(const binomial_distribution& __x,
3877 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003878 {return !(__x == __y);}
3879};
3880
3881template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003882binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3883 : __t_(__t), __p_(__p)
3884{
3885 if (0 < __p_ && __p_ < 1)
3886 {
3887 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003888 __pr_ = _VSTD::exp(_VSTD::lgamma(__t_ + 1.) - _VSTD::lgamma(__r0_ + 1.) -
3889 _VSTD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
3890 (__t_ - __r0_) * _VSTD::log(1 - __p_));
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003891 __odds_ratio_ = __p_ / (1 - __p_);
3892 }
3893}
3894
3895template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00003896template<class _URNG>
3897_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003898binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00003899{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003900 if (__pr.__t_ == 0 || __pr.__p_ == 0)
3901 return 0;
3902 if (__pr.__p_ == 1)
3903 return __pr.__t_;
3904 uniform_real_distribution<double> __gen;
3905 double __u = __gen(__g) - __pr.__pr_;
3906 if (__u < 0)
3907 return __pr.__r0_;
3908 double __pu = __pr.__pr_;
3909 double __pd = __pu;
3910 result_type __ru = __pr.__r0_;
3911 result_type __rd = __ru;
3912 while (true)
3913 {
3914 if (__rd >= 1)
3915 {
3916 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3917 __u -= __pd;
3918 if (__u < 0)
3919 return __rd - 1;
3920 }
3921 --__rd;
3922 ++__ru;
3923 if (__ru <= __pr.__t_)
3924 {
3925 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3926 __u -= __pu;
3927 if (__u < 0)
3928 return __ru;
3929 }
3930 }
Howard Hinnant03aad812010-05-11 23:26:59 +00003931}
3932
3933template <class _CharT, class _Traits, class _IntType>
3934basic_ostream<_CharT, _Traits>&
3935operator<<(basic_ostream<_CharT, _Traits>& __os,
3936 const binomial_distribution<_IntType>& __x)
3937{
3938 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003939 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3940 ios_base::scientific);
Howard Hinnant03aad812010-05-11 23:26:59 +00003941 _CharT __sp = __os.widen(' ');
3942 __os.fill(__sp);
3943 return __os << __x.t() << __sp << __x.p();
3944}
3945
3946template <class _CharT, class _Traits, class _IntType>
3947basic_istream<_CharT, _Traits>&
3948operator>>(basic_istream<_CharT, _Traits>& __is,
3949 binomial_distribution<_IntType>& __x)
3950{
3951 typedef binomial_distribution<_IntType> _Eng;
3952 typedef typename _Eng::result_type result_type;
3953 typedef typename _Eng::param_type param_type;
3954 __save_flags<_CharT, _Traits> _(__is);
3955 __is.flags(ios_base::dec | ios_base::skipws);
3956 result_type __t;
3957 double __p;
3958 __is >> __t >> __p;
3959 if (!__is.fail())
3960 __x.param(param_type(__t, __p));
3961 return __is;
3962}
3963
Howard Hinnant30a840f2010-05-12 17:08:57 +00003964// exponential_distribution
3965
3966template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003967class _LIBCPP_VISIBLE exponential_distribution
Howard Hinnant30a840f2010-05-12 17:08:57 +00003968{
3969public:
3970 // types
3971 typedef _RealType result_type;
3972
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003973 class _LIBCPP_VISIBLE param_type
Howard Hinnant30a840f2010-05-12 17:08:57 +00003974 {
3975 result_type __lambda_;
3976 public:
3977 typedef exponential_distribution distribution_type;
3978
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003980 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3981
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003983 result_type lambda() const {return __lambda_;}
3984
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003985 friend _LIBCPP_INLINE_VISIBILITY
3986 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00003987 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003988 friend _LIBCPP_INLINE_VISIBILITY
3989 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00003990 {return !(__x == __y);}
3991 };
3992
3993private:
3994 param_type __p_;
3995
3996public:
3997 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00003999 explicit exponential_distribution(result_type __lambda = 1)
4000 : __p_(param_type(__lambda)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004002 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004004 void reset() {}
4005
4006 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004007 template<class _URNG>
4008 _LIBCPP_INLINE_VISIBILITY
4009 result_type operator()(_URNG& __g)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004010 {return (*this)(__g, __p_);}
4011 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4012
4013 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004015 result_type lambda() const {return __p_.lambda();}
4016
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004018 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004019 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004020 void param(const param_type& __p) {__p_ = __p;}
4021
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004023 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf40dc62010-05-16 17:56:20 +00004025 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00004026
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004027 friend _LIBCPP_INLINE_VISIBILITY
4028 bool operator==(const exponential_distribution& __x,
4029 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004030 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004031 friend _LIBCPP_INLINE_VISIBILITY
4032 bool operator!=(const exponential_distribution& __x,
4033 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004034 {return !(__x == __y);}
4035};
4036
4037template <class _RealType>
4038template<class _URNG>
4039_RealType
4040exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4041{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004042 return -_VSTD::log
Howard Hinnant30a840f2010-05-12 17:08:57 +00004043 (
4044 result_type(1) -
Howard Hinnant0949eed2011-06-30 21:18:19 +00004045 _VSTD::generate_canonical<result_type,
Howard Hinnant30a840f2010-05-12 17:08:57 +00004046 numeric_limits<result_type>::digits>(__g)
4047 )
4048 / __p.lambda();
4049}
4050
4051template <class _CharT, class _Traits, class _RealType>
4052basic_ostream<_CharT, _Traits>&
4053operator<<(basic_ostream<_CharT, _Traits>& __os,
4054 const exponential_distribution<_RealType>& __x)
4055{
4056 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004057 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4058 ios_base::scientific);
Howard Hinnant30a840f2010-05-12 17:08:57 +00004059 return __os << __x.lambda();
4060}
4061
4062template <class _CharT, class _Traits, class _RealType>
4063basic_istream<_CharT, _Traits>&
4064operator>>(basic_istream<_CharT, _Traits>& __is,
4065 exponential_distribution<_RealType>& __x)
4066{
4067 typedef exponential_distribution<_RealType> _Eng;
4068 typedef typename _Eng::result_type result_type;
4069 typedef typename _Eng::param_type param_type;
4070 __save_flags<_CharT, _Traits> _(__is);
4071 __is.flags(ios_base::dec | ios_base::skipws);
4072 result_type __lambda;
4073 __is >> __lambda;
4074 if (!__is.fail())
4075 __x.param(param_type(__lambda));
4076 return __is;
4077}
4078
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004079// normal_distribution
4080
4081template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004082class _LIBCPP_VISIBLE normal_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004083{
4084public:
4085 // types
4086 typedef _RealType result_type;
4087
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004088 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004089 {
4090 result_type __mean_;
4091 result_type __stddev_;
4092 public:
4093 typedef normal_distribution distribution_type;
4094
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004095 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004096 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4097 : __mean_(__mean), __stddev_(__stddev) {}
4098
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004099 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004100 result_type mean() const {return __mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004102 result_type stddev() const {return __stddev_;}
4103
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004104 friend _LIBCPP_INLINE_VISIBILITY
4105 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004106 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004107 friend _LIBCPP_INLINE_VISIBILITY
4108 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004109 {return !(__x == __y);}
4110 };
4111
4112private:
4113 param_type __p_;
4114 result_type _V_;
4115 bool _V_hot_;
4116
4117public:
4118 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004120 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4121 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004122 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004123 explicit normal_distribution(const param_type& __p)
4124 : __p_(__p), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004126 void reset() {_V_hot_ = false;}
4127
4128 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004129 template<class _URNG>
4130 _LIBCPP_INLINE_VISIBILITY
4131 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004132 {return (*this)(__g, __p_);}
4133 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4134
4135 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004137 result_type mean() const {return __p_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004139 result_type stddev() const {return __p_.stddev();}
4140
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004142 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004144 void param(const param_type& __p) {__p_ = __p;}
4145
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004147 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004149 result_type max() const {return numeric_limits<result_type>::infinity();}
4150
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004151 friend _LIBCPP_INLINE_VISIBILITY
4152 bool operator==(const normal_distribution& __x,
4153 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004154 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4155 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004156 friend _LIBCPP_INLINE_VISIBILITY
4157 bool operator!=(const normal_distribution& __x,
4158 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004159 {return !(__x == __y);}
4160
4161 template <class _CharT, class _Traits, class _RT>
4162 friend
4163 basic_ostream<_CharT, _Traits>&
4164 operator<<(basic_ostream<_CharT, _Traits>& __os,
4165 const normal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004166
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004167 template <class _CharT, class _Traits, class _RT>
4168 friend
4169 basic_istream<_CharT, _Traits>&
4170 operator>>(basic_istream<_CharT, _Traits>& __is,
4171 normal_distribution<_RT>& __x);
4172};
4173
4174template <class _RealType>
4175template<class _URNG>
4176_RealType
4177normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4178{
Howard Hinnant99968442011-11-29 18:15:50 +00004179 result_type _Up;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004180 if (_V_hot_)
4181 {
4182 _V_hot_ = false;
Howard Hinnant99968442011-11-29 18:15:50 +00004183 _Up = _V_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004184 }
4185 else
4186 {
4187 uniform_real_distribution<result_type> _Uni(-1, 1);
4188 result_type __u;
4189 result_type __v;
4190 result_type __s;
4191 do
4192 {
4193 __u = _Uni(__g);
4194 __v = _Uni(__g);
4195 __s = __u * __u + __v * __v;
4196 } while (__s > 1 || __s == 0);
Howard Hinnant99968442011-11-29 18:15:50 +00004197 result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
4198 _V_ = __v * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004199 _V_hot_ = true;
Howard Hinnant99968442011-11-29 18:15:50 +00004200 _Up = __u * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004201 }
Howard Hinnant99968442011-11-29 18:15:50 +00004202 return _Up * __p.stddev() + __p.mean();
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004203}
4204
4205template <class _CharT, class _Traits, class _RT>
4206basic_ostream<_CharT, _Traits>&
4207operator<<(basic_ostream<_CharT, _Traits>& __os,
4208 const normal_distribution<_RT>& __x)
4209{
4210 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004211 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4212 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004213 _CharT __sp = __os.widen(' ');
4214 __os.fill(__sp);
4215 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4216 if (__x._V_hot_)
4217 __os << __sp << __x._V_;
4218 return __os;
4219}
4220
4221template <class _CharT, class _Traits, class _RT>
4222basic_istream<_CharT, _Traits>&
4223operator>>(basic_istream<_CharT, _Traits>& __is,
4224 normal_distribution<_RT>& __x)
4225{
4226 typedef normal_distribution<_RT> _Eng;
4227 typedef typename _Eng::result_type result_type;
4228 typedef typename _Eng::param_type param_type;
4229 __save_flags<_CharT, _Traits> _(__is);
4230 __is.flags(ios_base::dec | ios_base::skipws);
4231 result_type __mean;
4232 result_type __stddev;
Howard Hinnant99968442011-11-29 18:15:50 +00004233 result_type _Vp = 0;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004234 bool _V_hot = false;
4235 __is >> __mean >> __stddev >> _V_hot;
4236 if (_V_hot)
Howard Hinnant99968442011-11-29 18:15:50 +00004237 __is >> _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004238 if (!__is.fail())
4239 {
4240 __x.param(param_type(__mean, __stddev));
4241 __x._V_hot_ = _V_hot;
Howard Hinnant99968442011-11-29 18:15:50 +00004242 __x._V_ = _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004243 }
4244 return __is;
4245}
4246
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004247// lognormal_distribution
4248
4249template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004250class _LIBCPP_VISIBLE lognormal_distribution
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004251{
4252public:
4253 // types
4254 typedef _RealType result_type;
4255
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004256 class _LIBCPP_VISIBLE param_type
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004257 {
4258 normal_distribution<result_type> __nd_;
4259 public:
4260 typedef lognormal_distribution distribution_type;
4261
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004263 explicit param_type(result_type __m = 0, result_type __s = 1)
4264 : __nd_(__m, __s) {}
4265
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004267 result_type m() const {return __nd_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004269 result_type s() const {return __nd_.stddev();}
4270
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004271 friend _LIBCPP_INLINE_VISIBILITY
4272 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004273 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004274 friend _LIBCPP_INLINE_VISIBILITY
4275 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004276 {return !(__x == __y);}
4277 friend class lognormal_distribution;
4278
4279 template <class _CharT, class _Traits, class _RT>
4280 friend
4281 basic_ostream<_CharT, _Traits>&
4282 operator<<(basic_ostream<_CharT, _Traits>& __os,
4283 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004284
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004285 template <class _CharT, class _Traits, class _RT>
4286 friend
4287 basic_istream<_CharT, _Traits>&
4288 operator>>(basic_istream<_CharT, _Traits>& __is,
4289 lognormal_distribution<_RT>& __x);
4290 };
4291
4292private:
4293 param_type __p_;
4294
4295public:
4296 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004298 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4299 : __p_(param_type(__m, __s)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004301 explicit lognormal_distribution(const param_type& __p)
4302 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004304 void reset() {__p_.__nd_.reset();}
4305
4306 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004307 template<class _URNG>
4308 _LIBCPP_INLINE_VISIBILITY
4309 result_type operator()(_URNG& __g)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004310 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004311 template<class _URNG>
4312 _LIBCPP_INLINE_VISIBILITY
4313 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004314 {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004315
4316 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004318 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004320 result_type s() const {return __p_.s();}
4321
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004323 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00004325 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004326
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004328 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004330 result_type max() const {return numeric_limits<result_type>::infinity();}
4331
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004332 friend _LIBCPP_INLINE_VISIBILITY
4333 bool operator==(const lognormal_distribution& __x,
4334 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004335 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004336 friend _LIBCPP_INLINE_VISIBILITY
4337 bool operator!=(const lognormal_distribution& __x,
4338 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004339 {return !(__x == __y);}
4340
4341 template <class _CharT, class _Traits, class _RT>
4342 friend
4343 basic_ostream<_CharT, _Traits>&
4344 operator<<(basic_ostream<_CharT, _Traits>& __os,
4345 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004346
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004347 template <class _CharT, class _Traits, class _RT>
4348 friend
4349 basic_istream<_CharT, _Traits>&
4350 operator>>(basic_istream<_CharT, _Traits>& __is,
4351 lognormal_distribution<_RT>& __x);
4352};
4353
4354template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004355inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004356basic_ostream<_CharT, _Traits>&
4357operator<<(basic_ostream<_CharT, _Traits>& __os,
4358 const lognormal_distribution<_RT>& __x)
4359{
4360 return __os << __x.__p_.__nd_;
4361}
4362
4363template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004364inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004365basic_istream<_CharT, _Traits>&
4366operator>>(basic_istream<_CharT, _Traits>& __is,
4367 lognormal_distribution<_RT>& __x)
4368{
4369 return __is >> __x.__p_.__nd_;
4370}
4371
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004372// poisson_distribution
4373
4374template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004375class _LIBCPP_VISIBLE poisson_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004376{
4377public:
4378 // types
4379 typedef _IntType result_type;
4380
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004381 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004382 {
4383 double __mean_;
4384 double __s_;
4385 double __d_;
4386 double __l_;
4387 double __omega_;
4388 double __c0_;
4389 double __c1_;
4390 double __c2_;
4391 double __c3_;
4392 double __c_;
4393
4394 public:
4395 typedef poisson_distribution distribution_type;
4396
4397 explicit param_type(double __mean = 1.0);
4398
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004400 double mean() const {return __mean_;}
4401
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004402 friend _LIBCPP_INLINE_VISIBILITY
4403 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004404 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004405 friend _LIBCPP_INLINE_VISIBILITY
4406 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004407 {return !(__x == __y);}
4408
4409 friend class poisson_distribution;
4410 };
4411
4412private:
4413 param_type __p_;
4414
4415public:
4416 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004418 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004420 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004422 void reset() {}
4423
4424 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004425 template<class _URNG>
4426 _LIBCPP_INLINE_VISIBILITY
4427 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004428 {return (*this)(__g, __p_);}
4429 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4430
4431 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004433 double mean() const {return __p_.mean();}
4434
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004436 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004438 void param(const param_type& __p) {__p_ = __p;}
4439
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004441 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004443 result_type max() const {return numeric_limits<result_type>::max();}
4444
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004445 friend _LIBCPP_INLINE_VISIBILITY
4446 bool operator==(const poisson_distribution& __x,
4447 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004448 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004449 friend _LIBCPP_INLINE_VISIBILITY
4450 bool operator!=(const poisson_distribution& __x,
4451 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004452 {return !(__x == __y);}
4453};
4454
4455template<class _IntType>
4456poisson_distribution<_IntType>::param_type::param_type(double __mean)
4457 : __mean_(__mean)
4458{
4459 if (__mean_ < 10)
4460 {
4461 __s_ = 0;
4462 __d_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004463 __l_ = _VSTD::exp(-__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004464 __omega_ = 0;
4465 __c3_ = 0;
4466 __c2_ = 0;
4467 __c1_ = 0;
4468 __c0_ = 0;
4469 __c_ = 0;
4470 }
4471 else
4472 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004473 __s_ = _VSTD::sqrt(__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004474 __d_ = 6 * __mean_ * __mean_;
4475 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4476 __omega_ = .3989423 / __s_;
4477 double __b1_ = .4166667E-1 / __mean_;
4478 double __b2_ = .3 * __b1_ * __b1_;
4479 __c3_ = .1428571 * __b1_ * __b2_;
4480 __c2_ = __b2_ - 15. * __c3_;
4481 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4482 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4483 __c_ = .1069 / __mean_;
4484 }
4485}
4486
4487template <class _IntType>
4488template<class _URNG>
4489_IntType
4490poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4491{
4492 result_type __x;
4493 uniform_real_distribution<double> __urd;
Howard Hinnant75f76952011-04-14 15:59:22 +00004494 if (__pr.__mean_ < 10)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004495 {
4496 __x = 0;
4497 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4498 __p *= __urd(__urng);
4499 }
4500 else
4501 {
4502 double __difmuk;
4503 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4504 double __u;
4505 if (__g > 0)
4506 {
4507 __x = static_cast<result_type>(__g);
4508 if (__x >= __pr.__l_)
4509 return __x;
4510 __difmuk = __pr.__mean_ - __x;
4511 __u = __urd(__urng);
4512 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4513 return __x;
4514 }
4515 exponential_distribution<double> __edist;
4516 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4517 {
4518 double __e;
4519 if (__using_exp_dist || __g < 0)
4520 {
4521 double __t;
4522 do
4523 {
4524 __e = __edist(__urng);
4525 __u = __urd(__urng);
4526 __u += __u - 1;
4527 __t = 1.8 + (__u < 0 ? -__e : __e);
4528 } while (__t <= -.6744);
4529 __x = __pr.__mean_ + __pr.__s_ * __t;
4530 __difmuk = __pr.__mean_ - __x;
4531 __using_exp_dist = true;
4532 }
4533 double __px;
4534 double __py;
4535 if (__x < 10)
4536 {
4537 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4538 40320, 362880};
4539 __px = -__pr.__mean_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004540 __py = _VSTD::pow(__pr.__mean_, (double)__x) / __fac[__x];
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004541 }
4542 else
4543 {
4544 double __del = .8333333E-1 / __x;
4545 __del -= 4.8 * __del * __del * __del;
4546 double __v = __difmuk / __x;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004547 if (_VSTD::abs(__v) > 0.25)
4548 __px = __x * _VSTD::log(1 + __v) - __difmuk - __del;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004549 else
4550 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4551 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4552 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004553 __py = .3989423 / _VSTD::sqrt(__x);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004554 }
4555 double __r = (0.5 - __difmuk) / __pr.__s_;
4556 double __r2 = __r * __r;
4557 double __fx = -0.5 * __r2;
4558 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4559 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4560 if (__using_exp_dist)
4561 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004562 if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4563 __fy * _VSTD::exp(__fx + __e))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004564 break;
4565 }
4566 else
4567 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004568 if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004569 break;
4570 }
4571 }
4572 }
4573 return __x;
4574}
4575
4576template <class _CharT, class _Traits, class _IntType>
4577basic_ostream<_CharT, _Traits>&
4578operator<<(basic_ostream<_CharT, _Traits>& __os,
4579 const poisson_distribution<_IntType>& __x)
4580{
4581 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004582 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4583 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004584 return __os << __x.mean();
4585}
4586
4587template <class _CharT, class _Traits, class _IntType>
4588basic_istream<_CharT, _Traits>&
4589operator>>(basic_istream<_CharT, _Traits>& __is,
4590 poisson_distribution<_IntType>& __x)
4591{
4592 typedef poisson_distribution<_IntType> _Eng;
4593 typedef typename _Eng::param_type param_type;
4594 __save_flags<_CharT, _Traits> _(__is);
4595 __is.flags(ios_base::dec | ios_base::skipws);
4596 double __mean;
4597 __is >> __mean;
4598 if (!__is.fail())
4599 __x.param(param_type(__mean));
4600 return __is;
4601}
4602
Howard Hinnant9de6e302010-05-16 01:09:02 +00004603// weibull_distribution
4604
4605template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004606class _LIBCPP_VISIBLE weibull_distribution
Howard Hinnant9de6e302010-05-16 01:09:02 +00004607{
4608public:
4609 // types
4610 typedef _RealType result_type;
4611
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004612 class _LIBCPP_VISIBLE param_type
Howard Hinnant9de6e302010-05-16 01:09:02 +00004613 {
4614 result_type __a_;
4615 result_type __b_;
4616 public:
4617 typedef weibull_distribution distribution_type;
4618
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004620 explicit param_type(result_type __a = 1, result_type __b = 1)
4621 : __a_(__a), __b_(__b) {}
4622
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004624 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004626 result_type b() const {return __b_;}
4627
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004628 friend _LIBCPP_INLINE_VISIBILITY
4629 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004630 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004631 friend _LIBCPP_INLINE_VISIBILITY
4632 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004633 {return !(__x == __y);}
4634 };
4635
4636private:
4637 param_type __p_;
4638
4639public:
4640 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004642 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4643 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004645 explicit weibull_distribution(const param_type& __p)
4646 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004648 void reset() {}
4649
4650 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004651 template<class _URNG>
4652 _LIBCPP_INLINE_VISIBILITY
4653 result_type operator()(_URNG& __g)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004654 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004655 template<class _URNG>
4656 _LIBCPP_INLINE_VISIBILITY
4657 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004658 {return __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004659 _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
Howard Hinnant9de6e302010-05-16 01:09:02 +00004660
4661 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004663 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004665 result_type b() const {return __p_.b();}
4666
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004668 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004670 void param(const param_type& __p) {__p_ = __p;}
4671
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004673 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004675 result_type max() const {return numeric_limits<result_type>::infinity();}
4676
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004677 friend _LIBCPP_INLINE_VISIBILITY
4678 bool operator==(const weibull_distribution& __x,
4679 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004680 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004681 friend _LIBCPP_INLINE_VISIBILITY
4682 bool operator!=(const weibull_distribution& __x,
4683 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004684 {return !(__x == __y);}
4685};
4686
4687template <class _CharT, class _Traits, class _RT>
4688basic_ostream<_CharT, _Traits>&
4689operator<<(basic_ostream<_CharT, _Traits>& __os,
4690 const weibull_distribution<_RT>& __x)
4691{
4692 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004693 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4694 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004695 _CharT __sp = __os.widen(' ');
4696 __os.fill(__sp);
4697 __os << __x.a() << __sp << __x.b();
4698 return __os;
4699}
4700
4701template <class _CharT, class _Traits, class _RT>
4702basic_istream<_CharT, _Traits>&
4703operator>>(basic_istream<_CharT, _Traits>& __is,
4704 weibull_distribution<_RT>& __x)
4705{
4706 typedef weibull_distribution<_RT> _Eng;
4707 typedef typename _Eng::result_type result_type;
4708 typedef typename _Eng::param_type param_type;
4709 __save_flags<_CharT, _Traits> _(__is);
4710 __is.flags(ios_base::dec | ios_base::skipws);
4711 result_type __a;
4712 result_type __b;
4713 __is >> __a >> __b;
4714 if (!__is.fail())
4715 __x.param(param_type(__a, __b));
4716 return __is;
4717}
4718
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004719template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004720class _LIBCPP_VISIBLE extreme_value_distribution
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004721{
4722public:
4723 // types
4724 typedef _RealType result_type;
4725
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004726 class _LIBCPP_VISIBLE param_type
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004727 {
4728 result_type __a_;
4729 result_type __b_;
4730 public:
4731 typedef extreme_value_distribution distribution_type;
4732
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004734 explicit param_type(result_type __a = 0, result_type __b = 1)
4735 : __a_(__a), __b_(__b) {}
4736
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004738 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004740 result_type b() const {return __b_;}
4741
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004742 friend _LIBCPP_INLINE_VISIBILITY
4743 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004744 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004745 friend _LIBCPP_INLINE_VISIBILITY
4746 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004747 {return !(__x == __y);}
4748 };
4749
4750private:
4751 param_type __p_;
4752
4753public:
4754 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004756 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4757 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004759 explicit extreme_value_distribution(const param_type& __p)
4760 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004762 void reset() {}
4763
4764 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004765 template<class _URNG>
4766 _LIBCPP_INLINE_VISIBILITY
4767 result_type operator()(_URNG& __g)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004768 {return (*this)(__g, __p_);}
4769 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4770
4771 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004773 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004775 result_type b() const {return __p_.b();}
4776
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004778 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004780 void param(const param_type& __p) {__p_ = __p;}
4781
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004783 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004785 result_type max() const {return numeric_limits<result_type>::infinity();}
4786
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004787 friend _LIBCPP_INLINE_VISIBILITY
4788 bool operator==(const extreme_value_distribution& __x,
4789 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004790 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004791 friend _LIBCPP_INLINE_VISIBILITY
4792 bool operator!=(const extreme_value_distribution& __x,
4793 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004794 {return !(__x == __y);}
4795};
4796
4797template<class _RealType>
4798template<class _URNG>
4799_RealType
4800extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4801{
4802 return __p.a() - __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004803 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004804}
4805
4806template <class _CharT, class _Traits, class _RT>
4807basic_ostream<_CharT, _Traits>&
4808operator<<(basic_ostream<_CharT, _Traits>& __os,
4809 const extreme_value_distribution<_RT>& __x)
4810{
4811 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004812 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4813 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004814 _CharT __sp = __os.widen(' ');
4815 __os.fill(__sp);
4816 __os << __x.a() << __sp << __x.b();
4817 return __os;
4818}
4819
4820template <class _CharT, class _Traits, class _RT>
4821basic_istream<_CharT, _Traits>&
4822operator>>(basic_istream<_CharT, _Traits>& __is,
4823 extreme_value_distribution<_RT>& __x)
4824{
4825 typedef extreme_value_distribution<_RT> _Eng;
4826 typedef typename _Eng::result_type result_type;
4827 typedef typename _Eng::param_type param_type;
4828 __save_flags<_CharT, _Traits> _(__is);
4829 __is.flags(ios_base::dec | ios_base::skipws);
4830 result_type __a;
4831 result_type __b;
4832 __is >> __a >> __b;
4833 if (!__is.fail())
4834 __x.param(param_type(__a, __b));
4835 return __is;
4836}
4837
Howard Hinnantc7c49132010-05-13 17:58:28 +00004838// gamma_distribution
4839
4840template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004841class _LIBCPP_VISIBLE gamma_distribution
Howard Hinnantc7c49132010-05-13 17:58:28 +00004842{
4843public:
4844 // types
4845 typedef _RealType result_type;
4846
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004847 class _LIBCPP_VISIBLE param_type
Howard Hinnantc7c49132010-05-13 17:58:28 +00004848 {
4849 result_type __alpha_;
4850 result_type __beta_;
4851 public:
4852 typedef gamma_distribution distribution_type;
4853
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004855 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4856 : __alpha_(__alpha), __beta_(__beta) {}
4857
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004859 result_type alpha() const {return __alpha_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004861 result_type beta() const {return __beta_;}
4862
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004863 friend _LIBCPP_INLINE_VISIBILITY
4864 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004865 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004866 friend _LIBCPP_INLINE_VISIBILITY
4867 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004868 {return !(__x == __y);}
4869 };
4870
4871private:
4872 param_type __p_;
4873
4874public:
4875 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004877 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4878 : __p_(param_type(__alpha, __beta)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004880 explicit gamma_distribution(const param_type& __p)
4881 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004883 void reset() {}
4884
4885 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004886 template<class _URNG>
4887 _LIBCPP_INLINE_VISIBILITY
4888 result_type operator()(_URNG& __g)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004889 {return (*this)(__g, __p_);}
4890 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4891
4892 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004894 result_type alpha() const {return __p_.alpha();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004896 result_type beta() const {return __p_.beta();}
4897
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004899 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004901 void param(const param_type& __p) {__p_ = __p;}
4902
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004904 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004906 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantc7c49132010-05-13 17:58:28 +00004907
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004908 friend _LIBCPP_INLINE_VISIBILITY
4909 bool operator==(const gamma_distribution& __x,
4910 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004911 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004912 friend _LIBCPP_INLINE_VISIBILITY
4913 bool operator!=(const gamma_distribution& __x,
4914 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004915 {return !(__x == __y);}
4916};
4917
4918template <class _RealType>
4919template<class _URNG>
4920_RealType
4921gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4922{
Howard Hinnantf417abe2010-05-14 18:43:10 +00004923 result_type __a = __p.alpha();
4924 uniform_real_distribution<result_type> __gen(0, 1);
4925 exponential_distribution<result_type> __egen;
4926 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00004927 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00004928 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004929 else if (__a > 1)
4930 {
4931 const result_type __b = __a - 1;
4932 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004933 while (true)
4934 {
4935 const result_type __u = __gen(__g);
4936 const result_type __v = __gen(__g);
4937 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004938 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004939 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004940 const result_type __y = _VSTD::sqrt(__c / __w) *
Howard Hinnantc7c49132010-05-13 17:58:28 +00004941 (__u - result_type(0.5));
4942 __x = __b + __y;
4943 if (__x >= 0)
4944 {
4945 const result_type __z = 64 * __w * __w * __w * __v * __v;
4946 if (__z <= 1 - 2 * __y * __y / __x)
4947 break;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004948 if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
Howard Hinnantc7c49132010-05-13 17:58:28 +00004949 break;
4950 }
4951 }
4952 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00004953 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00004954 else // __a < 1
4955 {
4956 while (true)
4957 {
4958 const result_type __u = __gen(__g);
4959 const result_type __es = __egen(__g);
4960 if (__u <= 1 - __a)
4961 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004962 __x = _VSTD::pow(__u, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004963 if (__x <= __es)
4964 break;
4965 }
4966 else
4967 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004968 const result_type __e = -_VSTD::log((1-__u)/__a);
4969 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004970 if (__x <= __e + __es)
4971 break;
4972 }
4973 }
4974 }
4975 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00004976}
4977
4978template <class _CharT, class _Traits, class _RT>
4979basic_ostream<_CharT, _Traits>&
4980operator<<(basic_ostream<_CharT, _Traits>& __os,
4981 const gamma_distribution<_RT>& __x)
4982{
4983 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004984 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4985 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004986 _CharT __sp = __os.widen(' ');
4987 __os.fill(__sp);
4988 __os << __x.alpha() << __sp << __x.beta();
4989 return __os;
4990}
4991
4992template <class _CharT, class _Traits, class _RT>
4993basic_istream<_CharT, _Traits>&
4994operator>>(basic_istream<_CharT, _Traits>& __is,
4995 gamma_distribution<_RT>& __x)
4996{
4997 typedef gamma_distribution<_RT> _Eng;
4998 typedef typename _Eng::result_type result_type;
4999 typedef typename _Eng::param_type param_type;
5000 __save_flags<_CharT, _Traits> _(__is);
5001 __is.flags(ios_base::dec | ios_base::skipws);
5002 result_type __alpha;
5003 result_type __beta;
5004 __is >> __alpha >> __beta;
5005 if (!__is.fail())
5006 __x.param(param_type(__alpha, __beta));
5007 return __is;
5008}
Howard Hinnanta64111c2010-05-12 21:02:31 +00005009
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005010// negative_binomial_distribution
5011
5012template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005013class _LIBCPP_VISIBLE negative_binomial_distribution
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005014{
5015public:
5016 // types
5017 typedef _IntType result_type;
5018
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005019 class _LIBCPP_VISIBLE param_type
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005020 {
5021 result_type __k_;
5022 double __p_;
5023 public:
5024 typedef negative_binomial_distribution distribution_type;
5025
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005026 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005027 explicit param_type(result_type __k = 1, double __p = 0.5)
5028 : __k_(__k), __p_(__p) {}
5029
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005031 result_type k() const {return __k_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005032 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005033 double p() const {return __p_;}
5034
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005035 friend _LIBCPP_INLINE_VISIBILITY
5036 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005037 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005038 friend _LIBCPP_INLINE_VISIBILITY
5039 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005040 {return !(__x == __y);}
5041 };
5042
5043private:
5044 param_type __p_;
5045
5046public:
5047 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005049 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
5050 : __p_(__k, __p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005052 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005054 void reset() {}
5055
5056 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005057 template<class _URNG>
5058 _LIBCPP_INLINE_VISIBILITY
5059 result_type operator()(_URNG& __g)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005060 {return (*this)(__g, __p_);}
5061 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5062
5063 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005065 result_type k() const {return __p_.k();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005067 double p() const {return __p_.p();}
5068
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005069 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005070 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005072 void param(const param_type& __p) {__p_ = __p;}
5073
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005075 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005077 result_type max() const {return numeric_limits<result_type>::max();}
5078
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005079 friend _LIBCPP_INLINE_VISIBILITY
5080 bool operator==(const negative_binomial_distribution& __x,
5081 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005082 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005083 friend _LIBCPP_INLINE_VISIBILITY
5084 bool operator!=(const negative_binomial_distribution& __x,
5085 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005086 {return !(__x == __y);}
5087};
5088
5089template <class _IntType>
5090template<class _URNG>
5091_IntType
5092negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5093{
5094 result_type __k = __pr.k();
5095 double __p = __pr.p();
5096 if (__k <= 21 * __p)
5097 {
5098 bernoulli_distribution __gen(__p);
5099 result_type __f = 0;
5100 result_type __s = 0;
5101 while (__s < __k)
5102 {
5103 if (__gen(__urng))
5104 ++__s;
5105 else
5106 ++__f;
5107 }
5108 return __f;
5109 }
5110 return poisson_distribution<result_type>(gamma_distribution<double>
5111 (__k, (1-__p)/__p)(__urng))(__urng);
5112}
5113
5114template <class _CharT, class _Traits, class _IntType>
5115basic_ostream<_CharT, _Traits>&
5116operator<<(basic_ostream<_CharT, _Traits>& __os,
5117 const negative_binomial_distribution<_IntType>& __x)
5118{
5119 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005120 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5121 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005122 _CharT __sp = __os.widen(' ');
5123 __os.fill(__sp);
5124 return __os << __x.k() << __sp << __x.p();
5125}
5126
5127template <class _CharT, class _Traits, class _IntType>
5128basic_istream<_CharT, _Traits>&
5129operator>>(basic_istream<_CharT, _Traits>& __is,
5130 negative_binomial_distribution<_IntType>& __x)
5131{
5132 typedef negative_binomial_distribution<_IntType> _Eng;
5133 typedef typename _Eng::result_type result_type;
5134 typedef typename _Eng::param_type param_type;
5135 __save_flags<_CharT, _Traits> _(__is);
5136 __is.flags(ios_base::dec | ios_base::skipws);
5137 result_type __k;
5138 double __p;
5139 __is >> __k >> __p;
5140 if (!__is.fail())
5141 __x.param(param_type(__k, __p));
5142 return __is;
5143}
5144
Howard Hinnant34e8a572010-05-17 13:44:27 +00005145// geometric_distribution
5146
5147template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005148class _LIBCPP_VISIBLE geometric_distribution
Howard Hinnant34e8a572010-05-17 13:44:27 +00005149{
5150public:
5151 // types
5152 typedef _IntType result_type;
5153
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005154 class _LIBCPP_VISIBLE param_type
Howard Hinnant34e8a572010-05-17 13:44:27 +00005155 {
5156 double __p_;
5157 public:
5158 typedef geometric_distribution distribution_type;
5159
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005161 explicit param_type(double __p = 0.5) : __p_(__p) {}
5162
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005164 double p() const {return __p_;}
5165
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005166 friend _LIBCPP_INLINE_VISIBILITY
5167 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005168 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005169 friend _LIBCPP_INLINE_VISIBILITY
5170 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005171 {return !(__x == __y);}
5172 };
5173
5174private:
5175 param_type __p_;
5176
5177public:
5178 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005180 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005182 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005184 void reset() {}
5185
5186 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005187 template<class _URNG>
5188 _LIBCPP_INLINE_VISIBILITY
5189 result_type operator()(_URNG& __g)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005190 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005191 template<class _URNG>
5192 _LIBCPP_INLINE_VISIBILITY
5193 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005194 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5195
5196 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005198 double p() const {return __p_.p();}
5199
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005201 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005203 void param(const param_type& __p) {__p_ = __p;}
5204
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005205 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005206 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005208 result_type max() const {return numeric_limits<result_type>::max();}
5209
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005210 friend _LIBCPP_INLINE_VISIBILITY
5211 bool operator==(const geometric_distribution& __x,
5212 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005213 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005214 friend _LIBCPP_INLINE_VISIBILITY
5215 bool operator!=(const geometric_distribution& __x,
5216 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005217 {return !(__x == __y);}
5218};
5219
5220template <class _CharT, class _Traits, class _IntType>
5221basic_ostream<_CharT, _Traits>&
5222operator<<(basic_ostream<_CharT, _Traits>& __os,
5223 const geometric_distribution<_IntType>& __x)
5224{
5225 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005226 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5227 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005228 return __os << __x.p();
5229}
5230
5231template <class _CharT, class _Traits, class _IntType>
5232basic_istream<_CharT, _Traits>&
5233operator>>(basic_istream<_CharT, _Traits>& __is,
5234 geometric_distribution<_IntType>& __x)
5235{
5236 typedef geometric_distribution<_IntType> _Eng;
5237 typedef typename _Eng::param_type param_type;
5238 __save_flags<_CharT, _Traits> _(__is);
5239 __is.flags(ios_base::dec | ios_base::skipws);
5240 double __p;
5241 __is >> __p;
5242 if (!__is.fail())
5243 __x.param(param_type(__p));
5244 return __is;
5245}
5246
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005247// chi_squared_distribution
5248
5249template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005250class _LIBCPP_VISIBLE chi_squared_distribution
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005251{
5252public:
5253 // types
5254 typedef _RealType result_type;
5255
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005256 class _LIBCPP_VISIBLE param_type
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005257 {
5258 result_type __n_;
5259 public:
5260 typedef chi_squared_distribution distribution_type;
5261
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005263 explicit param_type(result_type __n = 1) : __n_(__n) {}
5264
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005266 result_type n() const {return __n_;}
5267
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005268 friend _LIBCPP_INLINE_VISIBILITY
5269 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005270 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005271 friend _LIBCPP_INLINE_VISIBILITY
5272 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005273 {return !(__x == __y);}
5274 };
5275
5276private:
5277 param_type __p_;
5278
5279public:
5280 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005282 explicit chi_squared_distribution(result_type __n = 1)
5283 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005285 explicit chi_squared_distribution(const param_type& __p)
5286 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005288 void reset() {}
5289
5290 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005291 template<class _URNG>
5292 _LIBCPP_INLINE_VISIBILITY
5293 result_type operator()(_URNG& __g)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005294 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005295 template<class _URNG>
5296 _LIBCPP_INLINE_VISIBILITY
5297 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005298 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5299
5300 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005302 result_type n() const {return __p_.n();}
5303
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005305 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005307 void param(const param_type& __p) {__p_ = __p;}
5308
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005310 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005312 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005313
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005314 friend _LIBCPP_INLINE_VISIBILITY
5315 bool operator==(const chi_squared_distribution& __x,
5316 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005317 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005318 friend _LIBCPP_INLINE_VISIBILITY
5319 bool operator!=(const chi_squared_distribution& __x,
5320 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005321 {return !(__x == __y);}
5322};
5323
5324template <class _CharT, class _Traits, class _RT>
5325basic_ostream<_CharT, _Traits>&
5326operator<<(basic_ostream<_CharT, _Traits>& __os,
5327 const chi_squared_distribution<_RT>& __x)
5328{
5329 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005330 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5331 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005332 __os << __x.n();
5333 return __os;
5334}
5335
5336template <class _CharT, class _Traits, class _RT>
5337basic_istream<_CharT, _Traits>&
5338operator>>(basic_istream<_CharT, _Traits>& __is,
5339 chi_squared_distribution<_RT>& __x)
5340{
5341 typedef chi_squared_distribution<_RT> _Eng;
5342 typedef typename _Eng::result_type result_type;
5343 typedef typename _Eng::param_type param_type;
5344 __save_flags<_CharT, _Traits> _(__is);
5345 __is.flags(ios_base::dec | ios_base::skipws);
5346 result_type __n;
5347 __is >> __n;
5348 if (!__is.fail())
5349 __x.param(param_type(__n));
5350 return __is;
5351}
5352
Howard Hinnantd7d01132010-05-17 21:55:46 +00005353// cauchy_distribution
5354
5355template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005356class _LIBCPP_VISIBLE cauchy_distribution
Howard Hinnantd7d01132010-05-17 21:55:46 +00005357{
5358public:
5359 // types
5360 typedef _RealType result_type;
5361
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005362 class _LIBCPP_VISIBLE param_type
Howard Hinnantd7d01132010-05-17 21:55:46 +00005363 {
5364 result_type __a_;
5365 result_type __b_;
5366 public:
5367 typedef cauchy_distribution distribution_type;
5368
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005370 explicit param_type(result_type __a = 0, result_type __b = 1)
5371 : __a_(__a), __b_(__b) {}
5372
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005374 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005376 result_type b() const {return __b_;}
5377
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005378 friend _LIBCPP_INLINE_VISIBILITY
5379 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005380 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005381 friend _LIBCPP_INLINE_VISIBILITY
5382 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005383 {return !(__x == __y);}
5384 };
5385
5386private:
5387 param_type __p_;
5388
5389public:
5390 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005392 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5393 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005395 explicit cauchy_distribution(const param_type& __p)
5396 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005398 void reset() {}
5399
5400 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005401 template<class _URNG>
5402 _LIBCPP_INLINE_VISIBILITY
5403 result_type operator()(_URNG& __g)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005404 {return (*this)(__g, __p_);}
5405 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5406
5407 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005409 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005411 result_type b() const {return __p_.b();}
5412
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005414 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005416 void param(const param_type& __p) {__p_ = __p;}
5417
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005419 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005421 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005422
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005423 friend _LIBCPP_INLINE_VISIBILITY
5424 bool operator==(const cauchy_distribution& __x,
5425 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005426 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005427 friend _LIBCPP_INLINE_VISIBILITY
5428 bool operator!=(const cauchy_distribution& __x,
5429 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005430 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005431};
5432
5433template <class _RealType>
5434template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005435inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005436_RealType
5437cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5438{
5439 uniform_real_distribution<result_type> __gen;
5440 // 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 +00005441 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
Howard Hinnantd7d01132010-05-17 21:55:46 +00005442}
5443
5444template <class _CharT, class _Traits, class _RT>
5445basic_ostream<_CharT, _Traits>&
5446operator<<(basic_ostream<_CharT, _Traits>& __os,
5447 const cauchy_distribution<_RT>& __x)
5448{
5449 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005450 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5451 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005452 _CharT __sp = __os.widen(' ');
5453 __os.fill(__sp);
5454 __os << __x.a() << __sp << __x.b();
5455 return __os;
5456}
5457
5458template <class _CharT, class _Traits, class _RT>
5459basic_istream<_CharT, _Traits>&
5460operator>>(basic_istream<_CharT, _Traits>& __is,
5461 cauchy_distribution<_RT>& __x)
5462{
5463 typedef cauchy_distribution<_RT> _Eng;
5464 typedef typename _Eng::result_type result_type;
5465 typedef typename _Eng::param_type param_type;
5466 __save_flags<_CharT, _Traits> _(__is);
5467 __is.flags(ios_base::dec | ios_base::skipws);
5468 result_type __a;
5469 result_type __b;
5470 __is >> __a >> __b;
5471 if (!__is.fail())
5472 __x.param(param_type(__a, __b));
5473 return __is;
5474}
5475
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005476// fisher_f_distribution
5477
5478template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005479class _LIBCPP_VISIBLE fisher_f_distribution
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005480{
5481public:
5482 // types
5483 typedef _RealType result_type;
5484
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005485 class _LIBCPP_VISIBLE param_type
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005486 {
5487 result_type __m_;
5488 result_type __n_;
5489 public:
5490 typedef fisher_f_distribution distribution_type;
5491
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005493 explicit param_type(result_type __m = 1, result_type __n = 1)
5494 : __m_(__m), __n_(__n) {}
5495
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005497 result_type m() const {return __m_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005498 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005499 result_type n() const {return __n_;}
5500
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005501 friend _LIBCPP_INLINE_VISIBILITY
5502 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005503 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005504 friend _LIBCPP_INLINE_VISIBILITY
5505 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005506 {return !(__x == __y);}
5507 };
5508
5509private:
5510 param_type __p_;
5511
5512public:
5513 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005515 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5516 : __p_(param_type(__m, __n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005517 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005518 explicit fisher_f_distribution(const param_type& __p)
5519 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005521 void reset() {}
5522
5523 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005524 template<class _URNG>
5525 _LIBCPP_INLINE_VISIBILITY
5526 result_type operator()(_URNG& __g)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005527 {return (*this)(__g, __p_);}
5528 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5529
5530 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005532 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005534 result_type n() const {return __p_.n();}
5535
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005537 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005539 void param(const param_type& __p) {__p_ = __p;}
5540
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005542 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005544 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005545
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005546 friend _LIBCPP_INLINE_VISIBILITY
5547 bool operator==(const fisher_f_distribution& __x,
5548 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005549 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005550 friend _LIBCPP_INLINE_VISIBILITY
5551 bool operator!=(const fisher_f_distribution& __x,
5552 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005553 {return !(__x == __y);}
5554};
5555
5556template <class _RealType>
5557template<class _URNG>
5558_RealType
5559fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5560{
5561 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5562 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5563 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5564}
5565
5566template <class _CharT, class _Traits, class _RT>
5567basic_ostream<_CharT, _Traits>&
5568operator<<(basic_ostream<_CharT, _Traits>& __os,
5569 const fisher_f_distribution<_RT>& __x)
5570{
5571 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005572 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5573 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005574 _CharT __sp = __os.widen(' ');
5575 __os.fill(__sp);
5576 __os << __x.m() << __sp << __x.n();
5577 return __os;
5578}
5579
5580template <class _CharT, class _Traits, class _RT>
5581basic_istream<_CharT, _Traits>&
5582operator>>(basic_istream<_CharT, _Traits>& __is,
5583 fisher_f_distribution<_RT>& __x)
5584{
5585 typedef fisher_f_distribution<_RT> _Eng;
5586 typedef typename _Eng::result_type result_type;
5587 typedef typename _Eng::param_type param_type;
5588 __save_flags<_CharT, _Traits> _(__is);
5589 __is.flags(ios_base::dec | ios_base::skipws);
5590 result_type __m;
5591 result_type __n;
5592 __is >> __m >> __n;
5593 if (!__is.fail())
5594 __x.param(param_type(__m, __n));
5595 return __is;
5596}
5597
Howard Hinnant551d8e42010-05-19 01:53:57 +00005598// student_t_distribution
5599
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005600template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005601class _LIBCPP_VISIBLE student_t_distribution
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005602{
5603public:
5604 // types
5605 typedef _RealType result_type;
5606
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005607 class _LIBCPP_VISIBLE param_type
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005608 {
5609 result_type __n_;
5610 public:
5611 typedef student_t_distribution distribution_type;
5612
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005614 explicit param_type(result_type __n = 1) : __n_(__n) {}
5615
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005617 result_type n() const {return __n_;}
5618
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005619 friend _LIBCPP_INLINE_VISIBILITY
5620 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005621 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005622 friend _LIBCPP_INLINE_VISIBILITY
5623 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005624 {return !(__x == __y);}
5625 };
5626
5627private:
5628 param_type __p_;
5629 normal_distribution<result_type> __nd_;
5630
5631public:
5632 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005634 explicit student_t_distribution(result_type __n = 1)
5635 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005637 explicit student_t_distribution(const param_type& __p)
5638 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005640 void reset() {__nd_.reset();}
5641
5642 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005643 template<class _URNG>
5644 _LIBCPP_INLINE_VISIBILITY
5645 result_type operator()(_URNG& __g)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005646 {return (*this)(__g, __p_);}
5647 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5648
5649 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005651 result_type n() const {return __p_.n();}
5652
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005654 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005656 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005657
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005659 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005661 result_type max() const {return numeric_limits<result_type>::infinity();}
5662
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005663 friend _LIBCPP_INLINE_VISIBILITY
5664 bool operator==(const student_t_distribution& __x,
5665 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005666 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005667 friend _LIBCPP_INLINE_VISIBILITY
5668 bool operator!=(const student_t_distribution& __x,
5669 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005670 {return !(__x == __y);}
5671};
5672
5673template <class _RealType>
5674template<class _URNG>
5675_RealType
5676student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5677{
5678 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005679 return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005680}
5681
5682template <class _CharT, class _Traits, class _RT>
5683basic_ostream<_CharT, _Traits>&
5684operator<<(basic_ostream<_CharT, _Traits>& __os,
5685 const student_t_distribution<_RT>& __x)
5686{
5687 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005688 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5689 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005690 __os << __x.n();
5691 return __os;
5692}
5693
5694template <class _CharT, class _Traits, class _RT>
5695basic_istream<_CharT, _Traits>&
5696operator>>(basic_istream<_CharT, _Traits>& __is,
5697 student_t_distribution<_RT>& __x)
5698{
5699 typedef student_t_distribution<_RT> _Eng;
5700 typedef typename _Eng::result_type result_type;
5701 typedef typename _Eng::param_type param_type;
5702 __save_flags<_CharT, _Traits> _(__is);
5703 __is.flags(ios_base::dec | ios_base::skipws);
5704 result_type __n;
5705 __is >> __n;
5706 if (!__is.fail())
5707 __x.param(param_type(__n));
5708 return __is;
5709}
5710
Howard Hinnant551d8e42010-05-19 01:53:57 +00005711// discrete_distribution
5712
5713template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005714class _LIBCPP_VISIBLE discrete_distribution
Howard Hinnant551d8e42010-05-19 01:53:57 +00005715{
5716public:
5717 // types
5718 typedef _IntType result_type;
5719
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005720 class _LIBCPP_VISIBLE param_type
Howard Hinnant551d8e42010-05-19 01:53:57 +00005721 {
5722 vector<double> __p_;
5723 public:
5724 typedef discrete_distribution distribution_type;
5725
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005727 param_type() {}
5728 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005730 param_type(_InputIterator __f, _InputIterator __l)
5731 : __p_(__f, __l) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005732#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005734 param_type(initializer_list<double> __wl)
5735 : __p_(__wl.begin(), __wl.end()) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005736#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005737 template<class _UnaryOperation>
5738 param_type(size_t __nw, double __xmin, double __xmax,
5739 _UnaryOperation __fw);
5740
5741 vector<double> probabilities() const;
5742
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005743 friend _LIBCPP_INLINE_VISIBILITY
5744 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005745 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005746 friend _LIBCPP_INLINE_VISIBILITY
5747 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005748 {return !(__x == __y);}
5749
5750 private:
5751 void __init();
5752
5753 friend class discrete_distribution;
5754
5755 template <class _CharT, class _Traits, class _IT>
5756 friend
5757 basic_ostream<_CharT, _Traits>&
5758 operator<<(basic_ostream<_CharT, _Traits>& __os,
5759 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005760
Howard Hinnant551d8e42010-05-19 01:53:57 +00005761 template <class _CharT, class _Traits, class _IT>
5762 friend
5763 basic_istream<_CharT, _Traits>&
5764 operator>>(basic_istream<_CharT, _Traits>& __is,
5765 discrete_distribution<_IT>& __x);
5766 };
5767
5768private:
5769 param_type __p_;
5770
5771public:
5772 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005774 discrete_distribution() {}
5775 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005777 discrete_distribution(_InputIterator __f, _InputIterator __l)
5778 : __p_(__f, __l) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005779#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005781 discrete_distribution(initializer_list<double> __wl)
5782 : __p_(__wl) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005783#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005784 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005786 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5787 _UnaryOperation __fw)
5788 : __p_(__nw, __xmin, __xmax, __fw) {}
5789 explicit discrete_distribution(const param_type& __p)
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005791 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005793 void reset() {}
5794
5795 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005796 template<class _URNG>
5797 _LIBCPP_INLINE_VISIBILITY
5798 result_type operator()(_URNG& __g)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005799 {return (*this)(__g, __p_);}
5800 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5801
5802 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005804 vector<double> probabilities() const {return __p_.probabilities();}
5805
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005807 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005809 void param(const param_type& __p) {__p_ = __p;}
5810
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005812 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005814 result_type max() const {return __p_.__p_.size();}
5815
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005816 friend _LIBCPP_INLINE_VISIBILITY
5817 bool operator==(const discrete_distribution& __x,
5818 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005819 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005820 friend _LIBCPP_INLINE_VISIBILITY
5821 bool operator!=(const discrete_distribution& __x,
5822 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005823 {return !(__x == __y);}
5824
5825 template <class _CharT, class _Traits, class _IT>
5826 friend
5827 basic_ostream<_CharT, _Traits>&
5828 operator<<(basic_ostream<_CharT, _Traits>& __os,
5829 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005830
Howard Hinnant551d8e42010-05-19 01:53:57 +00005831 template <class _CharT, class _Traits, class _IT>
5832 friend
5833 basic_istream<_CharT, _Traits>&
5834 operator>>(basic_istream<_CharT, _Traits>& __is,
5835 discrete_distribution<_IT>& __x);
5836};
5837
5838template<class _IntType>
5839template<class _UnaryOperation>
5840discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5841 double __xmin,
5842 double __xmax,
5843 _UnaryOperation __fw)
5844{
5845 if (__nw > 1)
5846 {
5847 __p_.reserve(__nw - 1);
5848 double __d = (__xmax - __xmin) / __nw;
5849 double __d2 = __d / 2;
5850 for (size_t __k = 0; __k < __nw; ++__k)
5851 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5852 __init();
5853 }
5854}
5855
5856template<class _IntType>
5857void
5858discrete_distribution<_IntType>::param_type::__init()
5859{
5860 if (!__p_.empty())
5861 {
5862 if (__p_.size() > 1)
5863 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005864 double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
5865 for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
Howard Hinnant551d8e42010-05-19 01:53:57 +00005866 __i < __e; ++__i)
5867 *__i /= __s;
5868 vector<double> __t(__p_.size() - 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005869 _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00005870 swap(__p_, __t);
5871 }
5872 else
5873 {
5874 __p_.clear();
5875 __p_.shrink_to_fit();
5876 }
5877 }
5878}
5879
5880template<class _IntType>
5881vector<double>
5882discrete_distribution<_IntType>::param_type::probabilities() const
5883{
5884 size_t __n = __p_.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00005885 _VSTD::vector<double> __p(__n+1);
5886 _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00005887 if (__n > 0)
5888 __p[__n] = 1 - __p_[__n-1];
5889 else
5890 __p[0] = 1;
5891 return __p;
5892}
5893
5894template<class _IntType>
5895template<class _URNG>
5896_IntType
5897discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
5898{
5899 uniform_real_distribution<double> __gen;
5900 return static_cast<_IntType>(
Howard Hinnant0949eed2011-06-30 21:18:19 +00005901 _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
Howard Hinnant551d8e42010-05-19 01:53:57 +00005902 __p.__p_.begin());
5903}
5904
5905template <class _CharT, class _Traits, class _IT>
5906basic_ostream<_CharT, _Traits>&
5907operator<<(basic_ostream<_CharT, _Traits>& __os,
5908 const discrete_distribution<_IT>& __x)
5909{
5910 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005911 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5912 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005913 _CharT __sp = __os.widen(' ');
5914 __os.fill(__sp);
5915 size_t __n = __x.__p_.__p_.size();
5916 __os << __n;
5917 for (size_t __i = 0; __i < __n; ++__i)
5918 __os << __sp << __x.__p_.__p_[__i];
5919 return __os;
5920}
5921
5922template <class _CharT, class _Traits, class _IT>
5923basic_istream<_CharT, _Traits>&
5924operator>>(basic_istream<_CharT, _Traits>& __is,
5925 discrete_distribution<_IT>& __x)
5926{
5927 typedef discrete_distribution<_IT> _Eng;
5928 typedef typename _Eng::result_type result_type;
5929 typedef typename _Eng::param_type param_type;
5930 __save_flags<_CharT, _Traits> _(__is);
5931 __is.flags(ios_base::dec | ios_base::skipws);
5932 size_t __n;
5933 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005934 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005935 for (size_t __i = 0; __i < __n; ++__i)
5936 __is >> __p[__i];
5937 if (!__is.fail())
5938 swap(__x.__p_.__p_, __p);
5939 return __is;
5940}
5941
Howard Hinnantd6d11712010-05-20 15:11:46 +00005942// piecewise_constant_distribution
5943
5944template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005945class _LIBCPP_VISIBLE piecewise_constant_distribution
Howard Hinnantd6d11712010-05-20 15:11:46 +00005946{
5947public:
5948 // types
5949 typedef _RealType result_type;
5950
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005951 class _LIBCPP_VISIBLE param_type
Howard Hinnantd6d11712010-05-20 15:11:46 +00005952 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00005953 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00005954 vector<result_type> __densities_;
5955 vector<result_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005956 public:
5957 typedef piecewise_constant_distribution distribution_type;
5958
5959 param_type();
5960 template<class _InputIteratorB, class _InputIteratorW>
5961 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5962 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00005963#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00005964 template<class _UnaryOperation>
5965 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00005966#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00005967 template<class _UnaryOperation>
5968 param_type(size_t __nw, result_type __xmin, result_type __xmax,
5969 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00005970 param_type & operator=(const param_type& __rhs);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005971
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00005973 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00005975 vector<result_type> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005976
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005977 friend _LIBCPP_INLINE_VISIBILITY
5978 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:40 +00005979 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005980 friend _LIBCPP_INLINE_VISIBILITY
5981 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00005982 {return !(__x == __y);}
5983
5984 private:
5985 void __init();
5986
5987 friend class piecewise_constant_distribution;
5988
5989 template <class _CharT, class _Traits, class _RT>
5990 friend
5991 basic_ostream<_CharT, _Traits>&
5992 operator<<(basic_ostream<_CharT, _Traits>& __os,
5993 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005994
Howard Hinnantd6d11712010-05-20 15:11:46 +00005995 template <class _CharT, class _Traits, class _RT>
5996 friend
5997 basic_istream<_CharT, _Traits>&
5998 operator>>(basic_istream<_CharT, _Traits>& __is,
5999 piecewise_constant_distribution<_RT>& __x);
6000 };
6001
6002private:
6003 param_type __p_;
6004
6005public:
6006 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006008 piecewise_constant_distribution() {}
6009 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006011 piecewise_constant_distribution(_InputIteratorB __fB,
6012 _InputIteratorB __lB,
6013 _InputIteratorW __fW)
6014 : __p_(__fB, __lB, __fW) {}
6015
Howard Hinnante3e32912011-08-12 21:56:02 +00006016#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006017 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006019 piecewise_constant_distribution(initializer_list<result_type> __bl,
6020 _UnaryOperation __fw)
6021 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006022#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006023
6024 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006026 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6027 result_type __xmax, _UnaryOperation __fw)
6028 : __p_(__nw, __xmin, __xmax, __fw) {}
6029
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006031 explicit piecewise_constant_distribution(const param_type& __p)
6032 : __p_(__p) {}
6033
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006035 void reset() {}
6036
6037 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006038 template<class _URNG>
6039 _LIBCPP_INLINE_VISIBILITY
6040 result_type operator()(_URNG& __g)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006041 {return (*this)(__g, __p_);}
6042 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6043
6044 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006046 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006048 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnantd6d11712010-05-20 15:11:46 +00006049
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006051 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006053 void param(const param_type& __p) {__p_ = __p;}
6054
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006056 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006058 result_type max() const {return __p_.__b_.back();}
6059
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006060 friend _LIBCPP_INLINE_VISIBILITY
6061 bool operator==(const piecewise_constant_distribution& __x,
6062 const piecewise_constant_distribution& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006063 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006064 friend _LIBCPP_INLINE_VISIBILITY
6065 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnantd6d11712010-05-20 15:11:46 +00006066 const piecewise_constant_distribution& __y)
6067 {return !(__x == __y);}
6068
6069 template <class _CharT, class _Traits, class _RT>
6070 friend
6071 basic_ostream<_CharT, _Traits>&
6072 operator<<(basic_ostream<_CharT, _Traits>& __os,
6073 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006074
Howard Hinnantd6d11712010-05-20 15:11:46 +00006075 template <class _CharT, class _Traits, class _RT>
6076 friend
6077 basic_istream<_CharT, _Traits>&
6078 operator>>(basic_istream<_CharT, _Traits>& __is,
6079 piecewise_constant_distribution<_RT>& __x);
6080};
6081
6082template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006083typename piecewise_constant_distribution<_RealType>::param_type &
6084piecewise_constant_distribution<_RealType>::param_type::operator=
6085 (const param_type& __rhs)
6086{
6087// These can throw
6088 __b_.reserve (__rhs.__b_.size ());
6089 __densities_.reserve(__rhs.__densities_.size());
6090 __areas_.reserve (__rhs.__areas_.size());
6091
6092// These can not throw
6093 __b_ = __rhs.__b_;
6094 __densities_ = __rhs.__densities_;
6095 __areas_ = __rhs.__areas_;
6096 return *this;
6097}
6098
6099template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006100void
6101piecewise_constant_distribution<_RealType>::param_type::__init()
6102{
Howard Hinnant2a592542010-05-24 00:35:40 +00006103 // __densities_ contains non-normalized areas
Howard Hinnant0949eed2011-06-30 21:18:19 +00006104 result_type __total_area = _VSTD::accumulate(__densities_.begin(),
Howard Hinnant2a592542010-05-24 00:35:40 +00006105 __densities_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006106 result_type());
Howard Hinnant2a592542010-05-24 00:35:40 +00006107 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6108 __densities_[__i] /= __total_area;
6109 // __densities_ contains normalized areas
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006110 __areas_.assign(__densities_.size(), result_type());
Howard Hinnant0949eed2011-06-30 21:18:19 +00006111 _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
Howard Hinnant2a592542010-05-24 00:35:40 +00006112 __areas_.begin() + 1);
6113 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6114 __densities_.back() = 1 - __areas_.back(); // correct round off error
6115 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6116 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6117 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:46 +00006118}
6119
6120template<class _RealType>
6121piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:40 +00006122 : __b_(2),
Howard Hinnant54305402010-05-25 00:27:34 +00006123 __densities_(1, 1.0),
6124 __areas_(1, 0.0)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006125{
6126 __b_[1] = 1;
6127}
6128
6129template<class _RealType>
6130template<class _InputIteratorB, class _InputIteratorW>
6131piecewise_constant_distribution<_RealType>::param_type::param_type(
6132 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6133 : __b_(__fB, __lB)
6134{
6135 if (__b_.size() < 2)
6136 {
6137 __b_.resize(2);
6138 __b_[0] = 0;
6139 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006140 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006141 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006142 }
6143 else
6144 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006145 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006146 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:40 +00006147 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006148 __init();
6149 }
6150}
6151
Howard Hinnante3e32912011-08-12 21:56:02 +00006152#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6153
Howard Hinnantd6d11712010-05-20 15:11:46 +00006154template<class _RealType>
6155template<class _UnaryOperation>
6156piecewise_constant_distribution<_RealType>::param_type::param_type(
6157 initializer_list<result_type> __bl, _UnaryOperation __fw)
6158 : __b_(__bl.begin(), __bl.end())
6159{
6160 if (__b_.size() < 2)
6161 {
6162 __b_.resize(2);
6163 __b_[0] = 0;
6164 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006165 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006166 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006167 }
6168 else
6169 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006170 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006171 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006172 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006173 __init();
6174 }
6175}
6176
Howard Hinnante3e32912011-08-12 21:56:02 +00006177#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6178
Howard Hinnantd6d11712010-05-20 15:11:46 +00006179template<class _RealType>
6180template<class _UnaryOperation>
6181piecewise_constant_distribution<_RealType>::param_type::param_type(
6182 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6183 : __b_(__nw == 0 ? 2 : __nw + 1)
6184{
6185 size_t __n = __b_.size() - 1;
6186 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:40 +00006187 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006188 for (size_t __i = 0; __i < __n; ++__i)
6189 {
6190 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:40 +00006191 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006192 }
6193 __b_[__n] = __xmax;
6194 __init();
6195}
6196
6197template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006198template<class _URNG>
6199_RealType
6200piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6201{
6202 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006203 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006204 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006205 __u) - __p.__areas_.begin() - 1;
6206 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006207}
6208
6209template <class _CharT, class _Traits, class _RT>
6210basic_ostream<_CharT, _Traits>&
6211operator<<(basic_ostream<_CharT, _Traits>& __os,
6212 const piecewise_constant_distribution<_RT>& __x)
6213{
6214 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00006215 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6216 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006217 _CharT __sp = __os.widen(' ');
6218 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:40 +00006219 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006220 __os << __n;
6221 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006222 __os << __sp << __x.__p_.__b_[__i];
6223 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006224 __os << __sp << __n;
6225 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006226 __os << __sp << __x.__p_.__densities_[__i];
6227 __n = __x.__p_.__areas_.size();
6228 __os << __sp << __n;
6229 for (size_t __i = 0; __i < __n; ++__i)
6230 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006231 return __os;
6232}
6233
6234template <class _CharT, class _Traits, class _RT>
6235basic_istream<_CharT, _Traits>&
6236operator>>(basic_istream<_CharT, _Traits>& __is,
6237 piecewise_constant_distribution<_RT>& __x)
6238{
6239 typedef piecewise_constant_distribution<_RT> _Eng;
6240 typedef typename _Eng::result_type result_type;
6241 typedef typename _Eng::param_type param_type;
6242 __save_flags<_CharT, _Traits> _(__is);
6243 __is.flags(ios_base::dec | ios_base::skipws);
6244 size_t __n;
6245 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006246 vector<result_type> __b(__n);
6247 for (size_t __i = 0; __i < __n; ++__i)
6248 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:40 +00006249 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006250 vector<result_type> __densities(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006251 for (size_t __i = 0; __i < __n; ++__i)
6252 __is >> __densities[__i];
6253 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006254 vector<result_type> __areas(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006255 for (size_t __i = 0; __i < __n; ++__i)
6256 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006257 if (!__is.fail())
6258 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00006259 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:40 +00006260 swap(__x.__p_.__densities_, __densities);
6261 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006262 }
6263 return __is;
6264}
6265
Howard Hinnant54305402010-05-25 00:27:34 +00006266// piecewise_linear_distribution
6267
6268template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006269class _LIBCPP_VISIBLE piecewise_linear_distribution
Howard Hinnant54305402010-05-25 00:27:34 +00006270{
6271public:
6272 // types
6273 typedef _RealType result_type;
6274
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006275 class _LIBCPP_VISIBLE param_type
Howard Hinnant54305402010-05-25 00:27:34 +00006276 {
Howard Hinnant54305402010-05-25 00:27:34 +00006277 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006278 vector<result_type> __densities_;
6279 vector<result_type> __areas_;
Howard Hinnant54305402010-05-25 00:27:34 +00006280 public:
6281 typedef piecewise_linear_distribution distribution_type;
6282
6283 param_type();
6284 template<class _InputIteratorB, class _InputIteratorW>
6285 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6286 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00006287#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006288 template<class _UnaryOperation>
6289 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00006290#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006291 template<class _UnaryOperation>
6292 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6293 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006294 param_type & operator=(const param_type& __rhs);
6295
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006297 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006299 vector<result_type> densities() const {return __densities_;}
Howard Hinnant54305402010-05-25 00:27:34 +00006300
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006301 friend _LIBCPP_INLINE_VISIBILITY
6302 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006303 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006304 friend _LIBCPP_INLINE_VISIBILITY
6305 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006306 {return !(__x == __y);}
6307
6308 private:
6309 void __init();
6310
6311 friend class piecewise_linear_distribution;
6312
6313 template <class _CharT, class _Traits, class _RT>
6314 friend
6315 basic_ostream<_CharT, _Traits>&
6316 operator<<(basic_ostream<_CharT, _Traits>& __os,
6317 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006318
Howard Hinnant54305402010-05-25 00:27:34 +00006319 template <class _CharT, class _Traits, class _RT>
6320 friend
6321 basic_istream<_CharT, _Traits>&
6322 operator>>(basic_istream<_CharT, _Traits>& __is,
6323 piecewise_linear_distribution<_RT>& __x);
6324 };
6325
6326private:
6327 param_type __p_;
6328
6329public:
6330 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006332 piecewise_linear_distribution() {}
6333 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006335 piecewise_linear_distribution(_InputIteratorB __fB,
6336 _InputIteratorB __lB,
6337 _InputIteratorW __fW)
6338 : __p_(__fB, __lB, __fW) {}
Howard Hinnant324bb032010-08-22 00:02:43 +00006339
Howard Hinnante3e32912011-08-12 21:56:02 +00006340#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006341 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006343 piecewise_linear_distribution(initializer_list<result_type> __bl,
6344 _UnaryOperation __fw)
6345 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006346#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006347
6348 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006350 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6351 result_type __xmax, _UnaryOperation __fw)
6352 : __p_(__nw, __xmin, __xmax, __fw) {}
6353
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006355 explicit piecewise_linear_distribution(const param_type& __p)
6356 : __p_(__p) {}
6357
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006358 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006359 void reset() {}
6360
6361 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006362 template<class _URNG>
6363 _LIBCPP_INLINE_VISIBILITY
6364 result_type operator()(_URNG& __g)
Howard Hinnant54305402010-05-25 00:27:34 +00006365 {return (*this)(__g, __p_);}
6366 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6367
6368 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006370 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006372 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant54305402010-05-25 00:27:34 +00006373
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006375 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006377 void param(const param_type& __p) {__p_ = __p;}
6378
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006380 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006382 result_type max() const {return __p_.__b_.back();}
6383
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006384 friend _LIBCPP_INLINE_VISIBILITY
6385 bool operator==(const piecewise_linear_distribution& __x,
6386 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006387 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006388 friend _LIBCPP_INLINE_VISIBILITY
6389 bool operator!=(const piecewise_linear_distribution& __x,
6390 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006391 {return !(__x == __y);}
6392
6393 template <class _CharT, class _Traits, class _RT>
6394 friend
6395 basic_ostream<_CharT, _Traits>&
6396 operator<<(basic_ostream<_CharT, _Traits>& __os,
6397 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006398
Howard Hinnant54305402010-05-25 00:27:34 +00006399 template <class _CharT, class _Traits, class _RT>
6400 friend
6401 basic_istream<_CharT, _Traits>&
6402 operator>>(basic_istream<_CharT, _Traits>& __is,
6403 piecewise_linear_distribution<_RT>& __x);
6404};
6405
6406template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006407typename piecewise_linear_distribution<_RealType>::param_type &
6408piecewise_linear_distribution<_RealType>::param_type::operator=
6409 (const param_type& __rhs)
6410{
6411// These can throw
6412 __b_.reserve (__rhs.__b_.size ());
6413 __densities_.reserve(__rhs.__densities_.size());
6414 __areas_.reserve (__rhs.__areas_.size());
6415
6416// These can not throw
6417 __b_ = __rhs.__b_;
6418 __densities_ = __rhs.__densities_;
6419 __areas_ = __rhs.__areas_;
6420 return *this;
6421}
6422
6423
6424template<class _RealType>
Howard Hinnant54305402010-05-25 00:27:34 +00006425void
6426piecewise_linear_distribution<_RealType>::param_type::__init()
6427{
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006428 __areas_.assign(__densities_.size() - 1, result_type());
Howard Hinnant99968442011-11-29 18:15:50 +00006429 result_type _Sp = 0;
Howard Hinnant54305402010-05-25 00:27:34 +00006430 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6431 {
6432 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6433 (__b_[__i+1] - __b_[__i]) * .5;
Howard Hinnant99968442011-11-29 18:15:50 +00006434 _Sp += __areas_[__i];
Howard Hinnant54305402010-05-25 00:27:34 +00006435 }
6436 for (size_t __i = __areas_.size(); __i > 1;)
6437 {
6438 --__i;
Howard Hinnant99968442011-11-29 18:15:50 +00006439 __areas_[__i] = __areas_[__i-1] / _Sp;
Howard Hinnant54305402010-05-25 00:27:34 +00006440 }
6441 __areas_[0] = 0;
6442 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6443 __areas_[__i] += __areas_[__i-1];
6444 for (size_t __i = 0; __i < __densities_.size(); ++__i)
Howard Hinnant99968442011-11-29 18:15:50 +00006445 __densities_[__i] /= _Sp;
Howard Hinnant54305402010-05-25 00:27:34 +00006446}
6447
6448template<class _RealType>
6449piecewise_linear_distribution<_RealType>::param_type::param_type()
6450 : __b_(2),
6451 __densities_(2, 1.0),
6452 __areas_(1, 0.0)
6453{
6454 __b_[1] = 1;
6455}
6456
6457template<class _RealType>
6458template<class _InputIteratorB, class _InputIteratorW>
6459piecewise_linear_distribution<_RealType>::param_type::param_type(
6460 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6461 : __b_(__fB, __lB)
6462{
6463 if (__b_.size() < 2)
6464 {
6465 __b_.resize(2);
6466 __b_[0] = 0;
6467 __b_[1] = 1;
6468 __densities_.assign(2, 1.0);
6469 __areas_.assign(1, 0.0);
6470 }
6471 else
6472 {
6473 __densities_.reserve(__b_.size());
6474 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6475 __densities_.push_back(*__fW);
6476 __init();
6477 }
6478}
6479
Howard Hinnante3e32912011-08-12 21:56:02 +00006480#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6481
Howard Hinnant54305402010-05-25 00:27:34 +00006482template<class _RealType>
6483template<class _UnaryOperation>
6484piecewise_linear_distribution<_RealType>::param_type::param_type(
6485 initializer_list<result_type> __bl, _UnaryOperation __fw)
6486 : __b_(__bl.begin(), __bl.end())
6487{
6488 if (__b_.size() < 2)
6489 {
6490 __b_.resize(2);
6491 __b_[0] = 0;
6492 __b_[1] = 1;
6493 __densities_.assign(2, 1.0);
6494 __areas_.assign(1, 0.0);
6495 }
6496 else
6497 {
6498 __densities_.reserve(__b_.size());
6499 for (size_t __i = 0; __i < __b_.size(); ++__i)
6500 __densities_.push_back(__fw(__b_[__i]));
6501 __init();
6502 }
6503}
6504
Howard Hinnante3e32912011-08-12 21:56:02 +00006505#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6506
Howard Hinnant54305402010-05-25 00:27:34 +00006507template<class _RealType>
6508template<class _UnaryOperation>
6509piecewise_linear_distribution<_RealType>::param_type::param_type(
6510 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6511 : __b_(__nw == 0 ? 2 : __nw + 1)
6512{
6513 size_t __n = __b_.size() - 1;
6514 result_type __d = (__xmax - __xmin) / __n;
6515 __densities_.reserve(__b_.size());
6516 for (size_t __i = 0; __i < __n; ++__i)
6517 {
6518 __b_[__i] = __xmin + __i * __d;
6519 __densities_.push_back(__fw(__b_[__i]));
6520 }
6521 __b_[__n] = __xmax;
6522 __densities_.push_back(__fw(__b_[__n]));
6523 __init();
6524}
6525
6526template<class _RealType>
6527template<class _URNG>
6528_RealType
6529piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6530{
6531 typedef uniform_real_distribution<result_type> _Gen;
6532 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006533 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006534 __u) - __p.__areas_.begin() - 1;
Howard Hinnant54305402010-05-25 00:27:34 +00006535 __u -= __p.__areas_[__k];
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006536 const result_type __dk = __p.__densities_[__k];
6537 const result_type __dk1 = __p.__densities_[__k+1];
6538 const result_type __deltad = __dk1 - __dk;
Howard Hinnant54305402010-05-25 00:27:34 +00006539 const result_type __bk = __p.__b_[__k];
6540 if (__deltad == 0)
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006541 return __u / __dk + __bk;
Howard Hinnant54305402010-05-25 00:27:34 +00006542 const result_type __bk1 = __p.__b_[__k+1];
6543 const result_type __deltab = __bk1 - __bk;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006544 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnant0949eed2011-06-30 21:18:19 +00006545 _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006546 __deltad;
Howard Hinnant54305402010-05-25 00:27:34 +00006547}
6548
6549template <class _CharT, class _Traits, class _RT>
6550basic_ostream<_CharT, _Traits>&
6551operator<<(basic_ostream<_CharT, _Traits>& __os,
6552 const piecewise_linear_distribution<_RT>& __x)
6553{
6554 __save_flags<_CharT, _Traits> _(__os);
6555 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6556 ios_base::scientific);
6557 _CharT __sp = __os.widen(' ');
6558 __os.fill(__sp);
6559 size_t __n = __x.__p_.__b_.size();
6560 __os << __n;
6561 for (size_t __i = 0; __i < __n; ++__i)
6562 __os << __sp << __x.__p_.__b_[__i];
6563 __n = __x.__p_.__densities_.size();
6564 __os << __sp << __n;
6565 for (size_t __i = 0; __i < __n; ++__i)
6566 __os << __sp << __x.__p_.__densities_[__i];
6567 __n = __x.__p_.__areas_.size();
6568 __os << __sp << __n;
6569 for (size_t __i = 0; __i < __n; ++__i)
6570 __os << __sp << __x.__p_.__areas_[__i];
6571 return __os;
6572}
6573
6574template <class _CharT, class _Traits, class _RT>
6575basic_istream<_CharT, _Traits>&
6576operator>>(basic_istream<_CharT, _Traits>& __is,
6577 piecewise_linear_distribution<_RT>& __x)
6578{
6579 typedef piecewise_linear_distribution<_RT> _Eng;
6580 typedef typename _Eng::result_type result_type;
6581 typedef typename _Eng::param_type param_type;
Howard Hinnant54305402010-05-25 00:27:34 +00006582 __save_flags<_CharT, _Traits> _(__is);
6583 __is.flags(ios_base::dec | ios_base::skipws);
6584 size_t __n;
6585 __is >> __n;
6586 vector<result_type> __b(__n);
6587 for (size_t __i = 0; __i < __n; ++__i)
6588 __is >> __b[__i];
6589 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006590 vector<result_type> __densities(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006591 for (size_t __i = 0; __i < __n; ++__i)
6592 __is >> __densities[__i];
6593 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006594 vector<result_type> __areas(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006595 for (size_t __i = 0; __i < __n; ++__i)
6596 __is >> __areas[__i];
6597 if (!__is.fail())
6598 {
6599 swap(__x.__p_.__b_, __b);
6600 swap(__x.__p_.__densities_, __densities);
6601 swap(__x.__p_.__areas_, __areas);
6602 }
6603 return __is;
6604}
6605
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00006606_LIBCPP_END_NAMESPACE_STD
6607
6608#endif // _LIBCPP_RANDOM