blob: a2f45ba7ea4213954c88dbd7d3b18b0d2bdccf5f [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>
1681#include <cstddef>
Eric Fiselier90adc202015-01-23 22:22:36 +00001682#include <cstdint>
1683#include <cmath>
Christopher Di Bella68074072021-02-17 02:52:17 +00001684#include <concepts>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685#include <type_traits>
1686#include <initializer_list>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687#include <limits>
1688#include <algorithm>
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00001689#include <numeric>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690#include <vector>
1691#include <string>
Louis Dionne3df65ce2020-10-15 13:27:27 -04001692#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693
Howard Hinnantaaaa52b2011-10-17 20:05:10 +00001694#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +00001696#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697
Eric Fiselierf4433a32017-05-31 22:07:49 +00001698_LIBCPP_PUSH_MACROS
1699#include <__undef_macros>
1700
1701
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702_LIBCPP_BEGIN_NAMESPACE_STD
1703
Christopher Di Bella88110022021-02-19 01:54:52 +00001704#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
Christopher Di Bella68074072021-02-17 02:52:17 +00001705
1706// [rand.req.urng]
1707template<class _Gen>
1708concept uniform_random_bit_generator =
1709 invocable<_Gen&> && unsigned_integral<invoke_result_t<_Gen&>> &&
1710 requires {
1711 { _Gen::min() } -> same_as<invoke_result_t<_Gen&>>;
1712 { _Gen::max() } -> same_as<invoke_result_t<_Gen&>>;
1713 requires bool_constant<(_Gen::min() < _Gen::max())>::value;
1714 };
1715
Christopher Di Bella88110022021-02-19 01:54:52 +00001716#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
Christopher Di Bella68074072021-02-17 02:52:17 +00001717
Howard Hinnantc8c73992011-04-11 18:22:12 +00001718// __is_seed_sequence
1719
1720template <class _Sseq, class _Engine>
1721struct __is_seed_sequence
1722{
Howard Hinnant5a646852012-04-02 21:00:45 +00001723 static _LIBCPP_CONSTEXPR const bool value =
Howard Hinnantc8c73992011-04-11 18:22:12 +00001724 !is_convertible<_Sseq, typename _Engine::result_type>::value &&
1725 !is_same<typename remove_cv<_Sseq>::type, _Engine>::value;
1726};
1727
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728// linear_congruential_engine
1729
1730template <unsigned long long __a, unsigned long long __c,
Howard Hinnantc834c512011-11-29 18:15:50 +00001731 unsigned long long __m, unsigned long long _Mp,
zoecarver68c37022020-11-10 18:23:22 -08001732 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_Mp-__c)/__a),
1733 bool _OverflowOK = ((__m|__m-1) > __m), // m = 2^n
1734 bool _SchrageOK = (__a != 0 && __m != 0 && __m % __a <= __m / __a)> // r <= q
1735struct __lce_alg_picker
1736{
1737 static_assert(__a != 0 || __m != 0 || !_MightOverflow || _OverflowOK || _SchrageOK,
1738 "The current values of a, c, and m cannot generate a number "
1739 "within bounds of linear_congruential_engine.");
1740
1741 static _LIBCPP_CONSTEXPR const bool __use_schrage = _MightOverflow &&
1742 !_OverflowOK &&
1743 _SchrageOK;
1744};
1745
1746template <unsigned long long __a, unsigned long long __c,
1747 unsigned long long __m, unsigned long long _Mp,
1748 bool _UseSchrage = __lce_alg_picker<__a, __c, __m, _Mp>::__use_schrage>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749struct __lce_ta;
1750
1751// 64
1752
1753template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1754struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1755{
1756 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 static result_type next(result_type __x)
1759 {
1760 // Schrage's algorithm
1761 const result_type __q = __m / __a;
1762 const result_type __r = __m % __a;
1763 const result_type __t0 = __a * (__x % __q);
1764 const result_type __t1 = __r * (__x / __q);
1765 __x = __t0 + (__t0 < __t1) * __m - __t1;
1766 __x += __c - (__x >= __m - __c) * __m;
1767 return __x;
1768 }
1769};
1770
1771template <unsigned long long __a, unsigned long long __m>
1772struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1773{
1774 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 static result_type next(result_type __x)
1777 {
1778 // Schrage's algorithm
1779 const result_type __q = __m / __a;
1780 const result_type __r = __m % __a;
1781 const result_type __t0 = __a * (__x % __q);
1782 const result_type __t1 = __r * (__x / __q);
1783 __x = __t0 + (__t0 < __t1) * __m - __t1;
1784 return __x;
1785 }
1786};
1787
1788template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1789struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1790{
1791 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 static result_type next(result_type __x)
1794 {
1795 return (__a * __x + __c) % __m;
1796 }
1797};
1798
1799template <unsigned long long __a, unsigned long long __c>
1800struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1801{
1802 typedef unsigned long long result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 static result_type next(result_type __x)
1805 {
1806 return __a * __x + __c;
1807 }
1808};
1809
1810// 32
1811
Howard Hinnantc834c512011-11-29 18:15:50 +00001812template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1813struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814{
1815 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 static result_type next(result_type __x)
1818 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001819 const result_type __a = static_cast<result_type>(_Ap);
1820 const result_type __c = static_cast<result_type>(_Cp);
1821 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822 // Schrage's algorithm
1823 const result_type __q = __m / __a;
1824 const result_type __r = __m % __a;
1825 const result_type __t0 = __a * (__x % __q);
1826 const result_type __t1 = __r * (__x / __q);
1827 __x = __t0 + (__t0 < __t1) * __m - __t1;
1828 __x += __c - (__x >= __m - __c) * __m;
1829 return __x;
1830 }
1831};
1832
Howard Hinnantc834c512011-11-29 18:15:50 +00001833template <unsigned long long _Ap, unsigned long long _Mp>
1834struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835{
1836 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 static result_type next(result_type __x)
1839 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001840 const result_type __a = static_cast<result_type>(_Ap);
1841 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001842 // Schrage's algorithm
1843 const result_type __q = __m / __a;
1844 const result_type __r = __m % __a;
1845 const result_type __t0 = __a * (__x % __q);
1846 const result_type __t1 = __r * (__x / __q);
1847 __x = __t0 + (__t0 < __t1) * __m - __t1;
1848 return __x;
1849 }
1850};
1851
Howard Hinnantc834c512011-11-29 18:15:50 +00001852template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
1853struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), false>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854{
1855 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857 static result_type next(result_type __x)
1858 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001859 const result_type __a = static_cast<result_type>(_Ap);
1860 const result_type __c = static_cast<result_type>(_Cp);
1861 const result_type __m = static_cast<result_type>(_Mp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862 return (__a * __x + __c) % __m;
1863 }
1864};
1865
Howard Hinnantc834c512011-11-29 18:15:50 +00001866template <unsigned long long _Ap, unsigned long long _Cp>
1867struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868{
1869 typedef unsigned result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001870 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 static result_type next(result_type __x)
1872 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001873 const result_type __a = static_cast<result_type>(_Ap);
1874 const result_type __c = static_cast<result_type>(_Cp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875 return __a * __x + __c;
1876 }
1877};
1878
1879// 16
1880
1881template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1882struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1883{
1884 typedef unsigned short result_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 static result_type next(result_type __x)
1887 {
1888 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1889 }
1890};
1891
1892template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001893class _LIBCPP_TEMPLATE_VIS linear_congruential_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894
1895template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001896 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001897_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898basic_ostream<_CharT, _Traits>&
1899operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00001900 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901
1902template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00001903 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904basic_istream<_CharT, _Traits>&
1905operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00001906 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907
1908template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001909class _LIBCPP_TEMPLATE_VIS linear_congruential_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910{
1911public:
1912 // types
1913 typedef _UIntType result_type;
1914
Howard Hinnanta741b242013-05-21 21:19:35 +00001915private:
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 result_type __x_;
1917
Howard Hinnant5a646852012-04-02 21:00:45 +00001918 static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919
1920 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1921 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
zoecarver68c37022020-11-10 18:23:22 -08001922 static_assert(_VSTD::is_unsigned<_UIntType>::value, "_UIntType must be unsigned type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923public:
Howard Hinnant5a646852012-04-02 21:00:45 +00001924 static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u: 0u;
1925 static _LIBCPP_CONSTEXPR const result_type _Max = __m - 1u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1927
1928 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00001929 static _LIBCPP_CONSTEXPR const result_type multiplier = __a;
1930 static _LIBCPP_CONSTEXPR const result_type increment = __c;
1931 static _LIBCPP_CONSTEXPR const result_type modulus = __m;
Howard Hinnantf5f99992010-09-22 18:02:38 +00001932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00001933 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00001935 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
1936 static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937
1938 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001939#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00001940 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01001941 linear_congruential_engine() : linear_congruential_engine(default_seed) {}
1942 _LIBCPP_INLINE_VISIBILITY
1943 explicit linear_congruential_engine(result_type __s) { seed(__s); }
1944#else
1945 _LIBCPP_INLINE_VISIBILITY
1946 explicit linear_congruential_engine(result_type __s = default_seed) {
1947 seed(__s);
1948 }
1949#endif
Howard Hinnant28b24882011-12-01 20:21:04 +00001950 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00001951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001952 explicit linear_congruential_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00001953 typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954 {seed(__q);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956 void seed(result_type __s = default_seed)
1957 {seed(integral_constant<bool, __m == 0>(),
1958 integral_constant<bool, __c == 0>(), __s);}
1959 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00001960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961 typename enable_if
1962 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00001963 __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964 void
1965 >::type
1966 seed(_Sseq& __q)
1967 {__seed(__q, integral_constant<unsigned,
1968 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
Howard Hinnanta0c4f7c2013-05-21 21:05:12 +00001969 : (__m > 0x100000000ull))>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970
1971 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00001972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973 result_type operator()()
Howard Hinnantc834c512011-11-29 18:15:50 +00001974 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1977
Howard Hinnantf5f99992010-09-22 18:02:38 +00001978 friend _LIBCPP_INLINE_VISIBILITY
1979 bool operator==(const linear_congruential_engine& __x,
1980 const linear_congruential_engine& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981 {return __x.__x_ == __y.__x_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001982 friend _LIBCPP_INLINE_VISIBILITY
1983 bool operator!=(const linear_congruential_engine& __x,
1984 const linear_congruential_engine& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985 {return !(__x == __y);}
1986
1987private:
1988
Howard Hinnantf5f99992010-09-22 18:02:38 +00001989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1995 1 : __s % __m;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00001996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1998
1999 template<class _Sseq>
2000 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2001 template<class _Sseq>
2002 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2003
2004 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00002005 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006 friend
2007 basic_ostream<_CharT, _Traits>&
2008 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00002009 const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002010
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00002012 class _Up, _Up _Ap, _Up _Cp, _Up _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013 friend
2014 basic_istream<_CharT, _Traits>&
2015 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00002016 linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017};
2018
2019template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002020 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2021 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
2022
2023template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2024 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2025 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
2026
2027template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2028 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2029 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
2030
2031template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2032 _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
2033 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
2034
2035template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036template<class _Sseq>
2037void
2038linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
2039 integral_constant<unsigned, 1>)
2040{
2041 const unsigned __k = 1;
2042 uint32_t __ar[__k+3];
2043 __q.generate(__ar, __ar + __k + 3);
2044 result_type __s = static_cast<result_type>(__ar[3] % __m);
2045 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
2046}
2047
2048template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2049template<class _Sseq>
2050void
2051linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
2052 integral_constant<unsigned, 2>)
2053{
2054 const unsigned __k = 2;
2055 uint32_t __ar[__k+3];
2056 __q.generate(__ar, __ar + __k + 3);
2057 result_type __s = static_cast<result_type>((__ar[3] +
Howard Hinnanta0c4f7c2013-05-21 21:05:12 +00002058 ((uint64_t)__ar[4] << 32)) % __m);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
2060}
2061
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062template <class _CharT, class _Traits,
2063 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002064inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065basic_ostream<_CharT, _Traits>&
2066operator<<(basic_ostream<_CharT, _Traits>& __os,
2067 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2068{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002069 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002070 typedef basic_ostream<_CharT, _Traits> _Ostream;
2071 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072 __os.fill(__os.widen(' '));
2073 return __os << __x.__x_;
2074}
2075
2076template <class _CharT, class _Traits,
2077 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
2078basic_istream<_CharT, _Traits>&
2079operator>>(basic_istream<_CharT, _Traits>& __is,
2080 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2081{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002082 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002083 typedef basic_istream<_CharT, _Traits> _Istream;
2084 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002085 _UIntType __t;
2086 __is >> __t;
2087 if (!__is.fail())
2088 __x.__x_ = __t;
2089 return __is;
2090}
2091
2092typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2093 minstd_rand0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2095 minstd_rand;
Howard Hinnant481ba382010-05-20 15:11:46 +00002096typedef minstd_rand default_random_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097// mersenne_twister_engine
2098
2099template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2100 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2101 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002102class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103
Eric Fiselier4638fca2017-05-31 21:20:18 +00002104template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2105 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2106 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002108operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002109 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002110 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002111 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002112
Eric Fiselier4638fca2017-05-31 21:20:18 +00002113template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2114 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2115 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnanta54386e2012-09-14 00:39:16 +00002116_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002118operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002119 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002120 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002121 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122
2123template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002124 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2125 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2126 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127basic_ostream<_CharT, _Traits>&
2128operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002129 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002130 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131
2132template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002133 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2134 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2135 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136basic_istream<_CharT, _Traits>&
2137operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002138 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002139 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140
2141template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2142 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2143 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002144class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145{
2146public:
2147 // types
2148 typedef _UIntType result_type;
2149
2150private:
2151 result_type __x_[__n];
2152 size_t __i_;
2153
2154 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2155 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
Howard Hinnant5a646852012-04-02 21:00:45 +00002156 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2158 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2159 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2160 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2161 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2162 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2163 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2164public:
Howard Hinnant5a646852012-04-02 21:00:45 +00002165 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2166 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2167 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2169 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2170 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2171 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2172 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2173 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2174
2175 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002176 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2177 static _LIBCPP_CONSTEXPR const size_t state_size = __n;
2178 static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
2179 static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
2180 static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
2181 static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
2182 static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
2183 static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
2184 static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
2185 static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
2186 static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
2187 static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
2188 static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
Howard Hinnantf5f99992010-09-22 18:02:38 +00002189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002190 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002192 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2193 static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194
2195 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002196#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002197 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002198 mersenne_twister_engine() : mersenne_twister_engine(default_seed) {}
2199 _LIBCPP_INLINE_VISIBILITY
2200 explicit mersenne_twister_engine(result_type __sd) { seed(__sd); }
2201#else
2202 _LIBCPP_INLINE_VISIBILITY
2203 explicit mersenne_twister_engine(result_type __sd = default_seed) {
2204 seed(__sd);
2205 }
2206#endif
Howard Hinnant28b24882011-12-01 20:21:04 +00002207 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002209 explicit mersenne_twister_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002210 typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 {seed(__q);}
2212 void seed(result_type __sd = default_seed);
2213 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215 typename enable_if
2216 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002217 __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218 void
2219 >::type
2220 seed(_Sseq& __q)
2221 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2222
2223 // generating functions
2224 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2227
Eric Fiselier4638fca2017-05-31 21:20:18 +00002228 template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2229 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2230 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231 friend
2232 bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002233 operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002234 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002235 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002236 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002237
Eric Fiselier4638fca2017-05-31 21:20:18 +00002238 template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2239 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2240 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241 friend
2242 bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002243 operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002244 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002245 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002246 _Bp, _Tp, _Cp, _Lp, _Fp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247
2248 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002249 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2250 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2251 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252 friend
2253 basic_ostream<_CharT, _Traits>&
2254 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002255 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002256 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257
2258 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002259 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2260 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2261 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 friend
2263 basic_istream<_CharT, _Traits>&
2264 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002265 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002266 _Bp, _Tp, _Cp, _Lp, _Fp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267private:
2268
2269 template<class _Sseq>
2270 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2271 template<class _Sseq>
2272 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2273
2274 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276 static
2277 typename enable_if
2278 <
2279 __count < __w,
2280 result_type
2281 >::type
2282 __lshift(result_type __x) {return (__x << __count) & _Max;}
2283
2284 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286 static
2287 typename enable_if
2288 <
2289 (__count >= __w),
2290 result_type
2291 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002292 __lshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002293
2294 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 static
2297 typename enable_if
2298 <
2299 __count < _Dt,
2300 result_type
2301 >::type
2302 __rshift(result_type __x) {return __x >> __count;}
2303
2304 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002306 static
2307 typename enable_if
2308 <
2309 (__count >= _Dt),
2310 result_type
2311 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002312 __rshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313};
2314
2315template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2316 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2317 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002318 _LIBCPP_CONSTEXPR const size_t
2319 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size;
2320
2321template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2322 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2323 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002324 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002325 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size;
2326
2327template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2328 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2329 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002330 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002331 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size;
2332
2333template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2334 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2335 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002336 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002337 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits;
2338
2339template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2340 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2341 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2342 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2343 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask;
2344
2345template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2346 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2347 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002348 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002349 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u;
2350
2351template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2352 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2353 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2354 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2355 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d;
2356
2357template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2358 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2359 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002360 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002361 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s;
2362
2363template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2364 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2365 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2366 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2367 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b;
2368
2369template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2370 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2371 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002372 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002373 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t;
2374
2375template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2376 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2377 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2378 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2379 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c;
2380
2381template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2382 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2383 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Louis Dionne173f29e2019-05-29 16:01:36 +00002384 _LIBCPP_CONSTEXPR const size_t
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002385 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l;
2386
2387template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2388 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2389 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2390 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2391 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier;
2392
2393template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2394 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2395 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2396 _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type
2397 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed;
2398
2399template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2400 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2401 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402void
2403mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2404 __t, __c, __l, __f>::seed(result_type __sd)
Marshall Clowc1894632017-09-11 18:10:33 +00002405 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002406{ // __w >= 2
2407 __x_[0] = __sd & _Max;
2408 for (size_t __i = 1; __i < __n; ++__i)
2409 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2410 __i_ = 0;
2411}
2412
2413template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2414 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2415 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2416template<class _Sseq>
2417void
2418mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2419 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2420{
2421 const unsigned __k = 1;
2422 uint32_t __ar[__n * __k];
2423 __q.generate(__ar, __ar + __n * __k);
2424 for (size_t __i = 0; __i < __n; ++__i)
2425 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2426 const result_type __mask = __r == _Dt ? result_type(~0) :
2427 (result_type(1) << __r) - result_type(1);
2428 __i_ = 0;
2429 if ((__x_[0] & ~__mask) == 0)
2430 {
2431 for (size_t __i = 1; __i < __n; ++__i)
2432 if (__x_[__i] != 0)
2433 return;
Hubert Tongca5b7762018-08-16 23:56:54 +00002434 __x_[0] = result_type(1) << (__w - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435 }
2436}
2437
2438template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2439 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2440 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2441template<class _Sseq>
2442void
2443mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2444 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2445{
2446 const unsigned __k = 2;
2447 uint32_t __ar[__n * __k];
2448 __q.generate(__ar, __ar + __n * __k);
2449 for (size_t __i = 0; __i < __n; ++__i)
2450 __x_[__i] = static_cast<result_type>(
2451 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2452 const result_type __mask = __r == _Dt ? result_type(~0) :
2453 (result_type(1) << __r) - result_type(1);
2454 __i_ = 0;
2455 if ((__x_[0] & ~__mask) == 0)
2456 {
2457 for (size_t __i = 1; __i < __n; ++__i)
2458 if (__x_[__i] != 0)
2459 return;
Hubert Tongca5b7762018-08-16 23:56:54 +00002460 __x_[0] = result_type(1) << (__w - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461 }
2462}
2463
2464template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2465 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2466 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2467_UIntType
2468mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2469 __t, __c, __l, __f>::operator()()
2470{
2471 const size_t __j = (__i_ + 1) % __n;
2472 const result_type __mask = __r == _Dt ? result_type(~0) :
2473 (result_type(1) << __r) - result_type(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00002474 const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475 const size_t __k = (__i_ + __m) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00002476 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2478 __i_ = __j;
2479 __z ^= __lshift<__s>(__z) & __b;
2480 __z ^= __lshift<__t>(__z) & __c;
2481 return __z ^ __rshift<__l>(__z);
2482}
2483
Eric Fiselier4638fca2017-05-31 21:20:18 +00002484template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2485 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2486 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002488operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002489 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002490 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002491 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492{
2493 if (__x.__i_ == __y.__i_)
Howard Hinnantc834c512011-11-29 18:15:50 +00002494 return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495 if (__x.__i_ == 0 || __y.__i_ == 0)
2496 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002497 size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002498 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499 __y.__x_ + __y.__i_))
2500 return false;
2501 if (__x.__i_ == 0)
Howard Hinnantc834c512011-11-29 18:15:50 +00002502 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_);
2503 return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504 }
2505 if (__x.__i_ < __y.__i_)
2506 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002507 size_t __j = _Np - __y.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002508 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509 __y.__x_ + __y.__i_))
2510 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002511 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512 __y.__x_))
2513 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002514 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002515 __y.__x_ + (_Np - (__x.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002516 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002517 size_t __j = _Np - __x.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002518 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519 __x.__x_ + __x.__i_))
2520 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002521 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522 __x.__x_))
2523 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002524 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002525 __x.__x_ + (_Np - (__y.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526}
2527
Eric Fiselier4638fca2017-05-31 21:20:18 +00002528template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2529 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2530 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002531inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532bool
Eric Fiselier4638fca2017-05-31 21:20:18 +00002533operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002534 _Bp, _Tp, _Cp, _Lp, _Fp>& __x,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002535 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002536 _Bp, _Tp, _Cp, _Lp, _Fp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537{
2538 return !(__x == __y);
2539}
2540
2541template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002542 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2543 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2544 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545basic_ostream<_CharT, _Traits>&
2546operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002547 const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002548 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002550 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002551 typedef basic_ostream<_CharT, _Traits> _Ostream;
2552 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553 _CharT __sp = __os.widen(' ');
2554 __os.fill(__sp);
2555 __os << __x.__x_[__x.__i_];
Howard Hinnantc834c512011-11-29 18:15:50 +00002556 for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557 __os << __sp << __x.__x_[__j];
2558 for (size_t __j = 0; __j < __x.__i_; ++__j)
2559 __os << __sp << __x.__x_[__j];
2560 return __os;
2561}
2562
2563template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002564 class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp,
2565 _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp,
2566 _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002567basic_istream<_CharT, _Traits>&
2568operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002569 mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
Howard Hinnantc834c512011-11-29 18:15:50 +00002570 _Bp, _Tp, _Cp, _Lp, _Fp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002572 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002573 typedef basic_istream<_CharT, _Traits> _Istream;
2574 __is.flags(_Istream::dec | _Istream::skipws);
Eric Fiselier4638fca2017-05-31 21:20:18 +00002575 _UInt __t[_Np];
Howard Hinnantc834c512011-11-29 18:15:50 +00002576 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577 __is >> __t[__i];
2578 if (!__is.fail())
2579 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002580 for (size_t __i = 0; __i < _Np; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581 __x.__x_[__i] = __t[__i];
2582 __x.__i_ = 0;
2583 }
2584 return __is;
2585}
2586
2587typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2588 0x9908b0df, 11, 0xffffffff,
2589 7, 0x9d2c5680,
2590 15, 0xefc60000,
2591 18, 1812433253> mt19937;
2592typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2593 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2594 17, 0x71d67fffeda60000ULL,
2595 37, 0xfff7eee000000000ULL,
2596 43, 6364136223846793005ULL> mt19937_64;
2597
2598// subtract_with_carry_engine
2599
2600template<class _UIntType, size_t __w, size_t __s, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002601class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602
Eric Fiselier4638fca2017-05-31 21:20:18 +00002603template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604bool
2605operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002606 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2607 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608
Eric Fiselier4638fca2017-05-31 21:20:18 +00002609template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnanta54386e2012-09-14 00:39:16 +00002610_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611bool
2612operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002613 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2614 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615
2616template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002617 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618basic_ostream<_CharT, _Traits>&
2619operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002620 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621
2622template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002623 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624basic_istream<_CharT, _Traits>&
2625operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002626 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627
2628template<class _UIntType, size_t __w, size_t __s, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002629class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630{
2631public:
2632 // types
2633 typedef _UIntType result_type;
2634
2635private:
2636 result_type __x_[__r];
2637 result_type __c_;
2638 size_t __i_;
2639
Howard Hinnant5a646852012-04-02 21:00:45 +00002640 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2642 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2643 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2644 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2645public:
Howard Hinnant5a646852012-04-02 21:00:45 +00002646 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
2647 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
2648 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2650
2651 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002652 static _LIBCPP_CONSTEXPR const size_t word_size = __w;
2653 static _LIBCPP_CONSTEXPR const size_t short_lag = __s;
2654 static _LIBCPP_CONSTEXPR const size_t long_lag = __r;
Howard Hinnantf5f99992010-09-22 18:02:38 +00002655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002656 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002658 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
2659 static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660
2661 // constructors and seeding functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002662#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002663 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01002664 subtract_with_carry_engine() : subtract_with_carry_engine(default_seed) {}
2665 _LIBCPP_INLINE_VISIBILITY
2666 explicit subtract_with_carry_engine(result_type __sd) { seed(__sd); }
2667#else
2668 _LIBCPP_INLINE_VISIBILITY
2669 explicit subtract_with_carry_engine(result_type __sd = default_seed) {
2670 seed(__sd);
2671 }
2672#endif
Howard Hinnant28b24882011-12-01 20:21:04 +00002673 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00002675 explicit subtract_with_carry_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002676 typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677 {seed(__q);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679 void seed(result_type __sd = default_seed)
2680 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2681 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683 typename enable_if
2684 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002685 __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686 void
2687 >::type
2688 seed(_Sseq& __q)
2689 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2690
2691 // generating functions
2692 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2695
Eric Fiselier4638fca2017-05-31 21:20:18 +00002696 template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697 friend
2698 bool
2699 operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002700 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2701 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002702
Eric Fiselier4638fca2017-05-31 21:20:18 +00002703 template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704 friend
2705 bool
2706 operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002707 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2708 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002709
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002711 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712 friend
2713 basic_ostream<_CharT, _Traits>&
2714 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002715 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002716
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002718 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719 friend
2720 basic_istream<_CharT, _Traits>&
2721 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002722 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002723
2724private:
2725
2726 void seed(result_type __sd, integral_constant<unsigned, 1>);
2727 void seed(result_type __sd, integral_constant<unsigned, 2>);
2728 template<class _Sseq>
2729 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2730 template<class _Sseq>
2731 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2732};
2733
2734template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00002735 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
2736
2737template<class _UIntType, size_t __w, size_t __s, size_t __r>
2738 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
2739
2740template<class _UIntType, size_t __w, size_t __s, size_t __r>
2741 _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
2742
2743template<class _UIntType, size_t __w, size_t __s, size_t __r>
2744 _LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type
2745 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
2746
2747template<class _UIntType, size_t __w, size_t __s, size_t __r>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748void
2749subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2750 integral_constant<unsigned, 1>)
2751{
2752 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2753 __e(__sd == 0u ? default_seed : __sd);
2754 for (size_t __i = 0; __i < __r; ++__i)
2755 __x_[__i] = static_cast<result_type>(__e() & _Max);
2756 __c_ = __x_[__r-1] == 0;
2757 __i_ = 0;
2758}
2759
2760template<class _UIntType, size_t __w, size_t __s, size_t __r>
2761void
2762subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2763 integral_constant<unsigned, 2>)
2764{
2765 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2766 __e(__sd == 0u ? default_seed : __sd);
2767 for (size_t __i = 0; __i < __r; ++__i)
Howard Hinnant93072c12011-08-15 17:22:22 +00002768 {
2769 result_type __e0 = __e();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770 __x_[__i] = static_cast<result_type>(
Howard Hinnant93072c12011-08-15 17:22:22 +00002771 (__e0 + ((uint64_t)__e() << 32)) & _Max);
2772 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773 __c_ = __x_[__r-1] == 0;
2774 __i_ = 0;
2775}
2776
2777template<class _UIntType, size_t __w, size_t __s, size_t __r>
2778template<class _Sseq>
2779void
2780subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2781 integral_constant<unsigned, 1>)
2782{
2783 const unsigned __k = 1;
2784 uint32_t __ar[__r * __k];
2785 __q.generate(__ar, __ar + __r * __k);
2786 for (size_t __i = 0; __i < __r; ++__i)
2787 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2788 __c_ = __x_[__r-1] == 0;
2789 __i_ = 0;
2790}
2791
2792template<class _UIntType, size_t __w, size_t __s, size_t __r>
2793template<class _Sseq>
2794void
2795subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2796 integral_constant<unsigned, 2>)
2797{
2798 const unsigned __k = 2;
2799 uint32_t __ar[__r * __k];
2800 __q.generate(__ar, __ar + __r * __k);
2801 for (size_t __i = 0; __i < __r; ++__i)
2802 __x_[__i] = static_cast<result_type>(
2803 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2804 __c_ = __x_[__r-1] == 0;
2805 __i_ = 0;
2806}
2807
2808template<class _UIntType, size_t __w, size_t __s, size_t __r>
2809_UIntType
2810subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2811{
2812 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2813 result_type& __xr = __x_[__i_];
2814 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2815 __xr = (__xs - __xr - __c_) & _Max;
2816 __c_ = __new_c;
2817 __i_ = (__i_ + 1) % __r;
2818 return __xr;
2819}
2820
Eric Fiselier4638fca2017-05-31 21:20:18 +00002821template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822bool
2823operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002824 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2825 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826{
2827 if (__x.__c_ != __y.__c_)
2828 return false;
2829 if (__x.__i_ == __y.__i_)
Howard Hinnantc834c512011-11-29 18:15:50 +00002830 return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831 if (__x.__i_ == 0 || __y.__i_ == 0)
2832 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002833 size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002834 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835 __y.__x_ + __y.__i_))
2836 return false;
2837 if (__x.__i_ == 0)
Howard Hinnantc834c512011-11-29 18:15:50 +00002838 return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
2839 return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840 }
2841 if (__x.__i_ < __y.__i_)
2842 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002843 size_t __j = _Rp - __y.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002844 if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845 __y.__x_ + __y.__i_))
2846 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002847 if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848 __y.__x_))
2849 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002850 return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002851 __y.__x_ + (_Rp - (__x.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002853 size_t __j = _Rp - __x.__i_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002854 if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855 __x.__x_ + __x.__i_))
2856 return false;
Howard Hinnantc834c512011-11-29 18:15:50 +00002857 if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858 __x.__x_))
2859 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002860 return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
Howard Hinnantc834c512011-11-29 18:15:50 +00002861 __x.__x_ + (_Rp - (__y.__i_ + __j)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862}
2863
Eric Fiselier4638fca2017-05-31 21:20:18 +00002864template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002865inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866bool
2867operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00002868 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
2869 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870{
2871 return !(__x == __y);
2872}
2873
2874template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002875 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876basic_ostream<_CharT, _Traits>&
2877operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002878 const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002880 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002881 typedef basic_ostream<_CharT, _Traits> _Ostream;
2882 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883 _CharT __sp = __os.widen(' ');
2884 __os.fill(__sp);
2885 __os << __x.__x_[__x.__i_];
Howard Hinnantc834c512011-11-29 18:15:50 +00002886 for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887 __os << __sp << __x.__x_[__j];
2888 for (size_t __j = 0; __j < __x.__i_; ++__j)
2889 __os << __sp << __x.__x_[__j];
2890 __os << __sp << __x.__c_;
2891 return __os;
2892}
2893
2894template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002895 class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896basic_istream<_CharT, _Traits>&
2897operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002898 subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899{
Howard Hinnant49e145e2012-10-30 19:06:59 +00002900 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04002901 typedef basic_istream<_CharT, _Traits> _Istream;
2902 __is.flags(_Istream::dec | _Istream::skipws);
Eric Fiselier4638fca2017-05-31 21:20:18 +00002903 _UInt __t[_Rp+1];
Howard Hinnantc834c512011-11-29 18:15:50 +00002904 for (size_t __i = 0; __i < _Rp+1; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905 __is >> __t[__i];
2906 if (!__is.fail())
2907 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002908 for (size_t __i = 0; __i < _Rp; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 __x.__x_[__i] = __t[__i];
Howard Hinnantc834c512011-11-29 18:15:50 +00002910 __x.__c_ = __t[_Rp];
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911 __x.__i_ = 0;
2912 }
2913 return __is;
2914}
2915
2916typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2917typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2918
2919// discard_block_engine
2920
2921template<class _Engine, size_t __p, size_t __r>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002922class _LIBCPP_TEMPLATE_VIS discard_block_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923{
2924 _Engine __e_;
2925 int __n_;
2926
2927 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2928 static_assert(__r <= __p, "discard_block_engine invalid parameters");
Eric Fiselier37c22152016-12-24 00:24:44 +00002929 static_assert(__r <= INT_MAX, "discard_block_engine invalid parameters");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930public:
2931 // types
2932 typedef typename _Engine::result_type result_type;
2933
2934 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00002935 static _LIBCPP_CONSTEXPR const size_t block_size = __p;
2936 static _LIBCPP_CONSTEXPR const size_t used_block = __r;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00002938#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939 static const result_type _Min = _Engine::_Min;
2940 static const result_type _Max = _Engine::_Max;
Howard Hinnant5a646852012-04-02 21:00:45 +00002941#else
2942 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
2943 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
2944#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002945
Howard Hinnantf5f99992010-09-22 18:02:38 +00002946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002947 static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
Howard Hinnantf5f99992010-09-22 18:02:38 +00002948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00002949 static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002950
2951 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00002952 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953 discard_block_engine() : __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002955 explicit discard_block_engine(const _Engine& __e)
2956 : __e_(__e), __n_(0) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00002957#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002959 explicit discard_block_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002960 : __e_(_VSTD::move(__e)), __n_(0) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00002961#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00002962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002964 template<class _Sseq>
2965 _LIBCPP_INLINE_VISIBILITY
2966 explicit discard_block_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00002967 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002968 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 : __e_(__q), __n_(0) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 void seed() {__e_.seed(); __n_ = 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00002972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002974 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00002975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002976 typename enable_if
2977 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00002978 __is_seed_sequence<_Sseq, discard_block_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00002979 void
2980 >::type
2981 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982
2983 // generating functions
2984 result_type operator()();
Howard Hinnantf5f99992010-09-22 18:02:38 +00002985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2987
2988 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00002989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00002990 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991
Howard Hinnantc834c512011-11-29 18:15:50 +00002992 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993 friend
2994 bool
2995 operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00002996 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
2997 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002998
Howard Hinnantc834c512011-11-29 18:15:50 +00002999 template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000 friend
3001 bool
3002 operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00003003 const discard_block_engine<_Eng, _Pp, _Rp>& __x,
3004 const discard_block_engine<_Eng, _Pp, _Rp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003005
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003007 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003008 friend
3009 basic_ostream<_CharT, _Traits>&
3010 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003011 const discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003012
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003014 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015 friend
3016 basic_istream<_CharT, _Traits>&
3017 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003018 discard_block_engine<_Eng, _Pp, _Rp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019};
3020
3021template<class _Engine, size_t __p, size_t __r>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00003022 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
3023
3024template<class _Engine, size_t __p, size_t __r>
3025 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
3026
3027template<class _Engine, size_t __p, size_t __r>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028typename discard_block_engine<_Engine, __p, __r>::result_type
3029discard_block_engine<_Engine, __p, __r>::operator()()
3030{
Eric Fiselier37c22152016-12-24 00:24:44 +00003031 if (__n_ >= static_cast<int>(__r))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032 {
3033 __e_.discard(__p - __r);
3034 __n_ = 0;
3035 }
3036 ++__n_;
3037 return __e_();
3038}
3039
Howard Hinnantc834c512011-11-29 18:15:50 +00003040template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003041inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042bool
Howard Hinnantc834c512011-11-29 18:15:50 +00003043operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
3044 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045{
3046 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
3047}
3048
Howard Hinnantc834c512011-11-29 18:15:50 +00003049template<class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003050inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051bool
Howard Hinnantc834c512011-11-29 18:15:50 +00003052operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
3053 const discard_block_engine<_Eng, _Pp, _Rp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054{
3055 return !(__x == __y);
3056}
3057
3058template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003059 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003060basic_ostream<_CharT, _Traits>&
3061operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003062 const discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003064 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003065 typedef basic_ostream<_CharT, _Traits> _Ostream;
3066 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067 _CharT __sp = __os.widen(' ');
3068 __os.fill(__sp);
3069 return __os << __x.__e_ << __sp << __x.__n_;
3070}
3071
3072template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003073 class _Eng, size_t _Pp, size_t _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074basic_istream<_CharT, _Traits>&
3075operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003076 discard_block_engine<_Eng, _Pp, _Rp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003078 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003079 typedef basic_istream<_CharT, _Traits> _Istream;
3080 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081 _Eng __e;
3082 int __n;
3083 __is >> __e >> __n;
3084 if (!__is.fail())
3085 {
3086 __x.__e_ = __e;
3087 __x.__n_ = __n;
3088 }
3089 return __is;
3090}
3091
3092typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
3093typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
3094
3095// independent_bits_engine
3096
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097template<class _Engine, size_t __w, class _UIntType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003098class _LIBCPP_TEMPLATE_VIS independent_bits_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099{
Eric Fiselier4638fca2017-05-31 21:20:18 +00003100 template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101 class __get_n
3102 {
Eric Fiselier4638fca2017-05-31 21:20:18 +00003103 static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits;
Howard Hinnant5a646852012-04-02 21:00:45 +00003104 static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
3105 static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
Eric Fiselier4638fca2017-05-31 21:20:18 +00003106 static _LIBCPP_CONSTEXPR const _UInt _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107 public:
Howard Hinnant5a646852012-04-02 21:00:45 +00003108 static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109 };
3110public:
3111 // types
3112 typedef _UIntType result_type;
3113
3114private:
3115 _Engine __e_;
3116
Howard Hinnant5a646852012-04-02 21:00:45 +00003117 static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
3119 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
3120
3121 typedef typename _Engine::result_type _Engine_result_type;
3122 typedef typename conditional
3123 <
3124 sizeof(_Engine_result_type) <= sizeof(result_type),
3125 result_type,
3126 _Engine_result_type
3127 >::type _Working_result_type;
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003128#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +00003129 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant5a646852012-04-02 21:00:45 +00003130 + _Working_result_type(1);
3131#else
3132 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
3133 + _Working_result_type(1);
3134#endif
3135 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
3136 static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
3137 static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
3138 static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
3139 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
3140 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
3141 static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
3142 (_Rp >> __w0) << __w0;
3143 static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
3144 (_Rp >> (__w0+1)) << (__w0+1);
3145 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146 _Engine_result_type(~0) >> (_EDt - __w0) :
3147 _Engine_result_type(0);
Howard Hinnant5a646852012-04-02 21:00:45 +00003148 static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
3150 _Engine_result_type(~0);
3151public:
Howard Hinnant5a646852012-04-02 21:00:45 +00003152 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3153 static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) :
3154 (result_type(1) << __w) - result_type(1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
3156
3157 // engine characteristics
Howard Hinnantf5f99992010-09-22 18:02:38 +00003158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003159 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00003160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003161 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003162
3163 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 independent_bits_engine() {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003167 explicit independent_bits_engine(const _Engine& __e)
3168 : __e_(__e) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003169#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003171 explicit independent_bits_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003172 : __e_(_VSTD::move(__e)) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003173#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
Howard Hinnant28b24882011-12-01 20:21:04 +00003176 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00003178 explicit independent_bits_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00003179 typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003180 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003181 : __e_(__q) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183 void seed() {__e_.seed();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003185 void seed(result_type __sd) {__e_.seed(__sd);}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003186 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003188 typename enable_if
3189 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00003190 __is_seed_sequence<_Sseq, independent_bits_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003191 void
3192 >::type
3193 seed(_Sseq& __q) {__e_.seed(__q);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194
3195 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003197 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003199 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3200
3201 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00003203 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204
Eric Fiselier4638fca2017-05-31 21:20:18 +00003205 template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206 friend
3207 bool
3208 operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003209 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3210 const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003211
Eric Fiselier4638fca2017-05-31 21:20:18 +00003212 template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213 friend
3214 bool
3215 operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003216 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3217 const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003218
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003220 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221 friend
3222 basic_ostream<_CharT, _Traits>&
3223 operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003224 const independent_bits_engine<_Eng, _Wp, _UInt>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003225
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226 template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003227 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003228 friend
3229 basic_istream<_CharT, _Traits>&
3230 operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003231 independent_bits_engine<_Eng, _Wp, _UInt>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232
3233private:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003234 _LIBCPP_INLINE_VISIBILITY
Marshall Clowfe778582017-09-20 19:38:43 +00003235 result_type __eval(false_type);
3236 result_type __eval(true_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237
3238 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240 static
3241 typename enable_if
3242 <
3243 __count < _Dt,
3244 result_type
3245 >::type
3246 __lshift(result_type __x) {return __x << __count;}
3247
3248 template <size_t __count>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003250 static
3251 typename enable_if
3252 <
3253 (__count >= _Dt),
3254 result_type
3255 >::type
Howard Hinnant28b24882011-12-01 20:21:04 +00003256 __lshift(result_type) {return result_type(0);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257};
3258
3259template<class _Engine, size_t __w, class _UIntType>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003260inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00003262independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263{
3264 return static_cast<result_type>(__e_() & __mask0);
3265}
3266
3267template<class _Engine, size_t __w, class _UIntType>
3268_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00003269independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270{
Howard Hinnantc834c512011-11-29 18:15:50 +00003271 result_type _Sp = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003272 for (size_t __k = 0; __k < __n0; ++__k)
3273 {
3274 _Engine_result_type __u;
3275 do
3276 {
3277 __u = __e_() - _Engine::min();
3278 } while (__u >= __y0);
Howard Hinnantc834c512011-11-29 18:15:50 +00003279 _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280 }
3281 for (size_t __k = __n0; __k < __n; ++__k)
3282 {
3283 _Engine_result_type __u;
3284 do
3285 {
3286 __u = __e_() - _Engine::min();
3287 } while (__u >= __y1);
Howard Hinnantc834c512011-11-29 18:15:50 +00003288 _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003289 }
Howard Hinnantc834c512011-11-29 18:15:50 +00003290 return _Sp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291}
3292
Eric Fiselier4638fca2017-05-31 21:20:18 +00003293template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003294inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003295bool
3296operator==(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003297 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3298 const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003299{
3300 return __x.base() == __y.base();
3301}
3302
Eric Fiselier4638fca2017-05-31 21:20:18 +00003303template<class _Eng, size_t _Wp, class _UInt>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003304inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305bool
3306operator!=(
Eric Fiselier4638fca2017-05-31 21:20:18 +00003307 const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
3308 const independent_bits_engine<_Eng, _Wp, _UInt>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309{
3310 return !(__x == __y);
3311}
3312
3313template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003314 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003315basic_ostream<_CharT, _Traits>&
3316operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003317 const independent_bits_engine<_Eng, _Wp, _UInt>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318{
3319 return __os << __x.base();
3320}
3321
3322template <class _CharT, class _Traits,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003323 class _Eng, size_t _Wp, class _UInt>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324basic_istream<_CharT, _Traits>&
3325operator>>(basic_istream<_CharT, _Traits>& __is,
Eric Fiselier4638fca2017-05-31 21:20:18 +00003326 independent_bits_engine<_Eng, _Wp, _UInt>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327{
3328 _Eng __e;
3329 __is >> __e;
3330 if (!__is.fail())
3331 __x.__e_ = __e;
3332 return __is;
3333}
3334
3335// shuffle_order_engine
3336
3337template <uint64_t _Xp, uint64_t _Yp>
3338struct __ugcd
3339{
Howard Hinnant5a646852012-04-02 21:00:45 +00003340 static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341};
3342
3343template <uint64_t _Xp>
3344struct __ugcd<_Xp, 0>
3345{
Howard Hinnant5a646852012-04-02 21:00:45 +00003346 static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347};
3348
Howard Hinnantc834c512011-11-29 18:15:50 +00003349template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350class __uratio
3351{
Howard Hinnantc834c512011-11-29 18:15:50 +00003352 static_assert(_Dp != 0, "__uratio divide by 0");
Howard Hinnant5a646852012-04-02 21:00:45 +00003353 static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354public:
Howard Hinnant5a646852012-04-02 21:00:45 +00003355 static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
3356 static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003357
3358 typedef __uratio<num, den> type;
3359};
3360
3361template<class _Engine, size_t __k>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003362class _LIBCPP_TEMPLATE_VIS shuffle_order_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363{
3364 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3365public:
3366 // types
3367 typedef typename _Engine::result_type result_type;
3368
3369private:
3370 _Engine __e_;
3371 result_type _V_[__k];
3372 result_type _Y_;
3373
3374public:
3375 // engine characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00003376 static _LIBCPP_CONSTEXPR const size_t table_size = __k;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003377
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003378#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003379 static const result_type _Min = _Engine::_Min;
3380 static const result_type _Max = _Engine::_Max;
Howard Hinnant5a646852012-04-02 21:00:45 +00003381#else
3382 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
3383 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
3384#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
Howard Hinnantf5f99992010-09-22 18:02:38 +00003386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003387 static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
Howard Hinnantf5f99992010-09-22 18:02:38 +00003388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5a646852012-04-02 21:00:45 +00003389 static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390
Howard Hinnant5a646852012-04-02 21:00:45 +00003391 static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392
3393 // constructors and seeding functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395 shuffle_order_engine() {__init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003397 explicit shuffle_order_engine(const _Engine& __e)
3398 : __e_(__e) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003399#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003401 explicit shuffle_order_engine(_Engine&& __e)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003402 : __e_(_VSTD::move(__e)) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003403#endif // _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003405 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
Howard Hinnant28b24882011-12-01 20:21:04 +00003406 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00003408 explicit shuffle_order_engine(_Sseq& __q,
Howard Hinnantc8c73992011-04-11 18:22:12 +00003409 typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003410 !is_convertible<_Sseq, _Engine>::value>::type* = 0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411 : __e_(__q) {__init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413 void seed() {__e_.seed(); __init();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415 void seed(result_type __sd) {__e_.seed(__sd); __init();}
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003416 template<class _Sseq>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003418 typename enable_if
3419 <
Howard Hinnantc8c73992011-04-11 18:22:12 +00003420 __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
Howard Hinnant6047d5b2010-05-28 15:49:54 +00003421 void
3422 >::type
3423 seed(_Sseq& __q) {__e_.seed(__q); __init();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003424
3425 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003427 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003429 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3430
3431 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6f926b12012-07-20 21:44:27 +00003433 const _Engine& base() const _NOEXCEPT {return __e_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434
3435private:
Howard Hinnantc834c512011-11-29 18:15:50 +00003436 template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437 friend
3438 bool
3439 operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00003440 const shuffle_order_engine<_Eng, _Kp>& __x,
3441 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003442
Howard Hinnantc834c512011-11-29 18:15:50 +00003443 template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444 friend
3445 bool
3446 operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00003447 const shuffle_order_engine<_Eng, _Kp>& __x,
3448 const shuffle_order_engine<_Eng, _Kp>& __y);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003449
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003451 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452 friend
3453 basic_ostream<_CharT, _Traits>&
3454 operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003455 const shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003456
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457 template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003458 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003459 friend
3460 basic_istream<_CharT, _Traits>&
3461 operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003462 shuffle_order_engine<_Eng, _Kp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463
Howard Hinnantf5f99992010-09-22 18:02:38 +00003464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465 void __init()
3466 {
3467 for (size_t __i = 0; __i < __k; ++__i)
3468 _V_[__i] = __e_();
3469 _Y_ = __e_();
3470 }
3471
Howard Hinnantf5f99992010-09-22 18:02:38 +00003472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003475 result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476
Howard Hinnantf5f99992010-09-22 18:02:38 +00003477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003478 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003480 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3481
Howard Hinnantc834c512011-11-29 18:15:50 +00003482 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003484 typename enable_if
3485 <
Howard Hinnantc834c512011-11-29 18:15:50 +00003486 (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487 result_type
3488 >::type
Howard Hinnantc834c512011-11-29 18:15:50 +00003489 __eval(__uratio<_Np, _Dp>)
3490 {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003491
Howard Hinnantc834c512011-11-29 18:15:50 +00003492 template <uint64_t _Np, uint64_t _Dp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494 typename enable_if
3495 <
Howard Hinnantc834c512011-11-29 18:15:50 +00003496 __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497 result_type
3498 >::type
Howard Hinnantc834c512011-11-29 18:15:50 +00003499 __eval(__uratio<_Np, _Dp>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003501 const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
3502 / __uratio<_Np, _Dp>::den);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503 _Y_ = _V_[__j];
3504 _V_[__j] = __e_();
3505 return _Y_;
3506 }
3507
3508 template <uint64_t __n, uint64_t __d>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510 result_type __evalf()
3511 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003512 const double _Fp = __d == 0 ?
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513 __n / (2. * 0x8000000000000000ull) :
3514 __n / (double)__d;
Howard Hinnantc834c512011-11-29 18:15:50 +00003515 const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516 _Y_ = _V_[__j];
3517 _V_[__j] = __e_();
3518 return _Y_;
3519 }
3520};
3521
Howard Hinnant2c45cb42012-12-12 21:14:28 +00003522template<class _Engine, size_t __k>
3523 _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size;
3524
Howard Hinnantc834c512011-11-29 18:15:50 +00003525template<class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526bool
3527operator==(
Howard Hinnantc834c512011-11-29 18:15:50 +00003528 const shuffle_order_engine<_Eng, _Kp>& __x,
3529 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530{
Howard Hinnantc834c512011-11-29 18:15:50 +00003531 return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532 __x.__e_ == __y.__e_;
3533}
3534
Howard Hinnantc834c512011-11-29 18:15:50 +00003535template<class _Eng, size_t _Kp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003536inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537bool
3538operator!=(
Howard Hinnantc834c512011-11-29 18:15:50 +00003539 const shuffle_order_engine<_Eng, _Kp>& __x,
3540 const shuffle_order_engine<_Eng, _Kp>& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541{
3542 return !(__x == __y);
3543}
3544
3545template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003546 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547basic_ostream<_CharT, _Traits>&
3548operator<<(basic_ostream<_CharT, _Traits>& __os,
Howard Hinnantc834c512011-11-29 18:15:50 +00003549 const shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003551 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003552 typedef basic_ostream<_CharT, _Traits> _Ostream;
3553 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554 _CharT __sp = __os.widen(' ');
3555 __os.fill(__sp);
3556 __os << __x.__e_ << __sp << __x._V_[0];
Howard Hinnantc834c512011-11-29 18:15:50 +00003557 for (size_t __i = 1; __i < _Kp; ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558 __os << __sp << __x._V_[__i];
3559 return __os << __sp << __x._Y_;
3560}
3561
3562template <class _CharT, class _Traits,
Howard Hinnantc834c512011-11-29 18:15:50 +00003563 class _Eng, size_t _Kp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564basic_istream<_CharT, _Traits>&
3565operator>>(basic_istream<_CharT, _Traits>& __is,
Howard Hinnantc834c512011-11-29 18:15:50 +00003566 shuffle_order_engine<_Eng, _Kp>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567{
Howard Hinnantc834c512011-11-29 18:15:50 +00003568 typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003569 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003570 typedef basic_istream<_CharT, _Traits> _Istream;
3571 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572 _Eng __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003573 result_type _Vp[_Kp+1];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574 __is >> __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003575 for (size_t __i = 0; __i < _Kp+1; ++__i)
3576 __is >> _Vp[__i];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577 if (!__is.fail())
3578 {
3579 __x.__e_ = __e;
Howard Hinnantc834c512011-11-29 18:15:50 +00003580 for (size_t __i = 0; __i < _Kp; ++__i)
3581 __x._V_[__i] = _Vp[__i];
3582 __x._Y_ = _Vp[_Kp];
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583 }
3584 return __is;
3585}
3586
3587typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3588
3589// random_device
3590
Louis Dionne3777e682020-10-15 10:32:09 -04003591#if !defined(_LIBCPP_HAS_NO_RANDOM_DEVICE)
3592
Howard Hinnant8331b762013-03-06 23:30:19 +00003593class _LIBCPP_TYPE_VIS random_device
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594{
Ed Schoutendcf37402015-03-10 07:46:06 +00003595#ifdef _LIBCPP_USING_DEV_RANDOM
Howard Hinnantc51e1022010-05-11 19:42:16 +00003596 int __f_;
Ed Schoutendcf37402015-03-10 07:46:06 +00003597#endif // defined(_LIBCPP_USING_DEV_RANDOM)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003598public:
3599 // types
3600 typedef unsigned result_type;
3601
3602 // generator characteristics
Howard Hinnant5a646852012-04-02 21:00:45 +00003603 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
3604 static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605
Howard Hinnantf5f99992010-09-22 18:02:38 +00003606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant664183b2012-04-02 00:40:41 +00003607 static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant664183b2012-04-02 00:40:41 +00003609 static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610
3611 // constructors
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003612#ifndef _LIBCPP_CXX03_LANG
3613 random_device() : random_device("/dev/urandom") {}
3614 explicit random_device(const string& __token);
3615#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616 explicit random_device(const string& __token = "/dev/urandom");
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003617#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003618 ~random_device();
3619
3620 // generating functions
3621 result_type operator()();
3622
3623 // property functions
Howard Hinnant6f926b12012-07-20 21:44:27 +00003624 double entropy() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625
3626private:
3627 // no copy functions
3628 random_device(const random_device&); // = delete;
3629 random_device& operator=(const random_device&); // = delete;
3630};
3631
Louis Dionne3777e682020-10-15 10:32:09 -04003632#endif // !_LIBCPP_HAS_NO_RANDOM_DEVICE
3633
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634// seed_seq
3635
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003636class _LIBCPP_TEMPLATE_VIS seed_seq
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637{
3638public:
3639 // types
3640 typedef uint32_t result_type;
3641
3642private:
3643 vector<result_type> __v_;
3644
3645 template<class _InputIterator>
3646 void init(_InputIterator __first, _InputIterator __last);
3647public:
3648 // constructors
Howard Hinnantf5f99992010-09-22 18:02:38 +00003649 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4b6b8092013-10-23 05:56:47 +00003650 seed_seq() _NOEXCEPT {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003651#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652 template<class _Tp>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003655#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003656
Howard Hinnantc51e1022010-05-11 19:42:16 +00003657 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659 seed_seq(_InputIterator __first, _InputIterator __last)
3660 {init(__first, __last);}
3661
3662 // generating functions
3663 template<class _RandomAccessIterator>
3664 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3665
3666 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003667 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4b6b8092013-10-23 05:56:47 +00003668 size_t size() const _NOEXCEPT {return __v_.size();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003669 template<class _OutputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00003670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671 void param(_OutputIterator __dest) const
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003672 {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673
3674private:
3675 // no copy functions
3676 seed_seq(const seed_seq&); // = delete;
3677 void operator=(const seed_seq&); // = delete;
3678
Howard Hinnantf5f99992010-09-22 18:02:38 +00003679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00003680 static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681};
3682
3683template<class _InputIterator>
3684void
3685seed_seq::init(_InputIterator __first, _InputIterator __last)
3686{
3687 for (_InputIterator __s = __first; __s != __last; ++__s)
3688 __v_.push_back(*__s & 0xFFFFFFFF);
3689}
3690
3691template<class _RandomAccessIterator>
3692void
3693seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3694{
3695 if (__first != __last)
3696 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003697 _VSTD::fill(__first, __last, 0x8b8b8b8b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003698 const size_t __n = static_cast<size_t>(__last - __first);
3699 const size_t __s = __v_.size();
3700 const size_t __t = (__n >= 623) ? 11
3701 : (__n >= 68) ? 7
3702 : (__n >= 39) ? 5
3703 : (__n >= 7) ? 3
3704 : (__n - 1) / 2;
3705 const size_t __p = (__n - __t) / 2;
3706 const size_t __q = __p + __t;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003707 const size_t __m = _VSTD::max(__s + 1, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708 // __k = 0;
3709 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003710 result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711 ^ __first[__n - 1]);
3712 __first[__p] += __r;
3713 __r += __s;
3714 __first[__q] += __r;
3715 __first[0] = __r;
3716 }
3717 for (size_t __k = 1; __k <= __s; ++__k)
3718 {
3719 const size_t __kmodn = __k % __n;
3720 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003721 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722 ^ __first[(__k - 1) % __n]);
3723 __first[__kpmodn] += __r;
3724 __r += __kmodn + __v_[__k-1];
3725 __first[(__k + __q) % __n] += __r;
3726 __first[__kmodn] = __r;
3727 }
3728 for (size_t __k = __s + 1; __k < __m; ++__k)
3729 {
3730 const size_t __kmodn = __k % __n;
3731 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003732 result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733 ^ __first[(__k - 1) % __n]);
3734 __first[__kpmodn] += __r;
3735 __r += __kmodn;
3736 __first[(__k + __q) % __n] += __r;
3737 __first[__kmodn] = __r;
3738 }
3739 for (size_t __k = __m; __k < __m + __n; ++__k)
3740 {
3741 const size_t __kmodn = __k % __n;
3742 const size_t __kpmodn = (__k + __p) % __n;
Howard Hinnantc834c512011-11-29 18:15:50 +00003743 result_type __r = 1566083941 * _Tp(__first[__kmodn] +
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744 __first[__kpmodn] +
3745 __first[(__k - 1) % __n]);
3746 __first[__kpmodn] ^= __r;
3747 __r -= __kmodn;
3748 __first[(__k + __q) % __n] ^= __r;
3749 __first[__kmodn] = __r;
3750 }
3751 }
3752}
3753
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003754// generate_canonical
3755
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756template<class _RealType, size_t __bits, class _URNG>
3757_RealType
3758generate_canonical(_URNG& __g)
3759{
3760 const size_t _Dt = numeric_limits<_RealType>::digits;
3761 const size_t __b = _Dt < __bits ? _Dt : __bits;
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00003762#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
Howard Hinnant5a646852012-04-02 21:00:45 +00003764#else
3765 const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
3766#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
Louis Dionne2422bdb2019-08-20 15:39:20 +00003768 const _RealType _Rp = static_cast<_RealType>(_URNG::max() - _URNG::min()) + _RealType(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00003769 _RealType __base = _Rp;
Howard Hinnant5a646852012-04-02 21:00:45 +00003770 _RealType _Sp = __g() - _URNG::min();
Howard Hinnantc834c512011-11-29 18:15:50 +00003771 for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp)
Howard Hinnant5a646852012-04-02 21:00:45 +00003772 _Sp += (__g() - _URNG::min()) * __base;
Howard Hinnantc834c512011-11-29 18:15:50 +00003773 return _Sp / __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003774}
3775
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003776// uniform_int_distribution
3777
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003778// in <algorithm>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779
3780template <class _CharT, class _Traits, class _IT>
3781basic_ostream<_CharT, _Traits>&
3782operator<<(basic_ostream<_CharT, _Traits>& __os,
3783 const uniform_int_distribution<_IT>& __x)
3784{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003785 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003786 typedef basic_ostream<_CharT, _Traits> _Ostream;
3787 __os.flags(_Ostream::dec | _Ostream::left);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788 _CharT __sp = __os.widen(' ');
3789 __os.fill(__sp);
3790 return __os << __x.a() << __sp << __x.b();
3791}
3792
3793template <class _CharT, class _Traits, class _IT>
3794basic_istream<_CharT, _Traits>&
3795operator>>(basic_istream<_CharT, _Traits>& __is,
3796 uniform_int_distribution<_IT>& __x)
3797{
3798 typedef uniform_int_distribution<_IT> _Eng;
3799 typedef typename _Eng::result_type result_type;
3800 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003801 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003802 typedef basic_istream<_CharT, _Traits> _Istream;
3803 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003804 result_type __a;
3805 result_type __b;
3806 __is >> __a >> __b;
3807 if (!__is.fail())
3808 __x.param(param_type(__a, __b));
3809 return __is;
3810}
3811
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003812// uniform_real_distribution
3813
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003815class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816{
3817public:
3818 // types
3819 typedef _RealType result_type;
3820
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003821 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822 {
3823 result_type __a_;
3824 result_type __b_;
3825 public:
3826 typedef uniform_real_distribution distribution_type;
3827
Howard Hinnantf5f99992010-09-22 18:02:38 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003829 explicit param_type(result_type __a = 0,
3830 result_type __b = 1)
3831 : __a_(__a), __b_(__b) {}
3832
Howard Hinnantf5f99992010-09-22 18:02:38 +00003833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003835 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836 result_type b() const {return __b_;}
3837
Howard Hinnantf5f99992010-09-22 18:02:38 +00003838 friend _LIBCPP_INLINE_VISIBILITY
3839 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003841 friend _LIBCPP_INLINE_VISIBILITY
3842 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843 {return !(__x == __y);}
3844 };
3845
3846private:
3847 param_type __p_;
3848
3849public:
3850 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003851#ifndef _LIBCPP_CXX03_LANG
3852 _LIBCPP_INLINE_VISIBILITY
3853 uniform_real_distribution() : uniform_real_distribution(0) {}
3854 explicit uniform_real_distribution(result_type __a, result_type __b = 1)
3855 : __p_(param_type(__a, __b)) {}
3856#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00003857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3859 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003860#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00003861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003862 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003864 void reset() {}
3865
3866 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003867 template<class _URNG>
3868 _LIBCPP_INLINE_VISIBILITY
3869 result_type operator()(_URNG& __g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003871 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872
3873 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877 result_type b() const {return __p_.b();}
3878
Howard Hinnantf5f99992010-09-22 18:02:38 +00003879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003880 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882 void param(const param_type& __p) {__p_ = __p;}
3883
Howard Hinnantf5f99992010-09-22 18:02:38 +00003884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003885 result_type min() const {return a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003887 result_type max() const {return b();}
3888
Howard Hinnantf5f99992010-09-22 18:02:38 +00003889 friend _LIBCPP_INLINE_VISIBILITY
3890 bool operator==(const uniform_real_distribution& __x,
3891 const uniform_real_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003892 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003893 friend _LIBCPP_INLINE_VISIBILITY
3894 bool operator!=(const uniform_real_distribution& __x,
3895 const uniform_real_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003896 {return !(__x == __y);}
3897};
3898
3899template<class _RealType>
3900template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003901inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003902typename uniform_real_distribution<_RealType>::result_type
3903uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3904{
3905 return (__p.b() - __p.a())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003906 * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003907 + __p.a();
3908}
3909
3910template <class _CharT, class _Traits, class _RT>
3911basic_ostream<_CharT, _Traits>&
3912operator<<(basic_ostream<_CharT, _Traits>& __os,
3913 const uniform_real_distribution<_RT>& __x)
3914{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003915 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003916 typedef basic_ostream<_CharT, _Traits> _OStream;
3917 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
3918 _OStream::scientific);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003919 _CharT __sp = __os.widen(' ');
3920 __os.fill(__sp);
3921 return __os << __x.a() << __sp << __x.b();
3922}
3923
3924template <class _CharT, class _Traits, class _RT>
3925basic_istream<_CharT, _Traits>&
3926operator>>(basic_istream<_CharT, _Traits>& __is,
3927 uniform_real_distribution<_RT>& __x)
3928{
3929 typedef uniform_real_distribution<_RT> _Eng;
3930 typedef typename _Eng::result_type result_type;
3931 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00003932 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04003933 typedef basic_istream<_CharT, _Traits> _Istream;
3934 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935 result_type __a;
3936 result_type __b;
3937 __is >> __a >> __b;
3938 if (!__is.fail())
3939 __x.param(param_type(__a, __b));
3940 return __is;
3941}
3942
Howard Hinnantd31dfb52010-05-12 17:08:57 +00003943// bernoulli_distribution
3944
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003945class _LIBCPP_TEMPLATE_VIS bernoulli_distribution
Howard Hinnantc51e1022010-05-11 19:42:16 +00003946{
3947public:
3948 // types
3949 typedef bool result_type;
3950
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003951 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003952 {
3953 double __p_;
3954 public:
3955 typedef bernoulli_distribution distribution_type;
3956
Howard Hinnantf5f99992010-09-22 18:02:38 +00003957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958 explicit param_type(double __p = 0.5) : __p_(__p) {}
3959
Howard Hinnantf5f99992010-09-22 18:02:38 +00003960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961 double p() const {return __p_;}
3962
Howard Hinnantf5f99992010-09-22 18:02:38 +00003963 friend _LIBCPP_INLINE_VISIBILITY
3964 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003966 friend _LIBCPP_INLINE_VISIBILITY
3967 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003968 {return !(__x == __y);}
3969 };
3970
3971private:
3972 param_type __p_;
3973
3974public:
3975 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003976#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00003977 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01003978 bernoulli_distribution() : bernoulli_distribution(0.5) {}
3979 _LIBCPP_INLINE_VISIBILITY
3980 explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {}
3981#else
3982 _LIBCPP_INLINE_VISIBILITY
3983 explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}
3984#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00003985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00003987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003988 void reset() {}
3989
3990 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003991 template<class _URNG>
3992 _LIBCPP_INLINE_VISIBILITY
3993 result_type operator()(_URNG& __g)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003994 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003995 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996
3997 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00003998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999 double p() const {return __p_.p();}
4000
Howard Hinnantf5f99992010-09-22 18:02:38 +00004001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004 void param(const param_type& __p) {__p_ = __p;}
4005
Howard Hinnantf5f99992010-09-22 18:02:38 +00004006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007 result_type min() const {return false;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009 result_type max() const {return true;}
4010
Howard Hinnantf5f99992010-09-22 18:02:38 +00004011 friend _LIBCPP_INLINE_VISIBILITY
4012 bool operator==(const bernoulli_distribution& __x,
4013 const bernoulli_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004015 friend _LIBCPP_INLINE_VISIBILITY
4016 bool operator!=(const bernoulli_distribution& __x,
4017 const bernoulli_distribution& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004018 {return !(__x == __y);}
4019};
4020
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004021template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004022inline
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004023bernoulli_distribution::result_type
4024bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
4025{
Howard Hinnant481ba382010-05-20 15:11:46 +00004026 uniform_real_distribution<double> __gen;
4027 return __gen(__g) < __p.p();
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004028}
4029
Howard Hinnantc51e1022010-05-11 19:42:16 +00004030template <class _CharT, class _Traits>
4031basic_ostream<_CharT, _Traits>&
4032operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
4033{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004034 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004035 typedef basic_ostream<_CharT, _Traits> _OStream;
4036 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4037 _OStream::scientific);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038 _CharT __sp = __os.widen(' ');
4039 __os.fill(__sp);
4040 return __os << __x.p();
4041}
4042
4043template <class _CharT, class _Traits>
4044basic_istream<_CharT, _Traits>&
4045operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
4046{
4047 typedef bernoulli_distribution _Eng;
4048 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004049 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004050 typedef basic_istream<_CharT, _Traits> _Istream;
4051 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004052 double __p;
4053 __is >> __p;
4054 if (!__is.fail())
4055 __x.param(param_type(__p));
4056 return __is;
4057}
4058
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004059// binomial_distribution
4060
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004061template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004062class _LIBCPP_TEMPLATE_VIS binomial_distribution
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004063{
4064public:
4065 // types
4066 typedef _IntType result_type;
4067
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004068 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004069 {
4070 result_type __t_;
4071 double __p_;
Howard Hinnant76310352010-05-15 21:36:23 +00004072 double __pr_;
4073 double __odds_ratio_;
4074 result_type __r0_;
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004075 public:
4076 typedef binomial_distribution distribution_type;
4077
Howard Hinnant76310352010-05-15 21:36:23 +00004078 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004079
Howard Hinnantf5f99992010-09-22 18:02:38 +00004080 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004081 result_type t() const {return __t_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004083 double p() const {return __p_;}
4084
Howard Hinnantf5f99992010-09-22 18:02:38 +00004085 friend _LIBCPP_INLINE_VISIBILITY
4086 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004087 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004088 friend _LIBCPP_INLINE_VISIBILITY
4089 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004090 {return !(__x == __y);}
Howard Hinnant76310352010-05-15 21:36:23 +00004091
4092 friend class binomial_distribution;
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004093 };
4094
4095private:
4096 param_type __p_;
4097
4098public:
4099 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004100#ifndef _LIBCPP_CXX03_LANG
4101 _LIBCPP_INLINE_VISIBILITY
4102 binomial_distribution() : binomial_distribution(1) {}
4103 _LIBCPP_INLINE_VISIBILITY
4104 explicit binomial_distribution(result_type __t, double __p = 0.5)
4105 : __p_(param_type(__t, __p)) {}
4106#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00004107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004108 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
4109 : __p_(param_type(__t, __p)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004110#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004112 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004114 void reset() {}
4115
4116 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004117 template<class _URNG>
4118 _LIBCPP_INLINE_VISIBILITY
4119 result_type operator()(_URNG& __g)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004120 {return (*this)(__g, __p_);}
4121 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4122
4123 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004125 result_type t() const {return __p_.t();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004127 double p() const {return __p_.p();}
4128
Howard Hinnantf5f99992010-09-22 18:02:38 +00004129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004130 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004132 void param(const param_type& __p) {__p_ = __p;}
4133
Howard Hinnantf5f99992010-09-22 18:02:38 +00004134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004135 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004137 result_type max() const {return t();}
4138
Howard Hinnantf5f99992010-09-22 18:02:38 +00004139 friend _LIBCPP_INLINE_VISIBILITY
4140 bool operator==(const binomial_distribution& __x,
4141 const binomial_distribution& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004142 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004143 friend _LIBCPP_INLINE_VISIBILITY
4144 bool operator!=(const binomial_distribution& __x,
4145 const binomial_distribution& __y)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004146 {return !(__x == __y);}
4147};
4148
Martin Storsjö408cee72020-12-01 13:37:16 +02004149#ifndef _LIBCPP_MSVCRT_LIKE
Marshall Clow7db28572017-05-04 16:36:39 +00004150extern "C" double lgamma_r(double, int *);
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004151#endif
4152
4153inline _LIBCPP_INLINE_VISIBILITY double __libcpp_lgamma(double __d) {
Martin Storsjö408cee72020-12-01 13:37:16 +02004154#if defined(_LIBCPP_MSVCRT_LIKE)
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004155 return lgamma(__d);
4156#else
4157 int __sign;
4158 return lgamma_r(__d, &__sign);
4159#endif
4160}
Marshall Clow7db28572017-05-04 16:36:39 +00004161
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004162template<class _IntType>
Marshall Clow7db28572017-05-04 16:36:39 +00004163binomial_distribution<_IntType>::param_type::param_type(const result_type __t, const double __p)
Howard Hinnant76310352010-05-15 21:36:23 +00004164 : __t_(__t), __p_(__p)
4165{
4166 if (0 < __p_ && __p_ < 1)
4167 {
4168 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
Eric Fiselier651ca6e2017-05-06 02:58:43 +00004169 __pr_ = _VSTD::exp(__libcpp_lgamma(__t_ + 1.) -
4170 __libcpp_lgamma(__r0_ + 1.) -
4171 __libcpp_lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
Marshall Clow7db28572017-05-04 16:36:39 +00004172 (__t_ - __r0_) * _VSTD::log(1 - __p_));
Howard Hinnant76310352010-05-15 21:36:23 +00004173 __odds_ratio_ = __p_ / (1 - __p_);
4174 }
4175}
4176
Marshall Clowf97a84f2014-09-17 18:33:58 +00004177// Reference: Kemp, C.D. (1986). `A modal method for generating binomial
4178// variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
Howard Hinnant76310352010-05-15 21:36:23 +00004179template<class _IntType>
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004180template<class _URNG>
4181_IntType
Howard Hinnant76310352010-05-15 21:36:23 +00004182binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004183{
Howard Hinnant76310352010-05-15 21:36:23 +00004184 if (__pr.__t_ == 0 || __pr.__p_ == 0)
4185 return 0;
4186 if (__pr.__p_ == 1)
4187 return __pr.__t_;
4188 uniform_real_distribution<double> __gen;
4189 double __u = __gen(__g) - __pr.__pr_;
4190 if (__u < 0)
4191 return __pr.__r0_;
4192 double __pu = __pr.__pr_;
4193 double __pd = __pu;
4194 result_type __ru = __pr.__r0_;
4195 result_type __rd = __ru;
4196 while (true)
4197 {
Atmn Patelda0b1b62020-03-17 15:52:36 -04004198 bool __break = true;
Howard Hinnant76310352010-05-15 21:36:23 +00004199 if (__rd >= 1)
4200 {
4201 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
4202 __u -= __pd;
Atmn Patelda0b1b62020-03-17 15:52:36 -04004203 __break = false;
Howard Hinnant76310352010-05-15 21:36:23 +00004204 if (__u < 0)
4205 return __rd - 1;
4206 }
Marshall Clowf97a84f2014-09-17 18:33:58 +00004207 if ( __rd != 0 )
4208 --__rd;
Howard Hinnant76310352010-05-15 21:36:23 +00004209 ++__ru;
4210 if (__ru <= __pr.__t_)
4211 {
4212 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
4213 __u -= __pu;
Atmn Patelda0b1b62020-03-17 15:52:36 -04004214 __break = false;
Howard Hinnant76310352010-05-15 21:36:23 +00004215 if (__u < 0)
4216 return __ru;
4217 }
Atmn Patelda0b1b62020-03-17 15:52:36 -04004218 if (__break)
4219 return 0;
Howard Hinnant76310352010-05-15 21:36:23 +00004220 }
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004221}
4222
4223template <class _CharT, class _Traits, class _IntType>
4224basic_ostream<_CharT, _Traits>&
4225operator<<(basic_ostream<_CharT, _Traits>& __os,
4226 const binomial_distribution<_IntType>& __x)
4227{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004228 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004229 typedef basic_ostream<_CharT, _Traits> _OStream;
4230 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4231 _OStream::scientific);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004232 _CharT __sp = __os.widen(' ');
4233 __os.fill(__sp);
4234 return __os << __x.t() << __sp << __x.p();
4235}
4236
4237template <class _CharT, class _Traits, class _IntType>
4238basic_istream<_CharT, _Traits>&
4239operator>>(basic_istream<_CharT, _Traits>& __is,
4240 binomial_distribution<_IntType>& __x)
4241{
4242 typedef binomial_distribution<_IntType> _Eng;
4243 typedef typename _Eng::result_type result_type;
4244 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004245 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004246 typedef basic_istream<_CharT, _Traits> _Istream;
4247 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant53f28fa2010-05-11 23:26:59 +00004248 result_type __t;
4249 double __p;
4250 __is >> __t >> __p;
4251 if (!__is.fail())
4252 __x.param(param_type(__t, __p));
4253 return __is;
4254}
4255
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004256// exponential_distribution
4257
4258template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004259class _LIBCPP_TEMPLATE_VIS exponential_distribution
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004260{
4261public:
4262 // types
4263 typedef _RealType result_type;
4264
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004265 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004266 {
4267 result_type __lambda_;
4268 public:
4269 typedef exponential_distribution distribution_type;
4270
Howard Hinnantf5f99992010-09-22 18:02:38 +00004271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004272 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
4273
Howard Hinnantf5f99992010-09-22 18:02:38 +00004274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004275 result_type lambda() const {return __lambda_;}
4276
Howard Hinnantf5f99992010-09-22 18:02:38 +00004277 friend _LIBCPP_INLINE_VISIBILITY
4278 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004279 {return __x.__lambda_ == __y.__lambda_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004280 friend _LIBCPP_INLINE_VISIBILITY
4281 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004282 {return !(__x == __y);}
4283 };
4284
4285private:
4286 param_type __p_;
4287
4288public:
4289 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004290#ifndef _LIBCPP_CXX03_LANG
4291 _LIBCPP_INLINE_VISIBILITY
4292 exponential_distribution() : exponential_distribution(1) {}
4293 _LIBCPP_INLINE_VISIBILITY
4294 explicit exponential_distribution(result_type __lambda)
4295 : __p_(param_type(__lambda)) {}
4296#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00004297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004298 explicit exponential_distribution(result_type __lambda = 1)
4299 : __p_(param_type(__lambda)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004300#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004302 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004304 void reset() {}
4305
4306 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004307 template<class _URNG>
4308 _LIBCPP_INLINE_VISIBILITY
4309 result_type operator()(_URNG& __g)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004310 {return (*this)(__g, __p_);}
4311 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4312
4313 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004315 result_type lambda() const {return __p_.lambda();}
4316
Howard Hinnantf5f99992010-09-22 18:02:38 +00004317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004318 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004320 void param(const param_type& __p) {__p_ = __p;}
4321
Howard Hinnantf5f99992010-09-22 18:02:38 +00004322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004323 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant021f9902010-05-16 17:56:20 +00004325 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004326
Howard Hinnantf5f99992010-09-22 18:02:38 +00004327 friend _LIBCPP_INLINE_VISIBILITY
4328 bool operator==(const exponential_distribution& __x,
4329 const exponential_distribution& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004330 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004331 friend _LIBCPP_INLINE_VISIBILITY
4332 bool operator!=(const exponential_distribution& __x,
4333 const exponential_distribution& __y)
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004334 {return !(__x == __y);}
4335};
4336
4337template <class _RealType>
4338template<class _URNG>
4339_RealType
4340exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4341{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004342 return -_VSTD::log
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004343 (
4344 result_type(1) -
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004345 _VSTD::generate_canonical<result_type,
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004346 numeric_limits<result_type>::digits>(__g)
4347 )
4348 / __p.lambda();
4349}
4350
4351template <class _CharT, class _Traits, class _RealType>
4352basic_ostream<_CharT, _Traits>&
4353operator<<(basic_ostream<_CharT, _Traits>& __os,
4354 const exponential_distribution<_RealType>& __x)
4355{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004356 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004357 typedef basic_ostream<_CharT, _Traits> _OStream;
4358 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4359 _OStream::scientific);
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004360 return __os << __x.lambda();
4361}
4362
4363template <class _CharT, class _Traits, class _RealType>
4364basic_istream<_CharT, _Traits>&
4365operator>>(basic_istream<_CharT, _Traits>& __is,
4366 exponential_distribution<_RealType>& __x)
4367{
4368 typedef exponential_distribution<_RealType> _Eng;
4369 typedef typename _Eng::result_type result_type;
4370 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004371 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004372 typedef basic_istream<_CharT, _Traits> _Istream;
4373 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantd31dfb52010-05-12 17:08:57 +00004374 result_type __lambda;
4375 __is >> __lambda;
4376 if (!__is.fail())
4377 __x.param(param_type(__lambda));
4378 return __is;
4379}
4380
Howard Hinnant76310352010-05-15 21:36:23 +00004381// normal_distribution
4382
4383template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004384class _LIBCPP_TEMPLATE_VIS normal_distribution
Howard Hinnant76310352010-05-15 21:36:23 +00004385{
4386public:
4387 // types
4388 typedef _RealType result_type;
4389
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004390 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant76310352010-05-15 21:36:23 +00004391 {
4392 result_type __mean_;
4393 result_type __stddev_;
4394 public:
4395 typedef normal_distribution distribution_type;
4396
Howard Hinnantf5f99992010-09-22 18:02:38 +00004397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004398 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4399 : __mean_(__mean), __stddev_(__stddev) {}
4400
Howard Hinnantf5f99992010-09-22 18:02:38 +00004401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004402 result_type mean() const {return __mean_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004404 result_type stddev() const {return __stddev_;}
4405
Howard Hinnantf5f99992010-09-22 18:02:38 +00004406 friend _LIBCPP_INLINE_VISIBILITY
4407 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004408 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004409 friend _LIBCPP_INLINE_VISIBILITY
4410 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004411 {return !(__x == __y);}
4412 };
4413
4414private:
4415 param_type __p_;
4416 result_type _V_;
4417 bool _V_hot_;
4418
4419public:
4420 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004421#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00004422 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004423 normal_distribution() : normal_distribution(0) {}
4424 _LIBCPP_INLINE_VISIBILITY
4425 explicit normal_distribution(result_type __mean, result_type __stddev = 1)
Howard Hinnant76310352010-05-15 21:36:23 +00004426 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004427#else
4428 _LIBCPP_INLINE_VISIBILITY
4429 explicit normal_distribution(result_type __mean = 0,
4430 result_type __stddev = 1)
4431 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
4432#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004434 explicit normal_distribution(const param_type& __p)
4435 : __p_(__p), _V_hot_(false) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004437 void reset() {_V_hot_ = false;}
4438
4439 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004440 template<class _URNG>
4441 _LIBCPP_INLINE_VISIBILITY
4442 result_type operator()(_URNG& __g)
Howard Hinnant76310352010-05-15 21:36:23 +00004443 {return (*this)(__g, __p_);}
4444 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4445
4446 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004448 result_type mean() const {return __p_.mean();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004450 result_type stddev() const {return __p_.stddev();}
4451
Howard Hinnantf5f99992010-09-22 18:02:38 +00004452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004453 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004455 void param(const param_type& __p) {__p_ = __p;}
4456
Howard Hinnantf5f99992010-09-22 18:02:38 +00004457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004458 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004460 result_type max() const {return numeric_limits<result_type>::infinity();}
4461
Howard Hinnantf5f99992010-09-22 18:02:38 +00004462 friend _LIBCPP_INLINE_VISIBILITY
4463 bool operator==(const normal_distribution& __x,
4464 const normal_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004465 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4466 (!__x._V_hot_ || __x._V_ == __y._V_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004467 friend _LIBCPP_INLINE_VISIBILITY
4468 bool operator!=(const normal_distribution& __x,
4469 const normal_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004470 {return !(__x == __y);}
4471
4472 template <class _CharT, class _Traits, class _RT>
4473 friend
4474 basic_ostream<_CharT, _Traits>&
4475 operator<<(basic_ostream<_CharT, _Traits>& __os,
4476 const normal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004477
Howard Hinnant76310352010-05-15 21:36:23 +00004478 template <class _CharT, class _Traits, class _RT>
4479 friend
4480 basic_istream<_CharT, _Traits>&
4481 operator>>(basic_istream<_CharT, _Traits>& __is,
4482 normal_distribution<_RT>& __x);
4483};
4484
4485template <class _RealType>
4486template<class _URNG>
4487_RealType
4488normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4489{
Howard Hinnantc834c512011-11-29 18:15:50 +00004490 result_type _Up;
Howard Hinnant76310352010-05-15 21:36:23 +00004491 if (_V_hot_)
4492 {
4493 _V_hot_ = false;
Howard Hinnantc834c512011-11-29 18:15:50 +00004494 _Up = _V_;
Howard Hinnant76310352010-05-15 21:36:23 +00004495 }
4496 else
4497 {
4498 uniform_real_distribution<result_type> _Uni(-1, 1);
4499 result_type __u;
4500 result_type __v;
4501 result_type __s;
4502 do
4503 {
4504 __u = _Uni(__g);
4505 __v = _Uni(__g);
4506 __s = __u * __u + __v * __v;
4507 } while (__s > 1 || __s == 0);
Howard Hinnantc834c512011-11-29 18:15:50 +00004508 result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
4509 _V_ = __v * _Fp;
Howard Hinnant76310352010-05-15 21:36:23 +00004510 _V_hot_ = true;
Howard Hinnantc834c512011-11-29 18:15:50 +00004511 _Up = __u * _Fp;
Howard Hinnant76310352010-05-15 21:36:23 +00004512 }
Howard Hinnantc834c512011-11-29 18:15:50 +00004513 return _Up * __p.stddev() + __p.mean();
Howard Hinnant76310352010-05-15 21:36:23 +00004514}
4515
4516template <class _CharT, class _Traits, class _RT>
4517basic_ostream<_CharT, _Traits>&
4518operator<<(basic_ostream<_CharT, _Traits>& __os,
4519 const normal_distribution<_RT>& __x)
4520{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004521 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004522 typedef basic_ostream<_CharT, _Traits> _OStream;
4523 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4524 _OStream::scientific);
Howard Hinnant76310352010-05-15 21:36:23 +00004525 _CharT __sp = __os.widen(' ');
4526 __os.fill(__sp);
4527 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4528 if (__x._V_hot_)
4529 __os << __sp << __x._V_;
4530 return __os;
4531}
4532
4533template <class _CharT, class _Traits, class _RT>
4534basic_istream<_CharT, _Traits>&
4535operator>>(basic_istream<_CharT, _Traits>& __is,
4536 normal_distribution<_RT>& __x)
4537{
4538 typedef normal_distribution<_RT> _Eng;
4539 typedef typename _Eng::result_type result_type;
4540 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004541 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004542 typedef basic_istream<_CharT, _Traits> _Istream;
4543 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant76310352010-05-15 21:36:23 +00004544 result_type __mean;
4545 result_type __stddev;
Howard Hinnantc834c512011-11-29 18:15:50 +00004546 result_type _Vp = 0;
Howard Hinnant76310352010-05-15 21:36:23 +00004547 bool _V_hot = false;
4548 __is >> __mean >> __stddev >> _V_hot;
4549 if (_V_hot)
Howard Hinnantc834c512011-11-29 18:15:50 +00004550 __is >> _Vp;
Howard Hinnant76310352010-05-15 21:36:23 +00004551 if (!__is.fail())
4552 {
4553 __x.param(param_type(__mean, __stddev));
4554 __x._V_hot_ = _V_hot;
Howard Hinnantc834c512011-11-29 18:15:50 +00004555 __x._V_ = _Vp;
Howard Hinnant76310352010-05-15 21:36:23 +00004556 }
4557 return __is;
4558}
4559
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004560// lognormal_distribution
4561
4562template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004563class _LIBCPP_TEMPLATE_VIS lognormal_distribution
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004564{
4565public:
4566 // types
4567 typedef _RealType result_type;
4568
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004569 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004570 {
4571 normal_distribution<result_type> __nd_;
4572 public:
4573 typedef lognormal_distribution distribution_type;
4574
Howard Hinnantf5f99992010-09-22 18:02:38 +00004575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004576 explicit param_type(result_type __m = 0, result_type __s = 1)
4577 : __nd_(__m, __s) {}
4578
Howard Hinnantf5f99992010-09-22 18:02:38 +00004579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004580 result_type m() const {return __nd_.mean();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004582 result_type s() const {return __nd_.stddev();}
4583
Howard Hinnantf5f99992010-09-22 18:02:38 +00004584 friend _LIBCPP_INLINE_VISIBILITY
4585 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004586 {return __x.__nd_ == __y.__nd_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004587 friend _LIBCPP_INLINE_VISIBILITY
4588 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004589 {return !(__x == __y);}
4590 friend class lognormal_distribution;
4591
4592 template <class _CharT, class _Traits, class _RT>
4593 friend
4594 basic_ostream<_CharT, _Traits>&
4595 operator<<(basic_ostream<_CharT, _Traits>& __os,
4596 const lognormal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004597
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004598 template <class _CharT, class _Traits, class _RT>
4599 friend
4600 basic_istream<_CharT, _Traits>&
4601 operator>>(basic_istream<_CharT, _Traits>& __is,
4602 lognormal_distribution<_RT>& __x);
4603 };
4604
4605private:
4606 param_type __p_;
4607
4608public:
4609 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004610#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00004611 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004612 lognormal_distribution() : lognormal_distribution(0) {}
4613 _LIBCPP_INLINE_VISIBILITY
4614 explicit lognormal_distribution(result_type __m, result_type __s = 1)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004615 : __p_(param_type(__m, __s)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004616#else
4617 _LIBCPP_INLINE_VISIBILITY
4618 explicit lognormal_distribution(result_type __m = 0,
4619 result_type __s = 1)
4620 : __p_(param_type(__m, __s)) {}
4621#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004623 explicit lognormal_distribution(const param_type& __p)
4624 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004626 void reset() {__p_.__nd_.reset();}
4627
4628 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004629 template<class _URNG>
4630 _LIBCPP_INLINE_VISIBILITY
4631 result_type operator()(_URNG& __g)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004632 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004633 template<class _URNG>
4634 _LIBCPP_INLINE_VISIBILITY
4635 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004636 {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004637
4638 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004640 result_type m() const {return __p_.m();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004642 result_type s() const {return __p_.s();}
4643
Howard Hinnantf5f99992010-09-22 18:02:38 +00004644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004645 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00004647 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004648
Howard Hinnantf5f99992010-09-22 18:02:38 +00004649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004650 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004652 result_type max() const {return numeric_limits<result_type>::infinity();}
4653
Howard Hinnantf5f99992010-09-22 18:02:38 +00004654 friend _LIBCPP_INLINE_VISIBILITY
4655 bool operator==(const lognormal_distribution& __x,
4656 const lognormal_distribution& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004657 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004658 friend _LIBCPP_INLINE_VISIBILITY
4659 bool operator!=(const lognormal_distribution& __x,
4660 const lognormal_distribution& __y)
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004661 {return !(__x == __y);}
4662
4663 template <class _CharT, class _Traits, class _RT>
4664 friend
4665 basic_ostream<_CharT, _Traits>&
4666 operator<<(basic_ostream<_CharT, _Traits>& __os,
4667 const lognormal_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004668
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004669 template <class _CharT, class _Traits, class _RT>
4670 friend
4671 basic_istream<_CharT, _Traits>&
4672 operator>>(basic_istream<_CharT, _Traits>& __is,
4673 lognormal_distribution<_RT>& __x);
4674};
4675
4676template <class _CharT, class _Traits, class _RT>
Howard Hinnantf5f99992010-09-22 18:02:38 +00004677inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004678basic_ostream<_CharT, _Traits>&
4679operator<<(basic_ostream<_CharT, _Traits>& __os,
4680 const lognormal_distribution<_RT>& __x)
4681{
4682 return __os << __x.__p_.__nd_;
4683}
4684
4685template <class _CharT, class _Traits, class _RT>
Howard Hinnantf5f99992010-09-22 18:02:38 +00004686inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdf3bed62010-05-17 18:31:53 +00004687basic_istream<_CharT, _Traits>&
4688operator>>(basic_istream<_CharT, _Traits>& __is,
4689 lognormal_distribution<_RT>& __x)
4690{
4691 return __is >> __x.__p_.__nd_;
4692}
4693
Howard Hinnant76310352010-05-15 21:36:23 +00004694// poisson_distribution
4695
4696template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004697class _LIBCPP_TEMPLATE_VIS poisson_distribution
Howard Hinnant76310352010-05-15 21:36:23 +00004698{
4699public:
4700 // types
4701 typedef _IntType result_type;
4702
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004703 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant76310352010-05-15 21:36:23 +00004704 {
4705 double __mean_;
4706 double __s_;
4707 double __d_;
4708 double __l_;
4709 double __omega_;
4710 double __c0_;
4711 double __c1_;
4712 double __c2_;
4713 double __c3_;
4714 double __c_;
4715
4716 public:
4717 typedef poisson_distribution distribution_type;
4718
4719 explicit param_type(double __mean = 1.0);
4720
Howard Hinnantf5f99992010-09-22 18:02:38 +00004721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004722 double mean() const {return __mean_;}
4723
Howard Hinnantf5f99992010-09-22 18:02:38 +00004724 friend _LIBCPP_INLINE_VISIBILITY
4725 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004726 {return __x.__mean_ == __y.__mean_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004727 friend _LIBCPP_INLINE_VISIBILITY
4728 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004729 {return !(__x == __y);}
4730
4731 friend class poisson_distribution;
4732 };
4733
4734private:
4735 param_type __p_;
4736
4737public:
4738 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004739#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00004740 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004741 poisson_distribution() : poisson_distribution(1.0) {}
4742 _LIBCPP_INLINE_VISIBILITY
4743 explicit poisson_distribution(double __mean)
4744 : __p_(__mean) {}
4745#else
4746 _LIBCPP_INLINE_VISIBILITY
4747 explicit poisson_distribution(double __mean = 1.0)
4748 : __p_(__mean) {}
4749#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004751 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004753 void reset() {}
4754
4755 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004756 template<class _URNG>
4757 _LIBCPP_INLINE_VISIBILITY
4758 result_type operator()(_URNG& __g)
Howard Hinnant76310352010-05-15 21:36:23 +00004759 {return (*this)(__g, __p_);}
4760 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4761
4762 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004764 double mean() const {return __p_.mean();}
4765
Howard Hinnantf5f99992010-09-22 18:02:38 +00004766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004767 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004769 void param(const param_type& __p) {__p_ = __p;}
4770
Howard Hinnantf5f99992010-09-22 18:02:38 +00004771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004772 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant76310352010-05-15 21:36:23 +00004774 result_type max() const {return numeric_limits<result_type>::max();}
4775
Howard Hinnantf5f99992010-09-22 18:02:38 +00004776 friend _LIBCPP_INLINE_VISIBILITY
4777 bool operator==(const poisson_distribution& __x,
4778 const poisson_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004779 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004780 friend _LIBCPP_INLINE_VISIBILITY
4781 bool operator!=(const poisson_distribution& __x,
4782 const poisson_distribution& __y)
Howard Hinnant76310352010-05-15 21:36:23 +00004783 {return !(__x == __y);}
4784};
4785
4786template<class _IntType>
4787poisson_distribution<_IntType>::param_type::param_type(double __mean)
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004788 // According to the standard `inf` is a valid input, but it causes the
4789 // distribution to hang, so we replace it with the maximum representable
4790 // mean.
4791 : __mean_(isinf(__mean) ? numeric_limits<double>::max() : __mean)
Howard Hinnant76310352010-05-15 21:36:23 +00004792{
4793 if (__mean_ < 10)
4794 {
4795 __s_ = 0;
4796 __d_ = 0;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004797 __l_ = _VSTD::exp(-__mean_);
Howard Hinnant76310352010-05-15 21:36:23 +00004798 __omega_ = 0;
4799 __c3_ = 0;
4800 __c2_ = 0;
4801 __c1_ = 0;
4802 __c0_ = 0;
4803 __c_ = 0;
4804 }
4805 else
4806 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004807 __s_ = _VSTD::sqrt(__mean_);
Howard Hinnant76310352010-05-15 21:36:23 +00004808 __d_ = 6 * __mean_ * __mean_;
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004809 __l_ = _VSTD::trunc(__mean_ - 1.1484);
Howard Hinnant76310352010-05-15 21:36:23 +00004810 __omega_ = .3989423 / __s_;
4811 double __b1_ = .4166667E-1 / __mean_;
4812 double __b2_ = .3 * __b1_ * __b1_;
4813 __c3_ = .1428571 * __b1_ * __b2_;
4814 __c2_ = __b2_ - 15. * __c3_;
4815 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4816 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4817 __c_ = .1069 / __mean_;
4818 }
4819}
4820
4821template <class _IntType>
4822template<class _URNG>
4823_IntType
4824poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4825{
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004826 double __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004827 uniform_real_distribution<double> __urd;
Howard Hinnanta5fb7ac2011-04-14 15:59:22 +00004828 if (__pr.__mean_ < 10)
Howard Hinnant76310352010-05-15 21:36:23 +00004829 {
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004830 __tx = 0;
4831 for (double __p = __urd(__urng); __p > __pr.__l_; ++__tx)
Howard Hinnant76310352010-05-15 21:36:23 +00004832 __p *= __urd(__urng);
4833 }
4834 else
4835 {
4836 double __difmuk;
4837 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4838 double __u;
4839 if (__g > 0)
4840 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004841 __tx = _VSTD::trunc(__g);
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004842 if (__tx >= __pr.__l_)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004843 return _VSTD::__clamp_to_integral<result_type>(__tx);
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004844 __difmuk = __pr.__mean_ - __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004845 __u = __urd(__urng);
4846 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004847 return _VSTD::__clamp_to_integral<result_type>(__tx);
Howard Hinnant76310352010-05-15 21:36:23 +00004848 }
4849 exponential_distribution<double> __edist;
4850 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4851 {
4852 double __e;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004853 if (__using_exp_dist || __g <= 0)
Howard Hinnant76310352010-05-15 21:36:23 +00004854 {
4855 double __t;
4856 do
4857 {
4858 __e = __edist(__urng);
4859 __u = __urd(__urng);
4860 __u += __u - 1;
4861 __t = 1.8 + (__u < 0 ? -__e : __e);
4862 } while (__t <= -.6744);
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004863 __tx = _VSTD::trunc(__pr.__mean_ + __pr.__s_ * __t);
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004864 __difmuk = __pr.__mean_ - __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004865 __using_exp_dist = true;
4866 }
4867 double __px;
4868 double __py;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004869 if (__tx < 10 && __tx >= 0)
Howard Hinnant76310352010-05-15 21:36:23 +00004870 {
Marshall Clowc7e36e82018-01-16 14:54:36 +00004871 const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
Howard Hinnant76310352010-05-15 21:36:23 +00004872 40320, 362880};
4873 __px = -__pr.__mean_;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004874 __py = _VSTD::pow(__pr.__mean_, (double)__tx) / __fac[static_cast<int>(__tx)];
Howard Hinnant76310352010-05-15 21:36:23 +00004875 }
4876 else
4877 {
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004878 double __del = .8333333E-1 / __tx;
Howard Hinnant76310352010-05-15 21:36:23 +00004879 __del -= 4.8 * __del * __del * __del;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004880 double __v = __difmuk / __tx;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004881 if (_VSTD::abs(__v) > 0.25)
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004882 __px = __tx * _VSTD::log(1 + __v) - __difmuk - __del;
Howard Hinnant76310352010-05-15 21:36:23 +00004883 else
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004884 __px = __tx * __v * __v * (((((((.1250060 * __v + -.1384794) *
Howard Hinnant76310352010-05-15 21:36:23 +00004885 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4886 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
Louis Dionne7d19d1c2019-11-07 12:06:14 +00004887 __py = .3989423 / _VSTD::sqrt(__tx);
Howard Hinnant76310352010-05-15 21:36:23 +00004888 }
4889 double __r = (0.5 - __difmuk) / __pr.__s_;
4890 double __r2 = __r * __r;
4891 double __fx = -0.5 * __r2;
4892 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4893 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4894 if (__using_exp_dist)
4895 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004896 if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4897 __fy * _VSTD::exp(__fx + __e))
Howard Hinnant76310352010-05-15 21:36:23 +00004898 break;
4899 }
4900 else
4901 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004902 if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
Howard Hinnant76310352010-05-15 21:36:23 +00004903 break;
4904 }
4905 }
4906 }
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004907 return _VSTD::__clamp_to_integral<result_type>(__tx);
Howard Hinnant76310352010-05-15 21:36:23 +00004908}
4909
4910template <class _CharT, class _Traits, class _IntType>
4911basic_ostream<_CharT, _Traits>&
4912operator<<(basic_ostream<_CharT, _Traits>& __os,
4913 const poisson_distribution<_IntType>& __x)
4914{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004915 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004916 typedef basic_ostream<_CharT, _Traits> _OStream;
4917 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
4918 _OStream::scientific);
Howard Hinnant76310352010-05-15 21:36:23 +00004919 return __os << __x.mean();
4920}
4921
4922template <class _CharT, class _Traits, class _IntType>
4923basic_istream<_CharT, _Traits>&
4924operator>>(basic_istream<_CharT, _Traits>& __is,
4925 poisson_distribution<_IntType>& __x)
4926{
4927 typedef poisson_distribution<_IntType> _Eng;
4928 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00004929 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04004930 typedef basic_istream<_CharT, _Traits> _Istream;
4931 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant76310352010-05-15 21:36:23 +00004932 double __mean;
4933 __is >> __mean;
4934 if (!__is.fail())
4935 __x.param(param_type(__mean));
4936 return __is;
4937}
4938
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004939// weibull_distribution
4940
4941template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004942class _LIBCPP_TEMPLATE_VIS weibull_distribution
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004943{
4944public:
4945 // types
4946 typedef _RealType result_type;
4947
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004948 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004949 {
4950 result_type __a_;
4951 result_type __b_;
4952 public:
4953 typedef weibull_distribution distribution_type;
4954
Howard Hinnantf5f99992010-09-22 18:02:38 +00004955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004956 explicit param_type(result_type __a = 1, result_type __b = 1)
4957 : __a_(__a), __b_(__b) {}
4958
Howard Hinnantf5f99992010-09-22 18:02:38 +00004959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004960 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004962 result_type b() const {return __b_;}
4963
Howard Hinnantf5f99992010-09-22 18:02:38 +00004964 friend _LIBCPP_INLINE_VISIBILITY
4965 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004966 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004967 friend _LIBCPP_INLINE_VISIBILITY
4968 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004969 {return !(__x == __y);}
4970 };
4971
4972private:
4973 param_type __p_;
4974
4975public:
4976 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004977#ifndef _LIBCPP_CXX03_LANG
4978 _LIBCPP_INLINE_VISIBILITY
4979 weibull_distribution() : weibull_distribution(1) {}
4980 _LIBCPP_INLINE_VISIBILITY
4981 explicit weibull_distribution(result_type __a, result_type __b = 1)
4982 : __p_(param_type(__a, __b)) {}
4983#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00004984 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004985 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4986 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01004987#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00004988 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004989 explicit weibull_distribution(const param_type& __p)
4990 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004992 void reset() {}
4993
4994 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00004995 template<class _URNG>
4996 _LIBCPP_INLINE_VISIBILITY
4997 result_type operator()(_URNG& __g)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00004998 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00004999 template<class _URNG>
5000 _LIBCPP_INLINE_VISIBILITY
5001 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005002 {return __p.b() *
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005003 _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005004
5005 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005007 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005009 result_type b() const {return __p_.b();}
5010
Howard Hinnantf5f99992010-09-22 18:02:38 +00005011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005012 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005014 void param(const param_type& __p) {__p_ = __p;}
5015
Howard Hinnantf5f99992010-09-22 18:02:38 +00005016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005017 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005019 result_type max() const {return numeric_limits<result_type>::infinity();}
5020
Howard Hinnantf5f99992010-09-22 18:02:38 +00005021 friend _LIBCPP_INLINE_VISIBILITY
5022 bool operator==(const weibull_distribution& __x,
5023 const weibull_distribution& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005024 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005025 friend _LIBCPP_INLINE_VISIBILITY
5026 bool operator!=(const weibull_distribution& __x,
5027 const weibull_distribution& __y)
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005028 {return !(__x == __y);}
5029};
5030
5031template <class _CharT, class _Traits, class _RT>
5032basic_ostream<_CharT, _Traits>&
5033operator<<(basic_ostream<_CharT, _Traits>& __os,
5034 const weibull_distribution<_RT>& __x)
5035{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005036 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005037 typedef basic_ostream<_CharT, _Traits> _OStream;
5038 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5039 _OStream::scientific);
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005040 _CharT __sp = __os.widen(' ');
5041 __os.fill(__sp);
5042 __os << __x.a() << __sp << __x.b();
5043 return __os;
5044}
5045
5046template <class _CharT, class _Traits, class _RT>
5047basic_istream<_CharT, _Traits>&
5048operator>>(basic_istream<_CharT, _Traits>& __is,
5049 weibull_distribution<_RT>& __x)
5050{
5051 typedef weibull_distribution<_RT> _Eng;
5052 typedef typename _Eng::result_type result_type;
5053 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005054 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005055 typedef basic_istream<_CharT, _Traits> _Istream;
5056 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantdb6b97b2010-05-16 01:09:02 +00005057 result_type __a;
5058 result_type __b;
5059 __is >> __a >> __b;
5060 if (!__is.fail())
5061 __x.param(param_type(__a, __b));
5062 return __is;
5063}
5064
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005065template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005066class _LIBCPP_TEMPLATE_VIS extreme_value_distribution
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005067{
5068public:
5069 // types
5070 typedef _RealType result_type;
5071
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005072 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005073 {
5074 result_type __a_;
5075 result_type __b_;
5076 public:
5077 typedef extreme_value_distribution distribution_type;
5078
Howard Hinnantf5f99992010-09-22 18:02:38 +00005079 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005080 explicit param_type(result_type __a = 0, result_type __b = 1)
5081 : __a_(__a), __b_(__b) {}
5082
Howard Hinnantf5f99992010-09-22 18:02:38 +00005083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005084 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005085 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005086 result_type b() const {return __b_;}
5087
Howard Hinnantf5f99992010-09-22 18:02:38 +00005088 friend _LIBCPP_INLINE_VISIBILITY
5089 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005090 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005091 friend _LIBCPP_INLINE_VISIBILITY
5092 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005093 {return !(__x == __y);}
5094 };
5095
5096private:
5097 param_type __p_;
5098
5099public:
5100 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005101#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005102 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005103 extreme_value_distribution() : extreme_value_distribution(0) {}
5104 _LIBCPP_INLINE_VISIBILITY
5105 explicit extreme_value_distribution(result_type __a, result_type __b = 1)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005106 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005107#else
5108 _LIBCPP_INLINE_VISIBILITY
5109 explicit extreme_value_distribution(result_type __a = 0,
5110 result_type __b = 1)
5111 : __p_(param_type(__a, __b)) {}
5112#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005114 explicit extreme_value_distribution(const param_type& __p)
5115 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005117 void reset() {}
5118
5119 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005120 template<class _URNG>
5121 _LIBCPP_INLINE_VISIBILITY
5122 result_type operator()(_URNG& __g)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005123 {return (*this)(__g, __p_);}
5124 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5125
5126 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005128 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005130 result_type b() const {return __p_.b();}
5131
Howard Hinnantf5f99992010-09-22 18:02:38 +00005132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005133 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005135 void param(const param_type& __p) {__p_ = __p;}
5136
Howard Hinnantf5f99992010-09-22 18:02:38 +00005137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005138 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005140 result_type max() const {return numeric_limits<result_type>::infinity();}
5141
Howard Hinnantf5f99992010-09-22 18:02:38 +00005142 friend _LIBCPP_INLINE_VISIBILITY
5143 bool operator==(const extreme_value_distribution& __x,
5144 const extreme_value_distribution& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005145 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005146 friend _LIBCPP_INLINE_VISIBILITY
5147 bool operator!=(const extreme_value_distribution& __x,
5148 const extreme_value_distribution& __y)
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005149 {return !(__x == __y);}
5150};
5151
5152template<class _RealType>
5153template<class _URNG>
5154_RealType
5155extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5156{
5157 return __p.a() - __p.b() *
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005158 _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005159}
5160
5161template <class _CharT, class _Traits, class _RT>
5162basic_ostream<_CharT, _Traits>&
5163operator<<(basic_ostream<_CharT, _Traits>& __os,
5164 const extreme_value_distribution<_RT>& __x)
5165{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005166 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005167 typedef basic_ostream<_CharT, _Traits> _OStream;
5168 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5169 _OStream::scientific);
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005170 _CharT __sp = __os.widen(' ');
5171 __os.fill(__sp);
5172 __os << __x.a() << __sp << __x.b();
5173 return __os;
5174}
5175
5176template <class _CharT, class _Traits, class _RT>
5177basic_istream<_CharT, _Traits>&
5178operator>>(basic_istream<_CharT, _Traits>& __is,
5179 extreme_value_distribution<_RT>& __x)
5180{
5181 typedef extreme_value_distribution<_RT> _Eng;
5182 typedef typename _Eng::result_type result_type;
5183 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005184 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005185 typedef basic_istream<_CharT, _Traits> _Istream;
5186 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant8e4ddf42010-05-17 16:21:56 +00005187 result_type __a;
5188 result_type __b;
5189 __is >> __a >> __b;
5190 if (!__is.fail())
5191 __x.param(param_type(__a, __b));
5192 return __is;
5193}
5194
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005195// gamma_distribution
5196
5197template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005198class _LIBCPP_TEMPLATE_VIS gamma_distribution
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005199{
5200public:
5201 // types
5202 typedef _RealType result_type;
5203
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005204 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005205 {
5206 result_type __alpha_;
5207 result_type __beta_;
5208 public:
5209 typedef gamma_distribution distribution_type;
5210
Howard Hinnantf5f99992010-09-22 18:02:38 +00005211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005212 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
5213 : __alpha_(__alpha), __beta_(__beta) {}
5214
Howard Hinnantf5f99992010-09-22 18:02:38 +00005215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005216 result_type alpha() const {return __alpha_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005218 result_type beta() const {return __beta_;}
5219
Howard Hinnantf5f99992010-09-22 18:02:38 +00005220 friend _LIBCPP_INLINE_VISIBILITY
5221 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005222 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005223 friend _LIBCPP_INLINE_VISIBILITY
5224 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005225 {return !(__x == __y);}
5226 };
5227
5228private:
5229 param_type __p_;
5230
5231public:
5232 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005233#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005234 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005235 gamma_distribution() : gamma_distribution(1) {}
5236 _LIBCPP_INLINE_VISIBILITY
5237 explicit gamma_distribution(result_type __alpha, result_type __beta = 1)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005238 : __p_(param_type(__alpha, __beta)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005239#else
5240 _LIBCPP_INLINE_VISIBILITY
5241 explicit gamma_distribution(result_type __alpha = 1,
5242 result_type __beta = 1)
5243 : __p_(param_type(__alpha, __beta)) {}
5244#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005246 explicit gamma_distribution(const param_type& __p)
5247 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005249 void reset() {}
5250
5251 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005252 template<class _URNG>
5253 _LIBCPP_INLINE_VISIBILITY
5254 result_type operator()(_URNG& __g)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005255 {return (*this)(__g, __p_);}
5256 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5257
5258 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005260 result_type alpha() const {return __p_.alpha();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005262 result_type beta() const {return __p_.beta();}
5263
Howard Hinnantf5f99992010-09-22 18:02:38 +00005264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005265 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005267 void param(const param_type& __p) {__p_ = __p;}
5268
Howard Hinnantf5f99992010-09-22 18:02:38 +00005269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005270 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005272 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005273
Howard Hinnantf5f99992010-09-22 18:02:38 +00005274 friend _LIBCPP_INLINE_VISIBILITY
5275 bool operator==(const gamma_distribution& __x,
5276 const gamma_distribution& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005277 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005278 friend _LIBCPP_INLINE_VISIBILITY
5279 bool operator!=(const gamma_distribution& __x,
5280 const gamma_distribution& __y)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005281 {return !(__x == __y);}
5282};
5283
5284template <class _RealType>
5285template<class _URNG>
5286_RealType
5287gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5288{
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005289 result_type __a = __p.alpha();
5290 uniform_real_distribution<result_type> __gen(0, 1);
5291 exponential_distribution<result_type> __egen;
5292 result_type __x;
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005293 if (__a == 1)
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005294 __x = __egen(__g);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005295 else if (__a > 1)
5296 {
5297 const result_type __b = __a - 1;
5298 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005299 while (true)
5300 {
5301 const result_type __u = __gen(__g);
5302 const result_type __v = __gen(__g);
5303 const result_type __w = __u * (1 - __u);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005304 if (__w != 0)
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005305 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005306 const result_type __y = _VSTD::sqrt(__c / __w) *
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005307 (__u - result_type(0.5));
5308 __x = __b + __y;
5309 if (__x >= 0)
5310 {
5311 const result_type __z = 64 * __w * __w * __w * __v * __v;
5312 if (__z <= 1 - 2 * __y * __y / __x)
5313 break;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005314 if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005315 break;
5316 }
5317 }
5318 }
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005319 }
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005320 else // __a < 1
5321 {
5322 while (true)
5323 {
5324 const result_type __u = __gen(__g);
5325 const result_type __es = __egen(__g);
5326 if (__u <= 1 - __a)
5327 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005328 __x = _VSTD::pow(__u, 1 / __a);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005329 if (__x <= __es)
5330 break;
5331 }
5332 else
5333 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005334 const result_type __e = -_VSTD::log((1-__u)/__a);
5335 __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
Howard Hinnant71c6b0b2010-05-14 18:43:10 +00005336 if (__x <= __e + __es)
5337 break;
5338 }
5339 }
5340 }
5341 return __x * __p.beta();
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005342}
5343
5344template <class _CharT, class _Traits, class _RT>
5345basic_ostream<_CharT, _Traits>&
5346operator<<(basic_ostream<_CharT, _Traits>& __os,
5347 const gamma_distribution<_RT>& __x)
5348{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005349 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005350 typedef basic_ostream<_CharT, _Traits> _OStream;
5351 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5352 _OStream::scientific);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005353 _CharT __sp = __os.widen(' ');
5354 __os.fill(__sp);
5355 __os << __x.alpha() << __sp << __x.beta();
5356 return __os;
5357}
5358
5359template <class _CharT, class _Traits, class _RT>
5360basic_istream<_CharT, _Traits>&
5361operator>>(basic_istream<_CharT, _Traits>& __is,
5362 gamma_distribution<_RT>& __x)
5363{
5364 typedef gamma_distribution<_RT> _Eng;
5365 typedef typename _Eng::result_type result_type;
5366 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005367 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005368 typedef basic_istream<_CharT, _Traits> _Istream;
5369 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantbb6d2022010-05-13 17:58:28 +00005370 result_type __alpha;
5371 result_type __beta;
5372 __is >> __alpha >> __beta;
5373 if (!__is.fail())
5374 __x.param(param_type(__alpha, __beta));
5375 return __is;
5376}
Howard Hinnant0b95bc92010-05-12 21:02:31 +00005377
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005378// negative_binomial_distribution
5379
5380template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005381class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005382{
5383public:
5384 // types
5385 typedef _IntType result_type;
5386
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005387 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005388 {
5389 result_type __k_;
5390 double __p_;
5391 public:
5392 typedef negative_binomial_distribution distribution_type;
5393
Howard Hinnantf5f99992010-09-22 18:02:38 +00005394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005395 explicit param_type(result_type __k = 1, double __p = 0.5)
5396 : __k_(__k), __p_(__p) {}
5397
Howard Hinnantf5f99992010-09-22 18:02:38 +00005398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005399 result_type k() const {return __k_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005401 double p() const {return __p_;}
5402
Howard Hinnantf5f99992010-09-22 18:02:38 +00005403 friend _LIBCPP_INLINE_VISIBILITY
5404 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005405 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005406 friend _LIBCPP_INLINE_VISIBILITY
5407 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005408 {return !(__x == __y);}
5409 };
5410
5411private:
5412 param_type __p_;
5413
5414public:
5415 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005416#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005417 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005418 negative_binomial_distribution() : negative_binomial_distribution(1) {}
5419 _LIBCPP_INLINE_VISIBILITY
5420 explicit negative_binomial_distribution(result_type __k, double __p = 0.5)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005421 : __p_(__k, __p) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005422#else
5423 _LIBCPP_INLINE_VISIBILITY
5424 explicit negative_binomial_distribution(result_type __k = 1,
5425 double __p = 0.5)
5426 : __p_(__k, __p) {}
5427#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005429 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005431 void reset() {}
5432
5433 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005434 template<class _URNG>
5435 _LIBCPP_INLINE_VISIBILITY
5436 result_type operator()(_URNG& __g)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005437 {return (*this)(__g, __p_);}
5438 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5439
5440 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005442 result_type k() const {return __p_.k();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005444 double p() const {return __p_.p();}
5445
Howard Hinnantf5f99992010-09-22 18:02:38 +00005446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005447 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005449 void param(const param_type& __p) {__p_ = __p;}
5450
Howard Hinnantf5f99992010-09-22 18:02:38 +00005451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005452 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005454 result_type max() const {return numeric_limits<result_type>::max();}
5455
Howard Hinnantf5f99992010-09-22 18:02:38 +00005456 friend _LIBCPP_INLINE_VISIBILITY
5457 bool operator==(const negative_binomial_distribution& __x,
5458 const negative_binomial_distribution& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005459 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005460 friend _LIBCPP_INLINE_VISIBILITY
5461 bool operator!=(const negative_binomial_distribution& __x,
5462 const negative_binomial_distribution& __y)
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005463 {return !(__x == __y);}
5464};
5465
5466template <class _IntType>
5467template<class _URNG>
5468_IntType
5469negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5470{
5471 result_type __k = __pr.k();
5472 double __p = __pr.p();
5473 if (__k <= 21 * __p)
5474 {
5475 bernoulli_distribution __gen(__p);
5476 result_type __f = 0;
5477 result_type __s = 0;
5478 while (__s < __k)
5479 {
5480 if (__gen(__urng))
5481 ++__s;
5482 else
5483 ++__f;
5484 }
5485 return __f;
5486 }
5487 return poisson_distribution<result_type>(gamma_distribution<double>
5488 (__k, (1-__p)/__p)(__urng))(__urng);
5489}
5490
5491template <class _CharT, class _Traits, class _IntType>
5492basic_ostream<_CharT, _Traits>&
5493operator<<(basic_ostream<_CharT, _Traits>& __os,
5494 const negative_binomial_distribution<_IntType>& __x)
5495{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005496 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005497 typedef basic_ostream<_CharT, _Traits> _OStream;
5498 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5499 _OStream::scientific);
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005500 _CharT __sp = __os.widen(' ');
5501 __os.fill(__sp);
5502 return __os << __x.k() << __sp << __x.p();
5503}
5504
5505template <class _CharT, class _Traits, class _IntType>
5506basic_istream<_CharT, _Traits>&
5507operator>>(basic_istream<_CharT, _Traits>& __is,
5508 negative_binomial_distribution<_IntType>& __x)
5509{
5510 typedef negative_binomial_distribution<_IntType> _Eng;
5511 typedef typename _Eng::result_type result_type;
5512 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005513 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005514 typedef basic_istream<_CharT, _Traits> _Istream;
5515 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant0f43ce62010-05-17 00:09:38 +00005516 result_type __k;
5517 double __p;
5518 __is >> __k >> __p;
5519 if (!__is.fail())
5520 __x.param(param_type(__k, __p));
5521 return __is;
5522}
5523
Howard Hinnant28beb162010-05-17 13:44:27 +00005524// geometric_distribution
5525
5526template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005527class _LIBCPP_TEMPLATE_VIS geometric_distribution
Howard Hinnant28beb162010-05-17 13:44:27 +00005528{
5529public:
5530 // types
5531 typedef _IntType result_type;
5532
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005533 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant28beb162010-05-17 13:44:27 +00005534 {
5535 double __p_;
5536 public:
5537 typedef geometric_distribution distribution_type;
5538
Howard Hinnantf5f99992010-09-22 18:02:38 +00005539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005540 explicit param_type(double __p = 0.5) : __p_(__p) {}
5541
Howard Hinnantf5f99992010-09-22 18:02:38 +00005542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005543 double p() const {return __p_;}
5544
Howard Hinnantf5f99992010-09-22 18:02:38 +00005545 friend _LIBCPP_INLINE_VISIBILITY
5546 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005547 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005548 friend _LIBCPP_INLINE_VISIBILITY
5549 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005550 {return !(__x == __y);}
5551 };
5552
5553private:
5554 param_type __p_;
5555
5556public:
5557 // constructors and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005558#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00005559 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005560 geometric_distribution() : geometric_distribution(0.5) {}
5561 _LIBCPP_INLINE_VISIBILITY
5562 explicit geometric_distribution(double __p)
5563 : __p_(__p) {}
5564#else
5565 _LIBCPP_INLINE_VISIBILITY
5566 explicit geometric_distribution(double __p = 0.5)
5567 : __p_(__p) {}
5568#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005570 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005572 void reset() {}
5573
5574 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005575 template<class _URNG>
5576 _LIBCPP_INLINE_VISIBILITY
5577 result_type operator()(_URNG& __g)
Howard Hinnant28beb162010-05-17 13:44:27 +00005578 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005579 template<class _URNG>
5580 _LIBCPP_INLINE_VISIBILITY
5581 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant28beb162010-05-17 13:44:27 +00005582 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5583
5584 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005586 double p() const {return __p_.p();}
5587
Howard Hinnantf5f99992010-09-22 18:02:38 +00005588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005589 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005591 void param(const param_type& __p) {__p_ = __p;}
5592
Howard Hinnantf5f99992010-09-22 18:02:38 +00005593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005594 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28beb162010-05-17 13:44:27 +00005596 result_type max() const {return numeric_limits<result_type>::max();}
5597
Howard Hinnantf5f99992010-09-22 18:02:38 +00005598 friend _LIBCPP_INLINE_VISIBILITY
5599 bool operator==(const geometric_distribution& __x,
5600 const geometric_distribution& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005601 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005602 friend _LIBCPP_INLINE_VISIBILITY
5603 bool operator!=(const geometric_distribution& __x,
5604 const geometric_distribution& __y)
Howard Hinnant28beb162010-05-17 13:44:27 +00005605 {return !(__x == __y);}
5606};
5607
5608template <class _CharT, class _Traits, class _IntType>
5609basic_ostream<_CharT, _Traits>&
5610operator<<(basic_ostream<_CharT, _Traits>& __os,
5611 const geometric_distribution<_IntType>& __x)
5612{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005613 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005614 typedef basic_ostream<_CharT, _Traits> _OStream;
5615 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5616 _OStream::scientific);
Howard Hinnant28beb162010-05-17 13:44:27 +00005617 return __os << __x.p();
5618}
5619
5620template <class _CharT, class _Traits, class _IntType>
5621basic_istream<_CharT, _Traits>&
5622operator>>(basic_istream<_CharT, _Traits>& __is,
5623 geometric_distribution<_IntType>& __x)
5624{
5625 typedef geometric_distribution<_IntType> _Eng;
5626 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005627 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005628 typedef basic_istream<_CharT, _Traits> _Istream;
5629 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant28beb162010-05-17 13:44:27 +00005630 double __p;
5631 __is >> __p;
5632 if (!__is.fail())
5633 __x.param(param_type(__p));
5634 return __is;
5635}
5636
Howard Hinnant252ebf02010-05-15 23:36:00 +00005637// chi_squared_distribution
5638
5639template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005640class _LIBCPP_TEMPLATE_VIS chi_squared_distribution
Howard Hinnant252ebf02010-05-15 23:36:00 +00005641{
5642public:
5643 // types
5644 typedef _RealType result_type;
5645
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005646 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant252ebf02010-05-15 23:36:00 +00005647 {
5648 result_type __n_;
5649 public:
5650 typedef chi_squared_distribution distribution_type;
5651
Howard Hinnantf5f99992010-09-22 18:02:38 +00005652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005653 explicit param_type(result_type __n = 1) : __n_(__n) {}
5654
Howard Hinnantf5f99992010-09-22 18:02:38 +00005655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005656 result_type n() const {return __n_;}
5657
Howard Hinnantf5f99992010-09-22 18:02:38 +00005658 friend _LIBCPP_INLINE_VISIBILITY
5659 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005660 {return __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005661 friend _LIBCPP_INLINE_VISIBILITY
5662 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005663 {return !(__x == __y);}
5664 };
5665
5666private:
5667 param_type __p_;
5668
5669public:
5670 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005671#ifndef _LIBCPP_CXX03_LANG
5672 _LIBCPP_INLINE_VISIBILITY
5673 chi_squared_distribution() : chi_squared_distribution(1) {}
5674 _LIBCPP_INLINE_VISIBILITY
5675 explicit chi_squared_distribution(result_type __n)
5676 : __p_(param_type(__n)) {}
5677#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00005678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005679 explicit chi_squared_distribution(result_type __n = 1)
5680 : __p_(param_type(__n)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005681#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005683 explicit chi_squared_distribution(const param_type& __p)
5684 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005686 void reset() {}
5687
5688 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005689 template<class _URNG>
5690 _LIBCPP_INLINE_VISIBILITY
5691 result_type operator()(_URNG& __g)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005692 {return (*this)(__g, __p_);}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005693 template<class _URNG>
5694 _LIBCPP_INLINE_VISIBILITY
5695 result_type operator()(_URNG& __g, const param_type& __p)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005696 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5697
5698 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005700 result_type n() const {return __p_.n();}
5701
Howard Hinnantf5f99992010-09-22 18:02:38 +00005702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005703 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005705 void param(const param_type& __p) {__p_ = __p;}
5706
Howard Hinnantf5f99992010-09-22 18:02:38 +00005707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant252ebf02010-05-15 23:36:00 +00005708 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005710 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant252ebf02010-05-15 23:36:00 +00005711
Howard Hinnantf5f99992010-09-22 18:02:38 +00005712 friend _LIBCPP_INLINE_VISIBILITY
5713 bool operator==(const chi_squared_distribution& __x,
5714 const chi_squared_distribution& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005715 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005716 friend _LIBCPP_INLINE_VISIBILITY
5717 bool operator!=(const chi_squared_distribution& __x,
5718 const chi_squared_distribution& __y)
Howard Hinnant252ebf02010-05-15 23:36:00 +00005719 {return !(__x == __y);}
5720};
5721
5722template <class _CharT, class _Traits, class _RT>
5723basic_ostream<_CharT, _Traits>&
5724operator<<(basic_ostream<_CharT, _Traits>& __os,
5725 const chi_squared_distribution<_RT>& __x)
5726{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005727 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005728 typedef basic_ostream<_CharT, _Traits> _OStream;
5729 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5730 _OStream::scientific);
Howard Hinnant252ebf02010-05-15 23:36:00 +00005731 __os << __x.n();
5732 return __os;
5733}
5734
5735template <class _CharT, class _Traits, class _RT>
5736basic_istream<_CharT, _Traits>&
5737operator>>(basic_istream<_CharT, _Traits>& __is,
5738 chi_squared_distribution<_RT>& __x)
5739{
5740 typedef chi_squared_distribution<_RT> _Eng;
5741 typedef typename _Eng::result_type result_type;
5742 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005743 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005744 typedef basic_istream<_CharT, _Traits> _Istream;
5745 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant252ebf02010-05-15 23:36:00 +00005746 result_type __n;
5747 __is >> __n;
5748 if (!__is.fail())
5749 __x.param(param_type(__n));
5750 return __is;
5751}
5752
Howard Hinnantf3292562010-05-17 21:55:46 +00005753// cauchy_distribution
5754
5755template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005756class _LIBCPP_TEMPLATE_VIS cauchy_distribution
Howard Hinnantf3292562010-05-17 21:55:46 +00005757{
5758public:
5759 // types
5760 typedef _RealType result_type;
5761
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005762 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantf3292562010-05-17 21:55:46 +00005763 {
5764 result_type __a_;
5765 result_type __b_;
5766 public:
5767 typedef cauchy_distribution distribution_type;
5768
Howard Hinnantf5f99992010-09-22 18:02:38 +00005769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005770 explicit param_type(result_type __a = 0, result_type __b = 1)
5771 : __a_(__a), __b_(__b) {}
5772
Howard Hinnantf5f99992010-09-22 18:02:38 +00005773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005774 result_type a() const {return __a_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005776 result_type b() const {return __b_;}
5777
Howard Hinnantf5f99992010-09-22 18:02:38 +00005778 friend _LIBCPP_INLINE_VISIBILITY
5779 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005780 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005781 friend _LIBCPP_INLINE_VISIBILITY
5782 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005783 {return !(__x == __y);}
5784 };
5785
5786private:
5787 param_type __p_;
5788
5789public:
5790 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005791#ifndef _LIBCPP_CXX03_LANG
5792 _LIBCPP_INLINE_VISIBILITY
5793 cauchy_distribution() : cauchy_distribution(0) {}
5794 _LIBCPP_INLINE_VISIBILITY
5795 explicit cauchy_distribution(result_type __a, result_type __b = 1)
5796 : __p_(param_type(__a, __b)) {}
5797#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00005798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005799 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5800 : __p_(param_type(__a, __b)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005801#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005803 explicit cauchy_distribution(const param_type& __p)
5804 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005806 void reset() {}
5807
5808 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005809 template<class _URNG>
5810 _LIBCPP_INLINE_VISIBILITY
5811 result_type operator()(_URNG& __g)
Howard Hinnantf3292562010-05-17 21:55:46 +00005812 {return (*this)(__g, __p_);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005813 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantf3292562010-05-17 21:55:46 +00005814
5815 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005817 result_type a() const {return __p_.a();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005819 result_type b() const {return __p_.b();}
5820
Howard Hinnantf5f99992010-09-22 18:02:38 +00005821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005822 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005824 void param(const param_type& __p) {__p_ = __p;}
5825
Howard Hinnantf5f99992010-09-22 18:02:38 +00005826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf3292562010-05-17 21:55:46 +00005827 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005829 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantf3292562010-05-17 21:55:46 +00005830
Howard Hinnantf5f99992010-09-22 18:02:38 +00005831 friend _LIBCPP_INLINE_VISIBILITY
5832 bool operator==(const cauchy_distribution& __x,
5833 const cauchy_distribution& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005834 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005835 friend _LIBCPP_INLINE_VISIBILITY
5836 bool operator!=(const cauchy_distribution& __x,
5837 const cauchy_distribution& __y)
Howard Hinnantf3292562010-05-17 21:55:46 +00005838 {return !(__x == __y);}
Howard Hinnantf3292562010-05-17 21:55:46 +00005839};
5840
5841template <class _RealType>
5842template<class _URNG>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005843inline
Howard Hinnantf3292562010-05-17 21:55:46 +00005844_RealType
5845cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5846{
5847 uniform_real_distribution<result_type> __gen;
5848 // 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 +00005849 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
Howard Hinnantf3292562010-05-17 21:55:46 +00005850}
5851
5852template <class _CharT, class _Traits, class _RT>
5853basic_ostream<_CharT, _Traits>&
5854operator<<(basic_ostream<_CharT, _Traits>& __os,
5855 const cauchy_distribution<_RT>& __x)
5856{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005857 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005858 typedef basic_ostream<_CharT, _Traits> _OStream;
5859 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5860 _OStream::scientific);
Howard Hinnantf3292562010-05-17 21:55:46 +00005861 _CharT __sp = __os.widen(' ');
5862 __os.fill(__sp);
5863 __os << __x.a() << __sp << __x.b();
5864 return __os;
5865}
5866
5867template <class _CharT, class _Traits, class _RT>
5868basic_istream<_CharT, _Traits>&
5869operator>>(basic_istream<_CharT, _Traits>& __is,
5870 cauchy_distribution<_RT>& __x)
5871{
5872 typedef cauchy_distribution<_RT> _Eng;
5873 typedef typename _Eng::result_type result_type;
5874 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00005875 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005876 typedef basic_istream<_CharT, _Traits> _Istream;
5877 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantf3292562010-05-17 21:55:46 +00005878 result_type __a;
5879 result_type __b;
5880 __is >> __a >> __b;
5881 if (!__is.fail())
5882 __x.param(param_type(__a, __b));
5883 return __is;
5884}
5885
Howard Hinnantf39463b2010-05-18 17:32:30 +00005886// fisher_f_distribution
5887
5888template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005889class _LIBCPP_TEMPLATE_VIS fisher_f_distribution
Howard Hinnantf39463b2010-05-18 17:32:30 +00005890{
5891public:
5892 // types
5893 typedef _RealType result_type;
5894
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005895 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnantf39463b2010-05-18 17:32:30 +00005896 {
5897 result_type __m_;
5898 result_type __n_;
5899 public:
5900 typedef fisher_f_distribution distribution_type;
5901
Howard Hinnantf5f99992010-09-22 18:02:38 +00005902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005903 explicit param_type(result_type __m = 1, result_type __n = 1)
5904 : __m_(__m), __n_(__n) {}
5905
Howard Hinnantf5f99992010-09-22 18:02:38 +00005906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005907 result_type m() const {return __m_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005909 result_type n() const {return __n_;}
5910
Howard Hinnantf5f99992010-09-22 18:02:38 +00005911 friend _LIBCPP_INLINE_VISIBILITY
5912 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005913 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005914 friend _LIBCPP_INLINE_VISIBILITY
5915 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005916 {return !(__x == __y);}
5917 };
5918
5919private:
5920 param_type __p_;
5921
5922public:
5923 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005924#ifndef _LIBCPP_CXX03_LANG
5925 _LIBCPP_INLINE_VISIBILITY
5926 fisher_f_distribution() : fisher_f_distribution(1) {}
5927 _LIBCPP_INLINE_VISIBILITY
5928 explicit fisher_f_distribution(result_type __m, result_type __n = 1)
5929 : __p_(param_type(__m, __n)) {}
5930#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00005931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005932 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5933 : __p_(param_type(__m, __n)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005934#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00005935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005936 explicit fisher_f_distribution(const param_type& __p)
5937 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005939 void reset() {}
5940
5941 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005942 template<class _URNG>
5943 _LIBCPP_INLINE_VISIBILITY
5944 result_type operator()(_URNG& __g)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005945 {return (*this)(__g, __p_);}
5946 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5947
5948 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00005949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005950 result_type m() const {return __p_.m();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005952 result_type n() const {return __p_.n();}
5953
Howard Hinnantf5f99992010-09-22 18:02:38 +00005954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005955 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005957 void param(const param_type& __p) {__p_ = __p;}
5958
Howard Hinnantf5f99992010-09-22 18:02:38 +00005959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39463b2010-05-18 17:32:30 +00005960 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005962 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnantf39463b2010-05-18 17:32:30 +00005963
Howard Hinnantf5f99992010-09-22 18:02:38 +00005964 friend _LIBCPP_INLINE_VISIBILITY
5965 bool operator==(const fisher_f_distribution& __x,
5966 const fisher_f_distribution& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005967 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00005968 friend _LIBCPP_INLINE_VISIBILITY
5969 bool operator!=(const fisher_f_distribution& __x,
5970 const fisher_f_distribution& __y)
Howard Hinnantf39463b2010-05-18 17:32:30 +00005971 {return !(__x == __y);}
5972};
5973
5974template <class _RealType>
5975template<class _URNG>
5976_RealType
5977fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5978{
5979 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5980 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5981 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5982}
5983
5984template <class _CharT, class _Traits, class _RT>
5985basic_ostream<_CharT, _Traits>&
5986operator<<(basic_ostream<_CharT, _Traits>& __os,
5987 const fisher_f_distribution<_RT>& __x)
5988{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005989 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04005990 typedef basic_ostream<_CharT, _Traits> _OStream;
5991 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
5992 _OStream::scientific);
Howard Hinnantf39463b2010-05-18 17:32:30 +00005993 _CharT __sp = __os.widen(' ');
5994 __os.fill(__sp);
5995 __os << __x.m() << __sp << __x.n();
5996 return __os;
5997}
5998
5999template <class _CharT, class _Traits, class _RT>
6000basic_istream<_CharT, _Traits>&
6001operator>>(basic_istream<_CharT, _Traits>& __is,
6002 fisher_f_distribution<_RT>& __x)
6003{
6004 typedef fisher_f_distribution<_RT> _Eng;
6005 typedef typename _Eng::result_type result_type;
6006 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00006007 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006008 typedef basic_istream<_CharT, _Traits> _Istream;
6009 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnantf39463b2010-05-18 17:32:30 +00006010 result_type __m;
6011 result_type __n;
6012 __is >> __m >> __n;
6013 if (!__is.fail())
6014 __x.param(param_type(__m, __n));
6015 return __is;
6016}
6017
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006018// student_t_distribution
6019
Howard Hinnant20c50882010-05-18 20:08:04 +00006020template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006021class _LIBCPP_TEMPLATE_VIS student_t_distribution
Howard Hinnant20c50882010-05-18 20:08:04 +00006022{
6023public:
6024 // types
6025 typedef _RealType result_type;
6026
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006027 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant20c50882010-05-18 20:08:04 +00006028 {
6029 result_type __n_;
6030 public:
6031 typedef student_t_distribution distribution_type;
6032
Howard Hinnantf5f99992010-09-22 18:02:38 +00006033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006034 explicit param_type(result_type __n = 1) : __n_(__n) {}
6035
Howard Hinnantf5f99992010-09-22 18:02:38 +00006036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006037 result_type n() const {return __n_;}
6038
Howard Hinnantf5f99992010-09-22 18:02:38 +00006039 friend _LIBCPP_INLINE_VISIBILITY
6040 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006041 {return __x.__n_ == __y.__n_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006042 friend _LIBCPP_INLINE_VISIBILITY
6043 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006044 {return !(__x == __y);}
6045 };
6046
6047private:
6048 param_type __p_;
6049 normal_distribution<result_type> __nd_;
6050
6051public:
6052 // constructor and reset functions
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01006053#ifndef _LIBCPP_CXX03_LANG
6054 _LIBCPP_INLINE_VISIBILITY
6055 student_t_distribution() : student_t_distribution(1) {}
6056 _LIBCPP_INLINE_VISIBILITY
6057 explicit student_t_distribution(result_type __n)
6058 : __p_(param_type(__n)) {}
6059#else
Howard Hinnantf5f99992010-09-22 18:02:38 +00006060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006061 explicit student_t_distribution(result_type __n = 1)
6062 : __p_(param_type(__n)) {}
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01006063#endif
Howard Hinnantf5f99992010-09-22 18:02:38 +00006064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006065 explicit student_t_distribution(const param_type& __p)
6066 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006068 void reset() {__nd_.reset();}
6069
6070 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006071 template<class _URNG>
6072 _LIBCPP_INLINE_VISIBILITY
6073 result_type operator()(_URNG& __g)
Howard Hinnant20c50882010-05-18 20:08:04 +00006074 {return (*this)(__g, __p_);}
6075 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6076
6077 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006079 result_type n() const {return __p_.n();}
6080
Howard Hinnantf5f99992010-09-22 18:02:38 +00006081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006082 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006084 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant20c50882010-05-18 20:08:04 +00006085
Howard Hinnantf5f99992010-09-22 18:02:38 +00006086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006087 result_type min() const {return -numeric_limits<result_type>::infinity();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006088 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant20c50882010-05-18 20:08:04 +00006089 result_type max() const {return numeric_limits<result_type>::infinity();}
6090
Howard Hinnantf5f99992010-09-22 18:02:38 +00006091 friend _LIBCPP_INLINE_VISIBILITY
6092 bool operator==(const student_t_distribution& __x,
6093 const student_t_distribution& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006094 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006095 friend _LIBCPP_INLINE_VISIBILITY
6096 bool operator!=(const student_t_distribution& __x,
6097 const student_t_distribution& __y)
Howard Hinnant20c50882010-05-18 20:08:04 +00006098 {return !(__x == __y);}
6099};
6100
6101template <class _RealType>
6102template<class _URNG>
6103_RealType
6104student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6105{
6106 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006107 return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
Howard Hinnant20c50882010-05-18 20:08:04 +00006108}
6109
6110template <class _CharT, class _Traits, class _RT>
6111basic_ostream<_CharT, _Traits>&
6112operator<<(basic_ostream<_CharT, _Traits>& __os,
6113 const student_t_distribution<_RT>& __x)
6114{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006115 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006116 typedef basic_ostream<_CharT, _Traits> _OStream;
6117 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6118 _OStream::scientific);
Howard Hinnant20c50882010-05-18 20:08:04 +00006119 __os << __x.n();
6120 return __os;
6121}
6122
6123template <class _CharT, class _Traits, class _RT>
6124basic_istream<_CharT, _Traits>&
6125operator>>(basic_istream<_CharT, _Traits>& __is,
6126 student_t_distribution<_RT>& __x)
6127{
6128 typedef student_t_distribution<_RT> _Eng;
6129 typedef typename _Eng::result_type result_type;
6130 typedef typename _Eng::param_type param_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00006131 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006132 typedef basic_istream<_CharT, _Traits> _Istream;
6133 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant20c50882010-05-18 20:08:04 +00006134 result_type __n;
6135 __is >> __n;
6136 if (!__is.fail())
6137 __x.param(param_type(__n));
6138 return __is;
6139}
6140
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006141// discrete_distribution
6142
6143template<class _IntType = int>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006144class _LIBCPP_TEMPLATE_VIS discrete_distribution
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006145{
6146public:
6147 // types
6148 typedef _IntType result_type;
6149
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006150 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006151 {
6152 vector<double> __p_;
6153 public:
6154 typedef discrete_distribution distribution_type;
6155
Howard Hinnantf5f99992010-09-22 18:02:38 +00006156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006157 param_type() {}
6158 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006160 param_type(_InputIterator __f, _InputIterator __l)
6161 : __p_(__f, __l) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006162#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00006163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006164 param_type(initializer_list<double> __wl)
6165 : __p_(__wl.begin(), __wl.end()) {__init();}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006166#endif // _LIBCPP_CXX03_LANG
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006167 template<class _UnaryOperation>
6168 param_type(size_t __nw, double __xmin, double __xmax,
6169 _UnaryOperation __fw);
6170
6171 vector<double> probabilities() const;
6172
Howard Hinnantf5f99992010-09-22 18:02:38 +00006173 friend _LIBCPP_INLINE_VISIBILITY
6174 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006175 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006176 friend _LIBCPP_INLINE_VISIBILITY
6177 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006178 {return !(__x == __y);}
6179
6180 private:
6181 void __init();
6182
6183 friend class discrete_distribution;
6184
6185 template <class _CharT, class _Traits, class _IT>
6186 friend
6187 basic_ostream<_CharT, _Traits>&
6188 operator<<(basic_ostream<_CharT, _Traits>& __os,
6189 const discrete_distribution<_IT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006190
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006191 template <class _CharT, class _Traits, class _IT>
6192 friend
6193 basic_istream<_CharT, _Traits>&
6194 operator>>(basic_istream<_CharT, _Traits>& __is,
6195 discrete_distribution<_IT>& __x);
6196 };
6197
6198private:
6199 param_type __p_;
6200
6201public:
6202 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006204 discrete_distribution() {}
6205 template<class _InputIterator>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006207 discrete_distribution(_InputIterator __f, _InputIterator __l)
6208 : __p_(__f, __l) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006209#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf5f99992010-09-22 18:02:38 +00006210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006211 discrete_distribution(initializer_list<double> __wl)
6212 : __p_(__wl) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006213#endif // _LIBCPP_CXX03_LANG
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006214 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006216 discrete_distribution(size_t __nw, double __xmin, double __xmax,
6217 _UnaryOperation __fw)
6218 : __p_(__nw, __xmin, __xmax, __fw) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00006220 explicit discrete_distribution(const param_type& __p)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006221 : __p_(__p) {}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006223 void reset() {}
6224
6225 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006226 template<class _URNG>
6227 _LIBCPP_INLINE_VISIBILITY
6228 result_type operator()(_URNG& __g)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006229 {return (*this)(__g, __p_);}
6230 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6231
6232 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006234 vector<double> probabilities() const {return __p_.probabilities();}
6235
Howard Hinnantf5f99992010-09-22 18:02:38 +00006236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006237 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006239 void param(const param_type& __p) {__p_ = __p;}
6240
Howard Hinnantf5f99992010-09-22 18:02:38 +00006241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006242 result_type min() const {return 0;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006244 result_type max() const {return __p_.__p_.size();}
6245
Howard Hinnantf5f99992010-09-22 18:02:38 +00006246 friend _LIBCPP_INLINE_VISIBILITY
6247 bool operator==(const discrete_distribution& __x,
6248 const discrete_distribution& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006249 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006250 friend _LIBCPP_INLINE_VISIBILITY
6251 bool operator!=(const discrete_distribution& __x,
6252 const discrete_distribution& __y)
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006253 {return !(__x == __y);}
6254
6255 template <class _CharT, class _Traits, class _IT>
6256 friend
6257 basic_ostream<_CharT, _Traits>&
6258 operator<<(basic_ostream<_CharT, _Traits>& __os,
6259 const discrete_distribution<_IT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006260
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006261 template <class _CharT, class _Traits, class _IT>
6262 friend
6263 basic_istream<_CharT, _Traits>&
6264 operator>>(basic_istream<_CharT, _Traits>& __is,
6265 discrete_distribution<_IT>& __x);
6266};
6267
6268template<class _IntType>
6269template<class _UnaryOperation>
6270discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
6271 double __xmin,
6272 double __xmax,
6273 _UnaryOperation __fw)
6274{
6275 if (__nw > 1)
6276 {
6277 __p_.reserve(__nw - 1);
6278 double __d = (__xmax - __xmin) / __nw;
6279 double __d2 = __d / 2;
6280 for (size_t __k = 0; __k < __nw; ++__k)
6281 __p_.push_back(__fw(__xmin + __k * __d + __d2));
6282 __init();
6283 }
6284}
6285
6286template<class _IntType>
6287void
6288discrete_distribution<_IntType>::param_type::__init()
6289{
6290 if (!__p_.empty())
6291 {
6292 if (__p_.size() > 1)
6293 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006294 double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
6295 for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006296 __i < __e; ++__i)
6297 *__i /= __s;
6298 vector<double> __t(__p_.size() - 1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006299 _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006300 swap(__p_, __t);
6301 }
6302 else
6303 {
6304 __p_.clear();
6305 __p_.shrink_to_fit();
6306 }
6307 }
6308}
6309
6310template<class _IntType>
6311vector<double>
6312discrete_distribution<_IntType>::param_type::probabilities() const
6313{
6314 size_t __n = __p_.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006315 _VSTD::vector<double> __p(__n+1);
6316 _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006317 if (__n > 0)
6318 __p[__n] = 1 - __p_[__n-1];
6319 else
6320 __p[0] = 1;
6321 return __p;
6322}
6323
6324template<class _IntType>
6325template<class _URNG>
6326_IntType
6327discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
6328{
6329 uniform_real_distribution<double> __gen;
6330 return static_cast<_IntType>(
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006331 _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006332 __p.__p_.begin());
6333}
6334
6335template <class _CharT, class _Traits, class _IT>
6336basic_ostream<_CharT, _Traits>&
6337operator<<(basic_ostream<_CharT, _Traits>& __os,
6338 const discrete_distribution<_IT>& __x)
6339{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006340 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006341 typedef basic_ostream<_CharT, _Traits> _OStream;
6342 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6343 _OStream::scientific);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006344 _CharT __sp = __os.widen(' ');
6345 __os.fill(__sp);
6346 size_t __n = __x.__p_.__p_.size();
6347 __os << __n;
6348 for (size_t __i = 0; __i < __n; ++__i)
6349 __os << __sp << __x.__p_.__p_[__i];
6350 return __os;
6351}
6352
6353template <class _CharT, class _Traits, class _IT>
6354basic_istream<_CharT, _Traits>&
6355operator>>(basic_istream<_CharT, _Traits>& __is,
6356 discrete_distribution<_IT>& __x)
6357{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006358 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006359 typedef basic_istream<_CharT, _Traits> _Istream;
6360 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006361 size_t __n;
6362 __is >> __n;
Howard Hinnant481ba382010-05-20 15:11:46 +00006363 vector<double> __p(__n);
Howard Hinnant7fdb18d2010-05-19 01:53:57 +00006364 for (size_t __i = 0; __i < __n; ++__i)
6365 __is >> __p[__i];
6366 if (!__is.fail())
6367 swap(__x.__p_.__p_, __p);
6368 return __is;
6369}
6370
Howard Hinnant481ba382010-05-20 15:11:46 +00006371// piecewise_constant_distribution
6372
6373template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006374class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution
Howard Hinnant481ba382010-05-20 15:11:46 +00006375{
6376public:
6377 // types
6378 typedef _RealType result_type;
6379
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006380 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnant481ba382010-05-20 15:11:46 +00006381 {
Howard Hinnant481ba382010-05-20 15:11:46 +00006382 vector<result_type> __b_;
Howard Hinnant167159f2010-11-18 17:01:36 +00006383 vector<result_type> __densities_;
6384 vector<result_type> __areas_;
Howard Hinnant481ba382010-05-20 15:11:46 +00006385 public:
6386 typedef piecewise_constant_distribution distribution_type;
6387
6388 param_type();
6389 template<class _InputIteratorB, class _InputIteratorW>
6390 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6391 _InputIteratorW __fW);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006392#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006393 template<class _UnaryOperation>
6394 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006395#endif // _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006396 template<class _UnaryOperation>
6397 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6398 _UnaryOperation __fw);
Eric Fiselierb9f37ca2019-12-12 20:48:11 -05006399 param_type(param_type const&) = default;
Howard Hinnant71c410b2010-10-13 14:37:09 +00006400 param_type & operator=(const param_type& __rhs);
Howard Hinnant481ba382010-05-20 15:11:46 +00006401
Howard Hinnantf5f99992010-09-22 18:02:38 +00006402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006403 vector<result_type> intervals() const {return __b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006405 vector<result_type> densities() const {return __densities_;}
Howard Hinnant481ba382010-05-20 15:11:46 +00006406
Howard Hinnantf5f99992010-09-22 18:02:38 +00006407 friend _LIBCPP_INLINE_VISIBILITY
6408 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006409 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006410 friend _LIBCPP_INLINE_VISIBILITY
6411 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnant481ba382010-05-20 15:11:46 +00006412 {return !(__x == __y);}
6413
6414 private:
6415 void __init();
6416
6417 friend class piecewise_constant_distribution;
6418
6419 template <class _CharT, class _Traits, class _RT>
6420 friend
6421 basic_ostream<_CharT, _Traits>&
6422 operator<<(basic_ostream<_CharT, _Traits>& __os,
6423 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006424
Howard Hinnant481ba382010-05-20 15:11:46 +00006425 template <class _CharT, class _Traits, class _RT>
6426 friend
6427 basic_istream<_CharT, _Traits>&
6428 operator>>(basic_istream<_CharT, _Traits>& __is,
6429 piecewise_constant_distribution<_RT>& __x);
6430 };
6431
6432private:
6433 param_type __p_;
6434
6435public:
6436 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006438 piecewise_constant_distribution() {}
6439 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006441 piecewise_constant_distribution(_InputIteratorB __fB,
6442 _InputIteratorB __lB,
6443 _InputIteratorW __fW)
6444 : __p_(__fB, __lB, __fW) {}
6445
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006446#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006447 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006449 piecewise_constant_distribution(initializer_list<result_type> __bl,
6450 _UnaryOperation __fw)
6451 : __p_(__bl, __fw) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006452#endif // _LIBCPP_CXX03_LANG
Howard Hinnant481ba382010-05-20 15:11:46 +00006453
6454 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006456 piecewise_constant_distribution(size_t __nw, result_type __xmin,
6457 result_type __xmax, _UnaryOperation __fw)
6458 : __p_(__nw, __xmin, __xmax, __fw) {}
6459
Howard Hinnantf5f99992010-09-22 18:02:38 +00006460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006461 explicit piecewise_constant_distribution(const param_type& __p)
6462 : __p_(__p) {}
6463
Howard Hinnantf5f99992010-09-22 18:02:38 +00006464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006465 void reset() {}
6466
6467 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006468 template<class _URNG>
6469 _LIBCPP_INLINE_VISIBILITY
6470 result_type operator()(_URNG& __g)
Howard Hinnant481ba382010-05-20 15:11:46 +00006471 {return (*this)(__g, __p_);}
6472 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6473
6474 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006476 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006478 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnant481ba382010-05-20 15:11:46 +00006479
Howard Hinnantf5f99992010-09-22 18:02:38 +00006480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006481 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006483 void param(const param_type& __p) {__p_ = __p;}
6484
Howard Hinnantf5f99992010-09-22 18:02:38 +00006485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006486 result_type min() const {return __p_.__b_.front();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant481ba382010-05-20 15:11:46 +00006488 result_type max() const {return __p_.__b_.back();}
6489
Howard Hinnantf5f99992010-09-22 18:02:38 +00006490 friend _LIBCPP_INLINE_VISIBILITY
6491 bool operator==(const piecewise_constant_distribution& __x,
6492 const piecewise_constant_distribution& __y)
Howard Hinnant481ba382010-05-20 15:11:46 +00006493 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006494 friend _LIBCPP_INLINE_VISIBILITY
6495 bool operator!=(const piecewise_constant_distribution& __x,
Howard Hinnant481ba382010-05-20 15:11:46 +00006496 const piecewise_constant_distribution& __y)
6497 {return !(__x == __y);}
6498
6499 template <class _CharT, class _Traits, class _RT>
6500 friend
6501 basic_ostream<_CharT, _Traits>&
6502 operator<<(basic_ostream<_CharT, _Traits>& __os,
6503 const piecewise_constant_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006504
Howard Hinnant481ba382010-05-20 15:11:46 +00006505 template <class _CharT, class _Traits, class _RT>
6506 friend
6507 basic_istream<_CharT, _Traits>&
6508 operator>>(basic_istream<_CharT, _Traits>& __is,
6509 piecewise_constant_distribution<_RT>& __x);
6510};
6511
6512template<class _RealType>
Howard Hinnant71c410b2010-10-13 14:37:09 +00006513typename piecewise_constant_distribution<_RealType>::param_type &
6514piecewise_constant_distribution<_RealType>::param_type::operator=
6515 (const param_type& __rhs)
6516{
6517// These can throw
6518 __b_.reserve (__rhs.__b_.size ());
6519 __densities_.reserve(__rhs.__densities_.size());
6520 __areas_.reserve (__rhs.__areas_.size());
6521
6522// These can not throw
6523 __b_ = __rhs.__b_;
6524 __densities_ = __rhs.__densities_;
6525 __areas_ = __rhs.__areas_;
6526 return *this;
6527}
6528
6529template<class _RealType>
Howard Hinnant481ba382010-05-20 15:11:46 +00006530void
6531piecewise_constant_distribution<_RealType>::param_type::__init()
6532{
Howard Hinnant35d83df2010-05-24 00:35:40 +00006533 // __densities_ contains non-normalized areas
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006534 result_type __total_area = _VSTD::accumulate(__densities_.begin(),
Howard Hinnant35d83df2010-05-24 00:35:40 +00006535 __densities_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006536 result_type());
Howard Hinnant35d83df2010-05-24 00:35:40 +00006537 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6538 __densities_[__i] /= __total_area;
6539 // __densities_ contains normalized areas
Howard Hinnant167159f2010-11-18 17:01:36 +00006540 __areas_.assign(__densities_.size(), result_type());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006541 _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
Howard Hinnant35d83df2010-05-24 00:35:40 +00006542 __areas_.begin() + 1);
6543 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6544 __densities_.back() = 1 - __areas_.back(); // correct round off error
6545 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6546 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6547 // __densities_ now contains __densities_
Howard Hinnant481ba382010-05-20 15:11:46 +00006548}
6549
6550template<class _RealType>
6551piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant35d83df2010-05-24 00:35:40 +00006552 : __b_(2),
Howard Hinnanta001ef32010-05-25 00:27:34 +00006553 __densities_(1, 1.0),
6554 __areas_(1, 0.0)
Howard Hinnant481ba382010-05-20 15:11:46 +00006555{
6556 __b_[1] = 1;
6557}
6558
6559template<class _RealType>
6560template<class _InputIteratorB, class _InputIteratorW>
6561piecewise_constant_distribution<_RealType>::param_type::param_type(
6562 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6563 : __b_(__fB, __lB)
6564{
6565 if (__b_.size() < 2)
6566 {
6567 __b_.resize(2);
6568 __b_[0] = 0;
6569 __b_[1] = 1;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006570 __densities_.assign(1, 1.0);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006571 __areas_.assign(1, 0.0);
Howard Hinnant481ba382010-05-20 15:11:46 +00006572 }
6573 else
6574 {
Howard Hinnant35d83df2010-05-24 00:35:40 +00006575 __densities_.reserve(__b_.size() - 1);
Howard Hinnant481ba382010-05-20 15:11:46 +00006576 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006577 __densities_.push_back(*__fW);
Howard Hinnant481ba382010-05-20 15:11:46 +00006578 __init();
6579 }
6580}
6581
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006582#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006583
Howard Hinnant481ba382010-05-20 15:11:46 +00006584template<class _RealType>
6585template<class _UnaryOperation>
6586piecewise_constant_distribution<_RealType>::param_type::param_type(
6587 initializer_list<result_type> __bl, _UnaryOperation __fw)
6588 : __b_(__bl.begin(), __bl.end())
6589{
6590 if (__b_.size() < 2)
6591 {
6592 __b_.resize(2);
6593 __b_[0] = 0;
6594 __b_[1] = 1;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006595 __densities_.assign(1, 1.0);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006596 __areas_.assign(1, 0.0);
Howard Hinnant481ba382010-05-20 15:11:46 +00006597 }
6598 else
6599 {
Howard Hinnant35d83df2010-05-24 00:35:40 +00006600 __densities_.reserve(__b_.size() - 1);
Howard Hinnant481ba382010-05-20 15:11:46 +00006601 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006602 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnant481ba382010-05-20 15:11:46 +00006603 __init();
6604 }
6605}
6606
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006607#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006608
Howard Hinnant481ba382010-05-20 15:11:46 +00006609template<class _RealType>
6610template<class _UnaryOperation>
6611piecewise_constant_distribution<_RealType>::param_type::param_type(
6612 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6613 : __b_(__nw == 0 ? 2 : __nw + 1)
6614{
6615 size_t __n = __b_.size() - 1;
6616 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006617 __densities_.reserve(__n);
Howard Hinnant481ba382010-05-20 15:11:46 +00006618 for (size_t __i = 0; __i < __n; ++__i)
6619 {
6620 __b_[__i] = __xmin + __i * __d;
Howard Hinnant35d83df2010-05-24 00:35:40 +00006621 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnant481ba382010-05-20 15:11:46 +00006622 }
6623 __b_[__n] = __xmax;
6624 __init();
6625}
6626
6627template<class _RealType>
Howard Hinnant481ba382010-05-20 15:11:46 +00006628template<class _URNG>
6629_RealType
6630piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6631{
6632 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnant481ba382010-05-20 15:11:46 +00006633 result_type __u = _Gen()(__g);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006634 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006635 __u) - __p.__areas_.begin() - 1;
6636 return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
Howard Hinnant481ba382010-05-20 15:11:46 +00006637}
6638
6639template <class _CharT, class _Traits, class _RT>
6640basic_ostream<_CharT, _Traits>&
6641operator<<(basic_ostream<_CharT, _Traits>& __os,
6642 const piecewise_constant_distribution<_RT>& __x)
6643{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006644 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006645 typedef basic_ostream<_CharT, _Traits> _OStream;
6646 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6647 _OStream::scientific);
Howard Hinnant481ba382010-05-20 15:11:46 +00006648 _CharT __sp = __os.widen(' ');
6649 __os.fill(__sp);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006650 size_t __n = __x.__p_.__b_.size();
Howard Hinnant481ba382010-05-20 15:11:46 +00006651 __os << __n;
6652 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006653 __os << __sp << __x.__p_.__b_[__i];
6654 __n = __x.__p_.__densities_.size();
Howard Hinnant481ba382010-05-20 15:11:46 +00006655 __os << __sp << __n;
6656 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant35d83df2010-05-24 00:35:40 +00006657 __os << __sp << __x.__p_.__densities_[__i];
6658 __n = __x.__p_.__areas_.size();
6659 __os << __sp << __n;
6660 for (size_t __i = 0; __i < __n; ++__i)
6661 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnant481ba382010-05-20 15:11:46 +00006662 return __os;
6663}
6664
6665template <class _CharT, class _Traits, class _RT>
6666basic_istream<_CharT, _Traits>&
6667operator>>(basic_istream<_CharT, _Traits>& __is,
6668 piecewise_constant_distribution<_RT>& __x)
6669{
6670 typedef piecewise_constant_distribution<_RT> _Eng;
6671 typedef typename _Eng::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00006672 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006673 typedef basic_istream<_CharT, _Traits> _Istream;
6674 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnant481ba382010-05-20 15:11:46 +00006675 size_t __n;
6676 __is >> __n;
Howard Hinnant481ba382010-05-20 15:11:46 +00006677 vector<result_type> __b(__n);
6678 for (size_t __i = 0; __i < __n; ++__i)
6679 __is >> __b[__i];
Howard Hinnant35d83df2010-05-24 00:35:40 +00006680 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006681 vector<result_type> __densities(__n);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006682 for (size_t __i = 0; __i < __n; ++__i)
6683 __is >> __densities[__i];
6684 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00006685 vector<result_type> __areas(__n);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006686 for (size_t __i = 0; __i < __n; ++__i)
6687 __is >> __areas[__i];
Howard Hinnant481ba382010-05-20 15:11:46 +00006688 if (!__is.fail())
6689 {
Howard Hinnant481ba382010-05-20 15:11:46 +00006690 swap(__x.__p_.__b_, __b);
Howard Hinnant35d83df2010-05-24 00:35:40 +00006691 swap(__x.__p_.__densities_, __densities);
6692 swap(__x.__p_.__areas_, __areas);
Howard Hinnant481ba382010-05-20 15:11:46 +00006693 }
6694 return __is;
6695}
6696
Howard Hinnanta001ef32010-05-25 00:27:34 +00006697// piecewise_linear_distribution
6698
6699template<class _RealType = double>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006700class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution
Howard Hinnanta001ef32010-05-25 00:27:34 +00006701{
6702public:
6703 // types
6704 typedef _RealType result_type;
6705
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00006706 class _LIBCPP_TEMPLATE_VIS param_type
Howard Hinnanta001ef32010-05-25 00:27:34 +00006707 {
Howard Hinnanta001ef32010-05-25 00:27:34 +00006708 vector<result_type> __b_;
Howard Hinnant167159f2010-11-18 17:01:36 +00006709 vector<result_type> __densities_;
6710 vector<result_type> __areas_;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006711 public:
6712 typedef piecewise_linear_distribution distribution_type;
6713
6714 param_type();
6715 template<class _InputIteratorB, class _InputIteratorW>
6716 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6717 _InputIteratorW __fW);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006718#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006719 template<class _UnaryOperation>
6720 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006721#endif // _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006722 template<class _UnaryOperation>
6723 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6724 _UnaryOperation __fw);
Eric Fiselierb9f37ca2019-12-12 20:48:11 -05006725 param_type(param_type const&) = default;
Howard Hinnant71c410b2010-10-13 14:37:09 +00006726 param_type & operator=(const param_type& __rhs);
Louis Dionne173f29e2019-05-29 16:01:36 +00006727
Howard Hinnantf5f99992010-09-22 18:02:38 +00006728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006729 vector<result_type> intervals() const {return __b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006731 vector<result_type> densities() const {return __densities_;}
Howard Hinnanta001ef32010-05-25 00:27:34 +00006732
Howard Hinnantf5f99992010-09-22 18:02:38 +00006733 friend _LIBCPP_INLINE_VISIBILITY
6734 bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006735 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006736 friend _LIBCPP_INLINE_VISIBILITY
6737 bool operator!=(const param_type& __x, const param_type& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006738 {return !(__x == __y);}
6739
6740 private:
6741 void __init();
6742
6743 friend class piecewise_linear_distribution;
6744
6745 template <class _CharT, class _Traits, class _RT>
6746 friend
6747 basic_ostream<_CharT, _Traits>&
6748 operator<<(basic_ostream<_CharT, _Traits>& __os,
6749 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006750
Howard Hinnanta001ef32010-05-25 00:27:34 +00006751 template <class _CharT, class _Traits, class _RT>
6752 friend
6753 basic_istream<_CharT, _Traits>&
6754 operator>>(basic_istream<_CharT, _Traits>& __is,
6755 piecewise_linear_distribution<_RT>& __x);
6756 };
6757
6758private:
6759 param_type __p_;
6760
6761public:
6762 // constructor and reset functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006764 piecewise_linear_distribution() {}
6765 template<class _InputIteratorB, class _InputIteratorW>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006767 piecewise_linear_distribution(_InputIteratorB __fB,
6768 _InputIteratorB __lB,
6769 _InputIteratorW __fW)
6770 : __p_(__fB, __lB, __fW) {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006771
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006772#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006773 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006775 piecewise_linear_distribution(initializer_list<result_type> __bl,
6776 _UnaryOperation __fw)
6777 : __p_(__bl, __fw) {}
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006778#endif // _LIBCPP_CXX03_LANG
Howard Hinnanta001ef32010-05-25 00:27:34 +00006779
6780 template<class _UnaryOperation>
Howard Hinnantf5f99992010-09-22 18:02:38 +00006781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006782 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6783 result_type __xmax, _UnaryOperation __fw)
6784 : __p_(__nw, __xmin, __xmax, __fw) {}
6785
Howard Hinnantf5f99992010-09-22 18:02:38 +00006786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006787 explicit piecewise_linear_distribution(const param_type& __p)
6788 : __p_(__p) {}
6789
Howard Hinnantf5f99992010-09-22 18:02:38 +00006790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006791 void reset() {}
6792
6793 // generating functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006794 template<class _URNG>
6795 _LIBCPP_INLINE_VISIBILITY
6796 result_type operator()(_URNG& __g)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006797 {return (*this)(__g, __p_);}
6798 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6799
6800 // property functions
Howard Hinnantf5f99992010-09-22 18:02:38 +00006801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006802 vector<result_type> intervals() const {return __p_.intervals();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant167159f2010-11-18 17:01:36 +00006804 vector<result_type> densities() const {return __p_.densities();}
Howard Hinnanta001ef32010-05-25 00:27:34 +00006805
Howard Hinnantf5f99992010-09-22 18:02:38 +00006806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006807 param_type param() const {return __p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006809 void param(const param_type& __p) {__p_ = __p;}
6810
Howard Hinnantf5f99992010-09-22 18:02:38 +00006811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006812 result_type min() const {return __p_.__b_.front();}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta001ef32010-05-25 00:27:34 +00006814 result_type max() const {return __p_.__b_.back();}
6815
Howard Hinnantf5f99992010-09-22 18:02:38 +00006816 friend _LIBCPP_INLINE_VISIBILITY
6817 bool operator==(const piecewise_linear_distribution& __x,
6818 const piecewise_linear_distribution& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006819 {return __x.__p_ == __y.__p_;}
Howard Hinnantf5f99992010-09-22 18:02:38 +00006820 friend _LIBCPP_INLINE_VISIBILITY
6821 bool operator!=(const piecewise_linear_distribution& __x,
6822 const piecewise_linear_distribution& __y)
Howard Hinnanta001ef32010-05-25 00:27:34 +00006823 {return !(__x == __y);}
6824
6825 template <class _CharT, class _Traits, class _RT>
6826 friend
6827 basic_ostream<_CharT, _Traits>&
6828 operator<<(basic_ostream<_CharT, _Traits>& __os,
6829 const piecewise_linear_distribution<_RT>& __x);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00006830
Howard Hinnanta001ef32010-05-25 00:27:34 +00006831 template <class _CharT, class _Traits, class _RT>
6832 friend
6833 basic_istream<_CharT, _Traits>&
6834 operator>>(basic_istream<_CharT, _Traits>& __is,
6835 piecewise_linear_distribution<_RT>& __x);
6836};
6837
6838template<class _RealType>
Howard Hinnant71c410b2010-10-13 14:37:09 +00006839typename piecewise_linear_distribution<_RealType>::param_type &
6840piecewise_linear_distribution<_RealType>::param_type::operator=
6841 (const param_type& __rhs)
6842{
6843// These can throw
6844 __b_.reserve (__rhs.__b_.size ());
6845 __densities_.reserve(__rhs.__densities_.size());
6846 __areas_.reserve (__rhs.__areas_.size());
6847
6848// These can not throw
6849 __b_ = __rhs.__b_;
6850 __densities_ = __rhs.__densities_;
6851 __areas_ = __rhs.__areas_;
6852 return *this;
6853}
6854
6855
6856template<class _RealType>
Howard Hinnanta001ef32010-05-25 00:27:34 +00006857void
6858piecewise_linear_distribution<_RealType>::param_type::__init()
6859{
Howard Hinnant167159f2010-11-18 17:01:36 +00006860 __areas_.assign(__densities_.size() - 1, result_type());
Howard Hinnantc834c512011-11-29 18:15:50 +00006861 result_type _Sp = 0;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006862 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6863 {
6864 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6865 (__b_[__i+1] - __b_[__i]) * .5;
Howard Hinnantc834c512011-11-29 18:15:50 +00006866 _Sp += __areas_[__i];
Howard Hinnanta001ef32010-05-25 00:27:34 +00006867 }
6868 for (size_t __i = __areas_.size(); __i > 1;)
6869 {
6870 --__i;
Howard Hinnantc834c512011-11-29 18:15:50 +00006871 __areas_[__i] = __areas_[__i-1] / _Sp;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006872 }
6873 __areas_[0] = 0;
6874 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6875 __areas_[__i] += __areas_[__i-1];
6876 for (size_t __i = 0; __i < __densities_.size(); ++__i)
Howard Hinnantc834c512011-11-29 18:15:50 +00006877 __densities_[__i] /= _Sp;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006878}
6879
6880template<class _RealType>
6881piecewise_linear_distribution<_RealType>::param_type::param_type()
6882 : __b_(2),
6883 __densities_(2, 1.0),
6884 __areas_(1, 0.0)
6885{
6886 __b_[1] = 1;
6887}
6888
6889template<class _RealType>
6890template<class _InputIteratorB, class _InputIteratorW>
6891piecewise_linear_distribution<_RealType>::param_type::param_type(
6892 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6893 : __b_(__fB, __lB)
6894{
6895 if (__b_.size() < 2)
6896 {
6897 __b_.resize(2);
6898 __b_[0] = 0;
6899 __b_[1] = 1;
6900 __densities_.assign(2, 1.0);
6901 __areas_.assign(1, 0.0);
6902 }
6903 else
6904 {
6905 __densities_.reserve(__b_.size());
6906 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6907 __densities_.push_back(*__fW);
6908 __init();
6909 }
6910}
6911
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006912#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006913
Howard Hinnanta001ef32010-05-25 00:27:34 +00006914template<class _RealType>
6915template<class _UnaryOperation>
6916piecewise_linear_distribution<_RealType>::param_type::param_type(
6917 initializer_list<result_type> __bl, _UnaryOperation __fw)
6918 : __b_(__bl.begin(), __bl.end())
6919{
6920 if (__b_.size() < 2)
6921 {
6922 __b_.resize(2);
6923 __b_[0] = 0;
6924 __b_[1] = 1;
6925 __densities_.assign(2, 1.0);
6926 __areas_.assign(1, 0.0);
6927 }
6928 else
6929 {
6930 __densities_.reserve(__b_.size());
6931 for (size_t __i = 0; __i < __b_.size(); ++__i)
6932 __densities_.push_back(__fw(__b_[__i]));
6933 __init();
6934 }
6935}
6936
Eric Fiselier3b0b81f2017-04-19 00:23:45 +00006937#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006938
Howard Hinnanta001ef32010-05-25 00:27:34 +00006939template<class _RealType>
6940template<class _UnaryOperation>
6941piecewise_linear_distribution<_RealType>::param_type::param_type(
6942 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6943 : __b_(__nw == 0 ? 2 : __nw + 1)
6944{
6945 size_t __n = __b_.size() - 1;
6946 result_type __d = (__xmax - __xmin) / __n;
6947 __densities_.reserve(__b_.size());
6948 for (size_t __i = 0; __i < __n; ++__i)
6949 {
6950 __b_[__i] = __xmin + __i * __d;
6951 __densities_.push_back(__fw(__b_[__i]));
6952 }
6953 __b_[__n] = __xmax;
6954 __densities_.push_back(__fw(__b_[__n]));
6955 __init();
6956}
6957
6958template<class _RealType>
6959template<class _URNG>
6960_RealType
6961piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6962{
6963 typedef uniform_real_distribution<result_type> _Gen;
6964 result_type __u = _Gen()(__g);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006965 ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
Howard Hinnant167159f2010-11-18 17:01:36 +00006966 __u) - __p.__areas_.begin() - 1;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006967 __u -= __p.__areas_[__k];
Howard Hinnant167159f2010-11-18 17:01:36 +00006968 const result_type __dk = __p.__densities_[__k];
6969 const result_type __dk1 = __p.__densities_[__k+1];
6970 const result_type __deltad = __dk1 - __dk;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006971 const result_type __bk = __p.__b_[__k];
6972 if (__deltad == 0)
Howard Hinnant167159f2010-11-18 17:01:36 +00006973 return __u / __dk + __bk;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006974 const result_type __bk1 = __p.__b_[__k+1];
6975 const result_type __deltab = __bk1 - __bk;
Howard Hinnant167159f2010-11-18 17:01:36 +00006976 return (__bk * __dk1 - __bk1 * __dk +
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006977 _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
Howard Hinnant167159f2010-11-18 17:01:36 +00006978 __deltad;
Howard Hinnanta001ef32010-05-25 00:27:34 +00006979}
6980
6981template <class _CharT, class _Traits, class _RT>
6982basic_ostream<_CharT, _Traits>&
6983operator<<(basic_ostream<_CharT, _Traits>& __os,
6984 const piecewise_linear_distribution<_RT>& __x)
6985{
Howard Hinnant49e145e2012-10-30 19:06:59 +00006986 __save_flags<_CharT, _Traits> __lx(__os);
Louis Dionne3df65ce2020-10-15 13:27:27 -04006987 typedef basic_ostream<_CharT, _Traits> _OStream;
6988 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
6989 _OStream::scientific);
Howard Hinnanta001ef32010-05-25 00:27:34 +00006990 _CharT __sp = __os.widen(' ');
6991 __os.fill(__sp);
6992 size_t __n = __x.__p_.__b_.size();
6993 __os << __n;
6994 for (size_t __i = 0; __i < __n; ++__i)
6995 __os << __sp << __x.__p_.__b_[__i];
6996 __n = __x.__p_.__densities_.size();
6997 __os << __sp << __n;
6998 for (size_t __i = 0; __i < __n; ++__i)
6999 __os << __sp << __x.__p_.__densities_[__i];
7000 __n = __x.__p_.__areas_.size();
7001 __os << __sp << __n;
7002 for (size_t __i = 0; __i < __n; ++__i)
7003 __os << __sp << __x.__p_.__areas_[__i];
7004 return __os;
7005}
7006
7007template <class _CharT, class _Traits, class _RT>
7008basic_istream<_CharT, _Traits>&
7009operator>>(basic_istream<_CharT, _Traits>& __is,
7010 piecewise_linear_distribution<_RT>& __x)
7011{
7012 typedef piecewise_linear_distribution<_RT> _Eng;
7013 typedef typename _Eng::result_type result_type;
Howard Hinnant49e145e2012-10-30 19:06:59 +00007014 __save_flags<_CharT, _Traits> __lx(__is);
Louis Dionne3df65ce2020-10-15 13:27:27 -04007015 typedef basic_istream<_CharT, _Traits> _Istream;
7016 __is.flags(_Istream::dec | _Istream::skipws);
Howard Hinnanta001ef32010-05-25 00:27:34 +00007017 size_t __n;
7018 __is >> __n;
7019 vector<result_type> __b(__n);
7020 for (size_t __i = 0; __i < __n; ++__i)
7021 __is >> __b[__i];
7022 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00007023 vector<result_type> __densities(__n);
Howard Hinnanta001ef32010-05-25 00:27:34 +00007024 for (size_t __i = 0; __i < __n; ++__i)
7025 __is >> __densities[__i];
7026 __is >> __n;
Howard Hinnant167159f2010-11-18 17:01:36 +00007027 vector<result_type> __areas(__n);
Howard Hinnanta001ef32010-05-25 00:27:34 +00007028 for (size_t __i = 0; __i < __n; ++__i)
7029 __is >> __areas[__i];
7030 if (!__is.fail())
7031 {
7032 swap(__x.__p_.__b_, __b);
7033 swap(__x.__p_.__densities_, __densities);
7034 swap(__x.__p_.__areas_, __areas);
7035 }
7036 return __is;
7037}
7038
Howard Hinnantc51e1022010-05-11 19:42:16 +00007039_LIBCPP_END_NAMESPACE_STD
7040
Eric Fiselierf4433a32017-05-31 22:07:49 +00007041_LIBCPP_POP_MACROS
7042
Howard Hinnantc51e1022010-05-11 19:42:16 +00007043#endif // _LIBCPP_RANDOM