blob: a73239519ac1f9b2300dc768a24fc37ffa5fc2a1 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- random -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_RANDOM
11#define _LIBCPP_RANDOM
12
13/*
14 random synopsis
15
16#include <initializer_list>
17
18namespace std
19{
20
21// Engines
22
23template <class UIntType, UIntType a, UIntType c, UIntType m>
24class linear_congruential_engine
25{
26public:
27 // types
28 typedef UIntType result_type;
29
30 // engine characteristics
31 static constexpr result_type multiplier = a;
32 static constexpr result_type increment = c;
33 static constexpr result_type modulus = m;
34 static constexpr result_type min() { return c == 0u ? 1u: 0u;}
35 static constexpr result_type max() { return m - 1u;}
36 static constexpr result_type default_seed = 1u;
37
38 // constructors and seeding functions
39 explicit linear_congruential_engine(result_type s = default_seed);
40 template<class Sseq> explicit linear_congruential_engine(Sseq& q);
41 void seed(result_type s = default_seed);
42 template<class Sseq> void seed(Sseq& q);
43
44 // generating functions
45 result_type operator()();
46 void discard(unsigned long long z);
47};
48
49template <class UIntType, UIntType a, UIntType c, UIntType m>
50bool
51operator==(const linear_congruential_engine<UIntType, a, c, m>& x,
52 const linear_congruential_engine<UIntType, a, c, m>& y);
53
54template <class UIntType, UIntType a, UIntType c, UIntType m>
55bool
56operator!=(const linear_congruential_engine<UIntType, a, c, m>& x,
57 const linear_congruential_engine<UIntType, a, c, m>& y);
58
59template <class charT, class traits,
60 class UIntType, UIntType a, UIntType c, UIntType m>
61basic_ostream<charT, traits>&
62operator<<(basic_ostream<charT, traits>& os,
63 const linear_congruential_engine<UIntType, a, c, m>& x);
64
65template <class charT, class traits,
66 class UIntType, UIntType a, UIntType c, UIntType m>
67basic_istream<charT, traits>&
68operator>>(basic_istream<charT, traits>& is,
69 linear_congruential_engine<UIntType, a, c, m>& x);
70
71template <class UIntType, size_t w, size_t n, size_t m, size_t r,
72 UIntType a, size_t u, UIntType d, size_t s,
73 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
74class mersenne_twister_engine
75{
76public:
77 // types
78 typedef UIntType result_type;
79
80 // engine characteristics
81 static constexpr size_t word_size = w;
82 static constexpr size_t state_size = n;
83 static constexpr size_t shift_size = m;
84 static constexpr size_t mask_bits = r;
85 static constexpr result_type xor_mask = a;
86 static constexpr size_t tempering_u = u;
87 static constexpr result_type tempering_d = d;
88 static constexpr size_t tempering_s = s;
89 static constexpr result_type tempering_b = b;
90 static constexpr size_t tempering_t = t;
91 static constexpr result_type tempering_c = c;
92 static constexpr size_t tempering_l = l;
93 static constexpr result_type initialization_multiplier = f;
94 static constexpr result_type min () { return 0; }
95 static constexpr result_type max() { return 2^w - 1; }
96 static constexpr result_type default_seed = 5489u;
97
98 // constructors and seeding functions
99 explicit mersenne_twister_engine(result_type value = default_seed);
100 template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
101 void seed(result_type value = default_seed);
102 template<class Sseq> void seed(Sseq& q);
103
104 // generating functions
105 result_type operator()();
106 void discard(unsigned long long z);
107};
108
109template <class UIntType, size_t w, size_t n, size_t m, size_t r,
110 UIntType a, size_t u, UIntType d, size_t s,
111 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
112bool
113operator==(
114 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
115 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
116
117template <class UIntType, size_t w, size_t n, size_t m, size_t r,
118 UIntType a, size_t u, UIntType d, size_t s,
119 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
120bool
121operator!=(
122 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
123 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
124
125template <class charT, class traits,
126 class UIntType, size_t w, size_t n, size_t m, size_t r,
127 UIntType a, size_t u, UIntType d, size_t s,
128 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
129basic_ostream<charT, traits>&
130operator<<(basic_ostream<charT, traits>& os,
131 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
132
133template <class charT, class traits,
134 class UIntType, size_t w, size_t n, size_t m, size_t r,
135 UIntType a, size_t u, UIntType d, size_t s,
136 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
137basic_istream<charT, traits>&
138operator>>(basic_istream<charT, traits>& is,
139 mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
140
141template<class UIntType, size_t w, size_t s, size_t r>
142class subtract_with_carry_engine
143{
144public:
145 // types
146 typedef UIntType result_type;
147
148 // engine characteristics
149 static constexpr size_t word_size = w;
150 static constexpr size_t short_lag = s;
151 static constexpr size_t long_lag = r;
152 static constexpr result_type min() { return 0; }
153 static constexpr result_type max() { return m-1; }
154 static constexpr result_type default_seed = 19780503u;
155
156 // constructors and seeding functions
157 explicit subtract_with_carry_engine(result_type value = default_seed);
158 template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
159 void seed(result_type value = default_seed);
160 template<class Sseq> void seed(Sseq& q);
161
162 // generating functions
163 result_type operator()();
164 void discard(unsigned long long z);
165};
166
167template<class UIntType, size_t w, size_t s, size_t r>
168bool
169operator==(
170 const subtract_with_carry_engine<UIntType, w, s, r>& x,
171 const subtract_with_carry_engine<UIntType, w, s, r>& y);
172
173template<class UIntType, size_t w, size_t s, size_t r>
174bool
175operator!=(
176 const subtract_with_carry_engine<UIntType, w, s, r>& x,
177 const subtract_with_carry_engine<UIntType, w, s, r>& y);
178
179template <class charT, class traits,
180 class UIntType, size_t w, size_t s, size_t r>
181basic_ostream<charT, traits>&
182operator<<(basic_ostream<charT, traits>& os,
183 const subtract_with_carry_engine<UIntType, w, s, r>& x);
184
185template <class charT, class traits,
186 class UIntType, size_t w, size_t s, size_t r>
187basic_istream<charT, traits>&
188operator>>(basic_istream<charT, traits>& is,
189 subtract_with_carry_engine<UIntType, w, s, r>& x);
190
191template<class Engine, size_t p, size_t r>
192class discard_block_engine
193{
194public:
195 // types
196 typedef typename Engine::result_type result_type;
197
198 // engine characteristics
199 static constexpr size_t block_size = p;
200 static constexpr size_t used_block = r;
201 static constexpr result_type min() { return Engine::min(); }
202 static constexpr result_type max() { return Engine::max(); }
203
204 // constructors and seeding functions
205 discard_block_engine();
206 explicit discard_block_engine(const Engine& e);
207 explicit discard_block_engine(Engine&& e);
208 explicit discard_block_engine(result_type s);
209 template<class Sseq> explicit discard_block_engine(Sseq& q);
210 void seed();
211 void seed(result_type s);
212 template<class Sseq> void seed(Sseq& q);
213
214 // generating functions
215 result_type operator()();
216 void discard(unsigned long long z);
217
218 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000219 const Engine& base() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220};
221
222template<class Engine, size_t p, size_t r>
223bool
224operator==(
225 const discard_block_engine<Engine, p, r>& x,
226 const discard_block_engine<Engine, p, r>& y);
227
228template<class Engine, size_t p, size_t r>
229bool
230operator!=(
231 const discard_block_engine<Engine, p, r>& x,
232 const discard_block_engine<Engine, p, r>& y);
233
234template <class charT, class traits,
235 class Engine, size_t p, size_t r>
236basic_ostream<charT, traits>&
237operator<<(basic_ostream<charT, traits>& os,
238 const discard_block_engine<Engine, p, r>& x);
239
240template <class charT, class traits,
241 class Engine, size_t p, size_t r>
242basic_istream<charT, traits>&
243operator>>(basic_istream<charT, traits>& is,
244 discard_block_engine<Engine, p, r>& x);
245
246template<class Engine, size_t w, class UIntType>
247class independent_bits_engine
248{
249public:
250 // types
251 typedef UIntType result_type;
252
253 // engine characteristics
254 static constexpr result_type min() { return 0; }
255 static constexpr result_type max() { return 2^w - 1; }
256
257 // constructors and seeding functions
258 independent_bits_engine();
259 explicit independent_bits_engine(const Engine& e);
260 explicit independent_bits_engine(Engine&& e);
261 explicit independent_bits_engine(result_type s);
262 template<class Sseq> explicit independent_bits_engine(Sseq& q);
263 void seed();
264 void seed(result_type s);
265 template<class Sseq> void seed(Sseq& q);
266
267 // generating functions
268 result_type operator()(); void discard(unsigned long long z);
269
270 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000271 const Engine& base() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272};
273
274template<class Engine, size_t w, class UIntType>
275bool
276operator==(
277 const independent_bits_engine<Engine, w, UIntType>& x,
278 const independent_bits_engine<Engine, w, UIntType>& y);
279
280template<class Engine, size_t w, class UIntType>
281bool
282operator!=(
283 const independent_bits_engine<Engine, w, UIntType>& x,
284 const independent_bits_engine<Engine, w, UIntType>& y);
285
286template <class charT, class traits,
287 class Engine, size_t w, class UIntType>
288basic_ostream<charT, traits>&
289operator<<(basic_ostream<charT, traits>& os,
290 const independent_bits_engine<Engine, w, UIntType>& x);
291
292template <class charT, class traits,
293 class Engine, size_t w, class UIntType>
294basic_istream<charT, traits>&
295operator>>(basic_istream<charT, traits>& is,
296 independent_bits_engine<Engine, w, UIntType>& x);
297
298template<class Engine, size_t k>
299class shuffle_order_engine
300{
301public:
302 // types
303 typedef typename Engine::result_type result_type;
304
305 // engine characteristics
306 static constexpr size_t table_size = k;
307 static constexpr result_type min() { return Engine::min; }
308 static constexpr result_type max() { return Engine::max; }
309
310 // constructors and seeding functions
311 shuffle_order_engine();
312 explicit shuffle_order_engine(const Engine& e);
313 explicit shuffle_order_engine(Engine&& e);
314 explicit shuffle_order_engine(result_type s);
315 template<class Sseq> explicit shuffle_order_engine(Sseq& q);
316 void seed();
317 void seed(result_type s);
318 template<class Sseq> void seed(Sseq& q);
319
320 // generating functions
321 result_type operator()();
322 void discard(unsigned long long z);
323
324 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000325 const Engine& base() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326};
327
328template<class Engine, size_t k>
329bool
330operator==(
331 const shuffle_order_engine<Engine, k>& x,
332 const shuffle_order_engine<Engine, k>& y);
333
334template<class Engine, size_t k>
335bool
336operator!=(
337 const shuffle_order_engine<Engine, k>& x,
338 const shuffle_order_engine<Engine, k>& y);
339
340template <class charT, class traits,
341 class Engine, size_t k>
342basic_ostream<charT, traits>&
343operator<<(basic_ostream<charT, traits>& os,
344 const shuffle_order_engine<Engine, k>& x);
345
346template <class charT, class traits,
347 class Engine, size_t k>
348basic_istream<charT, traits>&
349operator>>(basic_istream<charT, traits>& is,
350 shuffle_order_engine<Engine, k>& x);
351
352typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
353 minstd_rand0;
354typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
355 minstd_rand;
356typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
357 0x9908b0df,
358 11, 0xffffffff,
359 7, 0x9d2c5680,
360 15, 0xefc60000,
361 18, 1812433253> mt19937;
362typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
363 0xb5026f5aa96619e9,
364 29, 0x5555555555555555,
365 17, 0x71d67fffeda60000,
366 37, 0xfff7eee000000000,
367 43, 6364136223846793005> mt19937_64;
368typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
369typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
370typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
371typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
372typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
Howard Hinnant481ba382010-05-20 15:11:46 +0000373typedef minstd_rand default_random_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374
375// Generators
376
377class random_device
378{
379public:
380 // types
381 typedef unsigned int result_type;
382
383 // generator characteristics
384 static constexpr result_type min() { return numeric_limits<result_type>::min(); }
385 static constexpr result_type max() { return numeric_limits<result_type>::max(); }
386
387 // constructors
388 explicit random_device(const string& token = "/dev/urandom");
389
390 // generating functions
391 result_type operator()();
392
393 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000394 double entropy() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395
396 // no copy functions
397 random_device(const random_device& ) = delete;
398 void operator=(const random_device& ) = delete;
399};
400
401// Utilities
402
403class seed_seq
404{
405public:
406 // types
407 typedef uint_least32_t result_type;
408
409 // constructors
410 seed_seq();
411 template<class T>
412 seed_seq(initializer_list<T> il);
413 template<class InputIterator>
414 seed_seq(InputIterator begin, InputIterator end);
415
416 // generating functions
417 template<class RandomAccessIterator>
418 void generate(RandomAccessIterator begin, RandomAccessIterator end);
419
420 // property functions
421 size_t size() const;
422 template<class OutputIterator>
423 void param(OutputIterator dest) const;
424
425 // no copy functions
426 seed_seq(const seed_seq&) = delete;
427 void operator=(const seed_seq& ) = delete;
428};
429
430template<class RealType, size_t bits, class URNG>
431 RealType generate_canonical(URNG& g);
432
433// Distributions
434
435template<class IntType = int>
436class uniform_int_distribution
437{
438public:
439 // types
440 typedef IntType result_type;
441
442 class param_type
443 {
444 public:
445 typedef uniform_int_distribution distribution_type;
446
447 explicit param_type(IntType a = 0,
448 IntType b = numeric_limits<IntType>::max());
449
450 result_type a() const;
451 result_type b() const;
452
453 friend bool operator==(const param_type& x, const param_type& y);
454 friend bool operator!=(const param_type& x, const param_type& y);
455 };
456
457 // constructors and reset functions
458 explicit uniform_int_distribution(IntType a = 0,
459 IntType b = numeric_limits<IntType>::max());
460 explicit uniform_int_distribution(const param_type& parm);
461 void reset();
462
463 // generating functions
464 template<class URNG> result_type operator()(URNG& g);
465 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
466
467 // property functions
468 result_type a() const;
469 result_type b() const;
470
471 param_type param() const;
472 void param(const param_type& parm);
473
474 result_type min() const;
475 result_type max() const;
476
477 friend bool operator==(const uniform_int_distribution& x,
478 const uniform_int_distribution& y);
479 friend bool operator!=(const uniform_int_distribution& x,
480 const uniform_int_distribution& y);
481
482 template <class charT, class traits>
483 friend
484 basic_ostream<charT, traits>&
485 operator<<(basic_ostream<charT, traits>& os,
486 const uniform_int_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000487
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488 template <class charT, class traits>
489 friend
490 basic_istream<charT, traits>&
491 operator>>(basic_istream<charT, traits>& is,
492 uniform_int_distribution& x);
493};
494
495template<class RealType = double>
496class uniform_real_distribution
497{
498public:
499 // types
500 typedef RealType result_type;
501
502 class param_type
503 {
504 public:
505 typedef uniform_real_distribution distribution_type;
506
507 explicit param_type(RealType a = 0,
508 RealType b = 1);
509
510 result_type a() const;
511 result_type b() const;
512
513 friend bool operator==(const param_type& x, const param_type& y);
514 friend bool operator!=(const param_type& x, const param_type& y);
515 };
516
517 // constructors and reset functions
518 explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);
519 explicit uniform_real_distribution(const param_type& parm);
520 void reset();
521
522 // generating functions
523 template<class URNG> result_type operator()(URNG& g);
524 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
525
526 // property functions
527 result_type a() const;
528 result_type b() const;
529
530 param_type param() const;
531 void param(const param_type& parm);
532
533 result_type min() const;
534 result_type max() const;
535
536 friend bool operator==(const uniform_real_distribution& x,
537 const uniform_real_distribution& y);
538 friend bool operator!=(const uniform_real_distribution& x,
539 const uniform_real_distribution& y);
540
541 template <class charT, class traits>
542 friend
543 basic_ostream<charT, traits>&
544 operator<<(basic_ostream<charT, traits>& os,
545 const uniform_real_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000546
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547 template <class charT, class traits>
548 friend
549 basic_istream<charT, traits>&
550 operator>>(basic_istream<charT, traits>& is,
551 uniform_real_distribution& x);
552};
553
554class bernoulli_distribution
555{
556public:
557 // types
558 typedef bool result_type;
559
560 class param_type
561 {
562 public:
563 typedef bernoulli_distribution distribution_type;
564
565 explicit param_type(double p = 0.5);
566
567 double p() const;
568
569 friend bool operator==(const param_type& x, const param_type& y);
570 friend bool operator!=(const param_type& x, const param_type& y);
571 };
572
573 // constructors and reset functions
574 explicit bernoulli_distribution(double p = 0.5);
575 explicit bernoulli_distribution(const param_type& parm);
576 void reset();
577
578 // generating functions
579 template<class URNG> result_type operator()(URNG& g);
580 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
581
582 // property functions
583 double p() const;
584
585 param_type param() const;
586 void param(const param_type& parm);
587
588 result_type min() const;
589 result_type max() const;
590
591 friend bool operator==(const bernoulli_distribution& x,
592 const bernoulli_distribution& y);
593 friend bool operator!=(const bernoulli_distribution& x,
594 const bernoulli_distribution& y);
595
596 template <class charT, class traits>
597 friend
598 basic_ostream<charT, traits>&
599 operator<<(basic_ostream<charT, traits>& os,
600 const bernoulli_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000601
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602 template <class charT, class traits>
603 friend
604 basic_istream<charT, traits>&
605 operator>>(basic_istream<charT, traits>& is,
606 bernoulli_distribution& x);
607};
608
609template<class IntType = int>
Howard Hinnant53f28fa2010-05-11 23:26:59 +0000610class binomial_distribution
611{
612public:
613 // types
614 typedef IntType result_type;
615
616 class param_type
617 {
618 public:
619 typedef binomial_distribution distribution_type;
620
621 explicit param_type(IntType t = 1, double p = 0.5);
622
623 IntType t() const;
624 double p() const;
625
626 friend bool operator==(const param_type& x, const param_type& y);
627 friend bool operator!=(const param_type& x, const param_type& y);
628 };
629
630 // constructors and reset functions
631 explicit binomial_distribution(IntType t = 1, double p = 0.5);
632 explicit binomial_distribution(const param_type& parm);
633 void reset();
634
635 // generating functions
636 template<class URNG> result_type operator()(URNG& g);
637 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
638
639 // property functions
640 IntType t() const;
641 double p() const;
642
643 param_type param() const;
644 void param(const param_type& parm);
645
646 result_type min() const;
647 result_type max() const;
648
649 friend bool operator==(const binomial_distribution& x,
650 const binomial_distribution& y);
651 friend bool operator!=(const binomial_distribution& x,
652 const binomial_distribution& y);
653
654 template <class charT, class traits>
655 friend
656 basic_ostream<charT, traits>&
657 operator<<(basic_ostream<charT, traits>& os,
658 const binomial_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000659
Howard Hinnant53f28fa2010-05-11 23:26:59 +0000660 template <class charT, class traits>
661 friend
662 basic_istream<charT, traits>&
663 operator>>(basic_istream<charT, traits>& is,
664 binomial_distribution& x);
665};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666
667template<class IntType = int>
Howard Hinnant28beb162010-05-17 13:44:27 +0000668class geometric_distribution
669{
670public:
671 // types
672 typedef IntType result_type;
673
674 class param_type
675 {
676 public:
677 typedef geometric_distribution distribution_type;
678
679 explicit param_type(double p = 0.5);
680
681 double p() const;
682
683 friend bool operator==(const param_type& x, const param_type& y);
684 friend bool operator!=(const param_type& x, const param_type& y);
685 };
686
687 // constructors and reset functions
688 explicit geometric_distribution(double p = 0.5);
689 explicit geometric_distribution(const param_type& parm);
690 void reset();
691
692 // generating functions
693 template<class URNG> result_type operator()(URNG& g);
694 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
695
696 // property functions
697 double p() const;
698
699 param_type param() const;
700 void param(const param_type& parm);
701
702 result_type min() const;
703 result_type max() const;
704
705 friend bool operator==(const geometric_distribution& x,
706 const geometric_distribution& y);
707 friend bool operator!=(const geometric_distribution& x,
708 const geometric_distribution& y);
709
710 template <class charT, class traits>
711 friend
712 basic_ostream<charT, traits>&
713 operator<<(basic_ostream<charT, traits>& os,
714 const geometric_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000715
Howard Hinnant28beb162010-05-17 13:44:27 +0000716 template <class charT, class traits>
717 friend
718 basic_istream<charT, traits>&
719 operator>>(basic_istream<charT, traits>& is,
720 geometric_distribution& x);
721};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722
723template<class IntType = int>
Howard Hinnant0f43ce62010-05-17 00:09:38 +0000724class negative_binomial_distribution
725{
726public:
727 // types
728 typedef IntType result_type;
729
730 class param_type
731 {
732 public:
733 typedef negative_binomial_distribution distribution_type;
734
735 explicit param_type(result_type k = 1, double p = 0.5);
736
737 result_type k() const;
738 double p() const;
739
740 friend bool operator==(const param_type& x, const param_type& y);
741 friend bool operator!=(const param_type& x, const param_type& y);
742 };
743
744 // constructor and reset functions
745 explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
746 explicit negative_binomial_distribution(const param_type& parm);
747 void reset();
748
749 // generating functions
750 template<class URNG> result_type operator()(URNG& g);
751 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
752
753 // property functions
754 result_type k() const;
755 double p() const;
756
757 param_type param() const;
758 void param(const param_type& parm);
759
760 result_type min() const;
761 result_type max() const;
762
763 friend bool operator==(const negative_binomial_distribution& x,
764 const negative_binomial_distribution& y);
765 friend bool operator!=(const negative_binomial_distribution& x,
766 const negative_binomial_distribution& y);
767
768 template <class charT, class traits>
769 friend
770 basic_ostream<charT, traits>&
771 operator<<(basic_ostream<charT, traits>& os,
772 const negative_binomial_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000773
Howard Hinnant0f43ce62010-05-17 00:09:38 +0000774 template <class charT, class traits>
775 friend
776 basic_istream<charT, traits>&
777 operator>>(basic_istream<charT, traits>& is,
778 negative_binomial_distribution& x);
779};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780
781template<class IntType = int>
Howard Hinnant24a5f2a2010-05-14 21:38:54 +0000782class poisson_distribution
783{
784public:
785 // types
786 typedef IntType result_type;
787
788 class param_type
789 {
790 public:
791 typedef poisson_distribution distribution_type;
792
793 explicit param_type(double mean = 1.0);
794
795 double mean() const;
796
797 friend bool operator==(const param_type& x, const param_type& y);
798 friend bool operator!=(const param_type& x, const param_type& y);
799 };
800
801 // constructors and reset functions
802 explicit poisson_distribution(double mean = 1.0);
803 explicit poisson_distribution(const param_type& parm);
804 void reset();
805
806 // generating functions
807 template<class URNG> result_type operator()(URNG& g);
808 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
809
810 // property functions
811 double mean() const;
812
813 param_type param() const;
814 void param(const param_type& parm);
815
816 result_type min() const;
817 result_type max() const;
818
819 friend bool operator==(const poisson_distribution& x,
820 const poisson_distribution& y);
821 friend bool operator!=(const poisson_distribution& x,
822 const poisson_distribution& y);
823
824 template <class charT, class traits>
825 friend
826 basic_ostream<charT, traits>&
827 operator<<(basic_ostream<charT, traits>& os,
828 const poisson_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000829
Howard Hinnant24a5f2a2010-05-14 21:38:54 +0000830 template <class charT, class traits>
831 friend
832 basic_istream<charT, traits>&
833 operator>>(basic_istream<charT, traits>& is,
834 poisson_distribution& x);
835};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836
837template<class RealType = double>
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000838class exponential_distribution
839{
840public:
841 // types
842 typedef RealType result_type;
843
844 class param_type
845 {
846 public:
847 typedef exponential_distribution distribution_type;
848
Howard Hinnant0b95bc92010-05-12 21:02:31 +0000849 explicit param_type(result_type lambda = 1.0);
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000850
Howard Hinnant0b95bc92010-05-12 21:02:31 +0000851 result_type lambda() const;
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000852
853 friend bool operator==(const param_type& x, const param_type& y);
854 friend bool operator!=(const param_type& x, const param_type& y);
855 };
856
857 // constructors and reset functions
Howard Hinnant0b95bc92010-05-12 21:02:31 +0000858 explicit exponential_distribution(result_type lambda = 1.0);
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000859 explicit exponential_distribution(const param_type& parm);
860 void reset();
861
862 // generating functions
863 template<class URNG> result_type operator()(URNG& g);
864 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
865
866 // property functions
Howard Hinnant0b95bc92010-05-12 21:02:31 +0000867 result_type lambda() const;
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000868
869 param_type param() const;
870 void param(const param_type& parm);
871
872 result_type min() const;
873 result_type max() const;
874
875 friend bool operator==(const exponential_distribution& x,
876 const exponential_distribution& y);
877 friend bool operator!=(const exponential_distribution& x,
878 const exponential_distribution& y);
879
880 template <class charT, class traits>
881 friend
882 basic_ostream<charT, traits>&
883 operator<<(basic_ostream<charT, traits>& os,
884 const exponential_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000885
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000886 template <class charT, class traits>
887 friend
888 basic_istream<charT, traits>&
889 operator>>(basic_istream<charT, traits>& is,
890 exponential_distribution& x);
891};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892
893template<class RealType = double>
Howard Hinnantbb6d2022010-05-13 17:58:28 +0000894class gamma_distribution
895{
896public:
897 // types
898 typedef RealType result_type;
899
900 class param_type
901 {
902 public:
903 typedef gamma_distribution distribution_type;
904
905 explicit param_type(result_type alpha = 1, result_type beta = 1);
906
907 result_type alpha() const;
908 result_type beta() const;
909
910 friend bool operator==(const param_type& x, const param_type& y);
911 friend bool operator!=(const param_type& x, const param_type& y);
912 };
913
914 // constructors and reset functions
915 explicit gamma_distribution(result_type alpha = 1, result_type beta = 1);
916 explicit gamma_distribution(const param_type& parm);
917 void reset();
918
919 // generating functions
920 template<class URNG> result_type operator()(URNG& g);
921 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
922
923 // property functions
924 result_type alpha() const;
925 result_type beta() const;
926
927 param_type param() const;
928 void param(const param_type& parm);
929
930 result_type min() const;
931 result_type max() const;
932
933 friend bool operator==(const gamma_distribution& x,
934 const gamma_distribution& y);
935 friend bool operator!=(const gamma_distribution& x,
936 const gamma_distribution& y);
937
938 template <class charT, class traits>
939 friend
940 basic_ostream<charT, traits>&
941 operator<<(basic_ostream<charT, traits>& os,
942 const gamma_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000943
Howard Hinnantbb6d2022010-05-13 17:58:28 +0000944 template <class charT, class traits>
945 friend
946 basic_istream<charT, traits>&
947 operator>>(basic_istream<charT, traits>& is,
948 gamma_distribution& x);
949};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950
951template<class RealType = double>
Howard Hinnantdb6b97b2010-05-16 01:09:02 +0000952class weibull_distribution
953{
954public:
955 // types
956 typedef RealType result_type;
957
958 class param_type
959 {
960 public:
961 typedef weibull_distribution distribution_type;
962
963 explicit param_type(result_type alpha = 1, result_type beta = 1);
964
965 result_type a() const;
966 result_type b() const;
967
968 friend bool operator==(const param_type& x, const param_type& y);
969 friend bool operator!=(const param_type& x, const param_type& y);
970 };
971
972 // constructor and reset functions
973 explicit weibull_distribution(result_type a = 1, result_type b = 1);
974 explicit weibull_distribution(const param_type& parm);
975 void reset();
976
977 // generating functions
978 template<class URNG> result_type operator()(URNG& g);
979 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
980
981 // property functions
982 result_type a() const;
983 result_type b() const;
984
985 param_type param() const;
986 void param(const param_type& parm);
987
988 result_type min() const;
989 result_type max() const;
990
Howard Hinnantdb6b97b2010-05-16 01:09:02 +0000991 friend bool operator==(const weibull_distribution& x,
992 const weibull_distribution& y);
993 friend bool operator!=(const weibull_distribution& x,
994 const weibull_distribution& y);
995
996 template <class charT, class traits>
997 friend
998 basic_ostream<charT, traits>&
999 operator<<(basic_ostream<charT, traits>& os,
1000 const weibull_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001001
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00001002 template <class charT, class traits>
1003 friend
1004 basic_istream<charT, traits>&
1005 operator>>(basic_istream<charT, traits>& is,
1006 weibull_distribution& x);
1007};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008
1009template<class RealType = double>
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00001010class extreme_value_distribution
1011{
1012public:
1013 // types
1014 typedef RealType result_type;
1015
1016 class param_type
1017 {
1018 public:
1019 typedef extreme_value_distribution distribution_type;
1020
1021 explicit param_type(result_type a = 0, result_type b = 1);
1022
1023 result_type a() const;
1024 result_type b() const;
1025
1026 friend bool operator==(const param_type& x, const param_type& y);
1027 friend bool operator!=(const param_type& x, const param_type& y);
1028 };
1029
1030 // constructor and reset functions
1031 explicit extreme_value_distribution(result_type a = 0, result_type b = 1);
1032 explicit extreme_value_distribution(const param_type& parm);
1033 void reset();
1034
1035 // generating functions
1036 template<class URNG> result_type operator()(URNG& g);
1037 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1038
1039 // property functions
1040 result_type a() const;
1041 result_type b() const;
1042
1043 param_type param() const;
1044 void param(const param_type& parm);
1045
1046 result_type min() const;
1047 result_type max() const;
1048
1049 friend bool operator==(const extreme_value_distribution& x,
1050 const extreme_value_distribution& y);
1051 friend bool operator!=(const extreme_value_distribution& x,
1052 const extreme_value_distribution& y);
1053
1054 template <class charT, class traits>
1055 friend
1056 basic_ostream<charT, traits>&
1057 operator<<(basic_ostream<charT, traits>& os,
1058 const extreme_value_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001059
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00001060 template <class charT, class traits>
1061 friend
1062 basic_istream<charT, traits>&
1063 operator>>(basic_istream<charT, traits>& is,
1064 extreme_value_distribution& x);
1065};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066
1067template<class RealType = double>
Howard Hinnant0b95bc92010-05-12 21:02:31 +00001068class normal_distribution
1069{
1070public:
1071 // types
1072 typedef RealType result_type;
1073
1074 class param_type
1075 {
1076 public:
1077 typedef normal_distribution distribution_type;
1078
1079 explicit param_type(result_type mean = 0, result_type stddev = 1);
1080
1081 result_type mean() const;
1082 result_type stddev() const;
1083
1084 friend bool operator==(const param_type& x, const param_type& y);
1085 friend bool operator!=(const param_type& x, const param_type& y);
1086 };
1087
1088 // constructors and reset functions
1089 explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
1090 explicit normal_distribution(const param_type& parm);
1091 void reset();
1092
1093 // generating functions
1094 template<class URNG> result_type operator()(URNG& g);
1095 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1096
1097 // property functions
1098 result_type mean() const;
1099 result_type stddev() const;
1100
1101 param_type param() const;
1102 void param(const param_type& parm);
1103
1104 result_type min() const;
1105 result_type max() const;
1106
1107 friend bool operator==(const normal_distribution& x,
1108 const normal_distribution& y);
1109 friend bool operator!=(const normal_distribution& x,
1110 const normal_distribution& y);
1111
1112 template <class charT, class traits>
1113 friend
1114 basic_ostream<charT, traits>&
1115 operator<<(basic_ostream<charT, traits>& os,
1116 const normal_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001117
Howard Hinnant0b95bc92010-05-12 21:02:31 +00001118 template <class charT, class traits>
1119 friend
1120 basic_istream<charT, traits>&
1121 operator>>(basic_istream<charT, traits>& is,
1122 normal_distribution& x);
1123};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124
1125template<class RealType = double>
Howard Hinnantdf3bed62010-05-17 18:31:53 +00001126class lognormal_distribution
1127{
1128public:
1129 // types
1130 typedef RealType result_type;
1131
1132 class param_type
1133 {
1134 public:
1135 typedef lognormal_distribution distribution_type;
1136
1137 explicit param_type(result_type m = 0, result_type s = 1);
1138
1139 result_type m() const;
1140 result_type s() const;
1141
1142 friend bool operator==(const param_type& x, const param_type& y);
1143 friend bool operator!=(const param_type& x, const param_type& y);
1144 };
1145
1146 // constructor and reset functions
1147 explicit lognormal_distribution(result_type m = 0, result_type s = 1);
1148 explicit lognormal_distribution(const param_type& parm);
1149 void reset();
1150
1151 // generating functions
1152 template<class URNG> result_type operator()(URNG& g);
1153 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1154
1155 // property functions
1156 result_type m() const;
1157 result_type s() const;
1158
1159 param_type param() const;
1160 void param(const param_type& parm);
1161
1162 result_type min() const;
1163 result_type max() const;
1164
1165 friend bool operator==(const lognormal_distribution& x,
1166 const lognormal_distribution& y);
1167 friend bool operator!=(const lognormal_distribution& x,
1168 const lognormal_distribution& y);
1169
1170 template <class charT, class traits>
1171 friend
1172 basic_ostream<charT, traits>&
1173 operator<<(basic_ostream<charT, traits>& os,
1174 const lognormal_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001175
Howard Hinnantdf3bed62010-05-17 18:31:53 +00001176 template <class charT, class traits>
1177 friend
1178 basic_istream<charT, traits>&
1179 operator>>(basic_istream<charT, traits>& is,
1180 lognormal_distribution& x);
1181};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182
1183template<class RealType = double>
Howard Hinnant252ebf02010-05-15 23:36:00 +00001184class chi_squared_distribution
1185{
1186public:
1187 // types
1188 typedef RealType result_type;
1189
1190 class param_type
1191 {
1192 public:
1193 typedef chi_squared_distribution distribution_type;
1194
1195 explicit param_type(result_type n = 1);
1196
1197 result_type n() const;
1198
1199 friend bool operator==(const param_type& x, const param_type& y);
1200 friend bool operator!=(const param_type& x, const param_type& y);
1201 };
1202
1203 // constructor and reset functions
1204 explicit chi_squared_distribution(result_type n = 1);
1205 explicit chi_squared_distribution(const param_type& parm);
1206 void reset();
1207
1208 // generating functions
1209 template<class URNG> result_type operator()(URNG& g);
1210 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1211
1212 // property functions
1213 result_type n() const;
1214
1215 param_type param() const;
1216 void param(const param_type& parm);
1217
1218 result_type min() const;
1219 result_type max() const;
1220
Howard Hinnant252ebf02010-05-15 23:36:00 +00001221 friend bool operator==(const chi_squared_distribution& x,
1222 const chi_squared_distribution& y);
1223 friend bool operator!=(const chi_squared_distribution& x,
1224 const chi_squared_distribution& y);
1225
1226 template <class charT, class traits>
1227 friend
1228 basic_ostream<charT, traits>&
1229 operator<<(basic_ostream<charT, traits>& os,
1230 const chi_squared_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001231
Howard Hinnant252ebf02010-05-15 23:36:00 +00001232 template <class charT, class traits>
1233 friend
1234 basic_istream<charT, traits>&
1235 operator>>(basic_istream<charT, traits>& is,
1236 chi_squared_distribution& x);
1237};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238
1239template<class RealType = double>
Howard Hinnantf3292562010-05-17 21:55:46 +00001240class cauchy_distribution
1241{
1242public:
1243 // types
1244 typedef RealType result_type;
1245
1246 class param_type
1247 {
1248 public:
1249 typedef cauchy_distribution distribution_type;
1250
1251 explicit param_type(result_type a = 0, result_type b = 1);
1252
1253 result_type a() const;
1254 result_type b() const;
1255
1256 friend bool operator==(const param_type& x, const param_type& y);
1257 friend bool operator!=(const param_type& x, const param_type& y);
1258 };
1259
1260 // constructor and reset functions
1261 explicit cauchy_distribution(result_type a = 0, result_type b = 1);
1262 explicit cauchy_distribution(const param_type& parm);
1263 void reset();
1264
1265 // generating functions
1266 template<class URNG> result_type operator()(URNG& g);
1267 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1268
1269 // property functions
1270 result_type a() const;
1271 result_type b() const;
1272
1273 param_type param() const;
1274 void param(const param_type& parm);
1275
1276 result_type min() const;
1277 result_type max() const;
1278
1279 friend bool operator==(const cauchy_distribution& x,
1280 const cauchy_distribution& y);
1281 friend bool operator!=(const cauchy_distribution& x,
1282 const cauchy_distribution& y);
1283
1284 template <class charT, class traits>
1285 friend
1286 basic_ostream<charT, traits>&
1287 operator<<(basic_ostream<charT, traits>& os,
1288 const cauchy_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001289
Howard Hinnantf3292562010-05-17 21:55:46 +00001290 template <class charT, class traits>
1291 friend
1292 basic_istream<charT, traits>&
1293 operator>>(basic_istream<charT, traits>& is,
1294 cauchy_distribution& x);
1295};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296
1297template<class RealType = double>
Howard Hinnantf39463b2010-05-18 17:32:30 +00001298class fisher_f_distribution
1299{
1300public:
1301 // types
1302 typedef RealType result_type;
1303
1304 class param_type
1305 {
1306 public:
Howard Hinnant20c50882010-05-18 20:08:04 +00001307 typedef fisher_f_distribution distribution_type;
Howard Hinnantf39463b2010-05-18 17:32:30 +00001308
1309 explicit param_type(result_type m = 1, result_type n = 1);
1310
1311 result_type m() const;
1312 result_type n() const;
1313
1314 friend bool operator==(const param_type& x, const param_type& y);
1315 friend bool operator!=(const param_type& x, const param_type& y);
1316 };
1317
1318 // constructor and reset functions
1319 explicit fisher_f_distribution(result_type m = 1, result_type n = 1);
1320 explicit fisher_f_distribution(const param_type& parm);
1321 void reset();
1322
1323 // generating functions
1324 template<class URNG> result_type operator()(URNG& g);
1325 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1326
1327 // property functions
1328 result_type m() const;
1329 result_type n() const;
1330
1331 param_type param() const;
1332 void param(const param_type& parm);
1333
1334 result_type min() const;
1335 result_type max() const;
1336
1337 friend bool operator==(const fisher_f_distribution& x,
1338 const fisher_f_distribution& y);
1339 friend bool operator!=(const fisher_f_distribution& x,
1340 const fisher_f_distribution& y);
1341
1342 template <class charT, class traits>
1343 friend
1344 basic_ostream<charT, traits>&
1345 operator<<(basic_ostream<charT, traits>& os,
1346 const fisher_f_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001347
Howard Hinnantf39463b2010-05-18 17:32:30 +00001348 template <class charT, class traits>
1349 friend
1350 basic_istream<charT, traits>&
1351 operator>>(basic_istream<charT, traits>& is,
1352 fisher_f_distribution& x);
1353};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354
1355template<class RealType = double>
Howard Hinnant20c50882010-05-18 20:08:04 +00001356class student_t_distribution
1357{
1358public:
1359 // types
1360 typedef RealType result_type;
1361
1362 class param_type
1363 {
1364 public:
1365 typedef student_t_distribution distribution_type;
1366
1367 explicit param_type(result_type n = 1);
1368
1369 result_type n() const;
1370
1371 friend bool operator==(const param_type& x, const param_type& y);
1372 friend bool operator!=(const param_type& x, const param_type& y);
1373 };
1374
1375 // constructor and reset functions
1376 explicit student_t_distribution(result_type n = 1);
1377 explicit student_t_distribution(const param_type& parm);
1378 void reset();
1379
1380 // generating functions
1381 template<class URNG> result_type operator()(URNG& g);
1382 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1383
1384 // property functions
1385 result_type n() const;
1386
1387 param_type param() const;
1388 void param(const param_type& parm);
1389
1390 result_type min() const;
1391 result_type max() const;
1392
1393 friend bool operator==(const student_t_distribution& x,
1394 const student_t_distribution& y);
1395 friend bool operator!=(const student_t_distribution& x,
1396 const student_t_distribution& y);
1397
1398 template <class charT, class traits>
1399 friend
1400 basic_ostream<charT, traits>&
1401 operator<<(basic_ostream<charT, traits>& os,
1402 const student_t_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001403
Howard Hinnant20c50882010-05-18 20:08:04 +00001404 template <class charT, class traits>
1405 friend
1406 basic_istream<charT, traits>&
1407 operator>>(basic_istream<charT, traits>& is,
1408 student_t_distribution& x);
1409};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410
1411template<class IntType = int>
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00001412class discrete_distribution
1413{
1414public:
1415 // types
1416 typedef IntType result_type;
1417
1418 class param_type
1419 {
1420 public:
1421 typedef discrete_distribution distribution_type;
1422
1423 param_type();
1424 template<class InputIterator>
1425 param_type(InputIterator firstW, InputIterator lastW);
1426 param_type(initializer_list<double> wl);
1427 template<class UnaryOperation>
1428 param_type(size_t nw, double xmin, double xmax, UnaryOperation fw);
1429
1430 vector<double> probabilities() const;
1431
1432 friend bool operator==(const param_type& x, const param_type& y);
1433 friend bool operator!=(const param_type& x, const param_type& y);
1434 };
1435
1436 // constructor and reset functions
1437 discrete_distribution();
1438 template<class InputIterator>
1439 discrete_distribution(InputIterator firstW, InputIterator lastW);
1440 discrete_distribution(initializer_list<double> wl);
1441 template<class UnaryOperation>
1442 discrete_distribution(size_t nw, double xmin, double xmax,
1443 UnaryOperation fw);
1444 explicit discrete_distribution(const param_type& parm);
1445 void reset();
1446
1447 // generating functions
1448 template<class URNG> result_type operator()(URNG& g);
1449 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1450
1451 // property functions
1452 vector<double> probabilities() const;
1453
1454 param_type param() const;
1455 void param(const param_type& parm);
1456
1457 result_type min() const;
1458 result_type max() const;
1459
1460 friend bool operator==(const discrete_distribution& x,
1461 const discrete_distribution& y);
1462 friend bool operator!=(const discrete_distribution& x,
1463 const discrete_distribution& y);
1464
1465 template <class charT, class traits>
1466 friend
1467 basic_ostream<charT, traits>&
1468 operator<<(basic_ostream<charT, traits>& os,
1469 const discrete_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001470
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00001471 template <class charT, class traits>
1472 friend
1473 basic_istream<charT, traits>&
1474 operator>>(basic_istream<charT, traits>& is,
1475 discrete_distribution& x);
1476};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477
1478template<class RealType = double>
Howard Hinnant481ba382010-05-20 15:11:46 +00001479class piecewise_constant_distribution
1480{
1481 // types
1482 typedef RealType result_type;
1483
1484 class param_type
1485 {
1486 public:
1487 typedef piecewise_constant_distribution distribution_type;
1488
1489 param_type();
1490 template<class InputIteratorB, class InputIteratorW>
1491 param_type(InputIteratorB firstB, InputIteratorB lastB,
1492 InputIteratorW firstW);
1493 template<class UnaryOperation>
1494 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1495 template<class UnaryOperation>
1496 param_type(size_t nw, result_type xmin, result_type xmax,
1497 UnaryOperation fw);
1498
1499 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001500 vector<result_type> densities() const;
Howard Hinnant481ba382010-05-20 15:11:46 +00001501
1502 friend bool operator==(const param_type& x, const param_type& y);
1503 friend bool operator!=(const param_type& x, const param_type& y);
1504 };
1505
1506 // constructor and reset functions
1507 piecewise_constant_distribution();
1508 template<class InputIteratorB, class InputIteratorW>
1509 piecewise_constant_distribution(InputIteratorB firstB,
1510 InputIteratorB lastB,
1511 InputIteratorW firstW);
1512 template<class UnaryOperation>
1513 piecewise_constant_distribution(initializer_list<result_type> bl,
1514 UnaryOperation fw);
1515 template<class UnaryOperation>
1516 piecewise_constant_distribution(size_t nw, result_type xmin,
1517 result_type xmax, UnaryOperation fw);
1518 explicit piecewise_constant_distribution(const param_type& parm);
1519 void reset();
1520
1521 // generating functions
1522 template<class URNG> result_type operator()(URNG& g);
1523 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1524
1525 // property functions
1526 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001527 vector<result_type> densities() const;
Howard Hinnant481ba382010-05-20 15:11:46 +00001528
1529 param_type param() const;
1530 void param(const param_type& parm);
1531
1532 result_type min() const;
1533 result_type max() const;
1534
1535 friend bool operator==(const piecewise_constant_distribution& x,
1536 const piecewise_constant_distribution& y);
1537 friend bool operator!=(const piecewise_constant_distribution& x,
1538 const piecewise_constant_distribution& y);
1539
1540 template <class charT, class traits>
1541 friend
1542 basic_ostream<charT, traits>&
1543 operator<<(basic_ostream<charT, traits>& os,
1544 const piecewise_constant_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001545
Howard Hinnant481ba382010-05-20 15:11:46 +00001546 template <class charT, class traits>
1547 friend
1548 basic_istream<charT, traits>&
1549 operator>>(basic_istream<charT, traits>& is,
1550 piecewise_constant_distribution& x);
1551};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552
1553template<class RealType = double>
Howard Hinnanta001ef32010-05-25 00:27:34 +00001554class piecewise_linear_distribution
1555{
1556 // types
1557 typedef RealType result_type;
1558
1559 class param_type
1560 {
1561 public:
1562 typedef piecewise_linear_distribution distribution_type;
1563
1564 param_type();
1565 template<class InputIteratorB, class InputIteratorW>
1566 param_type(InputIteratorB firstB, InputIteratorB lastB,
1567 InputIteratorW firstW);
1568 template<class UnaryOperation>
1569 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1570 template<class UnaryOperation>
1571 param_type(size_t nw, result_type xmin, result_type xmax,
1572 UnaryOperation fw);
1573
1574 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001575 vector<result_type> densities() const;
Howard Hinnanta001ef32010-05-25 00:27:34 +00001576
1577 friend bool operator==(const param_type& x, const param_type& y);
1578 friend bool operator!=(const param_type& x, const param_type& y);
1579 };
1580
1581 // constructor and reset functions
1582 piecewise_linear_distribution();
1583 template<class InputIteratorB, class InputIteratorW>
1584 piecewise_linear_distribution(InputIteratorB firstB,
1585 InputIteratorB lastB,
1586 InputIteratorW firstW);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001587
Howard Hinnanta001ef32010-05-25 00:27:34 +00001588 template<class UnaryOperation>
1589 piecewise_linear_distribution(initializer_list<result_type> bl,
1590 UnaryOperation fw);
1591
1592 template<class UnaryOperation>
1593 piecewise_linear_distribution(size_t nw, result_type xmin,
1594 result_type xmax, UnaryOperation fw);
1595
1596 explicit piecewise_linear_distribution(const param_type& parm);
1597 void reset();
1598
1599 // generating functions
1600 template<class URNG> result_type operator()(URNG& g);
1601 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1602
1603 // property functions
1604 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001605 vector<result_type> densities() const;
Howard Hinnanta001ef32010-05-25 00:27:34 +00001606
1607 param_type param() const;
1608 void param(const param_type& parm);
1609
1610 result_type min() const;
1611 result_type max() const;
1612
1613 friend bool operator==(const piecewise_linear_distribution& x,
1614 const piecewise_linear_distribution& y);
1615 friend bool operator!=(const piecewise_linear_distribution& x,
1616 const piecewise_linear_distribution& y);
1617
1618 template <class charT, class traits>
1619 friend
1620 basic_ostream<charT, traits>&
1621 operator<<(basic_ostream<charT, traits>& os,
1622 const piecewise_linear_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001623
Howard Hinnanta001ef32010-05-25 00:27:34 +00001624 template <class charT, class traits>
1625 friend
1626 basic_istream<charT, traits>&
1627 operator>>(basic_istream<charT, traits>& is,
1628 piecewise_linear_distribution& x);
1629};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630
1631} // std
1632*/
1633
1634#include <__config>
1635#include <cstddef>
Eric Fiselier90adc202015-01-23 22:22:36 +00001636#include <cstdint>
1637#include <cmath>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638#include <type_traits>
1639#include <initializer_list>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640#include <limits>
1641#include <algorithm>
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00001642#include <numeric>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643#include <vector>
1644#include <string>
1645#include <istream>
1646#include <ostream>
1647
Howard Hinnantaaaa52b2011-10-17 20:05:10 +00001648#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +00001650#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651
Eric Fiselierf4433a32017-05-31 22:07:49 +00001652_LIBCPP_PUSH_MACROS
1653#include <__undef_macros>
1654
1655
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656_LIBCPP_BEGIN_NAMESPACE_STD
1657
Howard Hinnantc8c73992011-04-11 18:22:12 +00001658// __is_seed_sequence
1659
1660template <class _Sseq, class _Engine>
1661struct __is_seed_sequence
1662{
Howard Hinnant5a646852012-04-02 21:00:45 +00001663 static _LIBCPP_CONSTEXPR const bool value =
Howard Hinnantc8c73992011-04-11 18:22:12 +00001664 !is_convertible<_Sseq, typename _Engine::result_type>::value &&
1665 !is_same<typename remove_cv<_Sseq>::type, _Engine>::value;
1666};
1667
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668// linear_congruential_engine
1669
1670template <unsigned long long __a, unsigned long long __c,
Howard Hinnantc834c512011-11-29 18:15:50 +00001671 unsigned long long __m, unsigned long long _Mp,
1672 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_Mp-__c)/__a)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673struct __lce_ta;
1674
1675// 64
1676
1677template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1678struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1679{
1680 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 static result_type next(result_type __x)
1683 {
1684 // Schrage's algorithm
1685 const result_type __q = __m / __a;
1686 const result_type __r = __m % __a;
1687 const result_type __t0 = __a * (__x % __q);
1688 const result_type __t1 = __r * (__x / __q);
1689 __x = __t0 + (__t0 < __t1) * __m - __t1;
1690 __x += __c - (__x >= __m - __c) * __m;
1691 return __x;
1692 }
1693};
1694
1695template <unsigned long long __a, unsigned long long __m>
1696struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1697{
1698 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 static result_type next(result_type __x)
1701 {
1702 // Schrage's algorithm
1703 const result_type __q = __m / __a;
1704 const result_type __r = __m % __a;
1705 const result_type __t0 = __a * (__x % __q);
1706 const result_type __t1 = __r * (__x / __q);
1707 __x = __t0 + (__t0 < __t1) * __m - __t1;
1708 return __x;
1709 }
1710};
1711
1712template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1713struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1714{
1715 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001716 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717 static result_type next(result_type __x)
1718 {
1719 return (__a * __x + __c) % __m;
1720 }
1721};
1722
1723template <unsigned long long __a, unsigned long long __c>
1724struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1725{
1726 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728 static result_type next(result_type __x)
1729 {
1730 return __a * __x + __c;
1731 }
1732};
1733
1734// 32
1735
Howard Hinnantc834c512011-11-29 18:15:50 +00001736template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1737struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738{
1739 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 static result_type next(result_type __x)
1742 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001743 const result_type __a = static_cast<result_type>(_Ap);
1744 const result_type __c = static_cast<result_type>(_Cp);
1745 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746 // Schrage's algorithm
1747 const result_type __q = __m / __a;
1748 const result_type __r = __m % __a;
1749 const result_type __t0 = __a * (__x % __q);
1750 const result_type __t1 = __r * (__x / __q);
1751 __x = __t0 + (__t0 < __t1) * __m - __t1;
1752 __x += __c - (__x >= __m - __c) * __m;
1753 return __x;
1754 }
1755};
1756
Howard Hinnantc834c512011-11-29 18:15:50 +00001757template <unsigned long long _Ap, unsigned long long _Mp>
1758struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759{
1760 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762 static result_type next(result_type __x)
1763 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001764 const result_type __a = static_cast<result_type>(_Ap);
1765 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766 // Schrage's algorithm
1767 const result_type __q = __m / __a;
1768 const result_type __r = __m % __a;
1769 const result_type __t0 = __a * (__x % __q);
1770 const result_type __t1 = __r * (__x / __q);
1771 __x = __t0 + (__t0 < __t1) * __m - __t1;
1772 return __x;
1773 }
1774};
1775
Howard Hinnantc834c512011-11-29 18:15:50 +00001776template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1777struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), false>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778{
1779 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 static result_type next(result_type __x)
1782 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001783 const result_type __a = static_cast<result_type>(_Ap);
1784 const result_type __c = static_cast<result_type>(_Cp);
1785 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 return (__a * __x + __c) % __m;
1787 }
1788};
1789
Howard Hinnantc834c512011-11-29 18:15:50 +00001790template <unsigned long long _Ap, unsigned long long _Cp>
1791struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792{
1793 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795 static result_type next(result_type __x)
1796 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001797 const result_type __a = static_cast<result_type>(_Ap);
1798 const result_type __c = static_cast<result_type>(_Cp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 return __a * __x + __c;
1800 }
1801};
1802
1803// 16
1804
1805template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1806struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1807{
1808 typedef unsigned short result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810 static result_type next(result_type __x)
1811 {
1812 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1813 }
1814};
1815
1816template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001817class _LIBCPP_TEMPLATE_VIS linear_congruential_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818
1819template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001820 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001821_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822basic_ostream<_CharT, _Traits>&
1823operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00001824 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825
1826template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001827 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828basic_istream<_CharT, _Traits>&
1829operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00001830 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831
1832template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001833class _LIBCPP_TEMPLATE_VIS linear_congruential_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834{
1835public:
1836 // types
1837 typedef _UIntType result_type;
1838
Howard Hinnanta741b242013-05-21 21:19:35 +00001839private:
Howard Hinnantc51e1022010-05-11 19:42:16 +00001840 result_type __x_;
1841
Howard Hinnant5a646852012-04-02 21:00:45 +00001842 static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843
1844 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1845 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1846public:
Howard Hinnant5a646852012-04-02 21:00:45 +00001847 static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u: 0u;
1848 static _LIBCPP_CONSTEXPR const result_type _Max = __m - 1u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1850
1851 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00001852 static _LIBCPP_CONSTEXPR const result_type multiplier = __a;
1853 static _LIBCPP_CONSTEXPR const result_type increment = __c;
1854 static _LIBCPP_CONSTEXPR const result_type modulus = __m;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00001856 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00001858 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
1859 static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860
1861 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00001862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 explicit linear_congruential_engine(result_type __s = default_seed)
1864 {seed(__s);}
Howard Hinnant28b24882011-12-01 20:21:04 +00001865 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00001866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001867 explicit linear_congruential_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00001868 typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869 {seed(__q);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001870 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 void seed(result_type __s = default_seed)
1872 {seed(integral_constant<bool, __m == 0>(),
1873 integral_constant<bool, __c == 0>(), __s);}
1874 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00001875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 typename enable_if
1877 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00001878 __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879 void
1880 >::type
1881 seed(_Sseq& __q)
1882 {__seed(__q, integral_constant<unsigned,
1883 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
Howard Hinnanta0c4f7c2013-05-21 21:05:12 +00001884 : (__m > 0x100000000ull))>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885
1886 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00001887 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888 result_type operator()()
Howard Hinnantc834c512011-11-29 18:15:50 +00001889 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1892
Howard Hinnantf5f99992010-09-22 18:02:38 +00001893 friend _LIBCPP_INLINE_VISIBILITY
1894 bool operator==(const linear_congruential_engine& __x,
1895 const linear_congruential_engine& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896 {return __x.__x_ == __y.__x_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001897 friend _LIBCPP_INLINE_VISIBILITY
1898 bool operator!=(const linear_congruential_engine& __x,
1899 const linear_congruential_engine& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900 {return !(__x == __y);}
1901
1902private:
1903
Howard Hinnantf5f99992010-09-22 18:02:38 +00001904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1910 1 : __s % __m;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1913
1914 template<class _Sseq>
1915 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1916 template<class _Sseq>
1917 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1918
1919 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001920 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921 friend
1922 basic_ostream<_CharT, _Traits>&
1923 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00001924 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001925
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001927 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001928 friend
1929 basic_istream<_CharT, _Traits>&
1930 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00001931 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932};
1933
1934template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00001935 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1936 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
1937
1938template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1939 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1940 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
1941
1942template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1943 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1944 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
1945
1946template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1947 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
1948 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
1949
1950template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951template<class _Sseq>
1952void
1953linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1954 integral_constant<unsigned, 1>)
1955{
1956 const unsigned __k = 1;
1957 uint32_t __ar[__k+3];
1958 __q.generate(__ar, __ar + __k + 3);
1959 result_type __s = static_cast<result_type>(__ar[3] % __m);
1960 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1961}
1962
1963template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1964template<class _Sseq>
1965void
1966linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1967 integral_constant<unsigned, 2>)
1968{
1969 const unsigned __k = 2;
1970 uint32_t __ar[__k+3];
1971 __q.generate(__ar, __ar + __k + 3);
1972 result_type __s = static_cast<result_type>((__ar[3] +
Howard Hinnanta0c4f7c2013-05-21 21:05:12 +00001973 ((uint64_t)__ar[4] << 32)) % __m);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1975}
1976
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977template <class _CharT, class _Traits,
1978 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantf5f99992010-09-22 18:02:38 +00001979inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980basic_ostream<_CharT, _Traits>&
1981operator<<(basic_ostream<_CharT, _Traits>& __os,
1982 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1983{
Howard Hinnant49e145e2012-10-30 19:06:59 +00001984 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985 __os.flags(ios_base::dec | ios_base::left);
1986 __os.fill(__os.widen(' '));
1987 return __os << __x.__x_;
1988}
1989
1990template <class _CharT, class _Traits,
1991 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1992basic_istream<_CharT, _Traits>&
1993operator>>(basic_istream<_CharT, _Traits>& __is,
1994 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1995{
Howard Hinnant49e145e2012-10-30 19:06:59 +00001996 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997 __is.flags(ios_base::dec | ios_base::skipws);
1998 _UIntType __t;
1999 __is >> __t;
2000 if (!__is.fail())
2001 __x.__x_ = __t;
2002 return __is;
2003}
2004
2005typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2006 minstd_rand0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2008 minstd_rand;
Howard Hinnant481ba382010-05-20 15:11:46 +00002009typedef minstd_rand default_random_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010// mersenne_twister_engine
2011
2012template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2013 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2014 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002015class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016
Eric Fiselier4638fca2017-05-31 21:20:18 +00002017template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2018 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2019 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002021operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002022 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002023 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002024 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025
Eric Fiselier4638fca2017-05-31 21:20:18 +00002026template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2027 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2028 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnanta54386e2012-09-14 00:39:16 +00002029_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002030bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002031operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002032 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002033 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002034 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035
2036template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002037 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2038 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2039 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040basic_ostream<_CharT, _Traits>&
2041operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002042 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002043 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044
2045template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002046 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2047 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2048 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049basic_istream<_CharT, _Traits>&
2050operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002051 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002052 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053
2054template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2055 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2056 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002057class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058{
2059public:
2060 // types
2061 typedef _UIntType result_type;
2062
2063private:
2064 result_type __x_[__n];
2065 size_t __i_;
2066
2067 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2068 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
Howard Hinnant5a646852012-04-02 21:00:45 +00002069 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2071 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2072 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2073 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2074 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2075 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2076 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2077public:
Howard Hinnant5a646852012-04-02 21:00:45 +00002078 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2079 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2080 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2082 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2083 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2084 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2085 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2086 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2087
2088 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002089 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2090 static _LIBCPP_CONSTEXPR const size_t state_size = __n;
2091 static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
2092 static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
2093 static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
2094 static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
2095 static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
2096 static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
2097 static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
2098 static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
2099 static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
2100 static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
2101 static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
Howard Hinnantf5f99992010-09-22 18:02:38 +00002102 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002103 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002105 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2106 static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107
2108 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00002109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110 explicit mersenne_twister_engine(result_type __sd = default_seed)
2111 {seed(__sd);}
Howard Hinnant28b24882011-12-01 20:21:04 +00002112 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002114 explicit mersenne_twister_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002115 typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116 {seed(__q);}
2117 void seed(result_type __sd = default_seed);
2118 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120 typename enable_if
2121 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002122 __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123 void
2124 >::type
2125 seed(_Sseq& __q)
2126 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2127
2128 // generating functions
2129 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2132
Eric Fiselier4638fca2017-05-31 21:20:18 +00002133 template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2134 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2135 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136 friend
2137 bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002138 operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002139 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002140 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002141 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002142
Eric Fiselier4638fca2017-05-31 21:20:18 +00002143 template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2144 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2145 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146 friend
2147 bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002148 operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002149 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002150 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002151 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152
2153 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002154 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2155 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2156 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157 friend
2158 basic_ostream<_CharT, _Traits>&
2159 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002160 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002161 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162
2163 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002164 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2165 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2166 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167 friend
2168 basic_istream<_CharT, _Traits>&
2169 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002170 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002171 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172private:
2173
2174 template<class _Sseq>
2175 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2176 template<class _Sseq>
2177 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2178
2179 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181 static
2182 typename enable_if
2183 <
2184 __count < __w,
2185 result_type
2186 >::type
2187 __lshift(result_type __x) {return (__x << __count) & _Max;}
2188
2189 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191 static
2192 typename enable_if
2193 <
2194 (__count >= __w),
2195 result_type
2196 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002197 __lshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198
2199 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201 static
2202 typename enable_if
2203 <
2204 __count < _Dt,
2205 result_type
2206 >::type
2207 __rshift(result_type __x) {return __x >> __count;}
2208
2209 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 static
2212 typename enable_if
2213 <
2214 (__count >= _Dt),
2215 result_type
2216 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002217 __rshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218};
2219
2220template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2221 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2222 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002223 _LIBCPP_CONSTEXPR const size_t
2224 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size;
2225
2226template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2227 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2228 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2229 _LIBCPP_CONSTEXPR const size_t
2230 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size;
2231
2232template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2233 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2234 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2235 _LIBCPP_CONSTEXPR const size_t
2236 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size;
2237
2238template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2239 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2240 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2241 _LIBCPP_CONSTEXPR const size_t
2242 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits;
2243
2244template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2245 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2246 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2247 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2248 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask;
2249
2250template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2251 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2252 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2253 _LIBCPP_CONSTEXPR const size_t
2254 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u;
2255
2256template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2257 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2258 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2259 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2260 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d;
2261
2262template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2263 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2264 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2265 _LIBCPP_CONSTEXPR const size_t
2266 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s;
2267
2268template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2269 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2270 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2271 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2272 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b;
2273
2274template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2275 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2276 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2277 _LIBCPP_CONSTEXPR const size_t
2278 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t;
2279
2280template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2281 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2282 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2283 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2284 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c;
2285
2286template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2287 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2288 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2289 _LIBCPP_CONSTEXPR const size_t
2290 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l;
2291
2292template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2293 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2294 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2295 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2296 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier;
2297
2298template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2299 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2300 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2301 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2302 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed;
2303
2304template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2305 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2306 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307void
2308mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2309 __t, __c, __l, __f>::seed(result_type __sd)
Marshall Clowc1894632017-09-11 18:10:33 +00002310 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311{ // __w >= 2
2312 __x_[0] = __sd & _Max;
2313 for (size_t __i = 1; __i < __n; ++__i)
2314 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2315 __i_ = 0;
2316}
2317
2318template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2319 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2320 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2321template<class _Sseq>
2322void
2323mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2324 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2325{
2326 const unsigned __k = 1;
2327 uint32_t __ar[__n * __k];
2328 __q.generate(__ar, __ar + __n * __k);
2329 for (size_t __i = 0; __i < __n; ++__i)
2330 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2331 const result_type __mask = __r == _Dt ? result_type(~0) :
2332 (result_type(1) << __r) - result_type(1);
2333 __i_ = 0;
2334 if ((__x_[0] & ~__mask) == 0)
2335 {
2336 for (size_t __i = 1; __i < __n; ++__i)
2337 if (__x_[__i] != 0)
2338 return;
Hubert Tongca5b7762018-08-16 23:56:54 +00002339 __x_[0] = result_type(1) << (__w - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340 }
2341}
2342
2343template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2344 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2345 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2346template<class _Sseq>
2347void
2348mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2349 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2350{
2351 const unsigned __k = 2;
2352 uint32_t __ar[__n * __k];
2353 __q.generate(__ar, __ar + __n * __k);
2354 for (size_t __i = 0; __i < __n; ++__i)
2355 __x_[__i] = static_cast<result_type>(
2356 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2357 const result_type __mask = __r == _Dt ? result_type(~0) :
2358 (result_type(1) << __r) - result_type(1);
2359 __i_ = 0;
2360 if ((__x_[0] & ~__mask) == 0)
2361 {
2362 for (size_t __i = 1; __i < __n; ++__i)
2363 if (__x_[__i] != 0)
2364 return;
Hubert Tongca5b7762018-08-16 23:56:54 +00002365 __x_[0] = result_type(1) << (__w - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366 }
2367}
2368
2369template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2370 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2371 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2372_UIntType
2373mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2374 __t, __c, __l, __f>::operator()()
2375{
2376 const size_t __j = (__i_ + 1) % __n;
2377 const result_type __mask = __r == _Dt ? result_type(~0) :
2378 (result_type(1) << __r) - result_type(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00002379 const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380 const size_t __k = (__i_ + __m) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00002381 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2383 __i_ = __j;
2384 __z ^= __lshift<__s>(__z) & __b;
2385 __z ^= __lshift<__t>(__z) & __c;
2386 return __z ^ __rshift<__l>(__z);
2387}
2388
Eric Fiselier4638fca2017-05-31 21:20:18 +00002389template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2390 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2391 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002393operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002394 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002395 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002396 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397{
2398 if (__x.__i_ == __y.__i_)
Howard Hinnantc834c512011-11-29 18:15:50 +00002399 return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400 if (__x.__i_ == 0 || __y.__i_ == 0)
2401 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002402 size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002403 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002404 __y.__x_ + __y.__i_))
2405 return false;
2406 if (__x.__i_ == 0)
Howard Hinnantc834c512011-11-29 18:15:50 +00002407 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
2408 return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409 }
2410 if (__x.__i_ < __y.__i_)
2411 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002412 size_t __j = _Np - __y.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002413 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414 __y.__x_ + __y.__i_))
2415 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002416 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417 __y.__x_))
2418 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002419 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002420 __y.__x_ + (_Np - (__x.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002422 size_t __j = _Np - __x.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002423 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002424 __x.__x_ + __x.__i_))
2425 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002426 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427 __x.__x_))
2428 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002429 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002430 __x.__x_ + (_Np - (__y.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431}
2432
Eric Fiselier4638fca2017-05-31 21:20:18 +00002433template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2434 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2435 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002436inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002438operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002439 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002440 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002441 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442{
2443 return !(__x == __y);
2444}
2445
2446template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002447 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2448 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2449 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002450basic_ostream<_CharT, _Traits>&
2451operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002452 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002453 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002455 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456 __os.flags(ios_base::dec | ios_base::left);
2457 _CharT __sp = __os.widen(' ');
2458 __os.fill(__sp);
2459 __os << __x.__x_[__x.__i_];
Howard Hinnantc834c512011-11-29 18:15:50 +00002460 for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461 __os << __sp << __x.__x_[__j];
2462 for (size_t __j = 0; __j < __x.__i_; ++__j)
2463 __os << __sp << __x.__x_[__j];
2464 return __os;
2465}
2466
2467template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002468 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2469 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2470 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471basic_istream<_CharT, _Traits>&
2472operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002473 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002474 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002476 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477 __is.flags(ios_base::dec | ios_base::skipws);
Eric Fiselier4638fca2017-05-31 21:20:18 +00002478 _UInt __t[_Np];
Howard Hinnantc834c512011-11-29 18:15:50 +00002479 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 __is >> __t[__i];
2481 if (!__is.fail())
2482 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002483 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484 __x.__x_[__i] = __t[__i];
2485 __x.__i_ = 0;
2486 }
2487 return __is;
2488}
2489
2490typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2491 0x9908b0df, 11, 0xffffffff,
2492 7, 0x9d2c5680,
2493 15, 0xefc60000,
2494 18, 1812433253> mt19937;
2495typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2496 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2497 17, 0x71d67fffeda60000ULL,
2498 37, 0xfff7eee000000000ULL,
2499 43, 6364136223846793005ULL> mt19937_64;
2500
2501// subtract_with_carry_engine
2502
2503template<class _UIntType, size_t __w, size_t __s, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002504class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505
Eric Fiselier4638fca2017-05-31 21:20:18 +00002506template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507bool
2508operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002509 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2510 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511
Eric Fiselier4638fca2017-05-31 21:20:18 +00002512template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnanta54386e2012-09-14 00:39:16 +00002513_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514bool
2515operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002516 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2517 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518
2519template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002520 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521basic_ostream<_CharT, _Traits>&
2522operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002523 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524
2525template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002526 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527basic_istream<_CharT, _Traits>&
2528operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002529 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530
2531template<class _UIntType, size_t __w, size_t __s, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002532class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533{
2534public:
2535 // types
2536 typedef _UIntType result_type;
2537
2538private:
2539 result_type __x_[__r];
2540 result_type __c_;
2541 size_t __i_;
2542
Howard Hinnant5a646852012-04-02 21:00:45 +00002543 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2545 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2546 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2547 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2548public:
Howard Hinnant5a646852012-04-02 21:00:45 +00002549 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2550 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2551 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2553
2554 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002555 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2556 static _LIBCPP_CONSTEXPR const size_t short_lag = __s;
2557 static _LIBCPP_CONSTEXPR const size_t long_lag = __r;
Howard Hinnantf5f99992010-09-22 18:02:38 +00002558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002559 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002561 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2562 static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563
2564 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00002565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566 explicit subtract_with_carry_engine(result_type __sd = default_seed)
2567 {seed(__sd);}
Howard Hinnant28b24882011-12-01 20:21:04 +00002568 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002570 explicit subtract_with_carry_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002571 typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572 {seed(__q);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574 void seed(result_type __sd = default_seed)
2575 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2576 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578 typename enable_if
2579 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002580 __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581 void
2582 >::type
2583 seed(_Sseq& __q)
2584 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2585
2586 // generating functions
2587 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2590
Eric Fiselier4638fca2017-05-31 21:20:18 +00002591 template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592 friend
2593 bool
2594 operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002595 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2596 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002597
Eric Fiselier4638fca2017-05-31 21:20:18 +00002598 template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599 friend
2600 bool
2601 operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002602 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2603 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002604
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002606 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607 friend
2608 basic_ostream<_CharT, _Traits>&
2609 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002610 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002611
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002613 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614 friend
2615 basic_istream<_CharT, _Traits>&
2616 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002617 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618
2619private:
2620
2621 void seed(result_type __sd, integral_constant<unsigned, 1>);
2622 void seed(result_type __sd, integral_constant<unsigned, 2>);
2623 template<class _Sseq>
2624 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2625 template<class _Sseq>
2626 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2627};
2628
2629template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002630 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
2631
2632template<class _UIntType, size_t __w, size_t __s, size_t __r>
2633 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
2634
2635template<class _UIntType, size_t __w, size_t __s, size_t __r>
2636 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
2637
2638template<class _UIntType, size_t __w, size_t __s, size_t __r>
2639 _LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type
2640 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
2641
2642template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643void
2644subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2645 integral_constant<unsigned, 1>)
2646{
2647 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2648 __e(__sd == 0u ? default_seed : __sd);
2649 for (size_t __i = 0; __i < __r; ++__i)
2650 __x_[__i] = static_cast<result_type>(__e() & _Max);
2651 __c_ = __x_[__r-1] == 0;
2652 __i_ = 0;
2653}
2654
2655template<class _UIntType, size_t __w, size_t __s, size_t __r>
2656void
2657subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2658 integral_constant<unsigned, 2>)
2659{
2660 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2661 __e(__sd == 0u ? default_seed : __sd);
2662 for (size_t __i = 0; __i < __r; ++__i)
Howard Hinnant93072c12011-08-15 17:22:22 +00002663 {
2664 result_type __e0 = __e();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665 __x_[__i] = static_cast<result_type>(
Howard Hinnant93072c12011-08-15 17:22:22 +00002666 (__e0 + ((uint64_t)__e() << 32)) & _Max);
2667 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668 __c_ = __x_[__r-1] == 0;
2669 __i_ = 0;
2670}
2671
2672template<class _UIntType, size_t __w, size_t __s, size_t __r>
2673template<class _Sseq>
2674void
2675subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2676 integral_constant<unsigned, 1>)
2677{
2678 const unsigned __k = 1;
2679 uint32_t __ar[__r * __k];
2680 __q.generate(__ar, __ar + __r * __k);
2681 for (size_t __i = 0; __i < __r; ++__i)
2682 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2683 __c_ = __x_[__r-1] == 0;
2684 __i_ = 0;
2685}
2686
2687template<class _UIntType, size_t __w, size_t __s, size_t __r>
2688template<class _Sseq>
2689void
2690subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2691 integral_constant<unsigned, 2>)
2692{
2693 const unsigned __k = 2;
2694 uint32_t __ar[__r * __k];
2695 __q.generate(__ar, __ar + __r * __k);
2696 for (size_t __i = 0; __i < __r; ++__i)
2697 __x_[__i] = static_cast<result_type>(
2698 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2699 __c_ = __x_[__r-1] == 0;
2700 __i_ = 0;
2701}
2702
2703template<class _UIntType, size_t __w, size_t __s, size_t __r>
2704_UIntType
2705subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2706{
2707 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2708 result_type& __xr = __x_[__i_];
2709 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2710 __xr = (__xs - __xr - __c_) & _Max;
2711 __c_ = __new_c;
2712 __i_ = (__i_ + 1) % __r;
2713 return __xr;
2714}
2715
Eric Fiselier4638fca2017-05-31 21:20:18 +00002716template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717bool
2718operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002719 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2720 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721{
2722 if (__x.__c_ != __y.__c_)
2723 return false;
2724 if (__x.__i_ == __y.__i_)
Howard Hinnantc834c512011-11-29 18:15:50 +00002725 return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726 if (__x.__i_ == 0 || __y.__i_ == 0)
2727 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002728 size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002729 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730 __y.__x_ + __y.__i_))
2731 return false;
2732 if (__x.__i_ == 0)
Howard Hinnantc834c512011-11-29 18:15:50 +00002733 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
2734 return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735 }
2736 if (__x.__i_ < __y.__i_)
2737 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002738 size_t __j = _Rp - __y.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002739 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740 __y.__x_ + __y.__i_))
2741 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002742 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743 __y.__x_))
2744 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002745 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002746 __y.__x_ + (_Rp - (__x.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002748 size_t __j = _Rp - __x.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002749 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 __x.__x_ + __x.__i_))
2751 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002752 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753 __x.__x_))
2754 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002755 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002756 __x.__x_ + (_Rp - (__y.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757}
2758
Eric Fiselier4638fca2017-05-31 21:20:18 +00002759template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002760inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761bool
2762operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002763 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2764 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765{
2766 return !(__x == __y);
2767}
2768
2769template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002770 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771basic_ostream<_CharT, _Traits>&
2772operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002773 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002775 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776 __os.flags(ios_base::dec | ios_base::left);
2777 _CharT __sp = __os.widen(' ');
2778 __os.fill(__sp);
2779 __os << __x.__x_[__x.__i_];
Howard Hinnantc834c512011-11-29 18:15:50 +00002780 for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 __os << __sp << __x.__x_[__j];
2782 for (size_t __j = 0; __j < __x.__i_; ++__j)
2783 __os << __sp << __x.__x_[__j];
2784 __os << __sp << __x.__c_;
2785 return __os;
2786}
2787
2788template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002789 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790basic_istream<_CharT, _Traits>&
2791operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002792 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002794 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795 __is.flags(ios_base::dec | ios_base::skipws);
Eric Fiselier4638fca2017-05-31 21:20:18 +00002796 _UInt __t[_Rp+1];
Howard Hinnantc834c512011-11-29 18:15:50 +00002797 for (size_t __i = 0; __i < _Rp+1; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 __is >> __t[__i];
2799 if (!__is.fail())
2800 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002801 for (size_t __i = 0; __i < _Rp; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802 __x.__x_[__i] = __t[__i];
Howard Hinnantc834c512011-11-29 18:15:50 +00002803 __x.__c_ = __t[_Rp];
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804 __x.__i_ = 0;
2805 }
2806 return __is;
2807}
2808
2809typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2810typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2811
2812// discard_block_engine
2813
2814template<class _Engine, size_t __p, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002815class _LIBCPP_TEMPLATE_VIS discard_block_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816{
2817 _Engine __e_;
2818 int __n_;
2819
2820 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2821 static_assert(__r <= __p, "discard_block_engine invalid parameters");
Eric Fiselier37c22152016-12-24 00:24:44 +00002822 static_assert(__r <= INT_MAX, "discard_block_engine invalid parameters");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823public:
2824 // types
2825 typedef typename _Engine::result_type result_type;
2826
2827 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002828 static _LIBCPP_CONSTEXPR const size_t block_size = __p;
2829 static _LIBCPP_CONSTEXPR const size_t used_block = __r;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00002831#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 static const result_type _Min = _Engine::_Min;
2833 static const result_type _Max = _Engine::_Max;
Howard Hinnant5a646852012-04-02 21:00:45 +00002834#else
2835 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
2836 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
2837#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838
Howard Hinnantf5f99992010-09-22 18:02:38 +00002839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002840 static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002842 static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843
2844 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00002845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846 discard_block_engine() : __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002848 explicit discard_block_engine(const _Engine& __e)
2849 : __e_(__e), __n_(0) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00002850#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002852 explicit discard_block_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002853 : __e_(_VSTD::move(__e)), __n_(0) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00002854#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002857 template<class _Sseq>
2858 _LIBCPP_INLINE_VISIBILITY
2859 explicit discard_block_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002860 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002861 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862 : __e_(__q), __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864 void seed() {__e_.seed(); __n_ = 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002867 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002869 typename enable_if
2870 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002871 __is_seed_sequence<_Sseq, discard_block_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002872 void
2873 >::type
2874 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875
2876 // generating functions
2877 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2880
2881 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00002882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00002883 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884
Howard Hinnantc834c512011-11-29 18:15:50 +00002885 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886 friend
2887 bool
2888 operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00002889 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2890 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002891
Howard Hinnantc834c512011-11-29 18:15:50 +00002892 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893 friend
2894 bool
2895 operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00002896 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2897 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002898
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00002900 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901 friend
2902 basic_ostream<_CharT, _Traits>&
2903 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00002904 const discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002905
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00002907 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 friend
2909 basic_istream<_CharT, _Traits>&
2910 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00002911 discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912};
2913
2914template<class _Engine, size_t __p, size_t __r>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002915 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
2916
2917template<class _Engine, size_t __p, size_t __r>
2918 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
2919
2920template<class _Engine, size_t __p, size_t __r>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921typename discard_block_engine<_Engine, __p, __r>::result_type
2922discard_block_engine<_Engine, __p, __r>::operator()()
2923{
Eric Fiselier37c22152016-12-24 00:24:44 +00002924 if (__n_ >= static_cast<int>(__r))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925 {
2926 __e_.discard(__p - __r);
2927 __n_ = 0;
2928 }
2929 ++__n_;
2930 return __e_();
2931}
2932
Howard Hinnantc834c512011-11-29 18:15:50 +00002933template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002934inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002936operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2937 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938{
2939 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2940}
2941
Howard Hinnantc834c512011-11-29 18:15:50 +00002942template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002943inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002945operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2946 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947{
2948 return !(__x == __y);
2949}
2950
2951template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00002952 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953basic_ostream<_CharT, _Traits>&
2954operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00002955 const discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002957 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958 __os.flags(ios_base::dec | ios_base::left);
2959 _CharT __sp = __os.widen(' ');
2960 __os.fill(__sp);
2961 return __os << __x.__e_ << __sp << __x.__n_;
2962}
2963
2964template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00002965 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966basic_istream<_CharT, _Traits>&
2967operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00002968 discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002970 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 __is.flags(ios_base::dec | ios_base::skipws);
2972 _Eng __e;
2973 int __n;
2974 __is >> __e >> __n;
2975 if (!__is.fail())
2976 {
2977 __x.__e_ = __e;
2978 __x.__n_ = __n;
2979 }
2980 return __is;
2981}
2982
2983typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2984typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2985
2986// independent_bits_engine
2987
Howard Hinnantc51e1022010-05-11 19:42:16 +00002988template<class _Engine, size_t __w, class _UIntType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002989class _LIBCPP_TEMPLATE_VIS independent_bits_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990{
Eric Fiselier4638fca2017-05-31 21:20:18 +00002991 template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992 class __get_n
2993 {
Eric Fiselier4638fca2017-05-31 21:20:18 +00002994 static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits;
Howard Hinnant5a646852012-04-02 21:00:45 +00002995 static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
2996 static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
Eric Fiselier4638fca2017-05-31 21:20:18 +00002997 static _LIBCPP_CONSTEXPR const _UInt _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998 public:
Howard Hinnant5a646852012-04-02 21:00:45 +00002999 static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000 };
3001public:
3002 // types
3003 typedef _UIntType result_type;
3004
3005private:
3006 _Engine __e_;
3007
Howard Hinnant5a646852012-04-02 21:00:45 +00003008 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003009 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
3010 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
3011
3012 typedef typename _Engine::result_type _Engine_result_type;
3013 typedef typename conditional
3014 <
3015 sizeof(_Engine_result_type) <= sizeof(result_type),
3016 result_type,
3017 _Engine_result_type
3018 >::type _Working_result_type;
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003019#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +00003020 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant5a646852012-04-02 21:00:45 +00003021 + _Working_result_type(1);
3022#else
3023 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
3024 + _Working_result_type(1);
3025#endif
3026 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
3027 static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
3028 static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
3029 static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
3030 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
3031 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
3032 static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
3033 (_Rp >> __w0) << __w0;
3034 static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
3035 (_Rp >> (__w0+1)) << (__w0+1);
3036 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037 _Engine_result_type(~0) >> (_EDt - __w0) :
3038 _Engine_result_type(0);
Howard Hinnant5a646852012-04-02 21:00:45 +00003039 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
3041 _Engine_result_type(~0);
3042public:
Howard Hinnant5a646852012-04-02 21:00:45 +00003043 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3044 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
3045 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
3047
3048 // engine characteristics
Howard Hinnantf5f99992010-09-22 18:02:38 +00003049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003050 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00003051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003052 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003053
3054 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003056 independent_bits_engine() {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003058 explicit independent_bits_engine(const _Engine& __e)
3059 : __e_(__e) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003060#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003062 explicit independent_bits_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003063 : __e_(_VSTD::move(__e)) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003064#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnant28b24882011-12-01 20:21:04 +00003067 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00003069 explicit independent_bits_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00003070 typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003071 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072 : __e_(__q) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074 void seed() {__e_.seed();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003077 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003079 typename enable_if
3080 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00003081 __is_seed_sequence<_Sseq, independent_bits_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003082 void
3083 >::type
3084 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085
3086 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003088 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3091
3092 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00003094 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003095
Eric Fiselier4638fca2017-05-31 21:20:18 +00003096 template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097 friend
3098 bool
3099 operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003100 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3101 const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003102
Eric Fiselier4638fca2017-05-31 21:20:18 +00003103 template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003104 friend
3105 bool
3106 operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003107 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3108 const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003109
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003111 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112 friend
3113 basic_ostream<_CharT, _Traits>&
3114 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003115 const independent_bits_engine<_Eng, _Wp, _UInt>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003116
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003118 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119 friend
3120 basic_istream<_CharT, _Traits>&
3121 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003122 independent_bits_engine<_Eng, _Wp, _UInt>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123
3124private:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003125 _LIBCPP_INLINE_VISIBILITY
Marshall Clowfe778582017-09-20 19:38:43 +00003126 result_type __eval(false_type);
3127 result_type __eval(true_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128
3129 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131 static
3132 typename enable_if
3133 <
3134 __count < _Dt,
3135 result_type
3136 >::type
3137 __lshift(result_type __x) {return __x << __count;}
3138
3139 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141 static
3142 typename enable_if
3143 <
3144 (__count >= _Dt),
3145 result_type
3146 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00003147 __lshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148};
3149
3150template<class _Engine, size_t __w, class _UIntType>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003151inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00003153independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154{
3155 return static_cast<result_type>(__e_() & __mask0);
3156}
3157
3158template<class _Engine, size_t __w, class _UIntType>
3159_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00003160independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161{
Howard Hinnantc834c512011-11-29 18:15:50 +00003162 result_type _Sp = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163 for (size_t __k = 0; __k < __n0; ++__k)
3164 {
3165 _Engine_result_type __u;
3166 do
3167 {
3168 __u = __e_() - _Engine::min();
3169 } while (__u >= __y0);
Howard Hinnantc834c512011-11-29 18:15:50 +00003170 _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171 }
3172 for (size_t __k = __n0; __k < __n; ++__k)
3173 {
3174 _Engine_result_type __u;
3175 do
3176 {
3177 __u = __e_() - _Engine::min();
3178 } while (__u >= __y1);
Howard Hinnantc834c512011-11-29 18:15:50 +00003179 _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180 }
Howard Hinnantc834c512011-11-29 18:15:50 +00003181 return _Sp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182}
3183
Eric Fiselier4638fca2017-05-31 21:20:18 +00003184template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003185inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186bool
3187operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003188 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3189 const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190{
3191 return __x.base() == __y.base();
3192}
3193
Eric Fiselier4638fca2017-05-31 21:20:18 +00003194template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003195inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196bool
3197operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003198 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3199 const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200{
3201 return !(__x == __y);
3202}
3203
3204template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003205 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206basic_ostream<_CharT, _Traits>&
3207operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003208 const independent_bits_engine<_Eng, _Wp, _UInt>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209{
3210 return __os << __x.base();
3211}
3212
3213template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003214 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215basic_istream<_CharT, _Traits>&
3216operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003217 independent_bits_engine<_Eng, _Wp, _UInt>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218{
3219 _Eng __e;
3220 __is >> __e;
3221 if (!__is.fail())
3222 __x.__e_ = __e;
3223 return __is;
3224}
3225
3226// shuffle_order_engine
3227
3228template <uint64_t _Xp, uint64_t _Yp>
3229struct __ugcd
3230{
Howard Hinnant5a646852012-04-02 21:00:45 +00003231 static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232};
3233
3234template <uint64_t _Xp>
3235struct __ugcd<_Xp, 0>
3236{
Howard Hinnant5a646852012-04-02 21:00:45 +00003237 static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238};
3239
Howard Hinnantc834c512011-11-29 18:15:50 +00003240template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241class __uratio
3242{
Howard Hinnantc834c512011-11-29 18:15:50 +00003243 static_assert(_Dp != 0, "__uratio divide by 0");
Howard Hinnant5a646852012-04-02 21:00:45 +00003244 static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245public:
Howard Hinnant5a646852012-04-02 21:00:45 +00003246 static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
3247 static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003248
3249 typedef __uratio<num, den> type;
3250};
3251
3252template<class _Engine, size_t __k>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003253class _LIBCPP_TEMPLATE_VIS shuffle_order_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254{
3255 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3256public:
3257 // types
3258 typedef typename _Engine::result_type result_type;
3259
3260private:
3261 _Engine __e_;
3262 result_type _V_[__k];
3263 result_type _Y_;
3264
3265public:
3266 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00003267 static _LIBCPP_CONSTEXPR const size_t table_size = __k;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003268
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003269#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270 static const result_type _Min = _Engine::_Min;
3271 static const result_type _Max = _Engine::_Max;
Howard Hinnant5a646852012-04-02 21:00:45 +00003272#else
3273 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
3274 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
3275#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
Howard Hinnantf5f99992010-09-22 18:02:38 +00003277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003278 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00003279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003280 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281
Howard Hinnant5a646852012-04-02 21:00:45 +00003282 static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003283
3284 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286 shuffle_order_engine() {__init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003288 explicit shuffle_order_engine(const _Engine& __e)
3289 : __e_(__e) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003290#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003292 explicit shuffle_order_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003293 : __e_(_VSTD::move(__e)) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003294#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnant28b24882011-12-01 20:21:04 +00003297 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00003299 explicit shuffle_order_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00003300 typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003301 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302 : __e_(__q) {__init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304 void seed() {__e_.seed(); __init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003306 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003307 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003309 typename enable_if
3310 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00003311 __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003312 void
3313 >::type
3314 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003315
3316 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003318 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3321
3322 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00003324 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003325
3326private:
Howard Hinnantc834c512011-11-29 18:15:50 +00003327 template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328 friend
3329 bool
3330 operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00003331 const shuffle_order_engine<_Eng, _Kp>& __x,
3332 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003333
Howard Hinnantc834c512011-11-29 18:15:50 +00003334 template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003335 friend
3336 bool
3337 operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00003338 const shuffle_order_engine<_Eng, _Kp>& __x,
3339 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003340
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003342 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343 friend
3344 basic_ostream<_CharT, _Traits>&
3345 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003346 const shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003347
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003349 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350 friend
3351 basic_istream<_CharT, _Traits>&
3352 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003353 shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354
Howard Hinnantf5f99992010-09-22 18:02:38 +00003355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356 void __init()
3357 {
3358 for (size_t __i = 0; __i < __k; ++__i)
3359 _V_[__i] = __e_();
3360 _Y_ = __e_();
3361 }
3362
Howard Hinnantf5f99992010-09-22 18:02:38 +00003363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003366 result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367
Howard Hinnantf5f99992010-09-22 18:02:38 +00003368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003369 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003371 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3372
Howard Hinnantc834c512011-11-29 18:15:50 +00003373 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003375 typename enable_if
3376 <
Howard Hinnantc834c512011-11-29 18:15:50 +00003377 (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378 result_type
3379 >::type
Howard Hinnantc834c512011-11-29 18:15:50 +00003380 __eval(__uratio<_Np, _Dp>)
3381 {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003382
Howard Hinnantc834c512011-11-29 18:15:50 +00003383 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385 typename enable_if
3386 <
Howard Hinnantc834c512011-11-29 18:15:50 +00003387 __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388 result_type
3389 >::type
Howard Hinnantc834c512011-11-29 18:15:50 +00003390 __eval(__uratio<_Np, _Dp>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003392 const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
3393 / __uratio<_Np, _Dp>::den);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003394 _Y_ = _V_[__j];
3395 _V_[__j] = __e_();
3396 return _Y_;
3397 }
3398
3399 template <uint64_t __n, uint64_t __d>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401 result_type __evalf()
3402 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003403 const double _Fp = __d == 0 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404 __n / (2. * 0x8000000000000000ull) :
3405 __n / (double)__d;
Howard Hinnantc834c512011-11-29 18:15:50 +00003406 const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407 _Y_ = _V_[__j];
3408 _V_[__j] = __e_();
3409 return _Y_;
3410 }
3411};
3412
Howard Hinnant2c45cb42012-12-12 21:14:28 +00003413template<class _Engine, size_t __k>
3414 _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size;
3415
Howard Hinnantc834c512011-11-29 18:15:50 +00003416template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003417bool
3418operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00003419 const shuffle_order_engine<_Eng, _Kp>& __x,
3420 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003421{
Howard Hinnantc834c512011-11-29 18:15:50 +00003422 return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003423 __x.__e_ == __y.__e_;
3424}
3425
Howard Hinnantc834c512011-11-29 18:15:50 +00003426template<class _Eng, size_t _Kp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003427inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428bool
3429operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00003430 const shuffle_order_engine<_Eng, _Kp>& __x,
3431 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432{
3433 return !(__x == __y);
3434}
3435
3436template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003437 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003438basic_ostream<_CharT, _Traits>&
3439operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003440 const shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003442 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443 __os.flags(ios_base::dec | ios_base::left);
3444 _CharT __sp = __os.widen(' ');
3445 __os.fill(__sp);
3446 __os << __x.__e_ << __sp << __x._V_[0];
Howard Hinnantc834c512011-11-29 18:15:50 +00003447 for (size_t __i = 1; __i < _Kp; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448 __os << __sp << __x._V_[__i];
3449 return __os << __sp << __x._Y_;
3450}
3451
3452template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003453 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454basic_istream<_CharT, _Traits>&
3455operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003456 shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457{
Howard Hinnantc834c512011-11-29 18:15:50 +00003458 typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003459 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460 __is.flags(ios_base::dec | ios_base::skipws);
3461 _Eng __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003462 result_type _Vp[_Kp+1];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463 __is >> __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003464 for (size_t __i = 0; __i < _Kp+1; ++__i)
3465 __is >> _Vp[__i];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466 if (!__is.fail())
3467 {
3468 __x.__e_ = __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003469 for (size_t __i = 0; __i < _Kp; ++__i)
3470 __x._V_[__i] = _Vp[__i];
3471 __x._Y_ = _Vp[_Kp];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472 }
3473 return __is;
3474}
3475
3476typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3477
3478// random_device
3479
Howard Hinnant8331b762013-03-06 23:30:19 +00003480class _LIBCPP_TYPE_VIS random_device
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481{
Ed Schoutendcf37402015-03-10 07:46:06 +00003482#ifdef _LIBCPP_USING_DEV_RANDOM
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483 int __f_;
Ed Schoutendcf37402015-03-10 07:46:06 +00003484#endif // defined(_LIBCPP_USING_DEV_RANDOM)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485public:
3486 // types
3487 typedef unsigned result_type;
3488
3489 // generator characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00003490 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3491 static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492
Howard Hinnantf5f99992010-09-22 18:02:38 +00003493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant664183b2012-04-02 00:40:41 +00003494 static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant664183b2012-04-02 00:40:41 +00003496 static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497
3498 // constructors
3499 explicit random_device(const string& __token = "/dev/urandom");
3500 ~random_device();
3501
3502 // generating functions
3503 result_type operator()();
3504
3505 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +00003506 double entropy() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507
3508private:
3509 // no copy functions
3510 random_device(const random_device&); // = delete;
3511 random_device& operator=(const random_device&); // = delete;
3512};
3513
3514// seed_seq
3515
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003516class _LIBCPP_TEMPLATE_VIS seed_seq
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517{
3518public:
3519 // types
3520 typedef uint32_t result_type;
3521
3522private:
3523 vector<result_type> __v_;
3524
3525 template<class _InputIterator>
3526 void init(_InputIterator __first, _InputIterator __last);
3527public:
3528 // constructors
Howard Hinnantf5f99992010-09-22 18:02:38 +00003529 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4b6b8092013-10-23 05:56:47 +00003530 seed_seq() _NOEXCEPT {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003531#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532 template<class _Tp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003535#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003536
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539 seed_seq(_InputIterator __first, _InputIterator __last)
3540 {init(__first, __last);}
3541
3542 // generating functions
3543 template<class _RandomAccessIterator>
3544 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3545
3546 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003547 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4b6b8092013-10-23 05:56:47 +00003548 size_t size() const _NOEXCEPT {return __v_.size();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549 template<class _OutputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003551 void param(_OutputIterator __dest) const
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003552 {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553
3554private:
3555 // no copy functions
3556 seed_seq(const seed_seq&); // = delete;
3557 void operator=(const seed_seq&); // = delete;
3558
Howard Hinnantf5f99992010-09-22 18:02:38 +00003559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003560 static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561};
3562
3563template<class _InputIterator>
3564void
3565seed_seq::init(_InputIterator __first, _InputIterator __last)
3566{
3567 for (_InputIterator __s = __first; __s != __last; ++__s)
3568 __v_.push_back(*__s & 0xFFFFFFFF);
3569}
3570
3571template<class _RandomAccessIterator>
3572void
3573seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3574{
3575 if (__first != __last)
3576 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003577 _VSTD::fill(__first, __last, 0x8b8b8b8b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578 const size_t __n = static_cast<size_t>(__last - __first);
3579 const size_t __s = __v_.size();
3580 const size_t __t = (__n >= 623) ? 11
3581 : (__n >= 68) ? 7
3582 : (__n >= 39) ? 5
3583 : (__n >= 7) ? 3
3584 : (__n - 1) / 2;
3585 const size_t __p = (__n - __t) / 2;
3586 const size_t __q = __p + __t;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003587 const size_t __m = _VSTD::max(__s + 1, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588 // __k = 0;
3589 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003590 result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591 ^ __first[__n - 1]);
3592 __first[__p] += __r;
3593 __r += __s;
3594 __first[__q] += __r;
3595 __first[0] = __r;
3596 }
3597 for (size_t __k = 1; __k <= __s; ++__k)
3598 {
3599 const size_t __kmodn = __k % __n;
3600 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003601 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602 ^ __first[(__k - 1) % __n]);
3603 __first[__kpmodn] += __r;
3604 __r += __kmodn + __v_[__k-1];
3605 __first[(__k + __q) % __n] += __r;
3606 __first[__kmodn] = __r;
3607 }
3608 for (size_t __k = __s + 1; __k < __m; ++__k)
3609 {
3610 const size_t __kmodn = __k % __n;
3611 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003612 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613 ^ __first[(__k - 1) % __n]);
3614 __first[__kpmodn] += __r;
3615 __r += __kmodn;
3616 __first[(__k + __q) % __n] += __r;
3617 __first[__kmodn] = __r;
3618 }
3619 for (size_t __k = __m; __k < __m + __n; ++__k)
3620 {
3621 const size_t __kmodn = __k % __n;
3622 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003623 result_type __r = 1566083941 * _Tp(__first[__kmodn] +
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624 __first[__kpmodn] +
3625 __first[(__k - 1) % __n]);
3626 __first[__kpmodn] ^= __r;
3627 __r -= __kmodn;
3628 __first[(__k + __q) % __n] ^= __r;
3629 __first[__kmodn] = __r;
3630 }
3631 }
3632}
3633
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003634// generate_canonical
3635
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636template<class _RealType, size_t __bits, class _URNG>
3637_RealType
3638generate_canonical(_URNG& __g)
3639{
3640 const size_t _Dt = numeric_limits<_RealType>::digits;
3641 const size_t __b = _Dt < __bits ? _Dt : __bits;
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003642#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
Howard Hinnant5a646852012-04-02 21:00:45 +00003644#else
3645 const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
3646#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
Howard Hinnant5a646852012-04-02 21:00:45 +00003648 const _RealType _Rp = _URNG::max() - _URNG::min() + _RealType(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00003649 _RealType __base = _Rp;
Howard Hinnant5a646852012-04-02 21:00:45 +00003650 _RealType _Sp = __g() - _URNG::min();
Howard Hinnantc834c512011-11-29 18:15:50 +00003651 for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp)
Howard Hinnant5a646852012-04-02 21:00:45 +00003652 _Sp += (__g() - _URNG::min()) * __base;
Howard Hinnantc834c512011-11-29 18:15:50 +00003653 return _Sp / __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654}
3655
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003656// uniform_int_distribution
3657
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003658// in <algorithm>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659
3660template <class _CharT, class _Traits, class _IT>
3661basic_ostream<_CharT, _Traits>&
3662operator<<(basic_ostream<_CharT, _Traits>& __os,
3663 const uniform_int_distribution<_IT>& __x)
3664{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003665 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666 __os.flags(ios_base::dec | ios_base::left);
3667 _CharT __sp = __os.widen(' ');
3668 __os.fill(__sp);
3669 return __os << __x.a() << __sp << __x.b();
3670}
3671
3672template <class _CharT, class _Traits, class _IT>
3673basic_istream<_CharT, _Traits>&
3674operator>>(basic_istream<_CharT, _Traits>& __is,
3675 uniform_int_distribution<_IT>& __x)
3676{
3677 typedef uniform_int_distribution<_IT> _Eng;
3678 typedef typename _Eng::result_type result_type;
3679 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003680 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681 __is.flags(ios_base::dec | ios_base::skipws);
3682 result_type __a;
3683 result_type __b;
3684 __is >> __a >> __b;
3685 if (!__is.fail())
3686 __x.param(param_type(__a, __b));
3687 return __is;
3688}
3689
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003690// uniform_real_distribution
3691
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003693class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
Howard Hinnantc51e1022010-05-11 19:42:16 +00003694{
3695public:
3696 // types
3697 typedef _RealType result_type;
3698
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003699 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700 {
3701 result_type __a_;
3702 result_type __b_;
3703 public:
3704 typedef uniform_real_distribution distribution_type;
3705
Howard Hinnantf5f99992010-09-22 18:02:38 +00003706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707 explicit param_type(result_type __a = 0,
3708 result_type __b = 1)
3709 : __a_(__a), __b_(__b) {}
3710
Howard Hinnantf5f99992010-09-22 18:02:38 +00003711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003712 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714 result_type b() const {return __b_;}
3715
Howard Hinnantf5f99992010-09-22 18:02:38 +00003716 friend _LIBCPP_INLINE_VISIBILITY
3717 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003718 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003719 friend _LIBCPP_INLINE_VISIBILITY
3720 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003721 {return !(__x == __y);}
3722 };
3723
3724private:
3725 param_type __p_;
3726
3727public:
3728 // constructors and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3731 : __p_(param_type(__a, __b)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735 void reset() {}
3736
3737 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003738 template<class _URNG>
3739 _LIBCPP_INLINE_VISIBILITY
3740 result_type operator()(_URNG& __g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003741 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003742 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743
3744 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003746 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748 result_type b() const {return __p_.b();}
3749
Howard Hinnantf5f99992010-09-22 18:02:38 +00003750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753 void param(const param_type& __p) {__p_ = __p;}
3754
Howard Hinnantf5f99992010-09-22 18:02:38 +00003755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756 result_type min() const {return a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758 result_type max() const {return b();}
3759
Howard Hinnantf5f99992010-09-22 18:02:38 +00003760 friend _LIBCPP_INLINE_VISIBILITY
3761 bool operator==(const uniform_real_distribution& __x,
3762 const uniform_real_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003764 friend _LIBCPP_INLINE_VISIBILITY
3765 bool operator!=(const uniform_real_distribution& __x,
3766 const uniform_real_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767 {return !(__x == __y);}
3768};
3769
3770template<class _RealType>
3771template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003772inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773typename uniform_real_distribution<_RealType>::result_type
3774uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3775{
3776 return (__p.b() - __p.a())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003777 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778 + __p.a();
3779}
3780
3781template <class _CharT, class _Traits, class _RT>
3782basic_ostream<_CharT, _Traits>&
3783operator<<(basic_ostream<_CharT, _Traits>& __os,
3784 const uniform_real_distribution<_RT>& __x)
3785{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003786 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00003787 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3788 ios_base::scientific);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003789 _CharT __sp = __os.widen(' ');
3790 __os.fill(__sp);
3791 return __os << __x.a() << __sp << __x.b();
3792}
3793
3794template <class _CharT, class _Traits, class _RT>
3795basic_istream<_CharT, _Traits>&
3796operator>>(basic_istream<_CharT, _Traits>& __is,
3797 uniform_real_distribution<_RT>& __x)
3798{
3799 typedef uniform_real_distribution<_RT> _Eng;
3800 typedef typename _Eng::result_type result_type;
3801 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003802 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003803 __is.flags(ios_base::dec | ios_base::skipws);
3804 result_type __a;
3805 result_type __b;
3806 __is >> __a >> __b;
3807 if (!__is.fail())
3808 __x.param(param_type(__a, __b));
3809 return __is;
3810}
3811
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003812// bernoulli_distribution
3813
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003814class _LIBCPP_TEMPLATE_VIS bernoulli_distribution
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815{
3816public:
3817 // types
3818 typedef bool result_type;
3819
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003820 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821 {
3822 double __p_;
3823 public:
3824 typedef bernoulli_distribution distribution_type;
3825
Howard Hinnantf5f99992010-09-22 18:02:38 +00003826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827 explicit param_type(double __p = 0.5) : __p_(__p) {}
3828
Howard Hinnantf5f99992010-09-22 18:02:38 +00003829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830 double p() const {return __p_;}
3831
Howard Hinnantf5f99992010-09-22 18:02:38 +00003832 friend _LIBCPP_INLINE_VISIBILITY
3833 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003835 friend _LIBCPP_INLINE_VISIBILITY
3836 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837 {return !(__x == __y);}
3838 };
3839
3840private:
3841 param_type __p_;
3842
3843public:
3844 // constructors and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003846 explicit bernoulli_distribution(double __p = 0.5)
3847 : __p_(param_type(__p)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851 void reset() {}
3852
3853 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003854 template<class _URNG>
3855 _LIBCPP_INLINE_VISIBILITY
3856 result_type operator()(_URNG& __g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003858 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003859
3860 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003862 double p() const {return __p_.p();}
3863
Howard Hinnantf5f99992010-09-22 18:02:38 +00003864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003865 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867 void param(const param_type& __p) {__p_ = __p;}
3868
Howard Hinnantf5f99992010-09-22 18:02:38 +00003869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870 result_type min() const {return false;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872 result_type max() const {return true;}
3873
Howard Hinnantf5f99992010-09-22 18:02:38 +00003874 friend _LIBCPP_INLINE_VISIBILITY
3875 bool operator==(const bernoulli_distribution& __x,
3876 const bernoulli_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003878 friend _LIBCPP_INLINE_VISIBILITY
3879 bool operator!=(const bernoulli_distribution& __x,
3880 const bernoulli_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881 {return !(__x == __y);}
3882};
3883
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003884template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003885inline
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003886bernoulli_distribution::result_type
3887bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3888{
Howard Hinnant481ba382010-05-20 15:11:46 +00003889 uniform_real_distribution<double> __gen;
3890 return __gen(__g) < __p.p();
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003891}
3892
Howard Hinnantc51e1022010-05-11 19:42:16 +00003893template <class _CharT, class _Traits>
3894basic_ostream<_CharT, _Traits>&
3895operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3896{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003897 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00003898 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3899 ios_base::scientific);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900 _CharT __sp = __os.widen(' ');
3901 __os.fill(__sp);
3902 return __os << __x.p();
3903}
3904
3905template <class _CharT, class _Traits>
3906basic_istream<_CharT, _Traits>&
3907operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3908{
3909 typedef bernoulli_distribution _Eng;
3910 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003911 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912 __is.flags(ios_base::dec | ios_base::skipws);
3913 double __p;
3914 __is >> __p;
3915 if (!__is.fail())
3916 __x.param(param_type(__p));
3917 return __is;
3918}
3919
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003920// binomial_distribution
3921
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003922template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003923class _LIBCPP_TEMPLATE_VIS binomial_distribution
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003924{
3925public:
3926 // types
3927 typedef _IntType result_type;
3928
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003929 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003930 {
3931 result_type __t_;
3932 double __p_;
Howard Hinnant76310352010-05-15 21:36:23 +00003933 double __pr_;
3934 double __odds_ratio_;
3935 result_type __r0_;
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003936 public:
3937 typedef binomial_distribution distribution_type;
3938
Howard Hinnant76310352010-05-15 21:36:23 +00003939 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003940
Howard Hinnantf5f99992010-09-22 18:02:38 +00003941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003942 result_type t() const {return __t_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003944 double p() const {return __p_;}
3945
Howard Hinnantf5f99992010-09-22 18:02:38 +00003946 friend _LIBCPP_INLINE_VISIBILITY
3947 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003948 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003949 friend _LIBCPP_INLINE_VISIBILITY
3950 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003951 {return !(__x == __y);}
Howard Hinnant76310352010-05-15 21:36:23 +00003952
3953 friend class binomial_distribution;
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003954 };
3955
3956private:
3957 param_type __p_;
3958
3959public:
3960 // constructors and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003962 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3963 : __p_(param_type(__t, __p)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003965 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003967 void reset() {}
3968
3969 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003970 template<class _URNG>
3971 _LIBCPP_INLINE_VISIBILITY
3972 result_type operator()(_URNG& __g)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003973 {return (*this)(__g, __p_);}
3974 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3975
3976 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003978 result_type t() const {return __p_.t();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003980 double p() const {return __p_.p();}
3981
Howard Hinnantf5f99992010-09-22 18:02:38 +00003982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003983 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003984 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003985 void param(const param_type& __p) {__p_ = __p;}
3986
Howard Hinnantf5f99992010-09-22 18:02:38 +00003987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003988 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003990 result_type max() const {return t();}
3991
Howard Hinnantf5f99992010-09-22 18:02:38 +00003992 friend _LIBCPP_INLINE_VISIBILITY
3993 bool operator==(const binomial_distribution& __x,
3994 const binomial_distribution& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003995 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003996 friend _LIBCPP_INLINE_VISIBILITY
3997 bool operator!=(const binomial_distribution& __x,
3998 const binomial_distribution& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003999 {return !(__x == __y);}
4000};
4001
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004002#ifndef _LIBCPP_MSVCRT
Marshall Clow7db28572017-05-04 16:36:39 +00004003extern "C" double lgamma_r(double, int *);
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004004#endif
4005
4006inline _LIBCPP_INLINE_VISIBILITY double __libcpp_lgamma(double __d) {
4007#if defined(_LIBCPP_MSVCRT)
4008 return lgamma(__d);
4009#else
4010 int __sign;
4011 return lgamma_r(__d, &__sign);
4012#endif
4013}
Marshall Clow7db28572017-05-04 16:36:39 +00004014
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004015template<class _IntType>
Marshall Clow7db28572017-05-04 16:36:39 +00004016binomial_distribution<_IntType>::param_type::param_type(const result_type __t, const double __p)
Howard Hinnant76310352010-05-15 21:36:23 +00004017 : __t_(__t), __p_(__p)
4018{
4019 if (0 < __p_ && __p_ < 1)
4020 {
4021 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004022 __pr_ = _VSTD::exp(__libcpp_lgamma(__t_ + 1.) -
4023 __libcpp_lgamma(__r0_ + 1.) -
4024 __libcpp_lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
Marshall Clow7db28572017-05-04 16:36:39 +00004025 (__t_ - __r0_) * _VSTD::log(1 - __p_));
Howard Hinnant76310352010-05-15 21:36:23 +00004026 __odds_ratio_ = __p_ / (1 - __p_);
4027 }
4028}
4029
Marshall Clowf97a84f2014-09-17 18:33:58 +00004030// Reference: Kemp, C.D. (1986). `A modal method for generating binomial
4031// variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
Howard Hinnant76310352010-05-15 21:36:23 +00004032template<class _IntType>
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004033template<class _URNG>
4034_IntType
Howard Hinnant76310352010-05-15 21:36:23 +00004035binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004036{
Howard Hinnant76310352010-05-15 21:36:23 +00004037 if (__pr.__t_ == 0 || __pr.__p_ == 0)
4038 return 0;
4039 if (__pr.__p_ == 1)
4040 return __pr.__t_;
4041 uniform_real_distribution<double> __gen;
4042 double __u = __gen(__g) - __pr.__pr_;
4043 if (__u < 0)
4044 return __pr.__r0_;
4045 double __pu = __pr.__pr_;
4046 double __pd = __pu;
4047 result_type __ru = __pr.__r0_;
4048 result_type __rd = __ru;
4049 while (true)
4050 {
4051 if (__rd >= 1)
4052 {
4053 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
4054 __u -= __pd;
4055 if (__u < 0)
4056 return __rd - 1;
4057 }
Marshall Clowf97a84f2014-09-17 18:33:58 +00004058 if ( __rd != 0 )
4059 --__rd;
Howard Hinnant76310352010-05-15 21:36:23 +00004060 ++__ru;
4061 if (__ru <= __pr.__t_)
4062 {
4063 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
4064 __u -= __pu;
4065 if (__u < 0)
4066 return __ru;
4067 }
4068 }
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004069}
4070
4071template <class _CharT, class _Traits, class _IntType>
4072basic_ostream<_CharT, _Traits>&
4073operator<<(basic_ostream<_CharT, _Traits>& __os,
4074 const binomial_distribution<_IntType>& __x)
4075{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004076 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00004077 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4078 ios_base::scientific);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004079 _CharT __sp = __os.widen(' ');
4080 __os.fill(__sp);
4081 return __os << __x.t() << __sp << __x.p();
4082}
4083
4084template <class _CharT, class _Traits, class _IntType>
4085basic_istream<_CharT, _Traits>&
4086operator>>(basic_istream<_CharT, _Traits>& __is,
4087 binomial_distribution<_IntType>& __x)
4088{
4089 typedef binomial_distribution<_IntType> _Eng;
4090 typedef typename _Eng::result_type result_type;
4091 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004092 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004093 __is.flags(ios_base::dec | ios_base::skipws);
4094 result_type __t;
4095 double __p;
4096 __is >> __t >> __p;
4097 if (!__is.fail())
4098 __x.param(param_type(__t, __p));
4099 return __is;
4100}
4101
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004102// exponential_distribution
4103
4104template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004105class _LIBCPP_TEMPLATE_VIS exponential_distribution
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004106{
4107public:
4108 // types
4109 typedef _RealType result_type;
4110
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004111 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004112 {
4113 result_type __lambda_;
4114 public:
4115 typedef exponential_distribution distribution_type;
4116
Howard Hinnantf5f99992010-09-22 18:02:38 +00004117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004118 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
4119
Howard Hinnantf5f99992010-09-22 18:02:38 +00004120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004121 result_type lambda() const {return __lambda_;}
4122
Howard Hinnantf5f99992010-09-22 18:02:38 +00004123 friend _LIBCPP_INLINE_VISIBILITY
4124 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004125 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004126 friend _LIBCPP_INLINE_VISIBILITY
4127 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004128 {return !(__x == __y);}
4129 };
4130
4131private:
4132 param_type __p_;
4133
4134public:
4135 // constructors and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004137 explicit exponential_distribution(result_type __lambda = 1)
4138 : __p_(param_type(__lambda)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004140 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004142 void reset() {}
4143
4144 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004145 template<class _URNG>
4146 _LIBCPP_INLINE_VISIBILITY
4147 result_type operator()(_URNG& __g)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004148 {return (*this)(__g, __p_);}
4149 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4150
4151 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004153 result_type lambda() const {return __p_.lambda();}
4154
Howard Hinnantf5f99992010-09-22 18:02:38 +00004155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004156 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004158 void param(const param_type& __p) {__p_ = __p;}
4159
Howard Hinnantf5f99992010-09-22 18:02:38 +00004160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004161 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant021f9902010-05-16 17:56:20 +00004163 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004164
Howard Hinnantf5f99992010-09-22 18:02:38 +00004165 friend _LIBCPP_INLINE_VISIBILITY
4166 bool operator==(const exponential_distribution& __x,
4167 const exponential_distribution& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004168 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004169 friend _LIBCPP_INLINE_VISIBILITY
4170 bool operator!=(const exponential_distribution& __x,
4171 const exponential_distribution& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004172 {return !(__x == __y);}
4173};
4174
4175template <class _RealType>
4176template<class _URNG>
4177_RealType
4178exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4179{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004180 return -_VSTD::log
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004181 (
4182 result_type(1) -
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004183 _VSTD::generate_canonical<result_type,
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004184 numeric_limits<result_type>::digits>(__g)
4185 )
4186 / __p.lambda();
4187}
4188
4189template <class _CharT, class _Traits, class _RealType>
4190basic_ostream<_CharT, _Traits>&
4191operator<<(basic_ostream<_CharT, _Traits>& __os,
4192 const exponential_distribution<_RealType>& __x)
4193{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004194 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00004195 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4196 ios_base::scientific);
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004197 return __os << __x.lambda();
4198}
4199
4200template <class _CharT, class _Traits, class _RealType>
4201basic_istream<_CharT, _Traits>&
4202operator>>(basic_istream<_CharT, _Traits>& __is,
4203 exponential_distribution<_RealType>& __x)
4204{
4205 typedef exponential_distribution<_RealType> _Eng;
4206 typedef typename _Eng::result_type result_type;
4207 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004208 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004209 __is.flags(ios_base::dec | ios_base::skipws);
4210 result_type __lambda;
4211 __is >> __lambda;
4212 if (!__is.fail())
4213 __x.param(param_type(__lambda));
4214 return __is;
4215}
4216
Howard Hinnant76310352010-05-15 21:36:23 +00004217// normal_distribution
4218
4219template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004220class _LIBCPP_TEMPLATE_VIS normal_distribution
Howard Hinnant76310352010-05-15 21:36:23 +00004221{
4222public:
4223 // types
4224 typedef _RealType result_type;
4225
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004226 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant76310352010-05-15 21:36:23 +00004227 {
4228 result_type __mean_;
4229 result_type __stddev_;
4230 public:
4231 typedef normal_distribution distribution_type;
4232
Howard Hinnantf5f99992010-09-22 18:02:38 +00004233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004234 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4235 : __mean_(__mean), __stddev_(__stddev) {}
4236
Howard Hinnantf5f99992010-09-22 18:02:38 +00004237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004238 result_type mean() const {return __mean_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004240 result_type stddev() const {return __stddev_;}
4241
Howard Hinnantf5f99992010-09-22 18:02:38 +00004242 friend _LIBCPP_INLINE_VISIBILITY
4243 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004244 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004245 friend _LIBCPP_INLINE_VISIBILITY
4246 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004247 {return !(__x == __y);}
4248 };
4249
4250private:
4251 param_type __p_;
4252 result_type _V_;
4253 bool _V_hot_;
4254
4255public:
4256 // constructors and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004258 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4259 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004261 explicit normal_distribution(const param_type& __p)
4262 : __p_(__p), _V_hot_(false) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004264 void reset() {_V_hot_ = false;}
4265
4266 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004267 template<class _URNG>
4268 _LIBCPP_INLINE_VISIBILITY
4269 result_type operator()(_URNG& __g)
Howard Hinnant76310352010-05-15 21:36:23 +00004270 {return (*this)(__g, __p_);}
4271 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4272
4273 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004275 result_type mean() const {return __p_.mean();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004277 result_type stddev() const {return __p_.stddev();}
4278
Howard Hinnantf5f99992010-09-22 18:02:38 +00004279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004280 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004282 void param(const param_type& __p) {__p_ = __p;}
4283
Howard Hinnantf5f99992010-09-22 18:02:38 +00004284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004285 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004287 result_type max() const {return numeric_limits<result_type>::infinity();}
4288
Howard Hinnantf5f99992010-09-22 18:02:38 +00004289 friend _LIBCPP_INLINE_VISIBILITY
4290 bool operator==(const normal_distribution& __x,
4291 const normal_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004292 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4293 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004294 friend _LIBCPP_INLINE_VISIBILITY
4295 bool operator!=(const normal_distribution& __x,
4296 const normal_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004297 {return !(__x == __y);}
4298
4299 template <class _CharT, class _Traits, class _RT>
4300 friend
4301 basic_ostream<_CharT, _Traits>&
4302 operator<<(basic_ostream<_CharT, _Traits>& __os,
4303 const normal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004304
Howard Hinnant76310352010-05-15 21:36:23 +00004305 template <class _CharT, class _Traits, class _RT>
4306 friend
4307 basic_istream<_CharT, _Traits>&
4308 operator>>(basic_istream<_CharT, _Traits>& __is,
4309 normal_distribution<_RT>& __x);
4310};
4311
4312template <class _RealType>
4313template<class _URNG>
4314_RealType
4315normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4316{
Howard Hinnantc834c512011-11-29 18:15:50 +00004317 result_type _Up;
Howard Hinnant76310352010-05-15 21:36:23 +00004318 if (_V_hot_)
4319 {
4320 _V_hot_ = false;
Howard Hinnantc834c512011-11-29 18:15:50 +00004321 _Up = _V_;
Howard Hinnant76310352010-05-15 21:36:23 +00004322 }
4323 else
4324 {
4325 uniform_real_distribution<result_type> _Uni(-1, 1);
4326 result_type __u;
4327 result_type __v;
4328 result_type __s;
4329 do
4330 {
4331 __u = _Uni(__g);
4332 __v = _Uni(__g);
4333 __s = __u * __u + __v * __v;
4334 } while (__s > 1 || __s == 0);
Howard Hinnantc834c512011-11-29 18:15:50 +00004335 result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
4336 _V_ = __v * _Fp;
Howard Hinnant76310352010-05-15 21:36:23 +00004337 _V_hot_ = true;
Howard Hinnantc834c512011-11-29 18:15:50 +00004338 _Up = __u * _Fp;
Howard Hinnant76310352010-05-15 21:36:23 +00004339 }
Howard Hinnantc834c512011-11-29 18:15:50 +00004340 return _Up * __p.stddev() + __p.mean();
Howard Hinnant76310352010-05-15 21:36:23 +00004341}
4342
4343template <class _CharT, class _Traits, class _RT>
4344basic_ostream<_CharT, _Traits>&
4345operator<<(basic_ostream<_CharT, _Traits>& __os,
4346 const normal_distribution<_RT>& __x)
4347{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004348 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00004349 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4350 ios_base::scientific);
Howard Hinnant76310352010-05-15 21:36:23 +00004351 _CharT __sp = __os.widen(' ');
4352 __os.fill(__sp);
4353 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4354 if (__x._V_hot_)
4355 __os << __sp << __x._V_;
4356 return __os;
4357}
4358
4359template <class _CharT, class _Traits, class _RT>
4360basic_istream<_CharT, _Traits>&
4361operator>>(basic_istream<_CharT, _Traits>& __is,
4362 normal_distribution<_RT>& __x)
4363{
4364 typedef normal_distribution<_RT> _Eng;
4365 typedef typename _Eng::result_type result_type;
4366 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004367 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant76310352010-05-15 21:36:23 +00004368 __is.flags(ios_base::dec | ios_base::skipws);
4369 result_type __mean;
4370 result_type __stddev;
Howard Hinnantc834c512011-11-29 18:15:50 +00004371 result_type _Vp = 0;
Howard Hinnant76310352010-05-15 21:36:23 +00004372 bool _V_hot = false;
4373 __is >> __mean >> __stddev >> _V_hot;
4374 if (_V_hot)
Howard Hinnantc834c512011-11-29 18:15:50 +00004375 __is >> _Vp;
Howard Hinnant76310352010-05-15 21:36:23 +00004376 if (!__is.fail())
4377 {
4378 __x.param(param_type(__mean, __stddev));
4379 __x._V_hot_ = _V_hot;
Howard Hinnantc834c512011-11-29 18:15:50 +00004380 __x._V_ = _Vp;
Howard Hinnant76310352010-05-15 21:36:23 +00004381 }
4382 return __is;
4383}
4384
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004385// lognormal_distribution
4386
4387template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004388class _LIBCPP_TEMPLATE_VIS lognormal_distribution
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004389{
4390public:
4391 // types
4392 typedef _RealType result_type;
4393
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004394 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004395 {
4396 normal_distribution<result_type> __nd_;
4397 public:
4398 typedef lognormal_distribution distribution_type;
4399
Howard Hinnantf5f99992010-09-22 18:02:38 +00004400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004401 explicit param_type(result_type __m = 0, result_type __s = 1)
4402 : __nd_(__m, __s) {}
4403
Howard Hinnantf5f99992010-09-22 18:02:38 +00004404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004405 result_type m() const {return __nd_.mean();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004407 result_type s() const {return __nd_.stddev();}
4408
Howard Hinnantf5f99992010-09-22 18:02:38 +00004409 friend _LIBCPP_INLINE_VISIBILITY
4410 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004411 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004412 friend _LIBCPP_INLINE_VISIBILITY
4413 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004414 {return !(__x == __y);}
4415 friend class lognormal_distribution;
4416
4417 template <class _CharT, class _Traits, class _RT>
4418 friend
4419 basic_ostream<_CharT, _Traits>&
4420 operator<<(basic_ostream<_CharT, _Traits>& __os,
4421 const lognormal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004422
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004423 template <class _CharT, class _Traits, class _RT>
4424 friend
4425 basic_istream<_CharT, _Traits>&
4426 operator>>(basic_istream<_CharT, _Traits>& __is,
4427 lognormal_distribution<_RT>& __x);
4428 };
4429
4430private:
4431 param_type __p_;
4432
4433public:
4434 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004436 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4437 : __p_(param_type(__m, __s)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004439 explicit lognormal_distribution(const param_type& __p)
4440 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004442 void reset() {__p_.__nd_.reset();}
4443
4444 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004445 template<class _URNG>
4446 _LIBCPP_INLINE_VISIBILITY
4447 result_type operator()(_URNG& __g)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004448 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004449 template<class _URNG>
4450 _LIBCPP_INLINE_VISIBILITY
4451 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004452 {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004453
4454 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004456 result_type m() const {return __p_.m();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004458 result_type s() const {return __p_.s();}
4459
Howard Hinnantf5f99992010-09-22 18:02:38 +00004460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004461 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004462 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00004463 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004464
Howard Hinnantf5f99992010-09-22 18:02:38 +00004465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004466 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004468 result_type max() const {return numeric_limits<result_type>::infinity();}
4469
Howard Hinnantf5f99992010-09-22 18:02:38 +00004470 friend _LIBCPP_INLINE_VISIBILITY
4471 bool operator==(const lognormal_distribution& __x,
4472 const lognormal_distribution& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004473 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004474 friend _LIBCPP_INLINE_VISIBILITY
4475 bool operator!=(const lognormal_distribution& __x,
4476 const lognormal_distribution& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004477 {return !(__x == __y);}
4478
4479 template <class _CharT, class _Traits, class _RT>
4480 friend
4481 basic_ostream<_CharT, _Traits>&
4482 operator<<(basic_ostream<_CharT, _Traits>& __os,
4483 const lognormal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004484
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004485 template <class _CharT, class _Traits, class _RT>
4486 friend
4487 basic_istream<_CharT, _Traits>&
4488 operator>>(basic_istream<_CharT, _Traits>& __is,
4489 lognormal_distribution<_RT>& __x);
4490};
4491
4492template <class _CharT, class _Traits, class _RT>
Howard Hinnantf5f99992010-09-22 18:02:38 +00004493inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004494basic_ostream<_CharT, _Traits>&
4495operator<<(basic_ostream<_CharT, _Traits>& __os,
4496 const lognormal_distribution<_RT>& __x)
4497{
4498 return __os << __x.__p_.__nd_;
4499}
4500
4501template <class _CharT, class _Traits, class _RT>
Howard Hinnantf5f99992010-09-22 18:02:38 +00004502inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004503basic_istream<_CharT, _Traits>&
4504operator>>(basic_istream<_CharT, _Traits>& __is,
4505 lognormal_distribution<_RT>& __x)
4506{
4507 return __is >> __x.__p_.__nd_;
4508}
4509
Howard Hinnant76310352010-05-15 21:36:23 +00004510// poisson_distribution
4511
4512template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004513class _LIBCPP_TEMPLATE_VIS poisson_distribution
Howard Hinnant76310352010-05-15 21:36:23 +00004514{
4515public:
4516 // types
4517 typedef _IntType result_type;
4518
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004519 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant76310352010-05-15 21:36:23 +00004520 {
4521 double __mean_;
4522 double __s_;
4523 double __d_;
4524 double __l_;
4525 double __omega_;
4526 double __c0_;
4527 double __c1_;
4528 double __c2_;
4529 double __c3_;
4530 double __c_;
4531
4532 public:
4533 typedef poisson_distribution distribution_type;
4534
4535 explicit param_type(double __mean = 1.0);
4536
Howard Hinnantf5f99992010-09-22 18:02:38 +00004537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004538 double mean() const {return __mean_;}
4539
Howard Hinnantf5f99992010-09-22 18:02:38 +00004540 friend _LIBCPP_INLINE_VISIBILITY
4541 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004542 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004543 friend _LIBCPP_INLINE_VISIBILITY
4544 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004545 {return !(__x == __y);}
4546
4547 friend class poisson_distribution;
4548 };
4549
4550private:
4551 param_type __p_;
4552
4553public:
4554 // constructors and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004556 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004558 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004560 void reset() {}
4561
4562 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004563 template<class _URNG>
4564 _LIBCPP_INLINE_VISIBILITY
4565 result_type operator()(_URNG& __g)
Howard Hinnant76310352010-05-15 21:36:23 +00004566 {return (*this)(__g, __p_);}
4567 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4568
4569 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004571 double mean() const {return __p_.mean();}
4572
Howard Hinnantf5f99992010-09-22 18:02:38 +00004573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004574 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004576 void param(const param_type& __p) {__p_ = __p;}
4577
Howard Hinnantf5f99992010-09-22 18:02:38 +00004578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004579 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004581 result_type max() const {return numeric_limits<result_type>::max();}
4582
Howard Hinnantf5f99992010-09-22 18:02:38 +00004583 friend _LIBCPP_INLINE_VISIBILITY
4584 bool operator==(const poisson_distribution& __x,
4585 const poisson_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004586 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004587 friend _LIBCPP_INLINE_VISIBILITY
4588 bool operator!=(const poisson_distribution& __x,
4589 const poisson_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004590 {return !(__x == __y);}
4591};
4592
4593template<class _IntType>
4594poisson_distribution<_IntType>::param_type::param_type(double __mean)
4595 : __mean_(__mean)
4596{
4597 if (__mean_ < 10)
4598 {
4599 __s_ = 0;
4600 __d_ = 0;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004601 __l_ = _VSTD::exp(-__mean_);
Howard Hinnant76310352010-05-15 21:36:23 +00004602 __omega_ = 0;
4603 __c3_ = 0;
4604 __c2_ = 0;
4605 __c1_ = 0;
4606 __c0_ = 0;
4607 __c_ = 0;
4608 }
4609 else
4610 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004611 __s_ = _VSTD::sqrt(__mean_);
Howard Hinnant76310352010-05-15 21:36:23 +00004612 __d_ = 6 * __mean_ * __mean_;
4613 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4614 __omega_ = .3989423 / __s_;
4615 double __b1_ = .4166667E-1 / __mean_;
4616 double __b2_ = .3 * __b1_ * __b1_;
4617 __c3_ = .1428571 * __b1_ * __b2_;
4618 __c2_ = __b2_ - 15. * __c3_;
4619 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4620 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4621 __c_ = .1069 / __mean_;
4622 }
4623}
4624
4625template <class _IntType>
4626template<class _URNG>
4627_IntType
4628poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4629{
4630 result_type __x;
4631 uniform_real_distribution<double> __urd;
Howard Hinnanta5fb7ac2011-04-14 15:59:22 +00004632 if (__pr.__mean_ < 10)
Howard Hinnant76310352010-05-15 21:36:23 +00004633 {
4634 __x = 0;
4635 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4636 __p *= __urd(__urng);
4637 }
4638 else
4639 {
4640 double __difmuk;
4641 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4642 double __u;
4643 if (__g > 0)
4644 {
4645 __x = static_cast<result_type>(__g);
4646 if (__x >= __pr.__l_)
4647 return __x;
4648 __difmuk = __pr.__mean_ - __x;
4649 __u = __urd(__urng);
4650 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4651 return __x;
4652 }
4653 exponential_distribution<double> __edist;
4654 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4655 {
4656 double __e;
4657 if (__using_exp_dist || __g < 0)
4658 {
4659 double __t;
4660 do
4661 {
4662 __e = __edist(__urng);
4663 __u = __urd(__urng);
4664 __u += __u - 1;
4665 __t = 1.8 + (__u < 0 ? -__e : __e);
4666 } while (__t <= -.6744);
4667 __x = __pr.__mean_ + __pr.__s_ * __t;
4668 __difmuk = __pr.__mean_ - __x;
4669 __using_exp_dist = true;
4670 }
4671 double __px;
4672 double __py;
4673 if (__x < 10)
4674 {
Marshall Clowc7e36e82018-01-16 14:54:36 +00004675 const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
Howard Hinnant76310352010-05-15 21:36:23 +00004676 40320, 362880};
4677 __px = -__pr.__mean_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004678 __py = _VSTD::pow(__pr.__mean_, (double)__x) / __fac[__x];
Howard Hinnant76310352010-05-15 21:36:23 +00004679 }
4680 else
4681 {
4682 double __del = .8333333E-1 / __x;
4683 __del -= 4.8 * __del * __del * __del;
4684 double __v = __difmuk / __x;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004685 if (_VSTD::abs(__v) > 0.25)
4686 __px = __x * _VSTD::log(1 + __v) - __difmuk - __del;
Howard Hinnant76310352010-05-15 21:36:23 +00004687 else
4688 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4689 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4690 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004691 __py = .3989423 / _VSTD::sqrt(__x);
Howard Hinnant76310352010-05-15 21:36:23 +00004692 }
4693 double __r = (0.5 - __difmuk) / __pr.__s_;
4694 double __r2 = __r * __r;
4695 double __fx = -0.5 * __r2;
4696 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4697 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4698 if (__using_exp_dist)
4699 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004700 if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4701 __fy * _VSTD::exp(__fx + __e))
Howard Hinnant76310352010-05-15 21:36:23 +00004702 break;
4703 }
4704 else
4705 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004706 if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
Howard Hinnant76310352010-05-15 21:36:23 +00004707 break;
4708 }
4709 }
4710 }
4711 return __x;
4712}
4713
4714template <class _CharT, class _Traits, class _IntType>
4715basic_ostream<_CharT, _Traits>&
4716operator<<(basic_ostream<_CharT, _Traits>& __os,
4717 const poisson_distribution<_IntType>& __x)
4718{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004719 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00004720 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4721 ios_base::scientific);
Howard Hinnant76310352010-05-15 21:36:23 +00004722 return __os << __x.mean();
4723}
4724
4725template <class _CharT, class _Traits, class _IntType>
4726basic_istream<_CharT, _Traits>&
4727operator>>(basic_istream<_CharT, _Traits>& __is,
4728 poisson_distribution<_IntType>& __x)
4729{
4730 typedef poisson_distribution<_IntType> _Eng;
4731 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004732 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant76310352010-05-15 21:36:23 +00004733 __is.flags(ios_base::dec | ios_base::skipws);
4734 double __mean;
4735 __is >> __mean;
4736 if (!__is.fail())
4737 __x.param(param_type(__mean));
4738 return __is;
4739}
4740
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004741// weibull_distribution
4742
4743template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004744class _LIBCPP_TEMPLATE_VIS weibull_distribution
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004745{
4746public:
4747 // types
4748 typedef _RealType result_type;
4749
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004750 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004751 {
4752 result_type __a_;
4753 result_type __b_;
4754 public:
4755 typedef weibull_distribution distribution_type;
4756
Howard Hinnantf5f99992010-09-22 18:02:38 +00004757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004758 explicit param_type(result_type __a = 1, result_type __b = 1)
4759 : __a_(__a), __b_(__b) {}
4760
Howard Hinnantf5f99992010-09-22 18:02:38 +00004761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004762 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004764 result_type b() const {return __b_;}
4765
Howard Hinnantf5f99992010-09-22 18:02:38 +00004766 friend _LIBCPP_INLINE_VISIBILITY
4767 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004768 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004769 friend _LIBCPP_INLINE_VISIBILITY
4770 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004771 {return !(__x == __y);}
4772 };
4773
4774private:
4775 param_type __p_;
4776
4777public:
4778 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004780 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4781 : __p_(param_type(__a, __b)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004783 explicit weibull_distribution(const param_type& __p)
4784 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004786 void reset() {}
4787
4788 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004789 template<class _URNG>
4790 _LIBCPP_INLINE_VISIBILITY
4791 result_type operator()(_URNG& __g)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004792 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004793 template<class _URNG>
4794 _LIBCPP_INLINE_VISIBILITY
4795 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004796 {return __p.b() *
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004797 _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004798
4799 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004801 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004803 result_type b() const {return __p_.b();}
4804
Howard Hinnantf5f99992010-09-22 18:02:38 +00004805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004806 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004807 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004808 void param(const param_type& __p) {__p_ = __p;}
4809
Howard Hinnantf5f99992010-09-22 18:02:38 +00004810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004811 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004813 result_type max() const {return numeric_limits<result_type>::infinity();}
4814
Howard Hinnantf5f99992010-09-22 18:02:38 +00004815 friend _LIBCPP_INLINE_VISIBILITY
4816 bool operator==(const weibull_distribution& __x,
4817 const weibull_distribution& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004818 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004819 friend _LIBCPP_INLINE_VISIBILITY
4820 bool operator!=(const weibull_distribution& __x,
4821 const weibull_distribution& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004822 {return !(__x == __y);}
4823};
4824
4825template <class _CharT, class _Traits, class _RT>
4826basic_ostream<_CharT, _Traits>&
4827operator<<(basic_ostream<_CharT, _Traits>& __os,
4828 const weibull_distribution<_RT>& __x)
4829{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004830 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00004831 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4832 ios_base::scientific);
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004833 _CharT __sp = __os.widen(' ');
4834 __os.fill(__sp);
4835 __os << __x.a() << __sp << __x.b();
4836 return __os;
4837}
4838
4839template <class _CharT, class _Traits, class _RT>
4840basic_istream<_CharT, _Traits>&
4841operator>>(basic_istream<_CharT, _Traits>& __is,
4842 weibull_distribution<_RT>& __x)
4843{
4844 typedef weibull_distribution<_RT> _Eng;
4845 typedef typename _Eng::result_type result_type;
4846 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004847 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004848 __is.flags(ios_base::dec | ios_base::skipws);
4849 result_type __a;
4850 result_type __b;
4851 __is >> __a >> __b;
4852 if (!__is.fail())
4853 __x.param(param_type(__a, __b));
4854 return __is;
4855}
4856
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004857template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004858class _LIBCPP_TEMPLATE_VIS extreme_value_distribution
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004859{
4860public:
4861 // types
4862 typedef _RealType result_type;
4863
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004864 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004865 {
4866 result_type __a_;
4867 result_type __b_;
4868 public:
4869 typedef extreme_value_distribution distribution_type;
4870
Howard Hinnantf5f99992010-09-22 18:02:38 +00004871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004872 explicit param_type(result_type __a = 0, result_type __b = 1)
4873 : __a_(__a), __b_(__b) {}
4874
Howard Hinnantf5f99992010-09-22 18:02:38 +00004875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004876 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004878 result_type b() const {return __b_;}
4879
Howard Hinnantf5f99992010-09-22 18:02:38 +00004880 friend _LIBCPP_INLINE_VISIBILITY
4881 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004882 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004883 friend _LIBCPP_INLINE_VISIBILITY
4884 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004885 {return !(__x == __y);}
4886 };
4887
4888private:
4889 param_type __p_;
4890
4891public:
4892 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004894 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4895 : __p_(param_type(__a, __b)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004897 explicit extreme_value_distribution(const param_type& __p)
4898 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004900 void reset() {}
4901
4902 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004903 template<class _URNG>
4904 _LIBCPP_INLINE_VISIBILITY
4905 result_type operator()(_URNG& __g)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004906 {return (*this)(__g, __p_);}
4907 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4908
4909 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004911 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004913 result_type b() const {return __p_.b();}
4914
Howard Hinnantf5f99992010-09-22 18:02:38 +00004915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004916 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004918 void param(const param_type& __p) {__p_ = __p;}
4919
Howard Hinnantf5f99992010-09-22 18:02:38 +00004920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004921 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004923 result_type max() const {return numeric_limits<result_type>::infinity();}
4924
Howard Hinnantf5f99992010-09-22 18:02:38 +00004925 friend _LIBCPP_INLINE_VISIBILITY
4926 bool operator==(const extreme_value_distribution& __x,
4927 const extreme_value_distribution& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004928 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004929 friend _LIBCPP_INLINE_VISIBILITY
4930 bool operator!=(const extreme_value_distribution& __x,
4931 const extreme_value_distribution& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004932 {return !(__x == __y);}
4933};
4934
4935template<class _RealType>
4936template<class _URNG>
4937_RealType
4938extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4939{
4940 return __p.a() - __p.b() *
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004941 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004942}
4943
4944template <class _CharT, class _Traits, class _RT>
4945basic_ostream<_CharT, _Traits>&
4946operator<<(basic_ostream<_CharT, _Traits>& __os,
4947 const extreme_value_distribution<_RT>& __x)
4948{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004949 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00004950 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4951 ios_base::scientific);
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004952 _CharT __sp = __os.widen(' ');
4953 __os.fill(__sp);
4954 __os << __x.a() << __sp << __x.b();
4955 return __os;
4956}
4957
4958template <class _CharT, class _Traits, class _RT>
4959basic_istream<_CharT, _Traits>&
4960operator>>(basic_istream<_CharT, _Traits>& __is,
4961 extreme_value_distribution<_RT>& __x)
4962{
4963 typedef extreme_value_distribution<_RT> _Eng;
4964 typedef typename _Eng::result_type result_type;
4965 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004966 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00004967 __is.flags(ios_base::dec | ios_base::skipws);
4968 result_type __a;
4969 result_type __b;
4970 __is >> __a >> __b;
4971 if (!__is.fail())
4972 __x.param(param_type(__a, __b));
4973 return __is;
4974}
4975
Howard Hinnantbb6d2022010-05-13 17:58:28 +00004976// gamma_distribution
4977
4978template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004979class _LIBCPP_TEMPLATE_VIS gamma_distribution
Howard Hinnantbb6d2022010-05-13 17:58:28 +00004980{
4981public:
4982 // types
4983 typedef _RealType result_type;
4984
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004985 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantbb6d2022010-05-13 17:58:28 +00004986 {
4987 result_type __alpha_;
4988 result_type __beta_;
4989 public:
4990 typedef gamma_distribution distribution_type;
4991
Howard Hinnantf5f99992010-09-22 18:02:38 +00004992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00004993 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4994 : __alpha_(__alpha), __beta_(__beta) {}
4995
Howard Hinnantf5f99992010-09-22 18:02:38 +00004996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00004997 result_type alpha() const {return __alpha_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00004999 result_type beta() const {return __beta_;}
5000
Howard Hinnantf5f99992010-09-22 18:02:38 +00005001 friend _LIBCPP_INLINE_VISIBILITY
5002 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005003 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005004 friend _LIBCPP_INLINE_VISIBILITY
5005 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005006 {return !(__x == __y);}
5007 };
5008
5009private:
5010 param_type __p_;
5011
5012public:
5013 // constructors and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005015 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
5016 : __p_(param_type(__alpha, __beta)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005018 explicit gamma_distribution(const param_type& __p)
5019 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005021 void reset() {}
5022
5023 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005024 template<class _URNG>
5025 _LIBCPP_INLINE_VISIBILITY
5026 result_type operator()(_URNG& __g)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005027 {return (*this)(__g, __p_);}
5028 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5029
5030 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005031 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005032 result_type alpha() const {return __p_.alpha();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005034 result_type beta() const {return __p_.beta();}
5035
Howard Hinnantf5f99992010-09-22 18:02:38 +00005036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005037 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005039 void param(const param_type& __p) {__p_ = __p;}
5040
Howard Hinnantf5f99992010-09-22 18:02:38 +00005041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005042 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005044 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005045
Howard Hinnantf5f99992010-09-22 18:02:38 +00005046 friend _LIBCPP_INLINE_VISIBILITY
5047 bool operator==(const gamma_distribution& __x,
5048 const gamma_distribution& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005049 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005050 friend _LIBCPP_INLINE_VISIBILITY
5051 bool operator!=(const gamma_distribution& __x,
5052 const gamma_distribution& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005053 {return !(__x == __y);}
5054};
5055
5056template <class _RealType>
5057template<class _URNG>
5058_RealType
5059gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5060{
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005061 result_type __a = __p.alpha();
5062 uniform_real_distribution<result_type> __gen(0, 1);
5063 exponential_distribution<result_type> __egen;
5064 result_type __x;
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005065 if (__a == 1)
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005066 __x = __egen(__g);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005067 else if (__a > 1)
5068 {
5069 const result_type __b = __a - 1;
5070 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005071 while (true)
5072 {
5073 const result_type __u = __gen(__g);
5074 const result_type __v = __gen(__g);
5075 const result_type __w = __u * (1 - __u);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005076 if (__w != 0)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005077 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005078 const result_type __y = _VSTD::sqrt(__c / __w) *
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005079 (__u - result_type(0.5));
5080 __x = __b + __y;
5081 if (__x >= 0)
5082 {
5083 const result_type __z = 64 * __w * __w * __w * __v * __v;
5084 if (__z <= 1 - 2 * __y * __y / __x)
5085 break;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005086 if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005087 break;
5088 }
5089 }
5090 }
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005091 }
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005092 else // __a < 1
5093 {
5094 while (true)
5095 {
5096 const result_type __u = __gen(__g);
5097 const result_type __es = __egen(__g);
5098 if (__u <= 1 - __a)
5099 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005100 __x = _VSTD::pow(__u, 1 / __a);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005101 if (__x <= __es)
5102 break;
5103 }
5104 else
5105 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005106 const result_type __e = -_VSTD::log((1-__u)/__a);
5107 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005108 if (__x <= __e + __es)
5109 break;
5110 }
5111 }
5112 }
5113 return __x * __p.beta();
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005114}
5115
5116template <class _CharT, class _Traits, class _RT>
5117basic_ostream<_CharT, _Traits>&
5118operator<<(basic_ostream<_CharT, _Traits>& __os,
5119 const gamma_distribution<_RT>& __x)
5120{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005121 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00005122 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5123 ios_base::scientific);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005124 _CharT __sp = __os.widen(' ');
5125 __os.fill(__sp);
5126 __os << __x.alpha() << __sp << __x.beta();
5127 return __os;
5128}
5129
5130template <class _CharT, class _Traits, class _RT>
5131basic_istream<_CharT, _Traits>&
5132operator>>(basic_istream<_CharT, _Traits>& __is,
5133 gamma_distribution<_RT>& __x)
5134{
5135 typedef gamma_distribution<_RT> _Eng;
5136 typedef typename _Eng::result_type result_type;
5137 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005138 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005139 __is.flags(ios_base::dec | ios_base::skipws);
5140 result_type __alpha;
5141 result_type __beta;
5142 __is >> __alpha >> __beta;
5143 if (!__is.fail())
5144 __x.param(param_type(__alpha, __beta));
5145 return __is;
5146}
Howard Hinnant0b95bc92010-05-12 21:02:31 +00005147
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005148// negative_binomial_distribution
5149
5150template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005151class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005152{
5153public:
5154 // types
5155 typedef _IntType result_type;
5156
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005157 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005158 {
5159 result_type __k_;
5160 double __p_;
5161 public:
5162 typedef negative_binomial_distribution distribution_type;
5163
Howard Hinnantf5f99992010-09-22 18:02:38 +00005164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005165 explicit param_type(result_type __k = 1, double __p = 0.5)
5166 : __k_(__k), __p_(__p) {}
5167
Howard Hinnantf5f99992010-09-22 18:02:38 +00005168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005169 result_type k() const {return __k_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005171 double p() const {return __p_;}
5172
Howard Hinnantf5f99992010-09-22 18:02:38 +00005173 friend _LIBCPP_INLINE_VISIBILITY
5174 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005175 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005176 friend _LIBCPP_INLINE_VISIBILITY
5177 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005178 {return !(__x == __y);}
5179 };
5180
5181private:
5182 param_type __p_;
5183
5184public:
5185 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005187 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
5188 : __p_(__k, __p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005190 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005192 void reset() {}
5193
5194 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005195 template<class _URNG>
5196 _LIBCPP_INLINE_VISIBILITY
5197 result_type operator()(_URNG& __g)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005198 {return (*this)(__g, __p_);}
5199 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5200
5201 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005203 result_type k() const {return __p_.k();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005205 double p() const {return __p_.p();}
5206
Howard Hinnantf5f99992010-09-22 18:02:38 +00005207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005208 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005210 void param(const param_type& __p) {__p_ = __p;}
5211
Howard Hinnantf5f99992010-09-22 18:02:38 +00005212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005213 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005215 result_type max() const {return numeric_limits<result_type>::max();}
5216
Howard Hinnantf5f99992010-09-22 18:02:38 +00005217 friend _LIBCPP_INLINE_VISIBILITY
5218 bool operator==(const negative_binomial_distribution& __x,
5219 const negative_binomial_distribution& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005220 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005221 friend _LIBCPP_INLINE_VISIBILITY
5222 bool operator!=(const negative_binomial_distribution& __x,
5223 const negative_binomial_distribution& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005224 {return !(__x == __y);}
5225};
5226
5227template <class _IntType>
5228template<class _URNG>
5229_IntType
5230negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5231{
5232 result_type __k = __pr.k();
5233 double __p = __pr.p();
5234 if (__k <= 21 * __p)
5235 {
5236 bernoulli_distribution __gen(__p);
5237 result_type __f = 0;
5238 result_type __s = 0;
5239 while (__s < __k)
5240 {
5241 if (__gen(__urng))
5242 ++__s;
5243 else
5244 ++__f;
5245 }
5246 return __f;
5247 }
5248 return poisson_distribution<result_type>(gamma_distribution<double>
5249 (__k, (1-__p)/__p)(__urng))(__urng);
5250}
5251
5252template <class _CharT, class _Traits, class _IntType>
5253basic_ostream<_CharT, _Traits>&
5254operator<<(basic_ostream<_CharT, _Traits>& __os,
5255 const negative_binomial_distribution<_IntType>& __x)
5256{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005257 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00005258 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5259 ios_base::scientific);
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005260 _CharT __sp = __os.widen(' ');
5261 __os.fill(__sp);
5262 return __os << __x.k() << __sp << __x.p();
5263}
5264
5265template <class _CharT, class _Traits, class _IntType>
5266basic_istream<_CharT, _Traits>&
5267operator>>(basic_istream<_CharT, _Traits>& __is,
5268 negative_binomial_distribution<_IntType>& __x)
5269{
5270 typedef negative_binomial_distribution<_IntType> _Eng;
5271 typedef typename _Eng::result_type result_type;
5272 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005273 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005274 __is.flags(ios_base::dec | ios_base::skipws);
5275 result_type __k;
5276 double __p;
5277 __is >> __k >> __p;
5278 if (!__is.fail())
5279 __x.param(param_type(__k, __p));
5280 return __is;
5281}
5282
Howard Hinnant28beb162010-05-17 13:44:27 +00005283// geometric_distribution
5284
5285template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005286class _LIBCPP_TEMPLATE_VIS geometric_distribution
Howard Hinnant28beb162010-05-17 13:44:27 +00005287{
5288public:
5289 // types
5290 typedef _IntType result_type;
5291
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005292 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant28beb162010-05-17 13:44:27 +00005293 {
5294 double __p_;
5295 public:
5296 typedef geometric_distribution distribution_type;
5297
Howard Hinnantf5f99992010-09-22 18:02:38 +00005298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005299 explicit param_type(double __p = 0.5) : __p_(__p) {}
5300
Howard Hinnantf5f99992010-09-22 18:02:38 +00005301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005302 double p() const {return __p_;}
5303
Howard Hinnantf5f99992010-09-22 18:02:38 +00005304 friend _LIBCPP_INLINE_VISIBILITY
5305 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005306 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005307 friend _LIBCPP_INLINE_VISIBILITY
5308 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005309 {return !(__x == __y);}
5310 };
5311
5312private:
5313 param_type __p_;
5314
5315public:
5316 // constructors and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005318 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005320 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005322 void reset() {}
5323
5324 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005325 template<class _URNG>
5326 _LIBCPP_INLINE_VISIBILITY
5327 result_type operator()(_URNG& __g)
Howard Hinnant28beb162010-05-17 13:44:27 +00005328 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005329 template<class _URNG>
5330 _LIBCPP_INLINE_VISIBILITY
5331 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant28beb162010-05-17 13:44:27 +00005332 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5333
5334 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005336 double p() const {return __p_.p();}
5337
Howard Hinnantf5f99992010-09-22 18:02:38 +00005338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005339 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005341 void param(const param_type& __p) {__p_ = __p;}
5342
Howard Hinnantf5f99992010-09-22 18:02:38 +00005343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005344 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005345 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005346 result_type max() const {return numeric_limits<result_type>::max();}
5347
Howard Hinnantf5f99992010-09-22 18:02:38 +00005348 friend _LIBCPP_INLINE_VISIBILITY
5349 bool operator==(const geometric_distribution& __x,
5350 const geometric_distribution& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005351 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005352 friend _LIBCPP_INLINE_VISIBILITY
5353 bool operator!=(const geometric_distribution& __x,
5354 const geometric_distribution& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005355 {return !(__x == __y);}
5356};
5357
5358template <class _CharT, class _Traits, class _IntType>
5359basic_ostream<_CharT, _Traits>&
5360operator<<(basic_ostream<_CharT, _Traits>& __os,
5361 const geometric_distribution<_IntType>& __x)
5362{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005363 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00005364 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5365 ios_base::scientific);
Howard Hinnant28beb162010-05-17 13:44:27 +00005366 return __os << __x.p();
5367}
5368
5369template <class _CharT, class _Traits, class _IntType>
5370basic_istream<_CharT, _Traits>&
5371operator>>(basic_istream<_CharT, _Traits>& __is,
5372 geometric_distribution<_IntType>& __x)
5373{
5374 typedef geometric_distribution<_IntType> _Eng;
5375 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005376 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant28beb162010-05-17 13:44:27 +00005377 __is.flags(ios_base::dec | ios_base::skipws);
5378 double __p;
5379 __is >> __p;
5380 if (!__is.fail())
5381 __x.param(param_type(__p));
5382 return __is;
5383}
5384
Howard Hinnant252ebf02010-05-15 23:36:00 +00005385// chi_squared_distribution
5386
5387template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005388class _LIBCPP_TEMPLATE_VIS chi_squared_distribution
Howard Hinnant252ebf02010-05-15 23:36:00 +00005389{
5390public:
5391 // types
5392 typedef _RealType result_type;
5393
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005394 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant252ebf02010-05-15 23:36:00 +00005395 {
5396 result_type __n_;
5397 public:
5398 typedef chi_squared_distribution distribution_type;
5399
Howard Hinnantf5f99992010-09-22 18:02:38 +00005400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005401 explicit param_type(result_type __n = 1) : __n_(__n) {}
5402
Howard Hinnantf5f99992010-09-22 18:02:38 +00005403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005404 result_type n() const {return __n_;}
5405
Howard Hinnantf5f99992010-09-22 18:02:38 +00005406 friend _LIBCPP_INLINE_VISIBILITY
5407 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005408 {return __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005409 friend _LIBCPP_INLINE_VISIBILITY
5410 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005411 {return !(__x == __y);}
5412 };
5413
5414private:
5415 param_type __p_;
5416
5417public:
5418 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005420 explicit chi_squared_distribution(result_type __n = 1)
5421 : __p_(param_type(__n)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005423 explicit chi_squared_distribution(const param_type& __p)
5424 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005426 void reset() {}
5427
5428 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005429 template<class _URNG>
5430 _LIBCPP_INLINE_VISIBILITY
5431 result_type operator()(_URNG& __g)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005432 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005433 template<class _URNG>
5434 _LIBCPP_INLINE_VISIBILITY
5435 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005436 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5437
5438 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005440 result_type n() const {return __p_.n();}
5441
Howard Hinnantf5f99992010-09-22 18:02:38 +00005442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005443 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005445 void param(const param_type& __p) {__p_ = __p;}
5446
Howard Hinnantf5f99992010-09-22 18:02:38 +00005447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005448 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005450 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant252ebf02010-05-15 23:36:00 +00005451
Howard Hinnantf5f99992010-09-22 18:02:38 +00005452 friend _LIBCPP_INLINE_VISIBILITY
5453 bool operator==(const chi_squared_distribution& __x,
5454 const chi_squared_distribution& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005455 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005456 friend _LIBCPP_INLINE_VISIBILITY
5457 bool operator!=(const chi_squared_distribution& __x,
5458 const chi_squared_distribution& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005459 {return !(__x == __y);}
5460};
5461
5462template <class _CharT, class _Traits, class _RT>
5463basic_ostream<_CharT, _Traits>&
5464operator<<(basic_ostream<_CharT, _Traits>& __os,
5465 const chi_squared_distribution<_RT>& __x)
5466{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005467 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00005468 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5469 ios_base::scientific);
Howard Hinnant252ebf02010-05-15 23:36:00 +00005470 __os << __x.n();
5471 return __os;
5472}
5473
5474template <class _CharT, class _Traits, class _RT>
5475basic_istream<_CharT, _Traits>&
5476operator>>(basic_istream<_CharT, _Traits>& __is,
5477 chi_squared_distribution<_RT>& __x)
5478{
5479 typedef chi_squared_distribution<_RT> _Eng;
5480 typedef typename _Eng::result_type result_type;
5481 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005482 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant252ebf02010-05-15 23:36:00 +00005483 __is.flags(ios_base::dec | ios_base::skipws);
5484 result_type __n;
5485 __is >> __n;
5486 if (!__is.fail())
5487 __x.param(param_type(__n));
5488 return __is;
5489}
5490
Howard Hinnantf3292562010-05-17 21:55:46 +00005491// cauchy_distribution
5492
5493template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005494class _LIBCPP_TEMPLATE_VIS cauchy_distribution
Howard Hinnantf3292562010-05-17 21:55:46 +00005495{
5496public:
5497 // types
5498 typedef _RealType result_type;
5499
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005500 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantf3292562010-05-17 21:55:46 +00005501 {
5502 result_type __a_;
5503 result_type __b_;
5504 public:
5505 typedef cauchy_distribution distribution_type;
5506
Howard Hinnantf5f99992010-09-22 18:02:38 +00005507 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005508 explicit param_type(result_type __a = 0, result_type __b = 1)
5509 : __a_(__a), __b_(__b) {}
5510
Howard Hinnantf5f99992010-09-22 18:02:38 +00005511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005512 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005514 result_type b() const {return __b_;}
5515
Howard Hinnantf5f99992010-09-22 18:02:38 +00005516 friend _LIBCPP_INLINE_VISIBILITY
5517 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005518 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005519 friend _LIBCPP_INLINE_VISIBILITY
5520 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005521 {return !(__x == __y);}
5522 };
5523
5524private:
5525 param_type __p_;
5526
5527public:
5528 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005530 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5531 : __p_(param_type(__a, __b)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005533 explicit cauchy_distribution(const param_type& __p)
5534 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005536 void reset() {}
5537
5538 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005539 template<class _URNG>
5540 _LIBCPP_INLINE_VISIBILITY
5541 result_type operator()(_URNG& __g)
Howard Hinnantf3292562010-05-17 21:55:46 +00005542 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005543 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantf3292562010-05-17 21:55:46 +00005544
5545 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005547 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005549 result_type b() const {return __p_.b();}
5550
Howard Hinnantf5f99992010-09-22 18:02:38 +00005551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005552 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005554 void param(const param_type& __p) {__p_ = __p;}
5555
Howard Hinnantf5f99992010-09-22 18:02:38 +00005556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005557 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005559 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantf3292562010-05-17 21:55:46 +00005560
Howard Hinnantf5f99992010-09-22 18:02:38 +00005561 friend _LIBCPP_INLINE_VISIBILITY
5562 bool operator==(const cauchy_distribution& __x,
5563 const cauchy_distribution& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005564 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005565 friend _LIBCPP_INLINE_VISIBILITY
5566 bool operator!=(const cauchy_distribution& __x,
5567 const cauchy_distribution& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005568 {return !(__x == __y);}
Howard Hinnantf3292562010-05-17 21:55:46 +00005569};
5570
5571template <class _RealType>
5572template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005573inline
Howard Hinnantf3292562010-05-17 21:55:46 +00005574_RealType
5575cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5576{
5577 uniform_real_distribution<result_type> __gen;
5578 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005579 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
Howard Hinnantf3292562010-05-17 21:55:46 +00005580}
5581
5582template <class _CharT, class _Traits, class _RT>
5583basic_ostream<_CharT, _Traits>&
5584operator<<(basic_ostream<_CharT, _Traits>& __os,
5585 const cauchy_distribution<_RT>& __x)
5586{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005587 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00005588 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5589 ios_base::scientific);
Howard Hinnantf3292562010-05-17 21:55:46 +00005590 _CharT __sp = __os.widen(' ');
5591 __os.fill(__sp);
5592 __os << __x.a() << __sp << __x.b();
5593 return __os;
5594}
5595
5596template <class _CharT, class _Traits, class _RT>
5597basic_istream<_CharT, _Traits>&
5598operator>>(basic_istream<_CharT, _Traits>& __is,
5599 cauchy_distribution<_RT>& __x)
5600{
5601 typedef cauchy_distribution<_RT> _Eng;
5602 typedef typename _Eng::result_type result_type;
5603 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005604 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantf3292562010-05-17 21:55:46 +00005605 __is.flags(ios_base::dec | ios_base::skipws);
5606 result_type __a;
5607 result_type __b;
5608 __is >> __a >> __b;
5609 if (!__is.fail())
5610 __x.param(param_type(__a, __b));
5611 return __is;
5612}
5613
Howard Hinnantf39463b2010-05-18 17:32:30 +00005614// fisher_f_distribution
5615
5616template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005617class _LIBCPP_TEMPLATE_VIS fisher_f_distribution
Howard Hinnantf39463b2010-05-18 17:32:30 +00005618{
5619public:
5620 // types
5621 typedef _RealType result_type;
5622
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005623 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantf39463b2010-05-18 17:32:30 +00005624 {
5625 result_type __m_;
5626 result_type __n_;
5627 public:
5628 typedef fisher_f_distribution distribution_type;
5629
Howard Hinnantf5f99992010-09-22 18:02:38 +00005630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005631 explicit param_type(result_type __m = 1, result_type __n = 1)
5632 : __m_(__m), __n_(__n) {}
5633
Howard Hinnantf5f99992010-09-22 18:02:38 +00005634 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005635 result_type m() const {return __m_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005637 result_type n() const {return __n_;}
5638
Howard Hinnantf5f99992010-09-22 18:02:38 +00005639 friend _LIBCPP_INLINE_VISIBILITY
5640 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005641 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005642 friend _LIBCPP_INLINE_VISIBILITY
5643 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005644 {return !(__x == __y);}
5645 };
5646
5647private:
5648 param_type __p_;
5649
5650public:
5651 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005653 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5654 : __p_(param_type(__m, __n)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005656 explicit fisher_f_distribution(const param_type& __p)
5657 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005659 void reset() {}
5660
5661 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005662 template<class _URNG>
5663 _LIBCPP_INLINE_VISIBILITY
5664 result_type operator()(_URNG& __g)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005665 {return (*this)(__g, __p_);}
5666 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5667
5668 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005670 result_type m() const {return __p_.m();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005672 result_type n() const {return __p_.n();}
5673
Howard Hinnantf5f99992010-09-22 18:02:38 +00005674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005675 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005677 void param(const param_type& __p) {__p_ = __p;}
5678
Howard Hinnantf5f99992010-09-22 18:02:38 +00005679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005680 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005682 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantf39463b2010-05-18 17:32:30 +00005683
Howard Hinnantf5f99992010-09-22 18:02:38 +00005684 friend _LIBCPP_INLINE_VISIBILITY
5685 bool operator==(const fisher_f_distribution& __x,
5686 const fisher_f_distribution& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005687 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005688 friend _LIBCPP_INLINE_VISIBILITY
5689 bool operator!=(const fisher_f_distribution& __x,
5690 const fisher_f_distribution& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005691 {return !(__x == __y);}
5692};
5693
5694template <class _RealType>
5695template<class _URNG>
5696_RealType
5697fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5698{
5699 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5700 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5701 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5702}
5703
5704template <class _CharT, class _Traits, class _RT>
5705basic_ostream<_CharT, _Traits>&
5706operator<<(basic_ostream<_CharT, _Traits>& __os,
5707 const fisher_f_distribution<_RT>& __x)
5708{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005709 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00005710 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5711 ios_base::scientific);
Howard Hinnantf39463b2010-05-18 17:32:30 +00005712 _CharT __sp = __os.widen(' ');
5713 __os.fill(__sp);
5714 __os << __x.m() << __sp << __x.n();
5715 return __os;
5716}
5717
5718template <class _CharT, class _Traits, class _RT>
5719basic_istream<_CharT, _Traits>&
5720operator>>(basic_istream<_CharT, _Traits>& __is,
5721 fisher_f_distribution<_RT>& __x)
5722{
5723 typedef fisher_f_distribution<_RT> _Eng;
5724 typedef typename _Eng::result_type result_type;
5725 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005726 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnantf39463b2010-05-18 17:32:30 +00005727 __is.flags(ios_base::dec | ios_base::skipws);
5728 result_type __m;
5729 result_type __n;
5730 __is >> __m >> __n;
5731 if (!__is.fail())
5732 __x.param(param_type(__m, __n));
5733 return __is;
5734}
5735
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005736// student_t_distribution
5737
Howard Hinnant20c50882010-05-18 20:08:04 +00005738template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005739class _LIBCPP_TEMPLATE_VIS student_t_distribution
Howard Hinnant20c50882010-05-18 20:08:04 +00005740{
5741public:
5742 // types
5743 typedef _RealType result_type;
5744
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005745 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant20c50882010-05-18 20:08:04 +00005746 {
5747 result_type __n_;
5748 public:
5749 typedef student_t_distribution distribution_type;
5750
Howard Hinnantf5f99992010-09-22 18:02:38 +00005751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00005752 explicit param_type(result_type __n = 1) : __n_(__n) {}
5753
Howard Hinnantf5f99992010-09-22 18:02:38 +00005754 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00005755 result_type n() const {return __n_;}
5756
Howard Hinnantf5f99992010-09-22 18:02:38 +00005757 friend _LIBCPP_INLINE_VISIBILITY
5758 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00005759 {return __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005760 friend _LIBCPP_INLINE_VISIBILITY
5761 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00005762 {return !(__x == __y);}
5763 };
5764
5765private:
5766 param_type __p_;
5767 normal_distribution<result_type> __nd_;
5768
5769public:
5770 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00005772 explicit student_t_distribution(result_type __n = 1)
5773 : __p_(param_type(__n)) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00005775 explicit student_t_distribution(const param_type& __p)
5776 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00005778 void reset() {__nd_.reset();}
5779
5780 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005781 template<class _URNG>
5782 _LIBCPP_INLINE_VISIBILITY
5783 result_type operator()(_URNG& __g)
Howard Hinnant20c50882010-05-18 20:08:04 +00005784 {return (*this)(__g, __p_);}
5785 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5786
5787 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00005789 result_type n() const {return __p_.n();}
5790
Howard Hinnantf5f99992010-09-22 18:02:38 +00005791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00005792 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005794 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant20c50882010-05-18 20:08:04 +00005795
Howard Hinnantf5f99992010-09-22 18:02:38 +00005796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00005797 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00005799 result_type max() const {return numeric_limits<result_type>::infinity();}
5800
Howard Hinnantf5f99992010-09-22 18:02:38 +00005801 friend _LIBCPP_INLINE_VISIBILITY
5802 bool operator==(const student_t_distribution& __x,
5803 const student_t_distribution& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00005804 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005805 friend _LIBCPP_INLINE_VISIBILITY
5806 bool operator!=(const student_t_distribution& __x,
5807 const student_t_distribution& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00005808 {return !(__x == __y);}
5809};
5810
5811template <class _RealType>
5812template<class _URNG>
5813_RealType
5814student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5815{
5816 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005817 return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
Howard Hinnant20c50882010-05-18 20:08:04 +00005818}
5819
5820template <class _CharT, class _Traits, class _RT>
5821basic_ostream<_CharT, _Traits>&
5822operator<<(basic_ostream<_CharT, _Traits>& __os,
5823 const student_t_distribution<_RT>& __x)
5824{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005825 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00005826 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5827 ios_base::scientific);
Howard Hinnant20c50882010-05-18 20:08:04 +00005828 __os << __x.n();
5829 return __os;
5830}
5831
5832template <class _CharT, class _Traits, class _RT>
5833basic_istream<_CharT, _Traits>&
5834operator>>(basic_istream<_CharT, _Traits>& __is,
5835 student_t_distribution<_RT>& __x)
5836{
5837 typedef student_t_distribution<_RT> _Eng;
5838 typedef typename _Eng::result_type result_type;
5839 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005840 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant20c50882010-05-18 20:08:04 +00005841 __is.flags(ios_base::dec | ios_base::skipws);
5842 result_type __n;
5843 __is >> __n;
5844 if (!__is.fail())
5845 __x.param(param_type(__n));
5846 return __is;
5847}
5848
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005849// discrete_distribution
5850
5851template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005852class _LIBCPP_TEMPLATE_VIS discrete_distribution
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005853{
5854public:
5855 // types
5856 typedef _IntType result_type;
5857
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005858 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005859 {
5860 vector<double> __p_;
5861 public:
5862 typedef discrete_distribution distribution_type;
5863
Howard Hinnantf5f99992010-09-22 18:02:38 +00005864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005865 param_type() {}
5866 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00005867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005868 param_type(_InputIterator __f, _InputIterator __l)
5869 : __p_(__f, __l) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00005870#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005872 param_type(initializer_list<double> __wl)
5873 : __p_(__wl.begin(), __wl.end()) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00005874#endif // _LIBCPP_CXX03_LANG
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005875 template<class _UnaryOperation>
5876 param_type(size_t __nw, double __xmin, double __xmax,
5877 _UnaryOperation __fw);
5878
5879 vector<double> probabilities() const;
5880
Howard Hinnantf5f99992010-09-22 18:02:38 +00005881 friend _LIBCPP_INLINE_VISIBILITY
5882 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005883 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005884 friend _LIBCPP_INLINE_VISIBILITY
5885 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005886 {return !(__x == __y);}
5887
5888 private:
5889 void __init();
5890
5891 friend class discrete_distribution;
5892
5893 template <class _CharT, class _Traits, class _IT>
5894 friend
5895 basic_ostream<_CharT, _Traits>&
5896 operator<<(basic_ostream<_CharT, _Traits>& __os,
5897 const discrete_distribution<_IT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005898
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005899 template <class _CharT, class _Traits, class _IT>
5900 friend
5901 basic_istream<_CharT, _Traits>&
5902 operator>>(basic_istream<_CharT, _Traits>& __is,
5903 discrete_distribution<_IT>& __x);
5904 };
5905
5906private:
5907 param_type __p_;
5908
5909public:
5910 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005912 discrete_distribution() {}
5913 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00005914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005915 discrete_distribution(_InputIterator __f, _InputIterator __l)
5916 : __p_(__f, __l) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00005917#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005919 discrete_distribution(initializer_list<double> __wl)
5920 : __p_(__wl) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00005921#endif // _LIBCPP_CXX03_LANG
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005922 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00005923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005924 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5925 _UnaryOperation __fw)
5926 : __p_(__nw, __xmin, __xmax, __fw) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00005928 explicit discrete_distribution(const param_type& __p)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005929 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005931 void reset() {}
5932
5933 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005934 template<class _URNG>
5935 _LIBCPP_INLINE_VISIBILITY
5936 result_type operator()(_URNG& __g)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005937 {return (*this)(__g, __p_);}
5938 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5939
5940 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005942 vector<double> probabilities() const {return __p_.probabilities();}
5943
Howard Hinnantf5f99992010-09-22 18:02:38 +00005944 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005945 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005947 void param(const param_type& __p) {__p_ = __p;}
5948
Howard Hinnantf5f99992010-09-22 18:02:38 +00005949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005950 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005952 result_type max() const {return __p_.__p_.size();}
5953
Howard Hinnantf5f99992010-09-22 18:02:38 +00005954 friend _LIBCPP_INLINE_VISIBILITY
5955 bool operator==(const discrete_distribution& __x,
5956 const discrete_distribution& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005957 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005958 friend _LIBCPP_INLINE_VISIBILITY
5959 bool operator!=(const discrete_distribution& __x,
5960 const discrete_distribution& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005961 {return !(__x == __y);}
5962
5963 template <class _CharT, class _Traits, class _IT>
5964 friend
5965 basic_ostream<_CharT, _Traits>&
5966 operator<<(basic_ostream<_CharT, _Traits>& __os,
5967 const discrete_distribution<_IT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005968
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005969 template <class _CharT, class _Traits, class _IT>
5970 friend
5971 basic_istream<_CharT, _Traits>&
5972 operator>>(basic_istream<_CharT, _Traits>& __is,
5973 discrete_distribution<_IT>& __x);
5974};
5975
5976template<class _IntType>
5977template<class _UnaryOperation>
5978discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5979 double __xmin,
5980 double __xmax,
5981 _UnaryOperation __fw)
5982{
5983 if (__nw > 1)
5984 {
5985 __p_.reserve(__nw - 1);
5986 double __d = (__xmax - __xmin) / __nw;
5987 double __d2 = __d / 2;
5988 for (size_t __k = 0; __k < __nw; ++__k)
5989 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5990 __init();
5991 }
5992}
5993
5994template<class _IntType>
5995void
5996discrete_distribution<_IntType>::param_type::__init()
5997{
5998 if (!__p_.empty())
5999 {
6000 if (__p_.size() > 1)
6001 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006002 double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
6003 for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006004 __i < __e; ++__i)
6005 *__i /= __s;
6006 vector<double> __t(__p_.size() - 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006007 _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006008 swap(__p_, __t);
6009 }
6010 else
6011 {
6012 __p_.clear();
6013 __p_.shrink_to_fit();
6014 }
6015 }
6016}
6017
6018template<class _IntType>
6019vector<double>
6020discrete_distribution<_IntType>::param_type::probabilities() const
6021{
6022 size_t __n = __p_.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006023 _VSTD::vector<double> __p(__n+1);
6024 _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006025 if (__n > 0)
6026 __p[__n] = 1 - __p_[__n-1];
6027 else
6028 __p[0] = 1;
6029 return __p;
6030}
6031
6032template<class _IntType>
6033template<class _URNG>
6034_IntType
6035discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
6036{
6037 uniform_real_distribution<double> __gen;
6038 return static_cast<_IntType>(
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006039 _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006040 __p.__p_.begin());
6041}
6042
6043template <class _CharT, class _Traits, class _IT>
6044basic_ostream<_CharT, _Traits>&
6045operator<<(basic_ostream<_CharT, _Traits>& __os,
6046 const discrete_distribution<_IT>& __x)
6047{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006048 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006049 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6050 ios_base::scientific);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006051 _CharT __sp = __os.widen(' ');
6052 __os.fill(__sp);
6053 size_t __n = __x.__p_.__p_.size();
6054 __os << __n;
6055 for (size_t __i = 0; __i < __n; ++__i)
6056 __os << __sp << __x.__p_.__p_[__i];
6057 return __os;
6058}
6059
6060template <class _CharT, class _Traits, class _IT>
6061basic_istream<_CharT, _Traits>&
6062operator>>(basic_istream<_CharT, _Traits>& __is,
6063 discrete_distribution<_IT>& __x)
6064{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006065 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006066 __is.flags(ios_base::dec | ios_base::skipws);
6067 size_t __n;
6068 __is >> __n;
Howard Hinnant481ba382010-05-20 15:11:46 +00006069 vector<double> __p(__n);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006070 for (size_t __i = 0; __i < __n; ++__i)
6071 __is >> __p[__i];
6072 if (!__is.fail())
6073 swap(__x.__p_.__p_, __p);
6074 return __is;
6075}
6076
Howard Hinnant481ba382010-05-20 15:11:46 +00006077// piecewise_constant_distribution
6078
6079template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006080class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution
Howard Hinnant481ba382010-05-20 15:11:46 +00006081{
6082public:
6083 // types
6084 typedef _RealType result_type;
6085
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006086 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant481ba382010-05-20 15:11:46 +00006087 {
Howard Hinnant481ba382010-05-20 15:11:46 +00006088 vector<result_type> __b_;
Howard Hinnant167159f2010-11-18 17:01:36 +00006089 vector<result_type> __densities_;
6090 vector<result_type> __areas_;
Howard Hinnant481ba382010-05-20 15:11:46 +00006091 public:
6092 typedef piecewise_constant_distribution distribution_type;
6093
6094 param_type();
6095 template<class _InputIteratorB, class _InputIteratorW>
6096 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6097 _InputIteratorW __fW);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006098#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006099 template<class _UnaryOperation>
6100 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006101#endif // _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006102 template<class _UnaryOperation>
6103 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6104 _UnaryOperation __fw);
Howard Hinnant71c410b2010-10-13 14:37:09 +00006105 param_type & operator=(const param_type& __rhs);
Howard Hinnant481ba382010-05-20 15:11:46 +00006106
Howard Hinnantf5f99992010-09-22 18:02:38 +00006107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006108 vector<result_type> intervals() const {return __b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006110 vector<result_type> densities() const {return __densities_;}
Howard Hinnant481ba382010-05-20 15:11:46 +00006111
Howard Hinnantf5f99992010-09-22 18:02:38 +00006112 friend _LIBCPP_INLINE_VISIBILITY
6113 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006114 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006115 friend _LIBCPP_INLINE_VISIBILITY
6116 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant481ba382010-05-20 15:11:46 +00006117 {return !(__x == __y);}
6118
6119 private:
6120 void __init();
6121
6122 friend class piecewise_constant_distribution;
6123
6124 template <class _CharT, class _Traits, class _RT>
6125 friend
6126 basic_ostream<_CharT, _Traits>&
6127 operator<<(basic_ostream<_CharT, _Traits>& __os,
6128 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006129
Howard Hinnant481ba382010-05-20 15:11:46 +00006130 template <class _CharT, class _Traits, class _RT>
6131 friend
6132 basic_istream<_CharT, _Traits>&
6133 operator>>(basic_istream<_CharT, _Traits>& __is,
6134 piecewise_constant_distribution<_RT>& __x);
6135 };
6136
6137private:
6138 param_type __p_;
6139
6140public:
6141 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006143 piecewise_constant_distribution() {}
6144 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006146 piecewise_constant_distribution(_InputIteratorB __fB,
6147 _InputIteratorB __lB,
6148 _InputIteratorW __fW)
6149 : __p_(__fB, __lB, __fW) {}
6150
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006151#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006152 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006154 piecewise_constant_distribution(initializer_list<result_type> __bl,
6155 _UnaryOperation __fw)
6156 : __p_(__bl, __fw) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006157#endif // _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006158
6159 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006161 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6162 result_type __xmax, _UnaryOperation __fw)
6163 : __p_(__nw, __xmin, __xmax, __fw) {}
6164
Howard Hinnantf5f99992010-09-22 18:02:38 +00006165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006166 explicit piecewise_constant_distribution(const param_type& __p)
6167 : __p_(__p) {}
6168
Howard Hinnantf5f99992010-09-22 18:02:38 +00006169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006170 void reset() {}
6171
6172 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006173 template<class _URNG>
6174 _LIBCPP_INLINE_VISIBILITY
6175 result_type operator()(_URNG& __g)
Howard Hinnant481ba382010-05-20 15:11:46 +00006176 {return (*this)(__g, __p_);}
6177 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6178
6179 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006181 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006183 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant481ba382010-05-20 15:11:46 +00006184
Howard Hinnantf5f99992010-09-22 18:02:38 +00006185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006186 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006188 void param(const param_type& __p) {__p_ = __p;}
6189
Howard Hinnantf5f99992010-09-22 18:02:38 +00006190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006191 result_type min() const {return __p_.__b_.front();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006193 result_type max() const {return __p_.__b_.back();}
6194
Howard Hinnantf5f99992010-09-22 18:02:38 +00006195 friend _LIBCPP_INLINE_VISIBILITY
6196 bool operator==(const piecewise_constant_distribution& __x,
6197 const piecewise_constant_distribution& __y)
Howard Hinnant481ba382010-05-20 15:11:46 +00006198 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006199 friend _LIBCPP_INLINE_VISIBILITY
6200 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnant481ba382010-05-20 15:11:46 +00006201 const piecewise_constant_distribution& __y)
6202 {return !(__x == __y);}
6203
6204 template <class _CharT, class _Traits, class _RT>
6205 friend
6206 basic_ostream<_CharT, _Traits>&
6207 operator<<(basic_ostream<_CharT, _Traits>& __os,
6208 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006209
Howard Hinnant481ba382010-05-20 15:11:46 +00006210 template <class _CharT, class _Traits, class _RT>
6211 friend
6212 basic_istream<_CharT, _Traits>&
6213 operator>>(basic_istream<_CharT, _Traits>& __is,
6214 piecewise_constant_distribution<_RT>& __x);
6215};
6216
6217template<class _RealType>
Howard Hinnant71c410b2010-10-13 14:37:09 +00006218typename piecewise_constant_distribution<_RealType>::param_type &
6219piecewise_constant_distribution<_RealType>::param_type::operator=
6220 (const param_type& __rhs)
6221{
6222// These can throw
6223 __b_.reserve (__rhs.__b_.size ());
6224 __densities_.reserve(__rhs.__densities_.size());
6225 __areas_.reserve (__rhs.__areas_.size());
6226
6227// These can not throw
6228 __b_ = __rhs.__b_;
6229 __densities_ = __rhs.__densities_;
6230 __areas_ = __rhs.__areas_;
6231 return *this;
6232}
6233
6234template<class _RealType>
Howard Hinnant481ba382010-05-20 15:11:46 +00006235void
6236piecewise_constant_distribution<_RealType>::param_type::__init()
6237{
Howard Hinnant35d83df2010-05-24 00:35:40 +00006238 // __densities_ contains non-normalized areas
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006239 result_type __total_area = _VSTD::accumulate(__densities_.begin(),
Howard Hinnant35d83df2010-05-24 00:35:40 +00006240 __densities_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006241 result_type());
Howard Hinnant35d83df2010-05-24 00:35:40 +00006242 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6243 __densities_[__i] /= __total_area;
6244 // __densities_ contains normalized areas
Howard Hinnant167159f2010-11-18 17:01:36 +00006245 __areas_.assign(__densities_.size(), result_type());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006246 _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
Howard Hinnant35d83df2010-05-24 00:35:40 +00006247 __areas_.begin() + 1);
6248 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6249 __densities_.back() = 1 - __areas_.back(); // correct round off error
6250 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6251 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6252 // __densities_ now contains __densities_
Howard Hinnant481ba382010-05-20 15:11:46 +00006253}
6254
6255template<class _RealType>
6256piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant35d83df2010-05-24 00:35:40 +00006257 : __b_(2),
Howard Hinnanta001ef32010-05-25 00:27:34 +00006258 __densities_(1, 1.0),
6259 __areas_(1, 0.0)
Howard Hinnant481ba382010-05-20 15:11:46 +00006260{
6261 __b_[1] = 1;
6262}
6263
6264template<class _RealType>
6265template<class _InputIteratorB, class _InputIteratorW>
6266piecewise_constant_distribution<_RealType>::param_type::param_type(
6267 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6268 : __b_(__fB, __lB)
6269{
6270 if (__b_.size() < 2)
6271 {
6272 __b_.resize(2);
6273 __b_[0] = 0;
6274 __b_[1] = 1;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006275 __densities_.assign(1, 1.0);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006276 __areas_.assign(1, 0.0);
Howard Hinnant481ba382010-05-20 15:11:46 +00006277 }
6278 else
6279 {
Howard Hinnant35d83df2010-05-24 00:35:40 +00006280 __densities_.reserve(__b_.size() - 1);
Howard Hinnant481ba382010-05-20 15:11:46 +00006281 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006282 __densities_.push_back(*__fW);
Howard Hinnant481ba382010-05-20 15:11:46 +00006283 __init();
6284 }
6285}
6286
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006287#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006288
Howard Hinnant481ba382010-05-20 15:11:46 +00006289template<class _RealType>
6290template<class _UnaryOperation>
6291piecewise_constant_distribution<_RealType>::param_type::param_type(
6292 initializer_list<result_type> __bl, _UnaryOperation __fw)
6293 : __b_(__bl.begin(), __bl.end())
6294{
6295 if (__b_.size() < 2)
6296 {
6297 __b_.resize(2);
6298 __b_[0] = 0;
6299 __b_[1] = 1;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006300 __densities_.assign(1, 1.0);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006301 __areas_.assign(1, 0.0);
Howard Hinnant481ba382010-05-20 15:11:46 +00006302 }
6303 else
6304 {
Howard Hinnant35d83df2010-05-24 00:35:40 +00006305 __densities_.reserve(__b_.size() - 1);
Howard Hinnant481ba382010-05-20 15:11:46 +00006306 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006307 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnant481ba382010-05-20 15:11:46 +00006308 __init();
6309 }
6310}
6311
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006312#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006313
Howard Hinnant481ba382010-05-20 15:11:46 +00006314template<class _RealType>
6315template<class _UnaryOperation>
6316piecewise_constant_distribution<_RealType>::param_type::param_type(
6317 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6318 : __b_(__nw == 0 ? 2 : __nw + 1)
6319{
6320 size_t __n = __b_.size() - 1;
6321 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006322 __densities_.reserve(__n);
Howard Hinnant481ba382010-05-20 15:11:46 +00006323 for (size_t __i = 0; __i < __n; ++__i)
6324 {
6325 __b_[__i] = __xmin + __i * __d;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006326 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnant481ba382010-05-20 15:11:46 +00006327 }
6328 __b_[__n] = __xmax;
6329 __init();
6330}
6331
6332template<class _RealType>
Howard Hinnant481ba382010-05-20 15:11:46 +00006333template<class _URNG>
6334_RealType
6335piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6336{
6337 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnant481ba382010-05-20 15:11:46 +00006338 result_type __u = _Gen()(__g);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006339 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006340 __u) - __p.__areas_.begin() - 1;
6341 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnant481ba382010-05-20 15:11:46 +00006342}
6343
6344template <class _CharT, class _Traits, class _RT>
6345basic_ostream<_CharT, _Traits>&
6346operator<<(basic_ostream<_CharT, _Traits>& __os,
6347 const piecewise_constant_distribution<_RT>& __x)
6348{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006349 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006350 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6351 ios_base::scientific);
Howard Hinnant481ba382010-05-20 15:11:46 +00006352 _CharT __sp = __os.widen(' ');
6353 __os.fill(__sp);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006354 size_t __n = __x.__p_.__b_.size();
Howard Hinnant481ba382010-05-20 15:11:46 +00006355 __os << __n;
6356 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006357 __os << __sp << __x.__p_.__b_[__i];
6358 __n = __x.__p_.__densities_.size();
Howard Hinnant481ba382010-05-20 15:11:46 +00006359 __os << __sp << __n;
6360 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006361 __os << __sp << __x.__p_.__densities_[__i];
6362 __n = __x.__p_.__areas_.size();
6363 __os << __sp << __n;
6364 for (size_t __i = 0; __i < __n; ++__i)
6365 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnant481ba382010-05-20 15:11:46 +00006366 return __os;
6367}
6368
6369template <class _CharT, class _Traits, class _RT>
6370basic_istream<_CharT, _Traits>&
6371operator>>(basic_istream<_CharT, _Traits>& __is,
6372 piecewise_constant_distribution<_RT>& __x)
6373{
6374 typedef piecewise_constant_distribution<_RT> _Eng;
6375 typedef typename _Eng::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00006376 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnant481ba382010-05-20 15:11:46 +00006377 __is.flags(ios_base::dec | ios_base::skipws);
6378 size_t __n;
6379 __is >> __n;
Howard Hinnant481ba382010-05-20 15:11:46 +00006380 vector<result_type> __b(__n);
6381 for (size_t __i = 0; __i < __n; ++__i)
6382 __is >> __b[__i];
Howard Hinnant35d83df2010-05-24 00:35:40 +00006383 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006384 vector<result_type> __densities(__n);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006385 for (size_t __i = 0; __i < __n; ++__i)
6386 __is >> __densities[__i];
6387 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006388 vector<result_type> __areas(__n);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006389 for (size_t __i = 0; __i < __n; ++__i)
6390 __is >> __areas[__i];
Howard Hinnant481ba382010-05-20 15:11:46 +00006391 if (!__is.fail())
6392 {
Howard Hinnant481ba382010-05-20 15:11:46 +00006393 swap(__x.__p_.__b_, __b);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006394 swap(__x.__p_.__densities_, __densities);
6395 swap(__x.__p_.__areas_, __areas);
Howard Hinnant481ba382010-05-20 15:11:46 +00006396 }
6397 return __is;
6398}
6399
Howard Hinnanta001ef32010-05-25 00:27:34 +00006400// piecewise_linear_distribution
6401
6402template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006403class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution
Howard Hinnanta001ef32010-05-25 00:27:34 +00006404{
6405public:
6406 // types
6407 typedef _RealType result_type;
6408
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006409 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnanta001ef32010-05-25 00:27:34 +00006410 {
Howard Hinnanta001ef32010-05-25 00:27:34 +00006411 vector<result_type> __b_;
Howard Hinnant167159f2010-11-18 17:01:36 +00006412 vector<result_type> __densities_;
6413 vector<result_type> __areas_;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006414 public:
6415 typedef piecewise_linear_distribution distribution_type;
6416
6417 param_type();
6418 template<class _InputIteratorB, class _InputIteratorW>
6419 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6420 _InputIteratorW __fW);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006421#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006422 template<class _UnaryOperation>
6423 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006424#endif // _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006425 template<class _UnaryOperation>
6426 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6427 _UnaryOperation __fw);
Howard Hinnant71c410b2010-10-13 14:37:09 +00006428 param_type & operator=(const param_type& __rhs);
6429
Howard Hinnantf5f99992010-09-22 18:02:38 +00006430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006431 vector<result_type> intervals() const {return __b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006433 vector<result_type> densities() const {return __densities_;}
Howard Hinnanta001ef32010-05-25 00:27:34 +00006434
Howard Hinnantf5f99992010-09-22 18:02:38 +00006435 friend _LIBCPP_INLINE_VISIBILITY
6436 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006437 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006438 friend _LIBCPP_INLINE_VISIBILITY
6439 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006440 {return !(__x == __y);}
6441
6442 private:
6443 void __init();
6444
6445 friend class piecewise_linear_distribution;
6446
6447 template <class _CharT, class _Traits, class _RT>
6448 friend
6449 basic_ostream<_CharT, _Traits>&
6450 operator<<(basic_ostream<_CharT, _Traits>& __os,
6451 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006452
Howard Hinnanta001ef32010-05-25 00:27:34 +00006453 template <class _CharT, class _Traits, class _RT>
6454 friend
6455 basic_istream<_CharT, _Traits>&
6456 operator>>(basic_istream<_CharT, _Traits>& __is,
6457 piecewise_linear_distribution<_RT>& __x);
6458 };
6459
6460private:
6461 param_type __p_;
6462
6463public:
6464 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006466 piecewise_linear_distribution() {}
6467 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006469 piecewise_linear_distribution(_InputIteratorB __fB,
6470 _InputIteratorB __lB,
6471 _InputIteratorW __fW)
6472 : __p_(__fB, __lB, __fW) {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006473
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006474#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006475 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006477 piecewise_linear_distribution(initializer_list<result_type> __bl,
6478 _UnaryOperation __fw)
6479 : __p_(__bl, __fw) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006480#endif // _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006481
6482 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006484 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6485 result_type __xmax, _UnaryOperation __fw)
6486 : __p_(__nw, __xmin, __xmax, __fw) {}
6487
Howard Hinnantf5f99992010-09-22 18:02:38 +00006488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006489 explicit piecewise_linear_distribution(const param_type& __p)
6490 : __p_(__p) {}
6491
Howard Hinnantf5f99992010-09-22 18:02:38 +00006492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006493 void reset() {}
6494
6495 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006496 template<class _URNG>
6497 _LIBCPP_INLINE_VISIBILITY
6498 result_type operator()(_URNG& __g)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006499 {return (*this)(__g, __p_);}
6500 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6501
6502 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006503 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006504 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006506 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnanta001ef32010-05-25 00:27:34 +00006507
Howard Hinnantf5f99992010-09-22 18:02:38 +00006508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006509 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006511 void param(const param_type& __p) {__p_ = __p;}
6512
Howard Hinnantf5f99992010-09-22 18:02:38 +00006513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006514 result_type min() const {return __p_.__b_.front();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006516 result_type max() const {return __p_.__b_.back();}
6517
Howard Hinnantf5f99992010-09-22 18:02:38 +00006518 friend _LIBCPP_INLINE_VISIBILITY
6519 bool operator==(const piecewise_linear_distribution& __x,
6520 const piecewise_linear_distribution& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006521 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006522 friend _LIBCPP_INLINE_VISIBILITY
6523 bool operator!=(const piecewise_linear_distribution& __x,
6524 const piecewise_linear_distribution& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006525 {return !(__x == __y);}
6526
6527 template <class _CharT, class _Traits, class _RT>
6528 friend
6529 basic_ostream<_CharT, _Traits>&
6530 operator<<(basic_ostream<_CharT, _Traits>& __os,
6531 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006532
Howard Hinnanta001ef32010-05-25 00:27:34 +00006533 template <class _CharT, class _Traits, class _RT>
6534 friend
6535 basic_istream<_CharT, _Traits>&
6536 operator>>(basic_istream<_CharT, _Traits>& __is,
6537 piecewise_linear_distribution<_RT>& __x);
6538};
6539
6540template<class _RealType>
Howard Hinnant71c410b2010-10-13 14:37:09 +00006541typename piecewise_linear_distribution<_RealType>::param_type &
6542piecewise_linear_distribution<_RealType>::param_type::operator=
6543 (const param_type& __rhs)
6544{
6545// These can throw
6546 __b_.reserve (__rhs.__b_.size ());
6547 __densities_.reserve(__rhs.__densities_.size());
6548 __areas_.reserve (__rhs.__areas_.size());
6549
6550// These can not throw
6551 __b_ = __rhs.__b_;
6552 __densities_ = __rhs.__densities_;
6553 __areas_ = __rhs.__areas_;
6554 return *this;
6555}
6556
6557
6558template<class _RealType>
Howard Hinnanta001ef32010-05-25 00:27:34 +00006559void
6560piecewise_linear_distribution<_RealType>::param_type::__init()
6561{
Howard Hinnant167159f2010-11-18 17:01:36 +00006562 __areas_.assign(__densities_.size() - 1, result_type());
Howard Hinnantc834c512011-11-29 18:15:50 +00006563 result_type _Sp = 0;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006564 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6565 {
6566 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6567 (__b_[__i+1] - __b_[__i]) * .5;
Howard Hinnantc834c512011-11-29 18:15:50 +00006568 _Sp += __areas_[__i];
Howard Hinnanta001ef32010-05-25 00:27:34 +00006569 }
6570 for (size_t __i = __areas_.size(); __i > 1;)
6571 {
6572 --__i;
Howard Hinnantc834c512011-11-29 18:15:50 +00006573 __areas_[__i] = __areas_[__i-1] / _Sp;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006574 }
6575 __areas_[0] = 0;
6576 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6577 __areas_[__i] += __areas_[__i-1];
6578 for (size_t __i = 0; __i < __densities_.size(); ++__i)
Howard Hinnantc834c512011-11-29 18:15:50 +00006579 __densities_[__i] /= _Sp;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006580}
6581
6582template<class _RealType>
6583piecewise_linear_distribution<_RealType>::param_type::param_type()
6584 : __b_(2),
6585 __densities_(2, 1.0),
6586 __areas_(1, 0.0)
6587{
6588 __b_[1] = 1;
6589}
6590
6591template<class _RealType>
6592template<class _InputIteratorB, class _InputIteratorW>
6593piecewise_linear_distribution<_RealType>::param_type::param_type(
6594 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6595 : __b_(__fB, __lB)
6596{
6597 if (__b_.size() < 2)
6598 {
6599 __b_.resize(2);
6600 __b_[0] = 0;
6601 __b_[1] = 1;
6602 __densities_.assign(2, 1.0);
6603 __areas_.assign(1, 0.0);
6604 }
6605 else
6606 {
6607 __densities_.reserve(__b_.size());
6608 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6609 __densities_.push_back(*__fW);
6610 __init();
6611 }
6612}
6613
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006614#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006615
Howard Hinnanta001ef32010-05-25 00:27:34 +00006616template<class _RealType>
6617template<class _UnaryOperation>
6618piecewise_linear_distribution<_RealType>::param_type::param_type(
6619 initializer_list<result_type> __bl, _UnaryOperation __fw)
6620 : __b_(__bl.begin(), __bl.end())
6621{
6622 if (__b_.size() < 2)
6623 {
6624 __b_.resize(2);
6625 __b_[0] = 0;
6626 __b_[1] = 1;
6627 __densities_.assign(2, 1.0);
6628 __areas_.assign(1, 0.0);
6629 }
6630 else
6631 {
6632 __densities_.reserve(__b_.size());
6633 for (size_t __i = 0; __i < __b_.size(); ++__i)
6634 __densities_.push_back(__fw(__b_[__i]));
6635 __init();
6636 }
6637}
6638
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006639#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006640
Howard Hinnanta001ef32010-05-25 00:27:34 +00006641template<class _RealType>
6642template<class _UnaryOperation>
6643piecewise_linear_distribution<_RealType>::param_type::param_type(
6644 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6645 : __b_(__nw == 0 ? 2 : __nw + 1)
6646{
6647 size_t __n = __b_.size() - 1;
6648 result_type __d = (__xmax - __xmin) / __n;
6649 __densities_.reserve(__b_.size());
6650 for (size_t __i = 0; __i < __n; ++__i)
6651 {
6652 __b_[__i] = __xmin + __i * __d;
6653 __densities_.push_back(__fw(__b_[__i]));
6654 }
6655 __b_[__n] = __xmax;
6656 __densities_.push_back(__fw(__b_[__n]));
6657 __init();
6658}
6659
6660template<class _RealType>
6661template<class _URNG>
6662_RealType
6663piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6664{
6665 typedef uniform_real_distribution<result_type> _Gen;
6666 result_type __u = _Gen()(__g);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006667 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006668 __u) - __p.__areas_.begin() - 1;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006669 __u -= __p.__areas_[__k];
Howard Hinnant167159f2010-11-18 17:01:36 +00006670 const result_type __dk = __p.__densities_[__k];
6671 const result_type __dk1 = __p.__densities_[__k+1];
6672 const result_type __deltad = __dk1 - __dk;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006673 const result_type __bk = __p.__b_[__k];
6674 if (__deltad == 0)
Howard Hinnant167159f2010-11-18 17:01:36 +00006675 return __u / __dk + __bk;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006676 const result_type __bk1 = __p.__b_[__k+1];
6677 const result_type __deltab = __bk1 - __bk;
Howard Hinnant167159f2010-11-18 17:01:36 +00006678 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006679 _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant167159f2010-11-18 17:01:36 +00006680 __deltad;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006681}
6682
6683template <class _CharT, class _Traits, class _RT>
6684basic_ostream<_CharT, _Traits>&
6685operator<<(basic_ostream<_CharT, _Traits>& __os,
6686 const piecewise_linear_distribution<_RT>& __x)
6687{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006688 __save_flags<_CharT, _Traits> __lx(__os);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006689 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6690 ios_base::scientific);
6691 _CharT __sp = __os.widen(' ');
6692 __os.fill(__sp);
6693 size_t __n = __x.__p_.__b_.size();
6694 __os << __n;
6695 for (size_t __i = 0; __i < __n; ++__i)
6696 __os << __sp << __x.__p_.__b_[__i];
6697 __n = __x.__p_.__densities_.size();
6698 __os << __sp << __n;
6699 for (size_t __i = 0; __i < __n; ++__i)
6700 __os << __sp << __x.__p_.__densities_[__i];
6701 __n = __x.__p_.__areas_.size();
6702 __os << __sp << __n;
6703 for (size_t __i = 0; __i < __n; ++__i)
6704 __os << __sp << __x.__p_.__areas_[__i];
6705 return __os;
6706}
6707
6708template <class _CharT, class _Traits, class _RT>
6709basic_istream<_CharT, _Traits>&
6710operator>>(basic_istream<_CharT, _Traits>& __is,
6711 piecewise_linear_distribution<_RT>& __x)
6712{
6713 typedef piecewise_linear_distribution<_RT> _Eng;
6714 typedef typename _Eng::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00006715 __save_flags<_CharT, _Traits> __lx(__is);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006716 __is.flags(ios_base::dec | ios_base::skipws);
6717 size_t __n;
6718 __is >> __n;
6719 vector<result_type> __b(__n);
6720 for (size_t __i = 0; __i < __n; ++__i)
6721 __is >> __b[__i];
6722 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006723 vector<result_type> __densities(__n);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006724 for (size_t __i = 0; __i < __n; ++__i)
6725 __is >> __densities[__i];
6726 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006727 vector<result_type> __areas(__n);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006728 for (size_t __i = 0; __i < __n; ++__i)
6729 __is >> __areas[__i];
6730 if (!__is.fail())
6731 {
6732 swap(__x.__p_.__b_, __b);
6733 swap(__x.__p_.__densities_, __densities);
6734 swap(__x.__p_.__areas_, __areas);
6735 }
6736 return __is;
6737}
6738
Howard Hinnantc51e1022010-05-11 19:42:16 +00006739_LIBCPP_END_NAMESPACE_STD
6740
Eric Fiselierf4433a32017-05-31 22:07:49 +00006741_LIBCPP_POP_MACROS
6742
Howard Hinnantc51e1022010-05-11 19:42:16 +00006743#endif // _LIBCPP_RANDOM