blob: 6e0d2ecb47c02d801e6ae3c4624c9fa00fc92732 [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
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +010039 explicit linear_congruential_engine(result_type s = default_seed); // before C++20
40 linear_congruential_engine() : linear_congruential_engine(default_seed) {} // C++20
41 explicit linear_congruential_engine(result_type s); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000042 template<class Sseq> explicit linear_congruential_engine(Sseq& q);
43 void seed(result_type s = default_seed);
44 template<class Sseq> void seed(Sseq& q);
45
46 // generating functions
47 result_type operator()();
48 void discard(unsigned long long z);
49};
50
51template <class UIntType, UIntType a, UIntType c, UIntType m>
52bool
53operator==(const linear_congruential_engine<UIntType, a, c, m>& x,
54 const linear_congruential_engine<UIntType, a, c, m>& y);
55
56template <class UIntType, UIntType a, UIntType c, UIntType m>
57bool
58operator!=(const linear_congruential_engine<UIntType, a, c, m>& x,
59 const linear_congruential_engine<UIntType, a, c, m>& y);
60
61template <class charT, class traits,
62 class UIntType, UIntType a, UIntType c, UIntType m>
63basic_ostream<charT, traits>&
64operator<<(basic_ostream<charT, traits>& os,
65 const linear_congruential_engine<UIntType, a, c, m>& x);
66
67template <class charT, class traits,
68 class UIntType, UIntType a, UIntType c, UIntType m>
69basic_istream<charT, traits>&
70operator>>(basic_istream<charT, traits>& is,
71 linear_congruential_engine<UIntType, a, c, m>& x);
72
73template <class UIntType, size_t w, size_t n, size_t m, size_t r,
74 UIntType a, size_t u, UIntType d, size_t s,
75 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
76class mersenne_twister_engine
77{
78public:
79 // types
80 typedef UIntType result_type;
81
82 // engine characteristics
83 static constexpr size_t word_size = w;
84 static constexpr size_t state_size = n;
85 static constexpr size_t shift_size = m;
86 static constexpr size_t mask_bits = r;
87 static constexpr result_type xor_mask = a;
88 static constexpr size_t tempering_u = u;
89 static constexpr result_type tempering_d = d;
90 static constexpr size_t tempering_s = s;
91 static constexpr result_type tempering_b = b;
92 static constexpr size_t tempering_t = t;
93 static constexpr result_type tempering_c = c;
94 static constexpr size_t tempering_l = l;
95 static constexpr result_type initialization_multiplier = f;
96 static constexpr result_type min () { return 0; }
97 static constexpr result_type max() { return 2^w - 1; }
98 static constexpr result_type default_seed = 5489u;
99
100 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100101 explicit mersenne_twister_engine(result_type s = default_seed); // before C++20
102 mersenne_twister_engine() : mersenne_twister_engine(default_seed) {} // C++20
103 explicit mersenne_twister_engine(result_type s); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000104 template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
105 void seed(result_type value = default_seed);
106 template<class Sseq> void seed(Sseq& q);
107
108 // generating functions
109 result_type operator()();
110 void discard(unsigned long long z);
111};
112
113template <class UIntType, size_t w, size_t n, size_t m, size_t r,
114 UIntType a, size_t u, UIntType d, size_t s,
115 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
116bool
117operator==(
118 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
119 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
120
121template <class UIntType, size_t w, size_t n, size_t m, size_t r,
122 UIntType a, size_t u, UIntType d, size_t s,
123 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
124bool
125operator!=(
126 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
127 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
128
129template <class charT, class traits,
130 class UIntType, size_t w, size_t n, size_t m, size_t r,
131 UIntType a, size_t u, UIntType d, size_t s,
132 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
133basic_ostream<charT, traits>&
134operator<<(basic_ostream<charT, traits>& os,
135 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
136
137template <class charT, class traits,
138 class UIntType, size_t w, size_t n, size_t m, size_t r,
139 UIntType a, size_t u, UIntType d, size_t s,
140 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
141basic_istream<charT, traits>&
142operator>>(basic_istream<charT, traits>& is,
143 mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
144
145template<class UIntType, size_t w, size_t s, size_t r>
146class subtract_with_carry_engine
147{
148public:
149 // types
150 typedef UIntType result_type;
151
152 // engine characteristics
153 static constexpr size_t word_size = w;
154 static constexpr size_t short_lag = s;
155 static constexpr size_t long_lag = r;
156 static constexpr result_type min() { return 0; }
157 static constexpr result_type max() { return m-1; }
158 static constexpr result_type default_seed = 19780503u;
159
160 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100161 explicit subtract_with_carry_engine(result_type value = default_seed); // before C++20
162 subtract_with_carry_engine() : subtract_with_carry_engine(default_seed) {} // C++20
163 explicit subtract_with_carry_engine(result_type value); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164 template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
165 void seed(result_type value = default_seed);
166 template<class Sseq> void seed(Sseq& q);
167
168 // generating functions
169 result_type operator()();
170 void discard(unsigned long long z);
171};
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 UIntType, size_t w, size_t s, size_t r>
180bool
181operator!=(
182 const subtract_with_carry_engine<UIntType, w, s, r>& x,
183 const subtract_with_carry_engine<UIntType, w, s, r>& y);
184
185template <class charT, class traits,
186 class UIntType, size_t w, size_t s, size_t r>
187basic_ostream<charT, traits>&
188operator<<(basic_ostream<charT, traits>& os,
189 const subtract_with_carry_engine<UIntType, w, s, r>& x);
190
191template <class charT, class traits,
192 class UIntType, size_t w, size_t s, size_t r>
193basic_istream<charT, traits>&
194operator>>(basic_istream<charT, traits>& is,
195 subtract_with_carry_engine<UIntType, w, s, r>& x);
196
197template<class Engine, size_t p, size_t r>
198class discard_block_engine
199{
200public:
201 // types
202 typedef typename Engine::result_type result_type;
203
204 // engine characteristics
205 static constexpr size_t block_size = p;
206 static constexpr size_t used_block = r;
207 static constexpr result_type min() { return Engine::min(); }
208 static constexpr result_type max() { return Engine::max(); }
209
210 // constructors and seeding functions
211 discard_block_engine();
212 explicit discard_block_engine(const Engine& e);
213 explicit discard_block_engine(Engine&& e);
214 explicit discard_block_engine(result_type s);
215 template<class Sseq> explicit discard_block_engine(Sseq& q);
216 void seed();
217 void seed(result_type s);
218 template<class Sseq> void seed(Sseq& q);
219
220 // generating functions
221 result_type operator()();
222 void discard(unsigned long long z);
223
224 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000225 const Engine& base() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226};
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 Engine, size_t p, size_t r>
235bool
236operator!=(
237 const discard_block_engine<Engine, p, r>& x,
238 const discard_block_engine<Engine, p, r>& y);
239
240template <class charT, class traits,
241 class Engine, size_t p, size_t r>
242basic_ostream<charT, traits>&
243operator<<(basic_ostream<charT, traits>& os,
244 const discard_block_engine<Engine, p, r>& x);
245
246template <class charT, class traits,
247 class Engine, size_t p, size_t r>
248basic_istream<charT, traits>&
249operator>>(basic_istream<charT, traits>& is,
250 discard_block_engine<Engine, p, r>& x);
251
252template<class Engine, size_t w, class UIntType>
253class independent_bits_engine
254{
255public:
256 // types
257 typedef UIntType result_type;
258
259 // engine characteristics
260 static constexpr result_type min() { return 0; }
261 static constexpr result_type max() { return 2^w - 1; }
262
263 // constructors and seeding functions
264 independent_bits_engine();
265 explicit independent_bits_engine(const Engine& e);
266 explicit independent_bits_engine(Engine&& e);
267 explicit independent_bits_engine(result_type s);
268 template<class Sseq> explicit independent_bits_engine(Sseq& q);
269 void seed();
270 void seed(result_type s);
271 template<class Sseq> void seed(Sseq& q);
272
273 // generating functions
274 result_type operator()(); void discard(unsigned long long z);
275
276 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000277 const Engine& base() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000278};
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 Engine, size_t w, class UIntType>
287bool
288operator!=(
289 const independent_bits_engine<Engine, w, UIntType>& x,
290 const independent_bits_engine<Engine, w, UIntType>& y);
291
292template <class charT, class traits,
293 class Engine, size_t w, class UIntType>
294basic_ostream<charT, traits>&
295operator<<(basic_ostream<charT, traits>& os,
296 const independent_bits_engine<Engine, w, UIntType>& x);
297
298template <class charT, class traits,
299 class Engine, size_t w, class UIntType>
300basic_istream<charT, traits>&
301operator>>(basic_istream<charT, traits>& is,
302 independent_bits_engine<Engine, w, UIntType>& x);
303
304template<class Engine, size_t k>
305class shuffle_order_engine
306{
307public:
308 // types
309 typedef typename Engine::result_type result_type;
310
311 // engine characteristics
312 static constexpr size_t table_size = k;
313 static constexpr result_type min() { return Engine::min; }
314 static constexpr result_type max() { return Engine::max; }
315
316 // constructors and seeding functions
317 shuffle_order_engine();
318 explicit shuffle_order_engine(const Engine& e);
319 explicit shuffle_order_engine(Engine&& e);
320 explicit shuffle_order_engine(result_type s);
321 template<class Sseq> explicit shuffle_order_engine(Sseq& q);
322 void seed();
323 void seed(result_type s);
324 template<class Sseq> void seed(Sseq& q);
325
326 // generating functions
327 result_type operator()();
328 void discard(unsigned long long z);
329
330 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000331 const Engine& base() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332};
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 Engine, size_t k>
341bool
342operator!=(
343 const shuffle_order_engine<Engine, k>& x,
344 const shuffle_order_engine<Engine, k>& y);
345
346template <class charT, class traits,
347 class Engine, size_t k>
348basic_ostream<charT, traits>&
349operator<<(basic_ostream<charT, traits>& os,
350 const shuffle_order_engine<Engine, k>& x);
351
352template <class charT, class traits,
353 class Engine, size_t k>
354basic_istream<charT, traits>&
355operator>>(basic_istream<charT, traits>& is,
356 shuffle_order_engine<Engine, k>& x);
357
358typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
359 minstd_rand0;
360typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
361 minstd_rand;
362typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
363 0x9908b0df,
364 11, 0xffffffff,
365 7, 0x9d2c5680,
366 15, 0xefc60000,
367 18, 1812433253> mt19937;
368typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
369 0xb5026f5aa96619e9,
370 29, 0x5555555555555555,
371 17, 0x71d67fffeda60000,
372 37, 0xfff7eee000000000,
373 43, 6364136223846793005> mt19937_64;
374typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
375typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
376typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
377typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
378typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
Howard Hinnant481ba382010-05-20 15:11:46 +0000379typedef minstd_rand default_random_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000380
381// Generators
382
383class random_device
384{
385public:
386 // types
387 typedef unsigned int result_type;
388
389 // generator characteristics
390 static constexpr result_type min() { return numeric_limits<result_type>::min(); }
391 static constexpr result_type max() { return numeric_limits<result_type>::max(); }
392
393 // constructors
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100394 explicit random_device(const string& token = implementation-defined); // before C++20
395 random_device() : random_device(implementation-defined) {} // C++20
396 explicit random_device(const string& token); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397
398 // generating functions
399 result_type operator()();
400
401 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000402 double entropy() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404 // no copy functions
405 random_device(const random_device& ) = delete;
406 void operator=(const random_device& ) = delete;
407};
408
409// Utilities
410
411class seed_seq
412{
413public:
414 // types
415 typedef uint_least32_t result_type;
416
417 // constructors
418 seed_seq();
419 template<class T>
420 seed_seq(initializer_list<T> il);
421 template<class InputIterator>
422 seed_seq(InputIterator begin, InputIterator end);
423
424 // generating functions
425 template<class RandomAccessIterator>
426 void generate(RandomAccessIterator begin, RandomAccessIterator end);
427
428 // property functions
429 size_t size() const;
430 template<class OutputIterator>
431 void param(OutputIterator dest) const;
432
433 // no copy functions
434 seed_seq(const seed_seq&) = delete;
435 void operator=(const seed_seq& ) = delete;
436};
437
438template<class RealType, size_t bits, class URNG>
439 RealType generate_canonical(URNG& g);
440
441// Distributions
442
443template<class IntType = int>
444class uniform_int_distribution
445{
446public:
447 // types
448 typedef IntType result_type;
449
450 class param_type
451 {
452 public:
453 typedef uniform_int_distribution distribution_type;
454
455 explicit param_type(IntType a = 0,
456 IntType b = numeric_limits<IntType>::max());
457
458 result_type a() const;
459 result_type b() const;
460
461 friend bool operator==(const param_type& x, const param_type& y);
462 friend bool operator!=(const param_type& x, const param_type& y);
463 };
464
465 // constructors and reset functions
466 explicit uniform_int_distribution(IntType a = 0,
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100467 IntType b = numeric_limits<IntType>::max()); // before C++20
468 uniform_int_distribution() : uniform_int_distribution(0) {} // C++20
469 explicit uniform_int_distribution(IntType a,
470 IntType b = numeric_limits<IntType>::max()); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471 explicit uniform_int_distribution(const param_type& parm);
472 void reset();
473
474 // generating functions
475 template<class URNG> result_type operator()(URNG& g);
476 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
477
478 // property functions
479 result_type a() const;
480 result_type b() const;
481
482 param_type param() const;
483 void param(const param_type& parm);
484
485 result_type min() const;
486 result_type max() const;
487
488 friend bool operator==(const uniform_int_distribution& x,
489 const uniform_int_distribution& y);
490 friend bool operator!=(const uniform_int_distribution& x,
491 const uniform_int_distribution& y);
492
493 template <class charT, class traits>
494 friend
495 basic_ostream<charT, traits>&
496 operator<<(basic_ostream<charT, traits>& os,
497 const uniform_int_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000498
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499 template <class charT, class traits>
500 friend
501 basic_istream<charT, traits>&
502 operator>>(basic_istream<charT, traits>& is,
503 uniform_int_distribution& x);
504};
505
506template<class RealType = double>
507class uniform_real_distribution
508{
509public:
510 // types
511 typedef RealType result_type;
512
513 class param_type
514 {
515 public:
516 typedef uniform_real_distribution distribution_type;
517
518 explicit param_type(RealType a = 0,
519 RealType b = 1);
520
521 result_type a() const;
522 result_type b() const;
523
524 friend bool operator==(const param_type& x, const param_type& y);
525 friend bool operator!=(const param_type& x, const param_type& y);
526 };
527
528 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100529 explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0); // before C++20
530 uniform_real_distribution() : uniform_real_distribution(0.0) {} // C++20
531 explicit uniform_real_distribution(RealType a, RealType b = 1.0); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532 explicit uniform_real_distribution(const param_type& parm);
533 void reset();
534
535 // generating functions
536 template<class URNG> result_type operator()(URNG& g);
537 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
538
539 // property functions
540 result_type a() const;
541 result_type b() const;
542
543 param_type param() const;
544 void param(const param_type& parm);
545
546 result_type min() const;
547 result_type max() const;
548
549 friend bool operator==(const uniform_real_distribution& x,
550 const uniform_real_distribution& y);
551 friend bool operator!=(const uniform_real_distribution& x,
552 const uniform_real_distribution& y);
553
554 template <class charT, class traits>
555 friend
556 basic_ostream<charT, traits>&
557 operator<<(basic_ostream<charT, traits>& os,
558 const uniform_real_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000559
Howard Hinnantc51e1022010-05-11 19:42:16 +0000560 template <class charT, class traits>
561 friend
562 basic_istream<charT, traits>&
563 operator>>(basic_istream<charT, traits>& is,
564 uniform_real_distribution& x);
565};
566
567class bernoulli_distribution
568{
569public:
570 // types
571 typedef bool result_type;
572
573 class param_type
574 {
575 public:
576 typedef bernoulli_distribution distribution_type;
577
578 explicit param_type(double p = 0.5);
579
580 double p() const;
581
582 friend bool operator==(const param_type& x, const param_type& y);
583 friend bool operator!=(const param_type& x, const param_type& y);
584 };
585
586 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100587 explicit bernoulli_distribution(double p = 0.5); // before C++20
588 bernoulli_distribution() : bernoulli_distribution(0.5) {} // C++20
589 explicit bernoulli_distribution(double p); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590 explicit bernoulli_distribution(const param_type& parm);
591 void reset();
592
593 // generating functions
594 template<class URNG> result_type operator()(URNG& g);
595 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
596
597 // property functions
598 double p() const;
599
600 param_type param() const;
601 void param(const param_type& parm);
602
603 result_type min() const;
604 result_type max() const;
605
606 friend bool operator==(const bernoulli_distribution& x,
607 const bernoulli_distribution& y);
608 friend bool operator!=(const bernoulli_distribution& x,
609 const bernoulli_distribution& y);
610
611 template <class charT, class traits>
612 friend
613 basic_ostream<charT, traits>&
614 operator<<(basic_ostream<charT, traits>& os,
615 const bernoulli_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000616
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617 template <class charT, class traits>
618 friend
619 basic_istream<charT, traits>&
620 operator>>(basic_istream<charT, traits>& is,
621 bernoulli_distribution& x);
622};
623
624template<class IntType = int>
Howard Hinnant53f28fa2010-05-11 23:26:59 +0000625class binomial_distribution
626{
627public:
628 // types
629 typedef IntType result_type;
630
631 class param_type
632 {
633 public:
634 typedef binomial_distribution distribution_type;
635
636 explicit param_type(IntType t = 1, double p = 0.5);
637
638 IntType t() const;
639 double p() const;
640
641 friend bool operator==(const param_type& x, const param_type& y);
642 friend bool operator!=(const param_type& x, const param_type& y);
643 };
644
645 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100646 explicit binomial_distribution(IntType t = 1, double p = 0.5); // before C++20
647 binomial_distribution() : binomial_distribution(1) {} // C++20
648 explicit binomial_distribution(IntType t, double p = 0.5); // C++20
Howard Hinnant53f28fa2010-05-11 23:26:59 +0000649 explicit binomial_distribution(const param_type& parm);
650 void reset();
651
652 // generating functions
653 template<class URNG> result_type operator()(URNG& g);
654 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
655
656 // property functions
657 IntType t() const;
658 double p() const;
659
660 param_type param() const;
661 void param(const param_type& parm);
662
663 result_type min() const;
664 result_type max() const;
665
666 friend bool operator==(const binomial_distribution& x,
667 const binomial_distribution& y);
668 friend bool operator!=(const binomial_distribution& x,
669 const binomial_distribution& y);
670
671 template <class charT, class traits>
672 friend
673 basic_ostream<charT, traits>&
674 operator<<(basic_ostream<charT, traits>& os,
675 const binomial_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000676
Howard Hinnant53f28fa2010-05-11 23:26:59 +0000677 template <class charT, class traits>
678 friend
679 basic_istream<charT, traits>&
680 operator>>(basic_istream<charT, traits>& is,
681 binomial_distribution& x);
682};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000683
684template<class IntType = int>
Howard Hinnant28beb162010-05-17 13:44:27 +0000685class geometric_distribution
686{
687public:
688 // types
689 typedef IntType result_type;
690
691 class param_type
692 {
693 public:
694 typedef geometric_distribution distribution_type;
695
696 explicit param_type(double p = 0.5);
697
698 double p() const;
699
700 friend bool operator==(const param_type& x, const param_type& y);
701 friend bool operator!=(const param_type& x, const param_type& y);
702 };
703
704 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100705 explicit geometric_distribution(double p = 0.5); // before C++20
706 geometric_distribution() : geometric_distribution(0.5) {} // C++20
707 explicit geometric_distribution(double p); // C++20
Howard Hinnant28beb162010-05-17 13:44:27 +0000708 explicit geometric_distribution(const param_type& parm);
709 void reset();
710
711 // generating functions
712 template<class URNG> result_type operator()(URNG& g);
713 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
714
715 // property functions
716 double p() const;
717
718 param_type param() const;
719 void param(const param_type& parm);
720
721 result_type min() const;
722 result_type max() const;
723
724 friend bool operator==(const geometric_distribution& x,
725 const geometric_distribution& y);
726 friend bool operator!=(const geometric_distribution& x,
727 const geometric_distribution& y);
728
729 template <class charT, class traits>
730 friend
731 basic_ostream<charT, traits>&
732 operator<<(basic_ostream<charT, traits>& os,
733 const geometric_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000734
Howard Hinnant28beb162010-05-17 13:44:27 +0000735 template <class charT, class traits>
736 friend
737 basic_istream<charT, traits>&
738 operator>>(basic_istream<charT, traits>& is,
739 geometric_distribution& x);
740};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741
742template<class IntType = int>
Howard Hinnant0f43ce62010-05-17 00:09:38 +0000743class negative_binomial_distribution
744{
745public:
746 // types
747 typedef IntType result_type;
748
749 class param_type
750 {
751 public:
752 typedef negative_binomial_distribution distribution_type;
753
754 explicit param_type(result_type k = 1, double p = 0.5);
755
756 result_type k() const;
757 double p() const;
758
759 friend bool operator==(const param_type& x, const param_type& y);
760 friend bool operator!=(const param_type& x, const param_type& y);
761 };
762
763 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100764 explicit negative_binomial_distribution(IntType k = 1, double p = 0.5); // before C++20
765 negative_binomial_distribution() : negative_binomial_distribution(1) {} // C++20
766 explicit negative_binomial_distribution(IntType k, double p = 0.5); // C++20
Howard Hinnant0f43ce62010-05-17 00:09:38 +0000767 explicit negative_binomial_distribution(const param_type& parm);
768 void reset();
769
770 // generating functions
771 template<class URNG> result_type operator()(URNG& g);
772 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
773
774 // property functions
775 result_type k() const;
776 double p() const;
777
778 param_type param() const;
779 void param(const param_type& parm);
780
781 result_type min() const;
782 result_type max() const;
783
784 friend bool operator==(const negative_binomial_distribution& x,
785 const negative_binomial_distribution& y);
786 friend bool operator!=(const negative_binomial_distribution& x,
787 const negative_binomial_distribution& y);
788
789 template <class charT, class traits>
790 friend
791 basic_ostream<charT, traits>&
792 operator<<(basic_ostream<charT, traits>& os,
793 const negative_binomial_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000794
Howard Hinnant0f43ce62010-05-17 00:09:38 +0000795 template <class charT, class traits>
796 friend
797 basic_istream<charT, traits>&
798 operator>>(basic_istream<charT, traits>& is,
799 negative_binomial_distribution& x);
800};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000801
802template<class IntType = int>
Howard Hinnant24a5f2a2010-05-14 21:38:54 +0000803class poisson_distribution
804{
805public:
806 // types
807 typedef IntType result_type;
808
809 class param_type
810 {
811 public:
812 typedef poisson_distribution distribution_type;
813
814 explicit param_type(double mean = 1.0);
815
816 double mean() const;
817
818 friend bool operator==(const param_type& x, const param_type& y);
819 friend bool operator!=(const param_type& x, const param_type& y);
820 };
821
822 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100823 explicit poisson_distribution(double mean = 1.0); // before C++20
824 poisson_distribution() : poisson_distribution(1.0) {} // C++20
825 explicit poisson_distribution(double mean); // C++20
Howard Hinnant24a5f2a2010-05-14 21:38:54 +0000826 explicit poisson_distribution(const param_type& parm);
827 void reset();
828
829 // generating functions
830 template<class URNG> result_type operator()(URNG& g);
831 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
832
833 // property functions
834 double mean() const;
835
836 param_type param() const;
837 void param(const param_type& parm);
838
839 result_type min() const;
840 result_type max() const;
841
842 friend bool operator==(const poisson_distribution& x,
843 const poisson_distribution& y);
844 friend bool operator!=(const poisson_distribution& x,
845 const poisson_distribution& y);
846
847 template <class charT, class traits>
848 friend
849 basic_ostream<charT, traits>&
850 operator<<(basic_ostream<charT, traits>& os,
851 const poisson_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000852
Howard Hinnant24a5f2a2010-05-14 21:38:54 +0000853 template <class charT, class traits>
854 friend
855 basic_istream<charT, traits>&
856 operator>>(basic_istream<charT, traits>& is,
857 poisson_distribution& x);
858};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859
860template<class RealType = double>
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000861class exponential_distribution
862{
863public:
864 // types
865 typedef RealType result_type;
866
867 class param_type
868 {
869 public:
870 typedef exponential_distribution distribution_type;
871
Howard Hinnant0b95bc92010-05-12 21:02:31 +0000872 explicit param_type(result_type lambda = 1.0);
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000873
Howard Hinnant0b95bc92010-05-12 21:02:31 +0000874 result_type lambda() const;
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000875
876 friend bool operator==(const param_type& x, const param_type& y);
877 friend bool operator!=(const param_type& x, const param_type& y);
878 };
879
880 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100881 explicit exponential_distribution(RealType lambda = 1.0); // before C++20
882 exponential_distribution() : exponential_distribution(1.0) {} // C++20
883 explicit exponential_distribution(RealType lambda); // C++20
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000884 explicit exponential_distribution(const param_type& parm);
885 void reset();
886
887 // generating functions
888 template<class URNG> result_type operator()(URNG& g);
889 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
890
891 // property functions
Howard Hinnant0b95bc92010-05-12 21:02:31 +0000892 result_type lambda() const;
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000893
894 param_type param() const;
895 void param(const param_type& parm);
896
897 result_type min() const;
898 result_type max() const;
899
900 friend bool operator==(const exponential_distribution& x,
901 const exponential_distribution& y);
902 friend bool operator!=(const exponential_distribution& x,
903 const exponential_distribution& y);
904
905 template <class charT, class traits>
906 friend
907 basic_ostream<charT, traits>&
908 operator<<(basic_ostream<charT, traits>& os,
909 const exponential_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000910
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000911 template <class charT, class traits>
912 friend
913 basic_istream<charT, traits>&
914 operator>>(basic_istream<charT, traits>& is,
915 exponential_distribution& x);
916};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000917
918template<class RealType = double>
Howard Hinnantbb6d2022010-05-13 17:58:28 +0000919class gamma_distribution
920{
921public:
922 // types
923 typedef RealType result_type;
924
925 class param_type
926 {
927 public:
928 typedef gamma_distribution distribution_type;
929
930 explicit param_type(result_type alpha = 1, result_type beta = 1);
931
932 result_type alpha() const;
933 result_type beta() const;
934
935 friend bool operator==(const param_type& x, const param_type& y);
936 friend bool operator!=(const param_type& x, const param_type& y);
937 };
938
939 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100940 explicit gamma_distribution(RealType alpha = 0.0, RealType beta = 1.0); // before C++20
941 gamma_distribution() : gamma_distribution(0.0) {} // C++20
942 explicit gamma_distribution(RealType alpha, RealType beta = 1.0); // C++20
Howard Hinnantbb6d2022010-05-13 17:58:28 +0000943 explicit gamma_distribution(const param_type& parm);
944 void reset();
945
946 // generating functions
947 template<class URNG> result_type operator()(URNG& g);
948 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
949
950 // property functions
951 result_type alpha() const;
952 result_type beta() const;
953
954 param_type param() const;
955 void param(const param_type& parm);
956
957 result_type min() const;
958 result_type max() const;
959
960 friend bool operator==(const gamma_distribution& x,
961 const gamma_distribution& y);
962 friend bool operator!=(const gamma_distribution& x,
963 const gamma_distribution& y);
964
965 template <class charT, class traits>
966 friend
967 basic_ostream<charT, traits>&
968 operator<<(basic_ostream<charT, traits>& os,
969 const gamma_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000970
Howard Hinnantbb6d2022010-05-13 17:58:28 +0000971 template <class charT, class traits>
972 friend
973 basic_istream<charT, traits>&
974 operator>>(basic_istream<charT, traits>& is,
975 gamma_distribution& x);
976};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977
978template<class RealType = double>
Howard Hinnantdb6b97b2010-05-16 01:09:02 +0000979class weibull_distribution
980{
981public:
982 // types
983 typedef RealType result_type;
984
985 class param_type
986 {
987 public:
988 typedef weibull_distribution distribution_type;
989
990 explicit param_type(result_type alpha = 1, result_type beta = 1);
991
992 result_type a() const;
993 result_type b() const;
994
995 friend bool operator==(const param_type& x, const param_type& y);
996 friend bool operator!=(const param_type& x, const param_type& y);
997 };
998
999 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001000 explicit weibull_distribution(RealType a = 1.0, RealType b = 1.0); // before C++20
1001 weibull_distribution() : weibull_distribution(1.0) {} // C++20
1002 explicit weibull_distribution(RealType a, RealType b = 1.0); // C++20
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00001003 explicit weibull_distribution(const param_type& parm);
1004 void reset();
1005
1006 // generating functions
1007 template<class URNG> result_type operator()(URNG& g);
1008 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1009
1010 // property functions
1011 result_type a() const;
1012 result_type b() const;
1013
1014 param_type param() const;
1015 void param(const param_type& parm);
1016
1017 result_type min() const;
1018 result_type max() const;
1019
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00001020 friend bool operator==(const weibull_distribution& x,
1021 const weibull_distribution& y);
1022 friend bool operator!=(const weibull_distribution& x,
1023 const weibull_distribution& y);
1024
1025 template <class charT, class traits>
1026 friend
1027 basic_ostream<charT, traits>&
1028 operator<<(basic_ostream<charT, traits>& os,
1029 const weibull_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001030
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00001031 template <class charT, class traits>
1032 friend
1033 basic_istream<charT, traits>&
1034 operator>>(basic_istream<charT, traits>& is,
1035 weibull_distribution& x);
1036};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037
1038template<class RealType = double>
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00001039class extreme_value_distribution
1040{
1041public:
1042 // types
1043 typedef RealType result_type;
1044
1045 class param_type
1046 {
1047 public:
1048 typedef extreme_value_distribution distribution_type;
1049
1050 explicit param_type(result_type a = 0, result_type b = 1);
1051
1052 result_type a() const;
1053 result_type b() const;
1054
1055 friend bool operator==(const param_type& x, const param_type& y);
1056 friend bool operator!=(const param_type& x, const param_type& y);
1057 };
1058
1059 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001060 explicit extreme_value_distribution(RealType a = 0.0, RealType b = 1.0); // before C++20
1061 extreme_value_distribution() : extreme_value_distribution(0.0) {} // C++20
1062 explicit extreme_value_distribution(RealType a, RealType b = 1.0); // C++20
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00001063 explicit extreme_value_distribution(const param_type& parm);
1064 void reset();
1065
1066 // generating functions
1067 template<class URNG> result_type operator()(URNG& g);
1068 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1069
1070 // property functions
1071 result_type a() const;
1072 result_type b() const;
1073
1074 param_type param() const;
1075 void param(const param_type& parm);
1076
1077 result_type min() const;
1078 result_type max() const;
1079
1080 friend bool operator==(const extreme_value_distribution& x,
1081 const extreme_value_distribution& y);
1082 friend bool operator!=(const extreme_value_distribution& x,
1083 const extreme_value_distribution& y);
1084
1085 template <class charT, class traits>
1086 friend
1087 basic_ostream<charT, traits>&
1088 operator<<(basic_ostream<charT, traits>& os,
1089 const extreme_value_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001090
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00001091 template <class charT, class traits>
1092 friend
1093 basic_istream<charT, traits>&
1094 operator>>(basic_istream<charT, traits>& is,
1095 extreme_value_distribution& x);
1096};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097
1098template<class RealType = double>
Howard Hinnant0b95bc92010-05-12 21:02:31 +00001099class normal_distribution
1100{
1101public:
1102 // types
1103 typedef RealType result_type;
1104
1105 class param_type
1106 {
1107 public:
1108 typedef normal_distribution distribution_type;
1109
1110 explicit param_type(result_type mean = 0, result_type stddev = 1);
1111
1112 result_type mean() const;
1113 result_type stddev() const;
1114
1115 friend bool operator==(const param_type& x, const param_type& y);
1116 friend bool operator!=(const param_type& x, const param_type& y);
1117 };
1118
1119 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001120 explicit normal_distribution(RealType mean = 0.0, RealType stddev = 1.0); // before C++20
1121 normal_distribution() : normal_distribution(0.0) {} // C++20
1122 explicit normal_distribution(RealType mean, RealType stddev = 1.0); // C++20
Howard Hinnant0b95bc92010-05-12 21:02:31 +00001123 explicit normal_distribution(const param_type& parm);
1124 void reset();
1125
1126 // generating functions
1127 template<class URNG> result_type operator()(URNG& g);
1128 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1129
1130 // property functions
1131 result_type mean() const;
1132 result_type stddev() const;
1133
1134 param_type param() const;
1135 void param(const param_type& parm);
1136
1137 result_type min() const;
1138 result_type max() const;
1139
1140 friend bool operator==(const normal_distribution& x,
1141 const normal_distribution& y);
1142 friend bool operator!=(const normal_distribution& x,
1143 const normal_distribution& y);
1144
1145 template <class charT, class traits>
1146 friend
1147 basic_ostream<charT, traits>&
1148 operator<<(basic_ostream<charT, traits>& os,
1149 const normal_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001150
Howard Hinnant0b95bc92010-05-12 21:02:31 +00001151 template <class charT, class traits>
1152 friend
1153 basic_istream<charT, traits>&
1154 operator>>(basic_istream<charT, traits>& is,
1155 normal_distribution& x);
1156};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157
1158template<class RealType = double>
Howard Hinnantdf3bed62010-05-17 18:31:53 +00001159class lognormal_distribution
1160{
1161public:
1162 // types
1163 typedef RealType result_type;
1164
1165 class param_type
1166 {
1167 public:
1168 typedef lognormal_distribution distribution_type;
1169
1170 explicit param_type(result_type m = 0, result_type s = 1);
1171
1172 result_type m() const;
1173 result_type s() const;
1174
1175 friend bool operator==(const param_type& x, const param_type& y);
1176 friend bool operator!=(const param_type& x, const param_type& y);
1177 };
1178
1179 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001180 explicit lognormal_distribution(RealType mean = 0.0, RealType stddev = 1.0); // before C++20
1181 lognormal_distribution() : lognormal_distribution(0.0) {} // C++20
1182 explicit lognormal_distribution(RealType mean, RealType stddev = 1.0); // C++20
Howard Hinnantdf3bed62010-05-17 18:31:53 +00001183 explicit lognormal_distribution(const param_type& parm);
1184 void reset();
1185
1186 // generating functions
1187 template<class URNG> result_type operator()(URNG& g);
1188 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1189
1190 // property functions
1191 result_type m() const;
1192 result_type s() const;
1193
1194 param_type param() const;
1195 void param(const param_type& parm);
1196
1197 result_type min() const;
1198 result_type max() const;
1199
1200 friend bool operator==(const lognormal_distribution& x,
1201 const lognormal_distribution& y);
1202 friend bool operator!=(const lognormal_distribution& x,
1203 const lognormal_distribution& y);
1204
1205 template <class charT, class traits>
1206 friend
1207 basic_ostream<charT, traits>&
1208 operator<<(basic_ostream<charT, traits>& os,
1209 const lognormal_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001210
Howard Hinnantdf3bed62010-05-17 18:31:53 +00001211 template <class charT, class traits>
1212 friend
1213 basic_istream<charT, traits>&
1214 operator>>(basic_istream<charT, traits>& is,
1215 lognormal_distribution& x);
1216};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217
1218template<class RealType = double>
Howard Hinnant252ebf02010-05-15 23:36:00 +00001219class chi_squared_distribution
1220{
1221public:
1222 // types
1223 typedef RealType result_type;
1224
1225 class param_type
1226 {
1227 public:
1228 typedef chi_squared_distribution distribution_type;
1229
1230 explicit param_type(result_type n = 1);
1231
1232 result_type n() const;
1233
1234 friend bool operator==(const param_type& x, const param_type& y);
1235 friend bool operator!=(const param_type& x, const param_type& y);
1236 };
1237
1238 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001239 explicit chi_squared_distribution(RealType n = 1.0); // before C++20
1240 chi_squared_distribution() : chi_squared_distribution(1.0) {} // C++20
1241 explicit chi_squared_distribution(RealType n); // C++20
Howard Hinnant252ebf02010-05-15 23:36:00 +00001242 explicit chi_squared_distribution(const param_type& parm);
1243 void reset();
1244
1245 // generating functions
1246 template<class URNG> result_type operator()(URNG& g);
1247 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1248
1249 // property functions
1250 result_type n() const;
1251
1252 param_type param() const;
1253 void param(const param_type& parm);
1254
1255 result_type min() const;
1256 result_type max() const;
1257
Howard Hinnant252ebf02010-05-15 23:36:00 +00001258 friend bool operator==(const chi_squared_distribution& x,
1259 const chi_squared_distribution& y);
1260 friend bool operator!=(const chi_squared_distribution& x,
1261 const chi_squared_distribution& y);
1262
1263 template <class charT, class traits>
1264 friend
1265 basic_ostream<charT, traits>&
1266 operator<<(basic_ostream<charT, traits>& os,
1267 const chi_squared_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001268
Howard Hinnant252ebf02010-05-15 23:36:00 +00001269 template <class charT, class traits>
1270 friend
1271 basic_istream<charT, traits>&
1272 operator>>(basic_istream<charT, traits>& is,
1273 chi_squared_distribution& x);
1274};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
1276template<class RealType = double>
Howard Hinnantf3292562010-05-17 21:55:46 +00001277class cauchy_distribution
1278{
1279public:
1280 // types
1281 typedef RealType result_type;
1282
1283 class param_type
1284 {
1285 public:
1286 typedef cauchy_distribution distribution_type;
1287
1288 explicit param_type(result_type a = 0, result_type b = 1);
1289
1290 result_type a() const;
1291 result_type b() const;
1292
1293 friend bool operator==(const param_type& x, const param_type& y);
1294 friend bool operator!=(const param_type& x, const param_type& y);
1295 };
1296
1297 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001298 explicit cauchy_distribution(RealType a = 0.0, RealType b = 1.0); // before C++20
1299 cauchy_distribution() : cauchy_distribution(0.0) {} // C++20
1300 explicit cauchy_distribution(RealType a, RealType b = 1.0); // C++20
Howard Hinnantf3292562010-05-17 21:55:46 +00001301 explicit cauchy_distribution(const param_type& parm);
1302 void reset();
1303
1304 // generating functions
1305 template<class URNG> result_type operator()(URNG& g);
1306 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1307
1308 // property functions
1309 result_type a() const;
1310 result_type b() const;
1311
1312 param_type param() const;
1313 void param(const param_type& parm);
1314
1315 result_type min() const;
1316 result_type max() const;
1317
1318 friend bool operator==(const cauchy_distribution& x,
1319 const cauchy_distribution& y);
1320 friend bool operator!=(const cauchy_distribution& x,
1321 const cauchy_distribution& y);
1322
1323 template <class charT, class traits>
1324 friend
1325 basic_ostream<charT, traits>&
1326 operator<<(basic_ostream<charT, traits>& os,
1327 const cauchy_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001328
Howard Hinnantf3292562010-05-17 21:55:46 +00001329 template <class charT, class traits>
1330 friend
1331 basic_istream<charT, traits>&
1332 operator>>(basic_istream<charT, traits>& is,
1333 cauchy_distribution& x);
1334};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335
1336template<class RealType = double>
Howard Hinnantf39463b2010-05-18 17:32:30 +00001337class fisher_f_distribution
1338{
1339public:
1340 // types
1341 typedef RealType result_type;
1342
1343 class param_type
1344 {
1345 public:
Howard Hinnant20c50882010-05-18 20:08:04 +00001346 typedef fisher_f_distribution distribution_type;
Howard Hinnantf39463b2010-05-18 17:32:30 +00001347
1348 explicit param_type(result_type m = 1, result_type n = 1);
1349
1350 result_type m() const;
1351 result_type n() const;
1352
1353 friend bool operator==(const param_type& x, const param_type& y);
1354 friend bool operator!=(const param_type& x, const param_type& y);
1355 };
1356
1357 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001358 explicit fisher_f_distribution(RealType m = 1.0, RealType n = 1.0); // before C++20
1359 fisher_f_distribution() : fisher_f_distribution(1.0) {} // C++20
1360 explicit fisher_f_distribution(RealType m, RealType n = 1.0); // C++20
Howard Hinnantf39463b2010-05-18 17:32:30 +00001361 explicit fisher_f_distribution(const param_type& parm);
1362 void reset();
1363
1364 // generating functions
1365 template<class URNG> result_type operator()(URNG& g);
1366 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1367
1368 // property functions
1369 result_type m() const;
1370 result_type n() const;
1371
1372 param_type param() const;
1373 void param(const param_type& parm);
1374
1375 result_type min() const;
1376 result_type max() const;
1377
1378 friend bool operator==(const fisher_f_distribution& x,
1379 const fisher_f_distribution& y);
1380 friend bool operator!=(const fisher_f_distribution& x,
1381 const fisher_f_distribution& y);
1382
1383 template <class charT, class traits>
1384 friend
1385 basic_ostream<charT, traits>&
1386 operator<<(basic_ostream<charT, traits>& os,
1387 const fisher_f_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001388
Howard Hinnantf39463b2010-05-18 17:32:30 +00001389 template <class charT, class traits>
1390 friend
1391 basic_istream<charT, traits>&
1392 operator>>(basic_istream<charT, traits>& is,
1393 fisher_f_distribution& x);
1394};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395
1396template<class RealType = double>
Howard Hinnant20c50882010-05-18 20:08:04 +00001397class student_t_distribution
1398{
1399public:
1400 // types
1401 typedef RealType result_type;
1402
1403 class param_type
1404 {
1405 public:
1406 typedef student_t_distribution distribution_type;
1407
1408 explicit param_type(result_type n = 1);
1409
1410 result_type n() const;
1411
1412 friend bool operator==(const param_type& x, const param_type& y);
1413 friend bool operator!=(const param_type& x, const param_type& y);
1414 };
1415
1416 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001417 explicit student_t_distribution(RealType n = 1.0); // before C++20
1418 student_t_distribution() : student_t_distribution(1.0) {} // C++20
1419 explicit student_t_distribution(RealType n); // C++20
Howard Hinnant20c50882010-05-18 20:08:04 +00001420 explicit student_t_distribution(const param_type& parm);
1421 void reset();
1422
1423 // generating functions
1424 template<class URNG> result_type operator()(URNG& g);
1425 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1426
1427 // property functions
1428 result_type n() const;
1429
1430 param_type param() const;
1431 void param(const param_type& parm);
1432
1433 result_type min() const;
1434 result_type max() const;
1435
1436 friend bool operator==(const student_t_distribution& x,
1437 const student_t_distribution& y);
1438 friend bool operator!=(const student_t_distribution& x,
1439 const student_t_distribution& y);
1440
1441 template <class charT, class traits>
1442 friend
1443 basic_ostream<charT, traits>&
1444 operator<<(basic_ostream<charT, traits>& os,
1445 const student_t_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001446
Howard Hinnant20c50882010-05-18 20:08:04 +00001447 template <class charT, class traits>
1448 friend
1449 basic_istream<charT, traits>&
1450 operator>>(basic_istream<charT, traits>& is,
1451 student_t_distribution& x);
1452};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453
1454template<class IntType = int>
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00001455class discrete_distribution
1456{
1457public:
1458 // types
1459 typedef IntType result_type;
1460
1461 class param_type
1462 {
1463 public:
1464 typedef discrete_distribution distribution_type;
1465
1466 param_type();
1467 template<class InputIterator>
1468 param_type(InputIterator firstW, InputIterator lastW);
1469 param_type(initializer_list<double> wl);
1470 template<class UnaryOperation>
1471 param_type(size_t nw, double xmin, double xmax, UnaryOperation fw);
1472
1473 vector<double> probabilities() const;
1474
1475 friend bool operator==(const param_type& x, const param_type& y);
1476 friend bool operator!=(const param_type& x, const param_type& y);
1477 };
1478
1479 // constructor and reset functions
1480 discrete_distribution();
1481 template<class InputIterator>
1482 discrete_distribution(InputIterator firstW, InputIterator lastW);
1483 discrete_distribution(initializer_list<double> wl);
1484 template<class UnaryOperation>
1485 discrete_distribution(size_t nw, double xmin, double xmax,
1486 UnaryOperation fw);
1487 explicit discrete_distribution(const param_type& parm);
1488 void reset();
1489
1490 // generating functions
1491 template<class URNG> result_type operator()(URNG& g);
1492 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1493
1494 // property functions
1495 vector<double> probabilities() const;
1496
1497 param_type param() const;
1498 void param(const param_type& parm);
1499
1500 result_type min() const;
1501 result_type max() const;
1502
1503 friend bool operator==(const discrete_distribution& x,
1504 const discrete_distribution& y);
1505 friend bool operator!=(const discrete_distribution& x,
1506 const discrete_distribution& y);
1507
1508 template <class charT, class traits>
1509 friend
1510 basic_ostream<charT, traits>&
1511 operator<<(basic_ostream<charT, traits>& os,
1512 const discrete_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001513
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00001514 template <class charT, class traits>
1515 friend
1516 basic_istream<charT, traits>&
1517 operator>>(basic_istream<charT, traits>& is,
1518 discrete_distribution& x);
1519};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520
1521template<class RealType = double>
Howard Hinnant481ba382010-05-20 15:11:46 +00001522class piecewise_constant_distribution
1523{
1524 // types
1525 typedef RealType result_type;
1526
1527 class param_type
1528 {
1529 public:
1530 typedef piecewise_constant_distribution distribution_type;
1531
1532 param_type();
1533 template<class InputIteratorB, class InputIteratorW>
1534 param_type(InputIteratorB firstB, InputIteratorB lastB,
1535 InputIteratorW firstW);
1536 template<class UnaryOperation>
1537 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1538 template<class UnaryOperation>
1539 param_type(size_t nw, result_type xmin, result_type xmax,
1540 UnaryOperation fw);
1541
1542 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001543 vector<result_type> densities() const;
Howard Hinnant481ba382010-05-20 15:11:46 +00001544
1545 friend bool operator==(const param_type& x, const param_type& y);
1546 friend bool operator!=(const param_type& x, const param_type& y);
1547 };
1548
1549 // constructor and reset functions
1550 piecewise_constant_distribution();
1551 template<class InputIteratorB, class InputIteratorW>
1552 piecewise_constant_distribution(InputIteratorB firstB,
1553 InputIteratorB lastB,
1554 InputIteratorW firstW);
1555 template<class UnaryOperation>
1556 piecewise_constant_distribution(initializer_list<result_type> bl,
1557 UnaryOperation fw);
1558 template<class UnaryOperation>
1559 piecewise_constant_distribution(size_t nw, result_type xmin,
1560 result_type xmax, UnaryOperation fw);
1561 explicit piecewise_constant_distribution(const param_type& parm);
1562 void reset();
1563
1564 // generating functions
1565 template<class URNG> result_type operator()(URNG& g);
1566 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1567
1568 // property functions
1569 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001570 vector<result_type> densities() const;
Howard Hinnant481ba382010-05-20 15:11:46 +00001571
1572 param_type param() const;
1573 void param(const param_type& parm);
1574
1575 result_type min() const;
1576 result_type max() const;
1577
1578 friend bool operator==(const piecewise_constant_distribution& x,
1579 const piecewise_constant_distribution& y);
1580 friend bool operator!=(const piecewise_constant_distribution& x,
1581 const piecewise_constant_distribution& y);
1582
1583 template <class charT, class traits>
1584 friend
1585 basic_ostream<charT, traits>&
1586 operator<<(basic_ostream<charT, traits>& os,
1587 const piecewise_constant_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001588
Howard Hinnant481ba382010-05-20 15:11:46 +00001589 template <class charT, class traits>
1590 friend
1591 basic_istream<charT, traits>&
1592 operator>>(basic_istream<charT, traits>& is,
1593 piecewise_constant_distribution& x);
1594};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595
1596template<class RealType = double>
Howard Hinnanta001ef32010-05-25 00:27:34 +00001597class piecewise_linear_distribution
1598{
1599 // types
1600 typedef RealType result_type;
1601
1602 class param_type
1603 {
1604 public:
1605 typedef piecewise_linear_distribution distribution_type;
1606
1607 param_type();
1608 template<class InputIteratorB, class InputIteratorW>
1609 param_type(InputIteratorB firstB, InputIteratorB lastB,
1610 InputIteratorW firstW);
1611 template<class UnaryOperation>
1612 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1613 template<class UnaryOperation>
1614 param_type(size_t nw, result_type xmin, result_type xmax,
1615 UnaryOperation fw);
1616
1617 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001618 vector<result_type> densities() const;
Howard Hinnanta001ef32010-05-25 00:27:34 +00001619
1620 friend bool operator==(const param_type& x, const param_type& y);
1621 friend bool operator!=(const param_type& x, const param_type& y);
1622 };
1623
1624 // constructor and reset functions
1625 piecewise_linear_distribution();
1626 template<class InputIteratorB, class InputIteratorW>
1627 piecewise_linear_distribution(InputIteratorB firstB,
1628 InputIteratorB lastB,
1629 InputIteratorW firstW);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001630
Howard Hinnanta001ef32010-05-25 00:27:34 +00001631 template<class UnaryOperation>
1632 piecewise_linear_distribution(initializer_list<result_type> bl,
1633 UnaryOperation fw);
1634
1635 template<class UnaryOperation>
1636 piecewise_linear_distribution(size_t nw, result_type xmin,
1637 result_type xmax, UnaryOperation fw);
1638
1639 explicit piecewise_linear_distribution(const param_type& parm);
1640 void reset();
1641
1642 // generating functions
1643 template<class URNG> result_type operator()(URNG& g);
1644 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1645
1646 // property functions
1647 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001648 vector<result_type> densities() const;
Howard Hinnanta001ef32010-05-25 00:27:34 +00001649
1650 param_type param() const;
1651 void param(const param_type& parm);
1652
1653 result_type min() const;
1654 result_type max() const;
1655
1656 friend bool operator==(const piecewise_linear_distribution& x,
1657 const piecewise_linear_distribution& y);
1658 friend bool operator!=(const piecewise_linear_distribution& x,
1659 const piecewise_linear_distribution& y);
1660
1661 template <class charT, class traits>
1662 friend
1663 basic_ostream<charT, traits>&
1664 operator<<(basic_ostream<charT, traits>& os,
1665 const piecewise_linear_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001666
Howard Hinnanta001ef32010-05-25 00:27:34 +00001667 template <class charT, class traits>
1668 friend
1669 basic_istream<charT, traits>&
1670 operator>>(basic_istream<charT, traits>& is,
1671 piecewise_linear_distribution& x);
1672};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673
1674} // std
1675*/
1676
1677#include <__config>
1678#include <cstddef>
Eric Fiselier90adc202015-01-23 22:22:36 +00001679#include <cstdint>
1680#include <cmath>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681#include <type_traits>
1682#include <initializer_list>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683#include <limits>
1684#include <algorithm>
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00001685#include <numeric>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686#include <vector>
1687#include <string>
Louis Dionne3df65ce2020-10-15 13:27:27 -04001688#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689
Howard Hinnantaaaa52b2011-10-17 20:05:10 +00001690#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001691#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +00001692#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693
Eric Fiselierf4433a32017-05-31 22:07:49 +00001694_LIBCPP_PUSH_MACROS
1695#include <__undef_macros>
1696
1697
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698_LIBCPP_BEGIN_NAMESPACE_STD
1699
Howard Hinnantc8c73992011-04-11 18:22:12 +00001700// __is_seed_sequence
1701
1702template <class _Sseq, class _Engine>
1703struct __is_seed_sequence
1704{
Howard Hinnant5a646852012-04-02 21:00:45 +00001705 static _LIBCPP_CONSTEXPR const bool value =
Howard Hinnantc8c73992011-04-11 18:22:12 +00001706 !is_convertible<_Sseq, typename _Engine::result_type>::value &&
1707 !is_same<typename remove_cv<_Sseq>::type, _Engine>::value;
1708};
1709
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710// linear_congruential_engine
1711
1712template <unsigned long long __a, unsigned long long __c,
Howard Hinnantc834c512011-11-29 18:15:50 +00001713 unsigned long long __m, unsigned long long _Mp,
zoecarver68c37022020-11-10 18:23:22 -08001714 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_Mp-__c)/__a),
1715 bool _OverflowOK = ((__m|__m-1) > __m), // m = 2^n
1716 bool _SchrageOK = (__a != 0 && __m != 0 && __m % __a <= __m / __a)> // r <= q
1717struct __lce_alg_picker
1718{
1719 static_assert(__a != 0 || __m != 0 || !_MightOverflow || _OverflowOK || _SchrageOK,
1720 "The current values of a, c, and m cannot generate a number "
1721 "within bounds of linear_congruential_engine.");
1722
1723 static _LIBCPP_CONSTEXPR const bool __use_schrage = _MightOverflow &&
1724 !_OverflowOK &&
1725 _SchrageOK;
1726};
1727
1728template <unsigned long long __a, unsigned long long __c,
1729 unsigned long long __m, unsigned long long _Mp,
1730 bool _UseSchrage = __lce_alg_picker<__a, __c, __m, _Mp>::__use_schrage>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731struct __lce_ta;
1732
1733// 64
1734
1735template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1736struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1737{
1738 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740 static result_type next(result_type __x)
1741 {
1742 // Schrage's algorithm
1743 const result_type __q = __m / __a;
1744 const result_type __r = __m % __a;
1745 const result_type __t0 = __a * (__x % __q);
1746 const result_type __t1 = __r * (__x / __q);
1747 __x = __t0 + (__t0 < __t1) * __m - __t1;
1748 __x += __c - (__x >= __m - __c) * __m;
1749 return __x;
1750 }
1751};
1752
1753template <unsigned long long __a, unsigned long long __m>
1754struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1755{
1756 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 static result_type next(result_type __x)
1759 {
1760 // Schrage's algorithm
1761 const result_type __q = __m / __a;
1762 const result_type __r = __m % __a;
1763 const result_type __t0 = __a * (__x % __q);
1764 const result_type __t1 = __r * (__x / __q);
1765 __x = __t0 + (__t0 < __t1) * __m - __t1;
1766 return __x;
1767 }
1768};
1769
1770template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1771struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1772{
1773 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 static result_type next(result_type __x)
1776 {
1777 return (__a * __x + __c) % __m;
1778 }
1779};
1780
1781template <unsigned long long __a, unsigned long long __c>
1782struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1783{
1784 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 static result_type next(result_type __x)
1787 {
1788 return __a * __x + __c;
1789 }
1790};
1791
1792// 32
1793
Howard Hinnantc834c512011-11-29 18:15:50 +00001794template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1795struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796{
1797 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 static result_type next(result_type __x)
1800 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001801 const result_type __a = static_cast<result_type>(_Ap);
1802 const result_type __c = static_cast<result_type>(_Cp);
1803 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 // Schrage's algorithm
1805 const result_type __q = __m / __a;
1806 const result_type __r = __m % __a;
1807 const result_type __t0 = __a * (__x % __q);
1808 const result_type __t1 = __r * (__x / __q);
1809 __x = __t0 + (__t0 < __t1) * __m - __t1;
1810 __x += __c - (__x >= __m - __c) * __m;
1811 return __x;
1812 }
1813};
1814
Howard Hinnantc834c512011-11-29 18:15:50 +00001815template <unsigned long long _Ap, unsigned long long _Mp>
1816struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817{
1818 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 static result_type next(result_type __x)
1821 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001822 const result_type __a = static_cast<result_type>(_Ap);
1823 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 // Schrage's algorithm
1825 const result_type __q = __m / __a;
1826 const result_type __r = __m % __a;
1827 const result_type __t0 = __a * (__x % __q);
1828 const result_type __t1 = __r * (__x / __q);
1829 __x = __t0 + (__t0 < __t1) * __m - __t1;
1830 return __x;
1831 }
1832};
1833
Howard Hinnantc834c512011-11-29 18:15:50 +00001834template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1835struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), false>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836{
1837 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 static result_type next(result_type __x)
1840 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001841 const result_type __a = static_cast<result_type>(_Ap);
1842 const result_type __c = static_cast<result_type>(_Cp);
1843 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001844 return (__a * __x + __c) % __m;
1845 }
1846};
1847
Howard Hinnantc834c512011-11-29 18:15:50 +00001848template <unsigned long long _Ap, unsigned long long _Cp>
1849struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850{
1851 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 static result_type next(result_type __x)
1854 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001855 const result_type __a = static_cast<result_type>(_Ap);
1856 const result_type __c = static_cast<result_type>(_Cp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857 return __a * __x + __c;
1858 }
1859};
1860
1861// 16
1862
1863template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1864struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1865{
1866 typedef unsigned short result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868 static result_type next(result_type __x)
1869 {
1870 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1871 }
1872};
1873
1874template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001875class _LIBCPP_TEMPLATE_VIS linear_congruential_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876
1877template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001878 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001879_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880basic_ostream<_CharT, _Traits>&
1881operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00001882 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883
1884template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001885 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886basic_istream<_CharT, _Traits>&
1887operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00001888 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889
1890template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001891class _LIBCPP_TEMPLATE_VIS linear_congruential_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892{
1893public:
1894 // types
1895 typedef _UIntType result_type;
1896
Howard Hinnanta741b242013-05-21 21:19:35 +00001897private:
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898 result_type __x_;
1899
Howard Hinnant5a646852012-04-02 21:00:45 +00001900 static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901
1902 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1903 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
zoecarver68c37022020-11-10 18:23:22 -08001904 static_assert(_VSTD::is_unsigned<_UIntType>::value, "_UIntType must be unsigned type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905public:
Howard Hinnant5a646852012-04-02 21:00:45 +00001906 static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u: 0u;
1907 static _LIBCPP_CONSTEXPR const result_type _Max = __m - 1u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1909
1910 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00001911 static _LIBCPP_CONSTEXPR const result_type multiplier = __a;
1912 static _LIBCPP_CONSTEXPR const result_type increment = __c;
1913 static _LIBCPP_CONSTEXPR const result_type modulus = __m;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00001915 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00001917 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
1918 static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919
1920 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001921#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00001922 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001923 linear_congruential_engine() : linear_congruential_engine(default_seed) {}
1924 _LIBCPP_INLINE_VISIBILITY
1925 explicit linear_congruential_engine(result_type __s) { seed(__s); }
1926#else
1927 _LIBCPP_INLINE_VISIBILITY
1928 explicit linear_congruential_engine(result_type __s = default_seed) {
1929 seed(__s);
1930 }
1931#endif
Howard Hinnant28b24882011-12-01 20:21:04 +00001932 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00001933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001934 explicit linear_congruential_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00001935 typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936 {seed(__q);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938 void seed(result_type __s = default_seed)
1939 {seed(integral_constant<bool, __m == 0>(),
1940 integral_constant<bool, __c == 0>(), __s);}
1941 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00001942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943 typename enable_if
1944 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00001945 __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946 void
1947 >::type
1948 seed(_Sseq& __q)
1949 {__seed(__q, integral_constant<unsigned,
1950 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
Howard Hinnanta0c4f7c2013-05-21 21:05:12 +00001951 : (__m > 0x100000000ull))>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952
1953 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00001954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955 result_type operator()()
Howard Hinnantc834c512011-11-29 18:15:50 +00001956 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1959
Howard Hinnantf5f99992010-09-22 18:02:38 +00001960 friend _LIBCPP_INLINE_VISIBILITY
1961 bool operator==(const linear_congruential_engine& __x,
1962 const linear_congruential_engine& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963 {return __x.__x_ == __y.__x_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001964 friend _LIBCPP_INLINE_VISIBILITY
1965 bool operator!=(const linear_congruential_engine& __x,
1966 const linear_congruential_engine& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967 {return !(__x == __y);}
1968
1969private:
1970
Howard Hinnantf5f99992010-09-22 18:02:38 +00001971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1977 1 : __s % __m;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1980
1981 template<class _Sseq>
1982 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1983 template<class _Sseq>
1984 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1985
1986 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001987 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988 friend
1989 basic_ostream<_CharT, _Traits>&
1990 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00001991 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001992
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001994 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995 friend
1996 basic_istream<_CharT, _Traits>&
1997 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00001998 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999};
2000
2001template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002002 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2003 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
2004
2005template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2006 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2007 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
2008
2009template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2010 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2011 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
2012
2013template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2014 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2015 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
2016
2017template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018template<class _Sseq>
2019void
2020linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
2021 integral_constant<unsigned, 1>)
2022{
2023 const unsigned __k = 1;
2024 uint32_t __ar[__k+3];
2025 __q.generate(__ar, __ar + __k + 3);
2026 result_type __s = static_cast<result_type>(__ar[3] % __m);
2027 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
2028}
2029
2030template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2031template<class _Sseq>
2032void
2033linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
2034 integral_constant<unsigned, 2>)
2035{
2036 const unsigned __k = 2;
2037 uint32_t __ar[__k+3];
2038 __q.generate(__ar, __ar + __k + 3);
2039 result_type __s = static_cast<result_type>((__ar[3] +
Howard Hinnanta0c4f7c2013-05-21 21:05:12 +00002040 ((uint64_t)__ar[4] << 32)) % __m);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
2042}
2043
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044template <class _CharT, class _Traits,
2045 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002046inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047basic_ostream<_CharT, _Traits>&
2048operator<<(basic_ostream<_CharT, _Traits>& __os,
2049 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2050{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002051 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002052 typedef basic_ostream<_CharT, _Traits> _Ostream;
2053 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054 __os.fill(__os.widen(' '));
2055 return __os << __x.__x_;
2056}
2057
2058template <class _CharT, class _Traits,
2059 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2060basic_istream<_CharT, _Traits>&
2061operator>>(basic_istream<_CharT, _Traits>& __is,
2062 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2063{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002064 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002065 typedef basic_istream<_CharT, _Traits> _Istream;
2066 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067 _UIntType __t;
2068 __is >> __t;
2069 if (!__is.fail())
2070 __x.__x_ = __t;
2071 return __is;
2072}
2073
2074typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2075 minstd_rand0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2077 minstd_rand;
Howard Hinnant481ba382010-05-20 15:11:46 +00002078typedef minstd_rand default_random_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002079// mersenne_twister_engine
2080
2081template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2082 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2083 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002084class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002085
Eric Fiselier4638fca2017-05-31 21:20:18 +00002086template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2087 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2088 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002090operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002091 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002092 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002093 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094
Eric Fiselier4638fca2017-05-31 21:20:18 +00002095template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2096 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2097 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnanta54386e2012-09-14 00:39:16 +00002098_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002100operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002101 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002102 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002103 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104
2105template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002106 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2107 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2108 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109basic_ostream<_CharT, _Traits>&
2110operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002111 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002112 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113
2114template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002115 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2116 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2117 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118basic_istream<_CharT, _Traits>&
2119operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002120 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002121 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122
2123template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2124 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2125 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002126class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127{
2128public:
2129 // types
2130 typedef _UIntType result_type;
2131
2132private:
2133 result_type __x_[__n];
2134 size_t __i_;
2135
2136 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2137 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
Howard Hinnant5a646852012-04-02 21:00:45 +00002138 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2140 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2141 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2142 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2143 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2144 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2145 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2146public:
Howard Hinnant5a646852012-04-02 21:00:45 +00002147 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2148 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2149 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2151 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2152 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2153 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2154 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2155 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2156
2157 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002158 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2159 static _LIBCPP_CONSTEXPR const size_t state_size = __n;
2160 static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
2161 static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
2162 static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
2163 static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
2164 static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
2165 static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
2166 static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
2167 static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
2168 static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
2169 static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
2170 static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
Howard Hinnantf5f99992010-09-22 18:02:38 +00002171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002172 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002174 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2175 static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176
2177 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002178#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002179 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002180 mersenne_twister_engine() : mersenne_twister_engine(default_seed) {}
2181 _LIBCPP_INLINE_VISIBILITY
2182 explicit mersenne_twister_engine(result_type __sd) { seed(__sd); }
2183#else
2184 _LIBCPP_INLINE_VISIBILITY
2185 explicit mersenne_twister_engine(result_type __sd = default_seed) {
2186 seed(__sd);
2187 }
2188#endif
Howard Hinnant28b24882011-12-01 20:21:04 +00002189 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002191 explicit mersenne_twister_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002192 typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193 {seed(__q);}
2194 void seed(result_type __sd = default_seed);
2195 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197 typename enable_if
2198 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002199 __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200 void
2201 >::type
2202 seed(_Sseq& __q)
2203 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2204
2205 // generating functions
2206 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2209
Eric Fiselier4638fca2017-05-31 21:20:18 +00002210 template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2211 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2212 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213 friend
2214 bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002215 operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002216 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002217 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002218 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002219
Eric Fiselier4638fca2017-05-31 21:20:18 +00002220 template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2221 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2222 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223 friend
2224 bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002225 operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002226 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002227 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002228 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229
2230 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002231 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2232 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2233 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 friend
2235 basic_ostream<_CharT, _Traits>&
2236 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002237 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002238 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239
2240 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002241 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2242 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2243 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244 friend
2245 basic_istream<_CharT, _Traits>&
2246 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002247 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002248 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249private:
2250
2251 template<class _Sseq>
2252 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2253 template<class _Sseq>
2254 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2255
2256 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 static
2259 typename enable_if
2260 <
2261 __count < __w,
2262 result_type
2263 >::type
2264 __lshift(result_type __x) {return (__x << __count) & _Max;}
2265
2266 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 static
2269 typename enable_if
2270 <
2271 (__count >= __w),
2272 result_type
2273 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002274 __lshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275
2276 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278 static
2279 typename enable_if
2280 <
2281 __count < _Dt,
2282 result_type
2283 >::type
2284 __rshift(result_type __x) {return __x >> __count;}
2285
2286 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288 static
2289 typename enable_if
2290 <
2291 (__count >= _Dt),
2292 result_type
2293 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002294 __rshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295};
2296
2297template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2298 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2299 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002300 _LIBCPP_CONSTEXPR const size_t
2301 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size;
2302
2303template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2304 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2305 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002306 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002307 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size;
2308
2309template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2310 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2311 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002312 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002313 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size;
2314
2315template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2316 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2317 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002318 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002319 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits;
2320
2321template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2322 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2323 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2324 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2325 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask;
2326
2327template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2328 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2329 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002330 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002331 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u;
2332
2333template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2334 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2335 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2336 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2337 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d;
2338
2339template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2340 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2341 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002342 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002343 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s;
2344
2345template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2346 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2347 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2348 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2349 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b;
2350
2351template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2352 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2353 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002354 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002355 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t;
2356
2357template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2358 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2359 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2360 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2361 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c;
2362
2363template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2364 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2365 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002366 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002367 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l;
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 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2373 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier;
2374
2375template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2376 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2377 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2378 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2379 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed;
2380
2381template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2382 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2383 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002384void
2385mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2386 __t, __c, __l, __f>::seed(result_type __sd)
Marshall Clowc1894632017-09-11 18:10:33 +00002387 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002388{ // __w >= 2
2389 __x_[0] = __sd & _Max;
2390 for (size_t __i = 1; __i < __n; ++__i)
2391 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2392 __i_ = 0;
2393}
2394
2395template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2396 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2397 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2398template<class _Sseq>
2399void
2400mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2401 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2402{
2403 const unsigned __k = 1;
2404 uint32_t __ar[__n * __k];
2405 __q.generate(__ar, __ar + __n * __k);
2406 for (size_t __i = 0; __i < __n; ++__i)
2407 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2408 const result_type __mask = __r == _Dt ? result_type(~0) :
2409 (result_type(1) << __r) - result_type(1);
2410 __i_ = 0;
2411 if ((__x_[0] & ~__mask) == 0)
2412 {
2413 for (size_t __i = 1; __i < __n; ++__i)
2414 if (__x_[__i] != 0)
2415 return;
Hubert Tongca5b7762018-08-16 23:56:54 +00002416 __x_[0] = result_type(1) << (__w - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417 }
2418}
2419
2420template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2421 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2422 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2423template<class _Sseq>
2424void
2425mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2426 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2427{
2428 const unsigned __k = 2;
2429 uint32_t __ar[__n * __k];
2430 __q.generate(__ar, __ar + __n * __k);
2431 for (size_t __i = 0; __i < __n; ++__i)
2432 __x_[__i] = static_cast<result_type>(
2433 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2434 const result_type __mask = __r == _Dt ? result_type(~0) :
2435 (result_type(1) << __r) - result_type(1);
2436 __i_ = 0;
2437 if ((__x_[0] & ~__mask) == 0)
2438 {
2439 for (size_t __i = 1; __i < __n; ++__i)
2440 if (__x_[__i] != 0)
2441 return;
Hubert Tongca5b7762018-08-16 23:56:54 +00002442 __x_[0] = result_type(1) << (__w - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443 }
2444}
2445
2446template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2447 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2448 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2449_UIntType
2450mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2451 __t, __c, __l, __f>::operator()()
2452{
2453 const size_t __j = (__i_ + 1) % __n;
2454 const result_type __mask = __r == _Dt ? result_type(~0) :
2455 (result_type(1) << __r) - result_type(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00002456 const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457 const size_t __k = (__i_ + __m) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00002458 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2460 __i_ = __j;
2461 __z ^= __lshift<__s>(__z) & __b;
2462 __z ^= __lshift<__t>(__z) & __c;
2463 return __z ^ __rshift<__l>(__z);
2464}
2465
Eric Fiselier4638fca2017-05-31 21:20:18 +00002466template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2467 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2468 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002470operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002471 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002472 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002473 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474{
2475 if (__x.__i_ == __y.__i_)
Howard Hinnantc834c512011-11-29 18:15:50 +00002476 return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477 if (__x.__i_ == 0 || __y.__i_ == 0)
2478 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002479 size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002480 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481 __y.__x_ + __y.__i_))
2482 return false;
2483 if (__x.__i_ == 0)
Howard Hinnantc834c512011-11-29 18:15:50 +00002484 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
2485 return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486 }
2487 if (__x.__i_ < __y.__i_)
2488 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002489 size_t __j = _Np - __y.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002490 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002491 __y.__x_ + __y.__i_))
2492 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002493 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494 __y.__x_))
2495 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002496 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002497 __y.__x_ + (_Np - (__x.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002499 size_t __j = _Np - __x.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002500 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002501 __x.__x_ + __x.__i_))
2502 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002503 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504 __x.__x_))
2505 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002506 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002507 __x.__x_ + (_Np - (__y.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002508}
2509
Eric Fiselier4638fca2017-05-31 21:20:18 +00002510template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2511 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2512 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002513inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002515operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002516 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002517 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002518 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519{
2520 return !(__x == __y);
2521}
2522
2523template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002524 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2525 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2526 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527basic_ostream<_CharT, _Traits>&
2528operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002529 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002530 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002532 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002533 typedef basic_ostream<_CharT, _Traits> _Ostream;
2534 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535 _CharT __sp = __os.widen(' ');
2536 __os.fill(__sp);
2537 __os << __x.__x_[__x.__i_];
Howard Hinnantc834c512011-11-29 18:15:50 +00002538 for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002539 __os << __sp << __x.__x_[__j];
2540 for (size_t __j = 0; __j < __x.__i_; ++__j)
2541 __os << __sp << __x.__x_[__j];
2542 return __os;
2543}
2544
2545template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002546 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2547 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2548 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549basic_istream<_CharT, _Traits>&
2550operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002551 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002552 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002554 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002555 typedef basic_istream<_CharT, _Traits> _Istream;
2556 __is.flags(_Istream::dec | _Istream::skipws);
Eric Fiselier4638fca2017-05-31 21:20:18 +00002557 _UInt __t[_Np];
Howard Hinnantc834c512011-11-29 18:15:50 +00002558 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559 __is >> __t[__i];
2560 if (!__is.fail())
2561 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002562 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563 __x.__x_[__i] = __t[__i];
2564 __x.__i_ = 0;
2565 }
2566 return __is;
2567}
2568
2569typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2570 0x9908b0df, 11, 0xffffffff,
2571 7, 0x9d2c5680,
2572 15, 0xefc60000,
2573 18, 1812433253> mt19937;
2574typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2575 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2576 17, 0x71d67fffeda60000ULL,
2577 37, 0xfff7eee000000000ULL,
2578 43, 6364136223846793005ULL> mt19937_64;
2579
2580// subtract_with_carry_engine
2581
2582template<class _UIntType, size_t __w, size_t __s, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002583class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002584
Eric Fiselier4638fca2017-05-31 21:20:18 +00002585template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586bool
2587operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002588 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2589 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590
Eric Fiselier4638fca2017-05-31 21:20:18 +00002591template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnanta54386e2012-09-14 00:39:16 +00002592_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593bool
2594operator!=(
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 Hinnantc51e1022010-05-11 19:42:16 +00002597
2598template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002599 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600basic_ostream<_CharT, _Traits>&
2601operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002602 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603
2604template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002605 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606basic_istream<_CharT, _Traits>&
2607operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002608 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609
2610template<class _UIntType, size_t __w, size_t __s, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002611class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612{
2613public:
2614 // types
2615 typedef _UIntType result_type;
2616
2617private:
2618 result_type __x_[__r];
2619 result_type __c_;
2620 size_t __i_;
2621
Howard Hinnant5a646852012-04-02 21:00:45 +00002622 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2624 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2625 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2626 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2627public:
Howard Hinnant5a646852012-04-02 21:00:45 +00002628 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2629 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2630 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2632
2633 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002634 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2635 static _LIBCPP_CONSTEXPR const size_t short_lag = __s;
2636 static _LIBCPP_CONSTEXPR const size_t long_lag = __r;
Howard Hinnantf5f99992010-09-22 18:02:38 +00002637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002638 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002640 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2641 static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642
2643 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002644#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002645 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002646 subtract_with_carry_engine() : subtract_with_carry_engine(default_seed) {}
2647 _LIBCPP_INLINE_VISIBILITY
2648 explicit subtract_with_carry_engine(result_type __sd) { seed(__sd); }
2649#else
2650 _LIBCPP_INLINE_VISIBILITY
2651 explicit subtract_with_carry_engine(result_type __sd = default_seed) {
2652 seed(__sd);
2653 }
2654#endif
Howard Hinnant28b24882011-12-01 20:21:04 +00002655 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002657 explicit subtract_with_carry_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002658 typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 {seed(__q);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661 void seed(result_type __sd = default_seed)
2662 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2663 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665 typename enable_if
2666 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002667 __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668 void
2669 >::type
2670 seed(_Sseq& __q)
2671 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2672
2673 // generating functions
2674 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2677
Eric Fiselier4638fca2017-05-31 21:20:18 +00002678 template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679 friend
2680 bool
2681 operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002682 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2683 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002684
Eric Fiselier4638fca2017-05-31 21:20:18 +00002685 template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686 friend
2687 bool
2688 operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002689 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2690 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002691
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002693 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694 friend
2695 basic_ostream<_CharT, _Traits>&
2696 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002697 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002698
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002700 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701 friend
2702 basic_istream<_CharT, _Traits>&
2703 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002704 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705
2706private:
2707
2708 void seed(result_type __sd, integral_constant<unsigned, 1>);
2709 void seed(result_type __sd, integral_constant<unsigned, 2>);
2710 template<class _Sseq>
2711 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2712 template<class _Sseq>
2713 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2714};
2715
2716template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002717 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
2718
2719template<class _UIntType, size_t __w, size_t __s, size_t __r>
2720 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
2721
2722template<class _UIntType, size_t __w, size_t __s, size_t __r>
2723 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
2724
2725template<class _UIntType, size_t __w, size_t __s, size_t __r>
2726 _LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type
2727 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
2728
2729template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730void
2731subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2732 integral_constant<unsigned, 1>)
2733{
2734 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2735 __e(__sd == 0u ? default_seed : __sd);
2736 for (size_t __i = 0; __i < __r; ++__i)
2737 __x_[__i] = static_cast<result_type>(__e() & _Max);
2738 __c_ = __x_[__r-1] == 0;
2739 __i_ = 0;
2740}
2741
2742template<class _UIntType, size_t __w, size_t __s, size_t __r>
2743void
2744subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2745 integral_constant<unsigned, 2>)
2746{
2747 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2748 __e(__sd == 0u ? default_seed : __sd);
2749 for (size_t __i = 0; __i < __r; ++__i)
Howard Hinnant93072c12011-08-15 17:22:22 +00002750 {
2751 result_type __e0 = __e();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752 __x_[__i] = static_cast<result_type>(
Howard Hinnant93072c12011-08-15 17:22:22 +00002753 (__e0 + ((uint64_t)__e() << 32)) & _Max);
2754 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755 __c_ = __x_[__r-1] == 0;
2756 __i_ = 0;
2757}
2758
2759template<class _UIntType, size_t __w, size_t __s, size_t __r>
2760template<class _Sseq>
2761void
2762subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2763 integral_constant<unsigned, 1>)
2764{
2765 const unsigned __k = 1;
2766 uint32_t __ar[__r * __k];
2767 __q.generate(__ar, __ar + __r * __k);
2768 for (size_t __i = 0; __i < __r; ++__i)
2769 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2770 __c_ = __x_[__r-1] == 0;
2771 __i_ = 0;
2772}
2773
2774template<class _UIntType, size_t __w, size_t __s, size_t __r>
2775template<class _Sseq>
2776void
2777subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2778 integral_constant<unsigned, 2>)
2779{
2780 const unsigned __k = 2;
2781 uint32_t __ar[__r * __k];
2782 __q.generate(__ar, __ar + __r * __k);
2783 for (size_t __i = 0; __i < __r; ++__i)
2784 __x_[__i] = static_cast<result_type>(
2785 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2786 __c_ = __x_[__r-1] == 0;
2787 __i_ = 0;
2788}
2789
2790template<class _UIntType, size_t __w, size_t __s, size_t __r>
2791_UIntType
2792subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2793{
2794 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2795 result_type& __xr = __x_[__i_];
2796 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2797 __xr = (__xs - __xr - __c_) & _Max;
2798 __c_ = __new_c;
2799 __i_ = (__i_ + 1) % __r;
2800 return __xr;
2801}
2802
Eric Fiselier4638fca2017-05-31 21:20:18 +00002803template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804bool
2805operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002806 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2807 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808{
2809 if (__x.__c_ != __y.__c_)
2810 return false;
2811 if (__x.__i_ == __y.__i_)
Howard Hinnantc834c512011-11-29 18:15:50 +00002812 return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 if (__x.__i_ == 0 || __y.__i_ == 0)
2814 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002815 size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002816 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817 __y.__x_ + __y.__i_))
2818 return false;
2819 if (__x.__i_ == 0)
Howard Hinnantc834c512011-11-29 18:15:50 +00002820 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
2821 return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 }
2823 if (__x.__i_ < __y.__i_)
2824 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002825 size_t __j = _Rp - __y.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002826 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827 __y.__x_ + __y.__i_))
2828 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002829 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 __y.__x_))
2831 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002832 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002833 __y.__x_ + (_Rp - (__x.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002835 size_t __j = _Rp - __x.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002836 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837 __x.__x_ + __x.__i_))
2838 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002839 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840 __x.__x_))
2841 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002842 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002843 __x.__x_ + (_Rp - (__y.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844}
2845
Eric Fiselier4638fca2017-05-31 21:20:18 +00002846template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002847inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848bool
2849operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002850 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2851 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852{
2853 return !(__x == __y);
2854}
2855
2856template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002857 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858basic_ostream<_CharT, _Traits>&
2859operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002860 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002862 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002863 typedef basic_ostream<_CharT, _Traits> _Ostream;
2864 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865 _CharT __sp = __os.widen(' ');
2866 __os.fill(__sp);
2867 __os << __x.__x_[__x.__i_];
Howard Hinnantc834c512011-11-29 18:15:50 +00002868 for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869 __os << __sp << __x.__x_[__j];
2870 for (size_t __j = 0; __j < __x.__i_; ++__j)
2871 __os << __sp << __x.__x_[__j];
2872 __os << __sp << __x.__c_;
2873 return __os;
2874}
2875
2876template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002877 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878basic_istream<_CharT, _Traits>&
2879operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002880 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002882 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002883 typedef basic_istream<_CharT, _Traits> _Istream;
2884 __is.flags(_Istream::dec | _Istream::skipws);
Eric Fiselier4638fca2017-05-31 21:20:18 +00002885 _UInt __t[_Rp+1];
Howard Hinnantc834c512011-11-29 18:15:50 +00002886 for (size_t __i = 0; __i < _Rp+1; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887 __is >> __t[__i];
2888 if (!__is.fail())
2889 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002890 for (size_t __i = 0; __i < _Rp; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891 __x.__x_[__i] = __t[__i];
Howard Hinnantc834c512011-11-29 18:15:50 +00002892 __x.__c_ = __t[_Rp];
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893 __x.__i_ = 0;
2894 }
2895 return __is;
2896}
2897
2898typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2899typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2900
2901// discard_block_engine
2902
2903template<class _Engine, size_t __p, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002904class _LIBCPP_TEMPLATE_VIS discard_block_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905{
2906 _Engine __e_;
2907 int __n_;
2908
2909 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2910 static_assert(__r <= __p, "discard_block_engine invalid parameters");
Eric Fiselier37c22152016-12-24 00:24:44 +00002911 static_assert(__r <= INT_MAX, "discard_block_engine invalid parameters");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912public:
2913 // types
2914 typedef typename _Engine::result_type result_type;
2915
2916 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002917 static _LIBCPP_CONSTEXPR const size_t block_size = __p;
2918 static _LIBCPP_CONSTEXPR const size_t used_block = __r;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00002920#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921 static const result_type _Min = _Engine::_Min;
2922 static const result_type _Max = _Engine::_Max;
Howard Hinnant5a646852012-04-02 21:00:45 +00002923#else
2924 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
2925 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
2926#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927
Howard Hinnantf5f99992010-09-22 18:02:38 +00002928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002929 static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002931 static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932
2933 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00002934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935 discard_block_engine() : __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002937 explicit discard_block_engine(const _Engine& __e)
2938 : __e_(__e), __n_(0) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00002939#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002941 explicit discard_block_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002942 : __e_(_VSTD::move(__e)), __n_(0) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00002943#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002944 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002945 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002946 template<class _Sseq>
2947 _LIBCPP_INLINE_VISIBILITY
2948 explicit discard_block_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002949 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002950 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951 : __e_(__q), __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002952 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953 void seed() {__e_.seed(); __n_ = 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002956 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002958 typename enable_if
2959 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002960 __is_seed_sequence<_Sseq, discard_block_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002961 void
2962 >::type
2963 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964
2965 // generating functions
2966 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2969
2970 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00002971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00002972 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973
Howard Hinnantc834c512011-11-29 18:15:50 +00002974 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975 friend
2976 bool
2977 operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00002978 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2979 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002980
Howard Hinnantc834c512011-11-29 18:15:50 +00002981 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982 friend
2983 bool
2984 operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00002985 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2986 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002987
Howard Hinnantc51e1022010-05-11 19:42:16 +00002988 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00002989 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990 friend
2991 basic_ostream<_CharT, _Traits>&
2992 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00002993 const discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002994
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00002996 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997 friend
2998 basic_istream<_CharT, _Traits>&
2999 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003000 discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001};
3002
3003template<class _Engine, size_t __p, size_t __r>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00003004 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
3005
3006template<class _Engine, size_t __p, size_t __r>
3007 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
3008
3009template<class _Engine, size_t __p, size_t __r>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010typename discard_block_engine<_Engine, __p, __r>::result_type
3011discard_block_engine<_Engine, __p, __r>::operator()()
3012{
Eric Fiselier37c22152016-12-24 00:24:44 +00003013 if (__n_ >= static_cast<int>(__r))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014 {
3015 __e_.discard(__p - __r);
3016 __n_ = 0;
3017 }
3018 ++__n_;
3019 return __e_();
3020}
3021
Howard Hinnantc834c512011-11-29 18:15:50 +00003022template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003023inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024bool
Howard Hinnantc834c512011-11-29 18:15:50 +00003025operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
3026 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003027{
3028 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
3029}
3030
Howard Hinnantc834c512011-11-29 18:15:50 +00003031template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003032inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033bool
Howard Hinnantc834c512011-11-29 18:15:50 +00003034operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
3035 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003036{
3037 return !(__x == __y);
3038}
3039
3040template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003041 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042basic_ostream<_CharT, _Traits>&
3043operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003044 const discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003046 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003047 typedef basic_ostream<_CharT, _Traits> _Ostream;
3048 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049 _CharT __sp = __os.widen(' ');
3050 __os.fill(__sp);
3051 return __os << __x.__e_ << __sp << __x.__n_;
3052}
3053
3054template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003055 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003056basic_istream<_CharT, _Traits>&
3057operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003058 discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003060 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003061 typedef basic_istream<_CharT, _Traits> _Istream;
3062 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063 _Eng __e;
3064 int __n;
3065 __is >> __e >> __n;
3066 if (!__is.fail())
3067 {
3068 __x.__e_ = __e;
3069 __x.__n_ = __n;
3070 }
3071 return __is;
3072}
3073
3074typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
3075typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
3076
3077// independent_bits_engine
3078
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079template<class _Engine, size_t __w, class _UIntType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003080class _LIBCPP_TEMPLATE_VIS independent_bits_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081{
Eric Fiselier4638fca2017-05-31 21:20:18 +00003082 template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083 class __get_n
3084 {
Eric Fiselier4638fca2017-05-31 21:20:18 +00003085 static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits;
Howard Hinnant5a646852012-04-02 21:00:45 +00003086 static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
3087 static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
Eric Fiselier4638fca2017-05-31 21:20:18 +00003088 static _LIBCPP_CONSTEXPR const _UInt _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089 public:
Howard Hinnant5a646852012-04-02 21:00:45 +00003090 static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091 };
3092public:
3093 // types
3094 typedef _UIntType result_type;
3095
3096private:
3097 _Engine __e_;
3098
Howard Hinnant5a646852012-04-02 21:00:45 +00003099 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003100 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
3101 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
3102
3103 typedef typename _Engine::result_type _Engine_result_type;
3104 typedef typename conditional
3105 <
3106 sizeof(_Engine_result_type) <= sizeof(result_type),
3107 result_type,
3108 _Engine_result_type
3109 >::type _Working_result_type;
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003110#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +00003111 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant5a646852012-04-02 21:00:45 +00003112 + _Working_result_type(1);
3113#else
3114 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
3115 + _Working_result_type(1);
3116#endif
3117 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
3118 static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
3119 static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
3120 static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
3121 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
3122 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
3123 static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
3124 (_Rp >> __w0) << __w0;
3125 static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
3126 (_Rp >> (__w0+1)) << (__w0+1);
3127 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 _Engine_result_type(~0) >> (_EDt - __w0) :
3129 _Engine_result_type(0);
Howard Hinnant5a646852012-04-02 21:00:45 +00003130 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
3132 _Engine_result_type(~0);
3133public:
Howard Hinnant5a646852012-04-02 21:00:45 +00003134 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3135 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
3136 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
3138
3139 // engine characteristics
Howard Hinnantf5f99992010-09-22 18:02:38 +00003140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003141 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00003142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003143 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144
3145 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 independent_bits_engine() {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003149 explicit independent_bits_engine(const _Engine& __e)
3150 : __e_(__e) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003151#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003153 explicit independent_bits_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003154 : __e_(_VSTD::move(__e)) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003155#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnant28b24882011-12-01 20:21:04 +00003158 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00003160 explicit independent_bits_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00003161 typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003162 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163 : __e_(__q) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 void seed() {__e_.seed();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003167 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003168 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003170 typename enable_if
3171 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00003172 __is_seed_sequence<_Sseq, independent_bits_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003173 void
3174 >::type
3175 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176
3177 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003179 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003181 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3182
3183 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00003185 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186
Eric Fiselier4638fca2017-05-31 21:20:18 +00003187 template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188 friend
3189 bool
3190 operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003191 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3192 const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003193
Eric Fiselier4638fca2017-05-31 21:20:18 +00003194 template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003195 friend
3196 bool
3197 operator!=(
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 Hinnant3b6579a2010-08-22 00:02:43 +00003200
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003202 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203 friend
3204 basic_ostream<_CharT, _Traits>&
3205 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003206 const independent_bits_engine<_Eng, _Wp, _UInt>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003207
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003209 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210 friend
3211 basic_istream<_CharT, _Traits>&
3212 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003213 independent_bits_engine<_Eng, _Wp, _UInt>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214
3215private:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003216 _LIBCPP_INLINE_VISIBILITY
Marshall Clowfe778582017-09-20 19:38:43 +00003217 result_type __eval(false_type);
3218 result_type __eval(true_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219
3220 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222 static
3223 typename enable_if
3224 <
3225 __count < _Dt,
3226 result_type
3227 >::type
3228 __lshift(result_type __x) {return __x << __count;}
3229
3230 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232 static
3233 typename enable_if
3234 <
3235 (__count >= _Dt),
3236 result_type
3237 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00003238 __lshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239};
3240
3241template<class _Engine, size_t __w, class _UIntType>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003242inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00003244independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245{
3246 return static_cast<result_type>(__e_() & __mask0);
3247}
3248
3249template<class _Engine, size_t __w, class _UIntType>
3250_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00003251independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252{
Howard Hinnantc834c512011-11-29 18:15:50 +00003253 result_type _Sp = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254 for (size_t __k = 0; __k < __n0; ++__k)
3255 {
3256 _Engine_result_type __u;
3257 do
3258 {
3259 __u = __e_() - _Engine::min();
3260 } while (__u >= __y0);
Howard Hinnantc834c512011-11-29 18:15:50 +00003261 _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262 }
3263 for (size_t __k = __n0; __k < __n; ++__k)
3264 {
3265 _Engine_result_type __u;
3266 do
3267 {
3268 __u = __e_() - _Engine::min();
3269 } while (__u >= __y1);
Howard Hinnantc834c512011-11-29 18:15:50 +00003270 _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271 }
Howard Hinnantc834c512011-11-29 18:15:50 +00003272 return _Sp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273}
3274
Eric Fiselier4638fca2017-05-31 21:20:18 +00003275template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003276inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277bool
3278operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003279 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3280 const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281{
3282 return __x.base() == __y.base();
3283}
3284
Eric Fiselier4638fca2017-05-31 21:20:18 +00003285template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003286inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003287bool
3288operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003289 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3290 const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291{
3292 return !(__x == __y);
3293}
3294
3295template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003296 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297basic_ostream<_CharT, _Traits>&
3298operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003299 const independent_bits_engine<_Eng, _Wp, _UInt>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300{
3301 return __os << __x.base();
3302}
3303
3304template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003305 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003306basic_istream<_CharT, _Traits>&
3307operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003308 independent_bits_engine<_Eng, _Wp, _UInt>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309{
3310 _Eng __e;
3311 __is >> __e;
3312 if (!__is.fail())
3313 __x.__e_ = __e;
3314 return __is;
3315}
3316
3317// shuffle_order_engine
3318
3319template <uint64_t _Xp, uint64_t _Yp>
3320struct __ugcd
3321{
Howard Hinnant5a646852012-04-02 21:00:45 +00003322 static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003323};
3324
3325template <uint64_t _Xp>
3326struct __ugcd<_Xp, 0>
3327{
Howard Hinnant5a646852012-04-02 21:00:45 +00003328 static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329};
3330
Howard Hinnantc834c512011-11-29 18:15:50 +00003331template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332class __uratio
3333{
Howard Hinnantc834c512011-11-29 18:15:50 +00003334 static_assert(_Dp != 0, "__uratio divide by 0");
Howard Hinnant5a646852012-04-02 21:00:45 +00003335 static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003336public:
Howard Hinnant5a646852012-04-02 21:00:45 +00003337 static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
3338 static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339
3340 typedef __uratio<num, den> type;
3341};
3342
3343template<class _Engine, size_t __k>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003344class _LIBCPP_TEMPLATE_VIS shuffle_order_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345{
3346 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3347public:
3348 // types
3349 typedef typename _Engine::result_type result_type;
3350
3351private:
3352 _Engine __e_;
3353 result_type _V_[__k];
3354 result_type _Y_;
3355
3356public:
3357 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00003358 static _LIBCPP_CONSTEXPR const size_t table_size = __k;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003359
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003360#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361 static const result_type _Min = _Engine::_Min;
3362 static const result_type _Max = _Engine::_Max;
Howard Hinnant5a646852012-04-02 21:00:45 +00003363#else
3364 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
3365 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
3366#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
Howard Hinnantf5f99992010-09-22 18:02:38 +00003368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003369 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00003370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003371 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372
Howard Hinnant5a646852012-04-02 21:00:45 +00003373 static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003374
3375 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003377 shuffle_order_engine() {__init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003379 explicit shuffle_order_engine(const _Engine& __e)
3380 : __e_(__e) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003381#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003383 explicit shuffle_order_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003384 : __e_(_VSTD::move(__e)) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003385#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003387 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnant28b24882011-12-01 20:21:04 +00003388 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00003390 explicit shuffle_order_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00003391 typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003392 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003393 : __e_(__q) {__init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395 void seed() {__e_.seed(); __init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003398 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003400 typename enable_if
3401 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00003402 __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003403 void
3404 >::type
3405 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003406
3407 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003409 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3412
3413 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00003415 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416
3417private:
Howard Hinnantc834c512011-11-29 18:15:50 +00003418 template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003419 friend
3420 bool
3421 operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00003422 const shuffle_order_engine<_Eng, _Kp>& __x,
3423 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003424
Howard Hinnantc834c512011-11-29 18:15:50 +00003425 template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003426 friend
3427 bool
3428 operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00003429 const shuffle_order_engine<_Eng, _Kp>& __x,
3430 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003431
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003433 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434 friend
3435 basic_ostream<_CharT, _Traits>&
3436 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003437 const shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003438
Howard Hinnantc51e1022010-05-11 19:42:16 +00003439 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003440 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441 friend
3442 basic_istream<_CharT, _Traits>&
3443 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003444 shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445
Howard Hinnantf5f99992010-09-22 18:02:38 +00003446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447 void __init()
3448 {
3449 for (size_t __i = 0; __i < __k; ++__i)
3450 _V_[__i] = __e_();
3451 _Y_ = __e_();
3452 }
3453
Howard Hinnantf5f99992010-09-22 18:02:38 +00003454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003457 result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458
Howard Hinnantf5f99992010-09-22 18:02:38 +00003459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3463
Howard Hinnantc834c512011-11-29 18:15:50 +00003464 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466 typename enable_if
3467 <
Howard Hinnantc834c512011-11-29 18:15:50 +00003468 (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469 result_type
3470 >::type
Howard Hinnantc834c512011-11-29 18:15:50 +00003471 __eval(__uratio<_Np, _Dp>)
3472 {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473
Howard Hinnantc834c512011-11-29 18:15:50 +00003474 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476 typename enable_if
3477 <
Howard Hinnantc834c512011-11-29 18:15:50 +00003478 __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479 result_type
3480 >::type
Howard Hinnantc834c512011-11-29 18:15:50 +00003481 __eval(__uratio<_Np, _Dp>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003482 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003483 const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
3484 / __uratio<_Np, _Dp>::den);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485 _Y_ = _V_[__j];
3486 _V_[__j] = __e_();
3487 return _Y_;
3488 }
3489
3490 template <uint64_t __n, uint64_t __d>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492 result_type __evalf()
3493 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003494 const double _Fp = __d == 0 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003495 __n / (2. * 0x8000000000000000ull) :
3496 __n / (double)__d;
Howard Hinnantc834c512011-11-29 18:15:50 +00003497 const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498 _Y_ = _V_[__j];
3499 _V_[__j] = __e_();
3500 return _Y_;
3501 }
3502};
3503
Howard Hinnant2c45cb42012-12-12 21:14:28 +00003504template<class _Engine, size_t __k>
3505 _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size;
3506
Howard Hinnantc834c512011-11-29 18:15:50 +00003507template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508bool
3509operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00003510 const shuffle_order_engine<_Eng, _Kp>& __x,
3511 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512{
Howard Hinnantc834c512011-11-29 18:15:50 +00003513 return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514 __x.__e_ == __y.__e_;
3515}
3516
Howard Hinnantc834c512011-11-29 18:15:50 +00003517template<class _Eng, size_t _Kp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003518inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519bool
3520operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00003521 const shuffle_order_engine<_Eng, _Kp>& __x,
3522 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523{
3524 return !(__x == __y);
3525}
3526
3527template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003528 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529basic_ostream<_CharT, _Traits>&
3530operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003531 const shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003533 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003534 typedef basic_ostream<_CharT, _Traits> _Ostream;
3535 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536 _CharT __sp = __os.widen(' ');
3537 __os.fill(__sp);
3538 __os << __x.__e_ << __sp << __x._V_[0];
Howard Hinnantc834c512011-11-29 18:15:50 +00003539 for (size_t __i = 1; __i < _Kp; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003540 __os << __sp << __x._V_[__i];
3541 return __os << __sp << __x._Y_;
3542}
3543
3544template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003545 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546basic_istream<_CharT, _Traits>&
3547operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003548 shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549{
Howard Hinnantc834c512011-11-29 18:15:50 +00003550 typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003551 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003552 typedef basic_istream<_CharT, _Traits> _Istream;
3553 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554 _Eng __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003555 result_type _Vp[_Kp+1];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556 __is >> __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003557 for (size_t __i = 0; __i < _Kp+1; ++__i)
3558 __is >> _Vp[__i];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559 if (!__is.fail())
3560 {
3561 __x.__e_ = __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003562 for (size_t __i = 0; __i < _Kp; ++__i)
3563 __x._V_[__i] = _Vp[__i];
3564 __x._Y_ = _Vp[_Kp];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565 }
3566 return __is;
3567}
3568
3569typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3570
3571// random_device
3572
Louis Dionne3777e682020-10-15 10:32:09 -04003573#if !defined(_LIBCPP_HAS_NO_RANDOM_DEVICE)
3574
Howard Hinnant8331b762013-03-06 23:30:19 +00003575class _LIBCPP_TYPE_VIS random_device
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576{
Ed Schoutendcf37402015-03-10 07:46:06 +00003577#ifdef _LIBCPP_USING_DEV_RANDOM
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578 int __f_;
Ed Schoutendcf37402015-03-10 07:46:06 +00003579#endif // defined(_LIBCPP_USING_DEV_RANDOM)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580public:
3581 // types
3582 typedef unsigned result_type;
3583
3584 // generator characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00003585 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3586 static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587
Howard Hinnantf5f99992010-09-22 18:02:38 +00003588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant664183b2012-04-02 00:40:41 +00003589 static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant664183b2012-04-02 00:40:41 +00003591 static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592
3593 // constructors
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003594#ifndef _LIBCPP_CXX03_LANG
3595 random_device() : random_device("/dev/urandom") {}
3596 explicit random_device(const string& __token);
3597#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00003598 explicit random_device(const string& __token = "/dev/urandom");
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003599#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600 ~random_device();
3601
3602 // generating functions
3603 result_type operator()();
3604
3605 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +00003606 double entropy() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607
3608private:
3609 // no copy functions
3610 random_device(const random_device&); // = delete;
3611 random_device& operator=(const random_device&); // = delete;
3612};
3613
Louis Dionne3777e682020-10-15 10:32:09 -04003614#endif // !_LIBCPP_HAS_NO_RANDOM_DEVICE
3615
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616// seed_seq
3617
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003618class _LIBCPP_TEMPLATE_VIS seed_seq
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619{
3620public:
3621 // types
3622 typedef uint32_t result_type;
3623
3624private:
3625 vector<result_type> __v_;
3626
3627 template<class _InputIterator>
3628 void init(_InputIterator __first, _InputIterator __last);
3629public:
3630 // constructors
Howard Hinnantf5f99992010-09-22 18:02:38 +00003631 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4b6b8092013-10-23 05:56:47 +00003632 seed_seq() _NOEXCEPT {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003633#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634 template<class _Tp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003637#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003638
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641 seed_seq(_InputIterator __first, _InputIterator __last)
3642 {init(__first, __last);}
3643
3644 // generating functions
3645 template<class _RandomAccessIterator>
3646 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3647
3648 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003649 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4b6b8092013-10-23 05:56:47 +00003650 size_t size() const _NOEXCEPT {return __v_.size();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651 template<class _OutputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653 void param(_OutputIterator __dest) const
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003654 {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655
3656private:
3657 // no copy functions
3658 seed_seq(const seed_seq&); // = delete;
3659 void operator=(const seed_seq&); // = delete;
3660
Howard Hinnantf5f99992010-09-22 18:02:38 +00003661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003662 static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663};
3664
3665template<class _InputIterator>
3666void
3667seed_seq::init(_InputIterator __first, _InputIterator __last)
3668{
3669 for (_InputIterator __s = __first; __s != __last; ++__s)
3670 __v_.push_back(*__s & 0xFFFFFFFF);
3671}
3672
3673template<class _RandomAccessIterator>
3674void
3675seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3676{
3677 if (__first != __last)
3678 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003679 _VSTD::fill(__first, __last, 0x8b8b8b8b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680 const size_t __n = static_cast<size_t>(__last - __first);
3681 const size_t __s = __v_.size();
3682 const size_t __t = (__n >= 623) ? 11
3683 : (__n >= 68) ? 7
3684 : (__n >= 39) ? 5
3685 : (__n >= 7) ? 3
3686 : (__n - 1) / 2;
3687 const size_t __p = (__n - __t) / 2;
3688 const size_t __q = __p + __t;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003689 const size_t __m = _VSTD::max(__s + 1, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690 // __k = 0;
3691 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003692 result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693 ^ __first[__n - 1]);
3694 __first[__p] += __r;
3695 __r += __s;
3696 __first[__q] += __r;
3697 __first[0] = __r;
3698 }
3699 for (size_t __k = 1; __k <= __s; ++__k)
3700 {
3701 const size_t __kmodn = __k % __n;
3702 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003703 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704 ^ __first[(__k - 1) % __n]);
3705 __first[__kpmodn] += __r;
3706 __r += __kmodn + __v_[__k-1];
3707 __first[(__k + __q) % __n] += __r;
3708 __first[__kmodn] = __r;
3709 }
3710 for (size_t __k = __s + 1; __k < __m; ++__k)
3711 {
3712 const size_t __kmodn = __k % __n;
3713 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003714 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715 ^ __first[(__k - 1) % __n]);
3716 __first[__kpmodn] += __r;
3717 __r += __kmodn;
3718 __first[(__k + __q) % __n] += __r;
3719 __first[__kmodn] = __r;
3720 }
3721 for (size_t __k = __m; __k < __m + __n; ++__k)
3722 {
3723 const size_t __kmodn = __k % __n;
3724 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003725 result_type __r = 1566083941 * _Tp(__first[__kmodn] +
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726 __first[__kpmodn] +
3727 __first[(__k - 1) % __n]);
3728 __first[__kpmodn] ^= __r;
3729 __r -= __kmodn;
3730 __first[(__k + __q) % __n] ^= __r;
3731 __first[__kmodn] = __r;
3732 }
3733 }
3734}
3735
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003736// generate_canonical
3737
Howard Hinnantc51e1022010-05-11 19:42:16 +00003738template<class _RealType, size_t __bits, class _URNG>
3739_RealType
3740generate_canonical(_URNG& __g)
3741{
3742 const size_t _Dt = numeric_limits<_RealType>::digits;
3743 const size_t __b = _Dt < __bits ? _Dt : __bits;
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003744#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003745 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
Howard Hinnant5a646852012-04-02 21:00:45 +00003746#else
3747 const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
3748#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
Louis Dionne2422bdb2019-08-20 15:39:20 +00003750 const _RealType _Rp = static_cast<_RealType>(_URNG::max() - _URNG::min()) + _RealType(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00003751 _RealType __base = _Rp;
Howard Hinnant5a646852012-04-02 21:00:45 +00003752 _RealType _Sp = __g() - _URNG::min();
Howard Hinnantc834c512011-11-29 18:15:50 +00003753 for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp)
Howard Hinnant5a646852012-04-02 21:00:45 +00003754 _Sp += (__g() - _URNG::min()) * __base;
Howard Hinnantc834c512011-11-29 18:15:50 +00003755 return _Sp / __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756}
3757
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003758// uniform_int_distribution
3759
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003760// in <algorithm>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761
3762template <class _CharT, class _Traits, class _IT>
3763basic_ostream<_CharT, _Traits>&
3764operator<<(basic_ostream<_CharT, _Traits>& __os,
3765 const uniform_int_distribution<_IT>& __x)
3766{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003767 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003768 typedef basic_ostream<_CharT, _Traits> _Ostream;
3769 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770 _CharT __sp = __os.widen(' ');
3771 __os.fill(__sp);
3772 return __os << __x.a() << __sp << __x.b();
3773}
3774
3775template <class _CharT, class _Traits, class _IT>
3776basic_istream<_CharT, _Traits>&
3777operator>>(basic_istream<_CharT, _Traits>& __is,
3778 uniform_int_distribution<_IT>& __x)
3779{
3780 typedef uniform_int_distribution<_IT> _Eng;
3781 typedef typename _Eng::result_type result_type;
3782 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003783 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003784 typedef basic_istream<_CharT, _Traits> _Istream;
3785 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003786 result_type __a;
3787 result_type __b;
3788 __is >> __a >> __b;
3789 if (!__is.fail())
3790 __x.param(param_type(__a, __b));
3791 return __is;
3792}
3793
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003794// uniform_real_distribution
3795
Howard Hinnantc51e1022010-05-11 19:42:16 +00003796template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003797class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798{
3799public:
3800 // types
3801 typedef _RealType result_type;
3802
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003803 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003804 {
3805 result_type __a_;
3806 result_type __b_;
3807 public:
3808 typedef uniform_real_distribution distribution_type;
3809
Howard Hinnantf5f99992010-09-22 18:02:38 +00003810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811 explicit param_type(result_type __a = 0,
3812 result_type __b = 1)
3813 : __a_(__a), __b_(__b) {}
3814
Howard Hinnantf5f99992010-09-22 18:02:38 +00003815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818 result_type b() const {return __b_;}
3819
Howard Hinnantf5f99992010-09-22 18:02:38 +00003820 friend _LIBCPP_INLINE_VISIBILITY
3821 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003823 friend _LIBCPP_INLINE_VISIBILITY
3824 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825 {return !(__x == __y);}
3826 };
3827
3828private:
3829 param_type __p_;
3830
3831public:
3832 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003833#ifndef _LIBCPP_CXX03_LANG
3834 _LIBCPP_INLINE_VISIBILITY
3835 uniform_real_distribution() : uniform_real_distribution(0) {}
3836 explicit uniform_real_distribution(result_type __a, result_type __b = 1)
3837 : __p_(param_type(__a, __b)) {}
3838#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00003839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3841 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003842#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00003843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003846 void reset() {}
3847
3848 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003849 template<class _URNG>
3850 _LIBCPP_INLINE_VISIBILITY
3851 result_type operator()(_URNG& __g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003852 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003853 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854
3855 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003859 result_type b() const {return __p_.b();}
3860
Howard Hinnantf5f99992010-09-22 18:02:38 +00003861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003862 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003864 void param(const param_type& __p) {__p_ = __p;}
3865
Howard Hinnantf5f99992010-09-22 18:02:38 +00003866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867 result_type min() const {return a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003869 result_type max() const {return b();}
3870
Howard Hinnantf5f99992010-09-22 18:02:38 +00003871 friend _LIBCPP_INLINE_VISIBILITY
3872 bool operator==(const uniform_real_distribution& __x,
3873 const uniform_real_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003875 friend _LIBCPP_INLINE_VISIBILITY
3876 bool operator!=(const uniform_real_distribution& __x,
3877 const uniform_real_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878 {return !(__x == __y);}
3879};
3880
3881template<class _RealType>
3882template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003883inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884typename uniform_real_distribution<_RealType>::result_type
3885uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3886{
3887 return (__p.b() - __p.a())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003888 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889 + __p.a();
3890}
3891
3892template <class _CharT, class _Traits, class _RT>
3893basic_ostream<_CharT, _Traits>&
3894operator<<(basic_ostream<_CharT, _Traits>& __os,
3895 const uniform_real_distribution<_RT>& __x)
3896{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003897 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003898 typedef basic_ostream<_CharT, _Traits> _OStream;
3899 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
3900 _OStream::scientific);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003901 _CharT __sp = __os.widen(' ');
3902 __os.fill(__sp);
3903 return __os << __x.a() << __sp << __x.b();
3904}
3905
3906template <class _CharT, class _Traits, class _RT>
3907basic_istream<_CharT, _Traits>&
3908operator>>(basic_istream<_CharT, _Traits>& __is,
3909 uniform_real_distribution<_RT>& __x)
3910{
3911 typedef uniform_real_distribution<_RT> _Eng;
3912 typedef typename _Eng::result_type result_type;
3913 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003914 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003915 typedef basic_istream<_CharT, _Traits> _Istream;
3916 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917 result_type __a;
3918 result_type __b;
3919 __is >> __a >> __b;
3920 if (!__is.fail())
3921 __x.param(param_type(__a, __b));
3922 return __is;
3923}
3924
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003925// bernoulli_distribution
3926
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003927class _LIBCPP_TEMPLATE_VIS bernoulli_distribution
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928{
3929public:
3930 // types
3931 typedef bool result_type;
3932
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003933 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003934 {
3935 double __p_;
3936 public:
3937 typedef bernoulli_distribution distribution_type;
3938
Howard Hinnantf5f99992010-09-22 18:02:38 +00003939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003940 explicit param_type(double __p = 0.5) : __p_(__p) {}
3941
Howard Hinnantf5f99992010-09-22 18:02:38 +00003942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003943 double p() const {return __p_;}
3944
Howard Hinnantf5f99992010-09-22 18:02:38 +00003945 friend _LIBCPP_INLINE_VISIBILITY
3946 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003948 friend _LIBCPP_INLINE_VISIBILITY
3949 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950 {return !(__x == __y);}
3951 };
3952
3953private:
3954 param_type __p_;
3955
3956public:
3957 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003958#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003959 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003960 bernoulli_distribution() : bernoulli_distribution(0.5) {}
3961 _LIBCPP_INLINE_VISIBILITY
3962 explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {}
3963#else
3964 _LIBCPP_INLINE_VISIBILITY
3965 explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}
3966#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00003967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003968 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003969 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003970 void reset() {}
3971
3972 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003973 template<class _URNG>
3974 _LIBCPP_INLINE_VISIBILITY
3975 result_type operator()(_URNG& __g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003976 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003977 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978
3979 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003981 double p() const {return __p_.p();}
3982
Howard Hinnantf5f99992010-09-22 18:02:38 +00003983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003984 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986 void param(const param_type& __p) {__p_ = __p;}
3987
Howard Hinnantf5f99992010-09-22 18:02:38 +00003988 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989 result_type min() const {return false;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991 result_type max() const {return true;}
3992
Howard Hinnantf5f99992010-09-22 18:02:38 +00003993 friend _LIBCPP_INLINE_VISIBILITY
3994 bool operator==(const bernoulli_distribution& __x,
3995 const bernoulli_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003997 friend _LIBCPP_INLINE_VISIBILITY
3998 bool operator!=(const bernoulli_distribution& __x,
3999 const bernoulli_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000 {return !(__x == __y);}
4001};
4002
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004003template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004004inline
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004005bernoulli_distribution::result_type
4006bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
4007{
Howard Hinnant481ba382010-05-20 15:11:46 +00004008 uniform_real_distribution<double> __gen;
4009 return __gen(__g) < __p.p();
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004010}
4011
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012template <class _CharT, class _Traits>
4013basic_ostream<_CharT, _Traits>&
4014operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
4015{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004016 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004017 typedef basic_ostream<_CharT, _Traits> _OStream;
4018 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4019 _OStream::scientific);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020 _CharT __sp = __os.widen(' ');
4021 __os.fill(__sp);
4022 return __os << __x.p();
4023}
4024
4025template <class _CharT, class _Traits>
4026basic_istream<_CharT, _Traits>&
4027operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
4028{
4029 typedef bernoulli_distribution _Eng;
4030 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004031 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004032 typedef basic_istream<_CharT, _Traits> _Istream;
4033 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004034 double __p;
4035 __is >> __p;
4036 if (!__is.fail())
4037 __x.param(param_type(__p));
4038 return __is;
4039}
4040
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004041// binomial_distribution
4042
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004043template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004044class _LIBCPP_TEMPLATE_VIS binomial_distribution
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004045{
4046public:
4047 // types
4048 typedef _IntType result_type;
4049
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004050 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004051 {
4052 result_type __t_;
4053 double __p_;
Howard Hinnant76310352010-05-15 21:36:23 +00004054 double __pr_;
4055 double __odds_ratio_;
4056 result_type __r0_;
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004057 public:
4058 typedef binomial_distribution distribution_type;
4059
Howard Hinnant76310352010-05-15 21:36:23 +00004060 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004061
Howard Hinnantf5f99992010-09-22 18:02:38 +00004062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004063 result_type t() const {return __t_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004065 double p() const {return __p_;}
4066
Howard Hinnantf5f99992010-09-22 18:02:38 +00004067 friend _LIBCPP_INLINE_VISIBILITY
4068 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004069 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004070 friend _LIBCPP_INLINE_VISIBILITY
4071 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004072 {return !(__x == __y);}
Howard Hinnant76310352010-05-15 21:36:23 +00004073
4074 friend class binomial_distribution;
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004075 };
4076
4077private:
4078 param_type __p_;
4079
4080public:
4081 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004082#ifndef _LIBCPP_CXX03_LANG
4083 _LIBCPP_INLINE_VISIBILITY
4084 binomial_distribution() : binomial_distribution(1) {}
4085 _LIBCPP_INLINE_VISIBILITY
4086 explicit binomial_distribution(result_type __t, double __p = 0.5)
4087 : __p_(param_type(__t, __p)) {}
4088#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00004089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004090 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
4091 : __p_(param_type(__t, __p)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004092#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004094 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004095 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004096 void reset() {}
4097
4098 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004099 template<class _URNG>
4100 _LIBCPP_INLINE_VISIBILITY
4101 result_type operator()(_URNG& __g)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004102 {return (*this)(__g, __p_);}
4103 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4104
4105 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004107 result_type t() const {return __p_.t();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004109 double p() const {return __p_.p();}
4110
Howard Hinnantf5f99992010-09-22 18:02:38 +00004111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004112 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004114 void param(const param_type& __p) {__p_ = __p;}
4115
Howard Hinnantf5f99992010-09-22 18:02:38 +00004116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004117 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004119 result_type max() const {return t();}
4120
Howard Hinnantf5f99992010-09-22 18:02:38 +00004121 friend _LIBCPP_INLINE_VISIBILITY
4122 bool operator==(const binomial_distribution& __x,
4123 const binomial_distribution& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004124 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004125 friend _LIBCPP_INLINE_VISIBILITY
4126 bool operator!=(const binomial_distribution& __x,
4127 const binomial_distribution& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004128 {return !(__x == __y);}
4129};
4130
Martin Storsjö408cee72020-12-01 13:37:16 +02004131#ifndef _LIBCPP_MSVCRT_LIKE
Marshall Clow7db28572017-05-04 16:36:39 +00004132extern "C" double lgamma_r(double, int *);
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004133#endif
4134
4135inline _LIBCPP_INLINE_VISIBILITY double __libcpp_lgamma(double __d) {
Martin Storsjö408cee72020-12-01 13:37:16 +02004136#if defined(_LIBCPP_MSVCRT_LIKE)
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004137 return lgamma(__d);
4138#else
4139 int __sign;
4140 return lgamma_r(__d, &__sign);
4141#endif
4142}
Marshall Clow7db28572017-05-04 16:36:39 +00004143
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004144template<class _IntType>
Marshall Clow7db28572017-05-04 16:36:39 +00004145binomial_distribution<_IntType>::param_type::param_type(const result_type __t, const double __p)
Howard Hinnant76310352010-05-15 21:36:23 +00004146 : __t_(__t), __p_(__p)
4147{
4148 if (0 < __p_ && __p_ < 1)
4149 {
4150 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004151 __pr_ = _VSTD::exp(__libcpp_lgamma(__t_ + 1.) -
4152 __libcpp_lgamma(__r0_ + 1.) -
4153 __libcpp_lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
Marshall Clow7db28572017-05-04 16:36:39 +00004154 (__t_ - __r0_) * _VSTD::log(1 - __p_));
Howard Hinnant76310352010-05-15 21:36:23 +00004155 __odds_ratio_ = __p_ / (1 - __p_);
4156 }
4157}
4158
Marshall Clowf97a84f2014-09-17 18:33:58 +00004159// Reference: Kemp, C.D. (1986). `A modal method for generating binomial
4160// variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
Howard Hinnant76310352010-05-15 21:36:23 +00004161template<class _IntType>
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004162template<class _URNG>
4163_IntType
Howard Hinnant76310352010-05-15 21:36:23 +00004164binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004165{
Howard Hinnant76310352010-05-15 21:36:23 +00004166 if (__pr.__t_ == 0 || __pr.__p_ == 0)
4167 return 0;
4168 if (__pr.__p_ == 1)
4169 return __pr.__t_;
4170 uniform_real_distribution<double> __gen;
4171 double __u = __gen(__g) - __pr.__pr_;
4172 if (__u < 0)
4173 return __pr.__r0_;
4174 double __pu = __pr.__pr_;
4175 double __pd = __pu;
4176 result_type __ru = __pr.__r0_;
4177 result_type __rd = __ru;
4178 while (true)
4179 {
Atmn Patelda0b1b62020-03-17 15:52:36 -04004180 bool __break = true;
Howard Hinnant76310352010-05-15 21:36:23 +00004181 if (__rd >= 1)
4182 {
4183 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
4184 __u -= __pd;
Atmn Patelda0b1b62020-03-17 15:52:36 -04004185 __break = false;
Howard Hinnant76310352010-05-15 21:36:23 +00004186 if (__u < 0)
4187 return __rd - 1;
4188 }
Marshall Clowf97a84f2014-09-17 18:33:58 +00004189 if ( __rd != 0 )
4190 --__rd;
Howard Hinnant76310352010-05-15 21:36:23 +00004191 ++__ru;
4192 if (__ru <= __pr.__t_)
4193 {
4194 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
4195 __u -= __pu;
Atmn Patelda0b1b62020-03-17 15:52:36 -04004196 __break = false;
Howard Hinnant76310352010-05-15 21:36:23 +00004197 if (__u < 0)
4198 return __ru;
4199 }
Atmn Patelda0b1b62020-03-17 15:52:36 -04004200 if (__break)
4201 return 0;
Howard Hinnant76310352010-05-15 21:36:23 +00004202 }
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004203}
4204
4205template <class _CharT, class _Traits, class _IntType>
4206basic_ostream<_CharT, _Traits>&
4207operator<<(basic_ostream<_CharT, _Traits>& __os,
4208 const binomial_distribution<_IntType>& __x)
4209{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004210 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004211 typedef basic_ostream<_CharT, _Traits> _OStream;
4212 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4213 _OStream::scientific);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004214 _CharT __sp = __os.widen(' ');
4215 __os.fill(__sp);
4216 return __os << __x.t() << __sp << __x.p();
4217}
4218
4219template <class _CharT, class _Traits, class _IntType>
4220basic_istream<_CharT, _Traits>&
4221operator>>(basic_istream<_CharT, _Traits>& __is,
4222 binomial_distribution<_IntType>& __x)
4223{
4224 typedef binomial_distribution<_IntType> _Eng;
4225 typedef typename _Eng::result_type result_type;
4226 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004227 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004228 typedef basic_istream<_CharT, _Traits> _Istream;
4229 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004230 result_type __t;
4231 double __p;
4232 __is >> __t >> __p;
4233 if (!__is.fail())
4234 __x.param(param_type(__t, __p));
4235 return __is;
4236}
4237
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004238// exponential_distribution
4239
4240template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004241class _LIBCPP_TEMPLATE_VIS exponential_distribution
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004242{
4243public:
4244 // types
4245 typedef _RealType result_type;
4246
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004247 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004248 {
4249 result_type __lambda_;
4250 public:
4251 typedef exponential_distribution distribution_type;
4252
Howard Hinnantf5f99992010-09-22 18:02:38 +00004253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004254 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
4255
Howard Hinnantf5f99992010-09-22 18:02:38 +00004256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004257 result_type lambda() const {return __lambda_;}
4258
Howard Hinnantf5f99992010-09-22 18:02:38 +00004259 friend _LIBCPP_INLINE_VISIBILITY
4260 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004261 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004262 friend _LIBCPP_INLINE_VISIBILITY
4263 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004264 {return !(__x == __y);}
4265 };
4266
4267private:
4268 param_type __p_;
4269
4270public:
4271 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004272#ifndef _LIBCPP_CXX03_LANG
4273 _LIBCPP_INLINE_VISIBILITY
4274 exponential_distribution() : exponential_distribution(1) {}
4275 _LIBCPP_INLINE_VISIBILITY
4276 explicit exponential_distribution(result_type __lambda)
4277 : __p_(param_type(__lambda)) {}
4278#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00004279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004280 explicit exponential_distribution(result_type __lambda = 1)
4281 : __p_(param_type(__lambda)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004282#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004284 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004286 void reset() {}
4287
4288 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004289 template<class _URNG>
4290 _LIBCPP_INLINE_VISIBILITY
4291 result_type operator()(_URNG& __g)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004292 {return (*this)(__g, __p_);}
4293 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4294
4295 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004297 result_type lambda() const {return __p_.lambda();}
4298
Howard Hinnantf5f99992010-09-22 18:02:38 +00004299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004300 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004302 void param(const param_type& __p) {__p_ = __p;}
4303
Howard Hinnantf5f99992010-09-22 18:02:38 +00004304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004305 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant021f9902010-05-16 17:56:20 +00004307 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004308
Howard Hinnantf5f99992010-09-22 18:02:38 +00004309 friend _LIBCPP_INLINE_VISIBILITY
4310 bool operator==(const exponential_distribution& __x,
4311 const exponential_distribution& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004312 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004313 friend _LIBCPP_INLINE_VISIBILITY
4314 bool operator!=(const exponential_distribution& __x,
4315 const exponential_distribution& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004316 {return !(__x == __y);}
4317};
4318
4319template <class _RealType>
4320template<class _URNG>
4321_RealType
4322exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4323{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004324 return -_VSTD::log
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004325 (
4326 result_type(1) -
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004327 _VSTD::generate_canonical<result_type,
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004328 numeric_limits<result_type>::digits>(__g)
4329 )
4330 / __p.lambda();
4331}
4332
4333template <class _CharT, class _Traits, class _RealType>
4334basic_ostream<_CharT, _Traits>&
4335operator<<(basic_ostream<_CharT, _Traits>& __os,
4336 const exponential_distribution<_RealType>& __x)
4337{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004338 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004339 typedef basic_ostream<_CharT, _Traits> _OStream;
4340 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4341 _OStream::scientific);
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004342 return __os << __x.lambda();
4343}
4344
4345template <class _CharT, class _Traits, class _RealType>
4346basic_istream<_CharT, _Traits>&
4347operator>>(basic_istream<_CharT, _Traits>& __is,
4348 exponential_distribution<_RealType>& __x)
4349{
4350 typedef exponential_distribution<_RealType> _Eng;
4351 typedef typename _Eng::result_type result_type;
4352 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004353 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004354 typedef basic_istream<_CharT, _Traits> _Istream;
4355 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004356 result_type __lambda;
4357 __is >> __lambda;
4358 if (!__is.fail())
4359 __x.param(param_type(__lambda));
4360 return __is;
4361}
4362
Howard Hinnant76310352010-05-15 21:36:23 +00004363// normal_distribution
4364
4365template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004366class _LIBCPP_TEMPLATE_VIS normal_distribution
Howard Hinnant76310352010-05-15 21:36:23 +00004367{
4368public:
4369 // types
4370 typedef _RealType result_type;
4371
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004372 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant76310352010-05-15 21:36:23 +00004373 {
4374 result_type __mean_;
4375 result_type __stddev_;
4376 public:
4377 typedef normal_distribution distribution_type;
4378
Howard Hinnantf5f99992010-09-22 18:02:38 +00004379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004380 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4381 : __mean_(__mean), __stddev_(__stddev) {}
4382
Howard Hinnantf5f99992010-09-22 18:02:38 +00004383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004384 result_type mean() const {return __mean_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004386 result_type stddev() const {return __stddev_;}
4387
Howard Hinnantf5f99992010-09-22 18:02:38 +00004388 friend _LIBCPP_INLINE_VISIBILITY
4389 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004390 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004391 friend _LIBCPP_INLINE_VISIBILITY
4392 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004393 {return !(__x == __y);}
4394 };
4395
4396private:
4397 param_type __p_;
4398 result_type _V_;
4399 bool _V_hot_;
4400
4401public:
4402 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004403#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00004404 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004405 normal_distribution() : normal_distribution(0) {}
4406 _LIBCPP_INLINE_VISIBILITY
4407 explicit normal_distribution(result_type __mean, result_type __stddev = 1)
Howard Hinnant76310352010-05-15 21:36:23 +00004408 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004409#else
4410 _LIBCPP_INLINE_VISIBILITY
4411 explicit normal_distribution(result_type __mean = 0,
4412 result_type __stddev = 1)
4413 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
4414#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004416 explicit normal_distribution(const param_type& __p)
4417 : __p_(__p), _V_hot_(false) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004419 void reset() {_V_hot_ = false;}
4420
4421 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004422 template<class _URNG>
4423 _LIBCPP_INLINE_VISIBILITY
4424 result_type operator()(_URNG& __g)
Howard Hinnant76310352010-05-15 21:36:23 +00004425 {return (*this)(__g, __p_);}
4426 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4427
4428 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004430 result_type mean() const {return __p_.mean();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004432 result_type stddev() const {return __p_.stddev();}
4433
Howard Hinnantf5f99992010-09-22 18:02:38 +00004434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004435 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004437 void param(const param_type& __p) {__p_ = __p;}
4438
Howard Hinnantf5f99992010-09-22 18:02:38 +00004439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004440 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004442 result_type max() const {return numeric_limits<result_type>::infinity();}
4443
Howard Hinnantf5f99992010-09-22 18:02:38 +00004444 friend _LIBCPP_INLINE_VISIBILITY
4445 bool operator==(const normal_distribution& __x,
4446 const normal_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004447 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4448 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004449 friend _LIBCPP_INLINE_VISIBILITY
4450 bool operator!=(const normal_distribution& __x,
4451 const normal_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004452 {return !(__x == __y);}
4453
4454 template <class _CharT, class _Traits, class _RT>
4455 friend
4456 basic_ostream<_CharT, _Traits>&
4457 operator<<(basic_ostream<_CharT, _Traits>& __os,
4458 const normal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004459
Howard Hinnant76310352010-05-15 21:36:23 +00004460 template <class _CharT, class _Traits, class _RT>
4461 friend
4462 basic_istream<_CharT, _Traits>&
4463 operator>>(basic_istream<_CharT, _Traits>& __is,
4464 normal_distribution<_RT>& __x);
4465};
4466
4467template <class _RealType>
4468template<class _URNG>
4469_RealType
4470normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4471{
Howard Hinnantc834c512011-11-29 18:15:50 +00004472 result_type _Up;
Howard Hinnant76310352010-05-15 21:36:23 +00004473 if (_V_hot_)
4474 {
4475 _V_hot_ = false;
Howard Hinnantc834c512011-11-29 18:15:50 +00004476 _Up = _V_;
Howard Hinnant76310352010-05-15 21:36:23 +00004477 }
4478 else
4479 {
4480 uniform_real_distribution<result_type> _Uni(-1, 1);
4481 result_type __u;
4482 result_type __v;
4483 result_type __s;
4484 do
4485 {
4486 __u = _Uni(__g);
4487 __v = _Uni(__g);
4488 __s = __u * __u + __v * __v;
4489 } while (__s > 1 || __s == 0);
Howard Hinnantc834c512011-11-29 18:15:50 +00004490 result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
4491 _V_ = __v * _Fp;
Howard Hinnant76310352010-05-15 21:36:23 +00004492 _V_hot_ = true;
Howard Hinnantc834c512011-11-29 18:15:50 +00004493 _Up = __u * _Fp;
Howard Hinnant76310352010-05-15 21:36:23 +00004494 }
Howard Hinnantc834c512011-11-29 18:15:50 +00004495 return _Up * __p.stddev() + __p.mean();
Howard Hinnant76310352010-05-15 21:36:23 +00004496}
4497
4498template <class _CharT, class _Traits, class _RT>
4499basic_ostream<_CharT, _Traits>&
4500operator<<(basic_ostream<_CharT, _Traits>& __os,
4501 const normal_distribution<_RT>& __x)
4502{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004503 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004504 typedef basic_ostream<_CharT, _Traits> _OStream;
4505 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4506 _OStream::scientific);
Howard Hinnant76310352010-05-15 21:36:23 +00004507 _CharT __sp = __os.widen(' ');
4508 __os.fill(__sp);
4509 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4510 if (__x._V_hot_)
4511 __os << __sp << __x._V_;
4512 return __os;
4513}
4514
4515template <class _CharT, class _Traits, class _RT>
4516basic_istream<_CharT, _Traits>&
4517operator>>(basic_istream<_CharT, _Traits>& __is,
4518 normal_distribution<_RT>& __x)
4519{
4520 typedef normal_distribution<_RT> _Eng;
4521 typedef typename _Eng::result_type result_type;
4522 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004523 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004524 typedef basic_istream<_CharT, _Traits> _Istream;
4525 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant76310352010-05-15 21:36:23 +00004526 result_type __mean;
4527 result_type __stddev;
Howard Hinnantc834c512011-11-29 18:15:50 +00004528 result_type _Vp = 0;
Howard Hinnant76310352010-05-15 21:36:23 +00004529 bool _V_hot = false;
4530 __is >> __mean >> __stddev >> _V_hot;
4531 if (_V_hot)
Howard Hinnantc834c512011-11-29 18:15:50 +00004532 __is >> _Vp;
Howard Hinnant76310352010-05-15 21:36:23 +00004533 if (!__is.fail())
4534 {
4535 __x.param(param_type(__mean, __stddev));
4536 __x._V_hot_ = _V_hot;
Howard Hinnantc834c512011-11-29 18:15:50 +00004537 __x._V_ = _Vp;
Howard Hinnant76310352010-05-15 21:36:23 +00004538 }
4539 return __is;
4540}
4541
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004542// lognormal_distribution
4543
4544template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004545class _LIBCPP_TEMPLATE_VIS lognormal_distribution
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004546{
4547public:
4548 // types
4549 typedef _RealType result_type;
4550
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004551 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004552 {
4553 normal_distribution<result_type> __nd_;
4554 public:
4555 typedef lognormal_distribution distribution_type;
4556
Howard Hinnantf5f99992010-09-22 18:02:38 +00004557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004558 explicit param_type(result_type __m = 0, result_type __s = 1)
4559 : __nd_(__m, __s) {}
4560
Howard Hinnantf5f99992010-09-22 18:02:38 +00004561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004562 result_type m() const {return __nd_.mean();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004564 result_type s() const {return __nd_.stddev();}
4565
Howard Hinnantf5f99992010-09-22 18:02:38 +00004566 friend _LIBCPP_INLINE_VISIBILITY
4567 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004568 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004569 friend _LIBCPP_INLINE_VISIBILITY
4570 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004571 {return !(__x == __y);}
4572 friend class lognormal_distribution;
4573
4574 template <class _CharT, class _Traits, class _RT>
4575 friend
4576 basic_ostream<_CharT, _Traits>&
4577 operator<<(basic_ostream<_CharT, _Traits>& __os,
4578 const lognormal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004579
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004580 template <class _CharT, class _Traits, class _RT>
4581 friend
4582 basic_istream<_CharT, _Traits>&
4583 operator>>(basic_istream<_CharT, _Traits>& __is,
4584 lognormal_distribution<_RT>& __x);
4585 };
4586
4587private:
4588 param_type __p_;
4589
4590public:
4591 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004592#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00004593 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004594 lognormal_distribution() : lognormal_distribution(0) {}
4595 _LIBCPP_INLINE_VISIBILITY
4596 explicit lognormal_distribution(result_type __m, result_type __s = 1)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004597 : __p_(param_type(__m, __s)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004598#else
4599 _LIBCPP_INLINE_VISIBILITY
4600 explicit lognormal_distribution(result_type __m = 0,
4601 result_type __s = 1)
4602 : __p_(param_type(__m, __s)) {}
4603#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004605 explicit lognormal_distribution(const param_type& __p)
4606 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004608 void reset() {__p_.__nd_.reset();}
4609
4610 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004611 template<class _URNG>
4612 _LIBCPP_INLINE_VISIBILITY
4613 result_type operator()(_URNG& __g)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004614 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004615 template<class _URNG>
4616 _LIBCPP_INLINE_VISIBILITY
4617 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004618 {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004619
4620 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004622 result_type m() const {return __p_.m();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004624 result_type s() const {return __p_.s();}
4625
Howard Hinnantf5f99992010-09-22 18:02:38 +00004626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004627 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00004629 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004630
Howard Hinnantf5f99992010-09-22 18:02:38 +00004631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004632 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004634 result_type max() const {return numeric_limits<result_type>::infinity();}
4635
Howard Hinnantf5f99992010-09-22 18:02:38 +00004636 friend _LIBCPP_INLINE_VISIBILITY
4637 bool operator==(const lognormal_distribution& __x,
4638 const lognormal_distribution& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004639 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004640 friend _LIBCPP_INLINE_VISIBILITY
4641 bool operator!=(const lognormal_distribution& __x,
4642 const lognormal_distribution& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004643 {return !(__x == __y);}
4644
4645 template <class _CharT, class _Traits, class _RT>
4646 friend
4647 basic_ostream<_CharT, _Traits>&
4648 operator<<(basic_ostream<_CharT, _Traits>& __os,
4649 const lognormal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004650
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004651 template <class _CharT, class _Traits, class _RT>
4652 friend
4653 basic_istream<_CharT, _Traits>&
4654 operator>>(basic_istream<_CharT, _Traits>& __is,
4655 lognormal_distribution<_RT>& __x);
4656};
4657
4658template <class _CharT, class _Traits, class _RT>
Howard Hinnantf5f99992010-09-22 18:02:38 +00004659inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004660basic_ostream<_CharT, _Traits>&
4661operator<<(basic_ostream<_CharT, _Traits>& __os,
4662 const lognormal_distribution<_RT>& __x)
4663{
4664 return __os << __x.__p_.__nd_;
4665}
4666
4667template <class _CharT, class _Traits, class _RT>
Howard Hinnantf5f99992010-09-22 18:02:38 +00004668inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004669basic_istream<_CharT, _Traits>&
4670operator>>(basic_istream<_CharT, _Traits>& __is,
4671 lognormal_distribution<_RT>& __x)
4672{
4673 return __is >> __x.__p_.__nd_;
4674}
4675
Howard Hinnant76310352010-05-15 21:36:23 +00004676// poisson_distribution
4677
4678template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004679class _LIBCPP_TEMPLATE_VIS poisson_distribution
Howard Hinnant76310352010-05-15 21:36:23 +00004680{
4681public:
4682 // types
4683 typedef _IntType result_type;
4684
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004685 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant76310352010-05-15 21:36:23 +00004686 {
4687 double __mean_;
4688 double __s_;
4689 double __d_;
4690 double __l_;
4691 double __omega_;
4692 double __c0_;
4693 double __c1_;
4694 double __c2_;
4695 double __c3_;
4696 double __c_;
4697
4698 public:
4699 typedef poisson_distribution distribution_type;
4700
4701 explicit param_type(double __mean = 1.0);
4702
Howard Hinnantf5f99992010-09-22 18:02:38 +00004703 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004704 double mean() const {return __mean_;}
4705
Howard Hinnantf5f99992010-09-22 18:02:38 +00004706 friend _LIBCPP_INLINE_VISIBILITY
4707 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004708 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004709 friend _LIBCPP_INLINE_VISIBILITY
4710 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004711 {return !(__x == __y);}
4712
4713 friend class poisson_distribution;
4714 };
4715
4716private:
4717 param_type __p_;
4718
4719public:
4720 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004721#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00004722 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004723 poisson_distribution() : poisson_distribution(1.0) {}
4724 _LIBCPP_INLINE_VISIBILITY
4725 explicit poisson_distribution(double __mean)
4726 : __p_(__mean) {}
4727#else
4728 _LIBCPP_INLINE_VISIBILITY
4729 explicit poisson_distribution(double __mean = 1.0)
4730 : __p_(__mean) {}
4731#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004733 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004735 void reset() {}
4736
4737 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004738 template<class _URNG>
4739 _LIBCPP_INLINE_VISIBILITY
4740 result_type operator()(_URNG& __g)
Howard Hinnant76310352010-05-15 21:36:23 +00004741 {return (*this)(__g, __p_);}
4742 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4743
4744 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004746 double mean() const {return __p_.mean();}
4747
Howard Hinnantf5f99992010-09-22 18:02:38 +00004748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004749 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004751 void param(const param_type& __p) {__p_ = __p;}
4752
Howard Hinnantf5f99992010-09-22 18:02:38 +00004753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004754 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004756 result_type max() const {return numeric_limits<result_type>::max();}
4757
Howard Hinnantf5f99992010-09-22 18:02:38 +00004758 friend _LIBCPP_INLINE_VISIBILITY
4759 bool operator==(const poisson_distribution& __x,
4760 const poisson_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004761 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004762 friend _LIBCPP_INLINE_VISIBILITY
4763 bool operator!=(const poisson_distribution& __x,
4764 const poisson_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004765 {return !(__x == __y);}
4766};
4767
4768template<class _IntType>
4769poisson_distribution<_IntType>::param_type::param_type(double __mean)
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004770 // According to the standard `inf` is a valid input, but it causes the
4771 // distribution to hang, so we replace it with the maximum representable
4772 // mean.
4773 : __mean_(isinf(__mean) ? numeric_limits<double>::max() : __mean)
Howard Hinnant76310352010-05-15 21:36:23 +00004774{
4775 if (__mean_ < 10)
4776 {
4777 __s_ = 0;
4778 __d_ = 0;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004779 __l_ = _VSTD::exp(-__mean_);
Howard Hinnant76310352010-05-15 21:36:23 +00004780 __omega_ = 0;
4781 __c3_ = 0;
4782 __c2_ = 0;
4783 __c1_ = 0;
4784 __c0_ = 0;
4785 __c_ = 0;
4786 }
4787 else
4788 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004789 __s_ = _VSTD::sqrt(__mean_);
Howard Hinnant76310352010-05-15 21:36:23 +00004790 __d_ = 6 * __mean_ * __mean_;
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004791 __l_ = _VSTD::trunc(__mean_ - 1.1484);
Howard Hinnant76310352010-05-15 21:36:23 +00004792 __omega_ = .3989423 / __s_;
4793 double __b1_ = .4166667E-1 / __mean_;
4794 double __b2_ = .3 * __b1_ * __b1_;
4795 __c3_ = .1428571 * __b1_ * __b2_;
4796 __c2_ = __b2_ - 15. * __c3_;
4797 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4798 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4799 __c_ = .1069 / __mean_;
4800 }
4801}
4802
4803template <class _IntType>
4804template<class _URNG>
4805_IntType
4806poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4807{
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004808 double __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004809 uniform_real_distribution<double> __urd;
Howard Hinnanta5fb7ac2011-04-14 15:59:22 +00004810 if (__pr.__mean_ < 10)
Howard Hinnant76310352010-05-15 21:36:23 +00004811 {
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004812 __tx = 0;
4813 for (double __p = __urd(__urng); __p > __pr.__l_; ++__tx)
Howard Hinnant76310352010-05-15 21:36:23 +00004814 __p *= __urd(__urng);
4815 }
4816 else
4817 {
4818 double __difmuk;
4819 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4820 double __u;
4821 if (__g > 0)
4822 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004823 __tx = _VSTD::trunc(__g);
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004824 if (__tx >= __pr.__l_)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004825 return _VSTD::__clamp_to_integral<result_type>(__tx);
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004826 __difmuk = __pr.__mean_ - __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004827 __u = __urd(__urng);
4828 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004829 return _VSTD::__clamp_to_integral<result_type>(__tx);
Howard Hinnant76310352010-05-15 21:36:23 +00004830 }
4831 exponential_distribution<double> __edist;
4832 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4833 {
4834 double __e;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004835 if (__using_exp_dist || __g <= 0)
Howard Hinnant76310352010-05-15 21:36:23 +00004836 {
4837 double __t;
4838 do
4839 {
4840 __e = __edist(__urng);
4841 __u = __urd(__urng);
4842 __u += __u - 1;
4843 __t = 1.8 + (__u < 0 ? -__e : __e);
4844 } while (__t <= -.6744);
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004845 __tx = _VSTD::trunc(__pr.__mean_ + __pr.__s_ * __t);
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004846 __difmuk = __pr.__mean_ - __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004847 __using_exp_dist = true;
4848 }
4849 double __px;
4850 double __py;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004851 if (__tx < 10 && __tx >= 0)
Howard Hinnant76310352010-05-15 21:36:23 +00004852 {
Marshall Clowc7e36e82018-01-16 14:54:36 +00004853 const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
Howard Hinnant76310352010-05-15 21:36:23 +00004854 40320, 362880};
4855 __px = -__pr.__mean_;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004856 __py = _VSTD::pow(__pr.__mean_, (double)__tx) / __fac[static_cast<int>(__tx)];
Howard Hinnant76310352010-05-15 21:36:23 +00004857 }
4858 else
4859 {
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004860 double __del = .8333333E-1 / __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004861 __del -= 4.8 * __del * __del * __del;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004862 double __v = __difmuk / __tx;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004863 if (_VSTD::abs(__v) > 0.25)
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004864 __px = __tx * _VSTD::log(1 + __v) - __difmuk - __del;
Howard Hinnant76310352010-05-15 21:36:23 +00004865 else
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004866 __px = __tx * __v * __v * (((((((.1250060 * __v + -.1384794) *
Howard Hinnant76310352010-05-15 21:36:23 +00004867 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4868 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004869 __py = .3989423 / _VSTD::sqrt(__tx);
Howard Hinnant76310352010-05-15 21:36:23 +00004870 }
4871 double __r = (0.5 - __difmuk) / __pr.__s_;
4872 double __r2 = __r * __r;
4873 double __fx = -0.5 * __r2;
4874 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4875 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4876 if (__using_exp_dist)
4877 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004878 if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4879 __fy * _VSTD::exp(__fx + __e))
Howard Hinnant76310352010-05-15 21:36:23 +00004880 break;
4881 }
4882 else
4883 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004884 if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
Howard Hinnant76310352010-05-15 21:36:23 +00004885 break;
4886 }
4887 }
4888 }
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004889 return _VSTD::__clamp_to_integral<result_type>(__tx);
Howard Hinnant76310352010-05-15 21:36:23 +00004890}
4891
4892template <class _CharT, class _Traits, class _IntType>
4893basic_ostream<_CharT, _Traits>&
4894operator<<(basic_ostream<_CharT, _Traits>& __os,
4895 const poisson_distribution<_IntType>& __x)
4896{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004897 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004898 typedef basic_ostream<_CharT, _Traits> _OStream;
4899 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4900 _OStream::scientific);
Howard Hinnant76310352010-05-15 21:36:23 +00004901 return __os << __x.mean();
4902}
4903
4904template <class _CharT, class _Traits, class _IntType>
4905basic_istream<_CharT, _Traits>&
4906operator>>(basic_istream<_CharT, _Traits>& __is,
4907 poisson_distribution<_IntType>& __x)
4908{
4909 typedef poisson_distribution<_IntType> _Eng;
4910 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004911 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004912 typedef basic_istream<_CharT, _Traits> _Istream;
4913 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant76310352010-05-15 21:36:23 +00004914 double __mean;
4915 __is >> __mean;
4916 if (!__is.fail())
4917 __x.param(param_type(__mean));
4918 return __is;
4919}
4920
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004921// weibull_distribution
4922
4923template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004924class _LIBCPP_TEMPLATE_VIS weibull_distribution
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004925{
4926public:
4927 // types
4928 typedef _RealType result_type;
4929
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004930 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004931 {
4932 result_type __a_;
4933 result_type __b_;
4934 public:
4935 typedef weibull_distribution distribution_type;
4936
Howard Hinnantf5f99992010-09-22 18:02:38 +00004937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004938 explicit param_type(result_type __a = 1, result_type __b = 1)
4939 : __a_(__a), __b_(__b) {}
4940
Howard Hinnantf5f99992010-09-22 18:02:38 +00004941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004942 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004944 result_type b() const {return __b_;}
4945
Howard Hinnantf5f99992010-09-22 18:02:38 +00004946 friend _LIBCPP_INLINE_VISIBILITY
4947 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004948 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004949 friend _LIBCPP_INLINE_VISIBILITY
4950 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004951 {return !(__x == __y);}
4952 };
4953
4954private:
4955 param_type __p_;
4956
4957public:
4958 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004959#ifndef _LIBCPP_CXX03_LANG
4960 _LIBCPP_INLINE_VISIBILITY
4961 weibull_distribution() : weibull_distribution(1) {}
4962 _LIBCPP_INLINE_VISIBILITY
4963 explicit weibull_distribution(result_type __a, result_type __b = 1)
4964 : __p_(param_type(__a, __b)) {}
4965#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00004966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004967 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4968 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004969#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004971 explicit weibull_distribution(const param_type& __p)
4972 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004974 void reset() {}
4975
4976 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004977 template<class _URNG>
4978 _LIBCPP_INLINE_VISIBILITY
4979 result_type operator()(_URNG& __g)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004980 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004981 template<class _URNG>
4982 _LIBCPP_INLINE_VISIBILITY
4983 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004984 {return __p.b() *
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004985 _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004986
4987 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004988 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004989 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004991 result_type b() const {return __p_.b();}
4992
Howard Hinnantf5f99992010-09-22 18:02:38 +00004993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004994 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004996 void param(const param_type& __p) {__p_ = __p;}
4997
Howard Hinnantf5f99992010-09-22 18:02:38 +00004998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004999 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005001 result_type max() const {return numeric_limits<result_type>::infinity();}
5002
Howard Hinnantf5f99992010-09-22 18:02:38 +00005003 friend _LIBCPP_INLINE_VISIBILITY
5004 bool operator==(const weibull_distribution& __x,
5005 const weibull_distribution& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005006 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005007 friend _LIBCPP_INLINE_VISIBILITY
5008 bool operator!=(const weibull_distribution& __x,
5009 const weibull_distribution& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005010 {return !(__x == __y);}
5011};
5012
5013template <class _CharT, class _Traits, class _RT>
5014basic_ostream<_CharT, _Traits>&
5015operator<<(basic_ostream<_CharT, _Traits>& __os,
5016 const weibull_distribution<_RT>& __x)
5017{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005018 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005019 typedef basic_ostream<_CharT, _Traits> _OStream;
5020 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5021 _OStream::scientific);
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005022 _CharT __sp = __os.widen(' ');
5023 __os.fill(__sp);
5024 __os << __x.a() << __sp << __x.b();
5025 return __os;
5026}
5027
5028template <class _CharT, class _Traits, class _RT>
5029basic_istream<_CharT, _Traits>&
5030operator>>(basic_istream<_CharT, _Traits>& __is,
5031 weibull_distribution<_RT>& __x)
5032{
5033 typedef weibull_distribution<_RT> _Eng;
5034 typedef typename _Eng::result_type result_type;
5035 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005036 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005037 typedef basic_istream<_CharT, _Traits> _Istream;
5038 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005039 result_type __a;
5040 result_type __b;
5041 __is >> __a >> __b;
5042 if (!__is.fail())
5043 __x.param(param_type(__a, __b));
5044 return __is;
5045}
5046
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005047template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005048class _LIBCPP_TEMPLATE_VIS extreme_value_distribution
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005049{
5050public:
5051 // types
5052 typedef _RealType result_type;
5053
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005054 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005055 {
5056 result_type __a_;
5057 result_type __b_;
5058 public:
5059 typedef extreme_value_distribution distribution_type;
5060
Howard Hinnantf5f99992010-09-22 18:02:38 +00005061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005062 explicit param_type(result_type __a = 0, result_type __b = 1)
5063 : __a_(__a), __b_(__b) {}
5064
Howard Hinnantf5f99992010-09-22 18:02:38 +00005065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005066 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005068 result_type b() const {return __b_;}
5069
Howard Hinnantf5f99992010-09-22 18:02:38 +00005070 friend _LIBCPP_INLINE_VISIBILITY
5071 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005072 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005073 friend _LIBCPP_INLINE_VISIBILITY
5074 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005075 {return !(__x == __y);}
5076 };
5077
5078private:
5079 param_type __p_;
5080
5081public:
5082 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005083#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005084 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005085 extreme_value_distribution() : extreme_value_distribution(0) {}
5086 _LIBCPP_INLINE_VISIBILITY
5087 explicit extreme_value_distribution(result_type __a, result_type __b = 1)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005088 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005089#else
5090 _LIBCPP_INLINE_VISIBILITY
5091 explicit extreme_value_distribution(result_type __a = 0,
5092 result_type __b = 1)
5093 : __p_(param_type(__a, __b)) {}
5094#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005095 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005096 explicit extreme_value_distribution(const param_type& __p)
5097 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005099 void reset() {}
5100
5101 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005102 template<class _URNG>
5103 _LIBCPP_INLINE_VISIBILITY
5104 result_type operator()(_URNG& __g)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005105 {return (*this)(__g, __p_);}
5106 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5107
5108 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005110 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005112 result_type b() const {return __p_.b();}
5113
Howard Hinnantf5f99992010-09-22 18:02:38 +00005114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005115 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005117 void param(const param_type& __p) {__p_ = __p;}
5118
Howard Hinnantf5f99992010-09-22 18:02:38 +00005119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005120 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005122 result_type max() const {return numeric_limits<result_type>::infinity();}
5123
Howard Hinnantf5f99992010-09-22 18:02:38 +00005124 friend _LIBCPP_INLINE_VISIBILITY
5125 bool operator==(const extreme_value_distribution& __x,
5126 const extreme_value_distribution& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005127 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005128 friend _LIBCPP_INLINE_VISIBILITY
5129 bool operator!=(const extreme_value_distribution& __x,
5130 const extreme_value_distribution& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005131 {return !(__x == __y);}
5132};
5133
5134template<class _RealType>
5135template<class _URNG>
5136_RealType
5137extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5138{
5139 return __p.a() - __p.b() *
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005140 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005141}
5142
5143template <class _CharT, class _Traits, class _RT>
5144basic_ostream<_CharT, _Traits>&
5145operator<<(basic_ostream<_CharT, _Traits>& __os,
5146 const extreme_value_distribution<_RT>& __x)
5147{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005148 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005149 typedef basic_ostream<_CharT, _Traits> _OStream;
5150 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5151 _OStream::scientific);
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005152 _CharT __sp = __os.widen(' ');
5153 __os.fill(__sp);
5154 __os << __x.a() << __sp << __x.b();
5155 return __os;
5156}
5157
5158template <class _CharT, class _Traits, class _RT>
5159basic_istream<_CharT, _Traits>&
5160operator>>(basic_istream<_CharT, _Traits>& __is,
5161 extreme_value_distribution<_RT>& __x)
5162{
5163 typedef extreme_value_distribution<_RT> _Eng;
5164 typedef typename _Eng::result_type result_type;
5165 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005166 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005167 typedef basic_istream<_CharT, _Traits> _Istream;
5168 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005169 result_type __a;
5170 result_type __b;
5171 __is >> __a >> __b;
5172 if (!__is.fail())
5173 __x.param(param_type(__a, __b));
5174 return __is;
5175}
5176
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005177// gamma_distribution
5178
5179template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005180class _LIBCPP_TEMPLATE_VIS gamma_distribution
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005181{
5182public:
5183 // types
5184 typedef _RealType result_type;
5185
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005186 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005187 {
5188 result_type __alpha_;
5189 result_type __beta_;
5190 public:
5191 typedef gamma_distribution distribution_type;
5192
Howard Hinnantf5f99992010-09-22 18:02:38 +00005193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005194 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
5195 : __alpha_(__alpha), __beta_(__beta) {}
5196
Howard Hinnantf5f99992010-09-22 18:02:38 +00005197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005198 result_type alpha() const {return __alpha_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005200 result_type beta() const {return __beta_;}
5201
Howard Hinnantf5f99992010-09-22 18:02:38 +00005202 friend _LIBCPP_INLINE_VISIBILITY
5203 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005204 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005205 friend _LIBCPP_INLINE_VISIBILITY
5206 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005207 {return !(__x == __y);}
5208 };
5209
5210private:
5211 param_type __p_;
5212
5213public:
5214 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005215#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005216 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005217 gamma_distribution() : gamma_distribution(1) {}
5218 _LIBCPP_INLINE_VISIBILITY
5219 explicit gamma_distribution(result_type __alpha, result_type __beta = 1)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005220 : __p_(param_type(__alpha, __beta)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005221#else
5222 _LIBCPP_INLINE_VISIBILITY
5223 explicit gamma_distribution(result_type __alpha = 1,
5224 result_type __beta = 1)
5225 : __p_(param_type(__alpha, __beta)) {}
5226#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005228 explicit gamma_distribution(const param_type& __p)
5229 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005231 void reset() {}
5232
5233 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005234 template<class _URNG>
5235 _LIBCPP_INLINE_VISIBILITY
5236 result_type operator()(_URNG& __g)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005237 {return (*this)(__g, __p_);}
5238 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5239
5240 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005242 result_type alpha() const {return __p_.alpha();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005244 result_type beta() const {return __p_.beta();}
5245
Howard Hinnantf5f99992010-09-22 18:02:38 +00005246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005247 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005249 void param(const param_type& __p) {__p_ = __p;}
5250
Howard Hinnantf5f99992010-09-22 18:02:38 +00005251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005252 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005254 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005255
Howard Hinnantf5f99992010-09-22 18:02:38 +00005256 friend _LIBCPP_INLINE_VISIBILITY
5257 bool operator==(const gamma_distribution& __x,
5258 const gamma_distribution& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005259 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005260 friend _LIBCPP_INLINE_VISIBILITY
5261 bool operator!=(const gamma_distribution& __x,
5262 const gamma_distribution& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005263 {return !(__x == __y);}
5264};
5265
5266template <class _RealType>
5267template<class _URNG>
5268_RealType
5269gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5270{
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005271 result_type __a = __p.alpha();
5272 uniform_real_distribution<result_type> __gen(0, 1);
5273 exponential_distribution<result_type> __egen;
5274 result_type __x;
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005275 if (__a == 1)
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005276 __x = __egen(__g);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005277 else if (__a > 1)
5278 {
5279 const result_type __b = __a - 1;
5280 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005281 while (true)
5282 {
5283 const result_type __u = __gen(__g);
5284 const result_type __v = __gen(__g);
5285 const result_type __w = __u * (1 - __u);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005286 if (__w != 0)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005287 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005288 const result_type __y = _VSTD::sqrt(__c / __w) *
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005289 (__u - result_type(0.5));
5290 __x = __b + __y;
5291 if (__x >= 0)
5292 {
5293 const result_type __z = 64 * __w * __w * __w * __v * __v;
5294 if (__z <= 1 - 2 * __y * __y / __x)
5295 break;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005296 if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005297 break;
5298 }
5299 }
5300 }
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005301 }
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005302 else // __a < 1
5303 {
5304 while (true)
5305 {
5306 const result_type __u = __gen(__g);
5307 const result_type __es = __egen(__g);
5308 if (__u <= 1 - __a)
5309 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005310 __x = _VSTD::pow(__u, 1 / __a);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005311 if (__x <= __es)
5312 break;
5313 }
5314 else
5315 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005316 const result_type __e = -_VSTD::log((1-__u)/__a);
5317 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005318 if (__x <= __e + __es)
5319 break;
5320 }
5321 }
5322 }
5323 return __x * __p.beta();
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005324}
5325
5326template <class _CharT, class _Traits, class _RT>
5327basic_ostream<_CharT, _Traits>&
5328operator<<(basic_ostream<_CharT, _Traits>& __os,
5329 const gamma_distribution<_RT>& __x)
5330{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005331 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005332 typedef basic_ostream<_CharT, _Traits> _OStream;
5333 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5334 _OStream::scientific);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005335 _CharT __sp = __os.widen(' ');
5336 __os.fill(__sp);
5337 __os << __x.alpha() << __sp << __x.beta();
5338 return __os;
5339}
5340
5341template <class _CharT, class _Traits, class _RT>
5342basic_istream<_CharT, _Traits>&
5343operator>>(basic_istream<_CharT, _Traits>& __is,
5344 gamma_distribution<_RT>& __x)
5345{
5346 typedef gamma_distribution<_RT> _Eng;
5347 typedef typename _Eng::result_type result_type;
5348 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005349 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005350 typedef basic_istream<_CharT, _Traits> _Istream;
5351 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005352 result_type __alpha;
5353 result_type __beta;
5354 __is >> __alpha >> __beta;
5355 if (!__is.fail())
5356 __x.param(param_type(__alpha, __beta));
5357 return __is;
5358}
Howard Hinnant0b95bc92010-05-12 21:02:31 +00005359
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005360// negative_binomial_distribution
5361
5362template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005363class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005364{
5365public:
5366 // types
5367 typedef _IntType result_type;
5368
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005369 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005370 {
5371 result_type __k_;
5372 double __p_;
5373 public:
5374 typedef negative_binomial_distribution distribution_type;
5375
Howard Hinnantf5f99992010-09-22 18:02:38 +00005376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005377 explicit param_type(result_type __k = 1, double __p = 0.5)
5378 : __k_(__k), __p_(__p) {}
5379
Howard Hinnantf5f99992010-09-22 18:02:38 +00005380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005381 result_type k() const {return __k_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005383 double p() const {return __p_;}
5384
Howard Hinnantf5f99992010-09-22 18:02:38 +00005385 friend _LIBCPP_INLINE_VISIBILITY
5386 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005387 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005388 friend _LIBCPP_INLINE_VISIBILITY
5389 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005390 {return !(__x == __y);}
5391 };
5392
5393private:
5394 param_type __p_;
5395
5396public:
5397 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005398#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005399 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005400 negative_binomial_distribution() : negative_binomial_distribution(1) {}
5401 _LIBCPP_INLINE_VISIBILITY
5402 explicit negative_binomial_distribution(result_type __k, double __p = 0.5)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005403 : __p_(__k, __p) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005404#else
5405 _LIBCPP_INLINE_VISIBILITY
5406 explicit negative_binomial_distribution(result_type __k = 1,
5407 double __p = 0.5)
5408 : __p_(__k, __p) {}
5409#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005411 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005413 void reset() {}
5414
5415 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005416 template<class _URNG>
5417 _LIBCPP_INLINE_VISIBILITY
5418 result_type operator()(_URNG& __g)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005419 {return (*this)(__g, __p_);}
5420 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5421
5422 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005424 result_type k() const {return __p_.k();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005426 double p() const {return __p_.p();}
5427
Howard Hinnantf5f99992010-09-22 18:02:38 +00005428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005429 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005431 void param(const param_type& __p) {__p_ = __p;}
5432
Howard Hinnantf5f99992010-09-22 18:02:38 +00005433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005434 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005436 result_type max() const {return numeric_limits<result_type>::max();}
5437
Howard Hinnantf5f99992010-09-22 18:02:38 +00005438 friend _LIBCPP_INLINE_VISIBILITY
5439 bool operator==(const negative_binomial_distribution& __x,
5440 const negative_binomial_distribution& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005441 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005442 friend _LIBCPP_INLINE_VISIBILITY
5443 bool operator!=(const negative_binomial_distribution& __x,
5444 const negative_binomial_distribution& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005445 {return !(__x == __y);}
5446};
5447
5448template <class _IntType>
5449template<class _URNG>
5450_IntType
5451negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5452{
5453 result_type __k = __pr.k();
5454 double __p = __pr.p();
5455 if (__k <= 21 * __p)
5456 {
5457 bernoulli_distribution __gen(__p);
5458 result_type __f = 0;
5459 result_type __s = 0;
5460 while (__s < __k)
5461 {
5462 if (__gen(__urng))
5463 ++__s;
5464 else
5465 ++__f;
5466 }
5467 return __f;
5468 }
5469 return poisson_distribution<result_type>(gamma_distribution<double>
5470 (__k, (1-__p)/__p)(__urng))(__urng);
5471}
5472
5473template <class _CharT, class _Traits, class _IntType>
5474basic_ostream<_CharT, _Traits>&
5475operator<<(basic_ostream<_CharT, _Traits>& __os,
5476 const negative_binomial_distribution<_IntType>& __x)
5477{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005478 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005479 typedef basic_ostream<_CharT, _Traits> _OStream;
5480 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5481 _OStream::scientific);
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005482 _CharT __sp = __os.widen(' ');
5483 __os.fill(__sp);
5484 return __os << __x.k() << __sp << __x.p();
5485}
5486
5487template <class _CharT, class _Traits, class _IntType>
5488basic_istream<_CharT, _Traits>&
5489operator>>(basic_istream<_CharT, _Traits>& __is,
5490 negative_binomial_distribution<_IntType>& __x)
5491{
5492 typedef negative_binomial_distribution<_IntType> _Eng;
5493 typedef typename _Eng::result_type result_type;
5494 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005495 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005496 typedef basic_istream<_CharT, _Traits> _Istream;
5497 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005498 result_type __k;
5499 double __p;
5500 __is >> __k >> __p;
5501 if (!__is.fail())
5502 __x.param(param_type(__k, __p));
5503 return __is;
5504}
5505
Howard Hinnant28beb162010-05-17 13:44:27 +00005506// geometric_distribution
5507
5508template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005509class _LIBCPP_TEMPLATE_VIS geometric_distribution
Howard Hinnant28beb162010-05-17 13:44:27 +00005510{
5511public:
5512 // types
5513 typedef _IntType result_type;
5514
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005515 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant28beb162010-05-17 13:44:27 +00005516 {
5517 double __p_;
5518 public:
5519 typedef geometric_distribution distribution_type;
5520
Howard Hinnantf5f99992010-09-22 18:02:38 +00005521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005522 explicit param_type(double __p = 0.5) : __p_(__p) {}
5523
Howard Hinnantf5f99992010-09-22 18:02:38 +00005524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005525 double p() const {return __p_;}
5526
Howard Hinnantf5f99992010-09-22 18:02:38 +00005527 friend _LIBCPP_INLINE_VISIBILITY
5528 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005529 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005530 friend _LIBCPP_INLINE_VISIBILITY
5531 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005532 {return !(__x == __y);}
5533 };
5534
5535private:
5536 param_type __p_;
5537
5538public:
5539 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005540#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005541 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005542 geometric_distribution() : geometric_distribution(0.5) {}
5543 _LIBCPP_INLINE_VISIBILITY
5544 explicit geometric_distribution(double __p)
5545 : __p_(__p) {}
5546#else
5547 _LIBCPP_INLINE_VISIBILITY
5548 explicit geometric_distribution(double __p = 0.5)
5549 : __p_(__p) {}
5550#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005552 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005554 void reset() {}
5555
5556 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005557 template<class _URNG>
5558 _LIBCPP_INLINE_VISIBILITY
5559 result_type operator()(_URNG& __g)
Howard Hinnant28beb162010-05-17 13:44:27 +00005560 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005561 template<class _URNG>
5562 _LIBCPP_INLINE_VISIBILITY
5563 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant28beb162010-05-17 13:44:27 +00005564 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5565
5566 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005568 double p() const {return __p_.p();}
5569
Howard Hinnantf5f99992010-09-22 18:02:38 +00005570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005571 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005573 void param(const param_type& __p) {__p_ = __p;}
5574
Howard Hinnantf5f99992010-09-22 18:02:38 +00005575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005576 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005578 result_type max() const {return numeric_limits<result_type>::max();}
5579
Howard Hinnantf5f99992010-09-22 18:02:38 +00005580 friend _LIBCPP_INLINE_VISIBILITY
5581 bool operator==(const geometric_distribution& __x,
5582 const geometric_distribution& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005583 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005584 friend _LIBCPP_INLINE_VISIBILITY
5585 bool operator!=(const geometric_distribution& __x,
5586 const geometric_distribution& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005587 {return !(__x == __y);}
5588};
5589
5590template <class _CharT, class _Traits, class _IntType>
5591basic_ostream<_CharT, _Traits>&
5592operator<<(basic_ostream<_CharT, _Traits>& __os,
5593 const geometric_distribution<_IntType>& __x)
5594{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005595 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005596 typedef basic_ostream<_CharT, _Traits> _OStream;
5597 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5598 _OStream::scientific);
Howard Hinnant28beb162010-05-17 13:44:27 +00005599 return __os << __x.p();
5600}
5601
5602template <class _CharT, class _Traits, class _IntType>
5603basic_istream<_CharT, _Traits>&
5604operator>>(basic_istream<_CharT, _Traits>& __is,
5605 geometric_distribution<_IntType>& __x)
5606{
5607 typedef geometric_distribution<_IntType> _Eng;
5608 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005609 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005610 typedef basic_istream<_CharT, _Traits> _Istream;
5611 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant28beb162010-05-17 13:44:27 +00005612 double __p;
5613 __is >> __p;
5614 if (!__is.fail())
5615 __x.param(param_type(__p));
5616 return __is;
5617}
5618
Howard Hinnant252ebf02010-05-15 23:36:00 +00005619// chi_squared_distribution
5620
5621template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005622class _LIBCPP_TEMPLATE_VIS chi_squared_distribution
Howard Hinnant252ebf02010-05-15 23:36:00 +00005623{
5624public:
5625 // types
5626 typedef _RealType result_type;
5627
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005628 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant252ebf02010-05-15 23:36:00 +00005629 {
5630 result_type __n_;
5631 public:
5632 typedef chi_squared_distribution distribution_type;
5633
Howard Hinnantf5f99992010-09-22 18:02:38 +00005634 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005635 explicit param_type(result_type __n = 1) : __n_(__n) {}
5636
Howard Hinnantf5f99992010-09-22 18:02:38 +00005637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005638 result_type n() const {return __n_;}
5639
Howard Hinnantf5f99992010-09-22 18:02:38 +00005640 friend _LIBCPP_INLINE_VISIBILITY
5641 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005642 {return __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005643 friend _LIBCPP_INLINE_VISIBILITY
5644 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005645 {return !(__x == __y);}
5646 };
5647
5648private:
5649 param_type __p_;
5650
5651public:
5652 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005653#ifndef _LIBCPP_CXX03_LANG
5654 _LIBCPP_INLINE_VISIBILITY
5655 chi_squared_distribution() : chi_squared_distribution(1) {}
5656 _LIBCPP_INLINE_VISIBILITY
5657 explicit chi_squared_distribution(result_type __n)
5658 : __p_(param_type(__n)) {}
5659#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00005660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005661 explicit chi_squared_distribution(result_type __n = 1)
5662 : __p_(param_type(__n)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005663#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005665 explicit chi_squared_distribution(const param_type& __p)
5666 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005668 void reset() {}
5669
5670 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005671 template<class _URNG>
5672 _LIBCPP_INLINE_VISIBILITY
5673 result_type operator()(_URNG& __g)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005674 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005675 template<class _URNG>
5676 _LIBCPP_INLINE_VISIBILITY
5677 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005678 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5679
5680 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005682 result_type n() const {return __p_.n();}
5683
Howard Hinnantf5f99992010-09-22 18:02:38 +00005684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005685 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005687 void param(const param_type& __p) {__p_ = __p;}
5688
Howard Hinnantf5f99992010-09-22 18:02:38 +00005689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005690 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005692 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant252ebf02010-05-15 23:36:00 +00005693
Howard Hinnantf5f99992010-09-22 18:02:38 +00005694 friend _LIBCPP_INLINE_VISIBILITY
5695 bool operator==(const chi_squared_distribution& __x,
5696 const chi_squared_distribution& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005697 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005698 friend _LIBCPP_INLINE_VISIBILITY
5699 bool operator!=(const chi_squared_distribution& __x,
5700 const chi_squared_distribution& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005701 {return !(__x == __y);}
5702};
5703
5704template <class _CharT, class _Traits, class _RT>
5705basic_ostream<_CharT, _Traits>&
5706operator<<(basic_ostream<_CharT, _Traits>& __os,
5707 const chi_squared_distribution<_RT>& __x)
5708{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005709 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005710 typedef basic_ostream<_CharT, _Traits> _OStream;
5711 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5712 _OStream::scientific);
Howard Hinnant252ebf02010-05-15 23:36:00 +00005713 __os << __x.n();
5714 return __os;
5715}
5716
5717template <class _CharT, class _Traits, class _RT>
5718basic_istream<_CharT, _Traits>&
5719operator>>(basic_istream<_CharT, _Traits>& __is,
5720 chi_squared_distribution<_RT>& __x)
5721{
5722 typedef chi_squared_distribution<_RT> _Eng;
5723 typedef typename _Eng::result_type result_type;
5724 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005725 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005726 typedef basic_istream<_CharT, _Traits> _Istream;
5727 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant252ebf02010-05-15 23:36:00 +00005728 result_type __n;
5729 __is >> __n;
5730 if (!__is.fail())
5731 __x.param(param_type(__n));
5732 return __is;
5733}
5734
Howard Hinnantf3292562010-05-17 21:55:46 +00005735// cauchy_distribution
5736
5737template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005738class _LIBCPP_TEMPLATE_VIS cauchy_distribution
Howard Hinnantf3292562010-05-17 21:55:46 +00005739{
5740public:
5741 // types
5742 typedef _RealType result_type;
5743
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005744 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantf3292562010-05-17 21:55:46 +00005745 {
5746 result_type __a_;
5747 result_type __b_;
5748 public:
5749 typedef cauchy_distribution distribution_type;
5750
Howard Hinnantf5f99992010-09-22 18:02:38 +00005751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005752 explicit param_type(result_type __a = 0, result_type __b = 1)
5753 : __a_(__a), __b_(__b) {}
5754
Howard Hinnantf5f99992010-09-22 18:02:38 +00005755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005756 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005758 result_type b() const {return __b_;}
5759
Howard Hinnantf5f99992010-09-22 18:02:38 +00005760 friend _LIBCPP_INLINE_VISIBILITY
5761 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005762 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005763 friend _LIBCPP_INLINE_VISIBILITY
5764 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005765 {return !(__x == __y);}
5766 };
5767
5768private:
5769 param_type __p_;
5770
5771public:
5772 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005773#ifndef _LIBCPP_CXX03_LANG
5774 _LIBCPP_INLINE_VISIBILITY
5775 cauchy_distribution() : cauchy_distribution(0) {}
5776 _LIBCPP_INLINE_VISIBILITY
5777 explicit cauchy_distribution(result_type __a, result_type __b = 1)
5778 : __p_(param_type(__a, __b)) {}
5779#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00005780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005781 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5782 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005783#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005785 explicit cauchy_distribution(const param_type& __p)
5786 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005788 void reset() {}
5789
5790 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005791 template<class _URNG>
5792 _LIBCPP_INLINE_VISIBILITY
5793 result_type operator()(_URNG& __g)
Howard Hinnantf3292562010-05-17 21:55:46 +00005794 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005795 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantf3292562010-05-17 21:55:46 +00005796
5797 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005799 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005801 result_type b() const {return __p_.b();}
5802
Howard Hinnantf5f99992010-09-22 18:02:38 +00005803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005804 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005806 void param(const param_type& __p) {__p_ = __p;}
5807
Howard Hinnantf5f99992010-09-22 18:02:38 +00005808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005809 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005811 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantf3292562010-05-17 21:55:46 +00005812
Howard Hinnantf5f99992010-09-22 18:02:38 +00005813 friend _LIBCPP_INLINE_VISIBILITY
5814 bool operator==(const cauchy_distribution& __x,
5815 const cauchy_distribution& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005816 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005817 friend _LIBCPP_INLINE_VISIBILITY
5818 bool operator!=(const cauchy_distribution& __x,
5819 const cauchy_distribution& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005820 {return !(__x == __y);}
Howard Hinnantf3292562010-05-17 21:55:46 +00005821};
5822
5823template <class _RealType>
5824template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005825inline
Howard Hinnantf3292562010-05-17 21:55:46 +00005826_RealType
5827cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5828{
5829 uniform_real_distribution<result_type> __gen;
5830 // 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 +00005831 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
Howard Hinnantf3292562010-05-17 21:55:46 +00005832}
5833
5834template <class _CharT, class _Traits, class _RT>
5835basic_ostream<_CharT, _Traits>&
5836operator<<(basic_ostream<_CharT, _Traits>& __os,
5837 const cauchy_distribution<_RT>& __x)
5838{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005839 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005840 typedef basic_ostream<_CharT, _Traits> _OStream;
5841 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5842 _OStream::scientific);
Howard Hinnantf3292562010-05-17 21:55:46 +00005843 _CharT __sp = __os.widen(' ');
5844 __os.fill(__sp);
5845 __os << __x.a() << __sp << __x.b();
5846 return __os;
5847}
5848
5849template <class _CharT, class _Traits, class _RT>
5850basic_istream<_CharT, _Traits>&
5851operator>>(basic_istream<_CharT, _Traits>& __is,
5852 cauchy_distribution<_RT>& __x)
5853{
5854 typedef cauchy_distribution<_RT> _Eng;
5855 typedef typename _Eng::result_type result_type;
5856 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005857 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005858 typedef basic_istream<_CharT, _Traits> _Istream;
5859 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantf3292562010-05-17 21:55:46 +00005860 result_type __a;
5861 result_type __b;
5862 __is >> __a >> __b;
5863 if (!__is.fail())
5864 __x.param(param_type(__a, __b));
5865 return __is;
5866}
5867
Howard Hinnantf39463b2010-05-18 17:32:30 +00005868// fisher_f_distribution
5869
5870template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005871class _LIBCPP_TEMPLATE_VIS fisher_f_distribution
Howard Hinnantf39463b2010-05-18 17:32:30 +00005872{
5873public:
5874 // types
5875 typedef _RealType result_type;
5876
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005877 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantf39463b2010-05-18 17:32:30 +00005878 {
5879 result_type __m_;
5880 result_type __n_;
5881 public:
5882 typedef fisher_f_distribution distribution_type;
5883
Howard Hinnantf5f99992010-09-22 18:02:38 +00005884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005885 explicit param_type(result_type __m = 1, result_type __n = 1)
5886 : __m_(__m), __n_(__n) {}
5887
Howard Hinnantf5f99992010-09-22 18:02:38 +00005888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005889 result_type m() const {return __m_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005891 result_type n() const {return __n_;}
5892
Howard Hinnantf5f99992010-09-22 18:02:38 +00005893 friend _LIBCPP_INLINE_VISIBILITY
5894 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005895 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005896 friend _LIBCPP_INLINE_VISIBILITY
5897 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005898 {return !(__x == __y);}
5899 };
5900
5901private:
5902 param_type __p_;
5903
5904public:
5905 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005906#ifndef _LIBCPP_CXX03_LANG
5907 _LIBCPP_INLINE_VISIBILITY
5908 fisher_f_distribution() : fisher_f_distribution(1) {}
5909 _LIBCPP_INLINE_VISIBILITY
5910 explicit fisher_f_distribution(result_type __m, result_type __n = 1)
5911 : __p_(param_type(__m, __n)) {}
5912#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00005913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005914 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5915 : __p_(param_type(__m, __n)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005916#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005918 explicit fisher_f_distribution(const param_type& __p)
5919 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005921 void reset() {}
5922
5923 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005924 template<class _URNG>
5925 _LIBCPP_INLINE_VISIBILITY
5926 result_type operator()(_URNG& __g)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005927 {return (*this)(__g, __p_);}
5928 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5929
5930 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005932 result_type m() const {return __p_.m();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005934 result_type n() const {return __p_.n();}
5935
Howard Hinnantf5f99992010-09-22 18:02:38 +00005936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005937 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005939 void param(const param_type& __p) {__p_ = __p;}
5940
Howard Hinnantf5f99992010-09-22 18:02:38 +00005941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005942 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005944 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantf39463b2010-05-18 17:32:30 +00005945
Howard Hinnantf5f99992010-09-22 18:02:38 +00005946 friend _LIBCPP_INLINE_VISIBILITY
5947 bool operator==(const fisher_f_distribution& __x,
5948 const fisher_f_distribution& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005949 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005950 friend _LIBCPP_INLINE_VISIBILITY
5951 bool operator!=(const fisher_f_distribution& __x,
5952 const fisher_f_distribution& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005953 {return !(__x == __y);}
5954};
5955
5956template <class _RealType>
5957template<class _URNG>
5958_RealType
5959fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5960{
5961 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5962 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5963 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5964}
5965
5966template <class _CharT, class _Traits, class _RT>
5967basic_ostream<_CharT, _Traits>&
5968operator<<(basic_ostream<_CharT, _Traits>& __os,
5969 const fisher_f_distribution<_RT>& __x)
5970{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005971 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005972 typedef basic_ostream<_CharT, _Traits> _OStream;
5973 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5974 _OStream::scientific);
Howard Hinnantf39463b2010-05-18 17:32:30 +00005975 _CharT __sp = __os.widen(' ');
5976 __os.fill(__sp);
5977 __os << __x.m() << __sp << __x.n();
5978 return __os;
5979}
5980
5981template <class _CharT, class _Traits, class _RT>
5982basic_istream<_CharT, _Traits>&
5983operator>>(basic_istream<_CharT, _Traits>& __is,
5984 fisher_f_distribution<_RT>& __x)
5985{
5986 typedef fisher_f_distribution<_RT> _Eng;
5987 typedef typename _Eng::result_type result_type;
5988 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005989 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005990 typedef basic_istream<_CharT, _Traits> _Istream;
5991 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantf39463b2010-05-18 17:32:30 +00005992 result_type __m;
5993 result_type __n;
5994 __is >> __m >> __n;
5995 if (!__is.fail())
5996 __x.param(param_type(__m, __n));
5997 return __is;
5998}
5999
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006000// student_t_distribution
6001
Howard Hinnant20c50882010-05-18 20:08:04 +00006002template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006003class _LIBCPP_TEMPLATE_VIS student_t_distribution
Howard Hinnant20c50882010-05-18 20:08:04 +00006004{
6005public:
6006 // types
6007 typedef _RealType result_type;
6008
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006009 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant20c50882010-05-18 20:08:04 +00006010 {
6011 result_type __n_;
6012 public:
6013 typedef student_t_distribution distribution_type;
6014
Howard Hinnantf5f99992010-09-22 18:02:38 +00006015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006016 explicit param_type(result_type __n = 1) : __n_(__n) {}
6017
Howard Hinnantf5f99992010-09-22 18:02:38 +00006018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006019 result_type n() const {return __n_;}
6020
Howard Hinnantf5f99992010-09-22 18:02:38 +00006021 friend _LIBCPP_INLINE_VISIBILITY
6022 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006023 {return __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006024 friend _LIBCPP_INLINE_VISIBILITY
6025 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006026 {return !(__x == __y);}
6027 };
6028
6029private:
6030 param_type __p_;
6031 normal_distribution<result_type> __nd_;
6032
6033public:
6034 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01006035#ifndef _LIBCPP_CXX03_LANG
6036 _LIBCPP_INLINE_VISIBILITY
6037 student_t_distribution() : student_t_distribution(1) {}
6038 _LIBCPP_INLINE_VISIBILITY
6039 explicit student_t_distribution(result_type __n)
6040 : __p_(param_type(__n)) {}
6041#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00006042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006043 explicit student_t_distribution(result_type __n = 1)
6044 : __p_(param_type(__n)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01006045#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00006046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006047 explicit student_t_distribution(const param_type& __p)
6048 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006050 void reset() {__nd_.reset();}
6051
6052 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006053 template<class _URNG>
6054 _LIBCPP_INLINE_VISIBILITY
6055 result_type operator()(_URNG& __g)
Howard Hinnant20c50882010-05-18 20:08:04 +00006056 {return (*this)(__g, __p_);}
6057 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6058
6059 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006061 result_type n() const {return __p_.n();}
6062
Howard Hinnantf5f99992010-09-22 18:02:38 +00006063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006064 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006066 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant20c50882010-05-18 20:08:04 +00006067
Howard Hinnantf5f99992010-09-22 18:02:38 +00006068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006069 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006071 result_type max() const {return numeric_limits<result_type>::infinity();}
6072
Howard Hinnantf5f99992010-09-22 18:02:38 +00006073 friend _LIBCPP_INLINE_VISIBILITY
6074 bool operator==(const student_t_distribution& __x,
6075 const student_t_distribution& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006076 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006077 friend _LIBCPP_INLINE_VISIBILITY
6078 bool operator!=(const student_t_distribution& __x,
6079 const student_t_distribution& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006080 {return !(__x == __y);}
6081};
6082
6083template <class _RealType>
6084template<class _URNG>
6085_RealType
6086student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6087{
6088 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006089 return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
Howard Hinnant20c50882010-05-18 20:08:04 +00006090}
6091
6092template <class _CharT, class _Traits, class _RT>
6093basic_ostream<_CharT, _Traits>&
6094operator<<(basic_ostream<_CharT, _Traits>& __os,
6095 const student_t_distribution<_RT>& __x)
6096{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006097 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006098 typedef basic_ostream<_CharT, _Traits> _OStream;
6099 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6100 _OStream::scientific);
Howard Hinnant20c50882010-05-18 20:08:04 +00006101 __os << __x.n();
6102 return __os;
6103}
6104
6105template <class _CharT, class _Traits, class _RT>
6106basic_istream<_CharT, _Traits>&
6107operator>>(basic_istream<_CharT, _Traits>& __is,
6108 student_t_distribution<_RT>& __x)
6109{
6110 typedef student_t_distribution<_RT> _Eng;
6111 typedef typename _Eng::result_type result_type;
6112 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00006113 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006114 typedef basic_istream<_CharT, _Traits> _Istream;
6115 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant20c50882010-05-18 20:08:04 +00006116 result_type __n;
6117 __is >> __n;
6118 if (!__is.fail())
6119 __x.param(param_type(__n));
6120 return __is;
6121}
6122
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006123// discrete_distribution
6124
6125template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006126class _LIBCPP_TEMPLATE_VIS discrete_distribution
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006127{
6128public:
6129 // types
6130 typedef _IntType result_type;
6131
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006132 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006133 {
6134 vector<double> __p_;
6135 public:
6136 typedef discrete_distribution distribution_type;
6137
Howard Hinnantf5f99992010-09-22 18:02:38 +00006138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006139 param_type() {}
6140 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006142 param_type(_InputIterator __f, _InputIterator __l)
6143 : __p_(__f, __l) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006144#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00006145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006146 param_type(initializer_list<double> __wl)
6147 : __p_(__wl.begin(), __wl.end()) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006148#endif // _LIBCPP_CXX03_LANG
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006149 template<class _UnaryOperation>
6150 param_type(size_t __nw, double __xmin, double __xmax,
6151 _UnaryOperation __fw);
6152
6153 vector<double> probabilities() const;
6154
Howard Hinnantf5f99992010-09-22 18:02:38 +00006155 friend _LIBCPP_INLINE_VISIBILITY
6156 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006157 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006158 friend _LIBCPP_INLINE_VISIBILITY
6159 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006160 {return !(__x == __y);}
6161
6162 private:
6163 void __init();
6164
6165 friend class discrete_distribution;
6166
6167 template <class _CharT, class _Traits, class _IT>
6168 friend
6169 basic_ostream<_CharT, _Traits>&
6170 operator<<(basic_ostream<_CharT, _Traits>& __os,
6171 const discrete_distribution<_IT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006172
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006173 template <class _CharT, class _Traits, class _IT>
6174 friend
6175 basic_istream<_CharT, _Traits>&
6176 operator>>(basic_istream<_CharT, _Traits>& __is,
6177 discrete_distribution<_IT>& __x);
6178 };
6179
6180private:
6181 param_type __p_;
6182
6183public:
6184 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006186 discrete_distribution() {}
6187 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006189 discrete_distribution(_InputIterator __f, _InputIterator __l)
6190 : __p_(__f, __l) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006191#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00006192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006193 discrete_distribution(initializer_list<double> __wl)
6194 : __p_(__wl) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006195#endif // _LIBCPP_CXX03_LANG
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006196 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006198 discrete_distribution(size_t __nw, double __xmin, double __xmax,
6199 _UnaryOperation __fw)
6200 : __p_(__nw, __xmin, __xmax, __fw) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00006202 explicit discrete_distribution(const param_type& __p)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006203 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006205 void reset() {}
6206
6207 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006208 template<class _URNG>
6209 _LIBCPP_INLINE_VISIBILITY
6210 result_type operator()(_URNG& __g)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006211 {return (*this)(__g, __p_);}
6212 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6213
6214 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006216 vector<double> probabilities() const {return __p_.probabilities();}
6217
Howard Hinnantf5f99992010-09-22 18:02:38 +00006218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006219 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006221 void param(const param_type& __p) {__p_ = __p;}
6222
Howard Hinnantf5f99992010-09-22 18:02:38 +00006223 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006224 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006226 result_type max() const {return __p_.__p_.size();}
6227
Howard Hinnantf5f99992010-09-22 18:02:38 +00006228 friend _LIBCPP_INLINE_VISIBILITY
6229 bool operator==(const discrete_distribution& __x,
6230 const discrete_distribution& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006231 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006232 friend _LIBCPP_INLINE_VISIBILITY
6233 bool operator!=(const discrete_distribution& __x,
6234 const discrete_distribution& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006235 {return !(__x == __y);}
6236
6237 template <class _CharT, class _Traits, class _IT>
6238 friend
6239 basic_ostream<_CharT, _Traits>&
6240 operator<<(basic_ostream<_CharT, _Traits>& __os,
6241 const discrete_distribution<_IT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006242
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006243 template <class _CharT, class _Traits, class _IT>
6244 friend
6245 basic_istream<_CharT, _Traits>&
6246 operator>>(basic_istream<_CharT, _Traits>& __is,
6247 discrete_distribution<_IT>& __x);
6248};
6249
6250template<class _IntType>
6251template<class _UnaryOperation>
6252discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
6253 double __xmin,
6254 double __xmax,
6255 _UnaryOperation __fw)
6256{
6257 if (__nw > 1)
6258 {
6259 __p_.reserve(__nw - 1);
6260 double __d = (__xmax - __xmin) / __nw;
6261 double __d2 = __d / 2;
6262 for (size_t __k = 0; __k < __nw; ++__k)
6263 __p_.push_back(__fw(__xmin + __k * __d + __d2));
6264 __init();
6265 }
6266}
6267
6268template<class _IntType>
6269void
6270discrete_distribution<_IntType>::param_type::__init()
6271{
6272 if (!__p_.empty())
6273 {
6274 if (__p_.size() > 1)
6275 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006276 double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
6277 for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006278 __i < __e; ++__i)
6279 *__i /= __s;
6280 vector<double> __t(__p_.size() - 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006281 _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006282 swap(__p_, __t);
6283 }
6284 else
6285 {
6286 __p_.clear();
6287 __p_.shrink_to_fit();
6288 }
6289 }
6290}
6291
6292template<class _IntType>
6293vector<double>
6294discrete_distribution<_IntType>::param_type::probabilities() const
6295{
6296 size_t __n = __p_.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006297 _VSTD::vector<double> __p(__n+1);
6298 _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006299 if (__n > 0)
6300 __p[__n] = 1 - __p_[__n-1];
6301 else
6302 __p[0] = 1;
6303 return __p;
6304}
6305
6306template<class _IntType>
6307template<class _URNG>
6308_IntType
6309discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
6310{
6311 uniform_real_distribution<double> __gen;
6312 return static_cast<_IntType>(
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006313 _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006314 __p.__p_.begin());
6315}
6316
6317template <class _CharT, class _Traits, class _IT>
6318basic_ostream<_CharT, _Traits>&
6319operator<<(basic_ostream<_CharT, _Traits>& __os,
6320 const discrete_distribution<_IT>& __x)
6321{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006322 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006323 typedef basic_ostream<_CharT, _Traits> _OStream;
6324 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6325 _OStream::scientific);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006326 _CharT __sp = __os.widen(' ');
6327 __os.fill(__sp);
6328 size_t __n = __x.__p_.__p_.size();
6329 __os << __n;
6330 for (size_t __i = 0; __i < __n; ++__i)
6331 __os << __sp << __x.__p_.__p_[__i];
6332 return __os;
6333}
6334
6335template <class _CharT, class _Traits, class _IT>
6336basic_istream<_CharT, _Traits>&
6337operator>>(basic_istream<_CharT, _Traits>& __is,
6338 discrete_distribution<_IT>& __x)
6339{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006340 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006341 typedef basic_istream<_CharT, _Traits> _Istream;
6342 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006343 size_t __n;
6344 __is >> __n;
Howard Hinnant481ba382010-05-20 15:11:46 +00006345 vector<double> __p(__n);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006346 for (size_t __i = 0; __i < __n; ++__i)
6347 __is >> __p[__i];
6348 if (!__is.fail())
6349 swap(__x.__p_.__p_, __p);
6350 return __is;
6351}
6352
Howard Hinnant481ba382010-05-20 15:11:46 +00006353// piecewise_constant_distribution
6354
6355template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006356class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution
Howard Hinnant481ba382010-05-20 15:11:46 +00006357{
6358public:
6359 // types
6360 typedef _RealType result_type;
6361
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006362 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant481ba382010-05-20 15:11:46 +00006363 {
Howard Hinnant481ba382010-05-20 15:11:46 +00006364 vector<result_type> __b_;
Howard Hinnant167159f2010-11-18 17:01:36 +00006365 vector<result_type> __densities_;
6366 vector<result_type> __areas_;
Howard Hinnant481ba382010-05-20 15:11:46 +00006367 public:
6368 typedef piecewise_constant_distribution distribution_type;
6369
6370 param_type();
6371 template<class _InputIteratorB, class _InputIteratorW>
6372 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6373 _InputIteratorW __fW);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006374#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006375 template<class _UnaryOperation>
6376 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006377#endif // _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006378 template<class _UnaryOperation>
6379 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6380 _UnaryOperation __fw);
Eric Fiselierb9f37ca2019-12-12 20:48:11 -05006381 param_type(param_type const&) = default;
Howard Hinnant71c410b2010-10-13 14:37:09 +00006382 param_type & operator=(const param_type& __rhs);
Howard Hinnant481ba382010-05-20 15:11:46 +00006383
Howard Hinnantf5f99992010-09-22 18:02:38 +00006384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006385 vector<result_type> intervals() const {return __b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006387 vector<result_type> densities() const {return __densities_;}
Howard Hinnant481ba382010-05-20 15:11:46 +00006388
Howard Hinnantf5f99992010-09-22 18:02:38 +00006389 friend _LIBCPP_INLINE_VISIBILITY
6390 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006391 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006392 friend _LIBCPP_INLINE_VISIBILITY
6393 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant481ba382010-05-20 15:11:46 +00006394 {return !(__x == __y);}
6395
6396 private:
6397 void __init();
6398
6399 friend class piecewise_constant_distribution;
6400
6401 template <class _CharT, class _Traits, class _RT>
6402 friend
6403 basic_ostream<_CharT, _Traits>&
6404 operator<<(basic_ostream<_CharT, _Traits>& __os,
6405 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006406
Howard Hinnant481ba382010-05-20 15:11:46 +00006407 template <class _CharT, class _Traits, class _RT>
6408 friend
6409 basic_istream<_CharT, _Traits>&
6410 operator>>(basic_istream<_CharT, _Traits>& __is,
6411 piecewise_constant_distribution<_RT>& __x);
6412 };
6413
6414private:
6415 param_type __p_;
6416
6417public:
6418 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006420 piecewise_constant_distribution() {}
6421 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006423 piecewise_constant_distribution(_InputIteratorB __fB,
6424 _InputIteratorB __lB,
6425 _InputIteratorW __fW)
6426 : __p_(__fB, __lB, __fW) {}
6427
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006428#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006429 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006431 piecewise_constant_distribution(initializer_list<result_type> __bl,
6432 _UnaryOperation __fw)
6433 : __p_(__bl, __fw) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006434#endif // _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006435
6436 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006438 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6439 result_type __xmax, _UnaryOperation __fw)
6440 : __p_(__nw, __xmin, __xmax, __fw) {}
6441
Howard Hinnantf5f99992010-09-22 18:02:38 +00006442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006443 explicit piecewise_constant_distribution(const param_type& __p)
6444 : __p_(__p) {}
6445
Howard Hinnantf5f99992010-09-22 18:02:38 +00006446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006447 void reset() {}
6448
6449 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006450 template<class _URNG>
6451 _LIBCPP_INLINE_VISIBILITY
6452 result_type operator()(_URNG& __g)
Howard Hinnant481ba382010-05-20 15:11:46 +00006453 {return (*this)(__g, __p_);}
6454 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6455
6456 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006458 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006460 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant481ba382010-05-20 15:11:46 +00006461
Howard Hinnantf5f99992010-09-22 18:02:38 +00006462 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006463 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006465 void param(const param_type& __p) {__p_ = __p;}
6466
Howard Hinnantf5f99992010-09-22 18:02:38 +00006467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006468 result_type min() const {return __p_.__b_.front();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006470 result_type max() const {return __p_.__b_.back();}
6471
Howard Hinnantf5f99992010-09-22 18:02:38 +00006472 friend _LIBCPP_INLINE_VISIBILITY
6473 bool operator==(const piecewise_constant_distribution& __x,
6474 const piecewise_constant_distribution& __y)
Howard Hinnant481ba382010-05-20 15:11:46 +00006475 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006476 friend _LIBCPP_INLINE_VISIBILITY
6477 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnant481ba382010-05-20 15:11:46 +00006478 const piecewise_constant_distribution& __y)
6479 {return !(__x == __y);}
6480
6481 template <class _CharT, class _Traits, class _RT>
6482 friend
6483 basic_ostream<_CharT, _Traits>&
6484 operator<<(basic_ostream<_CharT, _Traits>& __os,
6485 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006486
Howard Hinnant481ba382010-05-20 15:11:46 +00006487 template <class _CharT, class _Traits, class _RT>
6488 friend
6489 basic_istream<_CharT, _Traits>&
6490 operator>>(basic_istream<_CharT, _Traits>& __is,
6491 piecewise_constant_distribution<_RT>& __x);
6492};
6493
6494template<class _RealType>
Howard Hinnant71c410b2010-10-13 14:37:09 +00006495typename piecewise_constant_distribution<_RealType>::param_type &
6496piecewise_constant_distribution<_RealType>::param_type::operator=
6497 (const param_type& __rhs)
6498{
6499// These can throw
6500 __b_.reserve (__rhs.__b_.size ());
6501 __densities_.reserve(__rhs.__densities_.size());
6502 __areas_.reserve (__rhs.__areas_.size());
6503
6504// These can not throw
6505 __b_ = __rhs.__b_;
6506 __densities_ = __rhs.__densities_;
6507 __areas_ = __rhs.__areas_;
6508 return *this;
6509}
6510
6511template<class _RealType>
Howard Hinnant481ba382010-05-20 15:11:46 +00006512void
6513piecewise_constant_distribution<_RealType>::param_type::__init()
6514{
Howard Hinnant35d83df2010-05-24 00:35:40 +00006515 // __densities_ contains non-normalized areas
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006516 result_type __total_area = _VSTD::accumulate(__densities_.begin(),
Howard Hinnant35d83df2010-05-24 00:35:40 +00006517 __densities_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006518 result_type());
Howard Hinnant35d83df2010-05-24 00:35:40 +00006519 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6520 __densities_[__i] /= __total_area;
6521 // __densities_ contains normalized areas
Howard Hinnant167159f2010-11-18 17:01:36 +00006522 __areas_.assign(__densities_.size(), result_type());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006523 _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
Howard Hinnant35d83df2010-05-24 00:35:40 +00006524 __areas_.begin() + 1);
6525 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6526 __densities_.back() = 1 - __areas_.back(); // correct round off error
6527 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6528 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6529 // __densities_ now contains __densities_
Howard Hinnant481ba382010-05-20 15:11:46 +00006530}
6531
6532template<class _RealType>
6533piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant35d83df2010-05-24 00:35:40 +00006534 : __b_(2),
Howard Hinnanta001ef32010-05-25 00:27:34 +00006535 __densities_(1, 1.0),
6536 __areas_(1, 0.0)
Howard Hinnant481ba382010-05-20 15:11:46 +00006537{
6538 __b_[1] = 1;
6539}
6540
6541template<class _RealType>
6542template<class _InputIteratorB, class _InputIteratorW>
6543piecewise_constant_distribution<_RealType>::param_type::param_type(
6544 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6545 : __b_(__fB, __lB)
6546{
6547 if (__b_.size() < 2)
6548 {
6549 __b_.resize(2);
6550 __b_[0] = 0;
6551 __b_[1] = 1;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006552 __densities_.assign(1, 1.0);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006553 __areas_.assign(1, 0.0);
Howard Hinnant481ba382010-05-20 15:11:46 +00006554 }
6555 else
6556 {
Howard Hinnant35d83df2010-05-24 00:35:40 +00006557 __densities_.reserve(__b_.size() - 1);
Howard Hinnant481ba382010-05-20 15:11:46 +00006558 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006559 __densities_.push_back(*__fW);
Howard Hinnant481ba382010-05-20 15:11:46 +00006560 __init();
6561 }
6562}
6563
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006564#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006565
Howard Hinnant481ba382010-05-20 15:11:46 +00006566template<class _RealType>
6567template<class _UnaryOperation>
6568piecewise_constant_distribution<_RealType>::param_type::param_type(
6569 initializer_list<result_type> __bl, _UnaryOperation __fw)
6570 : __b_(__bl.begin(), __bl.end())
6571{
6572 if (__b_.size() < 2)
6573 {
6574 __b_.resize(2);
6575 __b_[0] = 0;
6576 __b_[1] = 1;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006577 __densities_.assign(1, 1.0);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006578 __areas_.assign(1, 0.0);
Howard Hinnant481ba382010-05-20 15:11:46 +00006579 }
6580 else
6581 {
Howard Hinnant35d83df2010-05-24 00:35:40 +00006582 __densities_.reserve(__b_.size() - 1);
Howard Hinnant481ba382010-05-20 15:11:46 +00006583 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006584 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnant481ba382010-05-20 15:11:46 +00006585 __init();
6586 }
6587}
6588
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006589#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006590
Howard Hinnant481ba382010-05-20 15:11:46 +00006591template<class _RealType>
6592template<class _UnaryOperation>
6593piecewise_constant_distribution<_RealType>::param_type::param_type(
6594 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6595 : __b_(__nw == 0 ? 2 : __nw + 1)
6596{
6597 size_t __n = __b_.size() - 1;
6598 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006599 __densities_.reserve(__n);
Howard Hinnant481ba382010-05-20 15:11:46 +00006600 for (size_t __i = 0; __i < __n; ++__i)
6601 {
6602 __b_[__i] = __xmin + __i * __d;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006603 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnant481ba382010-05-20 15:11:46 +00006604 }
6605 __b_[__n] = __xmax;
6606 __init();
6607}
6608
6609template<class _RealType>
Howard Hinnant481ba382010-05-20 15:11:46 +00006610template<class _URNG>
6611_RealType
6612piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6613{
6614 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnant481ba382010-05-20 15:11:46 +00006615 result_type __u = _Gen()(__g);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006616 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006617 __u) - __p.__areas_.begin() - 1;
6618 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnant481ba382010-05-20 15:11:46 +00006619}
6620
6621template <class _CharT, class _Traits, class _RT>
6622basic_ostream<_CharT, _Traits>&
6623operator<<(basic_ostream<_CharT, _Traits>& __os,
6624 const piecewise_constant_distribution<_RT>& __x)
6625{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006626 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006627 typedef basic_ostream<_CharT, _Traits> _OStream;
6628 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6629 _OStream::scientific);
Howard Hinnant481ba382010-05-20 15:11:46 +00006630 _CharT __sp = __os.widen(' ');
6631 __os.fill(__sp);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006632 size_t __n = __x.__p_.__b_.size();
Howard Hinnant481ba382010-05-20 15:11:46 +00006633 __os << __n;
6634 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006635 __os << __sp << __x.__p_.__b_[__i];
6636 __n = __x.__p_.__densities_.size();
Howard Hinnant481ba382010-05-20 15:11:46 +00006637 __os << __sp << __n;
6638 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006639 __os << __sp << __x.__p_.__densities_[__i];
6640 __n = __x.__p_.__areas_.size();
6641 __os << __sp << __n;
6642 for (size_t __i = 0; __i < __n; ++__i)
6643 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnant481ba382010-05-20 15:11:46 +00006644 return __os;
6645}
6646
6647template <class _CharT, class _Traits, class _RT>
6648basic_istream<_CharT, _Traits>&
6649operator>>(basic_istream<_CharT, _Traits>& __is,
6650 piecewise_constant_distribution<_RT>& __x)
6651{
6652 typedef piecewise_constant_distribution<_RT> _Eng;
6653 typedef typename _Eng::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00006654 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006655 typedef basic_istream<_CharT, _Traits> _Istream;
6656 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant481ba382010-05-20 15:11:46 +00006657 size_t __n;
6658 __is >> __n;
Howard Hinnant481ba382010-05-20 15:11:46 +00006659 vector<result_type> __b(__n);
6660 for (size_t __i = 0; __i < __n; ++__i)
6661 __is >> __b[__i];
Howard Hinnant35d83df2010-05-24 00:35:40 +00006662 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006663 vector<result_type> __densities(__n);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006664 for (size_t __i = 0; __i < __n; ++__i)
6665 __is >> __densities[__i];
6666 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006667 vector<result_type> __areas(__n);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006668 for (size_t __i = 0; __i < __n; ++__i)
6669 __is >> __areas[__i];
Howard Hinnant481ba382010-05-20 15:11:46 +00006670 if (!__is.fail())
6671 {
Howard Hinnant481ba382010-05-20 15:11:46 +00006672 swap(__x.__p_.__b_, __b);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006673 swap(__x.__p_.__densities_, __densities);
6674 swap(__x.__p_.__areas_, __areas);
Howard Hinnant481ba382010-05-20 15:11:46 +00006675 }
6676 return __is;
6677}
6678
Howard Hinnanta001ef32010-05-25 00:27:34 +00006679// piecewise_linear_distribution
6680
6681template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006682class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution
Howard Hinnanta001ef32010-05-25 00:27:34 +00006683{
6684public:
6685 // types
6686 typedef _RealType result_type;
6687
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006688 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnanta001ef32010-05-25 00:27:34 +00006689 {
Howard Hinnanta001ef32010-05-25 00:27:34 +00006690 vector<result_type> __b_;
Howard Hinnant167159f2010-11-18 17:01:36 +00006691 vector<result_type> __densities_;
6692 vector<result_type> __areas_;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006693 public:
6694 typedef piecewise_linear_distribution distribution_type;
6695
6696 param_type();
6697 template<class _InputIteratorB, class _InputIteratorW>
6698 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6699 _InputIteratorW __fW);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006700#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006701 template<class _UnaryOperation>
6702 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006703#endif // _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006704 template<class _UnaryOperation>
6705 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6706 _UnaryOperation __fw);
Eric Fiselierb9f37ca2019-12-12 20:48:11 -05006707 param_type(param_type const&) = default;
Howard Hinnant71c410b2010-10-13 14:37:09 +00006708 param_type & operator=(const param_type& __rhs);
Louis Dionne173f29e2019-05-29 16:01:36 +00006709
Howard Hinnantf5f99992010-09-22 18:02:38 +00006710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006711 vector<result_type> intervals() const {return __b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006713 vector<result_type> densities() const {return __densities_;}
Howard Hinnanta001ef32010-05-25 00:27:34 +00006714
Howard Hinnantf5f99992010-09-22 18:02:38 +00006715 friend _LIBCPP_INLINE_VISIBILITY
6716 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006717 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006718 friend _LIBCPP_INLINE_VISIBILITY
6719 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006720 {return !(__x == __y);}
6721
6722 private:
6723 void __init();
6724
6725 friend class piecewise_linear_distribution;
6726
6727 template <class _CharT, class _Traits, class _RT>
6728 friend
6729 basic_ostream<_CharT, _Traits>&
6730 operator<<(basic_ostream<_CharT, _Traits>& __os,
6731 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006732
Howard Hinnanta001ef32010-05-25 00:27:34 +00006733 template <class _CharT, class _Traits, class _RT>
6734 friend
6735 basic_istream<_CharT, _Traits>&
6736 operator>>(basic_istream<_CharT, _Traits>& __is,
6737 piecewise_linear_distribution<_RT>& __x);
6738 };
6739
6740private:
6741 param_type __p_;
6742
6743public:
6744 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006746 piecewise_linear_distribution() {}
6747 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006749 piecewise_linear_distribution(_InputIteratorB __fB,
6750 _InputIteratorB __lB,
6751 _InputIteratorW __fW)
6752 : __p_(__fB, __lB, __fW) {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006753
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006754#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006755 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006757 piecewise_linear_distribution(initializer_list<result_type> __bl,
6758 _UnaryOperation __fw)
6759 : __p_(__bl, __fw) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006760#endif // _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006761
6762 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006764 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6765 result_type __xmax, _UnaryOperation __fw)
6766 : __p_(__nw, __xmin, __xmax, __fw) {}
6767
Howard Hinnantf5f99992010-09-22 18:02:38 +00006768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006769 explicit piecewise_linear_distribution(const param_type& __p)
6770 : __p_(__p) {}
6771
Howard Hinnantf5f99992010-09-22 18:02:38 +00006772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006773 void reset() {}
6774
6775 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006776 template<class _URNG>
6777 _LIBCPP_INLINE_VISIBILITY
6778 result_type operator()(_URNG& __g)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006779 {return (*this)(__g, __p_);}
6780 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6781
6782 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006784 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006786 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnanta001ef32010-05-25 00:27:34 +00006787
Howard Hinnantf5f99992010-09-22 18:02:38 +00006788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006789 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006791 void param(const param_type& __p) {__p_ = __p;}
6792
Howard Hinnantf5f99992010-09-22 18:02:38 +00006793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006794 result_type min() const {return __p_.__b_.front();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006796 result_type max() const {return __p_.__b_.back();}
6797
Howard Hinnantf5f99992010-09-22 18:02:38 +00006798 friend _LIBCPP_INLINE_VISIBILITY
6799 bool operator==(const piecewise_linear_distribution& __x,
6800 const piecewise_linear_distribution& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006801 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006802 friend _LIBCPP_INLINE_VISIBILITY
6803 bool operator!=(const piecewise_linear_distribution& __x,
6804 const piecewise_linear_distribution& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006805 {return !(__x == __y);}
6806
6807 template <class _CharT, class _Traits, class _RT>
6808 friend
6809 basic_ostream<_CharT, _Traits>&
6810 operator<<(basic_ostream<_CharT, _Traits>& __os,
6811 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006812
Howard Hinnanta001ef32010-05-25 00:27:34 +00006813 template <class _CharT, class _Traits, class _RT>
6814 friend
6815 basic_istream<_CharT, _Traits>&
6816 operator>>(basic_istream<_CharT, _Traits>& __is,
6817 piecewise_linear_distribution<_RT>& __x);
6818};
6819
6820template<class _RealType>
Howard Hinnant71c410b2010-10-13 14:37:09 +00006821typename piecewise_linear_distribution<_RealType>::param_type &
6822piecewise_linear_distribution<_RealType>::param_type::operator=
6823 (const param_type& __rhs)
6824{
6825// These can throw
6826 __b_.reserve (__rhs.__b_.size ());
6827 __densities_.reserve(__rhs.__densities_.size());
6828 __areas_.reserve (__rhs.__areas_.size());
6829
6830// These can not throw
6831 __b_ = __rhs.__b_;
6832 __densities_ = __rhs.__densities_;
6833 __areas_ = __rhs.__areas_;
6834 return *this;
6835}
6836
6837
6838template<class _RealType>
Howard Hinnanta001ef32010-05-25 00:27:34 +00006839void
6840piecewise_linear_distribution<_RealType>::param_type::__init()
6841{
Howard Hinnant167159f2010-11-18 17:01:36 +00006842 __areas_.assign(__densities_.size() - 1, result_type());
Howard Hinnantc834c512011-11-29 18:15:50 +00006843 result_type _Sp = 0;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006844 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6845 {
6846 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6847 (__b_[__i+1] - __b_[__i]) * .5;
Howard Hinnantc834c512011-11-29 18:15:50 +00006848 _Sp += __areas_[__i];
Howard Hinnanta001ef32010-05-25 00:27:34 +00006849 }
6850 for (size_t __i = __areas_.size(); __i > 1;)
6851 {
6852 --__i;
Howard Hinnantc834c512011-11-29 18:15:50 +00006853 __areas_[__i] = __areas_[__i-1] / _Sp;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006854 }
6855 __areas_[0] = 0;
6856 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6857 __areas_[__i] += __areas_[__i-1];
6858 for (size_t __i = 0; __i < __densities_.size(); ++__i)
Howard Hinnantc834c512011-11-29 18:15:50 +00006859 __densities_[__i] /= _Sp;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006860}
6861
6862template<class _RealType>
6863piecewise_linear_distribution<_RealType>::param_type::param_type()
6864 : __b_(2),
6865 __densities_(2, 1.0),
6866 __areas_(1, 0.0)
6867{
6868 __b_[1] = 1;
6869}
6870
6871template<class _RealType>
6872template<class _InputIteratorB, class _InputIteratorW>
6873piecewise_linear_distribution<_RealType>::param_type::param_type(
6874 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6875 : __b_(__fB, __lB)
6876{
6877 if (__b_.size() < 2)
6878 {
6879 __b_.resize(2);
6880 __b_[0] = 0;
6881 __b_[1] = 1;
6882 __densities_.assign(2, 1.0);
6883 __areas_.assign(1, 0.0);
6884 }
6885 else
6886 {
6887 __densities_.reserve(__b_.size());
6888 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6889 __densities_.push_back(*__fW);
6890 __init();
6891 }
6892}
6893
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006894#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006895
Howard Hinnanta001ef32010-05-25 00:27:34 +00006896template<class _RealType>
6897template<class _UnaryOperation>
6898piecewise_linear_distribution<_RealType>::param_type::param_type(
6899 initializer_list<result_type> __bl, _UnaryOperation __fw)
6900 : __b_(__bl.begin(), __bl.end())
6901{
6902 if (__b_.size() < 2)
6903 {
6904 __b_.resize(2);
6905 __b_[0] = 0;
6906 __b_[1] = 1;
6907 __densities_.assign(2, 1.0);
6908 __areas_.assign(1, 0.0);
6909 }
6910 else
6911 {
6912 __densities_.reserve(__b_.size());
6913 for (size_t __i = 0; __i < __b_.size(); ++__i)
6914 __densities_.push_back(__fw(__b_[__i]));
6915 __init();
6916 }
6917}
6918
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006919#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006920
Howard Hinnanta001ef32010-05-25 00:27:34 +00006921template<class _RealType>
6922template<class _UnaryOperation>
6923piecewise_linear_distribution<_RealType>::param_type::param_type(
6924 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6925 : __b_(__nw == 0 ? 2 : __nw + 1)
6926{
6927 size_t __n = __b_.size() - 1;
6928 result_type __d = (__xmax - __xmin) / __n;
6929 __densities_.reserve(__b_.size());
6930 for (size_t __i = 0; __i < __n; ++__i)
6931 {
6932 __b_[__i] = __xmin + __i * __d;
6933 __densities_.push_back(__fw(__b_[__i]));
6934 }
6935 __b_[__n] = __xmax;
6936 __densities_.push_back(__fw(__b_[__n]));
6937 __init();
6938}
6939
6940template<class _RealType>
6941template<class _URNG>
6942_RealType
6943piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6944{
6945 typedef uniform_real_distribution<result_type> _Gen;
6946 result_type __u = _Gen()(__g);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006947 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006948 __u) - __p.__areas_.begin() - 1;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006949 __u -= __p.__areas_[__k];
Howard Hinnant167159f2010-11-18 17:01:36 +00006950 const result_type __dk = __p.__densities_[__k];
6951 const result_type __dk1 = __p.__densities_[__k+1];
6952 const result_type __deltad = __dk1 - __dk;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006953 const result_type __bk = __p.__b_[__k];
6954 if (__deltad == 0)
Howard Hinnant167159f2010-11-18 17:01:36 +00006955 return __u / __dk + __bk;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006956 const result_type __bk1 = __p.__b_[__k+1];
6957 const result_type __deltab = __bk1 - __bk;
Howard Hinnant167159f2010-11-18 17:01:36 +00006958 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006959 _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant167159f2010-11-18 17:01:36 +00006960 __deltad;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006961}
6962
6963template <class _CharT, class _Traits, class _RT>
6964basic_ostream<_CharT, _Traits>&
6965operator<<(basic_ostream<_CharT, _Traits>& __os,
6966 const piecewise_linear_distribution<_RT>& __x)
6967{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006968 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006969 typedef basic_ostream<_CharT, _Traits> _OStream;
6970 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6971 _OStream::scientific);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006972 _CharT __sp = __os.widen(' ');
6973 __os.fill(__sp);
6974 size_t __n = __x.__p_.__b_.size();
6975 __os << __n;
6976 for (size_t __i = 0; __i < __n; ++__i)
6977 __os << __sp << __x.__p_.__b_[__i];
6978 __n = __x.__p_.__densities_.size();
6979 __os << __sp << __n;
6980 for (size_t __i = 0; __i < __n; ++__i)
6981 __os << __sp << __x.__p_.__densities_[__i];
6982 __n = __x.__p_.__areas_.size();
6983 __os << __sp << __n;
6984 for (size_t __i = 0; __i < __n; ++__i)
6985 __os << __sp << __x.__p_.__areas_[__i];
6986 return __os;
6987}
6988
6989template <class _CharT, class _Traits, class _RT>
6990basic_istream<_CharT, _Traits>&
6991operator>>(basic_istream<_CharT, _Traits>& __is,
6992 piecewise_linear_distribution<_RT>& __x)
6993{
6994 typedef piecewise_linear_distribution<_RT> _Eng;
6995 typedef typename _Eng::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00006996 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006997 typedef basic_istream<_CharT, _Traits> _Istream;
6998 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006999 size_t __n;
7000 __is >> __n;
7001 vector<result_type> __b(__n);
7002 for (size_t __i = 0; __i < __n; ++__i)
7003 __is >> __b[__i];
7004 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00007005 vector<result_type> __densities(__n);
Howard Hinnanta001ef32010-05-25 00:27:34 +00007006 for (size_t __i = 0; __i < __n; ++__i)
7007 __is >> __densities[__i];
7008 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00007009 vector<result_type> __areas(__n);
Howard Hinnanta001ef32010-05-25 00:27:34 +00007010 for (size_t __i = 0; __i < __n; ++__i)
7011 __is >> __areas[__i];
7012 if (!__is.fail())
7013 {
7014 swap(__x.__p_.__b_, __b);
7015 swap(__x.__p_.__densities_, __densities);
7016 swap(__x.__p_.__areas_, __areas);
7017 }
7018 return __is;
7019}
7020
Howard Hinnantc51e1022010-05-11 19:42:16 +00007021_LIBCPP_END_NAMESPACE_STD
7022
Eric Fiselierf4433a32017-05-31 22:07:49 +00007023_LIBCPP_POP_MACROS
7024
Howard Hinnantc51e1022010-05-11 19:42:16 +00007025#endif // _LIBCPP_RANDOM