blob: d838289551cad342fb517dd93820f92ca9d8c038 [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
Howard Hinnantc83960a2012-07-20 21:44:27 +0000220 const Engine& base() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000221};
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
Howard Hinnantc83960a2012-07-20 21:44:27 +0000272 const Engine& base() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000273};
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
Howard Hinnantc83960a2012-07-20 21:44:27 +0000326 const Engine& base() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327};
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
Howard Hinnantc83960a2012-07-20 21:44:27 +0000395 double entropy() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396
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{
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001662 static _LIBCPP_CONSTEXPR const bool value =
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001663 !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>
Howard Hinnant33be35e2012-09-14 00:39:16 +00001816class _LIBCPP_VISIBLE linear_congruential_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817
1818template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001819 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnant33be35e2012-09-14 00:39:16 +00001820_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821basic_ostream<_CharT, _Traits>&
1822operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00001823 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001824
1825template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001826 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827basic_istream<_CharT, _Traits>&
1828operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00001829 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001830
1831template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001832class _LIBCPP_VISIBLE linear_congruential_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833{
1834public:
1835 // types
1836 typedef _UIntType result_type;
1837
1838private:
1839 result_type __x_;
1840
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001841 static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842
1843 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1844 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1845public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001846 static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u: 0u;
1847 static _LIBCPP_CONSTEXPR const result_type _Max = __m - 1u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001848 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1849
1850 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001851 static _LIBCPP_CONSTEXPR const result_type multiplier = __a;
1852 static _LIBCPP_CONSTEXPR const result_type increment = __c;
1853 static _LIBCPP_CONSTEXPR const result_type modulus = __m;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001855 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00001857 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
1858 static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859
1860 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862 explicit linear_congruential_engine(result_type __s = default_seed)
1863 {seed(__s);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00001864 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001866 explicit linear_congruential_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001867 typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870 void seed(result_type __s = default_seed)
1871 {seed(integral_constant<bool, __m == 0>(),
1872 integral_constant<bool, __c == 0>(), __s);}
1873 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875 typename enable_if
1876 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00001877 __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001878 void
1879 >::type
1880 seed(_Sseq& __q)
1881 {__seed(__q, integral_constant<unsigned,
1882 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1883 : (__m-1) / 0x100000000ull)>());}
1884
1885 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887 result_type operator()()
Howard Hinnant99968442011-11-29 18:15:50 +00001888 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1891
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001892 friend _LIBCPP_INLINE_VISIBILITY
1893 bool operator==(const linear_congruential_engine& __x,
1894 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001895 {return __x.__x_ == __y.__x_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001896 friend _LIBCPP_INLINE_VISIBILITY
1897 bool operator!=(const linear_congruential_engine& __x,
1898 const linear_congruential_engine& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001899 {return !(__x == __y);}
1900
1901private:
1902
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1909 1 : __s % __m;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1912
1913 template<class _Sseq>
1914 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1915 template<class _Sseq>
1916 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1917
1918 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001919 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920 friend
1921 basic_ostream<_CharT, _Traits>&
1922 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00001923 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001924
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00001926 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 friend
1928 basic_istream<_CharT, _Traits>&
1929 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00001930 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931};
1932
1933template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1934template<class _Sseq>
1935void
1936linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1937 integral_constant<unsigned, 1>)
1938{
1939 const unsigned __k = 1;
1940 uint32_t __ar[__k+3];
1941 __q.generate(__ar, __ar + __k + 3);
1942 result_type __s = static_cast<result_type>(__ar[3] % __m);
1943 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1944}
1945
1946template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1947template<class _Sseq>
1948void
1949linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1950 integral_constant<unsigned, 2>)
1951{
1952 const unsigned __k = 2;
1953 uint32_t __ar[__k+3];
1954 __q.generate(__ar, __ar + __k + 3);
1955 result_type __s = static_cast<result_type>((__ar[3] +
1956 (uint64_t)__ar[4] << 32) % __m);
1957 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1958}
1959
1960template <class _CharT, class _Traits>
1961class __save_flags
1962{
1963 typedef basic_ios<_CharT, _Traits> __stream_type;
1964 typedef typename __stream_type::fmtflags fmtflags;
1965
1966 __stream_type& __stream_;
1967 fmtflags __fmtflags_;
1968 _CharT __fill_;
1969
1970 __save_flags(const __save_flags&);
1971 __save_flags& operator=(const __save_flags&);
1972public:
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001974 explicit __save_flags(__stream_type& __stream)
1975 : __stream_(__stream),
1976 __fmtflags_(__stream.flags()),
1977 __fill_(__stream.fill())
1978 {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001980 ~__save_flags()
1981 {
1982 __stream_.flags(__fmtflags_);
1983 __stream_.fill(__fill_);
1984 }
1985};
1986
1987template <class _CharT, class _Traits,
1988 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001990basic_ostream<_CharT, _Traits>&
1991operator<<(basic_ostream<_CharT, _Traits>& __os,
1992 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1993{
Howard Hinnant9c0df142012-10-30 19:06:59 +00001994 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001995 __os.flags(ios_base::dec | ios_base::left);
1996 __os.fill(__os.widen(' '));
1997 return __os << __x.__x_;
1998}
1999
2000template <class _CharT, class _Traits,
2001 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2002basic_istream<_CharT, _Traits>&
2003operator>>(basic_istream<_CharT, _Traits>& __is,
2004 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2005{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002006 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007 __is.flags(ios_base::dec | ios_base::skipws);
2008 _UIntType __t;
2009 __is >> __t;
2010 if (!__is.fail())
2011 __x.__x_ = __t;
2012 return __is;
2013}
2014
2015typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2016 minstd_rand0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002017typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2018 minstd_rand;
Howard Hinnantd6d11712010-05-20 15:11:46 +00002019typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020// mersenne_twister_engine
2021
2022template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2023 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2024 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant33be35e2012-09-14 00:39:16 +00002025class _LIBCPP_VISIBLE mersenne_twister_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026
Howard Hinnant99968442011-11-29 18:15:50 +00002027template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2028 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2029 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030bool
Howard Hinnant99968442011-11-29 18:15:50 +00002031operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2032 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2033 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2034 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035
Howard Hinnant99968442011-11-29 18:15:50 +00002036template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2037 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2038 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnant33be35e2012-09-14 00:39:16 +00002039_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002040bool
Howard Hinnant99968442011-11-29 18:15:50 +00002041operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2042 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2043 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2044 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045
2046template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002047 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2048 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2049 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050basic_ostream<_CharT, _Traits>&
2051operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002052 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2053 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054
2055template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002056 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2057 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2058 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002059basic_istream<_CharT, _Traits>&
2060operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002061 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2062 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063
2064template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2065 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2066 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002067class _LIBCPP_VISIBLE mersenne_twister_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002068{
2069public:
2070 // types
2071 typedef _UIntType result_type;
2072
2073private:
2074 result_type __x_[__n];
2075 size_t __i_;
2076
2077 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2078 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002079 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002080 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2081 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2082 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2083 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2084 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2085 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2086 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2087public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002088 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2089 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2090 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002091 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2092 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2093 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2094 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2095 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2096 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2097
2098 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002099 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2100 static _LIBCPP_CONSTEXPR const size_t state_size = __n;
2101 static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
2102 static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
2103 static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
2104 static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
2105 static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
2106 static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
2107 static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
2108 static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
2109 static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
2110 static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
2111 static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002113 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002115 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2116 static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002117
2118 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002120 explicit mersenne_twister_engine(result_type __sd = default_seed)
2121 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002122 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002124 explicit mersenne_twister_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002125 typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126 {seed(__q);}
2127 void seed(result_type __sd = default_seed);
2128 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130 typename enable_if
2131 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002132 __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002133 void
2134 >::type
2135 seed(_Sseq& __q)
2136 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2137
2138 // generating functions
2139 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2142
Howard Hinnant99968442011-11-29 18:15:50 +00002143 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2144 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2145 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002146 friend
2147 bool
Howard Hinnant99968442011-11-29 18:15:50 +00002148 operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2149 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2150 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2151 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002152
Howard Hinnant99968442011-11-29 18:15:50 +00002153 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2154 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2155 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002156 friend
2157 bool
Howard Hinnant99968442011-11-29 18:15:50 +00002158 operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2159 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2160 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2161 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162
2163 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002164 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2165 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2166 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002167 friend
2168 basic_ostream<_CharT, _Traits>&
2169 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002170 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2171 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002172
2173 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002174 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2175 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2176 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002177 friend
2178 basic_istream<_CharT, _Traits>&
2179 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002180 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2181 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182private:
2183
2184 template<class _Sseq>
2185 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2186 template<class _Sseq>
2187 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2188
2189 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002191 static
2192 typename enable_if
2193 <
2194 __count < __w,
2195 result_type
2196 >::type
2197 __lshift(result_type __x) {return (__x << __count) & _Max;}
2198
2199 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002201 static
2202 typename enable_if
2203 <
2204 (__count >= __w),
2205 result_type
2206 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00002207 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208
2209 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002211 static
2212 typename enable_if
2213 <
2214 __count < _Dt,
2215 result_type
2216 >::type
2217 __rshift(result_type __x) {return __x >> __count;}
2218
2219 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221 static
2222 typename enable_if
2223 <
2224 (__count >= _Dt),
2225 result_type
2226 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00002227 __rshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228};
2229
2230template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2231 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2232 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2233void
2234mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2235 __t, __c, __l, __f>::seed(result_type __sd)
2236{ // __w >= 2
2237 __x_[0] = __sd & _Max;
2238 for (size_t __i = 1; __i < __n; ++__i)
2239 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2240 __i_ = 0;
2241}
2242
2243template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2244 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2245 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2246template<class _Sseq>
2247void
2248mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2249 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2250{
2251 const unsigned __k = 1;
2252 uint32_t __ar[__n * __k];
2253 __q.generate(__ar, __ar + __n * __k);
2254 for (size_t __i = 0; __i < __n; ++__i)
2255 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2256 const result_type __mask = __r == _Dt ? result_type(~0) :
2257 (result_type(1) << __r) - result_type(1);
2258 __i_ = 0;
2259 if ((__x_[0] & ~__mask) == 0)
2260 {
2261 for (size_t __i = 1; __i < __n; ++__i)
2262 if (__x_[__i] != 0)
2263 return;
2264 __x_[0] = _Max;
2265 }
2266}
2267
2268template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2269 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2270 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2271template<class _Sseq>
2272void
2273mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2274 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2275{
2276 const unsigned __k = 2;
2277 uint32_t __ar[__n * __k];
2278 __q.generate(__ar, __ar + __n * __k);
2279 for (size_t __i = 0; __i < __n; ++__i)
2280 __x_[__i] = static_cast<result_type>(
2281 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2282 const result_type __mask = __r == _Dt ? result_type(~0) :
2283 (result_type(1) << __r) - result_type(1);
2284 __i_ = 0;
2285 if ((__x_[0] & ~__mask) == 0)
2286 {
2287 for (size_t __i = 1; __i < __n; ++__i)
2288 if (__x_[__i] != 0)
2289 return;
2290 __x_[0] = _Max;
2291 }
2292}
2293
2294template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2295 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2296 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2297_UIntType
2298mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2299 __t, __c, __l, __f>::operator()()
2300{
2301 const size_t __j = (__i_ + 1) % __n;
2302 const result_type __mask = __r == _Dt ? result_type(~0) :
2303 (result_type(1) << __r) - result_type(1);
Howard Hinnant99968442011-11-29 18:15:50 +00002304 const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002305 const size_t __k = (__i_ + __m) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00002306 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002307 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2308 __i_ = __j;
2309 __z ^= __lshift<__s>(__z) & __b;
2310 __z ^= __lshift<__t>(__z) & __c;
2311 return __z ^ __rshift<__l>(__z);
2312}
2313
Howard Hinnant99968442011-11-29 18:15:50 +00002314template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2315 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2316 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002317bool
Howard Hinnant99968442011-11-29 18:15:50 +00002318operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2319 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2320 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2321 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322{
2323 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:50 +00002324 return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002325 if (__x.__i_ == 0 || __y.__i_ == 0)
2326 {
Howard Hinnant99968442011-11-29 18:15:50 +00002327 size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002328 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329 __y.__x_ + __y.__i_))
2330 return false;
2331 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00002332 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
2333 return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002334 }
2335 if (__x.__i_ < __y.__i_)
2336 {
Howard Hinnant99968442011-11-29 18:15:50 +00002337 size_t __j = _Np - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002338 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002339 __y.__x_ + __y.__i_))
2340 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002341 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002342 __y.__x_))
2343 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002344 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002345 __y.__x_ + (_Np - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002346 }
Howard Hinnant99968442011-11-29 18:15:50 +00002347 size_t __j = _Np - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002348 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349 __x.__x_ + __x.__i_))
2350 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002351 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352 __x.__x_))
2353 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002354 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002355 __x.__x_ + (_Np - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002356}
2357
Howard Hinnant99968442011-11-29 18:15:50 +00002358template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2359 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2360 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002361inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002362bool
Howard Hinnant99968442011-11-29 18:15:50 +00002363operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2364 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2365 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2366 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002367{
2368 return !(__x == __y);
2369}
2370
2371template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002372 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2373 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2374 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002375basic_ostream<_CharT, _Traits>&
2376operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002377 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2378 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002379{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002380 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381 __os.flags(ios_base::dec | ios_base::left);
2382 _CharT __sp = __os.widen(' ');
2383 __os.fill(__sp);
2384 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:50 +00002385 for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002386 __os << __sp << __x.__x_[__j];
2387 for (size_t __j = 0; __j < __x.__i_; ++__j)
2388 __os << __sp << __x.__x_[__j];
2389 return __os;
2390}
2391
2392template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002393 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2394 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2395 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396basic_istream<_CharT, _Traits>&
2397operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002398 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2399 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002401 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:50 +00002403 _UI __t[_Np];
2404 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002405 __is >> __t[__i];
2406 if (!__is.fail())
2407 {
Howard Hinnant99968442011-11-29 18:15:50 +00002408 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409 __x.__x_[__i] = __t[__i];
2410 __x.__i_ = 0;
2411 }
2412 return __is;
2413}
2414
2415typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2416 0x9908b0df, 11, 0xffffffff,
2417 7, 0x9d2c5680,
2418 15, 0xefc60000,
2419 18, 1812433253> mt19937;
2420typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2421 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2422 17, 0x71d67fffeda60000ULL,
2423 37, 0xfff7eee000000000ULL,
2424 43, 6364136223846793005ULL> mt19937_64;
2425
2426// subtract_with_carry_engine
2427
2428template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant33be35e2012-09-14 00:39:16 +00002429class _LIBCPP_VISIBLE subtract_with_carry_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002430
Howard Hinnant99968442011-11-29 18:15:50 +00002431template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002432bool
2433operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002434 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2435 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436
Howard Hinnant99968442011-11-29 18:15:50 +00002437template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnant33be35e2012-09-14 00:39:16 +00002438_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439bool
2440operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002441 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2442 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443
2444template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002445 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002446basic_ostream<_CharT, _Traits>&
2447operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002448 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449
2450template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002451 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452basic_istream<_CharT, _Traits>&
2453operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002454 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455
2456template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002457class _LIBCPP_VISIBLE subtract_with_carry_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458{
2459public:
2460 // types
2461 typedef _UIntType result_type;
2462
2463private:
2464 result_type __x_[__r];
2465 result_type __c_;
2466 size_t __i_;
2467
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002468 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002469 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2470 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2471 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2472 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2473public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002474 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2475 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2476 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002477 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2478
2479 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002480 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2481 static _LIBCPP_CONSTEXPR const size_t short_lag = __s;
2482 static _LIBCPP_CONSTEXPR const size_t long_lag = __r;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002484 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002486 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2487 static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002488
2489 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002491 explicit subtract_with_carry_engine(result_type __sd = default_seed)
2492 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002493 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002495 explicit subtract_with_carry_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002496 typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002497 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002498 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002499 void seed(result_type __sd = default_seed)
2500 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2501 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503 typename enable_if
2504 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002505 __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002506 void
2507 >::type
2508 seed(_Sseq& __q)
2509 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2510
2511 // generating functions
2512 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2515
Howard Hinnant99968442011-11-29 18:15:50 +00002516 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002517 friend
2518 bool
2519 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002520 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2521 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002522
Howard Hinnant99968442011-11-29 18:15:50 +00002523 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002524 friend
2525 bool
2526 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002527 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2528 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002529
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002530 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002531 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532 friend
2533 basic_ostream<_CharT, _Traits>&
2534 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002535 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002536
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002537 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002538 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002539 friend
2540 basic_istream<_CharT, _Traits>&
2541 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002542 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002543
2544private:
2545
2546 void seed(result_type __sd, integral_constant<unsigned, 1>);
2547 void seed(result_type __sd, integral_constant<unsigned, 2>);
2548 template<class _Sseq>
2549 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2550 template<class _Sseq>
2551 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2552};
2553
2554template<class _UIntType, size_t __w, size_t __s, size_t __r>
2555void
2556subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2557 integral_constant<unsigned, 1>)
2558{
2559 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2560 __e(__sd == 0u ? default_seed : __sd);
2561 for (size_t __i = 0; __i < __r; ++__i)
2562 __x_[__i] = static_cast<result_type>(__e() & _Max);
2563 __c_ = __x_[__r-1] == 0;
2564 __i_ = 0;
2565}
2566
2567template<class _UIntType, size_t __w, size_t __s, size_t __r>
2568void
2569subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2570 integral_constant<unsigned, 2>)
2571{
2572 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2573 __e(__sd == 0u ? default_seed : __sd);
2574 for (size_t __i = 0; __i < __r; ++__i)
Howard Hinnant43edf2d2011-08-15 17:22:22 +00002575 {
2576 result_type __e0 = __e();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577 __x_[__i] = static_cast<result_type>(
Howard Hinnant43edf2d2011-08-15 17:22:22 +00002578 (__e0 + ((uint64_t)__e() << 32)) & _Max);
2579 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002580 __c_ = __x_[__r-1] == 0;
2581 __i_ = 0;
2582}
2583
2584template<class _UIntType, size_t __w, size_t __s, size_t __r>
2585template<class _Sseq>
2586void
2587subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2588 integral_constant<unsigned, 1>)
2589{
2590 const unsigned __k = 1;
2591 uint32_t __ar[__r * __k];
2592 __q.generate(__ar, __ar + __r * __k);
2593 for (size_t __i = 0; __i < __r; ++__i)
2594 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2595 __c_ = __x_[__r-1] == 0;
2596 __i_ = 0;
2597}
2598
2599template<class _UIntType, size_t __w, size_t __s, size_t __r>
2600template<class _Sseq>
2601void
2602subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2603 integral_constant<unsigned, 2>)
2604{
2605 const unsigned __k = 2;
2606 uint32_t __ar[__r * __k];
2607 __q.generate(__ar, __ar + __r * __k);
2608 for (size_t __i = 0; __i < __r; ++__i)
2609 __x_[__i] = static_cast<result_type>(
2610 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2611 __c_ = __x_[__r-1] == 0;
2612 __i_ = 0;
2613}
2614
2615template<class _UIntType, size_t __w, size_t __s, size_t __r>
2616_UIntType
2617subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2618{
2619 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2620 result_type& __xr = __x_[__i_];
2621 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2622 __xr = (__xs - __xr - __c_) & _Max;
2623 __c_ = __new_c;
2624 __i_ = (__i_ + 1) % __r;
2625 return __xr;
2626}
2627
Howard Hinnant99968442011-11-29 18:15:50 +00002628template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002629bool
2630operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002631 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2632 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633{
2634 if (__x.__c_ != __y.__c_)
2635 return false;
2636 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:50 +00002637 return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002638 if (__x.__i_ == 0 || __y.__i_ == 0)
2639 {
Howard Hinnant99968442011-11-29 18:15:50 +00002640 size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002641 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002642 __y.__x_ + __y.__i_))
2643 return false;
2644 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00002645 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
2646 return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002647 }
2648 if (__x.__i_ < __y.__i_)
2649 {
Howard Hinnant99968442011-11-29 18:15:50 +00002650 size_t __j = _Rp - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002651 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652 __y.__x_ + __y.__i_))
2653 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002654 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002655 __y.__x_))
2656 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002657 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002658 __y.__x_ + (_Rp - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002659 }
Howard Hinnant99968442011-11-29 18:15:50 +00002660 size_t __j = _Rp - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002661 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002662 __x.__x_ + __x.__i_))
2663 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002664 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665 __x.__x_))
2666 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002667 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002668 __x.__x_ + (_Rp - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002669}
2670
Howard Hinnant99968442011-11-29 18:15:50 +00002671template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002672inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002673bool
2674operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002675 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2676 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002677{
2678 return !(__x == __y);
2679}
2680
2681template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002682 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683basic_ostream<_CharT, _Traits>&
2684operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002685 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002686{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002687 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002688 __os.flags(ios_base::dec | ios_base::left);
2689 _CharT __sp = __os.widen(' ');
2690 __os.fill(__sp);
2691 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:50 +00002692 for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002693 __os << __sp << __x.__x_[__j];
2694 for (size_t __j = 0; __j < __x.__i_; ++__j)
2695 __os << __sp << __x.__x_[__j];
2696 __os << __sp << __x.__c_;
2697 return __os;
2698}
2699
2700template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002701 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702basic_istream<_CharT, _Traits>&
2703operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002704 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002705{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002706 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:50 +00002708 _UI __t[_Rp+1];
2709 for (size_t __i = 0; __i < _Rp+1; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002710 __is >> __t[__i];
2711 if (!__is.fail())
2712 {
Howard Hinnant99968442011-11-29 18:15:50 +00002713 for (size_t __i = 0; __i < _Rp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002714 __x.__x_[__i] = __t[__i];
Howard Hinnant99968442011-11-29 18:15:50 +00002715 __x.__c_ = __t[_Rp];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716 __x.__i_ = 0;
2717 }
2718 return __is;
2719}
2720
2721typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2722typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2723
2724// discard_block_engine
2725
2726template<class _Engine, size_t __p, size_t __r>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002727class _LIBCPP_VISIBLE discard_block_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728{
2729 _Engine __e_;
2730 int __n_;
2731
2732 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2733 static_assert(__r <= __p, "discard_block_engine invalid parameters");
2734public:
2735 // types
2736 typedef typename _Engine::result_type result_type;
2737
2738 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002739 static _LIBCPP_CONSTEXPR const size_t block_size = __p;
2740 static _LIBCPP_CONSTEXPR const size_t used_block = __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002741
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002742#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002743 static const result_type _Min = _Engine::_Min;
2744 static const result_type _Max = _Engine::_Max;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002745#else
2746 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
2747 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
2748#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002751 static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002753 static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754
2755 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002757 discard_block_engine() : __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002759 explicit discard_block_engine(const _Engine& __e)
2760 : __e_(__e), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002761#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002763 explicit discard_block_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002764 : __e_(_VSTD::move(__e)), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002765#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002768 template<class _Sseq>
2769 _LIBCPP_INLINE_VISIBILITY
2770 explicit discard_block_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002771 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00002772 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002773 : __e_(__q), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775 void seed() {__e_.seed(); __n_ = 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002777 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002778 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002780 typename enable_if
2781 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002782 __is_seed_sequence<_Sseq, discard_block_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00002783 void
2784 >::type
2785 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002786
2787 // generating functions
2788 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002790 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2791
2792 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00002794 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +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 Hinnant99968442011-11-29 18:15:50 +00002803 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002804 friend
2805 bool
2806 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002807 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2808 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
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_ostream<_CharT, _Traits>&
2814 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002815 const discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002816
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002817 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002818 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002819 friend
2820 basic_istream<_CharT, _Traits>&
2821 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002822 discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823};
2824
2825template<class _Engine, size_t __p, size_t __r>
2826typename discard_block_engine<_Engine, __p, __r>::result_type
2827discard_block_engine<_Engine, __p, __r>::operator()()
2828{
2829 if (__n_ >= __r)
2830 {
2831 __e_.discard(__p - __r);
2832 __n_ = 0;
2833 }
2834 ++__n_;
2835 return __e_();
2836}
2837
Howard Hinnant99968442011-11-29 18:15:50 +00002838template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002839inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840bool
Howard Hinnant99968442011-11-29 18:15:50 +00002841operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2842 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002843{
2844 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2845}
2846
Howard Hinnant99968442011-11-29 18:15:50 +00002847template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002848inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002849bool
Howard Hinnant99968442011-11-29 18:15:50 +00002850operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2851 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002852{
2853 return !(__x == __y);
2854}
2855
2856template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002857 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858basic_ostream<_CharT, _Traits>&
2859operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002860 const discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002861{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002862 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002863 __os.flags(ios_base::dec | ios_base::left);
2864 _CharT __sp = __os.widen(' ');
2865 __os.fill(__sp);
2866 return __os << __x.__e_ << __sp << __x.__n_;
2867}
2868
2869template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002870 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002871basic_istream<_CharT, _Traits>&
2872operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002873 discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002874{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002875 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002876 __is.flags(ios_base::dec | ios_base::skipws);
2877 _Eng __e;
2878 int __n;
2879 __is >> __e >> __n;
2880 if (!__is.fail())
2881 {
2882 __x.__e_ = __e;
2883 __x.__n_ = __n;
2884 }
2885 return __is;
2886}
2887
2888typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2889typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2890
2891// independent_bits_engine
2892
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002893template<class _Engine, size_t __w, class _UIntType>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002894class _LIBCPP_VISIBLE independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002895{
Howard Hinnant99968442011-11-29 18:15:50 +00002896 template <class _UI, _UI _R0, size_t _Wp, size_t _Mp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002897 class __get_n
2898 {
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002899 static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UI>::digits;
2900 static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
2901 static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
2902 static _LIBCPP_CONSTEXPR const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002903 public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002904 static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002905 };
2906public:
2907 // types
2908 typedef _UIntType result_type;
2909
2910private:
2911 _Engine __e_;
2912
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002913 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002914 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2915 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2916
2917 typedef typename _Engine::result_type _Engine_result_type;
2918 typedef typename conditional
2919 <
2920 sizeof(_Engine_result_type) <= sizeof(result_type),
2921 result_type,
2922 _Engine_result_type
2923 >::type _Working_result_type;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002924#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant99968442011-11-29 18:15:50 +00002925 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002926 + _Working_result_type(1);
2927#else
2928 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2929 + _Working_result_type(1);
2930#endif
2931 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2932 static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
2933 static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
2934 static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
2935 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2936 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2937 static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2938 (_Rp >> __w0) << __w0;
2939 static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2940 (_Rp >> (__w0+1)) << (__w0+1);
2941 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002942 _Engine_result_type(~0) >> (_EDt - __w0) :
2943 _Engine_result_type(0);
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002944 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002945 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2946 _Engine_result_type(~0);
2947public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002948 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2949 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2950 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002951 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2952
2953 // engine characteristics
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002955 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002957 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002958
2959 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002961 independent_bits_engine() {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002963 explicit independent_bits_engine(const _Engine& __e)
2964 : __e_(__e) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002965#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002967 explicit independent_bits_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002968 : __e_(_VSTD::move(__e)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002969#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002971 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002972 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002974 explicit independent_bits_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002975 typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00002976 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002977 : __e_(__q) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002979 void seed() {__e_.seed();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002981 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002982 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002984 typename enable_if
2985 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002986 __is_seed_sequence<_Sseq, independent_bits_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00002987 void
2988 >::type
2989 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002990
2991 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002993 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002995 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2996
2997 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00002999 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003000
Howard Hinnant99968442011-11-29 18:15:50 +00003001 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003002 friend
3003 bool
3004 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003005 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3006 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003007
Howard Hinnant99968442011-11-29 18:15:50 +00003008 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003009 friend
3010 bool
3011 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003012 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3013 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003014
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003015 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003016 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003017 friend
3018 basic_ostream<_CharT, _Traits>&
3019 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003020 const independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003021
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003022 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003023 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003024 friend
3025 basic_istream<_CharT, _Traits>&
3026 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003027 independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003028
3029private:
3030 result_type __eval(false_type);
3031 result_type __eval(true_type);
3032
3033 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003035 static
3036 typename enable_if
3037 <
3038 __count < _Dt,
3039 result_type
3040 >::type
3041 __lshift(result_type __x) {return __x << __count;}
3042
3043 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003045 static
3046 typename enable_if
3047 <
3048 (__count >= _Dt),
3049 result_type
3050 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00003051 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003052};
3053
3054template<class _Engine, size_t __w, class _UIntType>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003055inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003056_UIntType
3057independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
3058{
3059 return static_cast<result_type>(__e_() & __mask0);
3060}
3061
3062template<class _Engine, size_t __w, class _UIntType>
3063_UIntType
3064independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
3065{
Howard Hinnant99968442011-11-29 18:15:50 +00003066 result_type _Sp = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003067 for (size_t __k = 0; __k < __n0; ++__k)
3068 {
3069 _Engine_result_type __u;
3070 do
3071 {
3072 __u = __e_() - _Engine::min();
3073 } while (__u >= __y0);
Howard Hinnant99968442011-11-29 18:15:50 +00003074 _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003075 }
3076 for (size_t __k = __n0; __k < __n; ++__k)
3077 {
3078 _Engine_result_type __u;
3079 do
3080 {
3081 __u = __e_() - _Engine::min();
3082 } while (__u >= __y1);
Howard Hinnant99968442011-11-29 18:15:50 +00003083 _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003084 }
Howard Hinnant99968442011-11-29 18:15:50 +00003085 return _Sp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003086}
3087
Howard Hinnant99968442011-11-29 18:15:50 +00003088template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003089inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003090bool
3091operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003092 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3093 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003094{
3095 return __x.base() == __y.base();
3096}
3097
Howard Hinnant99968442011-11-29 18:15:50 +00003098template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003099inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003100bool
3101operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003102 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3103 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003104{
3105 return !(__x == __y);
3106}
3107
3108template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003109 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003110basic_ostream<_CharT, _Traits>&
3111operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003112 const independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003113{
3114 return __os << __x.base();
3115}
3116
3117template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003118 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003119basic_istream<_CharT, _Traits>&
3120operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003121 independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003122{
3123 _Eng __e;
3124 __is >> __e;
3125 if (!__is.fail())
3126 __x.__e_ = __e;
3127 return __is;
3128}
3129
3130// shuffle_order_engine
3131
3132template <uint64_t _Xp, uint64_t _Yp>
3133struct __ugcd
3134{
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003135 static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003136};
3137
3138template <uint64_t _Xp>
3139struct __ugcd<_Xp, 0>
3140{
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003141 static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003142};
3143
Howard Hinnant99968442011-11-29 18:15:50 +00003144template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003145class __uratio
3146{
Howard Hinnant99968442011-11-29 18:15:50 +00003147 static_assert(_Dp != 0, "__uratio divide by 0");
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003148 static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003149public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003150 static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
3151 static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003152
3153 typedef __uratio<num, den> type;
3154};
3155
3156template<class _Engine, size_t __k>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003157class _LIBCPP_VISIBLE shuffle_order_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003158{
3159 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3160public:
3161 // types
3162 typedef typename _Engine::result_type result_type;
3163
3164private:
3165 _Engine __e_;
3166 result_type _V_[__k];
3167 result_type _Y_;
3168
3169public:
3170 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003171 static _LIBCPP_CONSTEXPR const size_t table_size = __k;
Howard Hinnant324bb032010-08-22 00:02:43 +00003172
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003173#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003174 static const result_type _Min = _Engine::_Min;
3175 static const result_type _Max = _Engine::_Max;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003176#else
3177 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
3178 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
3179#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003180 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003182 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003184 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003185
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003186 static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003187
3188 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003190 shuffle_order_engine() {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003192 explicit shuffle_order_engine(const _Engine& __e)
3193 : __e_(__e) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003194#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003195 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003196 explicit shuffle_order_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003197 : __e_(_VSTD::move(__e)) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003198#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003200 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnantec3773c2011-12-01 20:21:04 +00003201 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00003203 explicit shuffle_order_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003204 typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00003205 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003206 : __e_(__q) {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003208 void seed() {__e_.seed(); __init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003210 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003211 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003213 typename enable_if
3214 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003215 __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00003216 void
3217 >::type
3218 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003219
3220 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003222 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003223 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003224 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3225
3226 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00003228 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003229
3230private:
Howard Hinnant99968442011-11-29 18:15:50 +00003231 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003232 friend
3233 bool
3234 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003235 const shuffle_order_engine<_Eng, _Kp>& __x,
3236 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003237
Howard Hinnant99968442011-11-29 18:15:50 +00003238 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003239 friend
3240 bool
3241 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003242 const shuffle_order_engine<_Eng, _Kp>& __x,
3243 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003244
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003245 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003246 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003247 friend
3248 basic_ostream<_CharT, _Traits>&
3249 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003250 const shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003251
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003252 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003253 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003254 friend
3255 basic_istream<_CharT, _Traits>&
3256 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003257 shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003258
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003260 void __init()
3261 {
3262 for (size_t __i = 0; __i < __k; ++__i)
3263 _V_[__i] = __e_();
3264 _Y_ = __e_();
3265 }
3266
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003268 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003270 result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003271
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003273 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003275 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3276
Howard Hinnant99968442011-11-29 18:15:50 +00003277 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003279 typename enable_if
3280 <
Howard Hinnant99968442011-11-29 18:15:50 +00003281 (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003282 result_type
3283 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00003284 __eval(__uratio<_Np, _Dp>)
3285 {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003286
Howard Hinnant99968442011-11-29 18:15:50 +00003287 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003289 typename enable_if
3290 <
Howard Hinnant99968442011-11-29 18:15:50 +00003291 __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003292 result_type
3293 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00003294 __eval(__uratio<_Np, _Dp>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003295 {
Howard Hinnant99968442011-11-29 18:15:50 +00003296 const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
3297 / __uratio<_Np, _Dp>::den);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003298 _Y_ = _V_[__j];
3299 _V_[__j] = __e_();
3300 return _Y_;
3301 }
3302
3303 template <uint64_t __n, uint64_t __d>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003305 result_type __evalf()
3306 {
Howard Hinnant99968442011-11-29 18:15:50 +00003307 const double _Fp = __d == 0 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003308 __n / (2. * 0x8000000000000000ull) :
3309 __n / (double)__d;
Howard Hinnant99968442011-11-29 18:15:50 +00003310 const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003311 _Y_ = _V_[__j];
3312 _V_[__j] = __e_();
3313 return _Y_;
3314 }
3315};
3316
Howard Hinnant99968442011-11-29 18:15:50 +00003317template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003318bool
3319operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003320 const shuffle_order_engine<_Eng, _Kp>& __x,
3321 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003322{
Howard Hinnant99968442011-11-29 18:15:50 +00003323 return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003324 __x.__e_ == __y.__e_;
3325}
3326
Howard Hinnant99968442011-11-29 18:15:50 +00003327template<class _Eng, size_t _Kp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003328inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003329bool
3330operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003331 const shuffle_order_engine<_Eng, _Kp>& __x,
3332 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003333{
3334 return !(__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_ostream<_CharT, _Traits>&
3340operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003341 const shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003342{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003343 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003344 __os.flags(ios_base::dec | ios_base::left);
3345 _CharT __sp = __os.widen(' ');
3346 __os.fill(__sp);
3347 __os << __x.__e_ << __sp << __x._V_[0];
Howard Hinnant99968442011-11-29 18:15:50 +00003348 for (size_t __i = 1; __i < _Kp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003349 __os << __sp << __x._V_[__i];
3350 return __os << __sp << __x._Y_;
3351}
3352
3353template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003354 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003355basic_istream<_CharT, _Traits>&
3356operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003357 shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003358{
Howard Hinnant99968442011-11-29 18:15:50 +00003359 typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003360 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003361 __is.flags(ios_base::dec | ios_base::skipws);
3362 _Eng __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003363 result_type _Vp[_Kp+1];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003364 __is >> __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003365 for (size_t __i = 0; __i < _Kp+1; ++__i)
3366 __is >> _Vp[__i];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003367 if (!__is.fail())
3368 {
3369 __x.__e_ = __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003370 for (size_t __i = 0; __i < _Kp; ++__i)
3371 __x._V_[__i] = _Vp[__i];
3372 __x._Y_ = _Vp[_Kp];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003373 }
3374 return __is;
3375}
3376
3377typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3378
3379// random_device
3380
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003381class _LIBCPP_VISIBLE random_device
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003382{
3383 int __f_;
3384public:
3385 // types
3386 typedef unsigned result_type;
3387
3388 // generator characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003389 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3390 static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003391
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003393 static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003395 static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003396
3397 // constructors
3398 explicit random_device(const string& __token = "/dev/urandom");
3399 ~random_device();
3400
3401 // generating functions
3402 result_type operator()();
3403
3404 // property functions
Howard Hinnantc83960a2012-07-20 21:44:27 +00003405 double entropy() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003406
3407private:
3408 // no copy functions
3409 random_device(const random_device&); // = delete;
3410 random_device& operator=(const random_device&); // = delete;
3411};
3412
3413// seed_seq
3414
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003415class _LIBCPP_VISIBLE seed_seq
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003416{
3417public:
3418 // types
3419 typedef uint32_t result_type;
3420
3421private:
3422 vector<result_type> __v_;
3423
3424 template<class _InputIterator>
3425 void init(_InputIterator __first, _InputIterator __last);
3426public:
3427 // constructors
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003429 seed_seq() {}
Howard Hinnante3e32912011-08-12 21:56:02 +00003430#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003431 template<class _Tp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003433 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00003434#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant324bb032010-08-22 00:02:43 +00003435
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003438 seed_seq(_InputIterator __first, _InputIterator __last)
3439 {init(__first, __last);}
3440
3441 // generating functions
3442 template<class _RandomAccessIterator>
3443 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3444
3445 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003447 size_t size() const {return __v_.size();}
3448 template<class _OutputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003450 void param(_OutputIterator __dest) const
Howard Hinnant0949eed2011-06-30 21:18:19 +00003451 {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003452
3453private:
3454 // no copy functions
3455 seed_seq(const seed_seq&); // = delete;
3456 void operator=(const seed_seq&); // = delete;
3457
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003459 static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003460};
3461
3462template<class _InputIterator>
3463void
3464seed_seq::init(_InputIterator __first, _InputIterator __last)
3465{
3466 for (_InputIterator __s = __first; __s != __last; ++__s)
3467 __v_.push_back(*__s & 0xFFFFFFFF);
3468}
3469
3470template<class _RandomAccessIterator>
3471void
3472seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3473{
3474 if (__first != __last)
3475 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003476 _VSTD::fill(__first, __last, 0x8b8b8b8b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003477 const size_t __n = static_cast<size_t>(__last - __first);
3478 const size_t __s = __v_.size();
3479 const size_t __t = (__n >= 623) ? 11
3480 : (__n >= 68) ? 7
3481 : (__n >= 39) ? 5
3482 : (__n >= 7) ? 3
3483 : (__n - 1) / 2;
3484 const size_t __p = (__n - __t) / 2;
3485 const size_t __q = __p + __t;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003486 const size_t __m = _VSTD::max(__s + 1, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003487 // __k = 0;
3488 {
Howard Hinnant99968442011-11-29 18:15:50 +00003489 result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003490 ^ __first[__n - 1]);
3491 __first[__p] += __r;
3492 __r += __s;
3493 __first[__q] += __r;
3494 __first[0] = __r;
3495 }
3496 for (size_t __k = 1; __k <= __s; ++__k)
3497 {
3498 const size_t __kmodn = __k % __n;
3499 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003500 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003501 ^ __first[(__k - 1) % __n]);
3502 __first[__kpmodn] += __r;
3503 __r += __kmodn + __v_[__k-1];
3504 __first[(__k + __q) % __n] += __r;
3505 __first[__kmodn] = __r;
3506 }
3507 for (size_t __k = __s + 1; __k < __m; ++__k)
3508 {
3509 const size_t __kmodn = __k % __n;
3510 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003511 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003512 ^ __first[(__k - 1) % __n]);
3513 __first[__kpmodn] += __r;
3514 __r += __kmodn;
3515 __first[(__k + __q) % __n] += __r;
3516 __first[__kmodn] = __r;
3517 }
3518 for (size_t __k = __m; __k < __m + __n; ++__k)
3519 {
3520 const size_t __kmodn = __k % __n;
3521 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003522 result_type __r = 1566083941 * _Tp(__first[__kmodn] +
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003523 __first[__kpmodn] +
3524 __first[(__k - 1) % __n]);
3525 __first[__kpmodn] ^= __r;
3526 __r -= __kmodn;
3527 __first[(__k + __q) % __n] ^= __r;
3528 __first[__kmodn] = __r;
3529 }
3530 }
3531}
3532
Howard Hinnant30a840f2010-05-12 17:08:57 +00003533// generate_canonical
3534
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003535template<class _RealType, size_t __bits, class _URNG>
3536_RealType
3537generate_canonical(_URNG& __g)
3538{
3539 const size_t _Dt = numeric_limits<_RealType>::digits;
3540 const size_t __b = _Dt < __bits ? _Dt : __bits;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003541#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003542 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003543#else
3544 const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
3545#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003546 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003547 const _RealType _Rp = _URNG::max() - _URNG::min() + _RealType(1);
Howard Hinnant99968442011-11-29 18:15:50 +00003548 _RealType __base = _Rp;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003549 _RealType _Sp = __g() - _URNG::min();
Howard Hinnant99968442011-11-29 18:15:50 +00003550 for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp)
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003551 _Sp += (__g() - _URNG::min()) * __base;
Howard Hinnant99968442011-11-29 18:15:50 +00003552 return _Sp / __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003553}
3554
Howard Hinnant30a840f2010-05-12 17:08:57 +00003555// uniform_int_distribution
3556
Howard Hinnantc3267212010-05-26 17:49:34 +00003557// in <algorithm>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003558
3559template <class _CharT, class _Traits, class _IT>
3560basic_ostream<_CharT, _Traits>&
3561operator<<(basic_ostream<_CharT, _Traits>& __os,
3562 const uniform_int_distribution<_IT>& __x)
3563{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003564 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003565 __os.flags(ios_base::dec | ios_base::left);
3566 _CharT __sp = __os.widen(' ');
3567 __os.fill(__sp);
3568 return __os << __x.a() << __sp << __x.b();
3569}
3570
3571template <class _CharT, class _Traits, class _IT>
3572basic_istream<_CharT, _Traits>&
3573operator>>(basic_istream<_CharT, _Traits>& __is,
3574 uniform_int_distribution<_IT>& __x)
3575{
3576 typedef uniform_int_distribution<_IT> _Eng;
3577 typedef typename _Eng::result_type result_type;
3578 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003579 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003580 __is.flags(ios_base::dec | ios_base::skipws);
3581 result_type __a;
3582 result_type __b;
3583 __is >> __a >> __b;
3584 if (!__is.fail())
3585 __x.param(param_type(__a, __b));
3586 return __is;
3587}
3588
Howard Hinnant30a840f2010-05-12 17:08:57 +00003589// uniform_real_distribution
3590
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003591template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003592class _LIBCPP_VISIBLE uniform_real_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003593{
3594public:
3595 // types
3596 typedef _RealType result_type;
3597
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003598 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003599 {
3600 result_type __a_;
3601 result_type __b_;
3602 public:
3603 typedef uniform_real_distribution distribution_type;
3604
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003606 explicit param_type(result_type __a = 0,
3607 result_type __b = 1)
3608 : __a_(__a), __b_(__b) {}
3609
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003611 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003613 result_type b() const {return __b_;}
3614
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003615 friend _LIBCPP_INLINE_VISIBILITY
3616 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003617 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003618 friend _LIBCPP_INLINE_VISIBILITY
3619 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003620 {return !(__x == __y);}
3621 };
3622
3623private:
3624 param_type __p_;
3625
3626public:
3627 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003629 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3630 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003634 void reset() {}
3635
3636 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003637 template<class _URNG>
3638 _LIBCPP_INLINE_VISIBILITY
3639 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003640 {return (*this)(__g, __p_);}
3641 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3642
3643 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647 result_type b() const {return __p_.b();}
3648
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003650 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003652 void param(const param_type& __p) {__p_ = __p;}
3653
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003655 result_type min() const {return a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657 result_type max() const {return b();}
3658
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003659 friend _LIBCPP_INLINE_VISIBILITY
3660 bool operator==(const uniform_real_distribution& __x,
3661 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003662 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003663 friend _LIBCPP_INLINE_VISIBILITY
3664 bool operator!=(const uniform_real_distribution& __x,
3665 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003666 {return !(__x == __y);}
3667};
3668
3669template<class _RealType>
3670template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003671inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003672typename uniform_real_distribution<_RealType>::result_type
3673uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3674{
3675 return (__p.b() - __p.a())
Howard Hinnant0949eed2011-06-30 21:18:19 +00003676 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003677 + __p.a();
3678}
3679
3680template <class _CharT, class _Traits, class _RT>
3681basic_ostream<_CharT, _Traits>&
3682operator<<(basic_ostream<_CharT, _Traits>& __os,
3683 const uniform_real_distribution<_RT>& __x)
3684{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003685 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003686 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3687 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003688 _CharT __sp = __os.widen(' ');
3689 __os.fill(__sp);
3690 return __os << __x.a() << __sp << __x.b();
3691}
3692
3693template <class _CharT, class _Traits, class _RT>
3694basic_istream<_CharT, _Traits>&
3695operator>>(basic_istream<_CharT, _Traits>& __is,
3696 uniform_real_distribution<_RT>& __x)
3697{
3698 typedef uniform_real_distribution<_RT> _Eng;
3699 typedef typename _Eng::result_type result_type;
3700 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003701 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003702 __is.flags(ios_base::dec | ios_base::skipws);
3703 result_type __a;
3704 result_type __b;
3705 __is >> __a >> __b;
3706 if (!__is.fail())
3707 __x.param(param_type(__a, __b));
3708 return __is;
3709}
3710
Howard Hinnant30a840f2010-05-12 17:08:57 +00003711// bernoulli_distribution
3712
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003713class _LIBCPP_VISIBLE bernoulli_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003714{
3715public:
3716 // types
3717 typedef bool result_type;
3718
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003719 class _LIBCPP_VISIBLE param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003720 {
3721 double __p_;
3722 public:
3723 typedef bernoulli_distribution distribution_type;
3724
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003726 explicit param_type(double __p = 0.5) : __p_(__p) {}
3727
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003729 double p() const {return __p_;}
3730
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003731 friend _LIBCPP_INLINE_VISIBILITY
3732 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003733 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003734 friend _LIBCPP_INLINE_VISIBILITY
3735 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003736 {return !(__x == __y);}
3737 };
3738
3739private:
3740 param_type __p_;
3741
3742public:
3743 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003745 explicit bernoulli_distribution(double __p = 0.5)
3746 : __p_(param_type(__p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003748 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003750 void reset() {}
3751
3752 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003753 template<class _URNG>
3754 _LIBCPP_INLINE_VISIBILITY
3755 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003756 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003757 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758
3759 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003761 double p() const {return __p_.p();}
3762
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003764 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766 void param(const param_type& __p) {__p_ = __p;}
3767
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003769 result_type min() const {return false;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003771 result_type max() const {return true;}
3772
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003773 friend _LIBCPP_INLINE_VISIBILITY
3774 bool operator==(const bernoulli_distribution& __x,
3775 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003776 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003777 friend _LIBCPP_INLINE_VISIBILITY
3778 bool operator!=(const bernoulli_distribution& __x,
3779 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003780 {return !(__x == __y);}
3781};
3782
Howard Hinnant03aad812010-05-11 23:26:59 +00003783template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003784inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003785bernoulli_distribution::result_type
3786bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3787{
Howard Hinnantd6d11712010-05-20 15:11:46 +00003788 uniform_real_distribution<double> __gen;
3789 return __gen(__g) < __p.p();
Howard Hinnant03aad812010-05-11 23:26:59 +00003790}
3791
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792template <class _CharT, class _Traits>
3793basic_ostream<_CharT, _Traits>&
3794operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3795{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003796 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003797 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3798 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003799 _CharT __sp = __os.widen(' ');
3800 __os.fill(__sp);
3801 return __os << __x.p();
3802}
3803
3804template <class _CharT, class _Traits>
3805basic_istream<_CharT, _Traits>&
3806operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3807{
3808 typedef bernoulli_distribution _Eng;
3809 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003810 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003811 __is.flags(ios_base::dec | ios_base::skipws);
3812 double __p;
3813 __is >> __p;
3814 if (!__is.fail())
3815 __x.param(param_type(__p));
3816 return __is;
3817}
3818
Howard Hinnant30a840f2010-05-12 17:08:57 +00003819// binomial_distribution
3820
Howard Hinnant03aad812010-05-11 23:26:59 +00003821template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003822class _LIBCPP_VISIBLE binomial_distribution
Howard Hinnant03aad812010-05-11 23:26:59 +00003823{
3824public:
3825 // types
3826 typedef _IntType result_type;
3827
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003828 class _LIBCPP_VISIBLE param_type
Howard Hinnant03aad812010-05-11 23:26:59 +00003829 {
3830 result_type __t_;
3831 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003832 double __pr_;
3833 double __odds_ratio_;
3834 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003835 public:
3836 typedef binomial_distribution distribution_type;
3837
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003838 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003839
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003841 result_type t() const {return __t_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003843 double p() const {return __p_;}
3844
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003845 friend _LIBCPP_INLINE_VISIBILITY
3846 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003847 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003848 friend _LIBCPP_INLINE_VISIBILITY
3849 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003850 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003851
3852 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003853 };
3854
3855private:
3856 param_type __p_;
3857
3858public:
3859 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003861 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3862 : __p_(param_type(__t, __p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003864 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003866 void reset() {}
3867
3868 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003869 template<class _URNG>
3870 _LIBCPP_INLINE_VISIBILITY
3871 result_type operator()(_URNG& __g)
Howard Hinnant03aad812010-05-11 23:26:59 +00003872 {return (*this)(__g, __p_);}
3873 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3874
3875 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003877 result_type t() const {return __p_.t();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003879 double p() const {return __p_.p();}
3880
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003882 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003884 void param(const param_type& __p) {__p_ = __p;}
3885
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003887 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003889 result_type max() const {return t();}
3890
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003891 friend _LIBCPP_INLINE_VISIBILITY
3892 bool operator==(const binomial_distribution& __x,
3893 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003894 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003895 friend _LIBCPP_INLINE_VISIBILITY
3896 bool operator!=(const binomial_distribution& __x,
3897 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003898 {return !(__x == __y);}
3899};
3900
3901template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003902binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3903 : __t_(__t), __p_(__p)
3904{
3905 if (0 < __p_ && __p_ < 1)
3906 {
3907 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003908 __pr_ = _VSTD::exp(_VSTD::lgamma(__t_ + 1.) - _VSTD::lgamma(__r0_ + 1.) -
3909 _VSTD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
3910 (__t_ - __r0_) * _VSTD::log(1 - __p_));
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003911 __odds_ratio_ = __p_ / (1 - __p_);
3912 }
3913}
3914
3915template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00003916template<class _URNG>
3917_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003918binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00003919{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003920 if (__pr.__t_ == 0 || __pr.__p_ == 0)
3921 return 0;
3922 if (__pr.__p_ == 1)
3923 return __pr.__t_;
3924 uniform_real_distribution<double> __gen;
3925 double __u = __gen(__g) - __pr.__pr_;
3926 if (__u < 0)
3927 return __pr.__r0_;
3928 double __pu = __pr.__pr_;
3929 double __pd = __pu;
3930 result_type __ru = __pr.__r0_;
3931 result_type __rd = __ru;
3932 while (true)
3933 {
3934 if (__rd >= 1)
3935 {
3936 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3937 __u -= __pd;
3938 if (__u < 0)
3939 return __rd - 1;
3940 }
3941 --__rd;
3942 ++__ru;
3943 if (__ru <= __pr.__t_)
3944 {
3945 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3946 __u -= __pu;
3947 if (__u < 0)
3948 return __ru;
3949 }
3950 }
Howard Hinnant03aad812010-05-11 23:26:59 +00003951}
3952
3953template <class _CharT, class _Traits, class _IntType>
3954basic_ostream<_CharT, _Traits>&
3955operator<<(basic_ostream<_CharT, _Traits>& __os,
3956 const binomial_distribution<_IntType>& __x)
3957{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003958 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003959 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3960 ios_base::scientific);
Howard Hinnant03aad812010-05-11 23:26:59 +00003961 _CharT __sp = __os.widen(' ');
3962 __os.fill(__sp);
3963 return __os << __x.t() << __sp << __x.p();
3964}
3965
3966template <class _CharT, class _Traits, class _IntType>
3967basic_istream<_CharT, _Traits>&
3968operator>>(basic_istream<_CharT, _Traits>& __is,
3969 binomial_distribution<_IntType>& __x)
3970{
3971 typedef binomial_distribution<_IntType> _Eng;
3972 typedef typename _Eng::result_type result_type;
3973 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003974 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant03aad812010-05-11 23:26:59 +00003975 __is.flags(ios_base::dec | ios_base::skipws);
3976 result_type __t;
3977 double __p;
3978 __is >> __t >> __p;
3979 if (!__is.fail())
3980 __x.param(param_type(__t, __p));
3981 return __is;
3982}
3983
Howard Hinnant30a840f2010-05-12 17:08:57 +00003984// exponential_distribution
3985
3986template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003987class _LIBCPP_VISIBLE exponential_distribution
Howard Hinnant30a840f2010-05-12 17:08:57 +00003988{
3989public:
3990 // types
3991 typedef _RealType result_type;
3992
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003993 class _LIBCPP_VISIBLE param_type
Howard Hinnant30a840f2010-05-12 17:08:57 +00003994 {
3995 result_type __lambda_;
3996 public:
3997 typedef exponential_distribution distribution_type;
3998
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003999 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004000 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
4001
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004003 result_type lambda() const {return __lambda_;}
4004
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004005 friend _LIBCPP_INLINE_VISIBILITY
4006 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004007 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004008 friend _LIBCPP_INLINE_VISIBILITY
4009 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004010 {return !(__x == __y);}
4011 };
4012
4013private:
4014 param_type __p_;
4015
4016public:
4017 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004019 explicit exponential_distribution(result_type __lambda = 1)
4020 : __p_(param_type(__lambda)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004022 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004024 void reset() {}
4025
4026 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004027 template<class _URNG>
4028 _LIBCPP_INLINE_VISIBILITY
4029 result_type operator()(_URNG& __g)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004030 {return (*this)(__g, __p_);}
4031 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4032
4033 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004035 result_type lambda() const {return __p_.lambda();}
4036
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004038 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004040 void param(const param_type& __p) {__p_ = __p;}
4041
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004043 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf40dc62010-05-16 17:56:20 +00004045 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00004046
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004047 friend _LIBCPP_INLINE_VISIBILITY
4048 bool operator==(const exponential_distribution& __x,
4049 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004050 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004051 friend _LIBCPP_INLINE_VISIBILITY
4052 bool operator!=(const exponential_distribution& __x,
4053 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004054 {return !(__x == __y);}
4055};
4056
4057template <class _RealType>
4058template<class _URNG>
4059_RealType
4060exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4061{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004062 return -_VSTD::log
Howard Hinnant30a840f2010-05-12 17:08:57 +00004063 (
4064 result_type(1) -
Howard Hinnant0949eed2011-06-30 21:18:19 +00004065 _VSTD::generate_canonical<result_type,
Howard Hinnant30a840f2010-05-12 17:08:57 +00004066 numeric_limits<result_type>::digits>(__g)
4067 )
4068 / __p.lambda();
4069}
4070
4071template <class _CharT, class _Traits, class _RealType>
4072basic_ostream<_CharT, _Traits>&
4073operator<<(basic_ostream<_CharT, _Traits>& __os,
4074 const exponential_distribution<_RealType>& __x)
4075{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004076 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004077 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4078 ios_base::scientific);
Howard Hinnant30a840f2010-05-12 17:08:57 +00004079 return __os << __x.lambda();
4080}
4081
4082template <class _CharT, class _Traits, class _RealType>
4083basic_istream<_CharT, _Traits>&
4084operator>>(basic_istream<_CharT, _Traits>& __is,
4085 exponential_distribution<_RealType>& __x)
4086{
4087 typedef exponential_distribution<_RealType> _Eng;
4088 typedef typename _Eng::result_type result_type;
4089 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004090 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant30a840f2010-05-12 17:08:57 +00004091 __is.flags(ios_base::dec | ios_base::skipws);
4092 result_type __lambda;
4093 __is >> __lambda;
4094 if (!__is.fail())
4095 __x.param(param_type(__lambda));
4096 return __is;
4097}
4098
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004099// normal_distribution
4100
4101template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004102class _LIBCPP_VISIBLE normal_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004103{
4104public:
4105 // types
4106 typedef _RealType result_type;
4107
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004108 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004109 {
4110 result_type __mean_;
4111 result_type __stddev_;
4112 public:
4113 typedef normal_distribution distribution_type;
4114
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004115 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004116 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4117 : __mean_(__mean), __stddev_(__stddev) {}
4118
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004120 result_type mean() const {return __mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004122 result_type stddev() const {return __stddev_;}
4123
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004124 friend _LIBCPP_INLINE_VISIBILITY
4125 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004126 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004127 friend _LIBCPP_INLINE_VISIBILITY
4128 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004129 {return !(__x == __y);}
4130 };
4131
4132private:
4133 param_type __p_;
4134 result_type _V_;
4135 bool _V_hot_;
4136
4137public:
4138 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004140 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4141 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004143 explicit normal_distribution(const param_type& __p)
4144 : __p_(__p), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004146 void reset() {_V_hot_ = false;}
4147
4148 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004149 template<class _URNG>
4150 _LIBCPP_INLINE_VISIBILITY
4151 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004152 {return (*this)(__g, __p_);}
4153 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4154
4155 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004157 result_type mean() const {return __p_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004159 result_type stddev() const {return __p_.stddev();}
4160
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004162 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004164 void param(const param_type& __p) {__p_ = __p;}
4165
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004167 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004169 result_type max() const {return numeric_limits<result_type>::infinity();}
4170
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004171 friend _LIBCPP_INLINE_VISIBILITY
4172 bool operator==(const normal_distribution& __x,
4173 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004174 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4175 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004176 friend _LIBCPP_INLINE_VISIBILITY
4177 bool operator!=(const normal_distribution& __x,
4178 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004179 {return !(__x == __y);}
4180
4181 template <class _CharT, class _Traits, class _RT>
4182 friend
4183 basic_ostream<_CharT, _Traits>&
4184 operator<<(basic_ostream<_CharT, _Traits>& __os,
4185 const normal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004186
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004187 template <class _CharT, class _Traits, class _RT>
4188 friend
4189 basic_istream<_CharT, _Traits>&
4190 operator>>(basic_istream<_CharT, _Traits>& __is,
4191 normal_distribution<_RT>& __x);
4192};
4193
4194template <class _RealType>
4195template<class _URNG>
4196_RealType
4197normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4198{
Howard Hinnant99968442011-11-29 18:15:50 +00004199 result_type _Up;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004200 if (_V_hot_)
4201 {
4202 _V_hot_ = false;
Howard Hinnant99968442011-11-29 18:15:50 +00004203 _Up = _V_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004204 }
4205 else
4206 {
4207 uniform_real_distribution<result_type> _Uni(-1, 1);
4208 result_type __u;
4209 result_type __v;
4210 result_type __s;
4211 do
4212 {
4213 __u = _Uni(__g);
4214 __v = _Uni(__g);
4215 __s = __u * __u + __v * __v;
4216 } while (__s > 1 || __s == 0);
Howard Hinnant99968442011-11-29 18:15:50 +00004217 result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
4218 _V_ = __v * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004219 _V_hot_ = true;
Howard Hinnant99968442011-11-29 18:15:50 +00004220 _Up = __u * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004221 }
Howard Hinnant99968442011-11-29 18:15:50 +00004222 return _Up * __p.stddev() + __p.mean();
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004223}
4224
4225template <class _CharT, class _Traits, class _RT>
4226basic_ostream<_CharT, _Traits>&
4227operator<<(basic_ostream<_CharT, _Traits>& __os,
4228 const normal_distribution<_RT>& __x)
4229{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004230 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004231 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4232 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004233 _CharT __sp = __os.widen(' ');
4234 __os.fill(__sp);
4235 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4236 if (__x._V_hot_)
4237 __os << __sp << __x._V_;
4238 return __os;
4239}
4240
4241template <class _CharT, class _Traits, class _RT>
4242basic_istream<_CharT, _Traits>&
4243operator>>(basic_istream<_CharT, _Traits>& __is,
4244 normal_distribution<_RT>& __x)
4245{
4246 typedef normal_distribution<_RT> _Eng;
4247 typedef typename _Eng::result_type result_type;
4248 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004249 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004250 __is.flags(ios_base::dec | ios_base::skipws);
4251 result_type __mean;
4252 result_type __stddev;
Howard Hinnant99968442011-11-29 18:15:50 +00004253 result_type _Vp = 0;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004254 bool _V_hot = false;
4255 __is >> __mean >> __stddev >> _V_hot;
4256 if (_V_hot)
Howard Hinnant99968442011-11-29 18:15:50 +00004257 __is >> _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004258 if (!__is.fail())
4259 {
4260 __x.param(param_type(__mean, __stddev));
4261 __x._V_hot_ = _V_hot;
Howard Hinnant99968442011-11-29 18:15:50 +00004262 __x._V_ = _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004263 }
4264 return __is;
4265}
4266
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004267// lognormal_distribution
4268
4269template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004270class _LIBCPP_VISIBLE lognormal_distribution
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004271{
4272public:
4273 // types
4274 typedef _RealType result_type;
4275
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004276 class _LIBCPP_VISIBLE param_type
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004277 {
4278 normal_distribution<result_type> __nd_;
4279 public:
4280 typedef lognormal_distribution distribution_type;
4281
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004283 explicit param_type(result_type __m = 0, result_type __s = 1)
4284 : __nd_(__m, __s) {}
4285
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004287 result_type m() const {return __nd_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004289 result_type s() const {return __nd_.stddev();}
4290
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004291 friend _LIBCPP_INLINE_VISIBILITY
4292 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004293 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004294 friend _LIBCPP_INLINE_VISIBILITY
4295 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004296 {return !(__x == __y);}
4297 friend class lognormal_distribution;
4298
4299 template <class _CharT, class _Traits, class _RT>
4300 friend
4301 basic_ostream<_CharT, _Traits>&
4302 operator<<(basic_ostream<_CharT, _Traits>& __os,
4303 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004304
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004305 template <class _CharT, class _Traits, class _RT>
4306 friend
4307 basic_istream<_CharT, _Traits>&
4308 operator>>(basic_istream<_CharT, _Traits>& __is,
4309 lognormal_distribution<_RT>& __x);
4310 };
4311
4312private:
4313 param_type __p_;
4314
4315public:
4316 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004318 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4319 : __p_(param_type(__m, __s)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004321 explicit lognormal_distribution(const param_type& __p)
4322 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004324 void reset() {__p_.__nd_.reset();}
4325
4326 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004327 template<class _URNG>
4328 _LIBCPP_INLINE_VISIBILITY
4329 result_type operator()(_URNG& __g)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004330 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004331 template<class _URNG>
4332 _LIBCPP_INLINE_VISIBILITY
4333 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004334 {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004335
4336 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004338 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004340 result_type s() const {return __p_.s();}
4341
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004343 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00004345 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004346
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004348 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004350 result_type max() const {return numeric_limits<result_type>::infinity();}
4351
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004352 friend _LIBCPP_INLINE_VISIBILITY
4353 bool operator==(const lognormal_distribution& __x,
4354 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004355 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004356 friend _LIBCPP_INLINE_VISIBILITY
4357 bool operator!=(const lognormal_distribution& __x,
4358 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004359 {return !(__x == __y);}
4360
4361 template <class _CharT, class _Traits, class _RT>
4362 friend
4363 basic_ostream<_CharT, _Traits>&
4364 operator<<(basic_ostream<_CharT, _Traits>& __os,
4365 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004366
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004367 template <class _CharT, class _Traits, class _RT>
4368 friend
4369 basic_istream<_CharT, _Traits>&
4370 operator>>(basic_istream<_CharT, _Traits>& __is,
4371 lognormal_distribution<_RT>& __x);
4372};
4373
4374template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004375inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004376basic_ostream<_CharT, _Traits>&
4377operator<<(basic_ostream<_CharT, _Traits>& __os,
4378 const lognormal_distribution<_RT>& __x)
4379{
4380 return __os << __x.__p_.__nd_;
4381}
4382
4383template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004384inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004385basic_istream<_CharT, _Traits>&
4386operator>>(basic_istream<_CharT, _Traits>& __is,
4387 lognormal_distribution<_RT>& __x)
4388{
4389 return __is >> __x.__p_.__nd_;
4390}
4391
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004392// poisson_distribution
4393
4394template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004395class _LIBCPP_VISIBLE poisson_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004396{
4397public:
4398 // types
4399 typedef _IntType result_type;
4400
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004401 class _LIBCPP_VISIBLE param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004402 {
4403 double __mean_;
4404 double __s_;
4405 double __d_;
4406 double __l_;
4407 double __omega_;
4408 double __c0_;
4409 double __c1_;
4410 double __c2_;
4411 double __c3_;
4412 double __c_;
4413
4414 public:
4415 typedef poisson_distribution distribution_type;
4416
4417 explicit param_type(double __mean = 1.0);
4418
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004420 double mean() const {return __mean_;}
4421
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004422 friend _LIBCPP_INLINE_VISIBILITY
4423 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004424 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004425 friend _LIBCPP_INLINE_VISIBILITY
4426 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004427 {return !(__x == __y);}
4428
4429 friend class poisson_distribution;
4430 };
4431
4432private:
4433 param_type __p_;
4434
4435public:
4436 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004438 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004440 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004442 void reset() {}
4443
4444 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004445 template<class _URNG>
4446 _LIBCPP_INLINE_VISIBILITY
4447 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004448 {return (*this)(__g, __p_);}
4449 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4450
4451 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004453 double mean() const {return __p_.mean();}
4454
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004456 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004458 void param(const param_type& __p) {__p_ = __p;}
4459
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004461 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004462 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004463 result_type max() const {return numeric_limits<result_type>::max();}
4464
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004465 friend _LIBCPP_INLINE_VISIBILITY
4466 bool operator==(const poisson_distribution& __x,
4467 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004468 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004469 friend _LIBCPP_INLINE_VISIBILITY
4470 bool operator!=(const poisson_distribution& __x,
4471 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004472 {return !(__x == __y);}
4473};
4474
4475template<class _IntType>
4476poisson_distribution<_IntType>::param_type::param_type(double __mean)
4477 : __mean_(__mean)
4478{
4479 if (__mean_ < 10)
4480 {
4481 __s_ = 0;
4482 __d_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004483 __l_ = _VSTD::exp(-__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004484 __omega_ = 0;
4485 __c3_ = 0;
4486 __c2_ = 0;
4487 __c1_ = 0;
4488 __c0_ = 0;
4489 __c_ = 0;
4490 }
4491 else
4492 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004493 __s_ = _VSTD::sqrt(__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004494 __d_ = 6 * __mean_ * __mean_;
4495 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4496 __omega_ = .3989423 / __s_;
4497 double __b1_ = .4166667E-1 / __mean_;
4498 double __b2_ = .3 * __b1_ * __b1_;
4499 __c3_ = .1428571 * __b1_ * __b2_;
4500 __c2_ = __b2_ - 15. * __c3_;
4501 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4502 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4503 __c_ = .1069 / __mean_;
4504 }
4505}
4506
4507template <class _IntType>
4508template<class _URNG>
4509_IntType
4510poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4511{
4512 result_type __x;
4513 uniform_real_distribution<double> __urd;
Howard Hinnant75f76952011-04-14 15:59:22 +00004514 if (__pr.__mean_ < 10)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004515 {
4516 __x = 0;
4517 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4518 __p *= __urd(__urng);
4519 }
4520 else
4521 {
4522 double __difmuk;
4523 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4524 double __u;
4525 if (__g > 0)
4526 {
4527 __x = static_cast<result_type>(__g);
4528 if (__x >= __pr.__l_)
4529 return __x;
4530 __difmuk = __pr.__mean_ - __x;
4531 __u = __urd(__urng);
4532 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4533 return __x;
4534 }
4535 exponential_distribution<double> __edist;
4536 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4537 {
4538 double __e;
4539 if (__using_exp_dist || __g < 0)
4540 {
4541 double __t;
4542 do
4543 {
4544 __e = __edist(__urng);
4545 __u = __urd(__urng);
4546 __u += __u - 1;
4547 __t = 1.8 + (__u < 0 ? -__e : __e);
4548 } while (__t <= -.6744);
4549 __x = __pr.__mean_ + __pr.__s_ * __t;
4550 __difmuk = __pr.__mean_ - __x;
4551 __using_exp_dist = true;
4552 }
4553 double __px;
4554 double __py;
4555 if (__x < 10)
4556 {
4557 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4558 40320, 362880};
4559 __px = -__pr.__mean_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004560 __py = _VSTD::pow(__pr.__mean_, (double)__x) / __fac[__x];
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004561 }
4562 else
4563 {
4564 double __del = .8333333E-1 / __x;
4565 __del -= 4.8 * __del * __del * __del;
4566 double __v = __difmuk / __x;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004567 if (_VSTD::abs(__v) > 0.25)
4568 __px = __x * _VSTD::log(1 + __v) - __difmuk - __del;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004569 else
4570 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4571 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4572 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004573 __py = .3989423 / _VSTD::sqrt(__x);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004574 }
4575 double __r = (0.5 - __difmuk) / __pr.__s_;
4576 double __r2 = __r * __r;
4577 double __fx = -0.5 * __r2;
4578 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4579 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4580 if (__using_exp_dist)
4581 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004582 if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4583 __fy * _VSTD::exp(__fx + __e))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004584 break;
4585 }
4586 else
4587 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004588 if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004589 break;
4590 }
4591 }
4592 }
4593 return __x;
4594}
4595
4596template <class _CharT, class _Traits, class _IntType>
4597basic_ostream<_CharT, _Traits>&
4598operator<<(basic_ostream<_CharT, _Traits>& __os,
4599 const poisson_distribution<_IntType>& __x)
4600{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004601 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004602 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4603 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004604 return __os << __x.mean();
4605}
4606
4607template <class _CharT, class _Traits, class _IntType>
4608basic_istream<_CharT, _Traits>&
4609operator>>(basic_istream<_CharT, _Traits>& __is,
4610 poisson_distribution<_IntType>& __x)
4611{
4612 typedef poisson_distribution<_IntType> _Eng;
4613 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004614 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004615 __is.flags(ios_base::dec | ios_base::skipws);
4616 double __mean;
4617 __is >> __mean;
4618 if (!__is.fail())
4619 __x.param(param_type(__mean));
4620 return __is;
4621}
4622
Howard Hinnant9de6e302010-05-16 01:09:02 +00004623// weibull_distribution
4624
4625template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004626class _LIBCPP_VISIBLE weibull_distribution
Howard Hinnant9de6e302010-05-16 01:09:02 +00004627{
4628public:
4629 // types
4630 typedef _RealType result_type;
4631
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004632 class _LIBCPP_VISIBLE param_type
Howard Hinnant9de6e302010-05-16 01:09:02 +00004633 {
4634 result_type __a_;
4635 result_type __b_;
4636 public:
4637 typedef weibull_distribution distribution_type;
4638
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004640 explicit param_type(result_type __a = 1, result_type __b = 1)
4641 : __a_(__a), __b_(__b) {}
4642
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004644 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004646 result_type b() const {return __b_;}
4647
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004648 friend _LIBCPP_INLINE_VISIBILITY
4649 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004650 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004651 friend _LIBCPP_INLINE_VISIBILITY
4652 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004653 {return !(__x == __y);}
4654 };
4655
4656private:
4657 param_type __p_;
4658
4659public:
4660 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004662 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4663 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004665 explicit weibull_distribution(const param_type& __p)
4666 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004668 void reset() {}
4669
4670 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004671 template<class _URNG>
4672 _LIBCPP_INLINE_VISIBILITY
4673 result_type operator()(_URNG& __g)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004674 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004675 template<class _URNG>
4676 _LIBCPP_INLINE_VISIBILITY
4677 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004678 {return __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004679 _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
Howard Hinnant9de6e302010-05-16 01:09:02 +00004680
4681 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004683 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004685 result_type b() const {return __p_.b();}
4686
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004688 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004690 void param(const param_type& __p) {__p_ = __p;}
4691
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004693 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004695 result_type max() const {return numeric_limits<result_type>::infinity();}
4696
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004697 friend _LIBCPP_INLINE_VISIBILITY
4698 bool operator==(const weibull_distribution& __x,
4699 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004700 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004701 friend _LIBCPP_INLINE_VISIBILITY
4702 bool operator!=(const weibull_distribution& __x,
4703 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004704 {return !(__x == __y);}
4705};
4706
4707template <class _CharT, class _Traits, class _RT>
4708basic_ostream<_CharT, _Traits>&
4709operator<<(basic_ostream<_CharT, _Traits>& __os,
4710 const weibull_distribution<_RT>& __x)
4711{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004712 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004713 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4714 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004715 _CharT __sp = __os.widen(' ');
4716 __os.fill(__sp);
4717 __os << __x.a() << __sp << __x.b();
4718 return __os;
4719}
4720
4721template <class _CharT, class _Traits, class _RT>
4722basic_istream<_CharT, _Traits>&
4723operator>>(basic_istream<_CharT, _Traits>& __is,
4724 weibull_distribution<_RT>& __x)
4725{
4726 typedef weibull_distribution<_RT> _Eng;
4727 typedef typename _Eng::result_type result_type;
4728 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004729 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004730 __is.flags(ios_base::dec | ios_base::skipws);
4731 result_type __a;
4732 result_type __b;
4733 __is >> __a >> __b;
4734 if (!__is.fail())
4735 __x.param(param_type(__a, __b));
4736 return __is;
4737}
4738
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004739template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004740class _LIBCPP_VISIBLE extreme_value_distribution
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004741{
4742public:
4743 // types
4744 typedef _RealType result_type;
4745
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004746 class _LIBCPP_VISIBLE param_type
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004747 {
4748 result_type __a_;
4749 result_type __b_;
4750 public:
4751 typedef extreme_value_distribution distribution_type;
4752
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004754 explicit param_type(result_type __a = 0, result_type __b = 1)
4755 : __a_(__a), __b_(__b) {}
4756
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004758 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004760 result_type b() const {return __b_;}
4761
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004762 friend _LIBCPP_INLINE_VISIBILITY
4763 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004764 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004765 friend _LIBCPP_INLINE_VISIBILITY
4766 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004767 {return !(__x == __y);}
4768 };
4769
4770private:
4771 param_type __p_;
4772
4773public:
4774 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004776 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4777 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004779 explicit extreme_value_distribution(const param_type& __p)
4780 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004782 void reset() {}
4783
4784 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004785 template<class _URNG>
4786 _LIBCPP_INLINE_VISIBILITY
4787 result_type operator()(_URNG& __g)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004788 {return (*this)(__g, __p_);}
4789 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4790
4791 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004793 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004795 result_type b() const {return __p_.b();}
4796
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004798 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004800 void param(const param_type& __p) {__p_ = __p;}
4801
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004803 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004805 result_type max() const {return numeric_limits<result_type>::infinity();}
4806
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004807 friend _LIBCPP_INLINE_VISIBILITY
4808 bool operator==(const extreme_value_distribution& __x,
4809 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004810 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004811 friend _LIBCPP_INLINE_VISIBILITY
4812 bool operator!=(const extreme_value_distribution& __x,
4813 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004814 {return !(__x == __y);}
4815};
4816
4817template<class _RealType>
4818template<class _URNG>
4819_RealType
4820extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4821{
4822 return __p.a() - __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004823 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004824}
4825
4826template <class _CharT, class _Traits, class _RT>
4827basic_ostream<_CharT, _Traits>&
4828operator<<(basic_ostream<_CharT, _Traits>& __os,
4829 const extreme_value_distribution<_RT>& __x)
4830{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004831 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004832 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4833 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004834 _CharT __sp = __os.widen(' ');
4835 __os.fill(__sp);
4836 __os << __x.a() << __sp << __x.b();
4837 return __os;
4838}
4839
4840template <class _CharT, class _Traits, class _RT>
4841basic_istream<_CharT, _Traits>&
4842operator>>(basic_istream<_CharT, _Traits>& __is,
4843 extreme_value_distribution<_RT>& __x)
4844{
4845 typedef extreme_value_distribution<_RT> _Eng;
4846 typedef typename _Eng::result_type result_type;
4847 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004848 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004849 __is.flags(ios_base::dec | ios_base::skipws);
4850 result_type __a;
4851 result_type __b;
4852 __is >> __a >> __b;
4853 if (!__is.fail())
4854 __x.param(param_type(__a, __b));
4855 return __is;
4856}
4857
Howard Hinnantc7c49132010-05-13 17:58:28 +00004858// gamma_distribution
4859
4860template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004861class _LIBCPP_VISIBLE gamma_distribution
Howard Hinnantc7c49132010-05-13 17:58:28 +00004862{
4863public:
4864 // types
4865 typedef _RealType result_type;
4866
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004867 class _LIBCPP_VISIBLE param_type
Howard Hinnantc7c49132010-05-13 17:58:28 +00004868 {
4869 result_type __alpha_;
4870 result_type __beta_;
4871 public:
4872 typedef gamma_distribution distribution_type;
4873
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004875 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4876 : __alpha_(__alpha), __beta_(__beta) {}
4877
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004879 result_type alpha() const {return __alpha_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004881 result_type beta() const {return __beta_;}
4882
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004883 friend _LIBCPP_INLINE_VISIBILITY
4884 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004885 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004886 friend _LIBCPP_INLINE_VISIBILITY
4887 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004888 {return !(__x == __y);}
4889 };
4890
4891private:
4892 param_type __p_;
4893
4894public:
4895 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004897 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4898 : __p_(param_type(__alpha, __beta)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004900 explicit gamma_distribution(const param_type& __p)
4901 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004903 void reset() {}
4904
4905 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004906 template<class _URNG>
4907 _LIBCPP_INLINE_VISIBILITY
4908 result_type operator()(_URNG& __g)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004909 {return (*this)(__g, __p_);}
4910 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4911
4912 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004914 result_type alpha() const {return __p_.alpha();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004916 result_type beta() const {return __p_.beta();}
4917
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004919 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004921 void param(const param_type& __p) {__p_ = __p;}
4922
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004924 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004926 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantc7c49132010-05-13 17:58:28 +00004927
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004928 friend _LIBCPP_INLINE_VISIBILITY
4929 bool operator==(const gamma_distribution& __x,
4930 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004931 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004932 friend _LIBCPP_INLINE_VISIBILITY
4933 bool operator!=(const gamma_distribution& __x,
4934 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004935 {return !(__x == __y);}
4936};
4937
4938template <class _RealType>
4939template<class _URNG>
4940_RealType
4941gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4942{
Howard Hinnantf417abe2010-05-14 18:43:10 +00004943 result_type __a = __p.alpha();
4944 uniform_real_distribution<result_type> __gen(0, 1);
4945 exponential_distribution<result_type> __egen;
4946 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00004947 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00004948 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004949 else if (__a > 1)
4950 {
4951 const result_type __b = __a - 1;
4952 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004953 while (true)
4954 {
4955 const result_type __u = __gen(__g);
4956 const result_type __v = __gen(__g);
4957 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004958 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004959 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004960 const result_type __y = _VSTD::sqrt(__c / __w) *
Howard Hinnantc7c49132010-05-13 17:58:28 +00004961 (__u - result_type(0.5));
4962 __x = __b + __y;
4963 if (__x >= 0)
4964 {
4965 const result_type __z = 64 * __w * __w * __w * __v * __v;
4966 if (__z <= 1 - 2 * __y * __y / __x)
4967 break;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004968 if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
Howard Hinnantc7c49132010-05-13 17:58:28 +00004969 break;
4970 }
4971 }
4972 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00004973 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00004974 else // __a < 1
4975 {
4976 while (true)
4977 {
4978 const result_type __u = __gen(__g);
4979 const result_type __es = __egen(__g);
4980 if (__u <= 1 - __a)
4981 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004982 __x = _VSTD::pow(__u, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004983 if (__x <= __es)
4984 break;
4985 }
4986 else
4987 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004988 const result_type __e = -_VSTD::log((1-__u)/__a);
4989 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004990 if (__x <= __e + __es)
4991 break;
4992 }
4993 }
4994 }
4995 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00004996}
4997
4998template <class _CharT, class _Traits, class _RT>
4999basic_ostream<_CharT, _Traits>&
5000operator<<(basic_ostream<_CharT, _Traits>& __os,
5001 const gamma_distribution<_RT>& __x)
5002{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005003 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005004 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5005 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:28 +00005006 _CharT __sp = __os.widen(' ');
5007 __os.fill(__sp);
5008 __os << __x.alpha() << __sp << __x.beta();
5009 return __os;
5010}
5011
5012template <class _CharT, class _Traits, class _RT>
5013basic_istream<_CharT, _Traits>&
5014operator>>(basic_istream<_CharT, _Traits>& __is,
5015 gamma_distribution<_RT>& __x)
5016{
5017 typedef gamma_distribution<_RT> _Eng;
5018 typedef typename _Eng::result_type result_type;
5019 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005020 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc7c49132010-05-13 17:58:28 +00005021 __is.flags(ios_base::dec | ios_base::skipws);
5022 result_type __alpha;
5023 result_type __beta;
5024 __is >> __alpha >> __beta;
5025 if (!__is.fail())
5026 __x.param(param_type(__alpha, __beta));
5027 return __is;
5028}
Howard Hinnanta64111c2010-05-12 21:02:31 +00005029
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005030// negative_binomial_distribution
5031
5032template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005033class _LIBCPP_VISIBLE negative_binomial_distribution
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005034{
5035public:
5036 // types
5037 typedef _IntType result_type;
5038
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005039 class _LIBCPP_VISIBLE param_type
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005040 {
5041 result_type __k_;
5042 double __p_;
5043 public:
5044 typedef negative_binomial_distribution distribution_type;
5045
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005047 explicit param_type(result_type __k = 1, double __p = 0.5)
5048 : __k_(__k), __p_(__p) {}
5049
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005051 result_type k() const {return __k_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005053 double p() const {return __p_;}
5054
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005055 friend _LIBCPP_INLINE_VISIBILITY
5056 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005057 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005058 friend _LIBCPP_INLINE_VISIBILITY
5059 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005060 {return !(__x == __y);}
5061 };
5062
5063private:
5064 param_type __p_;
5065
5066public:
5067 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005069 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
5070 : __p_(__k, __p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005072 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005074 void reset() {}
5075
5076 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005077 template<class _URNG>
5078 _LIBCPP_INLINE_VISIBILITY
5079 result_type operator()(_URNG& __g)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005080 {return (*this)(__g, __p_);}
5081 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5082
5083 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005084 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005085 result_type k() const {return __p_.k();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005087 double p() const {return __p_.p();}
5088
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005090 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005091 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005092 void param(const param_type& __p) {__p_ = __p;}
5093
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005095 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005097 result_type max() const {return numeric_limits<result_type>::max();}
5098
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005099 friend _LIBCPP_INLINE_VISIBILITY
5100 bool operator==(const negative_binomial_distribution& __x,
5101 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005102 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005103 friend _LIBCPP_INLINE_VISIBILITY
5104 bool operator!=(const negative_binomial_distribution& __x,
5105 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005106 {return !(__x == __y);}
5107};
5108
5109template <class _IntType>
5110template<class _URNG>
5111_IntType
5112negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5113{
5114 result_type __k = __pr.k();
5115 double __p = __pr.p();
5116 if (__k <= 21 * __p)
5117 {
5118 bernoulli_distribution __gen(__p);
5119 result_type __f = 0;
5120 result_type __s = 0;
5121 while (__s < __k)
5122 {
5123 if (__gen(__urng))
5124 ++__s;
5125 else
5126 ++__f;
5127 }
5128 return __f;
5129 }
5130 return poisson_distribution<result_type>(gamma_distribution<double>
5131 (__k, (1-__p)/__p)(__urng))(__urng);
5132}
5133
5134template <class _CharT, class _Traits, class _IntType>
5135basic_ostream<_CharT, _Traits>&
5136operator<<(basic_ostream<_CharT, _Traits>& __os,
5137 const negative_binomial_distribution<_IntType>& __x)
5138{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005139 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005140 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5141 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005142 _CharT __sp = __os.widen(' ');
5143 __os.fill(__sp);
5144 return __os << __x.k() << __sp << __x.p();
5145}
5146
5147template <class _CharT, class _Traits, class _IntType>
5148basic_istream<_CharT, _Traits>&
5149operator>>(basic_istream<_CharT, _Traits>& __is,
5150 negative_binomial_distribution<_IntType>& __x)
5151{
5152 typedef negative_binomial_distribution<_IntType> _Eng;
5153 typedef typename _Eng::result_type result_type;
5154 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005155 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005156 __is.flags(ios_base::dec | ios_base::skipws);
5157 result_type __k;
5158 double __p;
5159 __is >> __k >> __p;
5160 if (!__is.fail())
5161 __x.param(param_type(__k, __p));
5162 return __is;
5163}
5164
Howard Hinnant34e8a572010-05-17 13:44:27 +00005165// geometric_distribution
5166
5167template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005168class _LIBCPP_VISIBLE geometric_distribution
Howard Hinnant34e8a572010-05-17 13:44:27 +00005169{
5170public:
5171 // types
5172 typedef _IntType result_type;
5173
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005174 class _LIBCPP_VISIBLE param_type
Howard Hinnant34e8a572010-05-17 13:44:27 +00005175 {
5176 double __p_;
5177 public:
5178 typedef geometric_distribution distribution_type;
5179
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005181 explicit param_type(double __p = 0.5) : __p_(__p) {}
5182
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005184 double p() const {return __p_;}
5185
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005186 friend _LIBCPP_INLINE_VISIBILITY
5187 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005188 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005189 friend _LIBCPP_INLINE_VISIBILITY
5190 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005191 {return !(__x == __y);}
5192 };
5193
5194private:
5195 param_type __p_;
5196
5197public:
5198 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005200 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005202 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005204 void reset() {}
5205
5206 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005207 template<class _URNG>
5208 _LIBCPP_INLINE_VISIBILITY
5209 result_type operator()(_URNG& __g)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005210 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005211 template<class _URNG>
5212 _LIBCPP_INLINE_VISIBILITY
5213 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005214 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5215
5216 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005218 double p() const {return __p_.p();}
5219
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005221 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005223 void param(const param_type& __p) {__p_ = __p;}
5224
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005226 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005228 result_type max() const {return numeric_limits<result_type>::max();}
5229
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005230 friend _LIBCPP_INLINE_VISIBILITY
5231 bool operator==(const geometric_distribution& __x,
5232 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005233 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005234 friend _LIBCPP_INLINE_VISIBILITY
5235 bool operator!=(const geometric_distribution& __x,
5236 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005237 {return !(__x == __y);}
5238};
5239
5240template <class _CharT, class _Traits, class _IntType>
5241basic_ostream<_CharT, _Traits>&
5242operator<<(basic_ostream<_CharT, _Traits>& __os,
5243 const geometric_distribution<_IntType>& __x)
5244{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005245 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005246 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5247 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005248 return __os << __x.p();
5249}
5250
5251template <class _CharT, class _Traits, class _IntType>
5252basic_istream<_CharT, _Traits>&
5253operator>>(basic_istream<_CharT, _Traits>& __is,
5254 geometric_distribution<_IntType>& __x)
5255{
5256 typedef geometric_distribution<_IntType> _Eng;
5257 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005258 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005259 __is.flags(ios_base::dec | ios_base::skipws);
5260 double __p;
5261 __is >> __p;
5262 if (!__is.fail())
5263 __x.param(param_type(__p));
5264 return __is;
5265}
5266
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005267// chi_squared_distribution
5268
5269template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005270class _LIBCPP_VISIBLE chi_squared_distribution
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005271{
5272public:
5273 // types
5274 typedef _RealType result_type;
5275
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005276 class _LIBCPP_VISIBLE param_type
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005277 {
5278 result_type __n_;
5279 public:
5280 typedef chi_squared_distribution distribution_type;
5281
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005283 explicit param_type(result_type __n = 1) : __n_(__n) {}
5284
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005286 result_type n() const {return __n_;}
5287
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005288 friend _LIBCPP_INLINE_VISIBILITY
5289 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005290 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005291 friend _LIBCPP_INLINE_VISIBILITY
5292 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005293 {return !(__x == __y);}
5294 };
5295
5296private:
5297 param_type __p_;
5298
5299public:
5300 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005302 explicit chi_squared_distribution(result_type __n = 1)
5303 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005305 explicit chi_squared_distribution(const param_type& __p)
5306 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005308 void reset() {}
5309
5310 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005311 template<class _URNG>
5312 _LIBCPP_INLINE_VISIBILITY
5313 result_type operator()(_URNG& __g)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005314 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005315 template<class _URNG>
5316 _LIBCPP_INLINE_VISIBILITY
5317 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005318 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5319
5320 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005322 result_type n() const {return __p_.n();}
5323
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005325 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005327 void param(const param_type& __p) {__p_ = __p;}
5328
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005330 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005332 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005333
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005334 friend _LIBCPP_INLINE_VISIBILITY
5335 bool operator==(const chi_squared_distribution& __x,
5336 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005337 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005338 friend _LIBCPP_INLINE_VISIBILITY
5339 bool operator!=(const chi_squared_distribution& __x,
5340 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005341 {return !(__x == __y);}
5342};
5343
5344template <class _CharT, class _Traits, class _RT>
5345basic_ostream<_CharT, _Traits>&
5346operator<<(basic_ostream<_CharT, _Traits>& __os,
5347 const chi_squared_distribution<_RT>& __x)
5348{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005349 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005350 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5351 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005352 __os << __x.n();
5353 return __os;
5354}
5355
5356template <class _CharT, class _Traits, class _RT>
5357basic_istream<_CharT, _Traits>&
5358operator>>(basic_istream<_CharT, _Traits>& __is,
5359 chi_squared_distribution<_RT>& __x)
5360{
5361 typedef chi_squared_distribution<_RT> _Eng;
5362 typedef typename _Eng::result_type result_type;
5363 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005364 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005365 __is.flags(ios_base::dec | ios_base::skipws);
5366 result_type __n;
5367 __is >> __n;
5368 if (!__is.fail())
5369 __x.param(param_type(__n));
5370 return __is;
5371}
5372
Howard Hinnantd7d01132010-05-17 21:55:46 +00005373// cauchy_distribution
5374
5375template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005376class _LIBCPP_VISIBLE cauchy_distribution
Howard Hinnantd7d01132010-05-17 21:55:46 +00005377{
5378public:
5379 // types
5380 typedef _RealType result_type;
5381
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005382 class _LIBCPP_VISIBLE param_type
Howard Hinnantd7d01132010-05-17 21:55:46 +00005383 {
5384 result_type __a_;
5385 result_type __b_;
5386 public:
5387 typedef cauchy_distribution distribution_type;
5388
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005390 explicit param_type(result_type __a = 0, result_type __b = 1)
5391 : __a_(__a), __b_(__b) {}
5392
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005394 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005396 result_type b() const {return __b_;}
5397
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005398 friend _LIBCPP_INLINE_VISIBILITY
5399 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005400 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005401 friend _LIBCPP_INLINE_VISIBILITY
5402 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005403 {return !(__x == __y);}
5404 };
5405
5406private:
5407 param_type __p_;
5408
5409public:
5410 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005412 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5413 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005415 explicit cauchy_distribution(const param_type& __p)
5416 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005418 void reset() {}
5419
5420 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005421 template<class _URNG>
5422 _LIBCPP_INLINE_VISIBILITY
5423 result_type operator()(_URNG& __g)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005424 {return (*this)(__g, __p_);}
5425 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5426
5427 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005429 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005431 result_type b() const {return __p_.b();}
5432
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005434 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005436 void param(const param_type& __p) {__p_ = __p;}
5437
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005439 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005441 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005442
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005443 friend _LIBCPP_INLINE_VISIBILITY
5444 bool operator==(const cauchy_distribution& __x,
5445 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005446 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005447 friend _LIBCPP_INLINE_VISIBILITY
5448 bool operator!=(const cauchy_distribution& __x,
5449 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005450 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005451};
5452
5453template <class _RealType>
5454template<class _URNG>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005455inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005456_RealType
5457cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5458{
5459 uniform_real_distribution<result_type> __gen;
5460 // 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 +00005461 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
Howard Hinnantd7d01132010-05-17 21:55:46 +00005462}
5463
5464template <class _CharT, class _Traits, class _RT>
5465basic_ostream<_CharT, _Traits>&
5466operator<<(basic_ostream<_CharT, _Traits>& __os,
5467 const cauchy_distribution<_RT>& __x)
5468{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005469 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005470 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5471 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005472 _CharT __sp = __os.widen(' ');
5473 __os.fill(__sp);
5474 __os << __x.a() << __sp << __x.b();
5475 return __os;
5476}
5477
5478template <class _CharT, class _Traits, class _RT>
5479basic_istream<_CharT, _Traits>&
5480operator>>(basic_istream<_CharT, _Traits>& __is,
5481 cauchy_distribution<_RT>& __x)
5482{
5483 typedef cauchy_distribution<_RT> _Eng;
5484 typedef typename _Eng::result_type result_type;
5485 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005486 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005487 __is.flags(ios_base::dec | ios_base::skipws);
5488 result_type __a;
5489 result_type __b;
5490 __is >> __a >> __b;
5491 if (!__is.fail())
5492 __x.param(param_type(__a, __b));
5493 return __is;
5494}
5495
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005496// fisher_f_distribution
5497
5498template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005499class _LIBCPP_VISIBLE fisher_f_distribution
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005500{
5501public:
5502 // types
5503 typedef _RealType result_type;
5504
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005505 class _LIBCPP_VISIBLE param_type
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005506 {
5507 result_type __m_;
5508 result_type __n_;
5509 public:
5510 typedef fisher_f_distribution distribution_type;
5511
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005512 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005513 explicit param_type(result_type __m = 1, result_type __n = 1)
5514 : __m_(__m), __n_(__n) {}
5515
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005517 result_type m() const {return __m_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005519 result_type n() const {return __n_;}
5520
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005521 friend _LIBCPP_INLINE_VISIBILITY
5522 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005523 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005524 friend _LIBCPP_INLINE_VISIBILITY
5525 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005526 {return !(__x == __y);}
5527 };
5528
5529private:
5530 param_type __p_;
5531
5532public:
5533 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005535 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5536 : __p_(param_type(__m, __n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005538 explicit fisher_f_distribution(const param_type& __p)
5539 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005541 void reset() {}
5542
5543 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005544 template<class _URNG>
5545 _LIBCPP_INLINE_VISIBILITY
5546 result_type operator()(_URNG& __g)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005547 {return (*this)(__g, __p_);}
5548 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5549
5550 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005552 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005554 result_type n() const {return __p_.n();}
5555
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005557 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005559 void param(const param_type& __p) {__p_ = __p;}
5560
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005562 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005564 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005565
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005566 friend _LIBCPP_INLINE_VISIBILITY
5567 bool operator==(const fisher_f_distribution& __x,
5568 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005569 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005570 friend _LIBCPP_INLINE_VISIBILITY
5571 bool operator!=(const fisher_f_distribution& __x,
5572 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005573 {return !(__x == __y);}
5574};
5575
5576template <class _RealType>
5577template<class _URNG>
5578_RealType
5579fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5580{
5581 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5582 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5583 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5584}
5585
5586template <class _CharT, class _Traits, class _RT>
5587basic_ostream<_CharT, _Traits>&
5588operator<<(basic_ostream<_CharT, _Traits>& __os,
5589 const fisher_f_distribution<_RT>& __x)
5590{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005591 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005592 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5593 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005594 _CharT __sp = __os.widen(' ');
5595 __os.fill(__sp);
5596 __os << __x.m() << __sp << __x.n();
5597 return __os;
5598}
5599
5600template <class _CharT, class _Traits, class _RT>
5601basic_istream<_CharT, _Traits>&
5602operator>>(basic_istream<_CharT, _Traits>& __is,
5603 fisher_f_distribution<_RT>& __x)
5604{
5605 typedef fisher_f_distribution<_RT> _Eng;
5606 typedef typename _Eng::result_type result_type;
5607 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005608 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005609 __is.flags(ios_base::dec | ios_base::skipws);
5610 result_type __m;
5611 result_type __n;
5612 __is >> __m >> __n;
5613 if (!__is.fail())
5614 __x.param(param_type(__m, __n));
5615 return __is;
5616}
5617
Howard Hinnant551d8e42010-05-19 01:53:57 +00005618// student_t_distribution
5619
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005620template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005621class _LIBCPP_VISIBLE student_t_distribution
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005622{
5623public:
5624 // types
5625 typedef _RealType result_type;
5626
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005627 class _LIBCPP_VISIBLE param_type
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005628 {
5629 result_type __n_;
5630 public:
5631 typedef student_t_distribution distribution_type;
5632
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005634 explicit param_type(result_type __n = 1) : __n_(__n) {}
5635
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005637 result_type n() const {return __n_;}
5638
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005639 friend _LIBCPP_INLINE_VISIBILITY
5640 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005641 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005642 friend _LIBCPP_INLINE_VISIBILITY
5643 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005644 {return !(__x == __y);}
5645 };
5646
5647private:
5648 param_type __p_;
5649 normal_distribution<result_type> __nd_;
5650
5651public:
5652 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005654 explicit student_t_distribution(result_type __n = 1)
5655 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005657 explicit student_t_distribution(const param_type& __p)
5658 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005660 void reset() {__nd_.reset();}
5661
5662 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005663 template<class _URNG>
5664 _LIBCPP_INLINE_VISIBILITY
5665 result_type operator()(_URNG& __g)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005666 {return (*this)(__g, __p_);}
5667 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5668
5669 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005671 result_type n() const {return __p_.n();}
5672
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005674 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005676 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005677
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005679 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005681 result_type max() const {return numeric_limits<result_type>::infinity();}
5682
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005683 friend _LIBCPP_INLINE_VISIBILITY
5684 bool operator==(const student_t_distribution& __x,
5685 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005686 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005687 friend _LIBCPP_INLINE_VISIBILITY
5688 bool operator!=(const student_t_distribution& __x,
5689 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005690 {return !(__x == __y);}
5691};
5692
5693template <class _RealType>
5694template<class _URNG>
5695_RealType
5696student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5697{
5698 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005699 return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005700}
5701
5702template <class _CharT, class _Traits, class _RT>
5703basic_ostream<_CharT, _Traits>&
5704operator<<(basic_ostream<_CharT, _Traits>& __os,
5705 const student_t_distribution<_RT>& __x)
5706{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005707 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005708 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5709 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005710 __os << __x.n();
5711 return __os;
5712}
5713
5714template <class _CharT, class _Traits, class _RT>
5715basic_istream<_CharT, _Traits>&
5716operator>>(basic_istream<_CharT, _Traits>& __is,
5717 student_t_distribution<_RT>& __x)
5718{
5719 typedef student_t_distribution<_RT> _Eng;
5720 typedef typename _Eng::result_type result_type;
5721 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005722 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005723 __is.flags(ios_base::dec | ios_base::skipws);
5724 result_type __n;
5725 __is >> __n;
5726 if (!__is.fail())
5727 __x.param(param_type(__n));
5728 return __is;
5729}
5730
Howard Hinnant551d8e42010-05-19 01:53:57 +00005731// discrete_distribution
5732
5733template<class _IntType = int>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005734class _LIBCPP_VISIBLE discrete_distribution
Howard Hinnant551d8e42010-05-19 01:53:57 +00005735{
5736public:
5737 // types
5738 typedef _IntType result_type;
5739
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005740 class _LIBCPP_VISIBLE param_type
Howard Hinnant551d8e42010-05-19 01:53:57 +00005741 {
5742 vector<double> __p_;
5743 public:
5744 typedef discrete_distribution distribution_type;
5745
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005747 param_type() {}
5748 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005750 param_type(_InputIterator __f, _InputIterator __l)
5751 : __p_(__f, __l) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005752#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005754 param_type(initializer_list<double> __wl)
5755 : __p_(__wl.begin(), __wl.end()) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005756#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005757 template<class _UnaryOperation>
5758 param_type(size_t __nw, double __xmin, double __xmax,
5759 _UnaryOperation __fw);
5760
5761 vector<double> probabilities() const;
5762
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005763 friend _LIBCPP_INLINE_VISIBILITY
5764 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005765 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005766 friend _LIBCPP_INLINE_VISIBILITY
5767 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005768 {return !(__x == __y);}
5769
5770 private:
5771 void __init();
5772
5773 friend class discrete_distribution;
5774
5775 template <class _CharT, class _Traits, class _IT>
5776 friend
5777 basic_ostream<_CharT, _Traits>&
5778 operator<<(basic_ostream<_CharT, _Traits>& __os,
5779 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005780
Howard Hinnant551d8e42010-05-19 01:53:57 +00005781 template <class _CharT, class _Traits, class _IT>
5782 friend
5783 basic_istream<_CharT, _Traits>&
5784 operator>>(basic_istream<_CharT, _Traits>& __is,
5785 discrete_distribution<_IT>& __x);
5786 };
5787
5788private:
5789 param_type __p_;
5790
5791public:
5792 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005794 discrete_distribution() {}
5795 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005797 discrete_distribution(_InputIterator __f, _InputIterator __l)
5798 : __p_(__f, __l) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005799#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005801 discrete_distribution(initializer_list<double> __wl)
5802 : __p_(__wl) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005803#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005804 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005806 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5807 _UnaryOperation __fw)
5808 : __p_(__nw, __xmin, __xmax, __fw) {}
5809 explicit discrete_distribution(const param_type& __p)
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005811 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005813 void reset() {}
5814
5815 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005816 template<class _URNG>
5817 _LIBCPP_INLINE_VISIBILITY
5818 result_type operator()(_URNG& __g)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005819 {return (*this)(__g, __p_);}
5820 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5821
5822 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005824 vector<double> probabilities() const {return __p_.probabilities();}
5825
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005827 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005829 void param(const param_type& __p) {__p_ = __p;}
5830
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005832 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005834 result_type max() const {return __p_.__p_.size();}
5835
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005836 friend _LIBCPP_INLINE_VISIBILITY
5837 bool operator==(const discrete_distribution& __x,
5838 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005839 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005840 friend _LIBCPP_INLINE_VISIBILITY
5841 bool operator!=(const discrete_distribution& __x,
5842 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005843 {return !(__x == __y);}
5844
5845 template <class _CharT, class _Traits, class _IT>
5846 friend
5847 basic_ostream<_CharT, _Traits>&
5848 operator<<(basic_ostream<_CharT, _Traits>& __os,
5849 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005850
Howard Hinnant551d8e42010-05-19 01:53:57 +00005851 template <class _CharT, class _Traits, class _IT>
5852 friend
5853 basic_istream<_CharT, _Traits>&
5854 operator>>(basic_istream<_CharT, _Traits>& __is,
5855 discrete_distribution<_IT>& __x);
5856};
5857
5858template<class _IntType>
5859template<class _UnaryOperation>
5860discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5861 double __xmin,
5862 double __xmax,
5863 _UnaryOperation __fw)
5864{
5865 if (__nw > 1)
5866 {
5867 __p_.reserve(__nw - 1);
5868 double __d = (__xmax - __xmin) / __nw;
5869 double __d2 = __d / 2;
5870 for (size_t __k = 0; __k < __nw; ++__k)
5871 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5872 __init();
5873 }
5874}
5875
5876template<class _IntType>
5877void
5878discrete_distribution<_IntType>::param_type::__init()
5879{
5880 if (!__p_.empty())
5881 {
5882 if (__p_.size() > 1)
5883 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005884 double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
5885 for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
Howard Hinnant551d8e42010-05-19 01:53:57 +00005886 __i < __e; ++__i)
5887 *__i /= __s;
5888 vector<double> __t(__p_.size() - 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005889 _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00005890 swap(__p_, __t);
5891 }
5892 else
5893 {
5894 __p_.clear();
5895 __p_.shrink_to_fit();
5896 }
5897 }
5898}
5899
5900template<class _IntType>
5901vector<double>
5902discrete_distribution<_IntType>::param_type::probabilities() const
5903{
5904 size_t __n = __p_.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00005905 _VSTD::vector<double> __p(__n+1);
5906 _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00005907 if (__n > 0)
5908 __p[__n] = 1 - __p_[__n-1];
5909 else
5910 __p[0] = 1;
5911 return __p;
5912}
5913
5914template<class _IntType>
5915template<class _URNG>
5916_IntType
5917discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
5918{
5919 uniform_real_distribution<double> __gen;
5920 return static_cast<_IntType>(
Howard Hinnant0949eed2011-06-30 21:18:19 +00005921 _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
Howard Hinnant551d8e42010-05-19 01:53:57 +00005922 __p.__p_.begin());
5923}
5924
5925template <class _CharT, class _Traits, class _IT>
5926basic_ostream<_CharT, _Traits>&
5927operator<<(basic_ostream<_CharT, _Traits>& __os,
5928 const discrete_distribution<_IT>& __x)
5929{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005930 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005931 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5932 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005933 _CharT __sp = __os.widen(' ');
5934 __os.fill(__sp);
5935 size_t __n = __x.__p_.__p_.size();
5936 __os << __n;
5937 for (size_t __i = 0; __i < __n; ++__i)
5938 __os << __sp << __x.__p_.__p_[__i];
5939 return __os;
5940}
5941
5942template <class _CharT, class _Traits, class _IT>
5943basic_istream<_CharT, _Traits>&
5944operator>>(basic_istream<_CharT, _Traits>& __is,
5945 discrete_distribution<_IT>& __x)
5946{
5947 typedef discrete_distribution<_IT> _Eng;
5948 typedef typename _Eng::result_type result_type;
5949 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005950 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005951 __is.flags(ios_base::dec | ios_base::skipws);
5952 size_t __n;
5953 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005954 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005955 for (size_t __i = 0; __i < __n; ++__i)
5956 __is >> __p[__i];
5957 if (!__is.fail())
5958 swap(__x.__p_.__p_, __p);
5959 return __is;
5960}
5961
Howard Hinnantd6d11712010-05-20 15:11:46 +00005962// piecewise_constant_distribution
5963
5964template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005965class _LIBCPP_VISIBLE piecewise_constant_distribution
Howard Hinnantd6d11712010-05-20 15:11:46 +00005966{
5967public:
5968 // types
5969 typedef _RealType result_type;
5970
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005971 class _LIBCPP_VISIBLE param_type
Howard Hinnantd6d11712010-05-20 15:11:46 +00005972 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00005973 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00005974 vector<result_type> __densities_;
5975 vector<result_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005976 public:
5977 typedef piecewise_constant_distribution distribution_type;
5978
5979 param_type();
5980 template<class _InputIteratorB, class _InputIteratorW>
5981 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5982 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00005983#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00005984 template<class _UnaryOperation>
5985 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00005986#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00005987 template<class _UnaryOperation>
5988 param_type(size_t __nw, result_type __xmin, result_type __xmax,
5989 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00005990 param_type & operator=(const param_type& __rhs);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005991
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00005993 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00005995 vector<result_type> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005996
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005997 friend _LIBCPP_INLINE_VISIBILITY
5998 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:40 +00005999 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006000 friend _LIBCPP_INLINE_VISIBILITY
6001 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006002 {return !(__x == __y);}
6003
6004 private:
6005 void __init();
6006
6007 friend class piecewise_constant_distribution;
6008
6009 template <class _CharT, class _Traits, class _RT>
6010 friend
6011 basic_ostream<_CharT, _Traits>&
6012 operator<<(basic_ostream<_CharT, _Traits>& __os,
6013 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006014
Howard Hinnantd6d11712010-05-20 15:11:46 +00006015 template <class _CharT, class _Traits, class _RT>
6016 friend
6017 basic_istream<_CharT, _Traits>&
6018 operator>>(basic_istream<_CharT, _Traits>& __is,
6019 piecewise_constant_distribution<_RT>& __x);
6020 };
6021
6022private:
6023 param_type __p_;
6024
6025public:
6026 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006028 piecewise_constant_distribution() {}
6029 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006031 piecewise_constant_distribution(_InputIteratorB __fB,
6032 _InputIteratorB __lB,
6033 _InputIteratorW __fW)
6034 : __p_(__fB, __lB, __fW) {}
6035
Howard Hinnante3e32912011-08-12 21:56:02 +00006036#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006037 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006039 piecewise_constant_distribution(initializer_list<result_type> __bl,
6040 _UnaryOperation __fw)
6041 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006042#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006043
6044 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006046 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6047 result_type __xmax, _UnaryOperation __fw)
6048 : __p_(__nw, __xmin, __xmax, __fw) {}
6049
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006051 explicit piecewise_constant_distribution(const param_type& __p)
6052 : __p_(__p) {}
6053
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006055 void reset() {}
6056
6057 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006058 template<class _URNG>
6059 _LIBCPP_INLINE_VISIBILITY
6060 result_type operator()(_URNG& __g)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006061 {return (*this)(__g, __p_);}
6062 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6063
6064 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006066 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006068 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnantd6d11712010-05-20 15:11:46 +00006069
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006071 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006073 void param(const param_type& __p) {__p_ = __p;}
6074
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006076 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006077 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006078 result_type max() const {return __p_.__b_.back();}
6079
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006080 friend _LIBCPP_INLINE_VISIBILITY
6081 bool operator==(const piecewise_constant_distribution& __x,
6082 const piecewise_constant_distribution& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006083 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006084 friend _LIBCPP_INLINE_VISIBILITY
6085 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnantd6d11712010-05-20 15:11:46 +00006086 const piecewise_constant_distribution& __y)
6087 {return !(__x == __y);}
6088
6089 template <class _CharT, class _Traits, class _RT>
6090 friend
6091 basic_ostream<_CharT, _Traits>&
6092 operator<<(basic_ostream<_CharT, _Traits>& __os,
6093 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006094
Howard Hinnantd6d11712010-05-20 15:11:46 +00006095 template <class _CharT, class _Traits, class _RT>
6096 friend
6097 basic_istream<_CharT, _Traits>&
6098 operator>>(basic_istream<_CharT, _Traits>& __is,
6099 piecewise_constant_distribution<_RT>& __x);
6100};
6101
6102template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006103typename piecewise_constant_distribution<_RealType>::param_type &
6104piecewise_constant_distribution<_RealType>::param_type::operator=
6105 (const param_type& __rhs)
6106{
6107// These can throw
6108 __b_.reserve (__rhs.__b_.size ());
6109 __densities_.reserve(__rhs.__densities_.size());
6110 __areas_.reserve (__rhs.__areas_.size());
6111
6112// These can not throw
6113 __b_ = __rhs.__b_;
6114 __densities_ = __rhs.__densities_;
6115 __areas_ = __rhs.__areas_;
6116 return *this;
6117}
6118
6119template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006120void
6121piecewise_constant_distribution<_RealType>::param_type::__init()
6122{
Howard Hinnant2a592542010-05-24 00:35:40 +00006123 // __densities_ contains non-normalized areas
Howard Hinnant0949eed2011-06-30 21:18:19 +00006124 result_type __total_area = _VSTD::accumulate(__densities_.begin(),
Howard Hinnant2a592542010-05-24 00:35:40 +00006125 __densities_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006126 result_type());
Howard Hinnant2a592542010-05-24 00:35:40 +00006127 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6128 __densities_[__i] /= __total_area;
6129 // __densities_ contains normalized areas
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006130 __areas_.assign(__densities_.size(), result_type());
Howard Hinnant0949eed2011-06-30 21:18:19 +00006131 _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
Howard Hinnant2a592542010-05-24 00:35:40 +00006132 __areas_.begin() + 1);
6133 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6134 __densities_.back() = 1 - __areas_.back(); // correct round off error
6135 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6136 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6137 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:46 +00006138}
6139
6140template<class _RealType>
6141piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:40 +00006142 : __b_(2),
Howard Hinnant54305402010-05-25 00:27:34 +00006143 __densities_(1, 1.0),
6144 __areas_(1, 0.0)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006145{
6146 __b_[1] = 1;
6147}
6148
6149template<class _RealType>
6150template<class _InputIteratorB, class _InputIteratorW>
6151piecewise_constant_distribution<_RealType>::param_type::param_type(
6152 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6153 : __b_(__fB, __lB)
6154{
6155 if (__b_.size() < 2)
6156 {
6157 __b_.resize(2);
6158 __b_[0] = 0;
6159 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006160 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006161 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006162 }
6163 else
6164 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006165 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006166 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:40 +00006167 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006168 __init();
6169 }
6170}
6171
Howard Hinnante3e32912011-08-12 21:56:02 +00006172#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6173
Howard Hinnantd6d11712010-05-20 15:11:46 +00006174template<class _RealType>
6175template<class _UnaryOperation>
6176piecewise_constant_distribution<_RealType>::param_type::param_type(
6177 initializer_list<result_type> __bl, _UnaryOperation __fw)
6178 : __b_(__bl.begin(), __bl.end())
6179{
6180 if (__b_.size() < 2)
6181 {
6182 __b_.resize(2);
6183 __b_[0] = 0;
6184 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006185 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006186 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006187 }
6188 else
6189 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006190 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006191 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006192 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006193 __init();
6194 }
6195}
6196
Howard Hinnante3e32912011-08-12 21:56:02 +00006197#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6198
Howard Hinnantd6d11712010-05-20 15:11:46 +00006199template<class _RealType>
6200template<class _UnaryOperation>
6201piecewise_constant_distribution<_RealType>::param_type::param_type(
6202 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6203 : __b_(__nw == 0 ? 2 : __nw + 1)
6204{
6205 size_t __n = __b_.size() - 1;
6206 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:40 +00006207 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006208 for (size_t __i = 0; __i < __n; ++__i)
6209 {
6210 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:40 +00006211 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006212 }
6213 __b_[__n] = __xmax;
6214 __init();
6215}
6216
6217template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006218template<class _URNG>
6219_RealType
6220piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6221{
6222 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006223 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006224 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006225 __u) - __p.__areas_.begin() - 1;
6226 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006227}
6228
6229template <class _CharT, class _Traits, class _RT>
6230basic_ostream<_CharT, _Traits>&
6231operator<<(basic_ostream<_CharT, _Traits>& __os,
6232 const piecewise_constant_distribution<_RT>& __x)
6233{
Howard Hinnant9c0df142012-10-30 19:06:59 +00006234 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00006235 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6236 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006237 _CharT __sp = __os.widen(' ');
6238 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:40 +00006239 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006240 __os << __n;
6241 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006242 __os << __sp << __x.__p_.__b_[__i];
6243 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006244 __os << __sp << __n;
6245 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006246 __os << __sp << __x.__p_.__densities_[__i];
6247 __n = __x.__p_.__areas_.size();
6248 __os << __sp << __n;
6249 for (size_t __i = 0; __i < __n; ++__i)
6250 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006251 return __os;
6252}
6253
6254template <class _CharT, class _Traits, class _RT>
6255basic_istream<_CharT, _Traits>&
6256operator>>(basic_istream<_CharT, _Traits>& __is,
6257 piecewise_constant_distribution<_RT>& __x)
6258{
6259 typedef piecewise_constant_distribution<_RT> _Eng;
6260 typedef typename _Eng::result_type result_type;
6261 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00006262 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006263 __is.flags(ios_base::dec | ios_base::skipws);
6264 size_t __n;
6265 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006266 vector<result_type> __b(__n);
6267 for (size_t __i = 0; __i < __n; ++__i)
6268 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:40 +00006269 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006270 vector<result_type> __densities(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006271 for (size_t __i = 0; __i < __n; ++__i)
6272 __is >> __densities[__i];
6273 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006274 vector<result_type> __areas(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006275 for (size_t __i = 0; __i < __n; ++__i)
6276 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006277 if (!__is.fail())
6278 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00006279 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:40 +00006280 swap(__x.__p_.__densities_, __densities);
6281 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006282 }
6283 return __is;
6284}
6285
Howard Hinnant54305402010-05-25 00:27:34 +00006286// piecewise_linear_distribution
6287
6288template<class _RealType = double>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006289class _LIBCPP_VISIBLE piecewise_linear_distribution
Howard Hinnant54305402010-05-25 00:27:34 +00006290{
6291public:
6292 // types
6293 typedef _RealType result_type;
6294
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006295 class _LIBCPP_VISIBLE param_type
Howard Hinnant54305402010-05-25 00:27:34 +00006296 {
Howard Hinnant54305402010-05-25 00:27:34 +00006297 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006298 vector<result_type> __densities_;
6299 vector<result_type> __areas_;
Howard Hinnant54305402010-05-25 00:27:34 +00006300 public:
6301 typedef piecewise_linear_distribution distribution_type;
6302
6303 param_type();
6304 template<class _InputIteratorB, class _InputIteratorW>
6305 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6306 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00006307#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006308 template<class _UnaryOperation>
6309 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00006310#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006311 template<class _UnaryOperation>
6312 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6313 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006314 param_type & operator=(const param_type& __rhs);
6315
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006316 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006317 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006319 vector<result_type> densities() const {return __densities_;}
Howard Hinnant54305402010-05-25 00:27:34 +00006320
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006321 friend _LIBCPP_INLINE_VISIBILITY
6322 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006323 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006324 friend _LIBCPP_INLINE_VISIBILITY
6325 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006326 {return !(__x == __y);}
6327
6328 private:
6329 void __init();
6330
6331 friend class piecewise_linear_distribution;
6332
6333 template <class _CharT, class _Traits, class _RT>
6334 friend
6335 basic_ostream<_CharT, _Traits>&
6336 operator<<(basic_ostream<_CharT, _Traits>& __os,
6337 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006338
Howard Hinnant54305402010-05-25 00:27:34 +00006339 template <class _CharT, class _Traits, class _RT>
6340 friend
6341 basic_istream<_CharT, _Traits>&
6342 operator>>(basic_istream<_CharT, _Traits>& __is,
6343 piecewise_linear_distribution<_RT>& __x);
6344 };
6345
6346private:
6347 param_type __p_;
6348
6349public:
6350 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006352 piecewise_linear_distribution() {}
6353 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006355 piecewise_linear_distribution(_InputIteratorB __fB,
6356 _InputIteratorB __lB,
6357 _InputIteratorW __fW)
6358 : __p_(__fB, __lB, __fW) {}
Howard Hinnant324bb032010-08-22 00:02:43 +00006359
Howard Hinnante3e32912011-08-12 21:56:02 +00006360#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006361 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006363 piecewise_linear_distribution(initializer_list<result_type> __bl,
6364 _UnaryOperation __fw)
6365 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006366#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006367
6368 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006370 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6371 result_type __xmax, _UnaryOperation __fw)
6372 : __p_(__nw, __xmin, __xmax, __fw) {}
6373
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006375 explicit piecewise_linear_distribution(const param_type& __p)
6376 : __p_(__p) {}
6377
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006379 void reset() {}
6380
6381 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006382 template<class _URNG>
6383 _LIBCPP_INLINE_VISIBILITY
6384 result_type operator()(_URNG& __g)
Howard Hinnant54305402010-05-25 00:27:34 +00006385 {return (*this)(__g, __p_);}
6386 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6387
6388 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006390 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006392 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant54305402010-05-25 00:27:34 +00006393
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006395 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006397 void param(const param_type& __p) {__p_ = __p;}
6398
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006400 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006402 result_type max() const {return __p_.__b_.back();}
6403
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006404 friend _LIBCPP_INLINE_VISIBILITY
6405 bool operator==(const piecewise_linear_distribution& __x,
6406 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006407 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006408 friend _LIBCPP_INLINE_VISIBILITY
6409 bool operator!=(const piecewise_linear_distribution& __x,
6410 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006411 {return !(__x == __y);}
6412
6413 template <class _CharT, class _Traits, class _RT>
6414 friend
6415 basic_ostream<_CharT, _Traits>&
6416 operator<<(basic_ostream<_CharT, _Traits>& __os,
6417 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006418
Howard Hinnant54305402010-05-25 00:27:34 +00006419 template <class _CharT, class _Traits, class _RT>
6420 friend
6421 basic_istream<_CharT, _Traits>&
6422 operator>>(basic_istream<_CharT, _Traits>& __is,
6423 piecewise_linear_distribution<_RT>& __x);
6424};
6425
6426template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006427typename piecewise_linear_distribution<_RealType>::param_type &
6428piecewise_linear_distribution<_RealType>::param_type::operator=
6429 (const param_type& __rhs)
6430{
6431// These can throw
6432 __b_.reserve (__rhs.__b_.size ());
6433 __densities_.reserve(__rhs.__densities_.size());
6434 __areas_.reserve (__rhs.__areas_.size());
6435
6436// These can not throw
6437 __b_ = __rhs.__b_;
6438 __densities_ = __rhs.__densities_;
6439 __areas_ = __rhs.__areas_;
6440 return *this;
6441}
6442
6443
6444template<class _RealType>
Howard Hinnant54305402010-05-25 00:27:34 +00006445void
6446piecewise_linear_distribution<_RealType>::param_type::__init()
6447{
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006448 __areas_.assign(__densities_.size() - 1, result_type());
Howard Hinnant99968442011-11-29 18:15:50 +00006449 result_type _Sp = 0;
Howard Hinnant54305402010-05-25 00:27:34 +00006450 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6451 {
6452 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6453 (__b_[__i+1] - __b_[__i]) * .5;
Howard Hinnant99968442011-11-29 18:15:50 +00006454 _Sp += __areas_[__i];
Howard Hinnant54305402010-05-25 00:27:34 +00006455 }
6456 for (size_t __i = __areas_.size(); __i > 1;)
6457 {
6458 --__i;
Howard Hinnant99968442011-11-29 18:15:50 +00006459 __areas_[__i] = __areas_[__i-1] / _Sp;
Howard Hinnant54305402010-05-25 00:27:34 +00006460 }
6461 __areas_[0] = 0;
6462 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6463 __areas_[__i] += __areas_[__i-1];
6464 for (size_t __i = 0; __i < __densities_.size(); ++__i)
Howard Hinnant99968442011-11-29 18:15:50 +00006465 __densities_[__i] /= _Sp;
Howard Hinnant54305402010-05-25 00:27:34 +00006466}
6467
6468template<class _RealType>
6469piecewise_linear_distribution<_RealType>::param_type::param_type()
6470 : __b_(2),
6471 __densities_(2, 1.0),
6472 __areas_(1, 0.0)
6473{
6474 __b_[1] = 1;
6475}
6476
6477template<class _RealType>
6478template<class _InputIteratorB, class _InputIteratorW>
6479piecewise_linear_distribution<_RealType>::param_type::param_type(
6480 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6481 : __b_(__fB, __lB)
6482{
6483 if (__b_.size() < 2)
6484 {
6485 __b_.resize(2);
6486 __b_[0] = 0;
6487 __b_[1] = 1;
6488 __densities_.assign(2, 1.0);
6489 __areas_.assign(1, 0.0);
6490 }
6491 else
6492 {
6493 __densities_.reserve(__b_.size());
6494 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6495 __densities_.push_back(*__fW);
6496 __init();
6497 }
6498}
6499
Howard Hinnante3e32912011-08-12 21:56:02 +00006500#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6501
Howard Hinnant54305402010-05-25 00:27:34 +00006502template<class _RealType>
6503template<class _UnaryOperation>
6504piecewise_linear_distribution<_RealType>::param_type::param_type(
6505 initializer_list<result_type> __bl, _UnaryOperation __fw)
6506 : __b_(__bl.begin(), __bl.end())
6507{
6508 if (__b_.size() < 2)
6509 {
6510 __b_.resize(2);
6511 __b_[0] = 0;
6512 __b_[1] = 1;
6513 __densities_.assign(2, 1.0);
6514 __areas_.assign(1, 0.0);
6515 }
6516 else
6517 {
6518 __densities_.reserve(__b_.size());
6519 for (size_t __i = 0; __i < __b_.size(); ++__i)
6520 __densities_.push_back(__fw(__b_[__i]));
6521 __init();
6522 }
6523}
6524
Howard Hinnante3e32912011-08-12 21:56:02 +00006525#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6526
Howard Hinnant54305402010-05-25 00:27:34 +00006527template<class _RealType>
6528template<class _UnaryOperation>
6529piecewise_linear_distribution<_RealType>::param_type::param_type(
6530 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6531 : __b_(__nw == 0 ? 2 : __nw + 1)
6532{
6533 size_t __n = __b_.size() - 1;
6534 result_type __d = (__xmax - __xmin) / __n;
6535 __densities_.reserve(__b_.size());
6536 for (size_t __i = 0; __i < __n; ++__i)
6537 {
6538 __b_[__i] = __xmin + __i * __d;
6539 __densities_.push_back(__fw(__b_[__i]));
6540 }
6541 __b_[__n] = __xmax;
6542 __densities_.push_back(__fw(__b_[__n]));
6543 __init();
6544}
6545
6546template<class _RealType>
6547template<class _URNG>
6548_RealType
6549piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6550{
6551 typedef uniform_real_distribution<result_type> _Gen;
6552 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006553 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006554 __u) - __p.__areas_.begin() - 1;
Howard Hinnant54305402010-05-25 00:27:34 +00006555 __u -= __p.__areas_[__k];
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006556 const result_type __dk = __p.__densities_[__k];
6557 const result_type __dk1 = __p.__densities_[__k+1];
6558 const result_type __deltad = __dk1 - __dk;
Howard Hinnant54305402010-05-25 00:27:34 +00006559 const result_type __bk = __p.__b_[__k];
6560 if (__deltad == 0)
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006561 return __u / __dk + __bk;
Howard Hinnant54305402010-05-25 00:27:34 +00006562 const result_type __bk1 = __p.__b_[__k+1];
6563 const result_type __deltab = __bk1 - __bk;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006564 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnant0949eed2011-06-30 21:18:19 +00006565 _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006566 __deltad;
Howard Hinnant54305402010-05-25 00:27:34 +00006567}
6568
6569template <class _CharT, class _Traits, class _RT>
6570basic_ostream<_CharT, _Traits>&
6571operator<<(basic_ostream<_CharT, _Traits>& __os,
6572 const piecewise_linear_distribution<_RT>& __x)
6573{
Howard Hinnant9c0df142012-10-30 19:06:59 +00006574 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant54305402010-05-25 00:27:34 +00006575 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6576 ios_base::scientific);
6577 _CharT __sp = __os.widen(' ');
6578 __os.fill(__sp);
6579 size_t __n = __x.__p_.__b_.size();
6580 __os << __n;
6581 for (size_t __i = 0; __i < __n; ++__i)
6582 __os << __sp << __x.__p_.__b_[__i];
6583 __n = __x.__p_.__densities_.size();
6584 __os << __sp << __n;
6585 for (size_t __i = 0; __i < __n; ++__i)
6586 __os << __sp << __x.__p_.__densities_[__i];
6587 __n = __x.__p_.__areas_.size();
6588 __os << __sp << __n;
6589 for (size_t __i = 0; __i < __n; ++__i)
6590 __os << __sp << __x.__p_.__areas_[__i];
6591 return __os;
6592}
6593
6594template <class _CharT, class _Traits, class _RT>
6595basic_istream<_CharT, _Traits>&
6596operator>>(basic_istream<_CharT, _Traits>& __is,
6597 piecewise_linear_distribution<_RT>& __x)
6598{
6599 typedef piecewise_linear_distribution<_RT> _Eng;
6600 typedef typename _Eng::result_type result_type;
6601 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00006602 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant54305402010-05-25 00:27:34 +00006603 __is.flags(ios_base::dec | ios_base::skipws);
6604 size_t __n;
6605 __is >> __n;
6606 vector<result_type> __b(__n);
6607 for (size_t __i = 0; __i < __n; ++__i)
6608 __is >> __b[__i];
6609 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006610 vector<result_type> __densities(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006611 for (size_t __i = 0; __i < __n; ++__i)
6612 __is >> __densities[__i];
6613 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006614 vector<result_type> __areas(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006615 for (size_t __i = 0; __i < __n; ++__i)
6616 __is >> __areas[__i];
6617 if (!__is.fail())
6618 {
6619 swap(__x.__p_.__b_, __b);
6620 swap(__x.__p_.__densities_, __densities);
6621 swap(__x.__p_.__areas_, __areas);
6622 }
6623 return __is;
6624}
6625
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00006626_LIBCPP_END_NAMESPACE_STD
6627
6628#endif // _LIBCPP_RANDOM