blob: 4e11f9e70771113d827a506082e4715929a94443 [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>
Eric Fiselierf51d6762015-01-23 22:22:36 +00001637#include <cstdint>
1638#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001639#include <type_traits>
1640#include <initializer_list>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641#include <limits>
1642#include <algorithm>
Howard Hinnant551d8e42010-05-19 01:53:57 +00001643#include <numeric>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001644#include <vector>
1645#include <string>
1646#include <istream>
1647#include <ostream>
1648
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 Hinnant0f678bd2013-08-12 18:38:34 +00001816class _LIBCPP_TYPE_VIS_ONLY 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 Hinnant0f678bd2013-08-12 18:38:34 +00001832class _LIBCPP_TYPE_VIS_ONLY linear_congruential_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833{
1834public:
1835 // types
1836 typedef _UIntType result_type;
1837
Howard Hinnantddb4e4c2013-05-21 21:19:35 +00001838private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839 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
Howard Hinnant8f72d5c2013-05-21 21:05:12 +00001883 : (__m > 0x100000000ull))>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001884
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>
Howard Hinnant0a69fa12012-12-12 21:14:28 +00001934 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1935 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
1936
1937template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1938 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1939 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
1940
1941template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1942 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1943 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
1944
1945template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1946 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1947 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
1948
1949template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950template<class _Sseq>
1951void
1952linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1953 integral_constant<unsigned, 1>)
1954{
1955 const unsigned __k = 1;
1956 uint32_t __ar[__k+3];
1957 __q.generate(__ar, __ar + __k + 3);
1958 result_type __s = static_cast<result_type>(__ar[3] % __m);
1959 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1960}
1961
1962template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1963template<class _Sseq>
1964void
1965linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1966 integral_constant<unsigned, 2>)
1967{
1968 const unsigned __k = 2;
1969 uint32_t __ar[__k+3];
1970 __q.generate(__ar, __ar + __k + 3);
1971 result_type __s = static_cast<result_type>((__ar[3] +
Howard Hinnant8f72d5c2013-05-21 21:05:12 +00001972 ((uint64_t)__ar[4] << 32)) % __m);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001973 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1974}
1975
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976template <class _CharT, class _Traits,
1977 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00001978inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979basic_ostream<_CharT, _Traits>&
1980operator<<(basic_ostream<_CharT, _Traits>& __os,
1981 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1982{
Howard Hinnant9c0df142012-10-30 19:06:59 +00001983 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001984 __os.flags(ios_base::dec | ios_base::left);
1985 __os.fill(__os.widen(' '));
1986 return __os << __x.__x_;
1987}
1988
1989template <class _CharT, class _Traits,
1990 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1991basic_istream<_CharT, _Traits>&
1992operator>>(basic_istream<_CharT, _Traits>& __is,
1993 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1994{
Howard Hinnant9c0df142012-10-30 19:06:59 +00001995 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001996 __is.flags(ios_base::dec | ios_base::skipws);
1997 _UIntType __t;
1998 __is >> __t;
1999 if (!__is.fail())
2000 __x.__x_ = __t;
2001 return __is;
2002}
2003
2004typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2005 minstd_rand0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002006typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2007 minstd_rand;
Howard Hinnantd6d11712010-05-20 15:11:46 +00002008typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002009// mersenne_twister_engine
2010
2011template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2012 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2013 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002014class _LIBCPP_TYPE_VIS_ONLY mersenne_twister_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002015
Howard Hinnant99968442011-11-29 18:15:50 +00002016template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2017 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2018 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019bool
Howard Hinnant99968442011-11-29 18:15:50 +00002020operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2021 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2022 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2023 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024
Howard Hinnant99968442011-11-29 18:15:50 +00002025template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2026 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2027 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnant33be35e2012-09-14 00:39:16 +00002028_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002029bool
Howard Hinnant99968442011-11-29 18:15:50 +00002030operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2031 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2032 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2033 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034
2035template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002036 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 Hinnantbc8d3f92010-05-11 19:42:16 +00002039basic_ostream<_CharT, _Traits>&
2040operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002041 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2042 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043
2044template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002045 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2046 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2047 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048basic_istream<_CharT, _Traits>&
2049operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002050 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2051 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052
2053template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2054 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2055 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002056class _LIBCPP_TYPE_VIS_ONLY mersenne_twister_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002057{
2058public:
2059 // types
2060 typedef _UIntType result_type;
2061
2062private:
2063 result_type __x_[__n];
2064 size_t __i_;
2065
2066 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2067 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002068 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002069 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2070 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2071 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2072 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2073 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2074 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2075 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2076public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002077 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2078 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2079 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002080 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2081 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2082 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2083 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2084 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2085 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2086
2087 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002088 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2089 static _LIBCPP_CONSTEXPR const size_t state_size = __n;
2090 static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
2091 static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
2092 static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
2093 static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
2094 static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
2095 static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
2096 static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
2097 static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
2098 static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
2099 static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
2100 static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002102 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002104 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2105 static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106
2107 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109 explicit mersenne_twister_engine(result_type __sd = default_seed)
2110 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002111 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002113 explicit mersenne_twister_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002114 typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115 {seed(__q);}
2116 void seed(result_type __sd = default_seed);
2117 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119 typename enable_if
2120 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002121 __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122 void
2123 >::type
2124 seed(_Sseq& __q)
2125 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2126
2127 // generating functions
2128 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2131
Howard Hinnant99968442011-11-29 18:15:50 +00002132 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2133 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2134 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002135 friend
2136 bool
Howard Hinnant99968442011-11-29 18:15:50 +00002137 operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2138 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2139 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2140 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002141
Howard Hinnant99968442011-11-29 18:15:50 +00002142 template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2143 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2144 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002145 friend
2146 bool
Howard Hinnant99968442011-11-29 18:15:50 +00002147 operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2148 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2149 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2150 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151
2152 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002153 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 basic_ostream<_CharT, _Traits>&
2158 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002159 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2160 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002161
2162 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002163 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2164 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2165 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166 friend
2167 basic_istream<_CharT, _Traits>&
2168 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002169 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2170 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002171private:
2172
2173 template<class _Sseq>
2174 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2175 template<class _Sseq>
2176 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2177
2178 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180 static
2181 typename enable_if
2182 <
2183 __count < __w,
2184 result_type
2185 >::type
2186 __lshift(result_type __x) {return (__x << __count) & _Max;}
2187
2188 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002190 static
2191 typename enable_if
2192 <
2193 (__count >= __w),
2194 result_type
2195 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00002196 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002197
2198 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002200 static
2201 typename enable_if
2202 <
2203 __count < _Dt,
2204 result_type
2205 >::type
2206 __rshift(result_type __x) {return __x >> __count;}
2207
2208 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002210 static
2211 typename enable_if
2212 <
2213 (__count >= _Dt),
2214 result_type
2215 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00002216 __rshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002217};
2218
2219template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2220 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2221 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant0a69fa12012-12-12 21:14:28 +00002222 _LIBCPP_CONSTEXPR const size_t
2223 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size;
2224
2225template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2226 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2227 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2228 _LIBCPP_CONSTEXPR const size_t
2229 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size;
2230
2231template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2232 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2233 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2234 _LIBCPP_CONSTEXPR const size_t
2235 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size;
2236
2237template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2238 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2239 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2240 _LIBCPP_CONSTEXPR const size_t
2241 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits;
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>
2246 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2247 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask;
2248
2249template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2250 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2251 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2252 _LIBCPP_CONSTEXPR const size_t
2253 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u;
2254
2255template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2256 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2257 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2258 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2259 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d;
2260
2261template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2262 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2263 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2264 _LIBCPP_CONSTEXPR const size_t
2265 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s;
2266
2267template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2268 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2269 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2270 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2271 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b;
2272
2273template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2274 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2275 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2276 _LIBCPP_CONSTEXPR const size_t
2277 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t;
2278
2279template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2280 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2281 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2282 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2283 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c;
2284
2285template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2286 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2287 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2288 _LIBCPP_CONSTEXPR const size_t
2289 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l;
2290
2291template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2292 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2293 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2294 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2295 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier;
2296
2297template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2298 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2299 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2300 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2301 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed;
2302
2303template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2304 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2305 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002306void
2307mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2308 __t, __c, __l, __f>::seed(result_type __sd)
2309{ // __w >= 2
2310 __x_[0] = __sd & _Max;
2311 for (size_t __i = 1; __i < __n; ++__i)
2312 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2313 __i_ = 0;
2314}
2315
2316template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2317 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2318 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2319template<class _Sseq>
2320void
2321mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2322 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2323{
2324 const unsigned __k = 1;
2325 uint32_t __ar[__n * __k];
2326 __q.generate(__ar, __ar + __n * __k);
2327 for (size_t __i = 0; __i < __n; ++__i)
2328 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2329 const result_type __mask = __r == _Dt ? result_type(~0) :
2330 (result_type(1) << __r) - result_type(1);
2331 __i_ = 0;
2332 if ((__x_[0] & ~__mask) == 0)
2333 {
2334 for (size_t __i = 1; __i < __n; ++__i)
2335 if (__x_[__i] != 0)
2336 return;
2337 __x_[0] = _Max;
2338 }
2339}
2340
2341template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2342 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2343 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2344template<class _Sseq>
2345void
2346mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2347 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2348{
2349 const unsigned __k = 2;
2350 uint32_t __ar[__n * __k];
2351 __q.generate(__ar, __ar + __n * __k);
2352 for (size_t __i = 0; __i < __n; ++__i)
2353 __x_[__i] = static_cast<result_type>(
2354 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2355 const result_type __mask = __r == _Dt ? result_type(~0) :
2356 (result_type(1) << __r) - result_type(1);
2357 __i_ = 0;
2358 if ((__x_[0] & ~__mask) == 0)
2359 {
2360 for (size_t __i = 1; __i < __n; ++__i)
2361 if (__x_[__i] != 0)
2362 return;
2363 __x_[0] = _Max;
2364 }
2365}
2366
2367template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2368 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2369 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2370_UIntType
2371mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2372 __t, __c, __l, __f>::operator()()
2373{
2374 const size_t __j = (__i_ + 1) % __n;
2375 const result_type __mask = __r == _Dt ? result_type(~0) :
2376 (result_type(1) << __r) - result_type(1);
Howard Hinnant99968442011-11-29 18:15:50 +00002377 const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002378 const size_t __k = (__i_ + __m) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00002379 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002380 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2381 __i_ = __j;
2382 __z ^= __lshift<__s>(__z) & __b;
2383 __z ^= __lshift<__t>(__z) & __c;
2384 return __z ^ __rshift<__l>(__z);
2385}
2386
Howard Hinnant99968442011-11-29 18:15:50 +00002387template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2388 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2389 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002390bool
Howard Hinnant99968442011-11-29 18:15:50 +00002391operator==(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2392 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2393 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2394 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002395{
2396 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:50 +00002397 return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398 if (__x.__i_ == 0 || __y.__i_ == 0)
2399 {
Howard Hinnant99968442011-11-29 18:15:50 +00002400 size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002401 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 __y.__x_ + __y.__i_))
2403 return false;
2404 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00002405 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
2406 return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407 }
2408 if (__x.__i_ < __y.__i_)
2409 {
Howard Hinnant99968442011-11-29 18:15:50 +00002410 size_t __j = _Np - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002411 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002412 __y.__x_ + __y.__i_))
2413 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002414 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002415 __y.__x_))
2416 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002417 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002418 __y.__x_ + (_Np - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002419 }
Howard Hinnant99968442011-11-29 18:15:50 +00002420 size_t __j = _Np - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002421 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002422 __x.__x_ + __x.__i_))
2423 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002424 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002425 __x.__x_))
2426 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002427 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002428 __x.__x_ + (_Np - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429}
2430
Howard Hinnant99968442011-11-29 18:15:50 +00002431template <class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2432 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2433 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002434inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435bool
Howard Hinnant99968442011-11-29 18:15:50 +00002436operator!=(const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2437 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
2438 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2439 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440{
2441 return !(__x == __y);
2442}
2443
2444template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002445 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2446 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2447 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002448basic_ostream<_CharT, _Traits>&
2449operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002450 const mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2451 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002453 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454 __os.flags(ios_base::dec | ios_base::left);
2455 _CharT __sp = __os.widen(' ');
2456 __os.fill(__sp);
2457 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:50 +00002458 for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002459 __os << __sp << __x.__x_[__j];
2460 for (size_t __j = 0; __j < __x.__i_; ++__j)
2461 __os << __sp << __x.__x_[__j];
2462 return __os;
2463}
2464
2465template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002466 class _UI, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2467 _UI _Ap, size_t _Up, _UI _Dp, size_t _Sp,
2468 _UI _Bp, size_t _Tp, _UI _Cp, size_t _Lp, _UI _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002469basic_istream<_CharT, _Traits>&
2470operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002471 mersenne_twister_engine<_UI, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
2472 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002473{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002474 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002475 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:50 +00002476 _UI __t[_Np];
2477 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002478 __is >> __t[__i];
2479 if (!__is.fail())
2480 {
Howard Hinnant99968442011-11-29 18:15:50 +00002481 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002482 __x.__x_[__i] = __t[__i];
2483 __x.__i_ = 0;
2484 }
2485 return __is;
2486}
2487
2488typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2489 0x9908b0df, 11, 0xffffffff,
2490 7, 0x9d2c5680,
2491 15, 0xefc60000,
2492 18, 1812433253> mt19937;
2493typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2494 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2495 17, 0x71d67fffeda60000ULL,
2496 37, 0xfff7eee000000000ULL,
2497 43, 6364136223846793005ULL> mt19937_64;
2498
2499// subtract_with_carry_engine
2500
2501template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002502class _LIBCPP_TYPE_VIS_ONLY subtract_with_carry_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503
Howard Hinnant99968442011-11-29 18:15:50 +00002504template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002505bool
2506operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002507 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2508 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509
Howard Hinnant99968442011-11-29 18:15:50 +00002510template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnant33be35e2012-09-14 00:39:16 +00002511_LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512bool
2513operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002514 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2515 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516
2517template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002518 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519basic_ostream<_CharT, _Traits>&
2520operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002521 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002522
2523template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002524 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002525basic_istream<_CharT, _Traits>&
2526operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002527 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002528
2529template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002530class _LIBCPP_TYPE_VIS_ONLY subtract_with_carry_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531{
2532public:
2533 // types
2534 typedef _UIntType result_type;
2535
2536private:
2537 result_type __x_[__r];
2538 result_type __c_;
2539 size_t __i_;
2540
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002541 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002542 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2543 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2544 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2545 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2546public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002547 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2548 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2549 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002550 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2551
2552 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002553 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2554 static _LIBCPP_CONSTEXPR const size_t short_lag = __s;
2555 static _LIBCPP_CONSTEXPR const size_t long_lag = __r;
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002557 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002559 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2560 static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002561
2562 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564 explicit subtract_with_carry_engine(result_type __sd = default_seed)
2565 {seed(__sd);}
Howard Hinnantec3773c2011-12-01 20:21:04 +00002566 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00002568 explicit subtract_with_carry_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002569 typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570 {seed(__q);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002572 void seed(result_type __sd = default_seed)
2573 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2574 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002576 typename enable_if
2577 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002578 __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002579 void
2580 >::type
2581 seed(_Sseq& __q)
2582 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2583
2584 // generating functions
2585 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002587 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2588
Howard Hinnant99968442011-11-29 18:15:50 +00002589 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002590 friend
2591 bool
2592 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002593 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2594 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002595
Howard Hinnant99968442011-11-29 18:15:50 +00002596 template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002597 friend
2598 bool
2599 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002600 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2601 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002602
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002603 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002604 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 friend
2606 basic_ostream<_CharT, _Traits>&
2607 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002608 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002609
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002610 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002611 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002612 friend
2613 basic_istream<_CharT, _Traits>&
2614 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002615 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002616
2617private:
2618
2619 void seed(result_type __sd, integral_constant<unsigned, 1>);
2620 void seed(result_type __sd, integral_constant<unsigned, 2>);
2621 template<class _Sseq>
2622 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2623 template<class _Sseq>
2624 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2625};
2626
2627template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant0a69fa12012-12-12 21:14:28 +00002628 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
2629
2630template<class _UIntType, size_t __w, size_t __s, size_t __r>
2631 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
2632
2633template<class _UIntType, size_t __w, size_t __s, size_t __r>
2634 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
2635
2636template<class _UIntType, size_t __w, size_t __s, size_t __r>
2637 _LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type
2638 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
2639
2640template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002641void
2642subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2643 integral_constant<unsigned, 1>)
2644{
2645 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2646 __e(__sd == 0u ? default_seed : __sd);
2647 for (size_t __i = 0; __i < __r; ++__i)
2648 __x_[__i] = static_cast<result_type>(__e() & _Max);
2649 __c_ = __x_[__r-1] == 0;
2650 __i_ = 0;
2651}
2652
2653template<class _UIntType, size_t __w, size_t __s, size_t __r>
2654void
2655subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2656 integral_constant<unsigned, 2>)
2657{
2658 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2659 __e(__sd == 0u ? default_seed : __sd);
2660 for (size_t __i = 0; __i < __r; ++__i)
Howard Hinnant43edf2d2011-08-15 17:22:22 +00002661 {
2662 result_type __e0 = __e();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663 __x_[__i] = static_cast<result_type>(
Howard Hinnant43edf2d2011-08-15 17:22:22 +00002664 (__e0 + ((uint64_t)__e() << 32)) & _Max);
2665 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666 __c_ = __x_[__r-1] == 0;
2667 __i_ = 0;
2668}
2669
2670template<class _UIntType, size_t __w, size_t __s, size_t __r>
2671template<class _Sseq>
2672void
2673subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2674 integral_constant<unsigned, 1>)
2675{
2676 const unsigned __k = 1;
2677 uint32_t __ar[__r * __k];
2678 __q.generate(__ar, __ar + __r * __k);
2679 for (size_t __i = 0; __i < __r; ++__i)
2680 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2681 __c_ = __x_[__r-1] == 0;
2682 __i_ = 0;
2683}
2684
2685template<class _UIntType, size_t __w, size_t __s, size_t __r>
2686template<class _Sseq>
2687void
2688subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2689 integral_constant<unsigned, 2>)
2690{
2691 const unsigned __k = 2;
2692 uint32_t __ar[__r * __k];
2693 __q.generate(__ar, __ar + __r * __k);
2694 for (size_t __i = 0; __i < __r; ++__i)
2695 __x_[__i] = static_cast<result_type>(
2696 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2697 __c_ = __x_[__r-1] == 0;
2698 __i_ = 0;
2699}
2700
2701template<class _UIntType, size_t __w, size_t __s, size_t __r>
2702_UIntType
2703subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2704{
2705 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2706 result_type& __xr = __x_[__i_];
2707 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2708 __xr = (__xs - __xr - __c_) & _Max;
2709 __c_ = __new_c;
2710 __i_ = (__i_ + 1) % __r;
2711 return __xr;
2712}
2713
Howard Hinnant99968442011-11-29 18:15:50 +00002714template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715bool
2716operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002717 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2718 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719{
2720 if (__x.__c_ != __y.__c_)
2721 return false;
2722 if (__x.__i_ == __y.__i_)
Howard Hinnant99968442011-11-29 18:15:50 +00002723 return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002724 if (__x.__i_ == 0 || __y.__i_ == 0)
2725 {
Howard Hinnant99968442011-11-29 18:15:50 +00002726 size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002727 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728 __y.__x_ + __y.__i_))
2729 return false;
2730 if (__x.__i_ == 0)
Howard Hinnant99968442011-11-29 18:15:50 +00002731 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
2732 return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002733 }
2734 if (__x.__i_ < __y.__i_)
2735 {
Howard Hinnant99968442011-11-29 18:15:50 +00002736 size_t __j = _Rp - __y.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002737 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738 __y.__x_ + __y.__i_))
2739 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002740 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002741 __y.__x_))
2742 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002743 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002744 __y.__x_ + (_Rp - (__x.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002745 }
Howard Hinnant99968442011-11-29 18:15:50 +00002746 size_t __j = _Rp - __x.__i_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002747 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748 __x.__x_ + __x.__i_))
2749 return false;
Howard Hinnant99968442011-11-29 18:15:50 +00002750 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751 __x.__x_))
2752 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002753 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnant99968442011-11-29 18:15:50 +00002754 __x.__x_ + (_Rp - (__y.__i_ + __j)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002755}
2756
Howard Hinnant99968442011-11-29 18:15:50 +00002757template<class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002758inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002759bool
2760operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002761 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x,
2762 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763{
2764 return !(__x == __y);
2765}
2766
2767template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002768 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002769basic_ostream<_CharT, _Traits>&
2770operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002771 const subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002772{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002773 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002774 __os.flags(ios_base::dec | ios_base::left);
2775 _CharT __sp = __os.widen(' ');
2776 __os.fill(__sp);
2777 __os << __x.__x_[__x.__i_];
Howard Hinnant99968442011-11-29 18:15:50 +00002778 for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002779 __os << __sp << __x.__x_[__j];
2780 for (size_t __j = 0; __j < __x.__i_; ++__j)
2781 __os << __sp << __x.__x_[__j];
2782 __os << __sp << __x.__c_;
2783 return __os;
2784}
2785
2786template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002787 class _UI, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002788basic_istream<_CharT, _Traits>&
2789operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002790 subtract_with_carry_engine<_UI, _Wp, _Sp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002792 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793 __is.flags(ios_base::dec | ios_base::skipws);
Howard Hinnant99968442011-11-29 18:15:50 +00002794 _UI __t[_Rp+1];
2795 for (size_t __i = 0; __i < _Rp+1; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002796 __is >> __t[__i];
2797 if (!__is.fail())
2798 {
Howard Hinnant99968442011-11-29 18:15:50 +00002799 for (size_t __i = 0; __i < _Rp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002800 __x.__x_[__i] = __t[__i];
Howard Hinnant99968442011-11-29 18:15:50 +00002801 __x.__c_ = __t[_Rp];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002802 __x.__i_ = 0;
2803 }
2804 return __is;
2805}
2806
2807typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2808typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2809
2810// discard_block_engine
2811
2812template<class _Engine, size_t __p, size_t __r>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002813class _LIBCPP_TYPE_VIS_ONLY discard_block_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002814{
2815 _Engine __e_;
2816 int __n_;
2817
2818 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2819 static_assert(__r <= __p, "discard_block_engine invalid parameters");
Eric Fiselier50f65792016-12-24 00:24:44 +00002820 static_assert(__r <= INT_MAX, "discard_block_engine invalid parameters");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002821public:
2822 // types
2823 typedef typename _Engine::result_type result_type;
2824
2825 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002826 static _LIBCPP_CONSTEXPR const size_t block_size = __p;
2827 static _LIBCPP_CONSTEXPR const size_t used_block = __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002828
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002829#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002830 static const result_type _Min = _Engine::_Min;
2831 static const result_type _Max = _Engine::_Max;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002832#else
2833 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
2834 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
2835#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002836
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002838 static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002840 static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841
2842 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002844 discard_block_engine() : __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002846 explicit discard_block_engine(const _Engine& __e)
2847 : __e_(__e), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002848#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002850 explicit discard_block_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002851 : __e_(_VSTD::move(__e)), __n_(0) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002852#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002854 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002855 template<class _Sseq>
2856 _LIBCPP_INLINE_VISIBILITY
2857 explicit discard_block_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002858 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00002859 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002860 : __e_(__q), __n_(0) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002862 void seed() {__e_.seed(); __n_ = 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant3ec31842010-05-28 15:49:54 +00002865 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00002867 typename enable_if
2868 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00002869 __is_seed_sequence<_Sseq, discard_block_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00002870 void
2871 >::type
2872 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002873
2874 // generating functions
2875 result_type operator()();
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002877 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2878
2879 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00002881 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002882
Howard Hinnant99968442011-11-29 18:15:50 +00002883 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002884 friend
2885 bool
2886 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00002887 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2888 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002889
Howard Hinnant99968442011-11-29 18:15:50 +00002890 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002891 friend
2892 bool
2893 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00002894 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2895 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00002896
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002897 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002898 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002899 friend
2900 basic_ostream<_CharT, _Traits>&
2901 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002902 const discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00002903
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002904 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002905 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002906 friend
2907 basic_istream<_CharT, _Traits>&
2908 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002909 discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002910};
2911
2912template<class _Engine, size_t __p, size_t __r>
Howard Hinnant0a69fa12012-12-12 21:14:28 +00002913 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
2914
2915template<class _Engine, size_t __p, size_t __r>
2916 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
2917
2918template<class _Engine, size_t __p, size_t __r>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002919typename discard_block_engine<_Engine, __p, __r>::result_type
2920discard_block_engine<_Engine, __p, __r>::operator()()
2921{
Eric Fiselier50f65792016-12-24 00:24:44 +00002922 if (__n_ >= static_cast<int>(__r))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002923 {
2924 __e_.discard(__p - __r);
2925 __n_ = 0;
2926 }
2927 ++__n_;
2928 return __e_();
2929}
2930
Howard Hinnant99968442011-11-29 18:15:50 +00002931template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002932inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002933bool
Howard Hinnant99968442011-11-29 18:15:50 +00002934operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2935 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002936{
2937 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2938}
2939
Howard Hinnant99968442011-11-29 18:15:50 +00002940template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00002941inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002942bool
Howard Hinnant99968442011-11-29 18:15:50 +00002943operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2944 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002945{
2946 return !(__x == __y);
2947}
2948
2949template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002950 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002951basic_ostream<_CharT, _Traits>&
2952operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00002953 const discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002954{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002955 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002956 __os.flags(ios_base::dec | ios_base::left);
2957 _CharT __sp = __os.widen(' ');
2958 __os.fill(__sp);
2959 return __os << __x.__e_ << __sp << __x.__n_;
2960}
2961
2962template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00002963 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002964basic_istream<_CharT, _Traits>&
2965operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00002966 discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002967{
Howard Hinnant9c0df142012-10-30 19:06:59 +00002968 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002969 __is.flags(ios_base::dec | ios_base::skipws);
2970 _Eng __e;
2971 int __n;
2972 __is >> __e >> __n;
2973 if (!__is.fail())
2974 {
2975 __x.__e_ = __e;
2976 __x.__n_ = __n;
2977 }
2978 return __is;
2979}
2980
2981typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2982typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2983
2984// independent_bits_engine
2985
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002986template<class _Engine, size_t __w, class _UIntType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002987class _LIBCPP_TYPE_VIS_ONLY independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002988{
Howard Hinnant99968442011-11-29 18:15:50 +00002989 template <class _UI, _UI _R0, size_t _Wp, size_t _Mp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002990 class __get_n
2991 {
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002992 static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UI>::digits;
2993 static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
2994 static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
2995 static _LIBCPP_CONSTEXPR const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002996 public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002997 static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002998 };
2999public:
3000 // types
3001 typedef _UIntType result_type;
3002
3003private:
3004 _Engine __e_;
3005
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003006 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003007 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
3008 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
3009
3010 typedef typename _Engine::result_type _Engine_result_type;
3011 typedef typename conditional
3012 <
3013 sizeof(_Engine_result_type) <= sizeof(result_type),
3014 result_type,
3015 _Engine_result_type
3016 >::type _Working_result_type;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003017#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant99968442011-11-29 18:15:50 +00003018 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003019 + _Working_result_type(1);
3020#else
3021 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
3022 + _Working_result_type(1);
3023#endif
3024 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
3025 static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
3026 static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
3027 static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
3028 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
3029 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
3030 static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
3031 (_Rp >> __w0) << __w0;
3032 static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
3033 (_Rp >> (__w0+1)) << (__w0+1);
3034 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003035 _Engine_result_type(~0) >> (_EDt - __w0) :
3036 _Engine_result_type(0);
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003037 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003038 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
3039 _Engine_result_type(~0);
3040public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003041 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3042 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
3043 (result_type(1) << __w) - result_type(1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003044 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
3045
3046 // engine characteristics
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003048 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003050 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003051
3052 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003054 independent_bits_engine() {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003056 explicit independent_bits_engine(const _Engine& __e)
3057 : __e_(__e) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003058#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003060 explicit independent_bits_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003061 : __e_(_VSTD::move(__e)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003062#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003064 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnantec3773c2011-12-01 20:21:04 +00003065 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00003067 explicit independent_bits_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003068 typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00003069 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003070 : __e_(__q) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003072 void seed() {__e_.seed();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003074 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003075 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003077 typename enable_if
3078 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003079 __is_seed_sequence<_Sseq, independent_bits_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00003080 void
3081 >::type
3082 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003083
3084 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003085 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003086 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3089
3090 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003091 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00003092 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003093
Howard Hinnant99968442011-11-29 18:15:50 +00003094 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003095 friend
3096 bool
3097 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003098 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3099 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003100
Howard Hinnant99968442011-11-29 18:15:50 +00003101 template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003102 friend
3103 bool
3104 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003105 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3106 const independent_bits_engine<_Eng, _Wp, _UI>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003107
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003108 template <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 +00003110 friend
3111 basic_ostream<_CharT, _Traits>&
3112 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003113 const independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003114
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003115 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003116 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003117 friend
3118 basic_istream<_CharT, _Traits>&
3119 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003120 independent_bits_engine<_Eng, _Wp, _UI>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003121
3122private:
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003124 result_type __eval(false_type);
3125 result_type __eval(true_type);
3126
3127 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003129 static
3130 typename enable_if
3131 <
3132 __count < _Dt,
3133 result_type
3134 >::type
3135 __lshift(result_type __x) {return __x << __count;}
3136
3137 template <size_t __count>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003139 static
3140 typename enable_if
3141 <
3142 (__count >= _Dt),
3143 result_type
3144 >::type
Howard Hinnantec3773c2011-12-01 20:21:04 +00003145 __lshift(result_type) {return result_type(0);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003146};
3147
3148template<class _Engine, size_t __w, class _UIntType>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003149inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003150_UIntType
3151independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
3152{
3153 return static_cast<result_type>(__e_() & __mask0);
3154}
3155
3156template<class _Engine, size_t __w, class _UIntType>
3157_UIntType
3158independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
3159{
Howard Hinnant99968442011-11-29 18:15:50 +00003160 result_type _Sp = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003161 for (size_t __k = 0; __k < __n0; ++__k)
3162 {
3163 _Engine_result_type __u;
3164 do
3165 {
3166 __u = __e_() - _Engine::min();
3167 } while (__u >= __y0);
Howard Hinnant99968442011-11-29 18:15:50 +00003168 _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003169 }
3170 for (size_t __k = __n0; __k < __n; ++__k)
3171 {
3172 _Engine_result_type __u;
3173 do
3174 {
3175 __u = __e_() - _Engine::min();
3176 } while (__u >= __y1);
Howard Hinnant99968442011-11-29 18:15:50 +00003177 _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003178 }
Howard Hinnant99968442011-11-29 18:15:50 +00003179 return _Sp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003180}
3181
Howard Hinnant99968442011-11-29 18:15:50 +00003182template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003183inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003184bool
3185operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003186 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3187 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003188{
3189 return __x.base() == __y.base();
3190}
3191
Howard Hinnant99968442011-11-29 18:15:50 +00003192template<class _Eng, size_t _Wp, class _UI>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003193inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003194bool
3195operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003196 const independent_bits_engine<_Eng, _Wp, _UI>& __x,
3197 const independent_bits_engine<_Eng, _Wp, _UI>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003198{
3199 return !(__x == __y);
3200}
3201
3202template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003203 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003204basic_ostream<_CharT, _Traits>&
3205operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003206 const independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003207{
3208 return __os << __x.base();
3209}
3210
3211template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003212 class _Eng, size_t _Wp, class _UI>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003213basic_istream<_CharT, _Traits>&
3214operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003215 independent_bits_engine<_Eng, _Wp, _UI>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003216{
3217 _Eng __e;
3218 __is >> __e;
3219 if (!__is.fail())
3220 __x.__e_ = __e;
3221 return __is;
3222}
3223
3224// shuffle_order_engine
3225
3226template <uint64_t _Xp, uint64_t _Yp>
3227struct __ugcd
3228{
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003229 static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003230};
3231
3232template <uint64_t _Xp>
3233struct __ugcd<_Xp, 0>
3234{
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003235 static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003236};
3237
Howard Hinnant99968442011-11-29 18:15:50 +00003238template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003239class __uratio
3240{
Howard Hinnant99968442011-11-29 18:15:50 +00003241 static_assert(_Dp != 0, "__uratio divide by 0");
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003242 static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003243public:
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003244 static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
3245 static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003246
3247 typedef __uratio<num, den> type;
3248};
3249
3250template<class _Engine, size_t __k>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003251class _LIBCPP_TYPE_VIS_ONLY shuffle_order_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003252{
3253 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3254public:
3255 // types
3256 typedef typename _Engine::result_type result_type;
3257
3258private:
3259 _Engine __e_;
3260 result_type _V_[__k];
3261 result_type _Y_;
3262
3263public:
3264 // engine characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003265 static _LIBCPP_CONSTEXPR const size_t table_size = __k;
Howard Hinnant324bb032010-08-22 00:02:43 +00003266
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003267#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003268 static const result_type _Min = _Engine::_Min;
3269 static const result_type _Max = _Engine::_Max;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003270#else
3271 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
3272 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
3273#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003274 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003276 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003278 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003279
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003280 static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003281
3282 // constructors and seeding functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003284 shuffle_order_engine() {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003286 explicit shuffle_order_engine(const _Engine& __e)
3287 : __e_(__e) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003288#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003290 explicit shuffle_order_engine(_Engine&& __e)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003291 : __e_(_VSTD::move(__e)) {__init();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00003292#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003294 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnantec3773c2011-12-01 20:21:04 +00003295 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00003297 explicit shuffle_order_engine(_Sseq& __q,
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003298 typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
Howard Hinnant3ec31842010-05-28 15:49:54 +00003299 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003300 : __e_(__q) {__init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003302 void seed() {__e_.seed(); __init();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003304 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant3ec31842010-05-28 15:49:54 +00003305 template<class _Sseq>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3ec31842010-05-28 15:49:54 +00003307 typename enable_if
3308 <
Howard Hinnantef3b2e22011-04-11 18:22:12 +00003309 __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
Howard Hinnant3ec31842010-05-28 15:49:54 +00003310 void
3311 >::type
3312 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003313
3314 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003316 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003318 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3319
3320 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc83960a2012-07-20 21:44:27 +00003322 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003323
3324private:
Howard Hinnant99968442011-11-29 18:15:50 +00003325 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003326 friend
3327 bool
3328 operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003329 const shuffle_order_engine<_Eng, _Kp>& __x,
3330 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003331
Howard Hinnant99968442011-11-29 18:15:50 +00003332 template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003333 friend
3334 bool
3335 operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003336 const shuffle_order_engine<_Eng, _Kp>& __x,
3337 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00003338
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003339 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003340 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003341 friend
3342 basic_ostream<_CharT, _Traits>&
3343 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003344 const shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00003345
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003346 template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003347 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003348 friend
3349 basic_istream<_CharT, _Traits>&
3350 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003351 shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003352
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003354 void __init()
3355 {
3356 for (size_t __i = 0; __i < __k; ++__i)
3357 _V_[__i] = __e_();
3358 _Y_ = __e_();
3359 }
3360
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003362 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003364 result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003365
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003367 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003369 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3370
Howard Hinnant99968442011-11-29 18:15:50 +00003371 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003373 typename enable_if
3374 <
Howard Hinnant99968442011-11-29 18:15:50 +00003375 (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003376 result_type
3377 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00003378 __eval(__uratio<_Np, _Dp>)
3379 {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003380
Howard Hinnant99968442011-11-29 18:15:50 +00003381 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003383 typename enable_if
3384 <
Howard Hinnant99968442011-11-29 18:15:50 +00003385 __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003386 result_type
3387 >::type
Howard Hinnant99968442011-11-29 18:15:50 +00003388 __eval(__uratio<_Np, _Dp>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003389 {
Howard Hinnant99968442011-11-29 18:15:50 +00003390 const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
3391 / __uratio<_Np, _Dp>::den);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003392 _Y_ = _V_[__j];
3393 _V_[__j] = __e_();
3394 return _Y_;
3395 }
3396
3397 template <uint64_t __n, uint64_t __d>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003399 result_type __evalf()
3400 {
Howard Hinnant99968442011-11-29 18:15:50 +00003401 const double _Fp = __d == 0 ?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003402 __n / (2. * 0x8000000000000000ull) :
3403 __n / (double)__d;
Howard Hinnant99968442011-11-29 18:15:50 +00003404 const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003405 _Y_ = _V_[__j];
3406 _V_[__j] = __e_();
3407 return _Y_;
3408 }
3409};
3410
Howard Hinnant0a69fa12012-12-12 21:14:28 +00003411template<class _Engine, size_t __k>
3412 _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size;
3413
Howard Hinnant99968442011-11-29 18:15:50 +00003414template<class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003415bool
3416operator==(
Howard Hinnant99968442011-11-29 18:15:50 +00003417 const shuffle_order_engine<_Eng, _Kp>& __x,
3418 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003419{
Howard Hinnant99968442011-11-29 18:15:50 +00003420 return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003421 __x.__e_ == __y.__e_;
3422}
3423
Howard Hinnant99968442011-11-29 18:15:50 +00003424template<class _Eng, size_t _Kp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003425inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003426bool
3427operator!=(
Howard Hinnant99968442011-11-29 18:15:50 +00003428 const shuffle_order_engine<_Eng, _Kp>& __x,
3429 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003430{
3431 return !(__x == __y);
3432}
3433
3434template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003435 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436basic_ostream<_CharT, _Traits>&
3437operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnant99968442011-11-29 18:15:50 +00003438 const shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003439{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003440 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003441 __os.flags(ios_base::dec | ios_base::left);
3442 _CharT __sp = __os.widen(' ');
3443 __os.fill(__sp);
3444 __os << __x.__e_ << __sp << __x._V_[0];
Howard Hinnant99968442011-11-29 18:15:50 +00003445 for (size_t __i = 1; __i < _Kp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003446 __os << __sp << __x._V_[__i];
3447 return __os << __sp << __x._Y_;
3448}
3449
3450template <class _CharT, class _Traits,
Howard Hinnant99968442011-11-29 18:15:50 +00003451 class _Eng, size_t _Kp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003452basic_istream<_CharT, _Traits>&
3453operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnant99968442011-11-29 18:15:50 +00003454 shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003455{
Howard Hinnant99968442011-11-29 18:15:50 +00003456 typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003457 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003458 __is.flags(ios_base::dec | ios_base::skipws);
3459 _Eng __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003460 result_type _Vp[_Kp+1];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003461 __is >> __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003462 for (size_t __i = 0; __i < _Kp+1; ++__i)
3463 __is >> _Vp[__i];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003464 if (!__is.fail())
3465 {
3466 __x.__e_ = __e;
Howard Hinnant99968442011-11-29 18:15:50 +00003467 for (size_t __i = 0; __i < _Kp; ++__i)
3468 __x._V_[__i] = _Vp[__i];
3469 __x._Y_ = _Vp[_Kp];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003470 }
3471 return __is;
3472}
3473
3474typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3475
3476// random_device
3477
Howard Hinnant83eade62013-03-06 23:30:19 +00003478class _LIBCPP_TYPE_VIS random_device
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003479{
Ed Schouten63e70b62015-03-10 07:46:06 +00003480#ifdef _LIBCPP_USING_DEV_RANDOM
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003481 int __f_;
Ed Schouten63e70b62015-03-10 07:46:06 +00003482#endif // defined(_LIBCPP_USING_DEV_RANDOM)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003483public:
3484 // types
3485 typedef unsigned result_type;
3486
3487 // generator characteristics
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003488 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3489 static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003490
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003492 static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003494 static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003495
3496 // constructors
3497 explicit random_device(const string& __token = "/dev/urandom");
3498 ~random_device();
3499
3500 // generating functions
3501 result_type operator()();
3502
3503 // property functions
Howard Hinnantc83960a2012-07-20 21:44:27 +00003504 double entropy() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003505
3506private:
3507 // no copy functions
3508 random_device(const random_device&); // = delete;
3509 random_device& operator=(const random_device&); // = delete;
3510};
3511
3512// seed_seq
3513
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003514class _LIBCPP_TYPE_VIS_ONLY seed_seq
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003515{
3516public:
3517 // types
3518 typedef uint32_t result_type;
3519
3520private:
3521 vector<result_type> __v_;
3522
3523 template<class _InputIterator>
3524 void init(_InputIterator __first, _InputIterator __last);
3525public:
3526 // constructors
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003527 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65ccddb2013-10-23 05:56:47 +00003528 seed_seq() _NOEXCEPT {}
Howard Hinnante3e32912011-08-12 21:56:02 +00003529#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003530 template<class _Tp>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003532 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00003533#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant324bb032010-08-22 00:02:43 +00003534
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003535 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003537 seed_seq(_InputIterator __first, _InputIterator __last)
3538 {init(__first, __last);}
3539
3540 // generating functions
3541 template<class _RandomAccessIterator>
3542 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3543
3544 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003545 _LIBCPP_INLINE_VISIBILITY
Marshall Clow65ccddb2013-10-23 05:56:47 +00003546 size_t size() const _NOEXCEPT {return __v_.size();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003547 template<class _OutputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003549 void param(_OutputIterator __dest) const
Howard Hinnant0949eed2011-06-30 21:18:19 +00003550 {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003551
3552private:
3553 // no copy functions
3554 seed_seq(const seed_seq&); // = delete;
3555 void operator=(const seed_seq&); // = delete;
3556
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003558 static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003559};
3560
3561template<class _InputIterator>
3562void
3563seed_seq::init(_InputIterator __first, _InputIterator __last)
3564{
3565 for (_InputIterator __s = __first; __s != __last; ++__s)
3566 __v_.push_back(*__s & 0xFFFFFFFF);
3567}
3568
3569template<class _RandomAccessIterator>
3570void
3571seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3572{
3573 if (__first != __last)
3574 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003575 _VSTD::fill(__first, __last, 0x8b8b8b8b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576 const size_t __n = static_cast<size_t>(__last - __first);
3577 const size_t __s = __v_.size();
3578 const size_t __t = (__n >= 623) ? 11
3579 : (__n >= 68) ? 7
3580 : (__n >= 39) ? 5
3581 : (__n >= 7) ? 3
3582 : (__n - 1) / 2;
3583 const size_t __p = (__n - __t) / 2;
3584 const size_t __q = __p + __t;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003585 const size_t __m = _VSTD::max(__s + 1, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586 // __k = 0;
3587 {
Howard Hinnant99968442011-11-29 18:15:50 +00003588 result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003589 ^ __first[__n - 1]);
3590 __first[__p] += __r;
3591 __r += __s;
3592 __first[__q] += __r;
3593 __first[0] = __r;
3594 }
3595 for (size_t __k = 1; __k <= __s; ++__k)
3596 {
3597 const size_t __kmodn = __k % __n;
3598 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003599 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600 ^ __first[(__k - 1) % __n]);
3601 __first[__kpmodn] += __r;
3602 __r += __kmodn + __v_[__k-1];
3603 __first[(__k + __q) % __n] += __r;
3604 __first[__kmodn] = __r;
3605 }
3606 for (size_t __k = __s + 1; __k < __m; ++__k)
3607 {
3608 const size_t __kmodn = __k % __n;
3609 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003610 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003611 ^ __first[(__k - 1) % __n]);
3612 __first[__kpmodn] += __r;
3613 __r += __kmodn;
3614 __first[(__k + __q) % __n] += __r;
3615 __first[__kmodn] = __r;
3616 }
3617 for (size_t __k = __m; __k < __m + __n; ++__k)
3618 {
3619 const size_t __kmodn = __k % __n;
3620 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnant99968442011-11-29 18:15:50 +00003621 result_type __r = 1566083941 * _Tp(__first[__kmodn] +
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003622 __first[__kpmodn] +
3623 __first[(__k - 1) % __n]);
3624 __first[__kpmodn] ^= __r;
3625 __r -= __kmodn;
3626 __first[(__k + __q) % __n] ^= __r;
3627 __first[__kmodn] = __r;
3628 }
3629 }
3630}
3631
Howard Hinnant30a840f2010-05-12 17:08:57 +00003632// generate_canonical
3633
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003634template<class _RealType, size_t __bits, class _URNG>
3635_RealType
3636generate_canonical(_URNG& __g)
3637{
3638 const size_t _Dt = numeric_limits<_RealType>::digits;
3639 const size_t __b = _Dt < __bits ? _Dt : __bits;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003640#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003641 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003642#else
3643 const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
3644#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003646 const _RealType _Rp = _URNG::max() - _URNG::min() + _RealType(1);
Howard Hinnant99968442011-11-29 18:15:50 +00003647 _RealType __base = _Rp;
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003648 _RealType _Sp = __g() - _URNG::min();
Howard Hinnant99968442011-11-29 18:15:50 +00003649 for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp)
Howard Hinnant8efd3da2012-04-02 21:00:45 +00003650 _Sp += (__g() - _URNG::min()) * __base;
Howard Hinnant99968442011-11-29 18:15:50 +00003651 return _Sp / __base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003652}
3653
Howard Hinnant30a840f2010-05-12 17:08:57 +00003654// uniform_int_distribution
3655
Howard Hinnantc3267212010-05-26 17:49:34 +00003656// in <algorithm>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657
3658template <class _CharT, class _Traits, class _IT>
3659basic_ostream<_CharT, _Traits>&
3660operator<<(basic_ostream<_CharT, _Traits>& __os,
3661 const uniform_int_distribution<_IT>& __x)
3662{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003663 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003664 __os.flags(ios_base::dec | ios_base::left);
3665 _CharT __sp = __os.widen(' ');
3666 __os.fill(__sp);
3667 return __os << __x.a() << __sp << __x.b();
3668}
3669
3670template <class _CharT, class _Traits, class _IT>
3671basic_istream<_CharT, _Traits>&
3672operator>>(basic_istream<_CharT, _Traits>& __is,
3673 uniform_int_distribution<_IT>& __x)
3674{
3675 typedef uniform_int_distribution<_IT> _Eng;
3676 typedef typename _Eng::result_type result_type;
3677 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003678 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003679 __is.flags(ios_base::dec | ios_base::skipws);
3680 result_type __a;
3681 result_type __b;
3682 __is >> __a >> __b;
3683 if (!__is.fail())
3684 __x.param(param_type(__a, __b));
3685 return __is;
3686}
3687
Howard Hinnant30a840f2010-05-12 17:08:57 +00003688// uniform_real_distribution
3689
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003690template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003691class _LIBCPP_TYPE_VIS_ONLY uniform_real_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003692{
3693public:
3694 // types
3695 typedef _RealType result_type;
3696
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003697 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003698 {
3699 result_type __a_;
3700 result_type __b_;
3701 public:
3702 typedef uniform_real_distribution distribution_type;
3703
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003705 explicit param_type(result_type __a = 0,
3706 result_type __b = 1)
3707 : __a_(__a), __b_(__b) {}
3708
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003710 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003712 result_type b() const {return __b_;}
3713
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003714 friend _LIBCPP_INLINE_VISIBILITY
3715 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003716 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003717 friend _LIBCPP_INLINE_VISIBILITY
3718 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003719 {return !(__x == __y);}
3720 };
3721
3722private:
3723 param_type __p_;
3724
3725public:
3726 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3729 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003731 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003733 void reset() {}
3734
3735 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003736 template<class _URNG>
3737 _LIBCPP_INLINE_VISIBILITY
3738 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003739 {return (*this)(__g, __p_);}
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003740 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003741
3742 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003744 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003746 result_type b() const {return __p_.b();}
3747
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003749 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751 void param(const param_type& __p) {__p_ = __p;}
3752
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003754 result_type min() const {return a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003756 result_type max() const {return b();}
3757
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003758 friend _LIBCPP_INLINE_VISIBILITY
3759 bool operator==(const uniform_real_distribution& __x,
3760 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003761 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003762 friend _LIBCPP_INLINE_VISIBILITY
3763 bool operator!=(const uniform_real_distribution& __x,
3764 const uniform_real_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003765 {return !(__x == __y);}
3766};
3767
3768template<class _RealType>
3769template<class _URNG>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003770inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003771typename uniform_real_distribution<_RealType>::result_type
3772uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3773{
3774 return (__p.b() - __p.a())
Howard Hinnant0949eed2011-06-30 21:18:19 +00003775 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003776 + __p.a();
3777}
3778
3779template <class _CharT, class _Traits, class _RT>
3780basic_ostream<_CharT, _Traits>&
3781operator<<(basic_ostream<_CharT, _Traits>& __os,
3782 const uniform_real_distribution<_RT>& __x)
3783{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003784 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003785 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3786 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003787 _CharT __sp = __os.widen(' ');
3788 __os.fill(__sp);
3789 return __os << __x.a() << __sp << __x.b();
3790}
3791
3792template <class _CharT, class _Traits, class _RT>
3793basic_istream<_CharT, _Traits>&
3794operator>>(basic_istream<_CharT, _Traits>& __is,
3795 uniform_real_distribution<_RT>& __x)
3796{
3797 typedef uniform_real_distribution<_RT> _Eng;
3798 typedef typename _Eng::result_type result_type;
3799 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003800 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003801 __is.flags(ios_base::dec | ios_base::skipws);
3802 result_type __a;
3803 result_type __b;
3804 __is >> __a >> __b;
3805 if (!__is.fail())
3806 __x.param(param_type(__a, __b));
3807 return __is;
3808}
3809
Howard Hinnant30a840f2010-05-12 17:08:57 +00003810// bernoulli_distribution
3811
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003812class _LIBCPP_TYPE_VIS_ONLY bernoulli_distribution
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003813{
3814public:
3815 // types
3816 typedef bool result_type;
3817
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003818 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003819 {
3820 double __p_;
3821 public:
3822 typedef bernoulli_distribution distribution_type;
3823
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003824 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825 explicit param_type(double __p = 0.5) : __p_(__p) {}
3826
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003828 double p() const {return __p_;}
3829
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003830 friend _LIBCPP_INLINE_VISIBILITY
3831 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003832 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003833 friend _LIBCPP_INLINE_VISIBILITY
3834 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835 {return !(__x == __y);}
3836 };
3837
3838private:
3839 param_type __p_;
3840
3841public:
3842 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003844 explicit bernoulli_distribution(double __p = 0.5)
3845 : __p_(param_type(__p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003847 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003849 void reset() {}
3850
3851 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003852 template<class _URNG>
3853 _LIBCPP_INLINE_VISIBILITY
3854 result_type operator()(_URNG& __g)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003855 {return (*this)(__g, __p_);}
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003856 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003857
3858 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003860 double p() const {return __p_.p();}
3861
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003863 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003865 void param(const param_type& __p) {__p_ = __p;}
3866
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003868 result_type min() const {return false;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003870 result_type max() const {return true;}
3871
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003872 friend _LIBCPP_INLINE_VISIBILITY
3873 bool operator==(const bernoulli_distribution& __x,
3874 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003875 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003876 friend _LIBCPP_INLINE_VISIBILITY
3877 bool operator!=(const bernoulli_distribution& __x,
3878 const bernoulli_distribution& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003879 {return !(__x == __y);}
3880};
3881
Howard Hinnant03aad812010-05-11 23:26:59 +00003882template<class _URNG>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003883inline
Howard Hinnant03aad812010-05-11 23:26:59 +00003884bernoulli_distribution::result_type
3885bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3886{
Howard Hinnantd6d11712010-05-20 15:11:46 +00003887 uniform_real_distribution<double> __gen;
3888 return __gen(__g) < __p.p();
Howard Hinnant03aad812010-05-11 23:26:59 +00003889}
3890
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003891template <class _CharT, class _Traits>
3892basic_ostream<_CharT, _Traits>&
3893operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3894{
Howard Hinnant9c0df142012-10-30 19:06:59 +00003895 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003896 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3897 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003898 _CharT __sp = __os.widen(' ');
3899 __os.fill(__sp);
3900 return __os << __x.p();
3901}
3902
3903template <class _CharT, class _Traits>
3904basic_istream<_CharT, _Traits>&
3905operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3906{
3907 typedef bernoulli_distribution _Eng;
3908 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00003909 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003910 __is.flags(ios_base::dec | ios_base::skipws);
3911 double __p;
3912 __is >> __p;
3913 if (!__is.fail())
3914 __x.param(param_type(__p));
3915 return __is;
3916}
3917
Howard Hinnant30a840f2010-05-12 17:08:57 +00003918// binomial_distribution
3919
Howard Hinnant03aad812010-05-11 23:26:59 +00003920template<class _IntType = int>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003921class _LIBCPP_TYPE_VIS_ONLY binomial_distribution
Howard Hinnant03aad812010-05-11 23:26:59 +00003922{
3923public:
3924 // types
3925 typedef _IntType result_type;
3926
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003927 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant03aad812010-05-11 23:26:59 +00003928 {
3929 result_type __t_;
3930 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003931 double __pr_;
3932 double __odds_ratio_;
3933 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003934 public:
3935 typedef binomial_distribution distribution_type;
3936
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003937 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003938
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003940 result_type t() const {return __t_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003942 double p() const {return __p_;}
3943
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003944 friend _LIBCPP_INLINE_VISIBILITY
3945 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003946 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003947 friend _LIBCPP_INLINE_VISIBILITY
3948 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003949 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003950
3951 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003952 };
3953
3954private:
3955 param_type __p_;
3956
3957public:
3958 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003960 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3961 : __p_(param_type(__t, __p)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003963 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003965 void reset() {}
3966
3967 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003968 template<class _URNG>
3969 _LIBCPP_INLINE_VISIBILITY
3970 result_type operator()(_URNG& __g)
Howard Hinnant03aad812010-05-11 23:26:59 +00003971 {return (*this)(__g, __p_);}
3972 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3973
3974 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003976 result_type t() const {return __p_.t();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003978 double p() const {return __p_.p();}
3979
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003981 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003983 void param(const param_type& __p) {__p_ = __p;}
3984
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003986 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant03aad812010-05-11 23:26:59 +00003988 result_type max() const {return t();}
3989
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003990 friend _LIBCPP_INLINE_VISIBILITY
3991 bool operator==(const binomial_distribution& __x,
3992 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003993 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00003994 friend _LIBCPP_INLINE_VISIBILITY
3995 bool operator!=(const binomial_distribution& __x,
3996 const binomial_distribution& __y)
Howard Hinnant03aad812010-05-11 23:26:59 +00003997 {return !(__x == __y);}
3998};
3999
4000template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004001binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
4002 : __t_(__t), __p_(__p)
4003{
4004 if (0 < __p_ && __p_ < 1)
4005 {
4006 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004007 __pr_ = _VSTD::exp(_VSTD::lgamma(__t_ + 1.) - _VSTD::lgamma(__r0_ + 1.) -
4008 _VSTD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
4009 (__t_ - __r0_) * _VSTD::log(1 - __p_));
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004010 __odds_ratio_ = __p_ / (1 - __p_);
4011 }
4012}
4013
Marshall Clow5ffb8d02014-09-17 18:33:58 +00004014// Reference: Kemp, C.D. (1986). `A modal method for generating binomial
4015// variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004016template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00004017template<class _URNG>
4018_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004019binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00004020{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004021 if (__pr.__t_ == 0 || __pr.__p_ == 0)
4022 return 0;
4023 if (__pr.__p_ == 1)
4024 return __pr.__t_;
4025 uniform_real_distribution<double> __gen;
4026 double __u = __gen(__g) - __pr.__pr_;
4027 if (__u < 0)
4028 return __pr.__r0_;
4029 double __pu = __pr.__pr_;
4030 double __pd = __pu;
4031 result_type __ru = __pr.__r0_;
4032 result_type __rd = __ru;
4033 while (true)
4034 {
4035 if (__rd >= 1)
4036 {
4037 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
4038 __u -= __pd;
4039 if (__u < 0)
4040 return __rd - 1;
4041 }
Marshall Clow5ffb8d02014-09-17 18:33:58 +00004042 if ( __rd != 0 )
4043 --__rd;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004044 ++__ru;
4045 if (__ru <= __pr.__t_)
4046 {
4047 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
4048 __u -= __pu;
4049 if (__u < 0)
4050 return __ru;
4051 }
4052 }
Howard Hinnant03aad812010-05-11 23:26:59 +00004053}
4054
4055template <class _CharT, class _Traits, class _IntType>
4056basic_ostream<_CharT, _Traits>&
4057operator<<(basic_ostream<_CharT, _Traits>& __os,
4058 const binomial_distribution<_IntType>& __x)
4059{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004060 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004061 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4062 ios_base::scientific);
Howard Hinnant03aad812010-05-11 23:26:59 +00004063 _CharT __sp = __os.widen(' ');
4064 __os.fill(__sp);
4065 return __os << __x.t() << __sp << __x.p();
4066}
4067
4068template <class _CharT, class _Traits, class _IntType>
4069basic_istream<_CharT, _Traits>&
4070operator>>(basic_istream<_CharT, _Traits>& __is,
4071 binomial_distribution<_IntType>& __x)
4072{
4073 typedef binomial_distribution<_IntType> _Eng;
4074 typedef typename _Eng::result_type result_type;
4075 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004076 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant03aad812010-05-11 23:26:59 +00004077 __is.flags(ios_base::dec | ios_base::skipws);
4078 result_type __t;
4079 double __p;
4080 __is >> __t >> __p;
4081 if (!__is.fail())
4082 __x.param(param_type(__t, __p));
4083 return __is;
4084}
4085
Howard Hinnant30a840f2010-05-12 17:08:57 +00004086// exponential_distribution
4087
4088template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004089class _LIBCPP_TYPE_VIS_ONLY exponential_distribution
Howard Hinnant30a840f2010-05-12 17:08:57 +00004090{
4091public:
4092 // types
4093 typedef _RealType result_type;
4094
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004095 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant30a840f2010-05-12 17:08:57 +00004096 {
4097 result_type __lambda_;
4098 public:
4099 typedef exponential_distribution distribution_type;
4100
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004102 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
4103
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004105 result_type lambda() const {return __lambda_;}
4106
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004107 friend _LIBCPP_INLINE_VISIBILITY
4108 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004109 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004110 friend _LIBCPP_INLINE_VISIBILITY
4111 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004112 {return !(__x == __y);}
4113 };
4114
4115private:
4116 param_type __p_;
4117
4118public:
4119 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004121 explicit exponential_distribution(result_type __lambda = 1)
4122 : __p_(param_type(__lambda)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004124 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004126 void reset() {}
4127
4128 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004129 template<class _URNG>
4130 _LIBCPP_INLINE_VISIBILITY
4131 result_type operator()(_URNG& __g)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004132 {return (*this)(__g, __p_);}
4133 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4134
4135 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004137 result_type lambda() const {return __p_.lambda();}
4138
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004140 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004142 void param(const param_type& __p) {__p_ = __p;}
4143
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant30a840f2010-05-12 17:08:57 +00004145 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf40dc62010-05-16 17:56:20 +00004147 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00004148
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004149 friend _LIBCPP_INLINE_VISIBILITY
4150 bool operator==(const exponential_distribution& __x,
4151 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004152 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004153 friend _LIBCPP_INLINE_VISIBILITY
4154 bool operator!=(const exponential_distribution& __x,
4155 const exponential_distribution& __y)
Howard Hinnant30a840f2010-05-12 17:08:57 +00004156 {return !(__x == __y);}
4157};
4158
4159template <class _RealType>
4160template<class _URNG>
4161_RealType
4162exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4163{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004164 return -_VSTD::log
Howard Hinnant30a840f2010-05-12 17:08:57 +00004165 (
4166 result_type(1) -
Howard Hinnant0949eed2011-06-30 21:18:19 +00004167 _VSTD::generate_canonical<result_type,
Howard Hinnant30a840f2010-05-12 17:08:57 +00004168 numeric_limits<result_type>::digits>(__g)
4169 )
4170 / __p.lambda();
4171}
4172
4173template <class _CharT, class _Traits, class _RealType>
4174basic_ostream<_CharT, _Traits>&
4175operator<<(basic_ostream<_CharT, _Traits>& __os,
4176 const exponential_distribution<_RealType>& __x)
4177{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004178 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004179 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4180 ios_base::scientific);
Howard Hinnant30a840f2010-05-12 17:08:57 +00004181 return __os << __x.lambda();
4182}
4183
4184template <class _CharT, class _Traits, class _RealType>
4185basic_istream<_CharT, _Traits>&
4186operator>>(basic_istream<_CharT, _Traits>& __is,
4187 exponential_distribution<_RealType>& __x)
4188{
4189 typedef exponential_distribution<_RealType> _Eng;
4190 typedef typename _Eng::result_type result_type;
4191 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004192 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant30a840f2010-05-12 17:08:57 +00004193 __is.flags(ios_base::dec | ios_base::skipws);
4194 result_type __lambda;
4195 __is >> __lambda;
4196 if (!__is.fail())
4197 __x.param(param_type(__lambda));
4198 return __is;
4199}
4200
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004201// normal_distribution
4202
4203template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004204class _LIBCPP_TYPE_VIS_ONLY normal_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004205{
4206public:
4207 // types
4208 typedef _RealType result_type;
4209
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004210 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004211 {
4212 result_type __mean_;
4213 result_type __stddev_;
4214 public:
4215 typedef normal_distribution distribution_type;
4216
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004218 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4219 : __mean_(__mean), __stddev_(__stddev) {}
4220
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004222 result_type mean() const {return __mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004223 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004224 result_type stddev() const {return __stddev_;}
4225
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004226 friend _LIBCPP_INLINE_VISIBILITY
4227 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004228 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004229 friend _LIBCPP_INLINE_VISIBILITY
4230 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004231 {return !(__x == __y);}
4232 };
4233
4234private:
4235 param_type __p_;
4236 result_type _V_;
4237 bool _V_hot_;
4238
4239public:
4240 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004242 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4243 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004245 explicit normal_distribution(const param_type& __p)
4246 : __p_(__p), _V_hot_(false) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004248 void reset() {_V_hot_ = false;}
4249
4250 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004251 template<class _URNG>
4252 _LIBCPP_INLINE_VISIBILITY
4253 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004254 {return (*this)(__g, __p_);}
4255 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4256
4257 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004259 result_type mean() const {return __p_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004261 result_type stddev() const {return __p_.stddev();}
4262
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004264 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004266 void param(const param_type& __p) {__p_ = __p;}
4267
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004269 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004271 result_type max() const {return numeric_limits<result_type>::infinity();}
4272
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004273 friend _LIBCPP_INLINE_VISIBILITY
4274 bool operator==(const normal_distribution& __x,
4275 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004276 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4277 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004278 friend _LIBCPP_INLINE_VISIBILITY
4279 bool operator!=(const normal_distribution& __x,
4280 const normal_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004281 {return !(__x == __y);}
4282
4283 template <class _CharT, class _Traits, class _RT>
4284 friend
4285 basic_ostream<_CharT, _Traits>&
4286 operator<<(basic_ostream<_CharT, _Traits>& __os,
4287 const normal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004288
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004289 template <class _CharT, class _Traits, class _RT>
4290 friend
4291 basic_istream<_CharT, _Traits>&
4292 operator>>(basic_istream<_CharT, _Traits>& __is,
4293 normal_distribution<_RT>& __x);
4294};
4295
4296template <class _RealType>
4297template<class _URNG>
4298_RealType
4299normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4300{
Howard Hinnant99968442011-11-29 18:15:50 +00004301 result_type _Up;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004302 if (_V_hot_)
4303 {
4304 _V_hot_ = false;
Howard Hinnant99968442011-11-29 18:15:50 +00004305 _Up = _V_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004306 }
4307 else
4308 {
4309 uniform_real_distribution<result_type> _Uni(-1, 1);
4310 result_type __u;
4311 result_type __v;
4312 result_type __s;
4313 do
4314 {
4315 __u = _Uni(__g);
4316 __v = _Uni(__g);
4317 __s = __u * __u + __v * __v;
4318 } while (__s > 1 || __s == 0);
Howard Hinnant99968442011-11-29 18:15:50 +00004319 result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
4320 _V_ = __v * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004321 _V_hot_ = true;
Howard Hinnant99968442011-11-29 18:15:50 +00004322 _Up = __u * _Fp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004323 }
Howard Hinnant99968442011-11-29 18:15:50 +00004324 return _Up * __p.stddev() + __p.mean();
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004325}
4326
4327template <class _CharT, class _Traits, class _RT>
4328basic_ostream<_CharT, _Traits>&
4329operator<<(basic_ostream<_CharT, _Traits>& __os,
4330 const normal_distribution<_RT>& __x)
4331{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004332 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004333 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4334 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004335 _CharT __sp = __os.widen(' ');
4336 __os.fill(__sp);
4337 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4338 if (__x._V_hot_)
4339 __os << __sp << __x._V_;
4340 return __os;
4341}
4342
4343template <class _CharT, class _Traits, class _RT>
4344basic_istream<_CharT, _Traits>&
4345operator>>(basic_istream<_CharT, _Traits>& __is,
4346 normal_distribution<_RT>& __x)
4347{
4348 typedef normal_distribution<_RT> _Eng;
4349 typedef typename _Eng::result_type result_type;
4350 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004351 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004352 __is.flags(ios_base::dec | ios_base::skipws);
4353 result_type __mean;
4354 result_type __stddev;
Howard Hinnant99968442011-11-29 18:15:50 +00004355 result_type _Vp = 0;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004356 bool _V_hot = false;
4357 __is >> __mean >> __stddev >> _V_hot;
4358 if (_V_hot)
Howard Hinnant99968442011-11-29 18:15:50 +00004359 __is >> _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004360 if (!__is.fail())
4361 {
4362 __x.param(param_type(__mean, __stddev));
4363 __x._V_hot_ = _V_hot;
Howard Hinnant99968442011-11-29 18:15:50 +00004364 __x._V_ = _Vp;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004365 }
4366 return __is;
4367}
4368
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004369// lognormal_distribution
4370
4371template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004372class _LIBCPP_TYPE_VIS_ONLY lognormal_distribution
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004373{
4374public:
4375 // types
4376 typedef _RealType result_type;
4377
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004378 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004379 {
4380 normal_distribution<result_type> __nd_;
4381 public:
4382 typedef lognormal_distribution distribution_type;
4383
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004385 explicit param_type(result_type __m = 0, result_type __s = 1)
4386 : __nd_(__m, __s) {}
4387
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004389 result_type m() const {return __nd_.mean();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004391 result_type s() const {return __nd_.stddev();}
4392
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004393 friend _LIBCPP_INLINE_VISIBILITY
4394 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004395 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004396 friend _LIBCPP_INLINE_VISIBILITY
4397 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004398 {return !(__x == __y);}
4399 friend class lognormal_distribution;
4400
4401 template <class _CharT, class _Traits, class _RT>
4402 friend
4403 basic_ostream<_CharT, _Traits>&
4404 operator<<(basic_ostream<_CharT, _Traits>& __os,
4405 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004406
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004407 template <class _CharT, class _Traits, class _RT>
4408 friend
4409 basic_istream<_CharT, _Traits>&
4410 operator>>(basic_istream<_CharT, _Traits>& __is,
4411 lognormal_distribution<_RT>& __x);
4412 };
4413
4414private:
4415 param_type __p_;
4416
4417public:
4418 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004420 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4421 : __p_(param_type(__m, __s)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004423 explicit lognormal_distribution(const param_type& __p)
4424 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004426 void reset() {__p_.__nd_.reset();}
4427
4428 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004429 template<class _URNG>
4430 _LIBCPP_INLINE_VISIBILITY
4431 result_type operator()(_URNG& __g)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004432 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004433 template<class _URNG>
4434 _LIBCPP_INLINE_VISIBILITY
4435 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004436 {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004437
4438 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004440 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004442 result_type s() const {return __p_.s();}
4443
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004445 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00004447 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004448
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004450 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004452 result_type max() const {return numeric_limits<result_type>::infinity();}
4453
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004454 friend _LIBCPP_INLINE_VISIBILITY
4455 bool operator==(const lognormal_distribution& __x,
4456 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004457 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004458 friend _LIBCPP_INLINE_VISIBILITY
4459 bool operator!=(const lognormal_distribution& __x,
4460 const lognormal_distribution& __y)
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004461 {return !(__x == __y);}
4462
4463 template <class _CharT, class _Traits, class _RT>
4464 friend
4465 basic_ostream<_CharT, _Traits>&
4466 operator<<(basic_ostream<_CharT, _Traits>& __os,
4467 const lognormal_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00004468
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004469 template <class _CharT, class _Traits, class _RT>
4470 friend
4471 basic_istream<_CharT, _Traits>&
4472 operator>>(basic_istream<_CharT, _Traits>& __is,
4473 lognormal_distribution<_RT>& __x);
4474};
4475
4476template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004477inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004478basic_ostream<_CharT, _Traits>&
4479operator<<(basic_ostream<_CharT, _Traits>& __os,
4480 const lognormal_distribution<_RT>& __x)
4481{
4482 return __os << __x.__p_.__nd_;
4483}
4484
4485template <class _CharT, class _Traits, class _RT>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004486inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004487basic_istream<_CharT, _Traits>&
4488operator>>(basic_istream<_CharT, _Traits>& __is,
4489 lognormal_distribution<_RT>& __x)
4490{
4491 return __is >> __x.__p_.__nd_;
4492}
4493
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004494// poisson_distribution
4495
4496template<class _IntType = int>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004497class _LIBCPP_TYPE_VIS_ONLY poisson_distribution
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004498{
4499public:
4500 // types
4501 typedef _IntType result_type;
4502
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004503 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004504 {
4505 double __mean_;
4506 double __s_;
4507 double __d_;
4508 double __l_;
4509 double __omega_;
4510 double __c0_;
4511 double __c1_;
4512 double __c2_;
4513 double __c3_;
4514 double __c_;
4515
4516 public:
4517 typedef poisson_distribution distribution_type;
4518
4519 explicit param_type(double __mean = 1.0);
4520
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004522 double mean() const {return __mean_;}
4523
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004524 friend _LIBCPP_INLINE_VISIBILITY
4525 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004526 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004527 friend _LIBCPP_INLINE_VISIBILITY
4528 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004529 {return !(__x == __y);}
4530
4531 friend class poisson_distribution;
4532 };
4533
4534private:
4535 param_type __p_;
4536
4537public:
4538 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004540 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004542 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004544 void reset() {}
4545
4546 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004547 template<class _URNG>
4548 _LIBCPP_INLINE_VISIBILITY
4549 result_type operator()(_URNG& __g)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004550 {return (*this)(__g, __p_);}
4551 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4552
4553 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004555 double mean() const {return __p_.mean();}
4556
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004558 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004560 void param(const param_type& __p) {__p_ = __p;}
4561
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004563 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004565 result_type max() const {return numeric_limits<result_type>::max();}
4566
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004567 friend _LIBCPP_INLINE_VISIBILITY
4568 bool operator==(const poisson_distribution& __x,
4569 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004570 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004571 friend _LIBCPP_INLINE_VISIBILITY
4572 bool operator!=(const poisson_distribution& __x,
4573 const poisson_distribution& __y)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004574 {return !(__x == __y);}
4575};
4576
4577template<class _IntType>
4578poisson_distribution<_IntType>::param_type::param_type(double __mean)
4579 : __mean_(__mean)
4580{
4581 if (__mean_ < 10)
4582 {
4583 __s_ = 0;
4584 __d_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004585 __l_ = _VSTD::exp(-__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004586 __omega_ = 0;
4587 __c3_ = 0;
4588 __c2_ = 0;
4589 __c1_ = 0;
4590 __c0_ = 0;
4591 __c_ = 0;
4592 }
4593 else
4594 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004595 __s_ = _VSTD::sqrt(__mean_);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004596 __d_ = 6 * __mean_ * __mean_;
4597 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4598 __omega_ = .3989423 / __s_;
4599 double __b1_ = .4166667E-1 / __mean_;
4600 double __b2_ = .3 * __b1_ * __b1_;
4601 __c3_ = .1428571 * __b1_ * __b2_;
4602 __c2_ = __b2_ - 15. * __c3_;
4603 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4604 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4605 __c_ = .1069 / __mean_;
4606 }
4607}
4608
4609template <class _IntType>
4610template<class _URNG>
4611_IntType
4612poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4613{
4614 result_type __x;
4615 uniform_real_distribution<double> __urd;
Howard Hinnant75f76952011-04-14 15:59:22 +00004616 if (__pr.__mean_ < 10)
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004617 {
4618 __x = 0;
4619 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4620 __p *= __urd(__urng);
4621 }
4622 else
4623 {
4624 double __difmuk;
4625 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4626 double __u;
4627 if (__g > 0)
4628 {
4629 __x = static_cast<result_type>(__g);
4630 if (__x >= __pr.__l_)
4631 return __x;
4632 __difmuk = __pr.__mean_ - __x;
4633 __u = __urd(__urng);
4634 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4635 return __x;
4636 }
4637 exponential_distribution<double> __edist;
4638 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4639 {
4640 double __e;
4641 if (__using_exp_dist || __g < 0)
4642 {
4643 double __t;
4644 do
4645 {
4646 __e = __edist(__urng);
4647 __u = __urd(__urng);
4648 __u += __u - 1;
4649 __t = 1.8 + (__u < 0 ? -__e : __e);
4650 } while (__t <= -.6744);
4651 __x = __pr.__mean_ + __pr.__s_ * __t;
4652 __difmuk = __pr.__mean_ - __x;
4653 __using_exp_dist = true;
4654 }
4655 double __px;
4656 double __py;
4657 if (__x < 10)
4658 {
4659 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4660 40320, 362880};
4661 __px = -__pr.__mean_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004662 __py = _VSTD::pow(__pr.__mean_, (double)__x) / __fac[__x];
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004663 }
4664 else
4665 {
4666 double __del = .8333333E-1 / __x;
4667 __del -= 4.8 * __del * __del * __del;
4668 double __v = __difmuk / __x;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004669 if (_VSTD::abs(__v) > 0.25)
4670 __px = __x * _VSTD::log(1 + __v) - __difmuk - __del;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004671 else
4672 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4673 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4674 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004675 __py = .3989423 / _VSTD::sqrt(__x);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004676 }
4677 double __r = (0.5 - __difmuk) / __pr.__s_;
4678 double __r2 = __r * __r;
4679 double __fx = -0.5 * __r2;
4680 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4681 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4682 if (__using_exp_dist)
4683 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004684 if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4685 __fy * _VSTD::exp(__fx + __e))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004686 break;
4687 }
4688 else
4689 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004690 if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004691 break;
4692 }
4693 }
4694 }
4695 return __x;
4696}
4697
4698template <class _CharT, class _Traits, class _IntType>
4699basic_ostream<_CharT, _Traits>&
4700operator<<(basic_ostream<_CharT, _Traits>& __os,
4701 const poisson_distribution<_IntType>& __x)
4702{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004703 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004704 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4705 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004706 return __os << __x.mean();
4707}
4708
4709template <class _CharT, class _Traits, class _IntType>
4710basic_istream<_CharT, _Traits>&
4711operator>>(basic_istream<_CharT, _Traits>& __is,
4712 poisson_distribution<_IntType>& __x)
4713{
4714 typedef poisson_distribution<_IntType> _Eng;
4715 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004716 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004717 __is.flags(ios_base::dec | ios_base::skipws);
4718 double __mean;
4719 __is >> __mean;
4720 if (!__is.fail())
4721 __x.param(param_type(__mean));
4722 return __is;
4723}
4724
Howard Hinnant9de6e302010-05-16 01:09:02 +00004725// weibull_distribution
4726
4727template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004728class _LIBCPP_TYPE_VIS_ONLY weibull_distribution
Howard Hinnant9de6e302010-05-16 01:09:02 +00004729{
4730public:
4731 // types
4732 typedef _RealType result_type;
4733
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004734 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant9de6e302010-05-16 01:09:02 +00004735 {
4736 result_type __a_;
4737 result_type __b_;
4738 public:
4739 typedef weibull_distribution distribution_type;
4740
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004742 explicit param_type(result_type __a = 1, result_type __b = 1)
4743 : __a_(__a), __b_(__b) {}
4744
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004746 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004748 result_type b() const {return __b_;}
4749
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004750 friend _LIBCPP_INLINE_VISIBILITY
4751 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004752 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004753 friend _LIBCPP_INLINE_VISIBILITY
4754 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004755 {return !(__x == __y);}
4756 };
4757
4758private:
4759 param_type __p_;
4760
4761public:
4762 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004764 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4765 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004767 explicit weibull_distribution(const param_type& __p)
4768 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004770 void reset() {}
4771
4772 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004773 template<class _URNG>
4774 _LIBCPP_INLINE_VISIBILITY
4775 result_type operator()(_URNG& __g)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004776 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004777 template<class _URNG>
4778 _LIBCPP_INLINE_VISIBILITY
4779 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004780 {return __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004781 _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
Howard Hinnant9de6e302010-05-16 01:09:02 +00004782
4783 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004785 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004787 result_type b() const {return __p_.b();}
4788
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004790 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004792 void param(const param_type& __p) {__p_ = __p;}
4793
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004795 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9de6e302010-05-16 01:09:02 +00004797 result_type max() const {return numeric_limits<result_type>::infinity();}
4798
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004799 friend _LIBCPP_INLINE_VISIBILITY
4800 bool operator==(const weibull_distribution& __x,
4801 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004802 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004803 friend _LIBCPP_INLINE_VISIBILITY
4804 bool operator!=(const weibull_distribution& __x,
4805 const weibull_distribution& __y)
Howard Hinnant9de6e302010-05-16 01:09:02 +00004806 {return !(__x == __y);}
4807};
4808
4809template <class _CharT, class _Traits, class _RT>
4810basic_ostream<_CharT, _Traits>&
4811operator<<(basic_ostream<_CharT, _Traits>& __os,
4812 const weibull_distribution<_RT>& __x)
4813{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004814 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004815 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4816 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004817 _CharT __sp = __os.widen(' ');
4818 __os.fill(__sp);
4819 __os << __x.a() << __sp << __x.b();
4820 return __os;
4821}
4822
4823template <class _CharT, class _Traits, class _RT>
4824basic_istream<_CharT, _Traits>&
4825operator>>(basic_istream<_CharT, _Traits>& __is,
4826 weibull_distribution<_RT>& __x)
4827{
4828 typedef weibull_distribution<_RT> _Eng;
4829 typedef typename _Eng::result_type result_type;
4830 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004831 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004832 __is.flags(ios_base::dec | ios_base::skipws);
4833 result_type __a;
4834 result_type __b;
4835 __is >> __a >> __b;
4836 if (!__is.fail())
4837 __x.param(param_type(__a, __b));
4838 return __is;
4839}
4840
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004841template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004842class _LIBCPP_TYPE_VIS_ONLY extreme_value_distribution
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004843{
4844public:
4845 // types
4846 typedef _RealType result_type;
4847
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004848 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004849 {
4850 result_type __a_;
4851 result_type __b_;
4852 public:
4853 typedef extreme_value_distribution distribution_type;
4854
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004856 explicit param_type(result_type __a = 0, result_type __b = 1)
4857 : __a_(__a), __b_(__b) {}
4858
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004860 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004862 result_type b() const {return __b_;}
4863
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004864 friend _LIBCPP_INLINE_VISIBILITY
4865 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004866 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004867 friend _LIBCPP_INLINE_VISIBILITY
4868 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004869 {return !(__x == __y);}
4870 };
4871
4872private:
4873 param_type __p_;
4874
4875public:
4876 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004878 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4879 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004881 explicit extreme_value_distribution(const param_type& __p)
4882 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004884 void reset() {}
4885
4886 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004887 template<class _URNG>
4888 _LIBCPP_INLINE_VISIBILITY
4889 result_type operator()(_URNG& __g)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004890 {return (*this)(__g, __p_);}
4891 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4892
4893 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004894 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004895 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004897 result_type b() const {return __p_.b();}
4898
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004900 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004902 void param(const param_type& __p) {__p_ = __p;}
4903
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004905 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004907 result_type max() const {return numeric_limits<result_type>::infinity();}
4908
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004909 friend _LIBCPP_INLINE_VISIBILITY
4910 bool operator==(const extreme_value_distribution& __x,
4911 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004912 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004913 friend _LIBCPP_INLINE_VISIBILITY
4914 bool operator!=(const extreme_value_distribution& __x,
4915 const extreme_value_distribution& __y)
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004916 {return !(__x == __y);}
4917};
4918
4919template<class _RealType>
4920template<class _URNG>
4921_RealType
4922extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4923{
4924 return __p.a() - __p.b() *
Howard Hinnant0949eed2011-06-30 21:18:19 +00004925 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004926}
4927
4928template <class _CharT, class _Traits, class _RT>
4929basic_ostream<_CharT, _Traits>&
4930operator<<(basic_ostream<_CharT, _Traits>& __os,
4931 const extreme_value_distribution<_RT>& __x)
4932{
Howard Hinnant9c0df142012-10-30 19:06:59 +00004933 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004934 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4935 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004936 _CharT __sp = __os.widen(' ');
4937 __os.fill(__sp);
4938 __os << __x.a() << __sp << __x.b();
4939 return __os;
4940}
4941
4942template <class _CharT, class _Traits, class _RT>
4943basic_istream<_CharT, _Traits>&
4944operator>>(basic_istream<_CharT, _Traits>& __is,
4945 extreme_value_distribution<_RT>& __x)
4946{
4947 typedef extreme_value_distribution<_RT> _Eng;
4948 typedef typename _Eng::result_type result_type;
4949 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00004950 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004951 __is.flags(ios_base::dec | ios_base::skipws);
4952 result_type __a;
4953 result_type __b;
4954 __is >> __a >> __b;
4955 if (!__is.fail())
4956 __x.param(param_type(__a, __b));
4957 return __is;
4958}
4959
Howard Hinnantc7c49132010-05-13 17:58:28 +00004960// gamma_distribution
4961
4962template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004963class _LIBCPP_TYPE_VIS_ONLY gamma_distribution
Howard Hinnantc7c49132010-05-13 17:58:28 +00004964{
4965public:
4966 // types
4967 typedef _RealType result_type;
4968
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004969 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantc7c49132010-05-13 17:58:28 +00004970 {
4971 result_type __alpha_;
4972 result_type __beta_;
4973 public:
4974 typedef gamma_distribution distribution_type;
4975
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004977 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4978 : __alpha_(__alpha), __beta_(__beta) {}
4979
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004981 result_type alpha() const {return __alpha_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004983 result_type beta() const {return __beta_;}
4984
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004985 friend _LIBCPP_INLINE_VISIBILITY
4986 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004987 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004988 friend _LIBCPP_INLINE_VISIBILITY
4989 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004990 {return !(__x == __y);}
4991 };
4992
4993private:
4994 param_type __p_;
4995
4996public:
4997 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00004998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00004999 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
5000 : __p_(param_type(__alpha, __beta)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005002 explicit gamma_distribution(const param_type& __p)
5003 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005005 void reset() {}
5006
5007 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005008 template<class _URNG>
5009 _LIBCPP_INLINE_VISIBILITY
5010 result_type operator()(_URNG& __g)
Howard Hinnantc7c49132010-05-13 17:58:28 +00005011 {return (*this)(__g, __p_);}
5012 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5013
5014 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005016 result_type alpha() const {return __p_.alpha();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005018 result_type beta() const {return __p_.beta();}
5019
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005021 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005023 void param(const param_type& __p) {__p_ = __p;}
5024
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc7c49132010-05-13 17:58:28 +00005026 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005028 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantc7c49132010-05-13 17:58:28 +00005029
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005030 friend _LIBCPP_INLINE_VISIBILITY
5031 bool operator==(const gamma_distribution& __x,
5032 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00005033 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005034 friend _LIBCPP_INLINE_VISIBILITY
5035 bool operator!=(const gamma_distribution& __x,
5036 const gamma_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00005037 {return !(__x == __y);}
5038};
5039
5040template <class _RealType>
5041template<class _URNG>
5042_RealType
5043gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5044{
Howard Hinnantf417abe2010-05-14 18:43:10 +00005045 result_type __a = __p.alpha();
5046 uniform_real_distribution<result_type> __gen(0, 1);
5047 exponential_distribution<result_type> __egen;
5048 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00005049 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00005050 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00005051 else if (__a > 1)
5052 {
5053 const result_type __b = __a - 1;
5054 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00005055 while (true)
5056 {
5057 const result_type __u = __gen(__g);
5058 const result_type __v = __gen(__g);
5059 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00005060 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00005061 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005062 const result_type __y = _VSTD::sqrt(__c / __w) *
Howard Hinnantc7c49132010-05-13 17:58:28 +00005063 (__u - result_type(0.5));
5064 __x = __b + __y;
5065 if (__x >= 0)
5066 {
5067 const result_type __z = 64 * __w * __w * __w * __v * __v;
5068 if (__z <= 1 - 2 * __y * __y / __x)
5069 break;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005070 if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
Howard Hinnantc7c49132010-05-13 17:58:28 +00005071 break;
5072 }
5073 }
5074 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00005075 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00005076 else // __a < 1
5077 {
5078 while (true)
5079 {
5080 const result_type __u = __gen(__g);
5081 const result_type __es = __egen(__g);
5082 if (__u <= 1 - __a)
5083 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005084 __x = _VSTD::pow(__u, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00005085 if (__x <= __es)
5086 break;
5087 }
5088 else
5089 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005090 const result_type __e = -_VSTD::log((1-__u)/__a);
5091 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
Howard Hinnantf417abe2010-05-14 18:43:10 +00005092 if (__x <= __e + __es)
5093 break;
5094 }
5095 }
5096 }
5097 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00005098}
5099
5100template <class _CharT, class _Traits, class _RT>
5101basic_ostream<_CharT, _Traits>&
5102operator<<(basic_ostream<_CharT, _Traits>& __os,
5103 const gamma_distribution<_RT>& __x)
5104{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005105 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005106 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5107 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:28 +00005108 _CharT __sp = __os.widen(' ');
5109 __os.fill(__sp);
5110 __os << __x.alpha() << __sp << __x.beta();
5111 return __os;
5112}
5113
5114template <class _CharT, class _Traits, class _RT>
5115basic_istream<_CharT, _Traits>&
5116operator>>(basic_istream<_CharT, _Traits>& __is,
5117 gamma_distribution<_RT>& __x)
5118{
5119 typedef gamma_distribution<_RT> _Eng;
5120 typedef typename _Eng::result_type result_type;
5121 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005122 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc7c49132010-05-13 17:58:28 +00005123 __is.flags(ios_base::dec | ios_base::skipws);
5124 result_type __alpha;
5125 result_type __beta;
5126 __is >> __alpha >> __beta;
5127 if (!__is.fail())
5128 __x.param(param_type(__alpha, __beta));
5129 return __is;
5130}
Howard Hinnanta64111c2010-05-12 21:02:31 +00005131
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005132// negative_binomial_distribution
5133
5134template<class _IntType = int>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005135class _LIBCPP_TYPE_VIS_ONLY negative_binomial_distribution
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005136{
5137public:
5138 // types
5139 typedef _IntType result_type;
5140
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005141 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005142 {
5143 result_type __k_;
5144 double __p_;
5145 public:
5146 typedef negative_binomial_distribution distribution_type;
5147
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005149 explicit param_type(result_type __k = 1, double __p = 0.5)
5150 : __k_(__k), __p_(__p) {}
5151
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005153 result_type k() const {return __k_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005155 double p() const {return __p_;}
5156
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005157 friend _LIBCPP_INLINE_VISIBILITY
5158 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005159 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005160 friend _LIBCPP_INLINE_VISIBILITY
5161 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005162 {return !(__x == __y);}
5163 };
5164
5165private:
5166 param_type __p_;
5167
5168public:
5169 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005171 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
5172 : __p_(__k, __p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005174 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005176 void reset() {}
5177
5178 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005179 template<class _URNG>
5180 _LIBCPP_INLINE_VISIBILITY
5181 result_type operator()(_URNG& __g)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005182 {return (*this)(__g, __p_);}
5183 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5184
5185 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005187 result_type k() const {return __p_.k();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005189 double p() const {return __p_.p();}
5190
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005192 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005194 void param(const param_type& __p) {__p_ = __p;}
5195
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005197 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005199 result_type max() const {return numeric_limits<result_type>::max();}
5200
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005201 friend _LIBCPP_INLINE_VISIBILITY
5202 bool operator==(const negative_binomial_distribution& __x,
5203 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005204 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005205 friend _LIBCPP_INLINE_VISIBILITY
5206 bool operator!=(const negative_binomial_distribution& __x,
5207 const negative_binomial_distribution& __y)
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005208 {return !(__x == __y);}
5209};
5210
5211template <class _IntType>
5212template<class _URNG>
5213_IntType
5214negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5215{
5216 result_type __k = __pr.k();
5217 double __p = __pr.p();
5218 if (__k <= 21 * __p)
5219 {
5220 bernoulli_distribution __gen(__p);
5221 result_type __f = 0;
5222 result_type __s = 0;
5223 while (__s < __k)
5224 {
5225 if (__gen(__urng))
5226 ++__s;
5227 else
5228 ++__f;
5229 }
5230 return __f;
5231 }
5232 return poisson_distribution<result_type>(gamma_distribution<double>
5233 (__k, (1-__p)/__p)(__urng))(__urng);
5234}
5235
5236template <class _CharT, class _Traits, class _IntType>
5237basic_ostream<_CharT, _Traits>&
5238operator<<(basic_ostream<_CharT, _Traits>& __os,
5239 const negative_binomial_distribution<_IntType>& __x)
5240{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005241 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005242 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5243 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005244 _CharT __sp = __os.widen(' ');
5245 __os.fill(__sp);
5246 return __os << __x.k() << __sp << __x.p();
5247}
5248
5249template <class _CharT, class _Traits, class _IntType>
5250basic_istream<_CharT, _Traits>&
5251operator>>(basic_istream<_CharT, _Traits>& __is,
5252 negative_binomial_distribution<_IntType>& __x)
5253{
5254 typedef negative_binomial_distribution<_IntType> _Eng;
5255 typedef typename _Eng::result_type result_type;
5256 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005257 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005258 __is.flags(ios_base::dec | ios_base::skipws);
5259 result_type __k;
5260 double __p;
5261 __is >> __k >> __p;
5262 if (!__is.fail())
5263 __x.param(param_type(__k, __p));
5264 return __is;
5265}
5266
Howard Hinnant34e8a572010-05-17 13:44:27 +00005267// geometric_distribution
5268
5269template<class _IntType = int>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005270class _LIBCPP_TYPE_VIS_ONLY geometric_distribution
Howard Hinnant34e8a572010-05-17 13:44:27 +00005271{
5272public:
5273 // types
5274 typedef _IntType result_type;
5275
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005276 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant34e8a572010-05-17 13:44:27 +00005277 {
5278 double __p_;
5279 public:
5280 typedef geometric_distribution distribution_type;
5281
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005283 explicit param_type(double __p = 0.5) : __p_(__p) {}
5284
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005286 double p() const {return __p_;}
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 Hinnant34e8a572010-05-17 13:44:27 +00005290 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005291 friend _LIBCPP_INLINE_VISIBILITY
5292 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005293 {return !(__x == __y);}
5294 };
5295
5296private:
5297 param_type __p_;
5298
5299public:
5300 // constructors and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005302 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005304 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005306 void reset() {}
5307
5308 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005309 template<class _URNG>
5310 _LIBCPP_INLINE_VISIBILITY
5311 result_type operator()(_URNG& __g)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005312 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005313 template<class _URNG>
5314 _LIBCPP_INLINE_VISIBILITY
5315 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005316 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5317
5318 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005320 double p() const {return __p_.p();}
5321
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005323 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005325 void param(const param_type& __p) {__p_ = __p;}
5326
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005328 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant34e8a572010-05-17 13:44:27 +00005330 result_type max() const {return numeric_limits<result_type>::max();}
5331
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005332 friend _LIBCPP_INLINE_VISIBILITY
5333 bool operator==(const geometric_distribution& __x,
5334 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005335 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005336 friend _LIBCPP_INLINE_VISIBILITY
5337 bool operator!=(const geometric_distribution& __x,
5338 const geometric_distribution& __y)
Howard Hinnant34e8a572010-05-17 13:44:27 +00005339 {return !(__x == __y);}
5340};
5341
5342template <class _CharT, class _Traits, class _IntType>
5343basic_ostream<_CharT, _Traits>&
5344operator<<(basic_ostream<_CharT, _Traits>& __os,
5345 const geometric_distribution<_IntType>& __x)
5346{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005347 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005348 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5349 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005350 return __os << __x.p();
5351}
5352
5353template <class _CharT, class _Traits, class _IntType>
5354basic_istream<_CharT, _Traits>&
5355operator>>(basic_istream<_CharT, _Traits>& __is,
5356 geometric_distribution<_IntType>& __x)
5357{
5358 typedef geometric_distribution<_IntType> _Eng;
5359 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005360 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005361 __is.flags(ios_base::dec | ios_base::skipws);
5362 double __p;
5363 __is >> __p;
5364 if (!__is.fail())
5365 __x.param(param_type(__p));
5366 return __is;
5367}
5368
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005369// chi_squared_distribution
5370
5371template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005372class _LIBCPP_TYPE_VIS_ONLY chi_squared_distribution
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005373{
5374public:
5375 // types
5376 typedef _RealType result_type;
5377
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005378 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005379 {
5380 result_type __n_;
5381 public:
5382 typedef chi_squared_distribution distribution_type;
5383
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005385 explicit param_type(result_type __n = 1) : __n_(__n) {}
5386
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005388 result_type n() const {return __n_;}
5389
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005390 friend _LIBCPP_INLINE_VISIBILITY
5391 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005392 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005393 friend _LIBCPP_INLINE_VISIBILITY
5394 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005395 {return !(__x == __y);}
5396 };
5397
5398private:
5399 param_type __p_;
5400
5401public:
5402 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005404 explicit chi_squared_distribution(result_type __n = 1)
5405 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005407 explicit chi_squared_distribution(const param_type& __p)
5408 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005410 void reset() {}
5411
5412 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005413 template<class _URNG>
5414 _LIBCPP_INLINE_VISIBILITY
5415 result_type operator()(_URNG& __g)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005416 {return (*this)(__g, __p_);}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005417 template<class _URNG>
5418 _LIBCPP_INLINE_VISIBILITY
5419 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005420 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5421
5422 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005424 result_type n() const {return __p_.n();}
5425
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005427 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005429 void param(const param_type& __p) {__p_ = __p;}
5430
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005432 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005434 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005435
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005436 friend _LIBCPP_INLINE_VISIBILITY
5437 bool operator==(const chi_squared_distribution& __x,
5438 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005439 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005440 friend _LIBCPP_INLINE_VISIBILITY
5441 bool operator!=(const chi_squared_distribution& __x,
5442 const chi_squared_distribution& __y)
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005443 {return !(__x == __y);}
5444};
5445
5446template <class _CharT, class _Traits, class _RT>
5447basic_ostream<_CharT, _Traits>&
5448operator<<(basic_ostream<_CharT, _Traits>& __os,
5449 const chi_squared_distribution<_RT>& __x)
5450{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005451 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005452 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5453 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005454 __os << __x.n();
5455 return __os;
5456}
5457
5458template <class _CharT, class _Traits, class _RT>
5459basic_istream<_CharT, _Traits>&
5460operator>>(basic_istream<_CharT, _Traits>& __is,
5461 chi_squared_distribution<_RT>& __x)
5462{
5463 typedef chi_squared_distribution<_RT> _Eng;
5464 typedef typename _Eng::result_type result_type;
5465 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005466 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005467 __is.flags(ios_base::dec | ios_base::skipws);
5468 result_type __n;
5469 __is >> __n;
5470 if (!__is.fail())
5471 __x.param(param_type(__n));
5472 return __is;
5473}
5474
Howard Hinnantd7d01132010-05-17 21:55:46 +00005475// cauchy_distribution
5476
5477template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005478class _LIBCPP_TYPE_VIS_ONLY cauchy_distribution
Howard Hinnantd7d01132010-05-17 21:55:46 +00005479{
5480public:
5481 // types
5482 typedef _RealType result_type;
5483
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005484 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantd7d01132010-05-17 21:55:46 +00005485 {
5486 result_type __a_;
5487 result_type __b_;
5488 public:
5489 typedef cauchy_distribution distribution_type;
5490
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005492 explicit param_type(result_type __a = 0, result_type __b = 1)
5493 : __a_(__a), __b_(__b) {}
5494
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005496 result_type a() const {return __a_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005498 result_type b() const {return __b_;}
5499
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005500 friend _LIBCPP_INLINE_VISIBILITY
5501 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005502 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005503 friend _LIBCPP_INLINE_VISIBILITY
5504 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005505 {return !(__x == __y);}
5506 };
5507
5508private:
5509 param_type __p_;
5510
5511public:
5512 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005514 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5515 : __p_(param_type(__a, __b)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005517 explicit cauchy_distribution(const param_type& __p)
5518 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005519 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005520 void reset() {}
5521
5522 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005523 template<class _URNG>
5524 _LIBCPP_INLINE_VISIBILITY
5525 result_type operator()(_URNG& __g)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005526 {return (*this)(__g, __p_);}
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005527 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005528
5529 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005530 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005531 result_type a() const {return __p_.a();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005533 result_type b() const {return __p_.b();}
5534
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005536 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005538 void param(const param_type& __p) {__p_ = __p;}
5539
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd7d01132010-05-17 21:55:46 +00005541 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005543 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005544
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005545 friend _LIBCPP_INLINE_VISIBILITY
5546 bool operator==(const cauchy_distribution& __x,
5547 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005548 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005549 friend _LIBCPP_INLINE_VISIBILITY
5550 bool operator!=(const cauchy_distribution& __x,
5551 const cauchy_distribution& __y)
Howard Hinnantd7d01132010-05-17 21:55:46 +00005552 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005553};
5554
5555template <class _RealType>
5556template<class _URNG>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005557inline
Howard Hinnantd7d01132010-05-17 21:55:46 +00005558_RealType
5559cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5560{
5561 uniform_real_distribution<result_type> __gen;
5562 // 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 +00005563 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
Howard Hinnantd7d01132010-05-17 21:55:46 +00005564}
5565
5566template <class _CharT, class _Traits, class _RT>
5567basic_ostream<_CharT, _Traits>&
5568operator<<(basic_ostream<_CharT, _Traits>& __os,
5569 const cauchy_distribution<_RT>& __x)
5570{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005571 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005572 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5573 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005574 _CharT __sp = __os.widen(' ');
5575 __os.fill(__sp);
5576 __os << __x.a() << __sp << __x.b();
5577 return __os;
5578}
5579
5580template <class _CharT, class _Traits, class _RT>
5581basic_istream<_CharT, _Traits>&
5582operator>>(basic_istream<_CharT, _Traits>& __is,
5583 cauchy_distribution<_RT>& __x)
5584{
5585 typedef cauchy_distribution<_RT> _Eng;
5586 typedef typename _Eng::result_type result_type;
5587 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005588 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005589 __is.flags(ios_base::dec | ios_base::skipws);
5590 result_type __a;
5591 result_type __b;
5592 __is >> __a >> __b;
5593 if (!__is.fail())
5594 __x.param(param_type(__a, __b));
5595 return __is;
5596}
5597
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005598// fisher_f_distribution
5599
5600template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005601class _LIBCPP_TYPE_VIS_ONLY fisher_f_distribution
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005602{
5603public:
5604 // types
5605 typedef _RealType result_type;
5606
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005607 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005608 {
5609 result_type __m_;
5610 result_type __n_;
5611 public:
5612 typedef fisher_f_distribution distribution_type;
5613
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005614 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005615 explicit param_type(result_type __m = 1, result_type __n = 1)
5616 : __m_(__m), __n_(__n) {}
5617
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005619 result_type m() const {return __m_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005621 result_type n() const {return __n_;}
5622
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005623 friend _LIBCPP_INLINE_VISIBILITY
5624 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005625 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005626 friend _LIBCPP_INLINE_VISIBILITY
5627 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005628 {return !(__x == __y);}
5629 };
5630
5631private:
5632 param_type __p_;
5633
5634public:
5635 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005637 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5638 : __p_(param_type(__m, __n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005640 explicit fisher_f_distribution(const param_type& __p)
5641 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005643 void reset() {}
5644
5645 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005646 template<class _URNG>
5647 _LIBCPP_INLINE_VISIBILITY
5648 result_type operator()(_URNG& __g)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005649 {return (*this)(__g, __p_);}
5650 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5651
5652 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005654 result_type m() const {return __p_.m();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005656 result_type n() const {return __p_.n();}
5657
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005659 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005661 void param(const param_type& __p) {__p_ = __p;}
5662
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005664 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005666 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005667
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005668 friend _LIBCPP_INLINE_VISIBILITY
5669 bool operator==(const fisher_f_distribution& __x,
5670 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005671 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005672 friend _LIBCPP_INLINE_VISIBILITY
5673 bool operator!=(const fisher_f_distribution& __x,
5674 const fisher_f_distribution& __y)
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005675 {return !(__x == __y);}
5676};
5677
5678template <class _RealType>
5679template<class _URNG>
5680_RealType
5681fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5682{
5683 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5684 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5685 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5686}
5687
5688template <class _CharT, class _Traits, class _RT>
5689basic_ostream<_CharT, _Traits>&
5690operator<<(basic_ostream<_CharT, _Traits>& __os,
5691 const fisher_f_distribution<_RT>& __x)
5692{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005693 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005694 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5695 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005696 _CharT __sp = __os.widen(' ');
5697 __os.fill(__sp);
5698 __os << __x.m() << __sp << __x.n();
5699 return __os;
5700}
5701
5702template <class _CharT, class _Traits, class _RT>
5703basic_istream<_CharT, _Traits>&
5704operator>>(basic_istream<_CharT, _Traits>& __is,
5705 fisher_f_distribution<_RT>& __x)
5706{
5707 typedef fisher_f_distribution<_RT> _Eng;
5708 typedef typename _Eng::result_type result_type;
5709 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005710 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005711 __is.flags(ios_base::dec | ios_base::skipws);
5712 result_type __m;
5713 result_type __n;
5714 __is >> __m >> __n;
5715 if (!__is.fail())
5716 __x.param(param_type(__m, __n));
5717 return __is;
5718}
5719
Howard Hinnant551d8e42010-05-19 01:53:57 +00005720// student_t_distribution
5721
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005722template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005723class _LIBCPP_TYPE_VIS_ONLY student_t_distribution
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005724{
5725public:
5726 // types
5727 typedef _RealType result_type;
5728
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005729 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005730 {
5731 result_type __n_;
5732 public:
5733 typedef student_t_distribution distribution_type;
5734
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005736 explicit param_type(result_type __n = 1) : __n_(__n) {}
5737
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005739 result_type n() const {return __n_;}
5740
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005741 friend _LIBCPP_INLINE_VISIBILITY
5742 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005743 {return __x.__n_ == __y.__n_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005744 friend _LIBCPP_INLINE_VISIBILITY
5745 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005746 {return !(__x == __y);}
5747 };
5748
5749private:
5750 param_type __p_;
5751 normal_distribution<result_type> __nd_;
5752
5753public:
5754 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005756 explicit student_t_distribution(result_type __n = 1)
5757 : __p_(param_type(__n)) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005759 explicit student_t_distribution(const param_type& __p)
5760 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005762 void reset() {__nd_.reset();}
5763
5764 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005765 template<class _URNG>
5766 _LIBCPP_INLINE_VISIBILITY
5767 result_type operator()(_URNG& __g)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005768 {return (*this)(__g, __p_);}
5769 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5770
5771 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005773 result_type n() const {return __p_.n();}
5774
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005776 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005778 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005779
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005781 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005783 result_type max() const {return numeric_limits<result_type>::infinity();}
5784
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005785 friend _LIBCPP_INLINE_VISIBILITY
5786 bool operator==(const student_t_distribution& __x,
5787 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005788 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005789 friend _LIBCPP_INLINE_VISIBILITY
5790 bool operator!=(const student_t_distribution& __x,
5791 const student_t_distribution& __y)
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005792 {return !(__x == __y);}
5793};
5794
5795template <class _RealType>
5796template<class _URNG>
5797_RealType
5798student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5799{
5800 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005801 return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005802}
5803
5804template <class _CharT, class _Traits, class _RT>
5805basic_ostream<_CharT, _Traits>&
5806operator<<(basic_ostream<_CharT, _Traits>& __os,
5807 const student_t_distribution<_RT>& __x)
5808{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005809 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005810 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5811 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005812 __os << __x.n();
5813 return __os;
5814}
5815
5816template <class _CharT, class _Traits, class _RT>
5817basic_istream<_CharT, _Traits>&
5818operator>>(basic_istream<_CharT, _Traits>& __is,
5819 student_t_distribution<_RT>& __x)
5820{
5821 typedef student_t_distribution<_RT> _Eng;
5822 typedef typename _Eng::result_type result_type;
5823 typedef typename _Eng::param_type param_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00005824 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005825 __is.flags(ios_base::dec | ios_base::skipws);
5826 result_type __n;
5827 __is >> __n;
5828 if (!__is.fail())
5829 __x.param(param_type(__n));
5830 return __is;
5831}
5832
Howard Hinnant551d8e42010-05-19 01:53:57 +00005833// discrete_distribution
5834
5835template<class _IntType = int>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005836class _LIBCPP_TYPE_VIS_ONLY discrete_distribution
Howard Hinnant551d8e42010-05-19 01:53:57 +00005837{
5838public:
5839 // types
5840 typedef _IntType result_type;
5841
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005842 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant551d8e42010-05-19 01:53:57 +00005843 {
5844 vector<double> __p_;
5845 public:
5846 typedef discrete_distribution distribution_type;
5847
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005849 param_type() {}
5850 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005852 param_type(_InputIterator __f, _InputIterator __l)
5853 : __p_(__f, __l) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005854#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005856 param_type(initializer_list<double> __wl)
5857 : __p_(__wl.begin(), __wl.end()) {__init();}
Howard Hinnante3e32912011-08-12 21:56:02 +00005858#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005859 template<class _UnaryOperation>
5860 param_type(size_t __nw, double __xmin, double __xmax,
5861 _UnaryOperation __fw);
5862
5863 vector<double> probabilities() const;
5864
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005865 friend _LIBCPP_INLINE_VISIBILITY
5866 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005867 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005868 friend _LIBCPP_INLINE_VISIBILITY
5869 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005870 {return !(__x == __y);}
5871
5872 private:
5873 void __init();
5874
5875 friend class discrete_distribution;
5876
5877 template <class _CharT, class _Traits, class _IT>
5878 friend
5879 basic_ostream<_CharT, _Traits>&
5880 operator<<(basic_ostream<_CharT, _Traits>& __os,
5881 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005882
Howard Hinnant551d8e42010-05-19 01:53:57 +00005883 template <class _CharT, class _Traits, class _IT>
5884 friend
5885 basic_istream<_CharT, _Traits>&
5886 operator>>(basic_istream<_CharT, _Traits>& __is,
5887 discrete_distribution<_IT>& __x);
5888 };
5889
5890private:
5891 param_type __p_;
5892
5893public:
5894 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005896 discrete_distribution() {}
5897 template<class _InputIterator>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005899 discrete_distribution(_InputIterator __f, _InputIterator __l)
5900 : __p_(__f, __l) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005901#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005903 discrete_distribution(initializer_list<double> __wl)
5904 : __p_(__wl) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00005905#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant551d8e42010-05-19 01:53:57 +00005906 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005908 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5909 _UnaryOperation __fw)
5910 : __p_(__nw, __xmin, __xmax, __fw) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00005912 explicit discrete_distribution(const param_type& __p)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005913 : __p_(__p) {}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005915 void reset() {}
5916
5917 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005918 template<class _URNG>
5919 _LIBCPP_INLINE_VISIBILITY
5920 result_type operator()(_URNG& __g)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005921 {return (*this)(__g, __p_);}
5922 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5923
5924 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005926 vector<double> probabilities() const {return __p_.probabilities();}
5927
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005929 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005931 void param(const param_type& __p) {__p_ = __p;}
5932
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005934 result_type min() const {return 0;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant551d8e42010-05-19 01:53:57 +00005936 result_type max() const {return __p_.__p_.size();}
5937
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005938 friend _LIBCPP_INLINE_VISIBILITY
5939 bool operator==(const discrete_distribution& __x,
5940 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005941 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00005942 friend _LIBCPP_INLINE_VISIBILITY
5943 bool operator!=(const discrete_distribution& __x,
5944 const discrete_distribution& __y)
Howard Hinnant551d8e42010-05-19 01:53:57 +00005945 {return !(__x == __y);}
5946
5947 template <class _CharT, class _Traits, class _IT>
5948 friend
5949 basic_ostream<_CharT, _Traits>&
5950 operator<<(basic_ostream<_CharT, _Traits>& __os,
5951 const discrete_distribution<_IT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00005952
Howard Hinnant551d8e42010-05-19 01:53:57 +00005953 template <class _CharT, class _Traits, class _IT>
5954 friend
5955 basic_istream<_CharT, _Traits>&
5956 operator>>(basic_istream<_CharT, _Traits>& __is,
5957 discrete_distribution<_IT>& __x);
5958};
5959
5960template<class _IntType>
5961template<class _UnaryOperation>
5962discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5963 double __xmin,
5964 double __xmax,
5965 _UnaryOperation __fw)
5966{
5967 if (__nw > 1)
5968 {
5969 __p_.reserve(__nw - 1);
5970 double __d = (__xmax - __xmin) / __nw;
5971 double __d2 = __d / 2;
5972 for (size_t __k = 0; __k < __nw; ++__k)
5973 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5974 __init();
5975 }
5976}
5977
5978template<class _IntType>
5979void
5980discrete_distribution<_IntType>::param_type::__init()
5981{
5982 if (!__p_.empty())
5983 {
5984 if (__p_.size() > 1)
5985 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005986 double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
5987 for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
Howard Hinnant551d8e42010-05-19 01:53:57 +00005988 __i < __e; ++__i)
5989 *__i /= __s;
5990 vector<double> __t(__p_.size() - 1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005991 _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00005992 swap(__p_, __t);
5993 }
5994 else
5995 {
5996 __p_.clear();
5997 __p_.shrink_to_fit();
5998 }
5999 }
6000}
6001
6002template<class _IntType>
6003vector<double>
6004discrete_distribution<_IntType>::param_type::probabilities() const
6005{
6006 size_t __n = __p_.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00006007 _VSTD::vector<double> __p(__n+1);
6008 _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
Howard Hinnant551d8e42010-05-19 01:53:57 +00006009 if (__n > 0)
6010 __p[__n] = 1 - __p_[__n-1];
6011 else
6012 __p[0] = 1;
6013 return __p;
6014}
6015
6016template<class _IntType>
6017template<class _URNG>
6018_IntType
6019discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
6020{
6021 uniform_real_distribution<double> __gen;
6022 return static_cast<_IntType>(
Howard Hinnant0949eed2011-06-30 21:18:19 +00006023 _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
Howard Hinnant551d8e42010-05-19 01:53:57 +00006024 __p.__p_.begin());
6025}
6026
6027template <class _CharT, class _Traits, class _IT>
6028basic_ostream<_CharT, _Traits>&
6029operator<<(basic_ostream<_CharT, _Traits>& __os,
6030 const discrete_distribution<_IT>& __x)
6031{
Howard Hinnant9c0df142012-10-30 19:06:59 +00006032 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00006033 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6034 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:57 +00006035 _CharT __sp = __os.widen(' ');
6036 __os.fill(__sp);
6037 size_t __n = __x.__p_.__p_.size();
6038 __os << __n;
6039 for (size_t __i = 0; __i < __n; ++__i)
6040 __os << __sp << __x.__p_.__p_[__i];
6041 return __os;
6042}
6043
6044template <class _CharT, class _Traits, class _IT>
6045basic_istream<_CharT, _Traits>&
6046operator>>(basic_istream<_CharT, _Traits>& __is,
6047 discrete_distribution<_IT>& __x)
6048{
Howard Hinnant9c0df142012-10-30 19:06:59 +00006049 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant551d8e42010-05-19 01:53:57 +00006050 __is.flags(ios_base::dec | ios_base::skipws);
6051 size_t __n;
6052 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006053 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:57 +00006054 for (size_t __i = 0; __i < __n; ++__i)
6055 __is >> __p[__i];
6056 if (!__is.fail())
6057 swap(__x.__p_.__p_, __p);
6058 return __is;
6059}
6060
Howard Hinnantd6d11712010-05-20 15:11:46 +00006061// piecewise_constant_distribution
6062
6063template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00006064class _LIBCPP_TYPE_VIS_ONLY piecewise_constant_distribution
Howard Hinnantd6d11712010-05-20 15:11:46 +00006065{
6066public:
6067 // types
6068 typedef _RealType result_type;
6069
Howard Hinnant0f678bd2013-08-12 18:38:34 +00006070 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnantd6d11712010-05-20 15:11:46 +00006071 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00006072 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006073 vector<result_type> __densities_;
6074 vector<result_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006075 public:
6076 typedef piecewise_constant_distribution distribution_type;
6077
6078 param_type();
6079 template<class _InputIteratorB, class _InputIteratorW>
6080 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6081 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00006082#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006083 template<class _UnaryOperation>
6084 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00006085#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006086 template<class _UnaryOperation>
6087 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6088 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006089 param_type & operator=(const param_type& __rhs);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006090
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006091 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006092 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006094 vector<result_type> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00006095
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006096 friend _LIBCPP_INLINE_VISIBILITY
6097 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:40 +00006098 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006099 friend _LIBCPP_INLINE_VISIBILITY
6100 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006101 {return !(__x == __y);}
6102
6103 private:
6104 void __init();
6105
6106 friend class piecewise_constant_distribution;
6107
6108 template <class _CharT, class _Traits, class _RT>
6109 friend
6110 basic_ostream<_CharT, _Traits>&
6111 operator<<(basic_ostream<_CharT, _Traits>& __os,
6112 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006113
Howard Hinnantd6d11712010-05-20 15:11:46 +00006114 template <class _CharT, class _Traits, class _RT>
6115 friend
6116 basic_istream<_CharT, _Traits>&
6117 operator>>(basic_istream<_CharT, _Traits>& __is,
6118 piecewise_constant_distribution<_RT>& __x);
6119 };
6120
6121private:
6122 param_type __p_;
6123
6124public:
6125 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006127 piecewise_constant_distribution() {}
6128 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006130 piecewise_constant_distribution(_InputIteratorB __fB,
6131 _InputIteratorB __lB,
6132 _InputIteratorW __fW)
6133 : __p_(__fB, __lB, __fW) {}
6134
Howard Hinnante3e32912011-08-12 21:56:02 +00006135#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006136 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006138 piecewise_constant_distribution(initializer_list<result_type> __bl,
6139 _UnaryOperation __fw)
6140 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006141#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantd6d11712010-05-20 15:11:46 +00006142
6143 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006145 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6146 result_type __xmax, _UnaryOperation __fw)
6147 : __p_(__nw, __xmin, __xmax, __fw) {}
6148
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006150 explicit piecewise_constant_distribution(const param_type& __p)
6151 : __p_(__p) {}
6152
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006154 void reset() {}
6155
6156 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006157 template<class _URNG>
6158 _LIBCPP_INLINE_VISIBILITY
6159 result_type operator()(_URNG& __g)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006160 {return (*this)(__g, __p_);}
6161 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6162
6163 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006165 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006167 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnantd6d11712010-05-20 15:11:46 +00006168
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006170 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006172 void param(const param_type& __p) {__p_ = __p;}
6173
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006175 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd6d11712010-05-20 15:11:46 +00006177 result_type max() const {return __p_.__b_.back();}
6178
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006179 friend _LIBCPP_INLINE_VISIBILITY
6180 bool operator==(const piecewise_constant_distribution& __x,
6181 const piecewise_constant_distribution& __y)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006182 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006183 friend _LIBCPP_INLINE_VISIBILITY
6184 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnantd6d11712010-05-20 15:11:46 +00006185 const piecewise_constant_distribution& __y)
6186 {return !(__x == __y);}
6187
6188 template <class _CharT, class _Traits, class _RT>
6189 friend
6190 basic_ostream<_CharT, _Traits>&
6191 operator<<(basic_ostream<_CharT, _Traits>& __os,
6192 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006193
Howard Hinnantd6d11712010-05-20 15:11:46 +00006194 template <class _CharT, class _Traits, class _RT>
6195 friend
6196 basic_istream<_CharT, _Traits>&
6197 operator>>(basic_istream<_CharT, _Traits>& __is,
6198 piecewise_constant_distribution<_RT>& __x);
6199};
6200
6201template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006202typename piecewise_constant_distribution<_RealType>::param_type &
6203piecewise_constant_distribution<_RealType>::param_type::operator=
6204 (const param_type& __rhs)
6205{
6206// These can throw
6207 __b_.reserve (__rhs.__b_.size ());
6208 __densities_.reserve(__rhs.__densities_.size());
6209 __areas_.reserve (__rhs.__areas_.size());
6210
6211// These can not throw
6212 __b_ = __rhs.__b_;
6213 __densities_ = __rhs.__densities_;
6214 __areas_ = __rhs.__areas_;
6215 return *this;
6216}
6217
6218template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006219void
6220piecewise_constant_distribution<_RealType>::param_type::__init()
6221{
Howard Hinnant2a592542010-05-24 00:35:40 +00006222 // __densities_ contains non-normalized areas
Howard Hinnant0949eed2011-06-30 21:18:19 +00006223 result_type __total_area = _VSTD::accumulate(__densities_.begin(),
Howard Hinnant2a592542010-05-24 00:35:40 +00006224 __densities_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006225 result_type());
Howard Hinnant2a592542010-05-24 00:35:40 +00006226 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6227 __densities_[__i] /= __total_area;
6228 // __densities_ contains normalized areas
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006229 __areas_.assign(__densities_.size(), result_type());
Howard Hinnant0949eed2011-06-30 21:18:19 +00006230 _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
Howard Hinnant2a592542010-05-24 00:35:40 +00006231 __areas_.begin() + 1);
6232 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6233 __densities_.back() = 1 - __areas_.back(); // correct round off error
6234 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6235 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6236 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:46 +00006237}
6238
6239template<class _RealType>
6240piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:40 +00006241 : __b_(2),
Howard Hinnant54305402010-05-25 00:27:34 +00006242 __densities_(1, 1.0),
6243 __areas_(1, 0.0)
Howard Hinnantd6d11712010-05-20 15:11:46 +00006244{
6245 __b_[1] = 1;
6246}
6247
6248template<class _RealType>
6249template<class _InputIteratorB, class _InputIteratorW>
6250piecewise_constant_distribution<_RealType>::param_type::param_type(
6251 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6252 : __b_(__fB, __lB)
6253{
6254 if (__b_.size() < 2)
6255 {
6256 __b_.resize(2);
6257 __b_[0] = 0;
6258 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006259 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006260 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006261 }
6262 else
6263 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006264 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006265 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:40 +00006266 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006267 __init();
6268 }
6269}
6270
Howard Hinnante3e32912011-08-12 21:56:02 +00006271#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6272
Howard Hinnantd6d11712010-05-20 15:11:46 +00006273template<class _RealType>
6274template<class _UnaryOperation>
6275piecewise_constant_distribution<_RealType>::param_type::param_type(
6276 initializer_list<result_type> __bl, _UnaryOperation __fw)
6277 : __b_(__bl.begin(), __bl.end())
6278{
6279 if (__b_.size() < 2)
6280 {
6281 __b_.resize(2);
6282 __b_[0] = 0;
6283 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00006284 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00006285 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006286 }
6287 else
6288 {
Howard Hinnant2a592542010-05-24 00:35:40 +00006289 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006290 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006291 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006292 __init();
6293 }
6294}
6295
Howard Hinnante3e32912011-08-12 21:56:02 +00006296#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6297
Howard Hinnantd6d11712010-05-20 15:11:46 +00006298template<class _RealType>
6299template<class _UnaryOperation>
6300piecewise_constant_distribution<_RealType>::param_type::param_type(
6301 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6302 : __b_(__nw == 0 ? 2 : __nw + 1)
6303{
6304 size_t __n = __b_.size() - 1;
6305 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:40 +00006306 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006307 for (size_t __i = 0; __i < __n; ++__i)
6308 {
6309 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:40 +00006310 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00006311 }
6312 __b_[__n] = __xmax;
6313 __init();
6314}
6315
6316template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00006317template<class _URNG>
6318_RealType
6319piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6320{
6321 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006322 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006323 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006324 __u) - __p.__areas_.begin() - 1;
6325 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006326}
6327
6328template <class _CharT, class _Traits, class _RT>
6329basic_ostream<_CharT, _Traits>&
6330operator<<(basic_ostream<_CharT, _Traits>& __os,
6331 const piecewise_constant_distribution<_RT>& __x)
6332{
Howard Hinnant9c0df142012-10-30 19:06:59 +00006333 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00006334 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6335 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006336 _CharT __sp = __os.widen(' ');
6337 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:40 +00006338 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006339 __os << __n;
6340 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006341 __os << __sp << __x.__p_.__b_[__i];
6342 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00006343 __os << __sp << __n;
6344 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00006345 __os << __sp << __x.__p_.__densities_[__i];
6346 __n = __x.__p_.__areas_.size();
6347 __os << __sp << __n;
6348 for (size_t __i = 0; __i < __n; ++__i)
6349 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006350 return __os;
6351}
6352
6353template <class _CharT, class _Traits, class _RT>
6354basic_istream<_CharT, _Traits>&
6355operator>>(basic_istream<_CharT, _Traits>& __is,
6356 piecewise_constant_distribution<_RT>& __x)
6357{
6358 typedef piecewise_constant_distribution<_RT> _Eng;
6359 typedef typename _Eng::result_type result_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00006360 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006361 __is.flags(ios_base::dec | ios_base::skipws);
6362 size_t __n;
6363 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00006364 vector<result_type> __b(__n);
6365 for (size_t __i = 0; __i < __n; ++__i)
6366 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:40 +00006367 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006368 vector<result_type> __densities(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006369 for (size_t __i = 0; __i < __n; ++__i)
6370 __is >> __densities[__i];
6371 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006372 vector<result_type> __areas(__n);
Howard Hinnant2a592542010-05-24 00:35:40 +00006373 for (size_t __i = 0; __i < __n; ++__i)
6374 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00006375 if (!__is.fail())
6376 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00006377 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:40 +00006378 swap(__x.__p_.__densities_, __densities);
6379 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:46 +00006380 }
6381 return __is;
6382}
6383
Howard Hinnant54305402010-05-25 00:27:34 +00006384// piecewise_linear_distribution
6385
6386template<class _RealType = double>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00006387class _LIBCPP_TYPE_VIS_ONLY piecewise_linear_distribution
Howard Hinnant54305402010-05-25 00:27:34 +00006388{
6389public:
6390 // types
6391 typedef _RealType result_type;
6392
Howard Hinnant0f678bd2013-08-12 18:38:34 +00006393 class _LIBCPP_TYPE_VIS_ONLY param_type
Howard Hinnant54305402010-05-25 00:27:34 +00006394 {
Howard Hinnant54305402010-05-25 00:27:34 +00006395 vector<result_type> __b_;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006396 vector<result_type> __densities_;
6397 vector<result_type> __areas_;
Howard Hinnant54305402010-05-25 00:27:34 +00006398 public:
6399 typedef piecewise_linear_distribution distribution_type;
6400
6401 param_type();
6402 template<class _InputIteratorB, class _InputIteratorW>
6403 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6404 _InputIteratorW __fW);
Howard Hinnante3e32912011-08-12 21:56:02 +00006405#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006406 template<class _UnaryOperation>
6407 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Howard Hinnante3e32912011-08-12 21:56:02 +00006408#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006409 template<class _UnaryOperation>
6410 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6411 _UnaryOperation __fw);
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006412 param_type & operator=(const param_type& __rhs);
6413
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006415 vector<result_type> intervals() const {return __b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006417 vector<result_type> densities() const {return __densities_;}
Howard Hinnant54305402010-05-25 00:27:34 +00006418
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006419 friend _LIBCPP_INLINE_VISIBILITY
6420 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006421 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006422 friend _LIBCPP_INLINE_VISIBILITY
6423 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006424 {return !(__x == __y);}
6425
6426 private:
6427 void __init();
6428
6429 friend class piecewise_linear_distribution;
6430
6431 template <class _CharT, class _Traits, class _RT>
6432 friend
6433 basic_ostream<_CharT, _Traits>&
6434 operator<<(basic_ostream<_CharT, _Traits>& __os,
6435 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006436
Howard Hinnant54305402010-05-25 00:27:34 +00006437 template <class _CharT, class _Traits, class _RT>
6438 friend
6439 basic_istream<_CharT, _Traits>&
6440 operator>>(basic_istream<_CharT, _Traits>& __is,
6441 piecewise_linear_distribution<_RT>& __x);
6442 };
6443
6444private:
6445 param_type __p_;
6446
6447public:
6448 // constructor and reset functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006450 piecewise_linear_distribution() {}
6451 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006453 piecewise_linear_distribution(_InputIteratorB __fB,
6454 _InputIteratorB __lB,
6455 _InputIteratorW __fW)
6456 : __p_(__fB, __lB, __fW) {}
Howard Hinnant324bb032010-08-22 00:02:43 +00006457
Howard Hinnante3e32912011-08-12 21:56:02 +00006458#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006459 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006461 piecewise_linear_distribution(initializer_list<result_type> __bl,
6462 _UnaryOperation __fw)
6463 : __p_(__bl, __fw) {}
Howard Hinnante3e32912011-08-12 21:56:02 +00006464#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant54305402010-05-25 00:27:34 +00006465
6466 template<class _UnaryOperation>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006468 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6469 result_type __xmax, _UnaryOperation __fw)
6470 : __p_(__nw, __xmin, __xmax, __fw) {}
6471
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006473 explicit piecewise_linear_distribution(const param_type& __p)
6474 : __p_(__p) {}
6475
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006477 void reset() {}
6478
6479 // generating functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006480 template<class _URNG>
6481 _LIBCPP_INLINE_VISIBILITY
6482 result_type operator()(_URNG& __g)
Howard Hinnant54305402010-05-25 00:27:34 +00006483 {return (*this)(__g, __p_);}
6484 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6485
6486 // property functions
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006488 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006490 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant54305402010-05-25 00:27:34 +00006491
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006493 param_type param() const {return __p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006495 void param(const param_type& __p) {__p_ = __p;}
6496
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006498 result_type min() const {return __p_.__b_.front();}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54305402010-05-25 00:27:34 +00006500 result_type max() const {return __p_.__b_.back();}
6501
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006502 friend _LIBCPP_INLINE_VISIBILITY
6503 bool operator==(const piecewise_linear_distribution& __x,
6504 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006505 {return __x.__p_ == __y.__p_;}
Howard Hinnantb9af2ea2010-09-22 18:02:38 +00006506 friend _LIBCPP_INLINE_VISIBILITY
6507 bool operator!=(const piecewise_linear_distribution& __x,
6508 const piecewise_linear_distribution& __y)
Howard Hinnant54305402010-05-25 00:27:34 +00006509 {return !(__x == __y);}
6510
6511 template <class _CharT, class _Traits, class _RT>
6512 friend
6513 basic_ostream<_CharT, _Traits>&
6514 operator<<(basic_ostream<_CharT, _Traits>& __os,
6515 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant324bb032010-08-22 00:02:43 +00006516
Howard Hinnant54305402010-05-25 00:27:34 +00006517 template <class _CharT, class _Traits, class _RT>
6518 friend
6519 basic_istream<_CharT, _Traits>&
6520 operator>>(basic_istream<_CharT, _Traits>& __is,
6521 piecewise_linear_distribution<_RT>& __x);
6522};
6523
6524template<class _RealType>
Howard Hinnant3c143ad2010-10-13 14:37:09 +00006525typename piecewise_linear_distribution<_RealType>::param_type &
6526piecewise_linear_distribution<_RealType>::param_type::operator=
6527 (const param_type& __rhs)
6528{
6529// These can throw
6530 __b_.reserve (__rhs.__b_.size ());
6531 __densities_.reserve(__rhs.__densities_.size());
6532 __areas_.reserve (__rhs.__areas_.size());
6533
6534// These can not throw
6535 __b_ = __rhs.__b_;
6536 __densities_ = __rhs.__densities_;
6537 __areas_ = __rhs.__areas_;
6538 return *this;
6539}
6540
6541
6542template<class _RealType>
Howard Hinnant54305402010-05-25 00:27:34 +00006543void
6544piecewise_linear_distribution<_RealType>::param_type::__init()
6545{
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006546 __areas_.assign(__densities_.size() - 1, result_type());
Howard Hinnant99968442011-11-29 18:15:50 +00006547 result_type _Sp = 0;
Howard Hinnant54305402010-05-25 00:27:34 +00006548 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6549 {
6550 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6551 (__b_[__i+1] - __b_[__i]) * .5;
Howard Hinnant99968442011-11-29 18:15:50 +00006552 _Sp += __areas_[__i];
Howard Hinnant54305402010-05-25 00:27:34 +00006553 }
6554 for (size_t __i = __areas_.size(); __i > 1;)
6555 {
6556 --__i;
Howard Hinnant99968442011-11-29 18:15:50 +00006557 __areas_[__i] = __areas_[__i-1] / _Sp;
Howard Hinnant54305402010-05-25 00:27:34 +00006558 }
6559 __areas_[0] = 0;
6560 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6561 __areas_[__i] += __areas_[__i-1];
6562 for (size_t __i = 0; __i < __densities_.size(); ++__i)
Howard Hinnant99968442011-11-29 18:15:50 +00006563 __densities_[__i] /= _Sp;
Howard Hinnant54305402010-05-25 00:27:34 +00006564}
6565
6566template<class _RealType>
6567piecewise_linear_distribution<_RealType>::param_type::param_type()
6568 : __b_(2),
6569 __densities_(2, 1.0),
6570 __areas_(1, 0.0)
6571{
6572 __b_[1] = 1;
6573}
6574
6575template<class _RealType>
6576template<class _InputIteratorB, class _InputIteratorW>
6577piecewise_linear_distribution<_RealType>::param_type::param_type(
6578 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6579 : __b_(__fB, __lB)
6580{
6581 if (__b_.size() < 2)
6582 {
6583 __b_.resize(2);
6584 __b_[0] = 0;
6585 __b_[1] = 1;
6586 __densities_.assign(2, 1.0);
6587 __areas_.assign(1, 0.0);
6588 }
6589 else
6590 {
6591 __densities_.reserve(__b_.size());
6592 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6593 __densities_.push_back(*__fW);
6594 __init();
6595 }
6596}
6597
Howard Hinnante3e32912011-08-12 21:56:02 +00006598#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6599
Howard Hinnant54305402010-05-25 00:27:34 +00006600template<class _RealType>
6601template<class _UnaryOperation>
6602piecewise_linear_distribution<_RealType>::param_type::param_type(
6603 initializer_list<result_type> __bl, _UnaryOperation __fw)
6604 : __b_(__bl.begin(), __bl.end())
6605{
6606 if (__b_.size() < 2)
6607 {
6608 __b_.resize(2);
6609 __b_[0] = 0;
6610 __b_[1] = 1;
6611 __densities_.assign(2, 1.0);
6612 __areas_.assign(1, 0.0);
6613 }
6614 else
6615 {
6616 __densities_.reserve(__b_.size());
6617 for (size_t __i = 0; __i < __b_.size(); ++__i)
6618 __densities_.push_back(__fw(__b_[__i]));
6619 __init();
6620 }
6621}
6622
Howard Hinnante3e32912011-08-12 21:56:02 +00006623#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6624
Howard Hinnant54305402010-05-25 00:27:34 +00006625template<class _RealType>
6626template<class _UnaryOperation>
6627piecewise_linear_distribution<_RealType>::param_type::param_type(
6628 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6629 : __b_(__nw == 0 ? 2 : __nw + 1)
6630{
6631 size_t __n = __b_.size() - 1;
6632 result_type __d = (__xmax - __xmin) / __n;
6633 __densities_.reserve(__b_.size());
6634 for (size_t __i = 0; __i < __n; ++__i)
6635 {
6636 __b_[__i] = __xmin + __i * __d;
6637 __densities_.push_back(__fw(__b_[__i]));
6638 }
6639 __b_[__n] = __xmax;
6640 __densities_.push_back(__fw(__b_[__n]));
6641 __init();
6642}
6643
6644template<class _RealType>
6645template<class _URNG>
6646_RealType
6647piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6648{
6649 typedef uniform_real_distribution<result_type> _Gen;
6650 result_type __u = _Gen()(__g);
Howard Hinnant0949eed2011-06-30 21:18:19 +00006651 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006652 __u) - __p.__areas_.begin() - 1;
Howard Hinnant54305402010-05-25 00:27:34 +00006653 __u -= __p.__areas_[__k];
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006654 const result_type __dk = __p.__densities_[__k];
6655 const result_type __dk1 = __p.__densities_[__k+1];
6656 const result_type __deltad = __dk1 - __dk;
Howard Hinnant54305402010-05-25 00:27:34 +00006657 const result_type __bk = __p.__b_[__k];
6658 if (__deltad == 0)
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006659 return __u / __dk + __bk;
Howard Hinnant54305402010-05-25 00:27:34 +00006660 const result_type __bk1 = __p.__b_[__k+1];
6661 const result_type __deltab = __bk1 - __bk;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006662 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnant0949eed2011-06-30 21:18:19 +00006663 _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006664 __deltad;
Howard Hinnant54305402010-05-25 00:27:34 +00006665}
6666
6667template <class _CharT, class _Traits, class _RT>
6668basic_ostream<_CharT, _Traits>&
6669operator<<(basic_ostream<_CharT, _Traits>& __os,
6670 const piecewise_linear_distribution<_RT>& __x)
6671{
Howard Hinnant9c0df142012-10-30 19:06:59 +00006672 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant54305402010-05-25 00:27:34 +00006673 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6674 ios_base::scientific);
6675 _CharT __sp = __os.widen(' ');
6676 __os.fill(__sp);
6677 size_t __n = __x.__p_.__b_.size();
6678 __os << __n;
6679 for (size_t __i = 0; __i < __n; ++__i)
6680 __os << __sp << __x.__p_.__b_[__i];
6681 __n = __x.__p_.__densities_.size();
6682 __os << __sp << __n;
6683 for (size_t __i = 0; __i < __n; ++__i)
6684 __os << __sp << __x.__p_.__densities_[__i];
6685 __n = __x.__p_.__areas_.size();
6686 __os << __sp << __n;
6687 for (size_t __i = 0; __i < __n; ++__i)
6688 __os << __sp << __x.__p_.__areas_[__i];
6689 return __os;
6690}
6691
6692template <class _CharT, class _Traits, class _RT>
6693basic_istream<_CharT, _Traits>&
6694operator>>(basic_istream<_CharT, _Traits>& __is,
6695 piecewise_linear_distribution<_RT>& __x)
6696{
6697 typedef piecewise_linear_distribution<_RT> _Eng;
6698 typedef typename _Eng::result_type result_type;
Howard Hinnant9c0df142012-10-30 19:06:59 +00006699 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant54305402010-05-25 00:27:34 +00006700 __is.flags(ios_base::dec | ios_base::skipws);
6701 size_t __n;
6702 __is >> __n;
6703 vector<result_type> __b(__n);
6704 for (size_t __i = 0; __i < __n; ++__i)
6705 __is >> __b[__i];
6706 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006707 vector<result_type> __densities(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006708 for (size_t __i = 0; __i < __n; ++__i)
6709 __is >> __densities[__i];
6710 __is >> __n;
Howard Hinnant9650b6c2010-11-18 17:01:36 +00006711 vector<result_type> __areas(__n);
Howard Hinnant54305402010-05-25 00:27:34 +00006712 for (size_t __i = 0; __i < __n; ++__i)
6713 __is >> __areas[__i];
6714 if (!__is.fail())
6715 {
6716 swap(__x.__p_.__b_, __b);
6717 swap(__x.__p_.__densities_, __densities);
6718 swap(__x.__p_.__areas_, __areas);
6719 }
6720 return __is;
6721}
6722
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00006723_LIBCPP_END_NAMESPACE_STD
6724
6725#endif // _LIBCPP_RANDOM