blob: 3ef246b7c8f766725aa15dc3c270eddfb267be6a [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{
Christopher Di Bella68074072021-02-17 02:52:17 +000020// [rand.req.urng], uniform random bit generator requirements
21template<class G>
22concept uniform_random_bit_generator = see below; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000023
24// Engines
25
26template <class UIntType, UIntType a, UIntType c, UIntType m>
27class linear_congruential_engine
28{
29public:
30 // types
31 typedef UIntType result_type;
32
33 // engine characteristics
34 static constexpr result_type multiplier = a;
35 static constexpr result_type increment = c;
36 static constexpr result_type modulus = m;
37 static constexpr result_type min() { return c == 0u ? 1u: 0u;}
38 static constexpr result_type max() { return m - 1u;}
39 static constexpr result_type default_seed = 1u;
40
41 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +010042 explicit linear_congruential_engine(result_type s = default_seed); // before C++20
43 linear_congruential_engine() : linear_congruential_engine(default_seed) {} // C++20
44 explicit linear_congruential_engine(result_type s); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000045 template<class Sseq> explicit linear_congruential_engine(Sseq& q);
46 void seed(result_type s = default_seed);
47 template<class Sseq> void seed(Sseq& q);
48
49 // generating functions
50 result_type operator()();
51 void discard(unsigned long long z);
52};
53
54template <class UIntType, UIntType a, UIntType c, UIntType m>
55bool
56operator==(const linear_congruential_engine<UIntType, a, c, m>& x,
57 const linear_congruential_engine<UIntType, a, c, m>& y);
58
59template <class UIntType, UIntType a, UIntType c, UIntType m>
60bool
61operator!=(const linear_congruential_engine<UIntType, a, c, m>& x,
62 const linear_congruential_engine<UIntType, a, c, m>& y);
63
64template <class charT, class traits,
65 class UIntType, UIntType a, UIntType c, UIntType m>
66basic_ostream<charT, traits>&
67operator<<(basic_ostream<charT, traits>& os,
68 const linear_congruential_engine<UIntType, a, c, m>& x);
69
70template <class charT, class traits,
71 class UIntType, UIntType a, UIntType c, UIntType m>
72basic_istream<charT, traits>&
73operator>>(basic_istream<charT, traits>& is,
74 linear_congruential_engine<UIntType, a, c, m>& x);
75
76template <class UIntType, size_t w, size_t n, size_t m, size_t r,
77 UIntType a, size_t u, UIntType d, size_t s,
78 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
79class mersenne_twister_engine
80{
81public:
82 // types
83 typedef UIntType result_type;
84
85 // engine characteristics
86 static constexpr size_t word_size = w;
87 static constexpr size_t state_size = n;
88 static constexpr size_t shift_size = m;
89 static constexpr size_t mask_bits = r;
90 static constexpr result_type xor_mask = a;
91 static constexpr size_t tempering_u = u;
92 static constexpr result_type tempering_d = d;
93 static constexpr size_t tempering_s = s;
94 static constexpr result_type tempering_b = b;
95 static constexpr size_t tempering_t = t;
96 static constexpr result_type tempering_c = c;
97 static constexpr size_t tempering_l = l;
98 static constexpr result_type initialization_multiplier = f;
99 static constexpr result_type min () { return 0; }
100 static constexpr result_type max() { return 2^w - 1; }
101 static constexpr result_type default_seed = 5489u;
102
103 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100104 explicit mersenne_twister_engine(result_type s = default_seed); // before C++20
105 mersenne_twister_engine() : mersenne_twister_engine(default_seed) {} // C++20
106 explicit mersenne_twister_engine(result_type s); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000107 template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
108 void seed(result_type value = default_seed);
109 template<class Sseq> void seed(Sseq& q);
110
111 // generating functions
112 result_type operator()();
113 void discard(unsigned long long z);
114};
115
116template <class UIntType, size_t w, size_t n, size_t m, size_t r,
117 UIntType a, size_t u, UIntType d, size_t s,
118 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
119bool
120operator==(
121 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
122 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
123
124template <class UIntType, size_t w, size_t n, size_t m, size_t r,
125 UIntType a, size_t u, UIntType d, size_t s,
126 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
127bool
128operator!=(
129 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
130 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
131
132template <class charT, class traits,
133 class UIntType, size_t w, size_t n, size_t m, size_t r,
134 UIntType a, size_t u, UIntType d, size_t s,
135 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
136basic_ostream<charT, traits>&
137operator<<(basic_ostream<charT, traits>& os,
138 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
139
140template <class charT, class traits,
141 class UIntType, size_t w, size_t n, size_t m, size_t r,
142 UIntType a, size_t u, UIntType d, size_t s,
143 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
144basic_istream<charT, traits>&
145operator>>(basic_istream<charT, traits>& is,
146 mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
147
148template<class UIntType, size_t w, size_t s, size_t r>
149class subtract_with_carry_engine
150{
151public:
152 // types
153 typedef UIntType result_type;
154
155 // engine characteristics
156 static constexpr size_t word_size = w;
157 static constexpr size_t short_lag = s;
158 static constexpr size_t long_lag = r;
159 static constexpr result_type min() { return 0; }
160 static constexpr result_type max() { return m-1; }
161 static constexpr result_type default_seed = 19780503u;
162
163 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100164 explicit subtract_with_carry_engine(result_type value = default_seed); // before C++20
165 subtract_with_carry_engine() : subtract_with_carry_engine(default_seed) {} // C++20
166 explicit subtract_with_carry_engine(result_type value); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167 template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
168 void seed(result_type value = default_seed);
169 template<class Sseq> void seed(Sseq& q);
170
171 // generating functions
172 result_type operator()();
173 void discard(unsigned long long z);
174};
175
176template<class UIntType, size_t w, size_t s, size_t r>
177bool
178operator==(
179 const subtract_with_carry_engine<UIntType, w, s, r>& x,
180 const subtract_with_carry_engine<UIntType, w, s, r>& y);
181
182template<class UIntType, size_t w, size_t s, size_t r>
183bool
184operator!=(
185 const subtract_with_carry_engine<UIntType, w, s, r>& x,
186 const subtract_with_carry_engine<UIntType, w, s, r>& y);
187
188template <class charT, class traits,
189 class UIntType, size_t w, size_t s, size_t r>
190basic_ostream<charT, traits>&
191operator<<(basic_ostream<charT, traits>& os,
192 const subtract_with_carry_engine<UIntType, w, s, r>& x);
193
194template <class charT, class traits,
195 class UIntType, size_t w, size_t s, size_t r>
196basic_istream<charT, traits>&
197operator>>(basic_istream<charT, traits>& is,
198 subtract_with_carry_engine<UIntType, w, s, r>& x);
199
200template<class Engine, size_t p, size_t r>
201class discard_block_engine
202{
203public:
204 // types
205 typedef typename Engine::result_type result_type;
206
207 // engine characteristics
208 static constexpr size_t block_size = p;
209 static constexpr size_t used_block = r;
210 static constexpr result_type min() { return Engine::min(); }
211 static constexpr result_type max() { return Engine::max(); }
212
213 // constructors and seeding functions
214 discard_block_engine();
215 explicit discard_block_engine(const Engine& e);
216 explicit discard_block_engine(Engine&& e);
217 explicit discard_block_engine(result_type s);
218 template<class Sseq> explicit discard_block_engine(Sseq& q);
219 void seed();
220 void seed(result_type s);
221 template<class Sseq> void seed(Sseq& q);
222
223 // generating functions
224 result_type operator()();
225 void discard(unsigned long long z);
226
227 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000228 const Engine& base() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229};
230
231template<class Engine, size_t p, size_t r>
232bool
233operator==(
234 const discard_block_engine<Engine, p, r>& x,
235 const discard_block_engine<Engine, p, r>& y);
236
237template<class Engine, size_t p, size_t r>
238bool
239operator!=(
240 const discard_block_engine<Engine, p, r>& x,
241 const discard_block_engine<Engine, p, r>& y);
242
243template <class charT, class traits,
244 class Engine, size_t p, size_t r>
245basic_ostream<charT, traits>&
246operator<<(basic_ostream<charT, traits>& os,
247 const discard_block_engine<Engine, p, r>& x);
248
249template <class charT, class traits,
250 class Engine, size_t p, size_t r>
251basic_istream<charT, traits>&
252operator>>(basic_istream<charT, traits>& is,
253 discard_block_engine<Engine, p, r>& x);
254
255template<class Engine, size_t w, class UIntType>
256class independent_bits_engine
257{
258public:
259 // types
260 typedef UIntType result_type;
261
262 // engine characteristics
263 static constexpr result_type min() { return 0; }
264 static constexpr result_type max() { return 2^w - 1; }
265
266 // constructors and seeding functions
267 independent_bits_engine();
268 explicit independent_bits_engine(const Engine& e);
269 explicit independent_bits_engine(Engine&& e);
270 explicit independent_bits_engine(result_type s);
271 template<class Sseq> explicit independent_bits_engine(Sseq& q);
272 void seed();
273 void seed(result_type s);
274 template<class Sseq> void seed(Sseq& q);
275
276 // generating functions
277 result_type operator()(); void discard(unsigned long long z);
278
279 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000280 const Engine& base() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281};
282
283template<class Engine, size_t w, class UIntType>
284bool
285operator==(
286 const independent_bits_engine<Engine, w, UIntType>& x,
287 const independent_bits_engine<Engine, w, UIntType>& y);
288
289template<class Engine, size_t w, class UIntType>
290bool
291operator!=(
292 const independent_bits_engine<Engine, w, UIntType>& x,
293 const independent_bits_engine<Engine, w, UIntType>& y);
294
295template <class charT, class traits,
296 class Engine, size_t w, class UIntType>
297basic_ostream<charT, traits>&
298operator<<(basic_ostream<charT, traits>& os,
299 const independent_bits_engine<Engine, w, UIntType>& x);
300
301template <class charT, class traits,
302 class Engine, size_t w, class UIntType>
303basic_istream<charT, traits>&
304operator>>(basic_istream<charT, traits>& is,
305 independent_bits_engine<Engine, w, UIntType>& x);
306
307template<class Engine, size_t k>
308class shuffle_order_engine
309{
310public:
311 // types
312 typedef typename Engine::result_type result_type;
313
314 // engine characteristics
315 static constexpr size_t table_size = k;
316 static constexpr result_type min() { return Engine::min; }
317 static constexpr result_type max() { return Engine::max; }
318
319 // constructors and seeding functions
320 shuffle_order_engine();
321 explicit shuffle_order_engine(const Engine& e);
322 explicit shuffle_order_engine(Engine&& e);
323 explicit shuffle_order_engine(result_type s);
324 template<class Sseq> explicit shuffle_order_engine(Sseq& q);
325 void seed();
326 void seed(result_type s);
327 template<class Sseq> void seed(Sseq& q);
328
329 // generating functions
330 result_type operator()();
331 void discard(unsigned long long z);
332
333 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000334 const Engine& base() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000335};
336
337template<class Engine, size_t k>
338bool
339operator==(
340 const shuffle_order_engine<Engine, k>& x,
341 const shuffle_order_engine<Engine, k>& y);
342
343template<class Engine, size_t k>
344bool
345operator!=(
346 const shuffle_order_engine<Engine, k>& x,
347 const shuffle_order_engine<Engine, k>& y);
348
349template <class charT, class traits,
350 class Engine, size_t k>
351basic_ostream<charT, traits>&
352operator<<(basic_ostream<charT, traits>& os,
353 const shuffle_order_engine<Engine, k>& x);
354
355template <class charT, class traits,
356 class Engine, size_t k>
357basic_istream<charT, traits>&
358operator>>(basic_istream<charT, traits>& is,
359 shuffle_order_engine<Engine, k>& x);
360
361typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
362 minstd_rand0;
363typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
364 minstd_rand;
365typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
366 0x9908b0df,
367 11, 0xffffffff,
368 7, 0x9d2c5680,
369 15, 0xefc60000,
370 18, 1812433253> mt19937;
371typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
372 0xb5026f5aa96619e9,
373 29, 0x5555555555555555,
374 17, 0x71d67fffeda60000,
375 37, 0xfff7eee000000000,
376 43, 6364136223846793005> mt19937_64;
377typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
378typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
379typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
380typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
381typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
Howard Hinnant481ba382010-05-20 15:11:46 +0000382typedef minstd_rand default_random_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384// Generators
385
386class random_device
387{
388public:
389 // types
390 typedef unsigned int result_type;
391
392 // generator characteristics
393 static constexpr result_type min() { return numeric_limits<result_type>::min(); }
394 static constexpr result_type max() { return numeric_limits<result_type>::max(); }
395
396 // constructors
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100397 explicit random_device(const string& token = implementation-defined); // before C++20
398 random_device() : random_device(implementation-defined) {} // C++20
399 explicit random_device(const string& token); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400
401 // generating functions
402 result_type operator()();
403
404 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +0000405 double entropy() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407 // no copy functions
408 random_device(const random_device& ) = delete;
409 void operator=(const random_device& ) = delete;
410};
411
412// Utilities
413
414class seed_seq
415{
416public:
417 // types
418 typedef uint_least32_t result_type;
419
420 // constructors
421 seed_seq();
422 template<class T>
423 seed_seq(initializer_list<T> il);
424 template<class InputIterator>
425 seed_seq(InputIterator begin, InputIterator end);
426
427 // generating functions
428 template<class RandomAccessIterator>
429 void generate(RandomAccessIterator begin, RandomAccessIterator end);
430
431 // property functions
432 size_t size() const;
433 template<class OutputIterator>
434 void param(OutputIterator dest) const;
435
436 // no copy functions
437 seed_seq(const seed_seq&) = delete;
438 void operator=(const seed_seq& ) = delete;
439};
440
441template<class RealType, size_t bits, class URNG>
442 RealType generate_canonical(URNG& g);
443
444// Distributions
445
446template<class IntType = int>
447class uniform_int_distribution
448{
449public:
450 // types
451 typedef IntType result_type;
452
453 class param_type
454 {
455 public:
456 typedef uniform_int_distribution distribution_type;
457
458 explicit param_type(IntType a = 0,
459 IntType b = numeric_limits<IntType>::max());
460
461 result_type a() const;
462 result_type b() const;
463
464 friend bool operator==(const param_type& x, const param_type& y);
465 friend bool operator!=(const param_type& x, const param_type& y);
466 };
467
468 // constructors and reset functions
469 explicit uniform_int_distribution(IntType a = 0,
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100470 IntType b = numeric_limits<IntType>::max()); // before C++20
471 uniform_int_distribution() : uniform_int_distribution(0) {} // C++20
472 explicit uniform_int_distribution(IntType a,
473 IntType b = numeric_limits<IntType>::max()); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474 explicit uniform_int_distribution(const param_type& parm);
475 void reset();
476
477 // generating functions
478 template<class URNG> result_type operator()(URNG& g);
479 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
480
481 // property functions
482 result_type a() const;
483 result_type b() const;
484
485 param_type param() const;
486 void param(const param_type& parm);
487
488 result_type min() const;
489 result_type max() const;
490
491 friend bool operator==(const uniform_int_distribution& x,
492 const uniform_int_distribution& y);
493 friend bool operator!=(const uniform_int_distribution& x,
494 const uniform_int_distribution& y);
495
496 template <class charT, class traits>
497 friend
498 basic_ostream<charT, traits>&
499 operator<<(basic_ostream<charT, traits>& os,
500 const uniform_int_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000501
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502 template <class charT, class traits>
503 friend
504 basic_istream<charT, traits>&
505 operator>>(basic_istream<charT, traits>& is,
506 uniform_int_distribution& x);
507};
508
509template<class RealType = double>
510class uniform_real_distribution
511{
512public:
513 // types
514 typedef RealType result_type;
515
516 class param_type
517 {
518 public:
519 typedef uniform_real_distribution distribution_type;
520
521 explicit param_type(RealType a = 0,
522 RealType b = 1);
523
524 result_type a() const;
525 result_type b() const;
526
527 friend bool operator==(const param_type& x, const param_type& y);
528 friend bool operator!=(const param_type& x, const param_type& y);
529 };
530
531 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100532 explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0); // before C++20
533 uniform_real_distribution() : uniform_real_distribution(0.0) {} // C++20
534 explicit uniform_real_distribution(RealType a, RealType b = 1.0); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000535 explicit uniform_real_distribution(const param_type& parm);
536 void reset();
537
538 // generating functions
539 template<class URNG> result_type operator()(URNG& g);
540 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
541
542 // property functions
543 result_type a() const;
544 result_type b() const;
545
546 param_type param() const;
547 void param(const param_type& parm);
548
549 result_type min() const;
550 result_type max() const;
551
552 friend bool operator==(const uniform_real_distribution& x,
553 const uniform_real_distribution& y);
554 friend bool operator!=(const uniform_real_distribution& x,
555 const uniform_real_distribution& y);
556
557 template <class charT, class traits>
558 friend
559 basic_ostream<charT, traits>&
560 operator<<(basic_ostream<charT, traits>& os,
561 const uniform_real_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000562
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563 template <class charT, class traits>
564 friend
565 basic_istream<charT, traits>&
566 operator>>(basic_istream<charT, traits>& is,
567 uniform_real_distribution& x);
568};
569
570class bernoulli_distribution
571{
572public:
573 // types
574 typedef bool result_type;
575
576 class param_type
577 {
578 public:
579 typedef bernoulli_distribution distribution_type;
580
581 explicit param_type(double p = 0.5);
582
583 double p() const;
584
585 friend bool operator==(const param_type& x, const param_type& y);
586 friend bool operator!=(const param_type& x, const param_type& y);
587 };
588
589 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100590 explicit bernoulli_distribution(double p = 0.5); // before C++20
591 bernoulli_distribution() : bernoulli_distribution(0.5) {} // C++20
592 explicit bernoulli_distribution(double p); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593 explicit bernoulli_distribution(const param_type& parm);
594 void reset();
595
596 // generating functions
597 template<class URNG> result_type operator()(URNG& g);
598 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
599
600 // property functions
601 double p() const;
602
603 param_type param() const;
604 void param(const param_type& parm);
605
606 result_type min() const;
607 result_type max() const;
608
609 friend bool operator==(const bernoulli_distribution& x,
610 const bernoulli_distribution& y);
611 friend bool operator!=(const bernoulli_distribution& x,
612 const bernoulli_distribution& y);
613
614 template <class charT, class traits>
615 friend
616 basic_ostream<charT, traits>&
617 operator<<(basic_ostream<charT, traits>& os,
618 const bernoulli_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000619
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620 template <class charT, class traits>
621 friend
622 basic_istream<charT, traits>&
623 operator>>(basic_istream<charT, traits>& is,
624 bernoulli_distribution& x);
625};
626
627template<class IntType = int>
Howard Hinnant53f28fa2010-05-11 23:26:59 +0000628class binomial_distribution
629{
630public:
631 // types
632 typedef IntType result_type;
633
634 class param_type
635 {
636 public:
637 typedef binomial_distribution distribution_type;
638
639 explicit param_type(IntType t = 1, double p = 0.5);
640
641 IntType t() const;
642 double p() const;
643
644 friend bool operator==(const param_type& x, const param_type& y);
645 friend bool operator!=(const param_type& x, const param_type& y);
646 };
647
648 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100649 explicit binomial_distribution(IntType t = 1, double p = 0.5); // before C++20
650 binomial_distribution() : binomial_distribution(1) {} // C++20
651 explicit binomial_distribution(IntType t, double p = 0.5); // C++20
Howard Hinnant53f28fa2010-05-11 23:26:59 +0000652 explicit binomial_distribution(const param_type& parm);
653 void reset();
654
655 // generating functions
656 template<class URNG> result_type operator()(URNG& g);
657 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
658
659 // property functions
660 IntType t() const;
661 double p() const;
662
663 param_type param() const;
664 void param(const param_type& parm);
665
666 result_type min() const;
667 result_type max() const;
668
669 friend bool operator==(const binomial_distribution& x,
670 const binomial_distribution& y);
671 friend bool operator!=(const binomial_distribution& x,
672 const binomial_distribution& y);
673
674 template <class charT, class traits>
675 friend
676 basic_ostream<charT, traits>&
677 operator<<(basic_ostream<charT, traits>& os,
678 const binomial_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000679
Howard Hinnant53f28fa2010-05-11 23:26:59 +0000680 template <class charT, class traits>
681 friend
682 basic_istream<charT, traits>&
683 operator>>(basic_istream<charT, traits>& is,
684 binomial_distribution& x);
685};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686
687template<class IntType = int>
Howard Hinnant28beb162010-05-17 13:44:27 +0000688class geometric_distribution
689{
690public:
691 // types
692 typedef IntType result_type;
693
694 class param_type
695 {
696 public:
697 typedef geometric_distribution distribution_type;
698
699 explicit param_type(double p = 0.5);
700
701 double p() const;
702
703 friend bool operator==(const param_type& x, const param_type& y);
704 friend bool operator!=(const param_type& x, const param_type& y);
705 };
706
707 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100708 explicit geometric_distribution(double p = 0.5); // before C++20
709 geometric_distribution() : geometric_distribution(0.5) {} // C++20
710 explicit geometric_distribution(double p); // C++20
Howard Hinnant28beb162010-05-17 13:44:27 +0000711 explicit geometric_distribution(const param_type& parm);
712 void reset();
713
714 // generating functions
715 template<class URNG> result_type operator()(URNG& g);
716 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
717
718 // property functions
719 double p() const;
720
721 param_type param() const;
722 void param(const param_type& parm);
723
724 result_type min() const;
725 result_type max() const;
726
727 friend bool operator==(const geometric_distribution& x,
728 const geometric_distribution& y);
729 friend bool operator!=(const geometric_distribution& x,
730 const geometric_distribution& y);
731
732 template <class charT, class traits>
733 friend
734 basic_ostream<charT, traits>&
735 operator<<(basic_ostream<charT, traits>& os,
736 const geometric_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000737
Howard Hinnant28beb162010-05-17 13:44:27 +0000738 template <class charT, class traits>
739 friend
740 basic_istream<charT, traits>&
741 operator>>(basic_istream<charT, traits>& is,
742 geometric_distribution& x);
743};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744
745template<class IntType = int>
Howard Hinnant0f43ce62010-05-17 00:09:38 +0000746class negative_binomial_distribution
747{
748public:
749 // types
750 typedef IntType result_type;
751
752 class param_type
753 {
754 public:
755 typedef negative_binomial_distribution distribution_type;
756
757 explicit param_type(result_type k = 1, double p = 0.5);
758
759 result_type k() const;
760 double p() const;
761
762 friend bool operator==(const param_type& x, const param_type& y);
763 friend bool operator!=(const param_type& x, const param_type& y);
764 };
765
766 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100767 explicit negative_binomial_distribution(IntType k = 1, double p = 0.5); // before C++20
768 negative_binomial_distribution() : negative_binomial_distribution(1) {} // C++20
769 explicit negative_binomial_distribution(IntType k, double p = 0.5); // C++20
Howard Hinnant0f43ce62010-05-17 00:09:38 +0000770 explicit negative_binomial_distribution(const param_type& parm);
771 void reset();
772
773 // generating functions
774 template<class URNG> result_type operator()(URNG& g);
775 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
776
777 // property functions
778 result_type k() const;
779 double p() const;
780
781 param_type param() const;
782 void param(const param_type& parm);
783
784 result_type min() const;
785 result_type max() const;
786
787 friend bool operator==(const negative_binomial_distribution& x,
788 const negative_binomial_distribution& y);
789 friend bool operator!=(const negative_binomial_distribution& x,
790 const negative_binomial_distribution& y);
791
792 template <class charT, class traits>
793 friend
794 basic_ostream<charT, traits>&
795 operator<<(basic_ostream<charT, traits>& os,
796 const negative_binomial_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000797
Howard Hinnant0f43ce62010-05-17 00:09:38 +0000798 template <class charT, class traits>
799 friend
800 basic_istream<charT, traits>&
801 operator>>(basic_istream<charT, traits>& is,
802 negative_binomial_distribution& x);
803};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000804
805template<class IntType = int>
Howard Hinnant24a5f2a2010-05-14 21:38:54 +0000806class poisson_distribution
807{
808public:
809 // types
810 typedef IntType result_type;
811
812 class param_type
813 {
814 public:
815 typedef poisson_distribution distribution_type;
816
817 explicit param_type(double mean = 1.0);
818
819 double mean() const;
820
821 friend bool operator==(const param_type& x, const param_type& y);
822 friend bool operator!=(const param_type& x, const param_type& y);
823 };
824
825 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100826 explicit poisson_distribution(double mean = 1.0); // before C++20
827 poisson_distribution() : poisson_distribution(1.0) {} // C++20
828 explicit poisson_distribution(double mean); // C++20
Howard Hinnant24a5f2a2010-05-14 21:38:54 +0000829 explicit poisson_distribution(const param_type& parm);
830 void reset();
831
832 // generating functions
833 template<class URNG> result_type operator()(URNG& g);
834 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
835
836 // property functions
837 double mean() const;
838
839 param_type param() const;
840 void param(const param_type& parm);
841
842 result_type min() const;
843 result_type max() const;
844
845 friend bool operator==(const poisson_distribution& x,
846 const poisson_distribution& y);
847 friend bool operator!=(const poisson_distribution& x,
848 const poisson_distribution& y);
849
850 template <class charT, class traits>
851 friend
852 basic_ostream<charT, traits>&
853 operator<<(basic_ostream<charT, traits>& os,
854 const poisson_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000855
Howard Hinnant24a5f2a2010-05-14 21:38:54 +0000856 template <class charT, class traits>
857 friend
858 basic_istream<charT, traits>&
859 operator>>(basic_istream<charT, traits>& is,
860 poisson_distribution& x);
861};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862
863template<class RealType = double>
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000864class exponential_distribution
865{
866public:
867 // types
868 typedef RealType result_type;
869
870 class param_type
871 {
872 public:
873 typedef exponential_distribution distribution_type;
874
Howard Hinnant0b95bc92010-05-12 21:02:31 +0000875 explicit param_type(result_type lambda = 1.0);
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000876
Howard Hinnant0b95bc92010-05-12 21:02:31 +0000877 result_type lambda() const;
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000878
879 friend bool operator==(const param_type& x, const param_type& y);
880 friend bool operator!=(const param_type& x, const param_type& y);
881 };
882
883 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100884 explicit exponential_distribution(RealType lambda = 1.0); // before C++20
885 exponential_distribution() : exponential_distribution(1.0) {} // C++20
886 explicit exponential_distribution(RealType lambda); // C++20
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000887 explicit exponential_distribution(const param_type& parm);
888 void reset();
889
890 // generating functions
891 template<class URNG> result_type operator()(URNG& g);
892 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
893
894 // property functions
Howard Hinnant0b95bc92010-05-12 21:02:31 +0000895 result_type lambda() const;
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000896
897 param_type param() const;
898 void param(const param_type& parm);
899
900 result_type min() const;
901 result_type max() const;
902
903 friend bool operator==(const exponential_distribution& x,
904 const exponential_distribution& y);
905 friend bool operator!=(const exponential_distribution& x,
906 const exponential_distribution& y);
907
908 template <class charT, class traits>
909 friend
910 basic_ostream<charT, traits>&
911 operator<<(basic_ostream<charT, traits>& os,
912 const exponential_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000913
Howard Hinnantd31dfb52010-05-12 17:08:57 +0000914 template <class charT, class traits>
915 friend
916 basic_istream<charT, traits>&
917 operator>>(basic_istream<charT, traits>& is,
918 exponential_distribution& x);
919};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920
921template<class RealType = double>
Howard Hinnantbb6d2022010-05-13 17:58:28 +0000922class gamma_distribution
923{
924public:
925 // types
926 typedef RealType result_type;
927
928 class param_type
929 {
930 public:
931 typedef gamma_distribution distribution_type;
932
933 explicit param_type(result_type alpha = 1, result_type beta = 1);
934
935 result_type alpha() const;
936 result_type beta() const;
937
938 friend bool operator==(const param_type& x, const param_type& y);
939 friend bool operator!=(const param_type& x, const param_type& y);
940 };
941
942 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100943 explicit gamma_distribution(RealType alpha = 0.0, RealType beta = 1.0); // before C++20
944 gamma_distribution() : gamma_distribution(0.0) {} // C++20
945 explicit gamma_distribution(RealType alpha, RealType beta = 1.0); // C++20
Howard Hinnantbb6d2022010-05-13 17:58:28 +0000946 explicit gamma_distribution(const param_type& parm);
947 void reset();
948
949 // generating functions
950 template<class URNG> result_type operator()(URNG& g);
951 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
952
953 // property functions
954 result_type alpha() const;
955 result_type beta() const;
956
957 param_type param() const;
958 void param(const param_type& parm);
959
960 result_type min() const;
961 result_type max() const;
962
963 friend bool operator==(const gamma_distribution& x,
964 const gamma_distribution& y);
965 friend bool operator!=(const gamma_distribution& x,
966 const gamma_distribution& y);
967
968 template <class charT, class traits>
969 friend
970 basic_ostream<charT, traits>&
971 operator<<(basic_ostream<charT, traits>& os,
972 const gamma_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000973
Howard Hinnantbb6d2022010-05-13 17:58:28 +0000974 template <class charT, class traits>
975 friend
976 basic_istream<charT, traits>&
977 operator>>(basic_istream<charT, traits>& is,
978 gamma_distribution& x);
979};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980
981template<class RealType = double>
Howard Hinnantdb6b97b2010-05-16 01:09:02 +0000982class weibull_distribution
983{
984public:
985 // types
986 typedef RealType result_type;
987
988 class param_type
989 {
990 public:
991 typedef weibull_distribution distribution_type;
992
993 explicit param_type(result_type alpha = 1, result_type beta = 1);
994
995 result_type a() const;
996 result_type b() const;
997
998 friend bool operator==(const param_type& x, const param_type& y);
999 friend bool operator!=(const param_type& x, const param_type& y);
1000 };
1001
1002 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001003 explicit weibull_distribution(RealType a = 1.0, RealType b = 1.0); // before C++20
1004 weibull_distribution() : weibull_distribution(1.0) {} // C++20
1005 explicit weibull_distribution(RealType a, RealType b = 1.0); // C++20
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00001006 explicit weibull_distribution(const param_type& parm);
1007 void reset();
1008
1009 // generating functions
1010 template<class URNG> result_type operator()(URNG& g);
1011 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1012
1013 // property functions
1014 result_type a() const;
1015 result_type b() const;
1016
1017 param_type param() const;
1018 void param(const param_type& parm);
1019
1020 result_type min() const;
1021 result_type max() const;
1022
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00001023 friend bool operator==(const weibull_distribution& x,
1024 const weibull_distribution& y);
1025 friend bool operator!=(const weibull_distribution& x,
1026 const weibull_distribution& y);
1027
1028 template <class charT, class traits>
1029 friend
1030 basic_ostream<charT, traits>&
1031 operator<<(basic_ostream<charT, traits>& os,
1032 const weibull_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001033
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00001034 template <class charT, class traits>
1035 friend
1036 basic_istream<charT, traits>&
1037 operator>>(basic_istream<charT, traits>& is,
1038 weibull_distribution& x);
1039};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040
1041template<class RealType = double>
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00001042class extreme_value_distribution
1043{
1044public:
1045 // types
1046 typedef RealType result_type;
1047
1048 class param_type
1049 {
1050 public:
1051 typedef extreme_value_distribution distribution_type;
1052
1053 explicit param_type(result_type a = 0, result_type b = 1);
1054
1055 result_type a() const;
1056 result_type b() const;
1057
1058 friend bool operator==(const param_type& x, const param_type& y);
1059 friend bool operator!=(const param_type& x, const param_type& y);
1060 };
1061
1062 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001063 explicit extreme_value_distribution(RealType a = 0.0, RealType b = 1.0); // before C++20
1064 extreme_value_distribution() : extreme_value_distribution(0.0) {} // C++20
1065 explicit extreme_value_distribution(RealType a, RealType b = 1.0); // C++20
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00001066 explicit extreme_value_distribution(const param_type& parm);
1067 void reset();
1068
1069 // generating functions
1070 template<class URNG> result_type operator()(URNG& g);
1071 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1072
1073 // property functions
1074 result_type a() const;
1075 result_type b() const;
1076
1077 param_type param() const;
1078 void param(const param_type& parm);
1079
1080 result_type min() const;
1081 result_type max() const;
1082
1083 friend bool operator==(const extreme_value_distribution& x,
1084 const extreme_value_distribution& y);
1085 friend bool operator!=(const extreme_value_distribution& x,
1086 const extreme_value_distribution& y);
1087
1088 template <class charT, class traits>
1089 friend
1090 basic_ostream<charT, traits>&
1091 operator<<(basic_ostream<charT, traits>& os,
1092 const extreme_value_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001093
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00001094 template <class charT, class traits>
1095 friend
1096 basic_istream<charT, traits>&
1097 operator>>(basic_istream<charT, traits>& is,
1098 extreme_value_distribution& x);
1099};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100
1101template<class RealType = double>
Howard Hinnant0b95bc92010-05-12 21:02:31 +00001102class normal_distribution
1103{
1104public:
1105 // types
1106 typedef RealType result_type;
1107
1108 class param_type
1109 {
1110 public:
1111 typedef normal_distribution distribution_type;
1112
1113 explicit param_type(result_type mean = 0, result_type stddev = 1);
1114
1115 result_type mean() const;
1116 result_type stddev() const;
1117
1118 friend bool operator==(const param_type& x, const param_type& y);
1119 friend bool operator!=(const param_type& x, const param_type& y);
1120 };
1121
1122 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001123 explicit normal_distribution(RealType mean = 0.0, RealType stddev = 1.0); // before C++20
1124 normal_distribution() : normal_distribution(0.0) {} // C++20
1125 explicit normal_distribution(RealType mean, RealType stddev = 1.0); // C++20
Howard Hinnant0b95bc92010-05-12 21:02:31 +00001126 explicit normal_distribution(const param_type& parm);
1127 void reset();
1128
1129 // generating functions
1130 template<class URNG> result_type operator()(URNG& g);
1131 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1132
1133 // property functions
1134 result_type mean() const;
1135 result_type stddev() const;
1136
1137 param_type param() const;
1138 void param(const param_type& parm);
1139
1140 result_type min() const;
1141 result_type max() const;
1142
1143 friend bool operator==(const normal_distribution& x,
1144 const normal_distribution& y);
1145 friend bool operator!=(const normal_distribution& x,
1146 const normal_distribution& y);
1147
1148 template <class charT, class traits>
1149 friend
1150 basic_ostream<charT, traits>&
1151 operator<<(basic_ostream<charT, traits>& os,
1152 const normal_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001153
Howard Hinnant0b95bc92010-05-12 21:02:31 +00001154 template <class charT, class traits>
1155 friend
1156 basic_istream<charT, traits>&
1157 operator>>(basic_istream<charT, traits>& is,
1158 normal_distribution& x);
1159};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160
1161template<class RealType = double>
Howard Hinnantdf3bed62010-05-17 18:31:53 +00001162class lognormal_distribution
1163{
1164public:
1165 // types
1166 typedef RealType result_type;
1167
1168 class param_type
1169 {
1170 public:
1171 typedef lognormal_distribution distribution_type;
1172
1173 explicit param_type(result_type m = 0, result_type s = 1);
1174
1175 result_type m() const;
1176 result_type s() const;
1177
1178 friend bool operator==(const param_type& x, const param_type& y);
1179 friend bool operator!=(const param_type& x, const param_type& y);
1180 };
1181
1182 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001183 explicit lognormal_distribution(RealType mean = 0.0, RealType stddev = 1.0); // before C++20
1184 lognormal_distribution() : lognormal_distribution(0.0) {} // C++20
1185 explicit lognormal_distribution(RealType mean, RealType stddev = 1.0); // C++20
Howard Hinnantdf3bed62010-05-17 18:31:53 +00001186 explicit lognormal_distribution(const param_type& parm);
1187 void reset();
1188
1189 // generating functions
1190 template<class URNG> result_type operator()(URNG& g);
1191 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1192
1193 // property functions
1194 result_type m() const;
1195 result_type s() const;
1196
1197 param_type param() const;
1198 void param(const param_type& parm);
1199
1200 result_type min() const;
1201 result_type max() const;
1202
1203 friend bool operator==(const lognormal_distribution& x,
1204 const lognormal_distribution& y);
1205 friend bool operator!=(const lognormal_distribution& x,
1206 const lognormal_distribution& y);
1207
1208 template <class charT, class traits>
1209 friend
1210 basic_ostream<charT, traits>&
1211 operator<<(basic_ostream<charT, traits>& os,
1212 const lognormal_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001213
Howard Hinnantdf3bed62010-05-17 18:31:53 +00001214 template <class charT, class traits>
1215 friend
1216 basic_istream<charT, traits>&
1217 operator>>(basic_istream<charT, traits>& is,
1218 lognormal_distribution& x);
1219};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220
1221template<class RealType = double>
Howard Hinnant252ebf02010-05-15 23:36:00 +00001222class chi_squared_distribution
1223{
1224public:
1225 // types
1226 typedef RealType result_type;
1227
1228 class param_type
1229 {
1230 public:
1231 typedef chi_squared_distribution distribution_type;
1232
1233 explicit param_type(result_type n = 1);
1234
1235 result_type n() const;
1236
1237 friend bool operator==(const param_type& x, const param_type& y);
1238 friend bool operator!=(const param_type& x, const param_type& y);
1239 };
1240
1241 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001242 explicit chi_squared_distribution(RealType n = 1.0); // before C++20
1243 chi_squared_distribution() : chi_squared_distribution(1.0) {} // C++20
1244 explicit chi_squared_distribution(RealType n); // C++20
Howard Hinnant252ebf02010-05-15 23:36:00 +00001245 explicit chi_squared_distribution(const param_type& parm);
1246 void reset();
1247
1248 // generating functions
1249 template<class URNG> result_type operator()(URNG& g);
1250 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1251
1252 // property functions
1253 result_type n() const;
1254
1255 param_type param() const;
1256 void param(const param_type& parm);
1257
1258 result_type min() const;
1259 result_type max() const;
1260
Howard Hinnant252ebf02010-05-15 23:36:00 +00001261 friend bool operator==(const chi_squared_distribution& x,
1262 const chi_squared_distribution& y);
1263 friend bool operator!=(const chi_squared_distribution& x,
1264 const chi_squared_distribution& y);
1265
1266 template <class charT, class traits>
1267 friend
1268 basic_ostream<charT, traits>&
1269 operator<<(basic_ostream<charT, traits>& os,
1270 const chi_squared_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001271
Howard Hinnant252ebf02010-05-15 23:36:00 +00001272 template <class charT, class traits>
1273 friend
1274 basic_istream<charT, traits>&
1275 operator>>(basic_istream<charT, traits>& is,
1276 chi_squared_distribution& x);
1277};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278
1279template<class RealType = double>
Howard Hinnantf3292562010-05-17 21:55:46 +00001280class cauchy_distribution
1281{
1282public:
1283 // types
1284 typedef RealType result_type;
1285
1286 class param_type
1287 {
1288 public:
1289 typedef cauchy_distribution distribution_type;
1290
1291 explicit param_type(result_type a = 0, result_type b = 1);
1292
1293 result_type a() const;
1294 result_type b() const;
1295
1296 friend bool operator==(const param_type& x, const param_type& y);
1297 friend bool operator!=(const param_type& x, const param_type& y);
1298 };
1299
1300 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001301 explicit cauchy_distribution(RealType a = 0.0, RealType b = 1.0); // before C++20
1302 cauchy_distribution() : cauchy_distribution(0.0) {} // C++20
1303 explicit cauchy_distribution(RealType a, RealType b = 1.0); // C++20
Howard Hinnantf3292562010-05-17 21:55:46 +00001304 explicit cauchy_distribution(const param_type& parm);
1305 void reset();
1306
1307 // generating functions
1308 template<class URNG> result_type operator()(URNG& g);
1309 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1310
1311 // property functions
1312 result_type a() const;
1313 result_type b() const;
1314
1315 param_type param() const;
1316 void param(const param_type& parm);
1317
1318 result_type min() const;
1319 result_type max() const;
1320
1321 friend bool operator==(const cauchy_distribution& x,
1322 const cauchy_distribution& y);
1323 friend bool operator!=(const cauchy_distribution& x,
1324 const cauchy_distribution& y);
1325
1326 template <class charT, class traits>
1327 friend
1328 basic_ostream<charT, traits>&
1329 operator<<(basic_ostream<charT, traits>& os,
1330 const cauchy_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001331
Howard Hinnantf3292562010-05-17 21:55:46 +00001332 template <class charT, class traits>
1333 friend
1334 basic_istream<charT, traits>&
1335 operator>>(basic_istream<charT, traits>& is,
1336 cauchy_distribution& x);
1337};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
1339template<class RealType = double>
Howard Hinnantf39463b2010-05-18 17:32:30 +00001340class fisher_f_distribution
1341{
1342public:
1343 // types
1344 typedef RealType result_type;
1345
1346 class param_type
1347 {
1348 public:
Howard Hinnant20c50882010-05-18 20:08:04 +00001349 typedef fisher_f_distribution distribution_type;
Howard Hinnantf39463b2010-05-18 17:32:30 +00001350
1351 explicit param_type(result_type m = 1, result_type n = 1);
1352
1353 result_type m() const;
1354 result_type n() const;
1355
1356 friend bool operator==(const param_type& x, const param_type& y);
1357 friend bool operator!=(const param_type& x, const param_type& y);
1358 };
1359
1360 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001361 explicit fisher_f_distribution(RealType m = 1.0, RealType n = 1.0); // before C++20
1362 fisher_f_distribution() : fisher_f_distribution(1.0) {} // C++20
1363 explicit fisher_f_distribution(RealType m, RealType n = 1.0); // C++20
Howard Hinnantf39463b2010-05-18 17:32:30 +00001364 explicit fisher_f_distribution(const param_type& parm);
1365 void reset();
1366
1367 // generating functions
1368 template<class URNG> result_type operator()(URNG& g);
1369 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1370
1371 // property functions
1372 result_type m() const;
1373 result_type n() const;
1374
1375 param_type param() const;
1376 void param(const param_type& parm);
1377
1378 result_type min() const;
1379 result_type max() const;
1380
1381 friend bool operator==(const fisher_f_distribution& x,
1382 const fisher_f_distribution& y);
1383 friend bool operator!=(const fisher_f_distribution& x,
1384 const fisher_f_distribution& y);
1385
1386 template <class charT, class traits>
1387 friend
1388 basic_ostream<charT, traits>&
1389 operator<<(basic_ostream<charT, traits>& os,
1390 const fisher_f_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001391
Howard Hinnantf39463b2010-05-18 17:32:30 +00001392 template <class charT, class traits>
1393 friend
1394 basic_istream<charT, traits>&
1395 operator>>(basic_istream<charT, traits>& is,
1396 fisher_f_distribution& x);
1397};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001398
1399template<class RealType = double>
Howard Hinnant20c50882010-05-18 20:08:04 +00001400class student_t_distribution
1401{
1402public:
1403 // types
1404 typedef RealType result_type;
1405
1406 class param_type
1407 {
1408 public:
1409 typedef student_t_distribution distribution_type;
1410
1411 explicit param_type(result_type n = 1);
1412
1413 result_type n() const;
1414
1415 friend bool operator==(const param_type& x, const param_type& y);
1416 friend bool operator!=(const param_type& x, const param_type& y);
1417 };
1418
1419 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001420 explicit student_t_distribution(RealType n = 1.0); // before C++20
1421 student_t_distribution() : student_t_distribution(1.0) {} // C++20
1422 explicit student_t_distribution(RealType n); // C++20
Howard Hinnant20c50882010-05-18 20:08:04 +00001423 explicit student_t_distribution(const param_type& parm);
1424 void reset();
1425
1426 // generating functions
1427 template<class URNG> result_type operator()(URNG& g);
1428 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1429
1430 // property functions
1431 result_type n() const;
1432
1433 param_type param() const;
1434 void param(const param_type& parm);
1435
1436 result_type min() const;
1437 result_type max() const;
1438
1439 friend bool operator==(const student_t_distribution& x,
1440 const student_t_distribution& y);
1441 friend bool operator!=(const student_t_distribution& x,
1442 const student_t_distribution& y);
1443
1444 template <class charT, class traits>
1445 friend
1446 basic_ostream<charT, traits>&
1447 operator<<(basic_ostream<charT, traits>& os,
1448 const student_t_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001449
Howard Hinnant20c50882010-05-18 20:08:04 +00001450 template <class charT, class traits>
1451 friend
1452 basic_istream<charT, traits>&
1453 operator>>(basic_istream<charT, traits>& is,
1454 student_t_distribution& x);
1455};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456
1457template<class IntType = int>
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00001458class discrete_distribution
1459{
1460public:
1461 // types
1462 typedef IntType result_type;
1463
1464 class param_type
1465 {
1466 public:
1467 typedef discrete_distribution distribution_type;
1468
1469 param_type();
1470 template<class InputIterator>
1471 param_type(InputIterator firstW, InputIterator lastW);
1472 param_type(initializer_list<double> wl);
1473 template<class UnaryOperation>
1474 param_type(size_t nw, double xmin, double xmax, UnaryOperation fw);
1475
1476 vector<double> probabilities() const;
1477
1478 friend bool operator==(const param_type& x, const param_type& y);
1479 friend bool operator!=(const param_type& x, const param_type& y);
1480 };
1481
1482 // constructor and reset functions
1483 discrete_distribution();
1484 template<class InputIterator>
1485 discrete_distribution(InputIterator firstW, InputIterator lastW);
1486 discrete_distribution(initializer_list<double> wl);
1487 template<class UnaryOperation>
1488 discrete_distribution(size_t nw, double xmin, double xmax,
1489 UnaryOperation fw);
1490 explicit discrete_distribution(const param_type& parm);
1491 void reset();
1492
1493 // generating functions
1494 template<class URNG> result_type operator()(URNG& g);
1495 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1496
1497 // property functions
1498 vector<double> probabilities() const;
1499
1500 param_type param() const;
1501 void param(const param_type& parm);
1502
1503 result_type min() const;
1504 result_type max() const;
1505
1506 friend bool operator==(const discrete_distribution& x,
1507 const discrete_distribution& y);
1508 friend bool operator!=(const discrete_distribution& x,
1509 const discrete_distribution& y);
1510
1511 template <class charT, class traits>
1512 friend
1513 basic_ostream<charT, traits>&
1514 operator<<(basic_ostream<charT, traits>& os,
1515 const discrete_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001516
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00001517 template <class charT, class traits>
1518 friend
1519 basic_istream<charT, traits>&
1520 operator>>(basic_istream<charT, traits>& is,
1521 discrete_distribution& x);
1522};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523
1524template<class RealType = double>
Howard Hinnant481ba382010-05-20 15:11:46 +00001525class piecewise_constant_distribution
1526{
1527 // types
1528 typedef RealType result_type;
1529
1530 class param_type
1531 {
1532 public:
1533 typedef piecewise_constant_distribution distribution_type;
1534
1535 param_type();
1536 template<class InputIteratorB, class InputIteratorW>
1537 param_type(InputIteratorB firstB, InputIteratorB lastB,
1538 InputIteratorW firstW);
1539 template<class UnaryOperation>
1540 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1541 template<class UnaryOperation>
1542 param_type(size_t nw, result_type xmin, result_type xmax,
1543 UnaryOperation fw);
1544
1545 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001546 vector<result_type> densities() const;
Howard Hinnant481ba382010-05-20 15:11:46 +00001547
1548 friend bool operator==(const param_type& x, const param_type& y);
1549 friend bool operator!=(const param_type& x, const param_type& y);
1550 };
1551
1552 // constructor and reset functions
1553 piecewise_constant_distribution();
1554 template<class InputIteratorB, class InputIteratorW>
1555 piecewise_constant_distribution(InputIteratorB firstB,
1556 InputIteratorB lastB,
1557 InputIteratorW firstW);
1558 template<class UnaryOperation>
1559 piecewise_constant_distribution(initializer_list<result_type> bl,
1560 UnaryOperation fw);
1561 template<class UnaryOperation>
1562 piecewise_constant_distribution(size_t nw, result_type xmin,
1563 result_type xmax, UnaryOperation fw);
1564 explicit piecewise_constant_distribution(const param_type& parm);
1565 void reset();
1566
1567 // generating functions
1568 template<class URNG> result_type operator()(URNG& g);
1569 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1570
1571 // property functions
1572 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001573 vector<result_type> densities() const;
Howard Hinnant481ba382010-05-20 15:11:46 +00001574
1575 param_type param() const;
1576 void param(const param_type& parm);
1577
1578 result_type min() const;
1579 result_type max() const;
1580
1581 friend bool operator==(const piecewise_constant_distribution& x,
1582 const piecewise_constant_distribution& y);
1583 friend bool operator!=(const piecewise_constant_distribution& x,
1584 const piecewise_constant_distribution& y);
1585
1586 template <class charT, class traits>
1587 friend
1588 basic_ostream<charT, traits>&
1589 operator<<(basic_ostream<charT, traits>& os,
1590 const piecewise_constant_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001591
Howard Hinnant481ba382010-05-20 15:11:46 +00001592 template <class charT, class traits>
1593 friend
1594 basic_istream<charT, traits>&
1595 operator>>(basic_istream<charT, traits>& is,
1596 piecewise_constant_distribution& x);
1597};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598
1599template<class RealType = double>
Howard Hinnanta001ef32010-05-25 00:27:34 +00001600class piecewise_linear_distribution
1601{
1602 // types
1603 typedef RealType result_type;
1604
1605 class param_type
1606 {
1607 public:
1608 typedef piecewise_linear_distribution distribution_type;
1609
1610 param_type();
1611 template<class InputIteratorB, class InputIteratorW>
1612 param_type(InputIteratorB firstB, InputIteratorB lastB,
1613 InputIteratorW firstW);
1614 template<class UnaryOperation>
1615 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1616 template<class UnaryOperation>
1617 param_type(size_t nw, result_type xmin, result_type xmax,
1618 UnaryOperation fw);
1619
1620 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001621 vector<result_type> densities() const;
Howard Hinnanta001ef32010-05-25 00:27:34 +00001622
1623 friend bool operator==(const param_type& x, const param_type& y);
1624 friend bool operator!=(const param_type& x, const param_type& y);
1625 };
1626
1627 // constructor and reset functions
1628 piecewise_linear_distribution();
1629 template<class InputIteratorB, class InputIteratorW>
1630 piecewise_linear_distribution(InputIteratorB firstB,
1631 InputIteratorB lastB,
1632 InputIteratorW firstW);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001633
Howard Hinnanta001ef32010-05-25 00:27:34 +00001634 template<class UnaryOperation>
1635 piecewise_linear_distribution(initializer_list<result_type> bl,
1636 UnaryOperation fw);
1637
1638 template<class UnaryOperation>
1639 piecewise_linear_distribution(size_t nw, result_type xmin,
1640 result_type xmax, UnaryOperation fw);
1641
1642 explicit piecewise_linear_distribution(const param_type& parm);
1643 void reset();
1644
1645 // generating functions
1646 template<class URNG> result_type operator()(URNG& g);
1647 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1648
1649 // property functions
1650 vector<result_type> intervals() const;
Howard Hinnanta7533562010-11-18 17:34:48 +00001651 vector<result_type> densities() const;
Howard Hinnanta001ef32010-05-25 00:27:34 +00001652
1653 param_type param() const;
1654 void param(const param_type& parm);
1655
1656 result_type min() const;
1657 result_type max() const;
1658
1659 friend bool operator==(const piecewise_linear_distribution& x,
1660 const piecewise_linear_distribution& y);
1661 friend bool operator!=(const piecewise_linear_distribution& x,
1662 const piecewise_linear_distribution& y);
1663
1664 template <class charT, class traits>
1665 friend
1666 basic_ostream<charT, traits>&
1667 operator<<(basic_ostream<charT, traits>& os,
1668 const piecewise_linear_distribution& x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001669
Howard Hinnanta001ef32010-05-25 00:27:34 +00001670 template <class charT, class traits>
1671 friend
1672 basic_istream<charT, traits>&
1673 operator>>(basic_istream<charT, traits>& is,
1674 piecewise_linear_distribution& x);
1675};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001676
1677} // std
1678*/
1679
1680#include <__config>
Louis Dionnea60dd872021-06-17 11:30:11 -04001681#include <__random/uniform_int_distribution.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -04001682#include <algorithm>
Eric Fiselier90adc202015-01-23 22:22:36 +00001683#include <cmath>
Christopher Di Bella68074072021-02-17 02:52:17 +00001684#include <concepts>
Arthur O'Dwyeref181602021-05-19 11:57:04 -04001685#include <cstddef>
1686#include <cstdint>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687#include <initializer_list>
Louis Dionne3df65ce2020-10-15 13:27:27 -04001688#include <iosfwd>
Arthur O'Dwyeref181602021-05-19 11:57:04 -04001689#include <limits>
1690#include <numeric>
1691#include <string>
1692#include <type_traits>
1693#include <vector>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694
Howard Hinnantaaaa52b2011-10-17 20:05:10 +00001695#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +00001697#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698
Eric Fiselierf4433a32017-05-31 22:07:49 +00001699_LIBCPP_PUSH_MACROS
1700#include <__undef_macros>
1701
1702
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703_LIBCPP_BEGIN_NAMESPACE_STD
1704
Christopher Di Bella88110022021-02-19 01:54:52 +00001705#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
Christopher Di Bella68074072021-02-17 02:52:17 +00001706
1707// [rand.req.urng]
1708template<class _Gen>
1709concept uniform_random_bit_generator =
1710 invocable<_Gen&> && unsigned_integral<invoke_result_t<_Gen&>> &&
1711 requires {
1712 { _Gen::min() } -> same_as<invoke_result_t<_Gen&>>;
1713 { _Gen::max() } -> same_as<invoke_result_t<_Gen&>>;
1714 requires bool_constant<(_Gen::min() < _Gen::max())>::value;
1715 };
1716
Christopher Di Bella88110022021-02-19 01:54:52 +00001717#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
Christopher Di Bella68074072021-02-17 02:52:17 +00001718
Howard Hinnantc8c73992011-04-11 18:22:12 +00001719// __is_seed_sequence
1720
1721template <class _Sseq, class _Engine>
1722struct __is_seed_sequence
1723{
Howard Hinnant5a646852012-04-02 21:00:45 +00001724 static _LIBCPP_CONSTEXPR const bool value =
Howard Hinnantc8c73992011-04-11 18:22:12 +00001725 !is_convertible<_Sseq, typename _Engine::result_type>::value &&
1726 !is_same<typename remove_cv<_Sseq>::type, _Engine>::value;
1727};
1728
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729// linear_congruential_engine
1730
1731template <unsigned long long __a, unsigned long long __c,
Howard Hinnantc834c512011-11-29 18:15:50 +00001732 unsigned long long __m, unsigned long long _Mp,
zoecarver68c37022020-11-10 18:23:22 -08001733 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_Mp-__c)/__a),
1734 bool _OverflowOK = ((__m|__m-1) > __m), // m = 2^n
1735 bool _SchrageOK = (__a != 0 && __m != 0 && __m % __a <= __m / __a)> // r <= q
1736struct __lce_alg_picker
1737{
1738 static_assert(__a != 0 || __m != 0 || !_MightOverflow || _OverflowOK || _SchrageOK,
1739 "The current values of a, c, and m cannot generate a number "
1740 "within bounds of linear_congruential_engine.");
1741
1742 static _LIBCPP_CONSTEXPR const bool __use_schrage = _MightOverflow &&
1743 !_OverflowOK &&
1744 _SchrageOK;
1745};
1746
1747template <unsigned long long __a, unsigned long long __c,
1748 unsigned long long __m, unsigned long long _Mp,
1749 bool _UseSchrage = __lce_alg_picker<__a, __c, __m, _Mp>::__use_schrage>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750struct __lce_ta;
1751
1752// 64
1753
1754template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1755struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1756{
1757 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759 static result_type next(result_type __x)
1760 {
1761 // Schrage's algorithm
1762 const result_type __q = __m / __a;
1763 const result_type __r = __m % __a;
1764 const result_type __t0 = __a * (__x % __q);
1765 const result_type __t1 = __r * (__x / __q);
1766 __x = __t0 + (__t0 < __t1) * __m - __t1;
1767 __x += __c - (__x >= __m - __c) * __m;
1768 return __x;
1769 }
1770};
1771
1772template <unsigned long long __a, unsigned long long __m>
1773struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1774{
1775 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777 static result_type next(result_type __x)
1778 {
1779 // Schrage's algorithm
1780 const result_type __q = __m / __a;
1781 const result_type __r = __m % __a;
1782 const result_type __t0 = __a * (__x % __q);
1783 const result_type __t1 = __r * (__x / __q);
1784 __x = __t0 + (__t0 < __t1) * __m - __t1;
1785 return __x;
1786 }
1787};
1788
1789template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1790struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1791{
1792 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794 static result_type next(result_type __x)
1795 {
1796 return (__a * __x + __c) % __m;
1797 }
1798};
1799
1800template <unsigned long long __a, unsigned long long __c>
1801struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1802{
1803 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 static result_type next(result_type __x)
1806 {
1807 return __a * __x + __c;
1808 }
1809};
1810
1811// 32
1812
Howard Hinnantc834c512011-11-29 18:15:50 +00001813template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1814struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815{
1816 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 static result_type next(result_type __x)
1819 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001820 const result_type __a = static_cast<result_type>(_Ap);
1821 const result_type __c = static_cast<result_type>(_Cp);
1822 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823 // Schrage's algorithm
1824 const result_type __q = __m / __a;
1825 const result_type __r = __m % __a;
1826 const result_type __t0 = __a * (__x % __q);
1827 const result_type __t1 = __r * (__x / __q);
1828 __x = __t0 + (__t0 < __t1) * __m - __t1;
1829 __x += __c - (__x >= __m - __c) * __m;
1830 return __x;
1831 }
1832};
1833
Howard Hinnantc834c512011-11-29 18:15:50 +00001834template <unsigned long long _Ap, unsigned long long _Mp>
1835struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true>
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 __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843 // Schrage's algorithm
1844 const result_type __q = __m / __a;
1845 const result_type __r = __m % __a;
1846 const result_type __t0 = __a * (__x % __q);
1847 const result_type __t1 = __r * (__x / __q);
1848 __x = __t0 + (__t0 < __t1) * __m - __t1;
1849 return __x;
1850 }
1851};
1852
Howard Hinnantc834c512011-11-29 18:15:50 +00001853template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1854struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), false>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855{
1856 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858 static result_type next(result_type __x)
1859 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001860 const result_type __a = static_cast<result_type>(_Ap);
1861 const result_type __c = static_cast<result_type>(_Cp);
1862 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 return (__a * __x + __c) % __m;
1864 }
1865};
1866
Howard Hinnantc834c512011-11-29 18:15:50 +00001867template <unsigned long long _Ap, unsigned long long _Cp>
1868struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869{
1870 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872 static result_type next(result_type __x)
1873 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001874 const result_type __a = static_cast<result_type>(_Ap);
1875 const result_type __c = static_cast<result_type>(_Cp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 return __a * __x + __c;
1877 }
1878};
1879
1880// 16
1881
1882template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1883struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1884{
1885 typedef unsigned short result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887 static result_type next(result_type __x)
1888 {
1889 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1890 }
1891};
1892
1893template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001894class _LIBCPP_TEMPLATE_VIS linear_congruential_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001895
1896template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001897 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001898_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899basic_ostream<_CharT, _Traits>&
1900operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00001901 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902
1903template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001904 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905basic_istream<_CharT, _Traits>&
1906operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00001907 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908
1909template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001910class _LIBCPP_TEMPLATE_VIS linear_congruential_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911{
1912public:
1913 // types
1914 typedef _UIntType result_type;
1915
Howard Hinnanta741b242013-05-21 21:19:35 +00001916private:
Howard Hinnantc51e1022010-05-11 19:42:16 +00001917 result_type __x_;
1918
Howard Hinnant5a646852012-04-02 21:00:45 +00001919 static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920
1921 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1922 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
Arthur O'Dwyer101ce072021-05-10 15:32:38 -04001923 static_assert(is_unsigned<_UIntType>::value, "_UIntType must be unsigned type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924public:
Howard Hinnant5a646852012-04-02 21:00:45 +00001925 static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u: 0u;
1926 static _LIBCPP_CONSTEXPR const result_type _Max = __m - 1u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1928
1929 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00001930 static _LIBCPP_CONSTEXPR const result_type multiplier = __a;
1931 static _LIBCPP_CONSTEXPR const result_type increment = __c;
1932 static _LIBCPP_CONSTEXPR const result_type modulus = __m;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00001934 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00001936 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
1937 static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938
1939 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001940#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00001941 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001942 linear_congruential_engine() : linear_congruential_engine(default_seed) {}
1943 _LIBCPP_INLINE_VISIBILITY
1944 explicit linear_congruential_engine(result_type __s) { seed(__s); }
1945#else
1946 _LIBCPP_INLINE_VISIBILITY
1947 explicit linear_congruential_engine(result_type __s = default_seed) {
1948 seed(__s);
1949 }
1950#endif
Howard Hinnant28b24882011-12-01 20:21:04 +00001951 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00001952 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001953 explicit linear_congruential_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00001954 typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955 {seed(__q);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001957 void seed(result_type __s = default_seed)
1958 {seed(integral_constant<bool, __m == 0>(),
1959 integral_constant<bool, __c == 0>(), __s);}
1960 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00001961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962 typename enable_if
1963 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00001964 __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965 void
1966 >::type
1967 seed(_Sseq& __q)
1968 {__seed(__q, integral_constant<unsigned,
1969 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
Howard Hinnanta0c4f7c2013-05-21 21:05:12 +00001970 : (__m > 0x100000000ull))>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971
1972 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00001973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974 result_type operator()()
Howard Hinnantc834c512011-11-29 18:15:50 +00001975 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1978
Howard Hinnantf5f99992010-09-22 18:02:38 +00001979 friend _LIBCPP_INLINE_VISIBILITY
1980 bool operator==(const linear_congruential_engine& __x,
1981 const linear_congruential_engine& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982 {return __x.__x_ == __y.__x_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001983 friend _LIBCPP_INLINE_VISIBILITY
1984 bool operator!=(const linear_congruential_engine& __x,
1985 const linear_congruential_engine& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986 {return !(__x == __y);}
1987
1988private:
1989
Howard Hinnantf5f99992010-09-22 18:02:38 +00001990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1996 1 : __s % __m;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1999
2000 template<class _Sseq>
2001 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2002 template<class _Sseq>
2003 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2004
2005 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00002006 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007 friend
2008 basic_ostream<_CharT, _Traits>&
2009 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00002010 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002011
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00002013 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014 friend
2015 basic_istream<_CharT, _Traits>&
2016 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00002017 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018};
2019
2020template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002021 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2022 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
2023
2024template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2025 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2026 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
2027
2028template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2029 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2030 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
2031
2032template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2033 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2034 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
2035
2036template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037template<class _Sseq>
2038void
2039linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
2040 integral_constant<unsigned, 1>)
2041{
2042 const unsigned __k = 1;
2043 uint32_t __ar[__k+3];
2044 __q.generate(__ar, __ar + __k + 3);
2045 result_type __s = static_cast<result_type>(__ar[3] % __m);
2046 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
2047}
2048
2049template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2050template<class _Sseq>
2051void
2052linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
2053 integral_constant<unsigned, 2>)
2054{
2055 const unsigned __k = 2;
2056 uint32_t __ar[__k+3];
2057 __q.generate(__ar, __ar + __k + 3);
2058 result_type __s = static_cast<result_type>((__ar[3] +
Howard Hinnanta0c4f7c2013-05-21 21:05:12 +00002059 ((uint64_t)__ar[4] << 32)) % __m);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
2061}
2062
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063template <class _CharT, class _Traits,
2064 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002065inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002066basic_ostream<_CharT, _Traits>&
2067operator<<(basic_ostream<_CharT, _Traits>& __os,
2068 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2069{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002070 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002071 typedef basic_ostream<_CharT, _Traits> _Ostream;
2072 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073 __os.fill(__os.widen(' '));
2074 return __os << __x.__x_;
2075}
2076
2077template <class _CharT, class _Traits,
2078 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2079basic_istream<_CharT, _Traits>&
2080operator>>(basic_istream<_CharT, _Traits>& __is,
2081 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2082{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002083 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002084 typedef basic_istream<_CharT, _Traits> _Istream;
2085 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086 _UIntType __t;
2087 __is >> __t;
2088 if (!__is.fail())
2089 __x.__x_ = __t;
2090 return __is;
2091}
2092
2093typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2094 minstd_rand0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2096 minstd_rand;
Howard Hinnant481ba382010-05-20 15:11:46 +00002097typedef minstd_rand default_random_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098// mersenne_twister_engine
2099
2100template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2101 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2102 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002103class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104
Eric Fiselier4638fca2017-05-31 21:20:18 +00002105template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2106 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2107 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002109operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002110 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
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>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113
Eric Fiselier4638fca2017-05-31 21:20:18 +00002114template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2115 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2116 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnanta54386e2012-09-14 00:39:16 +00002117_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002119operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002120 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002121 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002122 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123
2124template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002125 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2126 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2127 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128basic_ostream<_CharT, _Traits>&
2129operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002130 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002131 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132
2133template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002134 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2135 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2136 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137basic_istream<_CharT, _Traits>&
2138operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002139 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002140 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141
2142template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2143 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2144 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002145class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146{
2147public:
2148 // types
2149 typedef _UIntType result_type;
2150
2151private:
2152 result_type __x_[__n];
2153 size_t __i_;
2154
2155 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2156 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
Howard Hinnant5a646852012-04-02 21:00:45 +00002157 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2159 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2160 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2161 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2162 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2163 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2164 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2165public:
Howard Hinnant5a646852012-04-02 21:00:45 +00002166 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2167 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2168 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2170 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2171 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2172 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2173 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2174 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2175
2176 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002177 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2178 static _LIBCPP_CONSTEXPR const size_t state_size = __n;
2179 static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
2180 static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
2181 static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
2182 static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
2183 static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
2184 static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
2185 static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
2186 static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
2187 static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
2188 static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
2189 static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
Howard Hinnantf5f99992010-09-22 18:02:38 +00002190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002191 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002193 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2194 static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195
2196 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002197#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002198 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002199 mersenne_twister_engine() : mersenne_twister_engine(default_seed) {}
2200 _LIBCPP_INLINE_VISIBILITY
2201 explicit mersenne_twister_engine(result_type __sd) { seed(__sd); }
2202#else
2203 _LIBCPP_INLINE_VISIBILITY
2204 explicit mersenne_twister_engine(result_type __sd = default_seed) {
2205 seed(__sd);
2206 }
2207#endif
Howard Hinnant28b24882011-12-01 20:21:04 +00002208 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002210 explicit mersenne_twister_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002211 typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 {seed(__q);}
2213 void seed(result_type __sd = default_seed);
2214 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 typename enable_if
2217 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002218 __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219 void
2220 >::type
2221 seed(_Sseq& __q)
2222 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2223
2224 // generating functions
2225 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2228
Eric Fiselier4638fca2017-05-31 21:20:18 +00002229 template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2230 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2231 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 friend
2233 bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002234 operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002235 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002236 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002237 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002238
Eric Fiselier4638fca2017-05-31 21:20:18 +00002239 template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2240 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2241 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242 friend
2243 bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002244 operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002245 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002246 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002247 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248
2249 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002250 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2251 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2252 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253 friend
2254 basic_ostream<_CharT, _Traits>&
2255 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002256 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002257 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258
2259 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002260 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2261 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2262 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 friend
2264 basic_istream<_CharT, _Traits>&
2265 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002266 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002267 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268private:
2269
2270 template<class _Sseq>
2271 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2272 template<class _Sseq>
2273 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2274
2275 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277 static
2278 typename enable_if
2279 <
2280 __count < __w,
2281 result_type
2282 >::type
2283 __lshift(result_type __x) {return (__x << __count) & _Max;}
2284
2285 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287 static
2288 typename enable_if
2289 <
2290 (__count >= __w),
2291 result_type
2292 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002293 __lshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294
2295 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 static
2298 typename enable_if
2299 <
2300 __count < _Dt,
2301 result_type
2302 >::type
2303 __rshift(result_type __x) {return __x >> __count;}
2304
2305 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307 static
2308 typename enable_if
2309 <
2310 (__count >= _Dt),
2311 result_type
2312 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002313 __rshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314};
2315
2316template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2317 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2318 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002319 _LIBCPP_CONSTEXPR const size_t
2320 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size;
2321
2322template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2323 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2324 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002325 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002326 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size;
2327
2328template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2329 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2330 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002331 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002332 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size;
2333
2334template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2335 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2336 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002337 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002338 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits;
2339
2340template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2341 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2342 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2343 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2344 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask;
2345
2346template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2347 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2348 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002349 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002350 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u;
2351
2352template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2353 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2354 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2355 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2356 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d;
2357
2358template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2359 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2360 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002361 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002362 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s;
2363
2364template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2365 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2366 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2367 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2368 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b;
2369
2370template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2371 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2372 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002373 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002374 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t;
2375
2376template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2377 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2378 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2379 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2380 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c;
2381
2382template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2383 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2384 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002385 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002386 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l;
2387
2388template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2389 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2390 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2391 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2392 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier;
2393
2394template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2395 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2396 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2397 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2398 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed;
2399
2400template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2401 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2402 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403void
2404mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2405 __t, __c, __l, __f>::seed(result_type __sd)
Marshall Clowc1894632017-09-11 18:10:33 +00002406 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002407{ // __w >= 2
2408 __x_[0] = __sd & _Max;
2409 for (size_t __i = 1; __i < __n; ++__i)
2410 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2411 __i_ = 0;
2412}
2413
2414template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2415 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2416 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2417template<class _Sseq>
2418void
2419mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2420 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2421{
2422 const unsigned __k = 1;
2423 uint32_t __ar[__n * __k];
2424 __q.generate(__ar, __ar + __n * __k);
2425 for (size_t __i = 0; __i < __n; ++__i)
2426 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2427 const result_type __mask = __r == _Dt ? result_type(~0) :
2428 (result_type(1) << __r) - result_type(1);
2429 __i_ = 0;
2430 if ((__x_[0] & ~__mask) == 0)
2431 {
2432 for (size_t __i = 1; __i < __n; ++__i)
2433 if (__x_[__i] != 0)
2434 return;
Hubert Tongca5b7762018-08-16 23:56:54 +00002435 __x_[0] = result_type(1) << (__w - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436 }
2437}
2438
2439template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2440 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2441 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2442template<class _Sseq>
2443void
2444mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2445 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2446{
2447 const unsigned __k = 2;
2448 uint32_t __ar[__n * __k];
2449 __q.generate(__ar, __ar + __n * __k);
2450 for (size_t __i = 0; __i < __n; ++__i)
2451 __x_[__i] = static_cast<result_type>(
2452 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2453 const result_type __mask = __r == _Dt ? result_type(~0) :
2454 (result_type(1) << __r) - result_type(1);
2455 __i_ = 0;
2456 if ((__x_[0] & ~__mask) == 0)
2457 {
2458 for (size_t __i = 1; __i < __n; ++__i)
2459 if (__x_[__i] != 0)
2460 return;
Hubert Tongca5b7762018-08-16 23:56:54 +00002461 __x_[0] = result_type(1) << (__w - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462 }
2463}
2464
2465template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2466 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2467 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2468_UIntType
2469mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2470 __t, __c, __l, __f>::operator()()
2471{
2472 const size_t __j = (__i_ + 1) % __n;
2473 const result_type __mask = __r == _Dt ? result_type(~0) :
2474 (result_type(1) << __r) - result_type(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00002475 const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476 const size_t __k = (__i_ + __m) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00002477 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2479 __i_ = __j;
2480 __z ^= __lshift<__s>(__z) & __b;
2481 __z ^= __lshift<__t>(__z) & __c;
2482 return __z ^ __rshift<__l>(__z);
2483}
2484
Eric Fiselier4638fca2017-05-31 21:20:18 +00002485template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2486 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2487 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002489operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002490 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002491 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002492 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493{
2494 if (__x.__i_ == __y.__i_)
Howard Hinnantc834c512011-11-29 18:15:50 +00002495 return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002496 if (__x.__i_ == 0 || __y.__i_ == 0)
2497 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002498 size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002499 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500 __y.__x_ + __y.__i_))
2501 return false;
2502 if (__x.__i_ == 0)
Howard Hinnantc834c512011-11-29 18:15:50 +00002503 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
2504 return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505 }
2506 if (__x.__i_ < __y.__i_)
2507 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002508 size_t __j = _Np - __y.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002509 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510 __y.__x_ + __y.__i_))
2511 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002512 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002513 __y.__x_))
2514 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002515 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002516 __y.__x_ + (_Np - (__x.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002518 size_t __j = _Np - __x.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002519 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520 __x.__x_ + __x.__i_))
2521 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002522 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523 __x.__x_))
2524 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002525 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002526 __x.__x_ + (_Np - (__y.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527}
2528
Eric Fiselier4638fca2017-05-31 21:20:18 +00002529template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2530 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2531 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002532inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002534operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002535 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002536 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002537 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538{
2539 return !(__x == __y);
2540}
2541
2542template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002543 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2544 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2545 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546basic_ostream<_CharT, _Traits>&
2547operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002548 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002549 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002551 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002552 typedef basic_ostream<_CharT, _Traits> _Ostream;
2553 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554 _CharT __sp = __os.widen(' ');
2555 __os.fill(__sp);
2556 __os << __x.__x_[__x.__i_];
Howard Hinnantc834c512011-11-29 18:15:50 +00002557 for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558 __os << __sp << __x.__x_[__j];
2559 for (size_t __j = 0; __j < __x.__i_; ++__j)
2560 __os << __sp << __x.__x_[__j];
2561 return __os;
2562}
2563
2564template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002565 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2566 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2567 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568basic_istream<_CharT, _Traits>&
2569operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002570 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002571 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002573 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002574 typedef basic_istream<_CharT, _Traits> _Istream;
2575 __is.flags(_Istream::dec | _Istream::skipws);
Eric Fiselier4638fca2017-05-31 21:20:18 +00002576 _UInt __t[_Np];
Howard Hinnantc834c512011-11-29 18:15:50 +00002577 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578 __is >> __t[__i];
2579 if (!__is.fail())
2580 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002581 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582 __x.__x_[__i] = __t[__i];
2583 __x.__i_ = 0;
2584 }
2585 return __is;
2586}
2587
2588typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2589 0x9908b0df, 11, 0xffffffff,
2590 7, 0x9d2c5680,
2591 15, 0xefc60000,
2592 18, 1812433253> mt19937;
2593typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2594 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2595 17, 0x71d67fffeda60000ULL,
2596 37, 0xfff7eee000000000ULL,
2597 43, 6364136223846793005ULL> mt19937_64;
2598
2599// subtract_with_carry_engine
2600
2601template<class _UIntType, size_t __w, size_t __s, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002602class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603
Eric Fiselier4638fca2017-05-31 21:20:18 +00002604template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605bool
2606operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002607 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2608 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609
Eric Fiselier4638fca2017-05-31 21:20:18 +00002610template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnanta54386e2012-09-14 00:39:16 +00002611_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612bool
2613operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002614 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2615 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616
2617template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002618 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619basic_ostream<_CharT, _Traits>&
2620operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002621 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622
2623template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002624 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625basic_istream<_CharT, _Traits>&
2626operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002627 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628
2629template<class _UIntType, size_t __w, size_t __s, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002630class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631{
2632public:
2633 // types
2634 typedef _UIntType result_type;
2635
2636private:
2637 result_type __x_[__r];
2638 result_type __c_;
2639 size_t __i_;
2640
Howard Hinnant5a646852012-04-02 21:00:45 +00002641 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2643 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2644 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2645 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2646public:
Howard Hinnant5a646852012-04-02 21:00:45 +00002647 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2648 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2649 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2651
2652 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002653 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2654 static _LIBCPP_CONSTEXPR const size_t short_lag = __s;
2655 static _LIBCPP_CONSTEXPR const size_t long_lag = __r;
Howard Hinnantf5f99992010-09-22 18:02:38 +00002656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002657 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002659 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2660 static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661
2662 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002663#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002664 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002665 subtract_with_carry_engine() : subtract_with_carry_engine(default_seed) {}
2666 _LIBCPP_INLINE_VISIBILITY
2667 explicit subtract_with_carry_engine(result_type __sd) { seed(__sd); }
2668#else
2669 _LIBCPP_INLINE_VISIBILITY
2670 explicit subtract_with_carry_engine(result_type __sd = default_seed) {
2671 seed(__sd);
2672 }
2673#endif
Howard Hinnant28b24882011-12-01 20:21:04 +00002674 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002676 explicit subtract_with_carry_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002677 typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 {seed(__q);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680 void seed(result_type __sd = default_seed)
2681 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2682 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684 typename enable_if
2685 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002686 __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687 void
2688 >::type
2689 seed(_Sseq& __q)
2690 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2691
2692 // generating functions
2693 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2696
Eric Fiselier4638fca2017-05-31 21:20:18 +00002697 template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 friend
2699 bool
2700 operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002701 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2702 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002703
Eric Fiselier4638fca2017-05-31 21:20:18 +00002704 template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705 friend
2706 bool
2707 operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002708 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2709 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002710
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002712 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713 friend
2714 basic_ostream<_CharT, _Traits>&
2715 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002716 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002717
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002719 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720 friend
2721 basic_istream<_CharT, _Traits>&
2722 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002723 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724
2725private:
2726
2727 void seed(result_type __sd, integral_constant<unsigned, 1>);
2728 void seed(result_type __sd, integral_constant<unsigned, 2>);
2729 template<class _Sseq>
2730 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2731 template<class _Sseq>
2732 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2733};
2734
2735template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002736 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
2737
2738template<class _UIntType, size_t __w, size_t __s, size_t __r>
2739 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
2740
2741template<class _UIntType, size_t __w, size_t __s, size_t __r>
2742 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
2743
2744template<class _UIntType, size_t __w, size_t __s, size_t __r>
2745 _LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type
2746 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
2747
2748template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749void
2750subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2751 integral_constant<unsigned, 1>)
2752{
2753 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2754 __e(__sd == 0u ? default_seed : __sd);
2755 for (size_t __i = 0; __i < __r; ++__i)
2756 __x_[__i] = static_cast<result_type>(__e() & _Max);
2757 __c_ = __x_[__r-1] == 0;
2758 __i_ = 0;
2759}
2760
2761template<class _UIntType, size_t __w, size_t __s, size_t __r>
2762void
2763subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2764 integral_constant<unsigned, 2>)
2765{
2766 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2767 __e(__sd == 0u ? default_seed : __sd);
2768 for (size_t __i = 0; __i < __r; ++__i)
Howard Hinnant93072c12011-08-15 17:22:22 +00002769 {
2770 result_type __e0 = __e();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 __x_[__i] = static_cast<result_type>(
Howard Hinnant93072c12011-08-15 17:22:22 +00002772 (__e0 + ((uint64_t)__e() << 32)) & _Max);
2773 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774 __c_ = __x_[__r-1] == 0;
2775 __i_ = 0;
2776}
2777
2778template<class _UIntType, size_t __w, size_t __s, size_t __r>
2779template<class _Sseq>
2780void
2781subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2782 integral_constant<unsigned, 1>)
2783{
2784 const unsigned __k = 1;
2785 uint32_t __ar[__r * __k];
2786 __q.generate(__ar, __ar + __r * __k);
2787 for (size_t __i = 0; __i < __r; ++__i)
2788 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2789 __c_ = __x_[__r-1] == 0;
2790 __i_ = 0;
2791}
2792
2793template<class _UIntType, size_t __w, size_t __s, size_t __r>
2794template<class _Sseq>
2795void
2796subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2797 integral_constant<unsigned, 2>)
2798{
2799 const unsigned __k = 2;
2800 uint32_t __ar[__r * __k];
2801 __q.generate(__ar, __ar + __r * __k);
2802 for (size_t __i = 0; __i < __r; ++__i)
2803 __x_[__i] = static_cast<result_type>(
2804 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2805 __c_ = __x_[__r-1] == 0;
2806 __i_ = 0;
2807}
2808
2809template<class _UIntType, size_t __w, size_t __s, size_t __r>
2810_UIntType
2811subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2812{
2813 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2814 result_type& __xr = __x_[__i_];
2815 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2816 __xr = (__xs - __xr - __c_) & _Max;
2817 __c_ = __new_c;
2818 __i_ = (__i_ + 1) % __r;
2819 return __xr;
2820}
2821
Eric Fiselier4638fca2017-05-31 21:20:18 +00002822template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823bool
2824operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002825 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2826 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827{
2828 if (__x.__c_ != __y.__c_)
2829 return false;
2830 if (__x.__i_ == __y.__i_)
Howard Hinnantc834c512011-11-29 18:15:50 +00002831 return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 if (__x.__i_ == 0 || __y.__i_ == 0)
2833 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002834 size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002835 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836 __y.__x_ + __y.__i_))
2837 return false;
2838 if (__x.__i_ == 0)
Howard Hinnantc834c512011-11-29 18:15:50 +00002839 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
2840 return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841 }
2842 if (__x.__i_ < __y.__i_)
2843 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002844 size_t __j = _Rp - __y.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002845 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846 __y.__x_ + __y.__i_))
2847 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002848 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849 __y.__x_))
2850 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002851 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002852 __y.__x_ + (_Rp - (__x.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002854 size_t __j = _Rp - __x.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002855 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856 __x.__x_ + __x.__i_))
2857 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002858 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859 __x.__x_))
2860 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002861 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002862 __x.__x_ + (_Rp - (__y.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863}
2864
Eric Fiselier4638fca2017-05-31 21:20:18 +00002865template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002866inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867bool
2868operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002869 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2870 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871{
2872 return !(__x == __y);
2873}
2874
2875template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002876 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877basic_ostream<_CharT, _Traits>&
2878operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002879 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002881 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002882 typedef basic_ostream<_CharT, _Traits> _Ostream;
2883 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884 _CharT __sp = __os.widen(' ');
2885 __os.fill(__sp);
2886 __os << __x.__x_[__x.__i_];
Howard Hinnantc834c512011-11-29 18:15:50 +00002887 for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 __os << __sp << __x.__x_[__j];
2889 for (size_t __j = 0; __j < __x.__i_; ++__j)
2890 __os << __sp << __x.__x_[__j];
2891 __os << __sp << __x.__c_;
2892 return __os;
2893}
2894
2895template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002896 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002897basic_istream<_CharT, _Traits>&
2898operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002899 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002900{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002901 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002902 typedef basic_istream<_CharT, _Traits> _Istream;
2903 __is.flags(_Istream::dec | _Istream::skipws);
Eric Fiselier4638fca2017-05-31 21:20:18 +00002904 _UInt __t[_Rp+1];
Howard Hinnantc834c512011-11-29 18:15:50 +00002905 for (size_t __i = 0; __i < _Rp+1; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906 __is >> __t[__i];
2907 if (!__is.fail())
2908 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002909 for (size_t __i = 0; __i < _Rp; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910 __x.__x_[__i] = __t[__i];
Howard Hinnantc834c512011-11-29 18:15:50 +00002911 __x.__c_ = __t[_Rp];
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912 __x.__i_ = 0;
2913 }
2914 return __is;
2915}
2916
2917typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2918typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2919
2920// discard_block_engine
2921
2922template<class _Engine, size_t __p, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002923class _LIBCPP_TEMPLATE_VIS discard_block_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924{
2925 _Engine __e_;
2926 int __n_;
2927
2928 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2929 static_assert(__r <= __p, "discard_block_engine invalid parameters");
Eric Fiselier37c22152016-12-24 00:24:44 +00002930 static_assert(__r <= INT_MAX, "discard_block_engine invalid parameters");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931public:
2932 // types
2933 typedef typename _Engine::result_type result_type;
2934
2935 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002936 static _LIBCPP_CONSTEXPR const size_t block_size = __p;
2937 static _LIBCPP_CONSTEXPR const size_t used_block = __r;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00002939#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940 static const result_type _Min = _Engine::_Min;
2941 static const result_type _Max = _Engine::_Max;
Howard Hinnant5a646852012-04-02 21:00:45 +00002942#else
2943 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
2944 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
2945#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002946
Howard Hinnantf5f99992010-09-22 18:02:38 +00002947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002948 static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002950 static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951
2952 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00002953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954 discard_block_engine() : __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002956 explicit discard_block_engine(const _Engine& __e)
2957 : __e_(__e), __n_(0) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00002958#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002960 explicit discard_block_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002961 : __e_(_VSTD::move(__e)), __n_(0) {}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002962#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002965 template<class _Sseq>
2966 _LIBCPP_INLINE_VISIBILITY
2967 explicit discard_block_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002968 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002969 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002970 : __e_(__q), __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972 void seed() {__e_.seed(); __n_ = 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002975 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002977 typename enable_if
2978 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002979 __is_seed_sequence<_Sseq, discard_block_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002980 void
2981 >::type
2982 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002983
2984 // generating functions
2985 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2988
2989 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00002990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00002991 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992
Howard Hinnantc834c512011-11-29 18:15:50 +00002993 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994 friend
2995 bool
2996 operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00002997 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2998 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002999
Howard Hinnantc834c512011-11-29 18:15:50 +00003000 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001 friend
3002 bool
3003 operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00003004 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
3005 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003006
Howard Hinnantc51e1022010-05-11 19:42:16 +00003007 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003008 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003009 friend
3010 basic_ostream<_CharT, _Traits>&
3011 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003012 const discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003013
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003015 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003016 friend
3017 basic_istream<_CharT, _Traits>&
3018 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003019 discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003020};
3021
3022template<class _Engine, size_t __p, size_t __r>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00003023 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
3024
3025template<class _Engine, size_t __p, size_t __r>
3026 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
3027
3028template<class _Engine, size_t __p, size_t __r>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003029typename discard_block_engine<_Engine, __p, __r>::result_type
3030discard_block_engine<_Engine, __p, __r>::operator()()
3031{
Eric Fiselier37c22152016-12-24 00:24:44 +00003032 if (__n_ >= static_cast<int>(__r))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033 {
3034 __e_.discard(__p - __r);
3035 __n_ = 0;
3036 }
3037 ++__n_;
3038 return __e_();
3039}
3040
Howard Hinnantc834c512011-11-29 18:15:50 +00003041template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003042inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043bool
Howard Hinnantc834c512011-11-29 18:15:50 +00003044operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
3045 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046{
3047 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
3048}
3049
Howard Hinnantc834c512011-11-29 18:15:50 +00003050template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003051inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052bool
Howard Hinnantc834c512011-11-29 18:15:50 +00003053operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
3054 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055{
3056 return !(__x == __y);
3057}
3058
3059template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003060 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061basic_ostream<_CharT, _Traits>&
3062operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003063 const discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003064{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003065 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003066 typedef basic_ostream<_CharT, _Traits> _Ostream;
3067 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068 _CharT __sp = __os.widen(' ');
3069 __os.fill(__sp);
3070 return __os << __x.__e_ << __sp << __x.__n_;
3071}
3072
3073template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003074 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003075basic_istream<_CharT, _Traits>&
3076operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003077 discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003079 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003080 typedef basic_istream<_CharT, _Traits> _Istream;
3081 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082 _Eng __e;
3083 int __n;
3084 __is >> __e >> __n;
3085 if (!__is.fail())
3086 {
3087 __x.__e_ = __e;
3088 __x.__n_ = __n;
3089 }
3090 return __is;
3091}
3092
3093typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
3094typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
3095
3096// independent_bits_engine
3097
Howard Hinnantc51e1022010-05-11 19:42:16 +00003098template<class _Engine, size_t __w, class _UIntType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003099class _LIBCPP_TEMPLATE_VIS independent_bits_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00003100{
Eric Fiselier4638fca2017-05-31 21:20:18 +00003101 template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102 class __get_n
3103 {
Eric Fiselier4638fca2017-05-31 21:20:18 +00003104 static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits;
Howard Hinnant5a646852012-04-02 21:00:45 +00003105 static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
3106 static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
Eric Fiselier4638fca2017-05-31 21:20:18 +00003107 static _LIBCPP_CONSTEXPR const _UInt _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108 public:
Howard Hinnant5a646852012-04-02 21:00:45 +00003109 static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110 };
3111public:
3112 // types
3113 typedef _UIntType result_type;
3114
3115private:
3116 _Engine __e_;
3117
Howard Hinnant5a646852012-04-02 21:00:45 +00003118 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
3120 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
3121
3122 typedef typename _Engine::result_type _Engine_result_type;
3123 typedef typename conditional
3124 <
3125 sizeof(_Engine_result_type) <= sizeof(result_type),
3126 result_type,
3127 _Engine_result_type
3128 >::type _Working_result_type;
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003129#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +00003130 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant5a646852012-04-02 21:00:45 +00003131 + _Working_result_type(1);
3132#else
3133 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
3134 + _Working_result_type(1);
3135#endif
3136 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
3137 static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
3138 static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
3139 static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
3140 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
3141 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
3142 static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
3143 (_Rp >> __w0) << __w0;
3144 static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
3145 (_Rp >> (__w0+1)) << (__w0+1);
3146 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 _Engine_result_type(~0) >> (_EDt - __w0) :
3148 _Engine_result_type(0);
Howard Hinnant5a646852012-04-02 21:00:45 +00003149 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
3151 _Engine_result_type(~0);
3152public:
Howard Hinnant5a646852012-04-02 21:00:45 +00003153 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3154 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
3155 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003156 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
3157
3158 // engine characteristics
Howard Hinnantf5f99992010-09-22 18:02:38 +00003159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003160 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00003161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003162 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163
3164 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166 independent_bits_engine() {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003168 explicit independent_bits_engine(const _Engine& __e)
3169 : __e_(__e) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003170#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003172 explicit independent_bits_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003173 : __e_(_VSTD::move(__e)) {}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003174#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnant28b24882011-12-01 20:21:04 +00003177 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00003179 explicit independent_bits_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00003180 typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003181 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182 : __e_(__q) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184 void seed() {__e_.seed();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003187 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003189 typename enable_if
3190 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00003191 __is_seed_sequence<_Sseq, independent_bits_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003192 void
3193 >::type
3194 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003195
3196 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003198 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3201
3202 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00003204 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205
Eric Fiselier4638fca2017-05-31 21:20:18 +00003206 template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207 friend
3208 bool
3209 operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003210 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3211 const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003212
Eric Fiselier4638fca2017-05-31 21:20:18 +00003213 template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214 friend
3215 bool
3216 operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003217 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3218 const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003219
Howard Hinnantc51e1022010-05-11 19:42:16 +00003220 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003221 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222 friend
3223 basic_ostream<_CharT, _Traits>&
3224 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003225 const independent_bits_engine<_Eng, _Wp, _UInt>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003226
Howard Hinnantc51e1022010-05-11 19:42:16 +00003227 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003228 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229 friend
3230 basic_istream<_CharT, _Traits>&
3231 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003232 independent_bits_engine<_Eng, _Wp, _UInt>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233
3234private:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003235 _LIBCPP_INLINE_VISIBILITY
Marshall Clowfe778582017-09-20 19:38:43 +00003236 result_type __eval(false_type);
3237 result_type __eval(true_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238
3239 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241 static
3242 typename enable_if
3243 <
3244 __count < _Dt,
3245 result_type
3246 >::type
3247 __lshift(result_type __x) {return __x << __count;}
3248
3249 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251 static
3252 typename enable_if
3253 <
3254 (__count >= _Dt),
3255 result_type
3256 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00003257 __lshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003258};
3259
3260template<class _Engine, size_t __w, class _UIntType>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003261inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00003263independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003264{
3265 return static_cast<result_type>(__e_() & __mask0);
3266}
3267
3268template<class _Engine, size_t __w, class _UIntType>
3269_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00003270independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271{
Howard Hinnantc834c512011-11-29 18:15:50 +00003272 result_type _Sp = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273 for (size_t __k = 0; __k < __n0; ++__k)
3274 {
3275 _Engine_result_type __u;
3276 do
3277 {
3278 __u = __e_() - _Engine::min();
3279 } while (__u >= __y0);
Howard Hinnantc834c512011-11-29 18:15:50 +00003280 _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281 }
3282 for (size_t __k = __n0; __k < __n; ++__k)
3283 {
3284 _Engine_result_type __u;
3285 do
3286 {
3287 __u = __e_() - _Engine::min();
3288 } while (__u >= __y1);
Howard Hinnantc834c512011-11-29 18:15:50 +00003289 _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003290 }
Howard Hinnantc834c512011-11-29 18:15:50 +00003291 return _Sp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003292}
3293
Eric Fiselier4638fca2017-05-31 21:20:18 +00003294template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003295inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296bool
3297operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003298 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3299 const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300{
3301 return __x.base() == __y.base();
3302}
3303
Eric Fiselier4638fca2017-05-31 21:20:18 +00003304template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003306bool
3307operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003308 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3309 const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310{
3311 return !(__x == __y);
3312}
3313
3314template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003315 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316basic_ostream<_CharT, _Traits>&
3317operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003318 const independent_bits_engine<_Eng, _Wp, _UInt>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319{
3320 return __os << __x.base();
3321}
3322
3323template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003324 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003325basic_istream<_CharT, _Traits>&
3326operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003327 independent_bits_engine<_Eng, _Wp, _UInt>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328{
3329 _Eng __e;
3330 __is >> __e;
3331 if (!__is.fail())
3332 __x.__e_ = __e;
3333 return __is;
3334}
3335
3336// shuffle_order_engine
3337
3338template <uint64_t _Xp, uint64_t _Yp>
3339struct __ugcd
3340{
Howard Hinnant5a646852012-04-02 21:00:45 +00003341 static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003342};
3343
3344template <uint64_t _Xp>
3345struct __ugcd<_Xp, 0>
3346{
Howard Hinnant5a646852012-04-02 21:00:45 +00003347 static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348};
3349
Howard Hinnantc834c512011-11-29 18:15:50 +00003350template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351class __uratio
3352{
Howard Hinnantc834c512011-11-29 18:15:50 +00003353 static_assert(_Dp != 0, "__uratio divide by 0");
Howard Hinnant5a646852012-04-02 21:00:45 +00003354 static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003355public:
Howard Hinnant5a646852012-04-02 21:00:45 +00003356 static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
3357 static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358
3359 typedef __uratio<num, den> type;
3360};
3361
3362template<class _Engine, size_t __k>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003363class _LIBCPP_TEMPLATE_VIS shuffle_order_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364{
3365 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3366public:
3367 // types
3368 typedef typename _Engine::result_type result_type;
3369
3370private:
3371 _Engine __e_;
3372 result_type _V_[__k];
3373 result_type _Y_;
3374
3375public:
3376 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00003377 static _LIBCPP_CONSTEXPR const size_t table_size = __k;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003378
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003379#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380 static const result_type _Min = _Engine::_Min;
3381 static const result_type _Max = _Engine::_Max;
Howard Hinnant5a646852012-04-02 21:00:45 +00003382#else
3383 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
3384 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
3385#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
Howard Hinnantf5f99992010-09-22 18:02:38 +00003387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003388 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00003389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003390 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391
Howard Hinnant5a646852012-04-02 21:00:45 +00003392 static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003393
3394 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396 shuffle_order_engine() {__init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003398 explicit shuffle_order_engine(const _Engine& __e)
3399 : __e_(__e) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003400#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003402 explicit shuffle_order_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003403 : __e_(_VSTD::move(__e)) {__init();}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003404#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003406 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnant28b24882011-12-01 20:21:04 +00003407 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00003409 explicit shuffle_order_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00003410 typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003411 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412 : __e_(__q) {__init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003414 void seed() {__e_.seed(); __init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003417 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003419 typename enable_if
3420 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00003421 __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003422 void
3423 >::type
3424 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425
3426 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003428 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3431
3432 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00003434 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003435
3436private:
Howard Hinnantc834c512011-11-29 18:15:50 +00003437 template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003438 friend
3439 bool
3440 operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00003441 const shuffle_order_engine<_Eng, _Kp>& __x,
3442 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003443
Howard Hinnantc834c512011-11-29 18:15:50 +00003444 template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445 friend
3446 bool
3447 operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00003448 const shuffle_order_engine<_Eng, _Kp>& __x,
3449 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003450
Howard Hinnantc51e1022010-05-11 19:42:16 +00003451 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003452 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453 friend
3454 basic_ostream<_CharT, _Traits>&
3455 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003456 const shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003457
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003459 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460 friend
3461 basic_istream<_CharT, _Traits>&
3462 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003463 shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464
Howard Hinnantf5f99992010-09-22 18:02:38 +00003465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466 void __init()
3467 {
3468 for (size_t __i = 0; __i < __k; ++__i)
3469 _V_[__i] = __e_();
3470 _Y_ = __e_();
3471 }
3472
Howard Hinnantf5f99992010-09-22 18:02:38 +00003473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003476 result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003477
Howard Hinnantf5f99992010-09-22 18:02:38 +00003478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3482
Howard Hinnantc834c512011-11-29 18:15:50 +00003483 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485 typename enable_if
3486 <
Howard Hinnantc834c512011-11-29 18:15:50 +00003487 (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488 result_type
3489 >::type
Howard Hinnantc834c512011-11-29 18:15:50 +00003490 __eval(__uratio<_Np, _Dp>)
3491 {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492
Howard Hinnantc834c512011-11-29 18:15:50 +00003493 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003495 typename enable_if
3496 <
Howard Hinnantc834c512011-11-29 18:15:50 +00003497 __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498 result_type
3499 >::type
Howard Hinnantc834c512011-11-29 18:15:50 +00003500 __eval(__uratio<_Np, _Dp>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003502 const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
3503 / __uratio<_Np, _Dp>::den);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504 _Y_ = _V_[__j];
3505 _V_[__j] = __e_();
3506 return _Y_;
3507 }
3508
3509 template <uint64_t __n, uint64_t __d>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511 result_type __evalf()
3512 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003513 const double _Fp = __d == 0 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514 __n / (2. * 0x8000000000000000ull) :
3515 __n / (double)__d;
Howard Hinnantc834c512011-11-29 18:15:50 +00003516 const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517 _Y_ = _V_[__j];
3518 _V_[__j] = __e_();
3519 return _Y_;
3520 }
3521};
3522
Howard Hinnant2c45cb42012-12-12 21:14:28 +00003523template<class _Engine, size_t __k>
3524 _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size;
3525
Howard Hinnantc834c512011-11-29 18:15:50 +00003526template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527bool
3528operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00003529 const shuffle_order_engine<_Eng, _Kp>& __x,
3530 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531{
Howard Hinnantc834c512011-11-29 18:15:50 +00003532 return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533 __x.__e_ == __y.__e_;
3534}
3535
Howard Hinnantc834c512011-11-29 18:15:50 +00003536template<class _Eng, size_t _Kp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003537inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003538bool
3539operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00003540 const shuffle_order_engine<_Eng, _Kp>& __x,
3541 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542{
3543 return !(__x == __y);
3544}
3545
3546template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003547 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548basic_ostream<_CharT, _Traits>&
3549operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003550 const shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003551{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003552 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003553 typedef basic_ostream<_CharT, _Traits> _Ostream;
3554 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555 _CharT __sp = __os.widen(' ');
3556 __os.fill(__sp);
3557 __os << __x.__e_ << __sp << __x._V_[0];
Howard Hinnantc834c512011-11-29 18:15:50 +00003558 for (size_t __i = 1; __i < _Kp; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559 __os << __sp << __x._V_[__i];
3560 return __os << __sp << __x._Y_;
3561}
3562
3563template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003564 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565basic_istream<_CharT, _Traits>&
3566operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003567 shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568{
Howard Hinnantc834c512011-11-29 18:15:50 +00003569 typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003570 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003571 typedef basic_istream<_CharT, _Traits> _Istream;
3572 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573 _Eng __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003574 result_type _Vp[_Kp+1];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575 __is >> __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003576 for (size_t __i = 0; __i < _Kp+1; ++__i)
3577 __is >> _Vp[__i];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578 if (!__is.fail())
3579 {
3580 __x.__e_ = __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003581 for (size_t __i = 0; __i < _Kp; ++__i)
3582 __x._V_[__i] = _Vp[__i];
3583 __x._Y_ = _Vp[_Kp];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584 }
3585 return __is;
3586}
3587
3588typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3589
3590// random_device
3591
Louis Dionne3777e682020-10-15 10:32:09 -04003592#if !defined(_LIBCPP_HAS_NO_RANDOM_DEVICE)
3593
Howard Hinnant8331b762013-03-06 23:30:19 +00003594class _LIBCPP_TYPE_VIS random_device
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595{
Ed Schoutendcf37402015-03-10 07:46:06 +00003596#ifdef _LIBCPP_USING_DEV_RANDOM
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597 int __f_;
Ed Schoutendcf37402015-03-10 07:46:06 +00003598#endif // defined(_LIBCPP_USING_DEV_RANDOM)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599public:
3600 // types
3601 typedef unsigned result_type;
3602
3603 // generator characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00003604 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3605 static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606
Howard Hinnantf5f99992010-09-22 18:02:38 +00003607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant664183b2012-04-02 00:40:41 +00003608 static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant664183b2012-04-02 00:40:41 +00003610 static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611
3612 // constructors
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003613#ifndef _LIBCPP_CXX03_LANG
3614 random_device() : random_device("/dev/urandom") {}
3615 explicit random_device(const string& __token);
3616#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617 explicit random_device(const string& __token = "/dev/urandom");
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003618#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619 ~random_device();
3620
3621 // generating functions
3622 result_type operator()();
3623
3624 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +00003625 double entropy() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626
3627private:
3628 // no copy functions
3629 random_device(const random_device&); // = delete;
3630 random_device& operator=(const random_device&); // = delete;
3631};
3632
Louis Dionne3777e682020-10-15 10:32:09 -04003633#endif // !_LIBCPP_HAS_NO_RANDOM_DEVICE
3634
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635// seed_seq
3636
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003637class _LIBCPP_TEMPLATE_VIS seed_seq
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638{
3639public:
3640 // types
3641 typedef uint32_t result_type;
3642
3643private:
3644 vector<result_type> __v_;
3645
3646 template<class _InputIterator>
3647 void init(_InputIterator __first, _InputIterator __last);
3648public:
3649 // constructors
Howard Hinnantf5f99992010-09-22 18:02:38 +00003650 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4b6b8092013-10-23 05:56:47 +00003651 seed_seq() _NOEXCEPT {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003652#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653 template<class _Tp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003656#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003657
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660 seed_seq(_InputIterator __first, _InputIterator __last)
3661 {init(__first, __last);}
3662
3663 // generating functions
3664 template<class _RandomAccessIterator>
3665 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3666
3667 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003668 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4b6b8092013-10-23 05:56:47 +00003669 size_t size() const _NOEXCEPT {return __v_.size();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670 template<class _OutputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672 void param(_OutputIterator __dest) const
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003673 {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674
3675private:
3676 // no copy functions
3677 seed_seq(const seed_seq&); // = delete;
3678 void operator=(const seed_seq&); // = delete;
3679
Howard Hinnantf5f99992010-09-22 18:02:38 +00003680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003681 static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682};
3683
3684template<class _InputIterator>
3685void
3686seed_seq::init(_InputIterator __first, _InputIterator __last)
3687{
3688 for (_InputIterator __s = __first; __s != __last; ++__s)
3689 __v_.push_back(*__s & 0xFFFFFFFF);
3690}
3691
3692template<class _RandomAccessIterator>
3693void
3694seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3695{
3696 if (__first != __last)
3697 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003698 _VSTD::fill(__first, __last, 0x8b8b8b8b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003699 const size_t __n = static_cast<size_t>(__last - __first);
3700 const size_t __s = __v_.size();
3701 const size_t __t = (__n >= 623) ? 11
3702 : (__n >= 68) ? 7
3703 : (__n >= 39) ? 5
3704 : (__n >= 7) ? 3
3705 : (__n - 1) / 2;
3706 const size_t __p = (__n - __t) / 2;
3707 const size_t __q = __p + __t;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003708 const size_t __m = _VSTD::max(__s + 1, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003709 // __k = 0;
3710 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003711 result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003712 ^ __first[__n - 1]);
3713 __first[__p] += __r;
3714 __r += __s;
3715 __first[__q] += __r;
3716 __first[0] = __r;
3717 }
3718 for (size_t __k = 1; __k <= __s; ++__k)
3719 {
3720 const size_t __kmodn = __k % __n;
3721 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003722 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723 ^ __first[(__k - 1) % __n]);
3724 __first[__kpmodn] += __r;
3725 __r += __kmodn + __v_[__k-1];
3726 __first[(__k + __q) % __n] += __r;
3727 __first[__kmodn] = __r;
3728 }
3729 for (size_t __k = __s + 1; __k < __m; ++__k)
3730 {
3731 const size_t __kmodn = __k % __n;
3732 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003733 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734 ^ __first[(__k - 1) % __n]);
3735 __first[__kpmodn] += __r;
3736 __r += __kmodn;
3737 __first[(__k + __q) % __n] += __r;
3738 __first[__kmodn] = __r;
3739 }
3740 for (size_t __k = __m; __k < __m + __n; ++__k)
3741 {
3742 const size_t __kmodn = __k % __n;
3743 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003744 result_type __r = 1566083941 * _Tp(__first[__kmodn] +
Howard Hinnantc51e1022010-05-11 19:42:16 +00003745 __first[__kpmodn] +
3746 __first[(__k - 1) % __n]);
3747 __first[__kpmodn] ^= __r;
3748 __r -= __kmodn;
3749 __first[(__k + __q) % __n] ^= __r;
3750 __first[__kmodn] = __r;
3751 }
3752 }
3753}
3754
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003755// generate_canonical
3756
Howard Hinnantc51e1022010-05-11 19:42:16 +00003757template<class _RealType, size_t __bits, class _URNG>
3758_RealType
3759generate_canonical(_URNG& __g)
3760{
3761 const size_t _Dt = numeric_limits<_RealType>::digits;
3762 const size_t __b = _Dt < __bits ? _Dt : __bits;
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003763#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
Howard Hinnant5a646852012-04-02 21:00:45 +00003765#else
3766 const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
3767#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
Louis Dionne2422bdb2019-08-20 15:39:20 +00003769 const _RealType _Rp = static_cast<_RealType>(_URNG::max() - _URNG::min()) + _RealType(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00003770 _RealType __base = _Rp;
Howard Hinnant5a646852012-04-02 21:00:45 +00003771 _RealType _Sp = __g() - _URNG::min();
Howard Hinnantc834c512011-11-29 18:15:50 +00003772 for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp)
Howard Hinnant5a646852012-04-02 21:00:45 +00003773 _Sp += (__g() - _URNG::min()) * __base;
Howard Hinnantc834c512011-11-29 18:15:50 +00003774 return _Sp / __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775}
3776
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003777// uniform_real_distribution
3778
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003780class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781{
3782public:
3783 // types
3784 typedef _RealType result_type;
3785
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003786 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787 {
3788 result_type __a_;
3789 result_type __b_;
3790 public:
3791 typedef uniform_real_distribution distribution_type;
3792
Howard Hinnantf5f99992010-09-22 18:02:38 +00003793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794 explicit param_type(result_type __a = 0,
3795 result_type __b = 1)
3796 : __a_(__a), __b_(__b) {}
3797
Howard Hinnantf5f99992010-09-22 18:02:38 +00003798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801 result_type b() const {return __b_;}
3802
Howard Hinnantf5f99992010-09-22 18:02:38 +00003803 friend _LIBCPP_INLINE_VISIBILITY
3804 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003805 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003806 friend _LIBCPP_INLINE_VISIBILITY
3807 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003808 {return !(__x == __y);}
3809 };
3810
3811private:
3812 param_type __p_;
3813
3814public:
3815 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003816#ifndef _LIBCPP_CXX03_LANG
3817 _LIBCPP_INLINE_VISIBILITY
3818 uniform_real_distribution() : uniform_real_distribution(0) {}
3819 explicit uniform_real_distribution(result_type __a, result_type __b = 1)
3820 : __p_(param_type(__a, __b)) {}
3821#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00003822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003823 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3824 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003825#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00003826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003829 void reset() {}
3830
3831 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003832 template<class _URNG>
3833 _LIBCPP_INLINE_VISIBILITY
3834 result_type operator()(_URNG& __g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003836 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837
3838 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842 result_type b() const {return __p_.b();}
3843
Howard Hinnantf5f99992010-09-22 18:02:38 +00003844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847 void param(const param_type& __p) {__p_ = __p;}
3848
Howard Hinnantf5f99992010-09-22 18:02:38 +00003849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003850 result_type min() const {return a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003852 result_type max() const {return b();}
3853
Howard Hinnantf5f99992010-09-22 18:02:38 +00003854 friend _LIBCPP_INLINE_VISIBILITY
3855 bool operator==(const uniform_real_distribution& __x,
3856 const uniform_real_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003858 friend _LIBCPP_INLINE_VISIBILITY
3859 bool operator!=(const uniform_real_distribution& __x,
3860 const uniform_real_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861 {return !(__x == __y);}
3862};
3863
3864template<class _RealType>
3865template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003866inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867typename uniform_real_distribution<_RealType>::result_type
3868uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3869{
3870 return (__p.b() - __p.a())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003871 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872 + __p.a();
3873}
3874
3875template <class _CharT, class _Traits, class _RT>
3876basic_ostream<_CharT, _Traits>&
3877operator<<(basic_ostream<_CharT, _Traits>& __os,
3878 const uniform_real_distribution<_RT>& __x)
3879{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003880 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003881 typedef basic_ostream<_CharT, _Traits> _OStream;
3882 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
3883 _OStream::scientific);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884 _CharT __sp = __os.widen(' ');
3885 __os.fill(__sp);
3886 return __os << __x.a() << __sp << __x.b();
3887}
3888
3889template <class _CharT, class _Traits, class _RT>
3890basic_istream<_CharT, _Traits>&
3891operator>>(basic_istream<_CharT, _Traits>& __is,
3892 uniform_real_distribution<_RT>& __x)
3893{
3894 typedef uniform_real_distribution<_RT> _Eng;
3895 typedef typename _Eng::result_type result_type;
3896 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003897 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003898 typedef basic_istream<_CharT, _Traits> _Istream;
3899 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900 result_type __a;
3901 result_type __b;
3902 __is >> __a >> __b;
3903 if (!__is.fail())
3904 __x.param(param_type(__a, __b));
3905 return __is;
3906}
3907
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003908// bernoulli_distribution
3909
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003910class _LIBCPP_TEMPLATE_VIS bernoulli_distribution
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911{
3912public:
3913 // types
3914 typedef bool result_type;
3915
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003916 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917 {
3918 double __p_;
3919 public:
3920 typedef bernoulli_distribution distribution_type;
3921
Howard Hinnantf5f99992010-09-22 18:02:38 +00003922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003923 explicit param_type(double __p = 0.5) : __p_(__p) {}
3924
Howard Hinnantf5f99992010-09-22 18:02:38 +00003925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926 double p() const {return __p_;}
3927
Howard Hinnantf5f99992010-09-22 18:02:38 +00003928 friend _LIBCPP_INLINE_VISIBILITY
3929 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003931 friend _LIBCPP_INLINE_VISIBILITY
3932 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003933 {return !(__x == __y);}
3934 };
3935
3936private:
3937 param_type __p_;
3938
3939public:
3940 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003941#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003942 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003943 bernoulli_distribution() : bernoulli_distribution(0.5) {}
3944 _LIBCPP_INLINE_VISIBILITY
3945 explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {}
3946#else
3947 _LIBCPP_INLINE_VISIBILITY
3948 explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}
3949#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00003950 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003951 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003952 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003953 void reset() {}
3954
3955 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003956 template<class _URNG>
3957 _LIBCPP_INLINE_VISIBILITY
3958 result_type operator()(_URNG& __g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003959 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003960 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961
3962 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003964 double p() const {return __p_.p();}
3965
Howard Hinnantf5f99992010-09-22 18:02:38 +00003966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003967 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969 void param(const param_type& __p) {__p_ = __p;}
3970
Howard Hinnantf5f99992010-09-22 18:02:38 +00003971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972 result_type min() const {return false;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974 result_type max() const {return true;}
3975
Howard Hinnantf5f99992010-09-22 18:02:38 +00003976 friend _LIBCPP_INLINE_VISIBILITY
3977 bool operator==(const bernoulli_distribution& __x,
3978 const bernoulli_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003980 friend _LIBCPP_INLINE_VISIBILITY
3981 bool operator!=(const bernoulli_distribution& __x,
3982 const bernoulli_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983 {return !(__x == __y);}
3984};
3985
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003986template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003987inline
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003988bernoulli_distribution::result_type
3989bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3990{
Howard Hinnant481ba382010-05-20 15:11:46 +00003991 uniform_real_distribution<double> __gen;
3992 return __gen(__g) < __p.p();
Howard Hinnant53f28fa2010-05-11 23:26:59 +00003993}
3994
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995template <class _CharT, class _Traits>
3996basic_ostream<_CharT, _Traits>&
3997operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3998{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003999 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004000 typedef basic_ostream<_CharT, _Traits> _OStream;
4001 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4002 _OStream::scientific);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003 _CharT __sp = __os.widen(' ');
4004 __os.fill(__sp);
4005 return __os << __x.p();
4006}
4007
4008template <class _CharT, class _Traits>
4009basic_istream<_CharT, _Traits>&
4010operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
4011{
4012 typedef bernoulli_distribution _Eng;
4013 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004014 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004015 typedef basic_istream<_CharT, _Traits> _Istream;
4016 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017 double __p;
4018 __is >> __p;
4019 if (!__is.fail())
4020 __x.param(param_type(__p));
4021 return __is;
4022}
4023
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004024// binomial_distribution
4025
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004026template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004027class _LIBCPP_TEMPLATE_VIS binomial_distribution
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004028{
4029public:
4030 // types
4031 typedef _IntType result_type;
4032
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004033 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004034 {
4035 result_type __t_;
4036 double __p_;
Howard Hinnant76310352010-05-15 21:36:23 +00004037 double __pr_;
4038 double __odds_ratio_;
4039 result_type __r0_;
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004040 public:
4041 typedef binomial_distribution distribution_type;
4042
Howard Hinnant76310352010-05-15 21:36:23 +00004043 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004044
Howard Hinnantf5f99992010-09-22 18:02:38 +00004045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004046 result_type t() const {return __t_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004048 double p() const {return __p_;}
4049
Howard Hinnantf5f99992010-09-22 18:02:38 +00004050 friend _LIBCPP_INLINE_VISIBILITY
4051 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004052 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004053 friend _LIBCPP_INLINE_VISIBILITY
4054 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004055 {return !(__x == __y);}
Howard Hinnant76310352010-05-15 21:36:23 +00004056
4057 friend class binomial_distribution;
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004058 };
4059
4060private:
4061 param_type __p_;
4062
4063public:
4064 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004065#ifndef _LIBCPP_CXX03_LANG
4066 _LIBCPP_INLINE_VISIBILITY
4067 binomial_distribution() : binomial_distribution(1) {}
4068 _LIBCPP_INLINE_VISIBILITY
4069 explicit binomial_distribution(result_type __t, double __p = 0.5)
4070 : __p_(param_type(__t, __p)) {}
4071#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00004072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004073 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
4074 : __p_(param_type(__t, __p)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004075#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004077 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004079 void reset() {}
4080
4081 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004082 template<class _URNG>
4083 _LIBCPP_INLINE_VISIBILITY
4084 result_type operator()(_URNG& __g)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004085 {return (*this)(__g, __p_);}
4086 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4087
4088 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004090 result_type t() const {return __p_.t();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004091 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004092 double p() const {return __p_.p();}
4093
Howard Hinnantf5f99992010-09-22 18:02:38 +00004094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004095 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004097 void param(const param_type& __p) {__p_ = __p;}
4098
Howard Hinnantf5f99992010-09-22 18:02:38 +00004099 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004100 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004102 result_type max() const {return t();}
4103
Howard Hinnantf5f99992010-09-22 18:02:38 +00004104 friend _LIBCPP_INLINE_VISIBILITY
4105 bool operator==(const binomial_distribution& __x,
4106 const binomial_distribution& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004107 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004108 friend _LIBCPP_INLINE_VISIBILITY
4109 bool operator!=(const binomial_distribution& __x,
4110 const binomial_distribution& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004111 {return !(__x == __y);}
4112};
4113
Martin Storsjö408cee72020-12-01 13:37:16 +02004114#ifndef _LIBCPP_MSVCRT_LIKE
Marshall Clow7db28572017-05-04 16:36:39 +00004115extern "C" double lgamma_r(double, int *);
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004116#endif
4117
4118inline _LIBCPP_INLINE_VISIBILITY double __libcpp_lgamma(double __d) {
Martin Storsjö408cee72020-12-01 13:37:16 +02004119#if defined(_LIBCPP_MSVCRT_LIKE)
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004120 return lgamma(__d);
4121#else
4122 int __sign;
4123 return lgamma_r(__d, &__sign);
4124#endif
4125}
Marshall Clow7db28572017-05-04 16:36:39 +00004126
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004127template<class _IntType>
Marshall Clow7db28572017-05-04 16:36:39 +00004128binomial_distribution<_IntType>::param_type::param_type(const result_type __t, const double __p)
Howard Hinnant76310352010-05-15 21:36:23 +00004129 : __t_(__t), __p_(__p)
4130{
4131 if (0 < __p_ && __p_ < 1)
4132 {
4133 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004134 __pr_ = _VSTD::exp(__libcpp_lgamma(__t_ + 1.) -
4135 __libcpp_lgamma(__r0_ + 1.) -
4136 __libcpp_lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
Marshall Clow7db28572017-05-04 16:36:39 +00004137 (__t_ - __r0_) * _VSTD::log(1 - __p_));
Howard Hinnant76310352010-05-15 21:36:23 +00004138 __odds_ratio_ = __p_ / (1 - __p_);
4139 }
4140}
4141
Marshall Clowf97a84f2014-09-17 18:33:58 +00004142// Reference: Kemp, C.D. (1986). `A modal method for generating binomial
4143// variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
Howard Hinnant76310352010-05-15 21:36:23 +00004144template<class _IntType>
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004145template<class _URNG>
4146_IntType
Howard Hinnant76310352010-05-15 21:36:23 +00004147binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004148{
Howard Hinnant76310352010-05-15 21:36:23 +00004149 if (__pr.__t_ == 0 || __pr.__p_ == 0)
4150 return 0;
4151 if (__pr.__p_ == 1)
4152 return __pr.__t_;
4153 uniform_real_distribution<double> __gen;
4154 double __u = __gen(__g) - __pr.__pr_;
4155 if (__u < 0)
4156 return __pr.__r0_;
4157 double __pu = __pr.__pr_;
4158 double __pd = __pu;
4159 result_type __ru = __pr.__r0_;
4160 result_type __rd = __ru;
4161 while (true)
4162 {
Atmn Patelda0b1b62020-03-17 15:52:36 -04004163 bool __break = true;
Howard Hinnant76310352010-05-15 21:36:23 +00004164 if (__rd >= 1)
4165 {
4166 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
4167 __u -= __pd;
Atmn Patelda0b1b62020-03-17 15:52:36 -04004168 __break = false;
Howard Hinnant76310352010-05-15 21:36:23 +00004169 if (__u < 0)
4170 return __rd - 1;
4171 }
Marshall Clowf97a84f2014-09-17 18:33:58 +00004172 if ( __rd != 0 )
4173 --__rd;
Howard Hinnant76310352010-05-15 21:36:23 +00004174 ++__ru;
4175 if (__ru <= __pr.__t_)
4176 {
4177 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
4178 __u -= __pu;
Atmn Patelda0b1b62020-03-17 15:52:36 -04004179 __break = false;
Howard Hinnant76310352010-05-15 21:36:23 +00004180 if (__u < 0)
4181 return __ru;
4182 }
Atmn Patelda0b1b62020-03-17 15:52:36 -04004183 if (__break)
4184 return 0;
Howard Hinnant76310352010-05-15 21:36:23 +00004185 }
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004186}
4187
4188template <class _CharT, class _Traits, class _IntType>
4189basic_ostream<_CharT, _Traits>&
4190operator<<(basic_ostream<_CharT, _Traits>& __os,
4191 const binomial_distribution<_IntType>& __x)
4192{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004193 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004194 typedef basic_ostream<_CharT, _Traits> _OStream;
4195 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4196 _OStream::scientific);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004197 _CharT __sp = __os.widen(' ');
4198 __os.fill(__sp);
4199 return __os << __x.t() << __sp << __x.p();
4200}
4201
4202template <class _CharT, class _Traits, class _IntType>
4203basic_istream<_CharT, _Traits>&
4204operator>>(basic_istream<_CharT, _Traits>& __is,
4205 binomial_distribution<_IntType>& __x)
4206{
4207 typedef binomial_distribution<_IntType> _Eng;
4208 typedef typename _Eng::result_type result_type;
4209 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004210 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004211 typedef basic_istream<_CharT, _Traits> _Istream;
4212 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004213 result_type __t;
4214 double __p;
4215 __is >> __t >> __p;
4216 if (!__is.fail())
4217 __x.param(param_type(__t, __p));
4218 return __is;
4219}
4220
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004221// exponential_distribution
4222
4223template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004224class _LIBCPP_TEMPLATE_VIS exponential_distribution
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004225{
4226public:
4227 // types
4228 typedef _RealType result_type;
4229
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004230 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004231 {
4232 result_type __lambda_;
4233 public:
4234 typedef exponential_distribution distribution_type;
4235
Howard Hinnantf5f99992010-09-22 18:02:38 +00004236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004237 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
4238
Howard Hinnantf5f99992010-09-22 18:02:38 +00004239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004240 result_type lambda() const {return __lambda_;}
4241
Howard Hinnantf5f99992010-09-22 18:02:38 +00004242 friend _LIBCPP_INLINE_VISIBILITY
4243 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004244 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004245 friend _LIBCPP_INLINE_VISIBILITY
4246 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004247 {return !(__x == __y);}
4248 };
4249
4250private:
4251 param_type __p_;
4252
4253public:
4254 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004255#ifndef _LIBCPP_CXX03_LANG
4256 _LIBCPP_INLINE_VISIBILITY
4257 exponential_distribution() : exponential_distribution(1) {}
4258 _LIBCPP_INLINE_VISIBILITY
4259 explicit exponential_distribution(result_type __lambda)
4260 : __p_(param_type(__lambda)) {}
4261#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00004262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004263 explicit exponential_distribution(result_type __lambda = 1)
4264 : __p_(param_type(__lambda)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004265#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004267 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004269 void reset() {}
4270
4271 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004272 template<class _URNG>
4273 _LIBCPP_INLINE_VISIBILITY
4274 result_type operator()(_URNG& __g)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004275 {return (*this)(__g, __p_);}
4276 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4277
4278 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004280 result_type lambda() const {return __p_.lambda();}
4281
Howard Hinnantf5f99992010-09-22 18:02:38 +00004282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004283 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004285 void param(const param_type& __p) {__p_ = __p;}
4286
Howard Hinnantf5f99992010-09-22 18:02:38 +00004287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004288 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant021f9902010-05-16 17:56:20 +00004290 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004291
Howard Hinnantf5f99992010-09-22 18:02:38 +00004292 friend _LIBCPP_INLINE_VISIBILITY
4293 bool operator==(const exponential_distribution& __x,
4294 const exponential_distribution& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004295 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004296 friend _LIBCPP_INLINE_VISIBILITY
4297 bool operator!=(const exponential_distribution& __x,
4298 const exponential_distribution& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004299 {return !(__x == __y);}
4300};
4301
4302template <class _RealType>
4303template<class _URNG>
4304_RealType
4305exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4306{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004307 return -_VSTD::log
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004308 (
4309 result_type(1) -
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004310 _VSTD::generate_canonical<result_type,
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004311 numeric_limits<result_type>::digits>(__g)
4312 )
4313 / __p.lambda();
4314}
4315
4316template <class _CharT, class _Traits, class _RealType>
4317basic_ostream<_CharT, _Traits>&
4318operator<<(basic_ostream<_CharT, _Traits>& __os,
4319 const exponential_distribution<_RealType>& __x)
4320{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004321 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004322 typedef basic_ostream<_CharT, _Traits> _OStream;
4323 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4324 _OStream::scientific);
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004325 return __os << __x.lambda();
4326}
4327
4328template <class _CharT, class _Traits, class _RealType>
4329basic_istream<_CharT, _Traits>&
4330operator>>(basic_istream<_CharT, _Traits>& __is,
4331 exponential_distribution<_RealType>& __x)
4332{
4333 typedef exponential_distribution<_RealType> _Eng;
4334 typedef typename _Eng::result_type result_type;
4335 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004336 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004337 typedef basic_istream<_CharT, _Traits> _Istream;
4338 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004339 result_type __lambda;
4340 __is >> __lambda;
4341 if (!__is.fail())
4342 __x.param(param_type(__lambda));
4343 return __is;
4344}
4345
Howard Hinnant76310352010-05-15 21:36:23 +00004346// normal_distribution
4347
4348template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004349class _LIBCPP_TEMPLATE_VIS normal_distribution
Howard Hinnant76310352010-05-15 21:36:23 +00004350{
4351public:
4352 // types
4353 typedef _RealType result_type;
4354
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004355 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant76310352010-05-15 21:36:23 +00004356 {
4357 result_type __mean_;
4358 result_type __stddev_;
4359 public:
4360 typedef normal_distribution distribution_type;
4361
Howard Hinnantf5f99992010-09-22 18:02:38 +00004362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004363 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4364 : __mean_(__mean), __stddev_(__stddev) {}
4365
Howard Hinnantf5f99992010-09-22 18:02:38 +00004366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004367 result_type mean() const {return __mean_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004369 result_type stddev() const {return __stddev_;}
4370
Howard Hinnantf5f99992010-09-22 18:02:38 +00004371 friend _LIBCPP_INLINE_VISIBILITY
4372 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004373 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004374 friend _LIBCPP_INLINE_VISIBILITY
4375 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004376 {return !(__x == __y);}
4377 };
4378
4379private:
4380 param_type __p_;
4381 result_type _V_;
4382 bool _V_hot_;
4383
4384public:
4385 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004386#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00004387 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004388 normal_distribution() : normal_distribution(0) {}
4389 _LIBCPP_INLINE_VISIBILITY
4390 explicit normal_distribution(result_type __mean, result_type __stddev = 1)
Howard Hinnant76310352010-05-15 21:36:23 +00004391 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004392#else
4393 _LIBCPP_INLINE_VISIBILITY
4394 explicit normal_distribution(result_type __mean = 0,
4395 result_type __stddev = 1)
4396 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
4397#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004399 explicit normal_distribution(const param_type& __p)
4400 : __p_(__p), _V_hot_(false) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004402 void reset() {_V_hot_ = false;}
4403
4404 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004405 template<class _URNG>
4406 _LIBCPP_INLINE_VISIBILITY
4407 result_type operator()(_URNG& __g)
Howard Hinnant76310352010-05-15 21:36:23 +00004408 {return (*this)(__g, __p_);}
4409 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4410
4411 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004413 result_type mean() const {return __p_.mean();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004415 result_type stddev() const {return __p_.stddev();}
4416
Howard Hinnantf5f99992010-09-22 18:02:38 +00004417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004418 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004420 void param(const param_type& __p) {__p_ = __p;}
4421
Howard Hinnantf5f99992010-09-22 18:02:38 +00004422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004423 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004425 result_type max() const {return numeric_limits<result_type>::infinity();}
4426
Howard Hinnantf5f99992010-09-22 18:02:38 +00004427 friend _LIBCPP_INLINE_VISIBILITY
4428 bool operator==(const normal_distribution& __x,
4429 const normal_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004430 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4431 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004432 friend _LIBCPP_INLINE_VISIBILITY
4433 bool operator!=(const normal_distribution& __x,
4434 const normal_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004435 {return !(__x == __y);}
4436
4437 template <class _CharT, class _Traits, class _RT>
4438 friend
4439 basic_ostream<_CharT, _Traits>&
4440 operator<<(basic_ostream<_CharT, _Traits>& __os,
4441 const normal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004442
Howard Hinnant76310352010-05-15 21:36:23 +00004443 template <class _CharT, class _Traits, class _RT>
4444 friend
4445 basic_istream<_CharT, _Traits>&
4446 operator>>(basic_istream<_CharT, _Traits>& __is,
4447 normal_distribution<_RT>& __x);
4448};
4449
4450template <class _RealType>
4451template<class _URNG>
4452_RealType
4453normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4454{
Howard Hinnantc834c512011-11-29 18:15:50 +00004455 result_type _Up;
Howard Hinnant76310352010-05-15 21:36:23 +00004456 if (_V_hot_)
4457 {
4458 _V_hot_ = false;
Howard Hinnantc834c512011-11-29 18:15:50 +00004459 _Up = _V_;
Howard Hinnant76310352010-05-15 21:36:23 +00004460 }
4461 else
4462 {
4463 uniform_real_distribution<result_type> _Uni(-1, 1);
4464 result_type __u;
4465 result_type __v;
4466 result_type __s;
4467 do
4468 {
4469 __u = _Uni(__g);
4470 __v = _Uni(__g);
4471 __s = __u * __u + __v * __v;
4472 } while (__s > 1 || __s == 0);
Howard Hinnantc834c512011-11-29 18:15:50 +00004473 result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
4474 _V_ = __v * _Fp;
Howard Hinnant76310352010-05-15 21:36:23 +00004475 _V_hot_ = true;
Howard Hinnantc834c512011-11-29 18:15:50 +00004476 _Up = __u * _Fp;
Howard Hinnant76310352010-05-15 21:36:23 +00004477 }
Howard Hinnantc834c512011-11-29 18:15:50 +00004478 return _Up * __p.stddev() + __p.mean();
Howard Hinnant76310352010-05-15 21:36:23 +00004479}
4480
4481template <class _CharT, class _Traits, class _RT>
4482basic_ostream<_CharT, _Traits>&
4483operator<<(basic_ostream<_CharT, _Traits>& __os,
4484 const normal_distribution<_RT>& __x)
4485{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004486 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004487 typedef basic_ostream<_CharT, _Traits> _OStream;
4488 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4489 _OStream::scientific);
Howard Hinnant76310352010-05-15 21:36:23 +00004490 _CharT __sp = __os.widen(' ');
4491 __os.fill(__sp);
4492 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4493 if (__x._V_hot_)
4494 __os << __sp << __x._V_;
4495 return __os;
4496}
4497
4498template <class _CharT, class _Traits, class _RT>
4499basic_istream<_CharT, _Traits>&
4500operator>>(basic_istream<_CharT, _Traits>& __is,
4501 normal_distribution<_RT>& __x)
4502{
4503 typedef normal_distribution<_RT> _Eng;
4504 typedef typename _Eng::result_type result_type;
4505 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004506 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004507 typedef basic_istream<_CharT, _Traits> _Istream;
4508 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant76310352010-05-15 21:36:23 +00004509 result_type __mean;
4510 result_type __stddev;
Howard Hinnantc834c512011-11-29 18:15:50 +00004511 result_type _Vp = 0;
Howard Hinnant76310352010-05-15 21:36:23 +00004512 bool _V_hot = false;
4513 __is >> __mean >> __stddev >> _V_hot;
4514 if (_V_hot)
Howard Hinnantc834c512011-11-29 18:15:50 +00004515 __is >> _Vp;
Howard Hinnant76310352010-05-15 21:36:23 +00004516 if (!__is.fail())
4517 {
4518 __x.param(param_type(__mean, __stddev));
4519 __x._V_hot_ = _V_hot;
Howard Hinnantc834c512011-11-29 18:15:50 +00004520 __x._V_ = _Vp;
Howard Hinnant76310352010-05-15 21:36:23 +00004521 }
4522 return __is;
4523}
4524
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004525// lognormal_distribution
4526
4527template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004528class _LIBCPP_TEMPLATE_VIS lognormal_distribution
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004529{
4530public:
4531 // types
4532 typedef _RealType result_type;
4533
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004534 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004535 {
4536 normal_distribution<result_type> __nd_;
4537 public:
4538 typedef lognormal_distribution distribution_type;
4539
Howard Hinnantf5f99992010-09-22 18:02:38 +00004540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004541 explicit param_type(result_type __m = 0, result_type __s = 1)
4542 : __nd_(__m, __s) {}
4543
Howard Hinnantf5f99992010-09-22 18:02:38 +00004544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004545 result_type m() const {return __nd_.mean();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004547 result_type s() const {return __nd_.stddev();}
4548
Howard Hinnantf5f99992010-09-22 18:02:38 +00004549 friend _LIBCPP_INLINE_VISIBILITY
4550 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004551 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004552 friend _LIBCPP_INLINE_VISIBILITY
4553 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004554 {return !(__x == __y);}
4555 friend class lognormal_distribution;
4556
4557 template <class _CharT, class _Traits, class _RT>
4558 friend
4559 basic_ostream<_CharT, _Traits>&
4560 operator<<(basic_ostream<_CharT, _Traits>& __os,
4561 const lognormal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004562
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004563 template <class _CharT, class _Traits, class _RT>
4564 friend
4565 basic_istream<_CharT, _Traits>&
4566 operator>>(basic_istream<_CharT, _Traits>& __is,
4567 lognormal_distribution<_RT>& __x);
4568 };
4569
4570private:
4571 param_type __p_;
4572
4573public:
4574 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004575#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00004576 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004577 lognormal_distribution() : lognormal_distribution(0) {}
4578 _LIBCPP_INLINE_VISIBILITY
4579 explicit lognormal_distribution(result_type __m, result_type __s = 1)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004580 : __p_(param_type(__m, __s)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004581#else
4582 _LIBCPP_INLINE_VISIBILITY
4583 explicit lognormal_distribution(result_type __m = 0,
4584 result_type __s = 1)
4585 : __p_(param_type(__m, __s)) {}
4586#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004588 explicit lognormal_distribution(const param_type& __p)
4589 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004591 void reset() {__p_.__nd_.reset();}
4592
4593 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004594 template<class _URNG>
4595 _LIBCPP_INLINE_VISIBILITY
4596 result_type operator()(_URNG& __g)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004597 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004598 template<class _URNG>
4599 _LIBCPP_INLINE_VISIBILITY
4600 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004601 {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004602
4603 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004605 result_type m() const {return __p_.m();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004607 result_type s() const {return __p_.s();}
4608
Howard Hinnantf5f99992010-09-22 18:02:38 +00004609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004610 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00004612 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004613
Howard Hinnantf5f99992010-09-22 18:02:38 +00004614 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004615 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004617 result_type max() const {return numeric_limits<result_type>::infinity();}
4618
Howard Hinnantf5f99992010-09-22 18:02:38 +00004619 friend _LIBCPP_INLINE_VISIBILITY
4620 bool operator==(const lognormal_distribution& __x,
4621 const lognormal_distribution& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004622 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004623 friend _LIBCPP_INLINE_VISIBILITY
4624 bool operator!=(const lognormal_distribution& __x,
4625 const lognormal_distribution& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004626 {return !(__x == __y);}
4627
4628 template <class _CharT, class _Traits, class _RT>
4629 friend
4630 basic_ostream<_CharT, _Traits>&
4631 operator<<(basic_ostream<_CharT, _Traits>& __os,
4632 const lognormal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004633
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004634 template <class _CharT, class _Traits, class _RT>
4635 friend
4636 basic_istream<_CharT, _Traits>&
4637 operator>>(basic_istream<_CharT, _Traits>& __is,
4638 lognormal_distribution<_RT>& __x);
4639};
4640
4641template <class _CharT, class _Traits, class _RT>
Howard Hinnantf5f99992010-09-22 18:02:38 +00004642inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004643basic_ostream<_CharT, _Traits>&
4644operator<<(basic_ostream<_CharT, _Traits>& __os,
4645 const lognormal_distribution<_RT>& __x)
4646{
4647 return __os << __x.__p_.__nd_;
4648}
4649
4650template <class _CharT, class _Traits, class _RT>
Howard Hinnantf5f99992010-09-22 18:02:38 +00004651inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004652basic_istream<_CharT, _Traits>&
4653operator>>(basic_istream<_CharT, _Traits>& __is,
4654 lognormal_distribution<_RT>& __x)
4655{
4656 return __is >> __x.__p_.__nd_;
4657}
4658
Howard Hinnant76310352010-05-15 21:36:23 +00004659// poisson_distribution
4660
4661template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004662class _LIBCPP_TEMPLATE_VIS poisson_distribution
Howard Hinnant76310352010-05-15 21:36:23 +00004663{
4664public:
4665 // types
4666 typedef _IntType result_type;
4667
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004668 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant76310352010-05-15 21:36:23 +00004669 {
4670 double __mean_;
4671 double __s_;
4672 double __d_;
4673 double __l_;
4674 double __omega_;
4675 double __c0_;
4676 double __c1_;
4677 double __c2_;
4678 double __c3_;
4679 double __c_;
4680
4681 public:
4682 typedef poisson_distribution distribution_type;
4683
4684 explicit param_type(double __mean = 1.0);
4685
Howard Hinnantf5f99992010-09-22 18:02:38 +00004686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004687 double mean() const {return __mean_;}
4688
Howard Hinnantf5f99992010-09-22 18:02:38 +00004689 friend _LIBCPP_INLINE_VISIBILITY
4690 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004691 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004692 friend _LIBCPP_INLINE_VISIBILITY
4693 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004694 {return !(__x == __y);}
4695
4696 friend class poisson_distribution;
4697 };
4698
4699private:
4700 param_type __p_;
4701
4702public:
4703 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004704#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00004705 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004706 poisson_distribution() : poisson_distribution(1.0) {}
4707 _LIBCPP_INLINE_VISIBILITY
4708 explicit poisson_distribution(double __mean)
4709 : __p_(__mean) {}
4710#else
4711 _LIBCPP_INLINE_VISIBILITY
4712 explicit poisson_distribution(double __mean = 1.0)
4713 : __p_(__mean) {}
4714#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004716 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004718 void reset() {}
4719
4720 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004721 template<class _URNG>
4722 _LIBCPP_INLINE_VISIBILITY
4723 result_type operator()(_URNG& __g)
Howard Hinnant76310352010-05-15 21:36:23 +00004724 {return (*this)(__g, __p_);}
4725 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4726
4727 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004729 double mean() const {return __p_.mean();}
4730
Howard Hinnantf5f99992010-09-22 18:02:38 +00004731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004732 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004734 void param(const param_type& __p) {__p_ = __p;}
4735
Howard Hinnantf5f99992010-09-22 18:02:38 +00004736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004737 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004739 result_type max() const {return numeric_limits<result_type>::max();}
4740
Howard Hinnantf5f99992010-09-22 18:02:38 +00004741 friend _LIBCPP_INLINE_VISIBILITY
4742 bool operator==(const poisson_distribution& __x,
4743 const poisson_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004744 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004745 friend _LIBCPP_INLINE_VISIBILITY
4746 bool operator!=(const poisson_distribution& __x,
4747 const poisson_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004748 {return !(__x == __y);}
4749};
4750
4751template<class _IntType>
4752poisson_distribution<_IntType>::param_type::param_type(double __mean)
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004753 // According to the standard `inf` is a valid input, but it causes the
4754 // distribution to hang, so we replace it with the maximum representable
4755 // mean.
4756 : __mean_(isinf(__mean) ? numeric_limits<double>::max() : __mean)
Howard Hinnant76310352010-05-15 21:36:23 +00004757{
4758 if (__mean_ < 10)
4759 {
4760 __s_ = 0;
4761 __d_ = 0;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004762 __l_ = _VSTD::exp(-__mean_);
Howard Hinnant76310352010-05-15 21:36:23 +00004763 __omega_ = 0;
4764 __c3_ = 0;
4765 __c2_ = 0;
4766 __c1_ = 0;
4767 __c0_ = 0;
4768 __c_ = 0;
4769 }
4770 else
4771 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004772 __s_ = _VSTD::sqrt(__mean_);
Howard Hinnant76310352010-05-15 21:36:23 +00004773 __d_ = 6 * __mean_ * __mean_;
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004774 __l_ = _VSTD::trunc(__mean_ - 1.1484);
Howard Hinnant76310352010-05-15 21:36:23 +00004775 __omega_ = .3989423 / __s_;
4776 double __b1_ = .4166667E-1 / __mean_;
4777 double __b2_ = .3 * __b1_ * __b1_;
4778 __c3_ = .1428571 * __b1_ * __b2_;
4779 __c2_ = __b2_ - 15. * __c3_;
4780 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4781 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4782 __c_ = .1069 / __mean_;
4783 }
4784}
4785
4786template <class _IntType>
4787template<class _URNG>
4788_IntType
4789poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4790{
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004791 double __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004792 uniform_real_distribution<double> __urd;
Howard Hinnanta5fb7ac2011-04-14 15:59:22 +00004793 if (__pr.__mean_ < 10)
Howard Hinnant76310352010-05-15 21:36:23 +00004794 {
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004795 __tx = 0;
4796 for (double __p = __urd(__urng); __p > __pr.__l_; ++__tx)
Howard Hinnant76310352010-05-15 21:36:23 +00004797 __p *= __urd(__urng);
4798 }
4799 else
4800 {
4801 double __difmuk;
4802 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4803 double __u;
4804 if (__g > 0)
4805 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004806 __tx = _VSTD::trunc(__g);
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004807 if (__tx >= __pr.__l_)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004808 return _VSTD::__clamp_to_integral<result_type>(__tx);
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004809 __difmuk = __pr.__mean_ - __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004810 __u = __urd(__urng);
4811 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004812 return _VSTD::__clamp_to_integral<result_type>(__tx);
Howard Hinnant76310352010-05-15 21:36:23 +00004813 }
4814 exponential_distribution<double> __edist;
4815 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4816 {
4817 double __e;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004818 if (__using_exp_dist || __g <= 0)
Howard Hinnant76310352010-05-15 21:36:23 +00004819 {
4820 double __t;
4821 do
4822 {
4823 __e = __edist(__urng);
4824 __u = __urd(__urng);
4825 __u += __u - 1;
4826 __t = 1.8 + (__u < 0 ? -__e : __e);
4827 } while (__t <= -.6744);
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004828 __tx = _VSTD::trunc(__pr.__mean_ + __pr.__s_ * __t);
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004829 __difmuk = __pr.__mean_ - __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004830 __using_exp_dist = true;
4831 }
4832 double __px;
4833 double __py;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004834 if (__tx < 10 && __tx >= 0)
Howard Hinnant76310352010-05-15 21:36:23 +00004835 {
Marshall Clowc7e36e82018-01-16 14:54:36 +00004836 const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
Howard Hinnant76310352010-05-15 21:36:23 +00004837 40320, 362880};
4838 __px = -__pr.__mean_;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004839 __py = _VSTD::pow(__pr.__mean_, (double)__tx) / __fac[static_cast<int>(__tx)];
Howard Hinnant76310352010-05-15 21:36:23 +00004840 }
4841 else
4842 {
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004843 double __del = .8333333E-1 / __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004844 __del -= 4.8 * __del * __del * __del;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004845 double __v = __difmuk / __tx;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004846 if (_VSTD::abs(__v) > 0.25)
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004847 __px = __tx * _VSTD::log(1 + __v) - __difmuk - __del;
Howard Hinnant76310352010-05-15 21:36:23 +00004848 else
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004849 __px = __tx * __v * __v * (((((((.1250060 * __v + -.1384794) *
Howard Hinnant76310352010-05-15 21:36:23 +00004850 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4851 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004852 __py = .3989423 / _VSTD::sqrt(__tx);
Howard Hinnant76310352010-05-15 21:36:23 +00004853 }
4854 double __r = (0.5 - __difmuk) / __pr.__s_;
4855 double __r2 = __r * __r;
4856 double __fx = -0.5 * __r2;
4857 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4858 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4859 if (__using_exp_dist)
4860 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004861 if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4862 __fy * _VSTD::exp(__fx + __e))
Howard Hinnant76310352010-05-15 21:36:23 +00004863 break;
4864 }
4865 else
4866 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004867 if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
Howard Hinnant76310352010-05-15 21:36:23 +00004868 break;
4869 }
4870 }
4871 }
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004872 return _VSTD::__clamp_to_integral<result_type>(__tx);
Howard Hinnant76310352010-05-15 21:36:23 +00004873}
4874
4875template <class _CharT, class _Traits, class _IntType>
4876basic_ostream<_CharT, _Traits>&
4877operator<<(basic_ostream<_CharT, _Traits>& __os,
4878 const poisson_distribution<_IntType>& __x)
4879{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004880 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004881 typedef basic_ostream<_CharT, _Traits> _OStream;
4882 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4883 _OStream::scientific);
Howard Hinnant76310352010-05-15 21:36:23 +00004884 return __os << __x.mean();
4885}
4886
4887template <class _CharT, class _Traits, class _IntType>
4888basic_istream<_CharT, _Traits>&
4889operator>>(basic_istream<_CharT, _Traits>& __is,
4890 poisson_distribution<_IntType>& __x)
4891{
4892 typedef poisson_distribution<_IntType> _Eng;
4893 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004894 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004895 typedef basic_istream<_CharT, _Traits> _Istream;
4896 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant76310352010-05-15 21:36:23 +00004897 double __mean;
4898 __is >> __mean;
4899 if (!__is.fail())
4900 __x.param(param_type(__mean));
4901 return __is;
4902}
4903
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004904// weibull_distribution
4905
4906template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004907class _LIBCPP_TEMPLATE_VIS weibull_distribution
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004908{
4909public:
4910 // types
4911 typedef _RealType result_type;
4912
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004913 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004914 {
4915 result_type __a_;
4916 result_type __b_;
4917 public:
4918 typedef weibull_distribution distribution_type;
4919
Howard Hinnantf5f99992010-09-22 18:02:38 +00004920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004921 explicit param_type(result_type __a = 1, result_type __b = 1)
4922 : __a_(__a), __b_(__b) {}
4923
Howard Hinnantf5f99992010-09-22 18:02:38 +00004924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004925 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004927 result_type b() const {return __b_;}
4928
Howard Hinnantf5f99992010-09-22 18:02:38 +00004929 friend _LIBCPP_INLINE_VISIBILITY
4930 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004931 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004932 friend _LIBCPP_INLINE_VISIBILITY
4933 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004934 {return !(__x == __y);}
4935 };
4936
4937private:
4938 param_type __p_;
4939
4940public:
4941 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004942#ifndef _LIBCPP_CXX03_LANG
4943 _LIBCPP_INLINE_VISIBILITY
4944 weibull_distribution() : weibull_distribution(1) {}
4945 _LIBCPP_INLINE_VISIBILITY
4946 explicit weibull_distribution(result_type __a, result_type __b = 1)
4947 : __p_(param_type(__a, __b)) {}
4948#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00004949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004950 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4951 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004952#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004954 explicit weibull_distribution(const param_type& __p)
4955 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004957 void reset() {}
4958
4959 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004960 template<class _URNG>
4961 _LIBCPP_INLINE_VISIBILITY
4962 result_type operator()(_URNG& __g)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004963 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004964 template<class _URNG>
4965 _LIBCPP_INLINE_VISIBILITY
4966 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004967 {return __p.b() *
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004968 _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004969
4970 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004972 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004974 result_type b() const {return __p_.b();}
4975
Howard Hinnantf5f99992010-09-22 18:02:38 +00004976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004977 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004979 void param(const param_type& __p) {__p_ = __p;}
4980
Howard Hinnantf5f99992010-09-22 18:02:38 +00004981 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004982 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004984 result_type max() const {return numeric_limits<result_type>::infinity();}
4985
Howard Hinnantf5f99992010-09-22 18:02:38 +00004986 friend _LIBCPP_INLINE_VISIBILITY
4987 bool operator==(const weibull_distribution& __x,
4988 const weibull_distribution& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004989 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004990 friend _LIBCPP_INLINE_VISIBILITY
4991 bool operator!=(const weibull_distribution& __x,
4992 const weibull_distribution& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004993 {return !(__x == __y);}
4994};
4995
4996template <class _CharT, class _Traits, class _RT>
4997basic_ostream<_CharT, _Traits>&
4998operator<<(basic_ostream<_CharT, _Traits>& __os,
4999 const weibull_distribution<_RT>& __x)
5000{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005001 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005002 typedef basic_ostream<_CharT, _Traits> _OStream;
5003 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5004 _OStream::scientific);
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005005 _CharT __sp = __os.widen(' ');
5006 __os.fill(__sp);
5007 __os << __x.a() << __sp << __x.b();
5008 return __os;
5009}
5010
5011template <class _CharT, class _Traits, class _RT>
5012basic_istream<_CharT, _Traits>&
5013operator>>(basic_istream<_CharT, _Traits>& __is,
5014 weibull_distribution<_RT>& __x)
5015{
5016 typedef weibull_distribution<_RT> _Eng;
5017 typedef typename _Eng::result_type result_type;
5018 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005019 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005020 typedef basic_istream<_CharT, _Traits> _Istream;
5021 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005022 result_type __a;
5023 result_type __b;
5024 __is >> __a >> __b;
5025 if (!__is.fail())
5026 __x.param(param_type(__a, __b));
5027 return __is;
5028}
5029
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005030template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005031class _LIBCPP_TEMPLATE_VIS extreme_value_distribution
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005032{
5033public:
5034 // types
5035 typedef _RealType result_type;
5036
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005037 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005038 {
5039 result_type __a_;
5040 result_type __b_;
5041 public:
5042 typedef extreme_value_distribution distribution_type;
5043
Howard Hinnantf5f99992010-09-22 18:02:38 +00005044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005045 explicit param_type(result_type __a = 0, result_type __b = 1)
5046 : __a_(__a), __b_(__b) {}
5047
Howard Hinnantf5f99992010-09-22 18:02:38 +00005048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005049 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005051 result_type b() const {return __b_;}
5052
Howard Hinnantf5f99992010-09-22 18:02:38 +00005053 friend _LIBCPP_INLINE_VISIBILITY
5054 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005055 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005056 friend _LIBCPP_INLINE_VISIBILITY
5057 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005058 {return !(__x == __y);}
5059 };
5060
5061private:
5062 param_type __p_;
5063
5064public:
5065 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005066#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005067 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005068 extreme_value_distribution() : extreme_value_distribution(0) {}
5069 _LIBCPP_INLINE_VISIBILITY
5070 explicit extreme_value_distribution(result_type __a, result_type __b = 1)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005071 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005072#else
5073 _LIBCPP_INLINE_VISIBILITY
5074 explicit extreme_value_distribution(result_type __a = 0,
5075 result_type __b = 1)
5076 : __p_(param_type(__a, __b)) {}
5077#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005079 explicit extreme_value_distribution(const param_type& __p)
5080 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005082 void reset() {}
5083
5084 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005085 template<class _URNG>
5086 _LIBCPP_INLINE_VISIBILITY
5087 result_type operator()(_URNG& __g)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005088 {return (*this)(__g, __p_);}
5089 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5090
5091 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005092 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005093 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005095 result_type b() const {return __p_.b();}
5096
Howard Hinnantf5f99992010-09-22 18:02:38 +00005097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005098 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005099 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005100 void param(const param_type& __p) {__p_ = __p;}
5101
Howard Hinnantf5f99992010-09-22 18:02:38 +00005102 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005103 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005105 result_type max() const {return numeric_limits<result_type>::infinity();}
5106
Howard Hinnantf5f99992010-09-22 18:02:38 +00005107 friend _LIBCPP_INLINE_VISIBILITY
5108 bool operator==(const extreme_value_distribution& __x,
5109 const extreme_value_distribution& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005110 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005111 friend _LIBCPP_INLINE_VISIBILITY
5112 bool operator!=(const extreme_value_distribution& __x,
5113 const extreme_value_distribution& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005114 {return !(__x == __y);}
5115};
5116
5117template<class _RealType>
5118template<class _URNG>
5119_RealType
5120extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5121{
5122 return __p.a() - __p.b() *
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005123 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005124}
5125
5126template <class _CharT, class _Traits, class _RT>
5127basic_ostream<_CharT, _Traits>&
5128operator<<(basic_ostream<_CharT, _Traits>& __os,
5129 const extreme_value_distribution<_RT>& __x)
5130{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005131 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005132 typedef basic_ostream<_CharT, _Traits> _OStream;
5133 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5134 _OStream::scientific);
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005135 _CharT __sp = __os.widen(' ');
5136 __os.fill(__sp);
5137 __os << __x.a() << __sp << __x.b();
5138 return __os;
5139}
5140
5141template <class _CharT, class _Traits, class _RT>
5142basic_istream<_CharT, _Traits>&
5143operator>>(basic_istream<_CharT, _Traits>& __is,
5144 extreme_value_distribution<_RT>& __x)
5145{
5146 typedef extreme_value_distribution<_RT> _Eng;
5147 typedef typename _Eng::result_type result_type;
5148 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005149 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005150 typedef basic_istream<_CharT, _Traits> _Istream;
5151 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005152 result_type __a;
5153 result_type __b;
5154 __is >> __a >> __b;
5155 if (!__is.fail())
5156 __x.param(param_type(__a, __b));
5157 return __is;
5158}
5159
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005160// gamma_distribution
5161
5162template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005163class _LIBCPP_TEMPLATE_VIS gamma_distribution
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005164{
5165public:
5166 // types
5167 typedef _RealType result_type;
5168
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005169 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005170 {
5171 result_type __alpha_;
5172 result_type __beta_;
5173 public:
5174 typedef gamma_distribution distribution_type;
5175
Howard Hinnantf5f99992010-09-22 18:02:38 +00005176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005177 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
5178 : __alpha_(__alpha), __beta_(__beta) {}
5179
Howard Hinnantf5f99992010-09-22 18:02:38 +00005180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005181 result_type alpha() const {return __alpha_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005183 result_type beta() const {return __beta_;}
5184
Howard Hinnantf5f99992010-09-22 18:02:38 +00005185 friend _LIBCPP_INLINE_VISIBILITY
5186 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005187 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005188 friend _LIBCPP_INLINE_VISIBILITY
5189 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005190 {return !(__x == __y);}
5191 };
5192
5193private:
5194 param_type __p_;
5195
5196public:
5197 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005198#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005199 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005200 gamma_distribution() : gamma_distribution(1) {}
5201 _LIBCPP_INLINE_VISIBILITY
5202 explicit gamma_distribution(result_type __alpha, result_type __beta = 1)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005203 : __p_(param_type(__alpha, __beta)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005204#else
5205 _LIBCPP_INLINE_VISIBILITY
5206 explicit gamma_distribution(result_type __alpha = 1,
5207 result_type __beta = 1)
5208 : __p_(param_type(__alpha, __beta)) {}
5209#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005211 explicit gamma_distribution(const param_type& __p)
5212 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005214 void reset() {}
5215
5216 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005217 template<class _URNG>
5218 _LIBCPP_INLINE_VISIBILITY
5219 result_type operator()(_URNG& __g)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005220 {return (*this)(__g, __p_);}
5221 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5222
5223 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005225 result_type alpha() const {return __p_.alpha();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005227 result_type beta() const {return __p_.beta();}
5228
Howard Hinnantf5f99992010-09-22 18:02:38 +00005229 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005230 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005232 void param(const param_type& __p) {__p_ = __p;}
5233
Howard Hinnantf5f99992010-09-22 18:02:38 +00005234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005235 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005237 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005238
Howard Hinnantf5f99992010-09-22 18:02:38 +00005239 friend _LIBCPP_INLINE_VISIBILITY
5240 bool operator==(const gamma_distribution& __x,
5241 const gamma_distribution& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005242 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005243 friend _LIBCPP_INLINE_VISIBILITY
5244 bool operator!=(const gamma_distribution& __x,
5245 const gamma_distribution& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005246 {return !(__x == __y);}
5247};
5248
5249template <class _RealType>
5250template<class _URNG>
5251_RealType
5252gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5253{
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005254 result_type __a = __p.alpha();
5255 uniform_real_distribution<result_type> __gen(0, 1);
5256 exponential_distribution<result_type> __egen;
5257 result_type __x;
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005258 if (__a == 1)
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005259 __x = __egen(__g);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005260 else if (__a > 1)
5261 {
5262 const result_type __b = __a - 1;
5263 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005264 while (true)
5265 {
5266 const result_type __u = __gen(__g);
5267 const result_type __v = __gen(__g);
5268 const result_type __w = __u * (1 - __u);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005269 if (__w != 0)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005270 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005271 const result_type __y = _VSTD::sqrt(__c / __w) *
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005272 (__u - result_type(0.5));
5273 __x = __b + __y;
5274 if (__x >= 0)
5275 {
5276 const result_type __z = 64 * __w * __w * __w * __v * __v;
5277 if (__z <= 1 - 2 * __y * __y / __x)
5278 break;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005279 if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005280 break;
5281 }
5282 }
5283 }
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005284 }
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005285 else // __a < 1
5286 {
5287 while (true)
5288 {
5289 const result_type __u = __gen(__g);
5290 const result_type __es = __egen(__g);
5291 if (__u <= 1 - __a)
5292 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005293 __x = _VSTD::pow(__u, 1 / __a);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005294 if (__x <= __es)
5295 break;
5296 }
5297 else
5298 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005299 const result_type __e = -_VSTD::log((1-__u)/__a);
5300 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005301 if (__x <= __e + __es)
5302 break;
5303 }
5304 }
5305 }
5306 return __x * __p.beta();
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005307}
5308
5309template <class _CharT, class _Traits, class _RT>
5310basic_ostream<_CharT, _Traits>&
5311operator<<(basic_ostream<_CharT, _Traits>& __os,
5312 const gamma_distribution<_RT>& __x)
5313{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005314 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005315 typedef basic_ostream<_CharT, _Traits> _OStream;
5316 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5317 _OStream::scientific);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005318 _CharT __sp = __os.widen(' ');
5319 __os.fill(__sp);
5320 __os << __x.alpha() << __sp << __x.beta();
5321 return __os;
5322}
5323
5324template <class _CharT, class _Traits, class _RT>
5325basic_istream<_CharT, _Traits>&
5326operator>>(basic_istream<_CharT, _Traits>& __is,
5327 gamma_distribution<_RT>& __x)
5328{
5329 typedef gamma_distribution<_RT> _Eng;
5330 typedef typename _Eng::result_type result_type;
5331 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005332 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005333 typedef basic_istream<_CharT, _Traits> _Istream;
5334 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005335 result_type __alpha;
5336 result_type __beta;
5337 __is >> __alpha >> __beta;
5338 if (!__is.fail())
5339 __x.param(param_type(__alpha, __beta));
5340 return __is;
5341}
Howard Hinnant0b95bc92010-05-12 21:02:31 +00005342
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005343// negative_binomial_distribution
5344
5345template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005346class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005347{
5348public:
5349 // types
5350 typedef _IntType result_type;
5351
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005352 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005353 {
5354 result_type __k_;
5355 double __p_;
5356 public:
5357 typedef negative_binomial_distribution distribution_type;
5358
Howard Hinnantf5f99992010-09-22 18:02:38 +00005359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005360 explicit param_type(result_type __k = 1, double __p = 0.5)
5361 : __k_(__k), __p_(__p) {}
5362
Howard Hinnantf5f99992010-09-22 18:02:38 +00005363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005364 result_type k() const {return __k_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005366 double p() const {return __p_;}
5367
Howard Hinnantf5f99992010-09-22 18:02:38 +00005368 friend _LIBCPP_INLINE_VISIBILITY
5369 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005370 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005371 friend _LIBCPP_INLINE_VISIBILITY
5372 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005373 {return !(__x == __y);}
5374 };
5375
5376private:
5377 param_type __p_;
5378
5379public:
5380 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005381#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005382 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005383 negative_binomial_distribution() : negative_binomial_distribution(1) {}
5384 _LIBCPP_INLINE_VISIBILITY
5385 explicit negative_binomial_distribution(result_type __k, double __p = 0.5)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005386 : __p_(__k, __p) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005387#else
5388 _LIBCPP_INLINE_VISIBILITY
5389 explicit negative_binomial_distribution(result_type __k = 1,
5390 double __p = 0.5)
5391 : __p_(__k, __p) {}
5392#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005394 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005396 void reset() {}
5397
5398 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005399 template<class _URNG>
5400 _LIBCPP_INLINE_VISIBILITY
5401 result_type operator()(_URNG& __g)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005402 {return (*this)(__g, __p_);}
5403 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5404
5405 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005407 result_type k() const {return __p_.k();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005409 double p() const {return __p_.p();}
5410
Howard Hinnantf5f99992010-09-22 18:02:38 +00005411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005412 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005414 void param(const param_type& __p) {__p_ = __p;}
5415
Howard Hinnantf5f99992010-09-22 18:02:38 +00005416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005417 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005419 result_type max() const {return numeric_limits<result_type>::max();}
5420
Howard Hinnantf5f99992010-09-22 18:02:38 +00005421 friend _LIBCPP_INLINE_VISIBILITY
5422 bool operator==(const negative_binomial_distribution& __x,
5423 const negative_binomial_distribution& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005424 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005425 friend _LIBCPP_INLINE_VISIBILITY
5426 bool operator!=(const negative_binomial_distribution& __x,
5427 const negative_binomial_distribution& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005428 {return !(__x == __y);}
5429};
5430
5431template <class _IntType>
5432template<class _URNG>
5433_IntType
5434negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5435{
5436 result_type __k = __pr.k();
5437 double __p = __pr.p();
5438 if (__k <= 21 * __p)
5439 {
5440 bernoulli_distribution __gen(__p);
5441 result_type __f = 0;
5442 result_type __s = 0;
5443 while (__s < __k)
5444 {
5445 if (__gen(__urng))
5446 ++__s;
5447 else
5448 ++__f;
5449 }
5450 return __f;
5451 }
5452 return poisson_distribution<result_type>(gamma_distribution<double>
5453 (__k, (1-__p)/__p)(__urng))(__urng);
5454}
5455
5456template <class _CharT, class _Traits, class _IntType>
5457basic_ostream<_CharT, _Traits>&
5458operator<<(basic_ostream<_CharT, _Traits>& __os,
5459 const negative_binomial_distribution<_IntType>& __x)
5460{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005461 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005462 typedef basic_ostream<_CharT, _Traits> _OStream;
5463 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5464 _OStream::scientific);
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005465 _CharT __sp = __os.widen(' ');
5466 __os.fill(__sp);
5467 return __os << __x.k() << __sp << __x.p();
5468}
5469
5470template <class _CharT, class _Traits, class _IntType>
5471basic_istream<_CharT, _Traits>&
5472operator>>(basic_istream<_CharT, _Traits>& __is,
5473 negative_binomial_distribution<_IntType>& __x)
5474{
5475 typedef negative_binomial_distribution<_IntType> _Eng;
5476 typedef typename _Eng::result_type result_type;
5477 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005478 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005479 typedef basic_istream<_CharT, _Traits> _Istream;
5480 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005481 result_type __k;
5482 double __p;
5483 __is >> __k >> __p;
5484 if (!__is.fail())
5485 __x.param(param_type(__k, __p));
5486 return __is;
5487}
5488
Howard Hinnant28beb162010-05-17 13:44:27 +00005489// geometric_distribution
5490
5491template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005492class _LIBCPP_TEMPLATE_VIS geometric_distribution
Howard Hinnant28beb162010-05-17 13:44:27 +00005493{
5494public:
5495 // types
5496 typedef _IntType result_type;
5497
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005498 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant28beb162010-05-17 13:44:27 +00005499 {
5500 double __p_;
5501 public:
5502 typedef geometric_distribution distribution_type;
5503
Howard Hinnantf5f99992010-09-22 18:02:38 +00005504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005505 explicit param_type(double __p = 0.5) : __p_(__p) {}
5506
Howard Hinnantf5f99992010-09-22 18:02:38 +00005507 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005508 double p() const {return __p_;}
5509
Howard Hinnantf5f99992010-09-22 18:02:38 +00005510 friend _LIBCPP_INLINE_VISIBILITY
5511 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005512 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005513 friend _LIBCPP_INLINE_VISIBILITY
5514 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005515 {return !(__x == __y);}
5516 };
5517
5518private:
5519 param_type __p_;
5520
5521public:
5522 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005523#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005524 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005525 geometric_distribution() : geometric_distribution(0.5) {}
5526 _LIBCPP_INLINE_VISIBILITY
5527 explicit geometric_distribution(double __p)
5528 : __p_(__p) {}
5529#else
5530 _LIBCPP_INLINE_VISIBILITY
5531 explicit geometric_distribution(double __p = 0.5)
5532 : __p_(__p) {}
5533#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005535 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005537 void reset() {}
5538
5539 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005540 template<class _URNG>
5541 _LIBCPP_INLINE_VISIBILITY
5542 result_type operator()(_URNG& __g)
Howard Hinnant28beb162010-05-17 13:44:27 +00005543 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005544 template<class _URNG>
5545 _LIBCPP_INLINE_VISIBILITY
5546 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant28beb162010-05-17 13:44:27 +00005547 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5548
5549 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005551 double p() const {return __p_.p();}
5552
Howard Hinnantf5f99992010-09-22 18:02:38 +00005553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005554 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005556 void param(const param_type& __p) {__p_ = __p;}
5557
Howard Hinnantf5f99992010-09-22 18:02:38 +00005558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005559 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005561 result_type max() const {return numeric_limits<result_type>::max();}
5562
Howard Hinnantf5f99992010-09-22 18:02:38 +00005563 friend _LIBCPP_INLINE_VISIBILITY
5564 bool operator==(const geometric_distribution& __x,
5565 const geometric_distribution& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005566 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005567 friend _LIBCPP_INLINE_VISIBILITY
5568 bool operator!=(const geometric_distribution& __x,
5569 const geometric_distribution& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005570 {return !(__x == __y);}
5571};
5572
5573template <class _CharT, class _Traits, class _IntType>
5574basic_ostream<_CharT, _Traits>&
5575operator<<(basic_ostream<_CharT, _Traits>& __os,
5576 const geometric_distribution<_IntType>& __x)
5577{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005578 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005579 typedef basic_ostream<_CharT, _Traits> _OStream;
5580 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5581 _OStream::scientific);
Howard Hinnant28beb162010-05-17 13:44:27 +00005582 return __os << __x.p();
5583}
5584
5585template <class _CharT, class _Traits, class _IntType>
5586basic_istream<_CharT, _Traits>&
5587operator>>(basic_istream<_CharT, _Traits>& __is,
5588 geometric_distribution<_IntType>& __x)
5589{
5590 typedef geometric_distribution<_IntType> _Eng;
5591 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005592 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005593 typedef basic_istream<_CharT, _Traits> _Istream;
5594 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant28beb162010-05-17 13:44:27 +00005595 double __p;
5596 __is >> __p;
5597 if (!__is.fail())
5598 __x.param(param_type(__p));
5599 return __is;
5600}
5601
Howard Hinnant252ebf02010-05-15 23:36:00 +00005602// chi_squared_distribution
5603
5604template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005605class _LIBCPP_TEMPLATE_VIS chi_squared_distribution
Howard Hinnant252ebf02010-05-15 23:36:00 +00005606{
5607public:
5608 // types
5609 typedef _RealType result_type;
5610
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005611 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant252ebf02010-05-15 23:36:00 +00005612 {
5613 result_type __n_;
5614 public:
5615 typedef chi_squared_distribution distribution_type;
5616
Howard Hinnantf5f99992010-09-22 18:02:38 +00005617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005618 explicit param_type(result_type __n = 1) : __n_(__n) {}
5619
Howard Hinnantf5f99992010-09-22 18:02:38 +00005620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005621 result_type n() const {return __n_;}
5622
Howard Hinnantf5f99992010-09-22 18:02:38 +00005623 friend _LIBCPP_INLINE_VISIBILITY
5624 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005625 {return __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005626 friend _LIBCPP_INLINE_VISIBILITY
5627 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005628 {return !(__x == __y);}
5629 };
5630
5631private:
5632 param_type __p_;
5633
5634public:
5635 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005636#ifndef _LIBCPP_CXX03_LANG
5637 _LIBCPP_INLINE_VISIBILITY
5638 chi_squared_distribution() : chi_squared_distribution(1) {}
5639 _LIBCPP_INLINE_VISIBILITY
5640 explicit chi_squared_distribution(result_type __n)
5641 : __p_(param_type(__n)) {}
5642#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00005643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005644 explicit chi_squared_distribution(result_type __n = 1)
5645 : __p_(param_type(__n)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005646#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005648 explicit chi_squared_distribution(const param_type& __p)
5649 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005651 void reset() {}
5652
5653 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005654 template<class _URNG>
5655 _LIBCPP_INLINE_VISIBILITY
5656 result_type operator()(_URNG& __g)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005657 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005658 template<class _URNG>
5659 _LIBCPP_INLINE_VISIBILITY
5660 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005661 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5662
5663 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005665 result_type n() const {return __p_.n();}
5666
Howard Hinnantf5f99992010-09-22 18:02:38 +00005667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005668 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005670 void param(const param_type& __p) {__p_ = __p;}
5671
Howard Hinnantf5f99992010-09-22 18:02:38 +00005672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005673 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005675 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant252ebf02010-05-15 23:36:00 +00005676
Howard Hinnantf5f99992010-09-22 18:02:38 +00005677 friend _LIBCPP_INLINE_VISIBILITY
5678 bool operator==(const chi_squared_distribution& __x,
5679 const chi_squared_distribution& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005680 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005681 friend _LIBCPP_INLINE_VISIBILITY
5682 bool operator!=(const chi_squared_distribution& __x,
5683 const chi_squared_distribution& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005684 {return !(__x == __y);}
5685};
5686
5687template <class _CharT, class _Traits, class _RT>
5688basic_ostream<_CharT, _Traits>&
5689operator<<(basic_ostream<_CharT, _Traits>& __os,
5690 const chi_squared_distribution<_RT>& __x)
5691{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005692 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005693 typedef basic_ostream<_CharT, _Traits> _OStream;
5694 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5695 _OStream::scientific);
Howard Hinnant252ebf02010-05-15 23:36:00 +00005696 __os << __x.n();
5697 return __os;
5698}
5699
5700template <class _CharT, class _Traits, class _RT>
5701basic_istream<_CharT, _Traits>&
5702operator>>(basic_istream<_CharT, _Traits>& __is,
5703 chi_squared_distribution<_RT>& __x)
5704{
5705 typedef chi_squared_distribution<_RT> _Eng;
5706 typedef typename _Eng::result_type result_type;
5707 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005708 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005709 typedef basic_istream<_CharT, _Traits> _Istream;
5710 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant252ebf02010-05-15 23:36:00 +00005711 result_type __n;
5712 __is >> __n;
5713 if (!__is.fail())
5714 __x.param(param_type(__n));
5715 return __is;
5716}
5717
Howard Hinnantf3292562010-05-17 21:55:46 +00005718// cauchy_distribution
5719
5720template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005721class _LIBCPP_TEMPLATE_VIS cauchy_distribution
Howard Hinnantf3292562010-05-17 21:55:46 +00005722{
5723public:
5724 // types
5725 typedef _RealType result_type;
5726
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005727 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantf3292562010-05-17 21:55:46 +00005728 {
5729 result_type __a_;
5730 result_type __b_;
5731 public:
5732 typedef cauchy_distribution distribution_type;
5733
Howard Hinnantf5f99992010-09-22 18:02:38 +00005734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005735 explicit param_type(result_type __a = 0, result_type __b = 1)
5736 : __a_(__a), __b_(__b) {}
5737
Howard Hinnantf5f99992010-09-22 18:02:38 +00005738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005739 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005741 result_type b() const {return __b_;}
5742
Howard Hinnantf5f99992010-09-22 18:02:38 +00005743 friend _LIBCPP_INLINE_VISIBILITY
5744 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005745 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005746 friend _LIBCPP_INLINE_VISIBILITY
5747 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005748 {return !(__x == __y);}
5749 };
5750
5751private:
5752 param_type __p_;
5753
5754public:
5755 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005756#ifndef _LIBCPP_CXX03_LANG
5757 _LIBCPP_INLINE_VISIBILITY
5758 cauchy_distribution() : cauchy_distribution(0) {}
5759 _LIBCPP_INLINE_VISIBILITY
5760 explicit cauchy_distribution(result_type __a, result_type __b = 1)
5761 : __p_(param_type(__a, __b)) {}
5762#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00005763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005764 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5765 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005766#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005768 explicit cauchy_distribution(const param_type& __p)
5769 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005771 void reset() {}
5772
5773 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005774 template<class _URNG>
5775 _LIBCPP_INLINE_VISIBILITY
5776 result_type operator()(_URNG& __g)
Howard Hinnantf3292562010-05-17 21:55:46 +00005777 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005778 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantf3292562010-05-17 21:55:46 +00005779
5780 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005782 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005784 result_type b() const {return __p_.b();}
5785
Howard Hinnantf5f99992010-09-22 18:02:38 +00005786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005787 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005789 void param(const param_type& __p) {__p_ = __p;}
5790
Howard Hinnantf5f99992010-09-22 18:02:38 +00005791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005792 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005794 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantf3292562010-05-17 21:55:46 +00005795
Howard Hinnantf5f99992010-09-22 18:02:38 +00005796 friend _LIBCPP_INLINE_VISIBILITY
5797 bool operator==(const cauchy_distribution& __x,
5798 const cauchy_distribution& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005799 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005800 friend _LIBCPP_INLINE_VISIBILITY
5801 bool operator!=(const cauchy_distribution& __x,
5802 const cauchy_distribution& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005803 {return !(__x == __y);}
Howard Hinnantf3292562010-05-17 21:55:46 +00005804};
5805
5806template <class _RealType>
5807template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005808inline
Howard Hinnantf3292562010-05-17 21:55:46 +00005809_RealType
5810cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5811{
5812 uniform_real_distribution<result_type> __gen;
5813 // 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 +00005814 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
Howard Hinnantf3292562010-05-17 21:55:46 +00005815}
5816
5817template <class _CharT, class _Traits, class _RT>
5818basic_ostream<_CharT, _Traits>&
5819operator<<(basic_ostream<_CharT, _Traits>& __os,
5820 const cauchy_distribution<_RT>& __x)
5821{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005822 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005823 typedef basic_ostream<_CharT, _Traits> _OStream;
5824 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5825 _OStream::scientific);
Howard Hinnantf3292562010-05-17 21:55:46 +00005826 _CharT __sp = __os.widen(' ');
5827 __os.fill(__sp);
5828 __os << __x.a() << __sp << __x.b();
5829 return __os;
5830}
5831
5832template <class _CharT, class _Traits, class _RT>
5833basic_istream<_CharT, _Traits>&
5834operator>>(basic_istream<_CharT, _Traits>& __is,
5835 cauchy_distribution<_RT>& __x)
5836{
5837 typedef cauchy_distribution<_RT> _Eng;
5838 typedef typename _Eng::result_type result_type;
5839 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005840 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005841 typedef basic_istream<_CharT, _Traits> _Istream;
5842 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantf3292562010-05-17 21:55:46 +00005843 result_type __a;
5844 result_type __b;
5845 __is >> __a >> __b;
5846 if (!__is.fail())
5847 __x.param(param_type(__a, __b));
5848 return __is;
5849}
5850
Howard Hinnantf39463b2010-05-18 17:32:30 +00005851// fisher_f_distribution
5852
5853template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005854class _LIBCPP_TEMPLATE_VIS fisher_f_distribution
Howard Hinnantf39463b2010-05-18 17:32:30 +00005855{
5856public:
5857 // types
5858 typedef _RealType result_type;
5859
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005860 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantf39463b2010-05-18 17:32:30 +00005861 {
5862 result_type __m_;
5863 result_type __n_;
5864 public:
5865 typedef fisher_f_distribution distribution_type;
5866
Howard Hinnantf5f99992010-09-22 18:02:38 +00005867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005868 explicit param_type(result_type __m = 1, result_type __n = 1)
5869 : __m_(__m), __n_(__n) {}
5870
Howard Hinnantf5f99992010-09-22 18:02:38 +00005871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005872 result_type m() const {return __m_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005874 result_type n() const {return __n_;}
5875
Howard Hinnantf5f99992010-09-22 18:02:38 +00005876 friend _LIBCPP_INLINE_VISIBILITY
5877 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005878 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005879 friend _LIBCPP_INLINE_VISIBILITY
5880 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005881 {return !(__x == __y);}
5882 };
5883
5884private:
5885 param_type __p_;
5886
5887public:
5888 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005889#ifndef _LIBCPP_CXX03_LANG
5890 _LIBCPP_INLINE_VISIBILITY
5891 fisher_f_distribution() : fisher_f_distribution(1) {}
5892 _LIBCPP_INLINE_VISIBILITY
5893 explicit fisher_f_distribution(result_type __m, result_type __n = 1)
5894 : __p_(param_type(__m, __n)) {}
5895#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00005896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005897 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5898 : __p_(param_type(__m, __n)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005899#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005901 explicit fisher_f_distribution(const param_type& __p)
5902 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005904 void reset() {}
5905
5906 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005907 template<class _URNG>
5908 _LIBCPP_INLINE_VISIBILITY
5909 result_type operator()(_URNG& __g)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005910 {return (*this)(__g, __p_);}
5911 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5912
5913 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005915 result_type m() const {return __p_.m();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005917 result_type n() const {return __p_.n();}
5918
Howard Hinnantf5f99992010-09-22 18:02:38 +00005919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005920 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005922 void param(const param_type& __p) {__p_ = __p;}
5923
Howard Hinnantf5f99992010-09-22 18:02:38 +00005924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005925 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005927 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantf39463b2010-05-18 17:32:30 +00005928
Howard Hinnantf5f99992010-09-22 18:02:38 +00005929 friend _LIBCPP_INLINE_VISIBILITY
5930 bool operator==(const fisher_f_distribution& __x,
5931 const fisher_f_distribution& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005932 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005933 friend _LIBCPP_INLINE_VISIBILITY
5934 bool operator!=(const fisher_f_distribution& __x,
5935 const fisher_f_distribution& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005936 {return !(__x == __y);}
5937};
5938
5939template <class _RealType>
5940template<class _URNG>
5941_RealType
5942fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5943{
5944 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5945 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5946 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5947}
5948
5949template <class _CharT, class _Traits, class _RT>
5950basic_ostream<_CharT, _Traits>&
5951operator<<(basic_ostream<_CharT, _Traits>& __os,
5952 const fisher_f_distribution<_RT>& __x)
5953{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005954 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005955 typedef basic_ostream<_CharT, _Traits> _OStream;
5956 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5957 _OStream::scientific);
Howard Hinnantf39463b2010-05-18 17:32:30 +00005958 _CharT __sp = __os.widen(' ');
5959 __os.fill(__sp);
5960 __os << __x.m() << __sp << __x.n();
5961 return __os;
5962}
5963
5964template <class _CharT, class _Traits, class _RT>
5965basic_istream<_CharT, _Traits>&
5966operator>>(basic_istream<_CharT, _Traits>& __is,
5967 fisher_f_distribution<_RT>& __x)
5968{
5969 typedef fisher_f_distribution<_RT> _Eng;
5970 typedef typename _Eng::result_type result_type;
5971 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005972 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005973 typedef basic_istream<_CharT, _Traits> _Istream;
5974 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantf39463b2010-05-18 17:32:30 +00005975 result_type __m;
5976 result_type __n;
5977 __is >> __m >> __n;
5978 if (!__is.fail())
5979 __x.param(param_type(__m, __n));
5980 return __is;
5981}
5982
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00005983// student_t_distribution
5984
Howard Hinnant20c50882010-05-18 20:08:04 +00005985template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005986class _LIBCPP_TEMPLATE_VIS student_t_distribution
Howard Hinnant20c50882010-05-18 20:08:04 +00005987{
5988public:
5989 // types
5990 typedef _RealType result_type;
5991
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005992 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant20c50882010-05-18 20:08:04 +00005993 {
5994 result_type __n_;
5995 public:
5996 typedef student_t_distribution distribution_type;
5997
Howard Hinnantf5f99992010-09-22 18:02:38 +00005998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00005999 explicit param_type(result_type __n = 1) : __n_(__n) {}
6000
Howard Hinnantf5f99992010-09-22 18:02:38 +00006001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006002 result_type n() const {return __n_;}
6003
Howard Hinnantf5f99992010-09-22 18:02:38 +00006004 friend _LIBCPP_INLINE_VISIBILITY
6005 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006006 {return __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006007 friend _LIBCPP_INLINE_VISIBILITY
6008 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006009 {return !(__x == __y);}
6010 };
6011
6012private:
6013 param_type __p_;
6014 normal_distribution<result_type> __nd_;
6015
6016public:
6017 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01006018#ifndef _LIBCPP_CXX03_LANG
6019 _LIBCPP_INLINE_VISIBILITY
6020 student_t_distribution() : student_t_distribution(1) {}
6021 _LIBCPP_INLINE_VISIBILITY
6022 explicit student_t_distribution(result_type __n)
6023 : __p_(param_type(__n)) {}
6024#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00006025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006026 explicit student_t_distribution(result_type __n = 1)
6027 : __p_(param_type(__n)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01006028#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00006029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006030 explicit student_t_distribution(const param_type& __p)
6031 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006032 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006033 void reset() {__nd_.reset();}
6034
6035 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006036 template<class _URNG>
6037 _LIBCPP_INLINE_VISIBILITY
6038 result_type operator()(_URNG& __g)
Howard Hinnant20c50882010-05-18 20:08:04 +00006039 {return (*this)(__g, __p_);}
6040 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6041
6042 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006044 result_type n() const {return __p_.n();}
6045
Howard Hinnantf5f99992010-09-22 18:02:38 +00006046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006047 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006049 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant20c50882010-05-18 20:08:04 +00006050
Howard Hinnantf5f99992010-09-22 18:02:38 +00006051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006052 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006054 result_type max() const {return numeric_limits<result_type>::infinity();}
6055
Howard Hinnantf5f99992010-09-22 18:02:38 +00006056 friend _LIBCPP_INLINE_VISIBILITY
6057 bool operator==(const student_t_distribution& __x,
6058 const student_t_distribution& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006059 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006060 friend _LIBCPP_INLINE_VISIBILITY
6061 bool operator!=(const student_t_distribution& __x,
6062 const student_t_distribution& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006063 {return !(__x == __y);}
6064};
6065
6066template <class _RealType>
6067template<class _URNG>
6068_RealType
6069student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6070{
6071 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006072 return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
Howard Hinnant20c50882010-05-18 20:08:04 +00006073}
6074
6075template <class _CharT, class _Traits, class _RT>
6076basic_ostream<_CharT, _Traits>&
6077operator<<(basic_ostream<_CharT, _Traits>& __os,
6078 const student_t_distribution<_RT>& __x)
6079{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006080 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006081 typedef basic_ostream<_CharT, _Traits> _OStream;
6082 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6083 _OStream::scientific);
Howard Hinnant20c50882010-05-18 20:08:04 +00006084 __os << __x.n();
6085 return __os;
6086}
6087
6088template <class _CharT, class _Traits, class _RT>
6089basic_istream<_CharT, _Traits>&
6090operator>>(basic_istream<_CharT, _Traits>& __is,
6091 student_t_distribution<_RT>& __x)
6092{
6093 typedef student_t_distribution<_RT> _Eng;
6094 typedef typename _Eng::result_type result_type;
6095 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00006096 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006097 typedef basic_istream<_CharT, _Traits> _Istream;
6098 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant20c50882010-05-18 20:08:04 +00006099 result_type __n;
6100 __is >> __n;
6101 if (!__is.fail())
6102 __x.param(param_type(__n));
6103 return __is;
6104}
6105
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006106// discrete_distribution
6107
6108template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006109class _LIBCPP_TEMPLATE_VIS discrete_distribution
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006110{
6111public:
6112 // types
6113 typedef _IntType result_type;
6114
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006115 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006116 {
6117 vector<double> __p_;
6118 public:
6119 typedef discrete_distribution distribution_type;
6120
Howard Hinnantf5f99992010-09-22 18:02:38 +00006121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006122 param_type() {}
6123 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006125 param_type(_InputIterator __f, _InputIterator __l)
6126 : __p_(__f, __l) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006127#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00006128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006129 param_type(initializer_list<double> __wl)
6130 : __p_(__wl.begin(), __wl.end()) {__init();}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04006131#endif // _LIBCPP_CXX03_LANG
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006132 template<class _UnaryOperation>
6133 param_type(size_t __nw, double __xmin, double __xmax,
6134 _UnaryOperation __fw);
6135
6136 vector<double> probabilities() const;
6137
Howard Hinnantf5f99992010-09-22 18:02:38 +00006138 friend _LIBCPP_INLINE_VISIBILITY
6139 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006140 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006141 friend _LIBCPP_INLINE_VISIBILITY
6142 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006143 {return !(__x == __y);}
6144
6145 private:
6146 void __init();
6147
6148 friend class discrete_distribution;
6149
6150 template <class _CharT, class _Traits, class _IT>
6151 friend
6152 basic_ostream<_CharT, _Traits>&
6153 operator<<(basic_ostream<_CharT, _Traits>& __os,
6154 const discrete_distribution<_IT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006155
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006156 template <class _CharT, class _Traits, class _IT>
6157 friend
6158 basic_istream<_CharT, _Traits>&
6159 operator>>(basic_istream<_CharT, _Traits>& __is,
6160 discrete_distribution<_IT>& __x);
6161 };
6162
6163private:
6164 param_type __p_;
6165
6166public:
6167 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006169 discrete_distribution() {}
6170 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006172 discrete_distribution(_InputIterator __f, _InputIterator __l)
6173 : __p_(__f, __l) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006174#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00006175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006176 discrete_distribution(initializer_list<double> __wl)
6177 : __p_(__wl) {}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04006178#endif // _LIBCPP_CXX03_LANG
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006179 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006181 discrete_distribution(size_t __nw, double __xmin, double __xmax,
6182 _UnaryOperation __fw)
6183 : __p_(__nw, __xmin, __xmax, __fw) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00006185 explicit discrete_distribution(const param_type& __p)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006186 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006188 void reset() {}
6189
6190 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006191 template<class _URNG>
6192 _LIBCPP_INLINE_VISIBILITY
6193 result_type operator()(_URNG& __g)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006194 {return (*this)(__g, __p_);}
6195 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6196
6197 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006199 vector<double> probabilities() const {return __p_.probabilities();}
6200
Howard Hinnantf5f99992010-09-22 18:02:38 +00006201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006202 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006204 void param(const param_type& __p) {__p_ = __p;}
6205
Howard Hinnantf5f99992010-09-22 18:02:38 +00006206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006207 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006209 result_type max() const {return __p_.__p_.size();}
6210
Howard Hinnantf5f99992010-09-22 18:02:38 +00006211 friend _LIBCPP_INLINE_VISIBILITY
6212 bool operator==(const discrete_distribution& __x,
6213 const discrete_distribution& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006214 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006215 friend _LIBCPP_INLINE_VISIBILITY
6216 bool operator!=(const discrete_distribution& __x,
6217 const discrete_distribution& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006218 {return !(__x == __y);}
6219
6220 template <class _CharT, class _Traits, class _IT>
6221 friend
6222 basic_ostream<_CharT, _Traits>&
6223 operator<<(basic_ostream<_CharT, _Traits>& __os,
6224 const discrete_distribution<_IT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006225
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006226 template <class _CharT, class _Traits, class _IT>
6227 friend
6228 basic_istream<_CharT, _Traits>&
6229 operator>>(basic_istream<_CharT, _Traits>& __is,
6230 discrete_distribution<_IT>& __x);
6231};
6232
6233template<class _IntType>
6234template<class _UnaryOperation>
6235discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
6236 double __xmin,
6237 double __xmax,
6238 _UnaryOperation __fw)
6239{
6240 if (__nw > 1)
6241 {
6242 __p_.reserve(__nw - 1);
6243 double __d = (__xmax - __xmin) / __nw;
6244 double __d2 = __d / 2;
6245 for (size_t __k = 0; __k < __nw; ++__k)
6246 __p_.push_back(__fw(__xmin + __k * __d + __d2));
6247 __init();
6248 }
6249}
6250
6251template<class _IntType>
6252void
6253discrete_distribution<_IntType>::param_type::__init()
6254{
6255 if (!__p_.empty())
6256 {
6257 if (__p_.size() > 1)
6258 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006259 double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -04006260 for (vector<double>::iterator __i = __p_.begin(), __e = __p_.end(); __i < __e; ++__i)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006261 *__i /= __s;
6262 vector<double> __t(__p_.size() - 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006263 _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006264 swap(__p_, __t);
6265 }
6266 else
6267 {
6268 __p_.clear();
6269 __p_.shrink_to_fit();
6270 }
6271 }
6272}
6273
6274template<class _IntType>
6275vector<double>
6276discrete_distribution<_IntType>::param_type::probabilities() const
6277{
6278 size_t __n = __p_.size();
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -04006279 vector<double> __p(__n+1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006280 _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006281 if (__n > 0)
6282 __p[__n] = 1 - __p_[__n-1];
6283 else
6284 __p[0] = 1;
6285 return __p;
6286}
6287
6288template<class _IntType>
6289template<class _URNG>
6290_IntType
6291discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
6292{
6293 uniform_real_distribution<double> __gen;
6294 return static_cast<_IntType>(
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006295 _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006296 __p.__p_.begin());
6297}
6298
6299template <class _CharT, class _Traits, class _IT>
6300basic_ostream<_CharT, _Traits>&
6301operator<<(basic_ostream<_CharT, _Traits>& __os,
6302 const discrete_distribution<_IT>& __x)
6303{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006304 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006305 typedef basic_ostream<_CharT, _Traits> _OStream;
6306 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6307 _OStream::scientific);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006308 _CharT __sp = __os.widen(' ');
6309 __os.fill(__sp);
6310 size_t __n = __x.__p_.__p_.size();
6311 __os << __n;
6312 for (size_t __i = 0; __i < __n; ++__i)
6313 __os << __sp << __x.__p_.__p_[__i];
6314 return __os;
6315}
6316
6317template <class _CharT, class _Traits, class _IT>
6318basic_istream<_CharT, _Traits>&
6319operator>>(basic_istream<_CharT, _Traits>& __is,
6320 discrete_distribution<_IT>& __x)
6321{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006322 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006323 typedef basic_istream<_CharT, _Traits> _Istream;
6324 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006325 size_t __n;
6326 __is >> __n;
Howard Hinnant481ba382010-05-20 15:11:46 +00006327 vector<double> __p(__n);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006328 for (size_t __i = 0; __i < __n; ++__i)
6329 __is >> __p[__i];
6330 if (!__is.fail())
6331 swap(__x.__p_.__p_, __p);
6332 return __is;
6333}
6334
Howard Hinnant481ba382010-05-20 15:11:46 +00006335// piecewise_constant_distribution
6336
6337template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006338class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution
Howard Hinnant481ba382010-05-20 15:11:46 +00006339{
6340public:
6341 // types
6342 typedef _RealType result_type;
6343
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006344 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant481ba382010-05-20 15:11:46 +00006345 {
Howard Hinnant481ba382010-05-20 15:11:46 +00006346 vector<result_type> __b_;
Howard Hinnant167159f2010-11-18 17:01:36 +00006347 vector<result_type> __densities_;
6348 vector<result_type> __areas_;
Howard Hinnant481ba382010-05-20 15:11:46 +00006349 public:
6350 typedef piecewise_constant_distribution distribution_type;
6351
6352 param_type();
6353 template<class _InputIteratorB, class _InputIteratorW>
6354 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6355 _InputIteratorW __fW);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006356#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006357 template<class _UnaryOperation>
6358 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04006359#endif // _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006360 template<class _UnaryOperation>
6361 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6362 _UnaryOperation __fw);
Eric Fiselierb9f37ca2019-12-12 20:48:11 -05006363 param_type(param_type const&) = default;
Howard Hinnant71c410b2010-10-13 14:37:09 +00006364 param_type & operator=(const param_type& __rhs);
Howard Hinnant481ba382010-05-20 15:11:46 +00006365
Howard Hinnantf5f99992010-09-22 18:02:38 +00006366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006367 vector<result_type> intervals() const {return __b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006369 vector<result_type> densities() const {return __densities_;}
Howard Hinnant481ba382010-05-20 15:11:46 +00006370
Howard Hinnantf5f99992010-09-22 18:02:38 +00006371 friend _LIBCPP_INLINE_VISIBILITY
6372 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006373 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006374 friend _LIBCPP_INLINE_VISIBILITY
6375 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant481ba382010-05-20 15:11:46 +00006376 {return !(__x == __y);}
6377
6378 private:
6379 void __init();
6380
6381 friend class piecewise_constant_distribution;
6382
6383 template <class _CharT, class _Traits, class _RT>
6384 friend
6385 basic_ostream<_CharT, _Traits>&
6386 operator<<(basic_ostream<_CharT, _Traits>& __os,
6387 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006388
Howard Hinnant481ba382010-05-20 15:11:46 +00006389 template <class _CharT, class _Traits, class _RT>
6390 friend
6391 basic_istream<_CharT, _Traits>&
6392 operator>>(basic_istream<_CharT, _Traits>& __is,
6393 piecewise_constant_distribution<_RT>& __x);
6394 };
6395
6396private:
6397 param_type __p_;
6398
6399public:
6400 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006402 piecewise_constant_distribution() {}
6403 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006405 piecewise_constant_distribution(_InputIteratorB __fB,
6406 _InputIteratorB __lB,
6407 _InputIteratorW __fW)
6408 : __p_(__fB, __lB, __fW) {}
6409
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006410#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006411 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006413 piecewise_constant_distribution(initializer_list<result_type> __bl,
6414 _UnaryOperation __fw)
6415 : __p_(__bl, __fw) {}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04006416#endif // _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006417
6418 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006420 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6421 result_type __xmax, _UnaryOperation __fw)
6422 : __p_(__nw, __xmin, __xmax, __fw) {}
6423
Howard Hinnantf5f99992010-09-22 18:02:38 +00006424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006425 explicit piecewise_constant_distribution(const param_type& __p)
6426 : __p_(__p) {}
6427
Howard Hinnantf5f99992010-09-22 18:02:38 +00006428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006429 void reset() {}
6430
6431 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006432 template<class _URNG>
6433 _LIBCPP_INLINE_VISIBILITY
6434 result_type operator()(_URNG& __g)
Howard Hinnant481ba382010-05-20 15:11:46 +00006435 {return (*this)(__g, __p_);}
6436 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6437
6438 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006440 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006442 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant481ba382010-05-20 15:11:46 +00006443
Howard Hinnantf5f99992010-09-22 18:02:38 +00006444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006445 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006447 void param(const param_type& __p) {__p_ = __p;}
6448
Howard Hinnantf5f99992010-09-22 18:02:38 +00006449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006450 result_type min() const {return __p_.__b_.front();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006452 result_type max() const {return __p_.__b_.back();}
6453
Howard Hinnantf5f99992010-09-22 18:02:38 +00006454 friend _LIBCPP_INLINE_VISIBILITY
6455 bool operator==(const piecewise_constant_distribution& __x,
6456 const piecewise_constant_distribution& __y)
Howard Hinnant481ba382010-05-20 15:11:46 +00006457 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006458 friend _LIBCPP_INLINE_VISIBILITY
6459 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnant481ba382010-05-20 15:11:46 +00006460 const piecewise_constant_distribution& __y)
6461 {return !(__x == __y);}
6462
6463 template <class _CharT, class _Traits, class _RT>
6464 friend
6465 basic_ostream<_CharT, _Traits>&
6466 operator<<(basic_ostream<_CharT, _Traits>& __os,
6467 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006468
Howard Hinnant481ba382010-05-20 15:11:46 +00006469 template <class _CharT, class _Traits, class _RT>
6470 friend
6471 basic_istream<_CharT, _Traits>&
6472 operator>>(basic_istream<_CharT, _Traits>& __is,
6473 piecewise_constant_distribution<_RT>& __x);
6474};
6475
6476template<class _RealType>
Howard Hinnant71c410b2010-10-13 14:37:09 +00006477typename piecewise_constant_distribution<_RealType>::param_type &
6478piecewise_constant_distribution<_RealType>::param_type::operator=
6479 (const param_type& __rhs)
6480{
6481// These can throw
6482 __b_.reserve (__rhs.__b_.size ());
6483 __densities_.reserve(__rhs.__densities_.size());
6484 __areas_.reserve (__rhs.__areas_.size());
6485
6486// These can not throw
6487 __b_ = __rhs.__b_;
6488 __densities_ = __rhs.__densities_;
6489 __areas_ = __rhs.__areas_;
6490 return *this;
6491}
6492
6493template<class _RealType>
Howard Hinnant481ba382010-05-20 15:11:46 +00006494void
6495piecewise_constant_distribution<_RealType>::param_type::__init()
6496{
Howard Hinnant35d83df2010-05-24 00:35:40 +00006497 // __densities_ contains non-normalized areas
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006498 result_type __total_area = _VSTD::accumulate(__densities_.begin(),
Howard Hinnant35d83df2010-05-24 00:35:40 +00006499 __densities_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006500 result_type());
Howard Hinnant35d83df2010-05-24 00:35:40 +00006501 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6502 __densities_[__i] /= __total_area;
6503 // __densities_ contains normalized areas
Howard Hinnant167159f2010-11-18 17:01:36 +00006504 __areas_.assign(__densities_.size(), result_type());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006505 _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
Howard Hinnant35d83df2010-05-24 00:35:40 +00006506 __areas_.begin() + 1);
6507 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6508 __densities_.back() = 1 - __areas_.back(); // correct round off error
6509 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6510 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6511 // __densities_ now contains __densities_
Howard Hinnant481ba382010-05-20 15:11:46 +00006512}
6513
6514template<class _RealType>
6515piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant35d83df2010-05-24 00:35:40 +00006516 : __b_(2),
Howard Hinnanta001ef32010-05-25 00:27:34 +00006517 __densities_(1, 1.0),
6518 __areas_(1, 0.0)
Howard Hinnant481ba382010-05-20 15:11:46 +00006519{
6520 __b_[1] = 1;
6521}
6522
6523template<class _RealType>
6524template<class _InputIteratorB, class _InputIteratorW>
6525piecewise_constant_distribution<_RealType>::param_type::param_type(
6526 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6527 : __b_(__fB, __lB)
6528{
6529 if (__b_.size() < 2)
6530 {
6531 __b_.resize(2);
6532 __b_[0] = 0;
6533 __b_[1] = 1;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006534 __densities_.assign(1, 1.0);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006535 __areas_.assign(1, 0.0);
Howard Hinnant481ba382010-05-20 15:11:46 +00006536 }
6537 else
6538 {
Howard Hinnant35d83df2010-05-24 00:35:40 +00006539 __densities_.reserve(__b_.size() - 1);
Howard Hinnant481ba382010-05-20 15:11:46 +00006540 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006541 __densities_.push_back(*__fW);
Howard Hinnant481ba382010-05-20 15:11:46 +00006542 __init();
6543 }
6544}
6545
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006546#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006547
Howard Hinnant481ba382010-05-20 15:11:46 +00006548template<class _RealType>
6549template<class _UnaryOperation>
6550piecewise_constant_distribution<_RealType>::param_type::param_type(
6551 initializer_list<result_type> __bl, _UnaryOperation __fw)
6552 : __b_(__bl.begin(), __bl.end())
6553{
6554 if (__b_.size() < 2)
6555 {
6556 __b_.resize(2);
6557 __b_[0] = 0;
6558 __b_[1] = 1;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006559 __densities_.assign(1, 1.0);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006560 __areas_.assign(1, 0.0);
Howard Hinnant481ba382010-05-20 15:11:46 +00006561 }
6562 else
6563 {
Howard Hinnant35d83df2010-05-24 00:35:40 +00006564 __densities_.reserve(__b_.size() - 1);
Howard Hinnant481ba382010-05-20 15:11:46 +00006565 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006566 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnant481ba382010-05-20 15:11:46 +00006567 __init();
6568 }
6569}
6570
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04006571#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006572
Howard Hinnant481ba382010-05-20 15:11:46 +00006573template<class _RealType>
6574template<class _UnaryOperation>
6575piecewise_constant_distribution<_RealType>::param_type::param_type(
6576 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6577 : __b_(__nw == 0 ? 2 : __nw + 1)
6578{
6579 size_t __n = __b_.size() - 1;
6580 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006581 __densities_.reserve(__n);
Howard Hinnant481ba382010-05-20 15:11:46 +00006582 for (size_t __i = 0; __i < __n; ++__i)
6583 {
6584 __b_[__i] = __xmin + __i * __d;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006585 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnant481ba382010-05-20 15:11:46 +00006586 }
6587 __b_[__n] = __xmax;
6588 __init();
6589}
6590
6591template<class _RealType>
Howard Hinnant481ba382010-05-20 15:11:46 +00006592template<class _URNG>
6593_RealType
6594piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6595{
6596 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnant481ba382010-05-20 15:11:46 +00006597 result_type __u = _Gen()(__g);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006598 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006599 __u) - __p.__areas_.begin() - 1;
6600 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnant481ba382010-05-20 15:11:46 +00006601}
6602
6603template <class _CharT, class _Traits, class _RT>
6604basic_ostream<_CharT, _Traits>&
6605operator<<(basic_ostream<_CharT, _Traits>& __os,
6606 const piecewise_constant_distribution<_RT>& __x)
6607{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006608 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006609 typedef basic_ostream<_CharT, _Traits> _OStream;
6610 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6611 _OStream::scientific);
Howard Hinnant481ba382010-05-20 15:11:46 +00006612 _CharT __sp = __os.widen(' ');
6613 __os.fill(__sp);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006614 size_t __n = __x.__p_.__b_.size();
Howard Hinnant481ba382010-05-20 15:11:46 +00006615 __os << __n;
6616 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006617 __os << __sp << __x.__p_.__b_[__i];
6618 __n = __x.__p_.__densities_.size();
Howard Hinnant481ba382010-05-20 15:11:46 +00006619 __os << __sp << __n;
6620 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006621 __os << __sp << __x.__p_.__densities_[__i];
6622 __n = __x.__p_.__areas_.size();
6623 __os << __sp << __n;
6624 for (size_t __i = 0; __i < __n; ++__i)
6625 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnant481ba382010-05-20 15:11:46 +00006626 return __os;
6627}
6628
6629template <class _CharT, class _Traits, class _RT>
6630basic_istream<_CharT, _Traits>&
6631operator>>(basic_istream<_CharT, _Traits>& __is,
6632 piecewise_constant_distribution<_RT>& __x)
6633{
6634 typedef piecewise_constant_distribution<_RT> _Eng;
6635 typedef typename _Eng::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00006636 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006637 typedef basic_istream<_CharT, _Traits> _Istream;
6638 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant481ba382010-05-20 15:11:46 +00006639 size_t __n;
6640 __is >> __n;
Howard Hinnant481ba382010-05-20 15:11:46 +00006641 vector<result_type> __b(__n);
6642 for (size_t __i = 0; __i < __n; ++__i)
6643 __is >> __b[__i];
Howard Hinnant35d83df2010-05-24 00:35:40 +00006644 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006645 vector<result_type> __densities(__n);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006646 for (size_t __i = 0; __i < __n; ++__i)
6647 __is >> __densities[__i];
6648 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006649 vector<result_type> __areas(__n);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006650 for (size_t __i = 0; __i < __n; ++__i)
6651 __is >> __areas[__i];
Howard Hinnant481ba382010-05-20 15:11:46 +00006652 if (!__is.fail())
6653 {
Howard Hinnant481ba382010-05-20 15:11:46 +00006654 swap(__x.__p_.__b_, __b);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006655 swap(__x.__p_.__densities_, __densities);
6656 swap(__x.__p_.__areas_, __areas);
Howard Hinnant481ba382010-05-20 15:11:46 +00006657 }
6658 return __is;
6659}
6660
Howard Hinnanta001ef32010-05-25 00:27:34 +00006661// piecewise_linear_distribution
6662
6663template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006664class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution
Howard Hinnanta001ef32010-05-25 00:27:34 +00006665{
6666public:
6667 // types
6668 typedef _RealType result_type;
6669
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006670 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnanta001ef32010-05-25 00:27:34 +00006671 {
Howard Hinnanta001ef32010-05-25 00:27:34 +00006672 vector<result_type> __b_;
Howard Hinnant167159f2010-11-18 17:01:36 +00006673 vector<result_type> __densities_;
6674 vector<result_type> __areas_;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006675 public:
6676 typedef piecewise_linear_distribution distribution_type;
6677
6678 param_type();
6679 template<class _InputIteratorB, class _InputIteratorW>
6680 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6681 _InputIteratorW __fW);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006682#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006683 template<class _UnaryOperation>
6684 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04006685#endif // _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006686 template<class _UnaryOperation>
6687 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6688 _UnaryOperation __fw);
Eric Fiselierb9f37ca2019-12-12 20:48:11 -05006689 param_type(param_type const&) = default;
Howard Hinnant71c410b2010-10-13 14:37:09 +00006690 param_type & operator=(const param_type& __rhs);
Louis Dionne173f29e2019-05-29 16:01:36 +00006691
Howard Hinnantf5f99992010-09-22 18:02:38 +00006692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006693 vector<result_type> intervals() const {return __b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006695 vector<result_type> densities() const {return __densities_;}
Howard Hinnanta001ef32010-05-25 00:27:34 +00006696
Howard Hinnantf5f99992010-09-22 18:02:38 +00006697 friend _LIBCPP_INLINE_VISIBILITY
6698 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006699 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006700 friend _LIBCPP_INLINE_VISIBILITY
6701 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006702 {return !(__x == __y);}
6703
6704 private:
6705 void __init();
6706
6707 friend class piecewise_linear_distribution;
6708
6709 template <class _CharT, class _Traits, class _RT>
6710 friend
6711 basic_ostream<_CharT, _Traits>&
6712 operator<<(basic_ostream<_CharT, _Traits>& __os,
6713 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006714
Howard Hinnanta001ef32010-05-25 00:27:34 +00006715 template <class _CharT, class _Traits, class _RT>
6716 friend
6717 basic_istream<_CharT, _Traits>&
6718 operator>>(basic_istream<_CharT, _Traits>& __is,
6719 piecewise_linear_distribution<_RT>& __x);
6720 };
6721
6722private:
6723 param_type __p_;
6724
6725public:
6726 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006728 piecewise_linear_distribution() {}
6729 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006731 piecewise_linear_distribution(_InputIteratorB __fB,
6732 _InputIteratorB __lB,
6733 _InputIteratorW __fW)
6734 : __p_(__fB, __lB, __fW) {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006735
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006736#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006737 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006739 piecewise_linear_distribution(initializer_list<result_type> __bl,
6740 _UnaryOperation __fw)
6741 : __p_(__bl, __fw) {}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04006742#endif // _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006743
6744 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006746 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6747 result_type __xmax, _UnaryOperation __fw)
6748 : __p_(__nw, __xmin, __xmax, __fw) {}
6749
Howard Hinnantf5f99992010-09-22 18:02:38 +00006750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006751 explicit piecewise_linear_distribution(const param_type& __p)
6752 : __p_(__p) {}
6753
Howard Hinnantf5f99992010-09-22 18:02:38 +00006754 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006755 void reset() {}
6756
6757 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006758 template<class _URNG>
6759 _LIBCPP_INLINE_VISIBILITY
6760 result_type operator()(_URNG& __g)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006761 {return (*this)(__g, __p_);}
6762 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6763
6764 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006766 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006768 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnanta001ef32010-05-25 00:27:34 +00006769
Howard Hinnantf5f99992010-09-22 18:02:38 +00006770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006771 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006773 void param(const param_type& __p) {__p_ = __p;}
6774
Howard Hinnantf5f99992010-09-22 18:02:38 +00006775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006776 result_type min() const {return __p_.__b_.front();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006778 result_type max() const {return __p_.__b_.back();}
6779
Howard Hinnantf5f99992010-09-22 18:02:38 +00006780 friend _LIBCPP_INLINE_VISIBILITY
6781 bool operator==(const piecewise_linear_distribution& __x,
6782 const piecewise_linear_distribution& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006783 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006784 friend _LIBCPP_INLINE_VISIBILITY
6785 bool operator!=(const piecewise_linear_distribution& __x,
6786 const piecewise_linear_distribution& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006787 {return !(__x == __y);}
6788
6789 template <class _CharT, class _Traits, class _RT>
6790 friend
6791 basic_ostream<_CharT, _Traits>&
6792 operator<<(basic_ostream<_CharT, _Traits>& __os,
6793 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006794
Howard Hinnanta001ef32010-05-25 00:27:34 +00006795 template <class _CharT, class _Traits, class _RT>
6796 friend
6797 basic_istream<_CharT, _Traits>&
6798 operator>>(basic_istream<_CharT, _Traits>& __is,
6799 piecewise_linear_distribution<_RT>& __x);
6800};
6801
6802template<class _RealType>
Howard Hinnant71c410b2010-10-13 14:37:09 +00006803typename piecewise_linear_distribution<_RealType>::param_type &
6804piecewise_linear_distribution<_RealType>::param_type::operator=
6805 (const param_type& __rhs)
6806{
6807// These can throw
6808 __b_.reserve (__rhs.__b_.size ());
6809 __densities_.reserve(__rhs.__densities_.size());
6810 __areas_.reserve (__rhs.__areas_.size());
6811
6812// These can not throw
6813 __b_ = __rhs.__b_;
6814 __densities_ = __rhs.__densities_;
6815 __areas_ = __rhs.__areas_;
6816 return *this;
6817}
6818
6819
6820template<class _RealType>
Howard Hinnanta001ef32010-05-25 00:27:34 +00006821void
6822piecewise_linear_distribution<_RealType>::param_type::__init()
6823{
Howard Hinnant167159f2010-11-18 17:01:36 +00006824 __areas_.assign(__densities_.size() - 1, result_type());
Howard Hinnantc834c512011-11-29 18:15:50 +00006825 result_type _Sp = 0;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006826 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6827 {
6828 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6829 (__b_[__i+1] - __b_[__i]) * .5;
Howard Hinnantc834c512011-11-29 18:15:50 +00006830 _Sp += __areas_[__i];
Howard Hinnanta001ef32010-05-25 00:27:34 +00006831 }
6832 for (size_t __i = __areas_.size(); __i > 1;)
6833 {
6834 --__i;
Howard Hinnantc834c512011-11-29 18:15:50 +00006835 __areas_[__i] = __areas_[__i-1] / _Sp;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006836 }
6837 __areas_[0] = 0;
6838 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6839 __areas_[__i] += __areas_[__i-1];
6840 for (size_t __i = 0; __i < __densities_.size(); ++__i)
Howard Hinnantc834c512011-11-29 18:15:50 +00006841 __densities_[__i] /= _Sp;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006842}
6843
6844template<class _RealType>
6845piecewise_linear_distribution<_RealType>::param_type::param_type()
6846 : __b_(2),
6847 __densities_(2, 1.0),
6848 __areas_(1, 0.0)
6849{
6850 __b_[1] = 1;
6851}
6852
6853template<class _RealType>
6854template<class _InputIteratorB, class _InputIteratorW>
6855piecewise_linear_distribution<_RealType>::param_type::param_type(
6856 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6857 : __b_(__fB, __lB)
6858{
6859 if (__b_.size() < 2)
6860 {
6861 __b_.resize(2);
6862 __b_[0] = 0;
6863 __b_[1] = 1;
6864 __densities_.assign(2, 1.0);
6865 __areas_.assign(1, 0.0);
6866 }
6867 else
6868 {
6869 __densities_.reserve(__b_.size());
6870 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6871 __densities_.push_back(*__fW);
6872 __init();
6873 }
6874}
6875
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006876#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006877
Howard Hinnanta001ef32010-05-25 00:27:34 +00006878template<class _RealType>
6879template<class _UnaryOperation>
6880piecewise_linear_distribution<_RealType>::param_type::param_type(
6881 initializer_list<result_type> __bl, _UnaryOperation __fw)
6882 : __b_(__bl.begin(), __bl.end())
6883{
6884 if (__b_.size() < 2)
6885 {
6886 __b_.resize(2);
6887 __b_[0] = 0;
6888 __b_[1] = 1;
6889 __densities_.assign(2, 1.0);
6890 __areas_.assign(1, 0.0);
6891 }
6892 else
6893 {
6894 __densities_.reserve(__b_.size());
6895 for (size_t __i = 0; __i < __b_.size(); ++__i)
6896 __densities_.push_back(__fw(__b_[__i]));
6897 __init();
6898 }
6899}
6900
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04006901#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006902
Howard Hinnanta001ef32010-05-25 00:27:34 +00006903template<class _RealType>
6904template<class _UnaryOperation>
6905piecewise_linear_distribution<_RealType>::param_type::param_type(
6906 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6907 : __b_(__nw == 0 ? 2 : __nw + 1)
6908{
6909 size_t __n = __b_.size() - 1;
6910 result_type __d = (__xmax - __xmin) / __n;
6911 __densities_.reserve(__b_.size());
6912 for (size_t __i = 0; __i < __n; ++__i)
6913 {
6914 __b_[__i] = __xmin + __i * __d;
6915 __densities_.push_back(__fw(__b_[__i]));
6916 }
6917 __b_[__n] = __xmax;
6918 __densities_.push_back(__fw(__b_[__n]));
6919 __init();
6920}
6921
6922template<class _RealType>
6923template<class _URNG>
6924_RealType
6925piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6926{
6927 typedef uniform_real_distribution<result_type> _Gen;
6928 result_type __u = _Gen()(__g);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006929 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006930 __u) - __p.__areas_.begin() - 1;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006931 __u -= __p.__areas_[__k];
Howard Hinnant167159f2010-11-18 17:01:36 +00006932 const result_type __dk = __p.__densities_[__k];
6933 const result_type __dk1 = __p.__densities_[__k+1];
6934 const result_type __deltad = __dk1 - __dk;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006935 const result_type __bk = __p.__b_[__k];
6936 if (__deltad == 0)
Howard Hinnant167159f2010-11-18 17:01:36 +00006937 return __u / __dk + __bk;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006938 const result_type __bk1 = __p.__b_[__k+1];
6939 const result_type __deltab = __bk1 - __bk;
Howard Hinnant167159f2010-11-18 17:01:36 +00006940 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006941 _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant167159f2010-11-18 17:01:36 +00006942 __deltad;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006943}
6944
6945template <class _CharT, class _Traits, class _RT>
6946basic_ostream<_CharT, _Traits>&
6947operator<<(basic_ostream<_CharT, _Traits>& __os,
6948 const piecewise_linear_distribution<_RT>& __x)
6949{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006950 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006951 typedef basic_ostream<_CharT, _Traits> _OStream;
6952 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6953 _OStream::scientific);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006954 _CharT __sp = __os.widen(' ');
6955 __os.fill(__sp);
6956 size_t __n = __x.__p_.__b_.size();
6957 __os << __n;
6958 for (size_t __i = 0; __i < __n; ++__i)
6959 __os << __sp << __x.__p_.__b_[__i];
6960 __n = __x.__p_.__densities_.size();
6961 __os << __sp << __n;
6962 for (size_t __i = 0; __i < __n; ++__i)
6963 __os << __sp << __x.__p_.__densities_[__i];
6964 __n = __x.__p_.__areas_.size();
6965 __os << __sp << __n;
6966 for (size_t __i = 0; __i < __n; ++__i)
6967 __os << __sp << __x.__p_.__areas_[__i];
6968 return __os;
6969}
6970
6971template <class _CharT, class _Traits, class _RT>
6972basic_istream<_CharT, _Traits>&
6973operator>>(basic_istream<_CharT, _Traits>& __is,
6974 piecewise_linear_distribution<_RT>& __x)
6975{
6976 typedef piecewise_linear_distribution<_RT> _Eng;
6977 typedef typename _Eng::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00006978 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006979 typedef basic_istream<_CharT, _Traits> _Istream;
6980 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006981 size_t __n;
6982 __is >> __n;
6983 vector<result_type> __b(__n);
6984 for (size_t __i = 0; __i < __n; ++__i)
6985 __is >> __b[__i];
6986 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006987 vector<result_type> __densities(__n);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006988 for (size_t __i = 0; __i < __n; ++__i)
6989 __is >> __densities[__i];
6990 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006991 vector<result_type> __areas(__n);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006992 for (size_t __i = 0; __i < __n; ++__i)
6993 __is >> __areas[__i];
6994 if (!__is.fail())
6995 {
6996 swap(__x.__p_.__b_, __b);
6997 swap(__x.__p_.__densities_, __densities);
6998 swap(__x.__p_.__areas_, __areas);
6999 }
7000 return __is;
7001}
7002
Howard Hinnantc51e1022010-05-11 19:42:16 +00007003_LIBCPP_END_NAMESPACE_STD
7004
Eric Fiselierf4433a32017-05-31 22:07:49 +00007005_LIBCPP_POP_MACROS
7006
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04007007#endif // _LIBCPP_RANDOM