Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- random -----------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_RANDOM |
| 11 | #define _LIBCPP_RANDOM |
| 12 | |
| 13 | /* |
| 14 | random synopsis |
| 15 | |
| 16 | #include <initializer_list> |
| 17 | |
| 18 | namespace std |
| 19 | { |
Christopher Di Bella | 6807407 | 2021-02-17 02:52:17 +0000 | [diff] [blame] | 20 | // [rand.req.urng], uniform random bit generator requirements |
| 21 | template<class G> |
| 22 | concept uniform_random_bit_generator = see below; // C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | |
| 24 | // Engines |
| 25 | |
| 26 | template <class UIntType, UIntType a, UIntType c, UIntType m> |
| 27 | class linear_congruential_engine |
| 28 | { |
| 29 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 42 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 45 | 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 | |
| 54 | template <class UIntType, UIntType a, UIntType c, UIntType m> |
| 55 | bool |
| 56 | operator==(const linear_congruential_engine<UIntType, a, c, m>& x, |
| 57 | const linear_congruential_engine<UIntType, a, c, m>& y); |
| 58 | |
| 59 | template <class UIntType, UIntType a, UIntType c, UIntType m> |
| 60 | bool |
| 61 | operator!=(const linear_congruential_engine<UIntType, a, c, m>& x, |
| 62 | const linear_congruential_engine<UIntType, a, c, m>& y); |
| 63 | |
| 64 | template <class charT, class traits, |
| 65 | class UIntType, UIntType a, UIntType c, UIntType m> |
| 66 | basic_ostream<charT, traits>& |
| 67 | operator<<(basic_ostream<charT, traits>& os, |
| 68 | const linear_congruential_engine<UIntType, a, c, m>& x); |
| 69 | |
| 70 | template <class charT, class traits, |
| 71 | class UIntType, UIntType a, UIntType c, UIntType m> |
| 72 | basic_istream<charT, traits>& |
| 73 | operator>>(basic_istream<charT, traits>& is, |
| 74 | linear_congruential_engine<UIntType, a, c, m>& x); |
| 75 | |
| 76 | template <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> |
| 79 | class mersenne_twister_engine |
| 80 | { |
| 81 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 104 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 107 | 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 | |
| 116 | template <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> |
| 119 | bool |
| 120 | operator==( |
| 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 | |
| 124 | template <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> |
| 127 | bool |
| 128 | operator!=( |
| 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 | |
| 132 | template <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> |
| 136 | basic_ostream<charT, traits>& |
| 137 | operator<<(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 | |
| 140 | template <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> |
| 144 | basic_istream<charT, traits>& |
| 145 | operator>>(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 | |
| 148 | template<class UIntType, size_t w, size_t s, size_t r> |
| 149 | class subtract_with_carry_engine |
| 150 | { |
| 151 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 164 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 167 | 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 | |
| 176 | template<class UIntType, size_t w, size_t s, size_t r> |
| 177 | bool |
| 178 | operator==( |
| 179 | const subtract_with_carry_engine<UIntType, w, s, r>& x, |
| 180 | const subtract_with_carry_engine<UIntType, w, s, r>& y); |
| 181 | |
| 182 | template<class UIntType, size_t w, size_t s, size_t r> |
| 183 | bool |
| 184 | operator!=( |
| 185 | const subtract_with_carry_engine<UIntType, w, s, r>& x, |
| 186 | const subtract_with_carry_engine<UIntType, w, s, r>& y); |
| 187 | |
| 188 | template <class charT, class traits, |
| 189 | class UIntType, size_t w, size_t s, size_t r> |
| 190 | basic_ostream<charT, traits>& |
| 191 | operator<<(basic_ostream<charT, traits>& os, |
| 192 | const subtract_with_carry_engine<UIntType, w, s, r>& x); |
| 193 | |
| 194 | template <class charT, class traits, |
| 195 | class UIntType, size_t w, size_t s, size_t r> |
| 196 | basic_istream<charT, traits>& |
| 197 | operator>>(basic_istream<charT, traits>& is, |
| 198 | subtract_with_carry_engine<UIntType, w, s, r>& x); |
| 199 | |
| 200 | template<class Engine, size_t p, size_t r> |
| 201 | class discard_block_engine |
| 202 | { |
| 203 | public: |
| 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 Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 228 | const Engine& base() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | template<class Engine, size_t p, size_t r> |
| 232 | bool |
| 233 | operator==( |
| 234 | const discard_block_engine<Engine, p, r>& x, |
| 235 | const discard_block_engine<Engine, p, r>& y); |
| 236 | |
| 237 | template<class Engine, size_t p, size_t r> |
| 238 | bool |
| 239 | operator!=( |
| 240 | const discard_block_engine<Engine, p, r>& x, |
| 241 | const discard_block_engine<Engine, p, r>& y); |
| 242 | |
| 243 | template <class charT, class traits, |
| 244 | class Engine, size_t p, size_t r> |
| 245 | basic_ostream<charT, traits>& |
| 246 | operator<<(basic_ostream<charT, traits>& os, |
| 247 | const discard_block_engine<Engine, p, r>& x); |
| 248 | |
| 249 | template <class charT, class traits, |
| 250 | class Engine, size_t p, size_t r> |
| 251 | basic_istream<charT, traits>& |
| 252 | operator>>(basic_istream<charT, traits>& is, |
| 253 | discard_block_engine<Engine, p, r>& x); |
| 254 | |
| 255 | template<class Engine, size_t w, class UIntType> |
| 256 | class independent_bits_engine |
| 257 | { |
| 258 | public: |
| 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 Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 280 | const Engine& base() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | template<class Engine, size_t w, class UIntType> |
| 284 | bool |
| 285 | operator==( |
| 286 | const independent_bits_engine<Engine, w, UIntType>& x, |
| 287 | const independent_bits_engine<Engine, w, UIntType>& y); |
| 288 | |
| 289 | template<class Engine, size_t w, class UIntType> |
| 290 | bool |
| 291 | operator!=( |
| 292 | const independent_bits_engine<Engine, w, UIntType>& x, |
| 293 | const independent_bits_engine<Engine, w, UIntType>& y); |
| 294 | |
| 295 | template <class charT, class traits, |
| 296 | class Engine, size_t w, class UIntType> |
| 297 | basic_ostream<charT, traits>& |
| 298 | operator<<(basic_ostream<charT, traits>& os, |
| 299 | const independent_bits_engine<Engine, w, UIntType>& x); |
| 300 | |
| 301 | template <class charT, class traits, |
| 302 | class Engine, size_t w, class UIntType> |
| 303 | basic_istream<charT, traits>& |
| 304 | operator>>(basic_istream<charT, traits>& is, |
| 305 | independent_bits_engine<Engine, w, UIntType>& x); |
| 306 | |
| 307 | template<class Engine, size_t k> |
| 308 | class shuffle_order_engine |
| 309 | { |
| 310 | public: |
| 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 Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 334 | const Engine& base() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | template<class Engine, size_t k> |
| 338 | bool |
| 339 | operator==( |
| 340 | const shuffle_order_engine<Engine, k>& x, |
| 341 | const shuffle_order_engine<Engine, k>& y); |
| 342 | |
| 343 | template<class Engine, size_t k> |
| 344 | bool |
| 345 | operator!=( |
| 346 | const shuffle_order_engine<Engine, k>& x, |
| 347 | const shuffle_order_engine<Engine, k>& y); |
| 348 | |
| 349 | template <class charT, class traits, |
| 350 | class Engine, size_t k> |
| 351 | basic_ostream<charT, traits>& |
| 352 | operator<<(basic_ostream<charT, traits>& os, |
| 353 | const shuffle_order_engine<Engine, k>& x); |
| 354 | |
| 355 | template <class charT, class traits, |
| 356 | class Engine, size_t k> |
| 357 | basic_istream<charT, traits>& |
| 358 | operator>>(basic_istream<charT, traits>& is, |
| 359 | shuffle_order_engine<Engine, k>& x); |
| 360 | |
| 361 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> |
| 362 | minstd_rand0; |
| 363 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> |
| 364 | minstd_rand; |
| 365 | typedef 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; |
| 371 | typedef 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; |
| 377 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base; |
| 378 | typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base; |
| 379 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
| 380 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
| 381 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 382 | typedef minstd_rand default_random_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 383 | |
| 384 | // Generators |
| 385 | |
| 386 | class random_device |
| 387 | { |
| 388 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 397 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 400 | |
| 401 | // generating functions |
| 402 | result_type operator()(); |
| 403 | |
| 404 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 405 | double entropy() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 406 | |
| 407 | // no copy functions |
| 408 | random_device(const random_device& ) = delete; |
| 409 | void operator=(const random_device& ) = delete; |
| 410 | }; |
| 411 | |
| 412 | // Utilities |
| 413 | |
| 414 | class seed_seq |
| 415 | { |
| 416 | public: |
| 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 | |
| 441 | template<class RealType, size_t bits, class URNG> |
| 442 | RealType generate_canonical(URNG& g); |
| 443 | |
| 444 | // Distributions |
| 445 | |
| 446 | template<class IntType = int> |
| 447 | class uniform_int_distribution |
| 448 | { |
| 449 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 470 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 474 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 501 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 502 | 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 | |
| 509 | template<class RealType = double> |
| 510 | class uniform_real_distribution |
| 511 | { |
| 512 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 532 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 535 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 562 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 563 | 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 | |
| 570 | class bernoulli_distribution |
| 571 | { |
| 572 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 590 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 593 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 619 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 620 | 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 | |
| 627 | template<class IntType = int> |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 628 | class binomial_distribution |
| 629 | { |
| 630 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 649 | 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 Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 652 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 679 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 680 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 686 | |
| 687 | template<class IntType = int> |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 688 | class geometric_distribution |
| 689 | { |
| 690 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 708 | 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 Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 711 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 737 | |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 738 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 744 | |
| 745 | template<class IntType = int> |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 746 | class negative_binomial_distribution |
| 747 | { |
| 748 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 767 | 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 Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 770 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 797 | |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 798 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 804 | |
| 805 | template<class IntType = int> |
Howard Hinnant | 24a5f2a | 2010-05-14 21:38:54 +0000 | [diff] [blame] | 806 | class poisson_distribution |
| 807 | { |
| 808 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 826 | 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 Hinnant | 24a5f2a | 2010-05-14 21:38:54 +0000 | [diff] [blame] | 829 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 855 | |
Howard Hinnant | 24a5f2a | 2010-05-14 21:38:54 +0000 | [diff] [blame] | 856 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 862 | |
| 863 | template<class RealType = double> |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 864 | class exponential_distribution |
| 865 | { |
| 866 | public: |
| 867 | // types |
| 868 | typedef RealType result_type; |
| 869 | |
| 870 | class param_type |
| 871 | { |
| 872 | public: |
| 873 | typedef exponential_distribution distribution_type; |
| 874 | |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 875 | explicit param_type(result_type lambda = 1.0); |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 876 | |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 877 | result_type lambda() const; |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 878 | |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 884 | 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 Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 887 | 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 Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 895 | result_type lambda() const; |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 896 | |
| 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 913 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 914 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 920 | |
| 921 | template<class RealType = double> |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 922 | class gamma_distribution |
| 923 | { |
| 924 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 943 | 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 Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 946 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 973 | |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 974 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 980 | |
| 981 | template<class RealType = double> |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 982 | class weibull_distribution |
| 983 | { |
| 984 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 1003 | 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 Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 1006 | 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 Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 1023 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1033 | |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 1034 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1040 | |
| 1041 | template<class RealType = double> |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 1042 | class extreme_value_distribution |
| 1043 | { |
| 1044 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 1063 | 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 Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 1066 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1093 | |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 1094 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1100 | |
| 1101 | template<class RealType = double> |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 1102 | class normal_distribution |
| 1103 | { |
| 1104 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 1123 | 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 Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 1126 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1153 | |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 1154 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1160 | |
| 1161 | template<class RealType = double> |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 1162 | class lognormal_distribution |
| 1163 | { |
| 1164 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 1183 | 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 Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 1186 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1213 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 1214 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1220 | |
| 1221 | template<class RealType = double> |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1222 | class chi_squared_distribution |
| 1223 | { |
| 1224 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 1242 | 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 Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1245 | 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 Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1261 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1271 | |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1272 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1278 | |
| 1279 | template<class RealType = double> |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 1280 | class cauchy_distribution |
| 1281 | { |
| 1282 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 1301 | 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 Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 1304 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1331 | |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 1332 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1338 | |
| 1339 | template<class RealType = double> |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1340 | class fisher_f_distribution |
| 1341 | { |
| 1342 | public: |
| 1343 | // types |
| 1344 | typedef RealType result_type; |
| 1345 | |
| 1346 | class param_type |
| 1347 | { |
| 1348 | public: |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1349 | typedef fisher_f_distribution distribution_type; |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1350 | |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 1361 | 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 Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1364 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1391 | |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1392 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1398 | |
| 1399 | template<class RealType = double> |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1400 | class student_t_distribution |
| 1401 | { |
| 1402 | public: |
| 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 Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 1420 | 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 Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1423 | 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1449 | |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1450 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1456 | |
| 1457 | template<class IntType = int> |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1458 | class discrete_distribution |
| 1459 | { |
| 1460 | public: |
| 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1516 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1517 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1523 | |
| 1524 | template<class RealType = double> |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1525 | class 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 Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1546 | vector<result_type> densities() const; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1547 | |
| 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 Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1573 | vector<result_type> densities() const; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1574 | |
| 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1591 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1592 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1598 | |
| 1599 | template<class RealType = double> |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1600 | class 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 Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1621 | vector<result_type> densities() const; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1622 | |
| 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1633 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1634 | 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 Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1651 | vector<result_type> densities() const; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1652 | |
| 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 Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1669 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1670 | 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 Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1676 | |
| 1677 | } // std |
| 1678 | */ |
| 1679 | |
| 1680 | #include <__config> |
Louis Dionne | a60dd87 | 2021-06-17 11:30:11 -0400 | [diff] [blame] | 1681 | #include <__random/uniform_int_distribution.h> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 1682 | #include <algorithm> |
Eric Fiselier | 90adc20 | 2015-01-23 22:22:36 +0000 | [diff] [blame] | 1683 | #include <cmath> |
Christopher Di Bella | 6807407 | 2021-02-17 02:52:17 +0000 | [diff] [blame] | 1684 | #include <concepts> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 1685 | #include <cstddef> |
| 1686 | #include <cstdint> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1687 | #include <initializer_list> |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 1688 | #include <iosfwd> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 1689 | #include <limits> |
| 1690 | #include <numeric> |
| 1691 | #include <string> |
| 1692 | #include <type_traits> |
| 1693 | #include <vector> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1694 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 1695 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1696 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 1697 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1698 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 1699 | _LIBCPP_PUSH_MACROS |
| 1700 | #include <__undef_macros> |
| 1701 | |
| 1702 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1703 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 1704 | |
Christopher Di Bella | 8811002 | 2021-02-19 01:54:52 +0000 | [diff] [blame] | 1705 | #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS) |
Christopher Di Bella | 6807407 | 2021-02-17 02:52:17 +0000 | [diff] [blame] | 1706 | |
| 1707 | // [rand.req.urng] |
| 1708 | template<class _Gen> |
| 1709 | concept uniform_random_bit_generator = |
| 1710 | invocable<_Gen&> && unsigned_integral<invoke_result_t<_Gen&>> && |
| 1711 | requires { |
| 1712 | { _Gen::min() } -> same_as<invoke_result_t<_Gen&>>; |
| 1713 | { _Gen::max() } -> same_as<invoke_result_t<_Gen&>>; |
| 1714 | requires bool_constant<(_Gen::min() < _Gen::max())>::value; |
| 1715 | }; |
| 1716 | |
Christopher Di Bella | 8811002 | 2021-02-19 01:54:52 +0000 | [diff] [blame] | 1717 | #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS) |
Christopher Di Bella | 6807407 | 2021-02-17 02:52:17 +0000 | [diff] [blame] | 1718 | |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1719 | // __is_seed_sequence |
| 1720 | |
| 1721 | template <class _Sseq, class _Engine> |
| 1722 | struct __is_seed_sequence |
| 1723 | { |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1724 | static _LIBCPP_CONSTEXPR const bool value = |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1725 | !is_convertible<_Sseq, typename _Engine::result_type>::value && |
| 1726 | !is_same<typename remove_cv<_Sseq>::type, _Engine>::value; |
| 1727 | }; |
| 1728 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1729 | // linear_congruential_engine |
| 1730 | |
| 1731 | template <unsigned long long __a, unsigned long long __c, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1732 | unsigned long long __m, unsigned long long _Mp, |
zoecarver | 68c3702 | 2020-11-10 18:23:22 -0800 | [diff] [blame] | 1733 | bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_Mp-__c)/__a), |
| 1734 | bool _OverflowOK = ((__m|__m-1) > __m), // m = 2^n |
| 1735 | bool _SchrageOK = (__a != 0 && __m != 0 && __m % __a <= __m / __a)> // r <= q |
| 1736 | struct __lce_alg_picker |
| 1737 | { |
| 1738 | static_assert(__a != 0 || __m != 0 || !_MightOverflow || _OverflowOK || _SchrageOK, |
| 1739 | "The current values of a, c, and m cannot generate a number " |
| 1740 | "within bounds of linear_congruential_engine."); |
| 1741 | |
| 1742 | static _LIBCPP_CONSTEXPR const bool __use_schrage = _MightOverflow && |
| 1743 | !_OverflowOK && |
| 1744 | _SchrageOK; |
| 1745 | }; |
| 1746 | |
| 1747 | template <unsigned long long __a, unsigned long long __c, |
| 1748 | unsigned long long __m, unsigned long long _Mp, |
| 1749 | bool _UseSchrage = __lce_alg_picker<__a, __c, __m, _Mp>::__use_schrage> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1750 | struct __lce_ta; |
| 1751 | |
| 1752 | // 64 |
| 1753 | |
| 1754 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 1755 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true> |
| 1756 | { |
| 1757 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1758 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1759 | static result_type next(result_type __x) |
| 1760 | { |
| 1761 | // Schrage's algorithm |
| 1762 | const result_type __q = __m / __a; |
| 1763 | const result_type __r = __m % __a; |
| 1764 | const result_type __t0 = __a * (__x % __q); |
| 1765 | const result_type __t1 = __r * (__x / __q); |
| 1766 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1767 | __x += __c - (__x >= __m - __c) * __m; |
| 1768 | return __x; |
| 1769 | } |
| 1770 | }; |
| 1771 | |
| 1772 | template <unsigned long long __a, unsigned long long __m> |
| 1773 | struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true> |
| 1774 | { |
| 1775 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1776 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1777 | static result_type next(result_type __x) |
| 1778 | { |
| 1779 | // Schrage's algorithm |
| 1780 | const result_type __q = __m / __a; |
| 1781 | const result_type __r = __m % __a; |
| 1782 | const result_type __t0 = __a * (__x % __q); |
| 1783 | const result_type __t1 = __r * (__x / __q); |
| 1784 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1785 | return __x; |
| 1786 | } |
| 1787 | }; |
| 1788 | |
| 1789 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 1790 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false> |
| 1791 | { |
| 1792 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1793 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1794 | static result_type next(result_type __x) |
| 1795 | { |
| 1796 | return (__a * __x + __c) % __m; |
| 1797 | } |
| 1798 | }; |
| 1799 | |
| 1800 | template <unsigned long long __a, unsigned long long __c> |
| 1801 | struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false> |
| 1802 | { |
| 1803 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1804 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1805 | static result_type next(result_type __x) |
| 1806 | { |
| 1807 | return __a * __x + __c; |
| 1808 | } |
| 1809 | }; |
| 1810 | |
| 1811 | // 32 |
| 1812 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1813 | template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp> |
| 1814 | struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), true> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1815 | { |
| 1816 | typedef unsigned result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1817 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1818 | static result_type next(result_type __x) |
| 1819 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1820 | const result_type __a = static_cast<result_type>(_Ap); |
| 1821 | const result_type __c = static_cast<result_type>(_Cp); |
| 1822 | const result_type __m = static_cast<result_type>(_Mp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1823 | // Schrage's algorithm |
| 1824 | const result_type __q = __m / __a; |
| 1825 | const result_type __r = __m % __a; |
| 1826 | const result_type __t0 = __a * (__x % __q); |
| 1827 | const result_type __t1 = __r * (__x / __q); |
| 1828 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1829 | __x += __c - (__x >= __m - __c) * __m; |
| 1830 | return __x; |
| 1831 | } |
| 1832 | }; |
| 1833 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1834 | template <unsigned long long _Ap, unsigned long long _Mp> |
| 1835 | struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1836 | { |
| 1837 | typedef unsigned result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1838 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1839 | static result_type next(result_type __x) |
| 1840 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1841 | const result_type __a = static_cast<result_type>(_Ap); |
| 1842 | const result_type __m = static_cast<result_type>(_Mp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1843 | // Schrage's algorithm |
| 1844 | const result_type __q = __m / __a; |
| 1845 | const result_type __r = __m % __a; |
| 1846 | const result_type __t0 = __a * (__x % __q); |
| 1847 | const result_type __t1 = __r * (__x / __q); |
| 1848 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1849 | return __x; |
| 1850 | } |
| 1851 | }; |
| 1852 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1853 | template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp> |
| 1854 | struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), false> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1855 | { |
| 1856 | typedef unsigned result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1857 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1858 | static result_type next(result_type __x) |
| 1859 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1860 | const result_type __a = static_cast<result_type>(_Ap); |
| 1861 | const result_type __c = static_cast<result_type>(_Cp); |
| 1862 | const result_type __m = static_cast<result_type>(_Mp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1863 | return (__a * __x + __c) % __m; |
| 1864 | } |
| 1865 | }; |
| 1866 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1867 | template <unsigned long long _Ap, unsigned long long _Cp> |
| 1868 | struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1869 | { |
| 1870 | typedef unsigned result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1871 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1872 | static result_type next(result_type __x) |
| 1873 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1874 | const result_type __a = static_cast<result_type>(_Ap); |
| 1875 | const result_type __c = static_cast<result_type>(_Cp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1876 | return __a * __x + __c; |
| 1877 | } |
| 1878 | }; |
| 1879 | |
| 1880 | // 16 |
| 1881 | |
| 1882 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b> |
| 1883 | struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b> |
| 1884 | { |
| 1885 | typedef unsigned short result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1886 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1887 | static result_type next(result_type __x) |
| 1888 | { |
| 1889 | return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x)); |
| 1890 | } |
| 1891 | }; |
| 1892 | |
| 1893 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1894 | class _LIBCPP_TEMPLATE_VIS linear_congruential_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1895 | |
| 1896 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1897 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 1898 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1899 | basic_ostream<_CharT, _Traits>& |
| 1900 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1901 | const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1902 | |
| 1903 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1904 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1905 | basic_istream<_CharT, _Traits>& |
| 1906 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1907 | linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1908 | |
| 1909 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1910 | class _LIBCPP_TEMPLATE_VIS linear_congruential_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1911 | { |
| 1912 | public: |
| 1913 | // types |
| 1914 | typedef _UIntType result_type; |
| 1915 | |
Howard Hinnant | a741b24 | 2013-05-21 21:19:35 +0000 | [diff] [blame] | 1916 | private: |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1917 | result_type __x_; |
| 1918 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1919 | static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1920 | |
| 1921 | static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters"); |
| 1922 | static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters"); |
Arthur O'Dwyer | 101ce07 | 2021-05-10 15:32:38 -0400 | [diff] [blame] | 1923 | static_assert(is_unsigned<_UIntType>::value, "_UIntType must be unsigned type"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1924 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1925 | static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u: 0u; |
| 1926 | static _LIBCPP_CONSTEXPR const result_type _Max = __m - 1u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1927 | static_assert(_Min < _Max, "linear_congruential_engine invalid parameters"); |
| 1928 | |
| 1929 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1930 | static _LIBCPP_CONSTEXPR const result_type multiplier = __a; |
| 1931 | static _LIBCPP_CONSTEXPR const result_type increment = __c; |
| 1932 | static _LIBCPP_CONSTEXPR const result_type modulus = __m; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1933 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1934 | static _LIBCPP_CONSTEXPR result_type min() {return _Min;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1935 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1936 | static _LIBCPP_CONSTEXPR result_type max() {return _Max;} |
| 1937 | static _LIBCPP_CONSTEXPR const result_type default_seed = 1u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1938 | |
| 1939 | // constructors and seeding functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 1940 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1941 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 1942 | linear_congruential_engine() : linear_congruential_engine(default_seed) {} |
| 1943 | _LIBCPP_INLINE_VISIBILITY |
| 1944 | explicit linear_congruential_engine(result_type __s) { seed(__s); } |
| 1945 | #else |
| 1946 | _LIBCPP_INLINE_VISIBILITY |
| 1947 | explicit linear_congruential_engine(result_type __s = default_seed) { |
| 1948 | seed(__s); |
| 1949 | } |
| 1950 | #endif |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1951 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1952 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1953 | explicit linear_congruential_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1954 | typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1955 | {seed(__q);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1956 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1957 | void seed(result_type __s = default_seed) |
| 1958 | {seed(integral_constant<bool, __m == 0>(), |
| 1959 | integral_constant<bool, __c == 0>(), __s);} |
| 1960 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1961 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1962 | typename enable_if |
| 1963 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1964 | __is_seed_sequence<_Sseq, linear_congruential_engine>::value, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1965 | void |
| 1966 | >::type |
| 1967 | seed(_Sseq& __q) |
| 1968 | {__seed(__q, integral_constant<unsigned, |
| 1969 | 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32 |
Howard Hinnant | a0c4f7c | 2013-05-21 21:05:12 +0000 | [diff] [blame] | 1970 | : (__m > 0x100000000ull))>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1971 | |
| 1972 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1973 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1974 | result_type operator()() |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1975 | {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1976 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1977 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 1978 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1979 | friend _LIBCPP_INLINE_VISIBILITY |
| 1980 | bool operator==(const linear_congruential_engine& __x, |
| 1981 | const linear_congruential_engine& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1982 | {return __x.__x_ == __y.__x_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1983 | friend _LIBCPP_INLINE_VISIBILITY |
| 1984 | bool operator!=(const linear_congruential_engine& __x, |
| 1985 | const linear_congruential_engine& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1986 | {return !(__x == __y);} |
| 1987 | |
| 1988 | private: |
| 1989 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1990 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1991 | void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1992 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1993 | void seed(true_type, false_type, result_type __s) {__x_ = __s;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1994 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1995 | void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ? |
| 1996 | 1 : __s % __m;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1997 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1998 | void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;} |
| 1999 | |
| 2000 | template<class _Sseq> |
| 2001 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 2002 | template<class _Sseq> |
| 2003 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 2004 | |
| 2005 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2006 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2007 | friend |
| 2008 | basic_ostream<_CharT, _Traits>& |
| 2009 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2010 | const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2011 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2012 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2013 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2014 | friend |
| 2015 | basic_istream<_CharT, _Traits>& |
| 2016 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2017 | linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2018 | }; |
| 2019 | |
| 2020 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2021 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 2022 | linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier; |
| 2023 | |
| 2024 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 2025 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 2026 | linear_congruential_engine<_UIntType, __a, __c, __m>::increment; |
| 2027 | |
| 2028 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 2029 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 2030 | linear_congruential_engine<_UIntType, __a, __c, __m>::modulus; |
| 2031 | |
| 2032 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 2033 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 2034 | linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed; |
| 2035 | |
| 2036 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2037 | template<class _Sseq> |
| 2038 | void |
| 2039 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 2040 | integral_constant<unsigned, 1>) |
| 2041 | { |
| 2042 | const unsigned __k = 1; |
| 2043 | uint32_t __ar[__k+3]; |
| 2044 | __q.generate(__ar, __ar + __k + 3); |
| 2045 | result_type __s = static_cast<result_type>(__ar[3] % __m); |
| 2046 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 2047 | } |
| 2048 | |
| 2049 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 2050 | template<class _Sseq> |
| 2051 | void |
| 2052 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 2053 | integral_constant<unsigned, 2>) |
| 2054 | { |
| 2055 | const unsigned __k = 2; |
| 2056 | uint32_t __ar[__k+3]; |
| 2057 | __q.generate(__ar, __ar + __k + 3); |
| 2058 | result_type __s = static_cast<result_type>((__ar[3] + |
Howard Hinnant | a0c4f7c | 2013-05-21 21:05:12 +0000 | [diff] [blame] | 2059 | ((uint64_t)__ar[4] << 32)) % __m); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2060 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 2061 | } |
| 2062 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2063 | template <class _CharT, class _Traits, |
| 2064 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2065 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2066 | basic_ostream<_CharT, _Traits>& |
| 2067 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2068 | const linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 2069 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2070 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2071 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
| 2072 | __os.flags(_Ostream::dec | _Ostream::left); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2073 | __os.fill(__os.widen(' ')); |
| 2074 | return __os << __x.__x_; |
| 2075 | } |
| 2076 | |
| 2077 | template <class _CharT, class _Traits, |
| 2078 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 2079 | basic_istream<_CharT, _Traits>& |
| 2080 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2081 | linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 2082 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2083 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2084 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 2085 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2086 | _UIntType __t; |
| 2087 | __is >> __t; |
| 2088 | if (!__is.fail()) |
| 2089 | __x.__x_ = __t; |
| 2090 | return __is; |
| 2091 | } |
| 2092 | |
| 2093 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> |
| 2094 | minstd_rand0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2095 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> |
| 2096 | minstd_rand; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 2097 | typedef minstd_rand default_random_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2098 | // mersenne_twister_engine |
| 2099 | |
| 2100 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2101 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2102 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2103 | class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2104 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2105 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2106 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2107 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2108 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2109 | operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2110 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2111 | const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2112 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2113 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2114 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2115 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2116 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 2117 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2118 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2119 | operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2120 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2121 | const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2122 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2123 | |
| 2124 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2125 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2126 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2127 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2128 | basic_ostream<_CharT, _Traits>& |
| 2129 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2130 | const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2131 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2132 | |
| 2133 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2134 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2135 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2136 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2137 | basic_istream<_CharT, _Traits>& |
| 2138 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2139 | mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2140 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2141 | |
| 2142 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2143 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2144 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2145 | class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2146 | { |
| 2147 | public: |
| 2148 | // types |
| 2149 | typedef _UIntType result_type; |
| 2150 | |
| 2151 | private: |
| 2152 | result_type __x_[__n]; |
| 2153 | size_t __i_; |
| 2154 | |
| 2155 | static_assert( 0 < __m, "mersenne_twister_engine invalid parameters"); |
| 2156 | static_assert(__m <= __n, "mersenne_twister_engine invalid parameters"); |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2157 | static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2158 | static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters"); |
| 2159 | static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters"); |
| 2160 | static_assert(__r <= __w, "mersenne_twister_engine invalid parameters"); |
| 2161 | static_assert(__u <= __w, "mersenne_twister_engine invalid parameters"); |
| 2162 | static_assert(__s <= __w, "mersenne_twister_engine invalid parameters"); |
| 2163 | static_assert(__t <= __w, "mersenne_twister_engine invalid parameters"); |
| 2164 | static_assert(__l <= __w, "mersenne_twister_engine invalid parameters"); |
| 2165 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2166 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 2167 | static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2168 | (result_type(1) << __w) - result_type(1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2169 | static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters"); |
| 2170 | static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2171 | static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2172 | static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2173 | static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2174 | static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2175 | |
| 2176 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2177 | static _LIBCPP_CONSTEXPR const size_t word_size = __w; |
| 2178 | static _LIBCPP_CONSTEXPR const size_t state_size = __n; |
| 2179 | static _LIBCPP_CONSTEXPR const size_t shift_size = __m; |
| 2180 | static _LIBCPP_CONSTEXPR const size_t mask_bits = __r; |
| 2181 | static _LIBCPP_CONSTEXPR const result_type xor_mask = __a; |
| 2182 | static _LIBCPP_CONSTEXPR const size_t tempering_u = __u; |
| 2183 | static _LIBCPP_CONSTEXPR const result_type tempering_d = __d; |
| 2184 | static _LIBCPP_CONSTEXPR const size_t tempering_s = __s; |
| 2185 | static _LIBCPP_CONSTEXPR const result_type tempering_b = __b; |
| 2186 | static _LIBCPP_CONSTEXPR const size_t tempering_t = __t; |
| 2187 | static _LIBCPP_CONSTEXPR const result_type tempering_c = __c; |
| 2188 | static _LIBCPP_CONSTEXPR const size_t tempering_l = __l; |
| 2189 | static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2190 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2191 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2192 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2193 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
| 2194 | static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2195 | |
| 2196 | // constructors and seeding functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 2197 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2198 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 2199 | mersenne_twister_engine() : mersenne_twister_engine(default_seed) {} |
| 2200 | _LIBCPP_INLINE_VISIBILITY |
| 2201 | explicit mersenne_twister_engine(result_type __sd) { seed(__sd); } |
| 2202 | #else |
| 2203 | _LIBCPP_INLINE_VISIBILITY |
| 2204 | explicit mersenne_twister_engine(result_type __sd = default_seed) { |
| 2205 | seed(__sd); |
| 2206 | } |
| 2207 | #endif |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2208 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2209 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2210 | explicit mersenne_twister_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2211 | typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2212 | {seed(__q);} |
| 2213 | void seed(result_type __sd = default_seed); |
| 2214 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2215 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2216 | typename enable_if |
| 2217 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2218 | __is_seed_sequence<_Sseq, mersenne_twister_engine>::value, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2219 | void |
| 2220 | >::type |
| 2221 | seed(_Sseq& __q) |
| 2222 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2223 | |
| 2224 | // generating functions |
| 2225 | result_type operator()(); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2226 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2227 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2228 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2229 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2230 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2231 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2232 | friend |
| 2233 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2234 | operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2235 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2236 | const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2237 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2238 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2239 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2240 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2241 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2242 | friend |
| 2243 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2244 | operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2245 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2246 | const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2247 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2248 | |
| 2249 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2250 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2251 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2252 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2253 | friend |
| 2254 | basic_ostream<_CharT, _Traits>& |
| 2255 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2256 | const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2257 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2258 | |
| 2259 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2260 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2261 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2262 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2263 | friend |
| 2264 | basic_istream<_CharT, _Traits>& |
| 2265 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2266 | mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2267 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2268 | private: |
| 2269 | |
| 2270 | template<class _Sseq> |
| 2271 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 2272 | template<class _Sseq> |
| 2273 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 2274 | |
| 2275 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2276 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2277 | static |
| 2278 | typename enable_if |
| 2279 | < |
| 2280 | __count < __w, |
| 2281 | result_type |
| 2282 | >::type |
| 2283 | __lshift(result_type __x) {return (__x << __count) & _Max;} |
| 2284 | |
| 2285 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2286 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2287 | static |
| 2288 | typename enable_if |
| 2289 | < |
| 2290 | (__count >= __w), |
| 2291 | result_type |
| 2292 | >::type |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2293 | __lshift(result_type) {return result_type(0);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2294 | |
| 2295 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2296 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2297 | static |
| 2298 | typename enable_if |
| 2299 | < |
| 2300 | __count < _Dt, |
| 2301 | result_type |
| 2302 | >::type |
| 2303 | __rshift(result_type __x) {return __x >> __count;} |
| 2304 | |
| 2305 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2306 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2307 | static |
| 2308 | typename enable_if |
| 2309 | < |
| 2310 | (__count >= _Dt), |
| 2311 | result_type |
| 2312 | >::type |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2313 | __rshift(result_type) {return result_type(0);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2314 | }; |
| 2315 | |
| 2316 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2317 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2318 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2319 | _LIBCPP_CONSTEXPR const size_t |
| 2320 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size; |
| 2321 | |
| 2322 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2323 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2324 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2325 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2326 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size; |
| 2327 | |
| 2328 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2329 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2330 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2331 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2332 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size; |
| 2333 | |
| 2334 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2335 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2336 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2337 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2338 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits; |
| 2339 | |
| 2340 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2341 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2342 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2343 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2344 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask; |
| 2345 | |
| 2346 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2347 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2348 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2349 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2350 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u; |
| 2351 | |
| 2352 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2353 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2354 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2355 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2356 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d; |
| 2357 | |
| 2358 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2359 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2360 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2361 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2362 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s; |
| 2363 | |
| 2364 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2365 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2366 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2367 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2368 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b; |
| 2369 | |
| 2370 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2371 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2372 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2373 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2374 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t; |
| 2375 | |
| 2376 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2377 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2378 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2379 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2380 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c; |
| 2381 | |
| 2382 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2383 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2384 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2385 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2386 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l; |
| 2387 | |
| 2388 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2389 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2390 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2391 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2392 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier; |
| 2393 | |
| 2394 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2395 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2396 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2397 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2398 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed; |
| 2399 | |
| 2400 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2401 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2402 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2403 | void |
| 2404 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2405 | __t, __c, __l, __f>::seed(result_type __sd) |
Marshall Clow | c189463 | 2017-09-11 18:10:33 +0000 | [diff] [blame] | 2406 | _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2407 | { // __w >= 2 |
| 2408 | __x_[0] = __sd & _Max; |
| 2409 | for (size_t __i = 1; __i < __n; ++__i) |
| 2410 | __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max; |
| 2411 | __i_ = 0; |
| 2412 | } |
| 2413 | |
| 2414 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2415 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2416 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2417 | template<class _Sseq> |
| 2418 | void |
| 2419 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2420 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>) |
| 2421 | { |
| 2422 | const unsigned __k = 1; |
| 2423 | uint32_t __ar[__n * __k]; |
| 2424 | __q.generate(__ar, __ar + __n * __k); |
| 2425 | for (size_t __i = 0; __i < __n; ++__i) |
| 2426 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 2427 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2428 | (result_type(1) << __r) - result_type(1); |
| 2429 | __i_ = 0; |
| 2430 | if ((__x_[0] & ~__mask) == 0) |
| 2431 | { |
| 2432 | for (size_t __i = 1; __i < __n; ++__i) |
| 2433 | if (__x_[__i] != 0) |
| 2434 | return; |
Hubert Tong | ca5b776 | 2018-08-16 23:56:54 +0000 | [diff] [blame] | 2435 | __x_[0] = result_type(1) << (__w - 1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2440 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2441 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2442 | template<class _Sseq> |
| 2443 | void |
| 2444 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2445 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>) |
| 2446 | { |
| 2447 | const unsigned __k = 2; |
| 2448 | uint32_t __ar[__n * __k]; |
| 2449 | __q.generate(__ar, __ar + __n * __k); |
| 2450 | for (size_t __i = 0; __i < __n; ++__i) |
| 2451 | __x_[__i] = static_cast<result_type>( |
| 2452 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 2453 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2454 | (result_type(1) << __r) - result_type(1); |
| 2455 | __i_ = 0; |
| 2456 | if ((__x_[0] & ~__mask) == 0) |
| 2457 | { |
| 2458 | for (size_t __i = 1; __i < __n; ++__i) |
| 2459 | if (__x_[__i] != 0) |
| 2460 | return; |
Hubert Tong | ca5b776 | 2018-08-16 23:56:54 +0000 | [diff] [blame] | 2461 | __x_[0] = result_type(1) << (__w - 1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2466 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2467 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2468 | _UIntType |
| 2469 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2470 | __t, __c, __l, __f>::operator()() |
| 2471 | { |
| 2472 | const size_t __j = (__i_ + 1) % __n; |
| 2473 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2474 | (result_type(1) << __r) - result_type(1); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2475 | const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2476 | const size_t __k = (__i_ + __m) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2477 | __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2478 | result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d); |
| 2479 | __i_ = __j; |
| 2480 | __z ^= __lshift<__s>(__z) & __b; |
| 2481 | __z ^= __lshift<__t>(__z) & __c; |
| 2482 | return __z ^ __rshift<__l>(__z); |
| 2483 | } |
| 2484 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2485 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2486 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2487 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2488 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2489 | operator==(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2490 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2491 | const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2492 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2493 | { |
| 2494 | if (__x.__i_ == __y.__i_) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2495 | return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2496 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 2497 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2498 | size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2499 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2500 | __y.__x_ + __y.__i_)) |
| 2501 | return false; |
| 2502 | if (__x.__i_ == 0) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2503 | return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_); |
| 2504 | return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2505 | } |
| 2506 | if (__x.__i_ < __y.__i_) |
| 2507 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2508 | size_t __j = _Np - __y.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2509 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2510 | __y.__x_ + __y.__i_)) |
| 2511 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2512 | if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2513 | __y.__x_)) |
| 2514 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2515 | return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2516 | __y.__x_ + (_Np - (__x.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2517 | } |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2518 | size_t __j = _Np - __x.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2519 | if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2520 | __x.__x_ + __x.__i_)) |
| 2521 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2522 | if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2523 | __x.__x_)) |
| 2524 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2525 | return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2526 | __x.__x_ + (_Np - (__y.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2529 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2530 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2531 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2532 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2533 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2534 | operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2535 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2536 | const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2537 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2538 | { |
| 2539 | return !(__x == __y); |
| 2540 | } |
| 2541 | |
| 2542 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2543 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2544 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2545 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2546 | basic_ostream<_CharT, _Traits>& |
| 2547 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2548 | const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2549 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2550 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2551 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2552 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
| 2553 | __os.flags(_Ostream::dec | _Ostream::left); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2554 | _CharT __sp = __os.widen(' '); |
| 2555 | __os.fill(__sp); |
| 2556 | __os << __x.__x_[__x.__i_]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2557 | for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2558 | __os << __sp << __x.__x_[__j]; |
| 2559 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 2560 | __os << __sp << __x.__x_[__j]; |
| 2561 | return __os; |
| 2562 | } |
| 2563 | |
| 2564 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2565 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2566 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2567 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2568 | basic_istream<_CharT, _Traits>& |
| 2569 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2570 | mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2571 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2572 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2573 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2574 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 2575 | __is.flags(_Istream::dec | _Istream::skipws); |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2576 | _UInt __t[_Np]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2577 | for (size_t __i = 0; __i < _Np; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2578 | __is >> __t[__i]; |
| 2579 | if (!__is.fail()) |
| 2580 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2581 | for (size_t __i = 0; __i < _Np; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2582 | __x.__x_[__i] = __t[__i]; |
| 2583 | __x.__i_ = 0; |
| 2584 | } |
| 2585 | return __is; |
| 2586 | } |
| 2587 | |
| 2588 | typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, |
| 2589 | 0x9908b0df, 11, 0xffffffff, |
| 2590 | 7, 0x9d2c5680, |
| 2591 | 15, 0xefc60000, |
| 2592 | 18, 1812433253> mt19937; |
| 2593 | typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31, |
| 2594 | 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL, |
| 2595 | 17, 0x71d67fffeda60000ULL, |
| 2596 | 37, 0xfff7eee000000000ULL, |
| 2597 | 43, 6364136223846793005ULL> mt19937_64; |
| 2598 | |
| 2599 | // subtract_with_carry_engine |
| 2600 | |
| 2601 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2602 | class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2603 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2604 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2605 | bool |
| 2606 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2607 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2608 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2609 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2610 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 2611 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2612 | bool |
| 2613 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2614 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2615 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2616 | |
| 2617 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2618 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2619 | basic_ostream<_CharT, _Traits>& |
| 2620 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2621 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2622 | |
| 2623 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2624 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2625 | basic_istream<_CharT, _Traits>& |
| 2626 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2627 | subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2628 | |
| 2629 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2630 | class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2631 | { |
| 2632 | public: |
| 2633 | // types |
| 2634 | typedef _UIntType result_type; |
| 2635 | |
| 2636 | private: |
| 2637 | result_type __x_[__r]; |
| 2638 | result_type __c_; |
| 2639 | size_t __i_; |
| 2640 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2641 | static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2642 | static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters"); |
| 2643 | static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters"); |
| 2644 | static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters"); |
| 2645 | static_assert(__s < __r, "subtract_with_carry_engine invalid parameters"); |
| 2646 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2647 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 2648 | static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2649 | (result_type(1) << __w) - result_type(1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2650 | static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters"); |
| 2651 | |
| 2652 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2653 | static _LIBCPP_CONSTEXPR const size_t word_size = __w; |
| 2654 | static _LIBCPP_CONSTEXPR const size_t short_lag = __s; |
| 2655 | static _LIBCPP_CONSTEXPR const size_t long_lag = __r; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2656 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2657 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2658 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2659 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
| 2660 | static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2661 | |
| 2662 | // constructors and seeding functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 2663 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2664 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 2665 | subtract_with_carry_engine() : subtract_with_carry_engine(default_seed) {} |
| 2666 | _LIBCPP_INLINE_VISIBILITY |
| 2667 | explicit subtract_with_carry_engine(result_type __sd) { seed(__sd); } |
| 2668 | #else |
| 2669 | _LIBCPP_INLINE_VISIBILITY |
| 2670 | explicit subtract_with_carry_engine(result_type __sd = default_seed) { |
| 2671 | seed(__sd); |
| 2672 | } |
| 2673 | #endif |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2674 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2675 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2676 | explicit subtract_with_carry_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2677 | typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2678 | {seed(__q);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2679 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2680 | void seed(result_type __sd = default_seed) |
| 2681 | {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2682 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2683 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2684 | typename enable_if |
| 2685 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2686 | __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2687 | void |
| 2688 | >::type |
| 2689 | seed(_Sseq& __q) |
| 2690 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2691 | |
| 2692 | // generating functions |
| 2693 | result_type operator()(); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2694 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2695 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2696 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2697 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2698 | friend |
| 2699 | bool |
| 2700 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2701 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2702 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2703 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2704 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2705 | friend |
| 2706 | bool |
| 2707 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2708 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2709 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2710 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2711 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2712 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2713 | friend |
| 2714 | basic_ostream<_CharT, _Traits>& |
| 2715 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2716 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2717 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2718 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2719 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2720 | friend |
| 2721 | basic_istream<_CharT, _Traits>& |
| 2722 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2723 | subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2724 | |
| 2725 | private: |
| 2726 | |
| 2727 | void seed(result_type __sd, integral_constant<unsigned, 1>); |
| 2728 | void seed(result_type __sd, integral_constant<unsigned, 2>); |
| 2729 | template<class _Sseq> |
| 2730 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 2731 | template<class _Sseq> |
| 2732 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 2733 | }; |
| 2734 | |
| 2735 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2736 | _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size; |
| 2737 | |
| 2738 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2739 | _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag; |
| 2740 | |
| 2741 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2742 | _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag; |
| 2743 | |
| 2744 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2745 | _LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type |
| 2746 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed; |
| 2747 | |
| 2748 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2749 | void |
| 2750 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 2751 | integral_constant<unsigned, 1>) |
| 2752 | { |
| 2753 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 2754 | __e(__sd == 0u ? default_seed : __sd); |
| 2755 | for (size_t __i = 0; __i < __r; ++__i) |
| 2756 | __x_[__i] = static_cast<result_type>(__e() & _Max); |
| 2757 | __c_ = __x_[__r-1] == 0; |
| 2758 | __i_ = 0; |
| 2759 | } |
| 2760 | |
| 2761 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2762 | void |
| 2763 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 2764 | integral_constant<unsigned, 2>) |
| 2765 | { |
| 2766 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 2767 | __e(__sd == 0u ? default_seed : __sd); |
| 2768 | for (size_t __i = 0; __i < __r; ++__i) |
Howard Hinnant | 93072c1 | 2011-08-15 17:22:22 +0000 | [diff] [blame] | 2769 | { |
| 2770 | result_type __e0 = __e(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2771 | __x_[__i] = static_cast<result_type>( |
Howard Hinnant | 93072c1 | 2011-08-15 17:22:22 +0000 | [diff] [blame] | 2772 | (__e0 + ((uint64_t)__e() << 32)) & _Max); |
| 2773 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2774 | __c_ = __x_[__r-1] == 0; |
| 2775 | __i_ = 0; |
| 2776 | } |
| 2777 | |
| 2778 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2779 | template<class _Sseq> |
| 2780 | void |
| 2781 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 2782 | integral_constant<unsigned, 1>) |
| 2783 | { |
| 2784 | const unsigned __k = 1; |
| 2785 | uint32_t __ar[__r * __k]; |
| 2786 | __q.generate(__ar, __ar + __r * __k); |
| 2787 | for (size_t __i = 0; __i < __r; ++__i) |
| 2788 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 2789 | __c_ = __x_[__r-1] == 0; |
| 2790 | __i_ = 0; |
| 2791 | } |
| 2792 | |
| 2793 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2794 | template<class _Sseq> |
| 2795 | void |
| 2796 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 2797 | integral_constant<unsigned, 2>) |
| 2798 | { |
| 2799 | const unsigned __k = 2; |
| 2800 | uint32_t __ar[__r * __k]; |
| 2801 | __q.generate(__ar, __ar + __r * __k); |
| 2802 | for (size_t __i = 0; __i < __r; ++__i) |
| 2803 | __x_[__i] = static_cast<result_type>( |
| 2804 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 2805 | __c_ = __x_[__r-1] == 0; |
| 2806 | __i_ = 0; |
| 2807 | } |
| 2808 | |
| 2809 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2810 | _UIntType |
| 2811 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()() |
| 2812 | { |
| 2813 | const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r]; |
| 2814 | result_type& __xr = __x_[__i_]; |
| 2815 | result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1; |
| 2816 | __xr = (__xs - __xr - __c_) & _Max; |
| 2817 | __c_ = __new_c; |
| 2818 | __i_ = (__i_ + 1) % __r; |
| 2819 | return __xr; |
| 2820 | } |
| 2821 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2822 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2823 | bool |
| 2824 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2825 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2826 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2827 | { |
| 2828 | if (__x.__c_ != __y.__c_) |
| 2829 | return false; |
| 2830 | if (__x.__i_ == __y.__i_) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2831 | return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2832 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 2833 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2834 | size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2835 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2836 | __y.__x_ + __y.__i_)) |
| 2837 | return false; |
| 2838 | if (__x.__i_ == 0) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2839 | return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_); |
| 2840 | return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2841 | } |
| 2842 | if (__x.__i_ < __y.__i_) |
| 2843 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2844 | size_t __j = _Rp - __y.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2845 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2846 | __y.__x_ + __y.__i_)) |
| 2847 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2848 | if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2849 | __y.__x_)) |
| 2850 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2851 | return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2852 | __y.__x_ + (_Rp - (__x.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2853 | } |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2854 | size_t __j = _Rp - __x.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2855 | if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2856 | __x.__x_ + __x.__i_)) |
| 2857 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2858 | if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2859 | __x.__x_)) |
| 2860 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2861 | return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2862 | __x.__x_ + (_Rp - (__y.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2865 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2866 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2867 | bool |
| 2868 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2869 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2870 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2871 | { |
| 2872 | return !(__x == __y); |
| 2873 | } |
| 2874 | |
| 2875 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2876 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2877 | basic_ostream<_CharT, _Traits>& |
| 2878 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2879 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2880 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2881 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2882 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
| 2883 | __os.flags(_Ostream::dec | _Ostream::left); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2884 | _CharT __sp = __os.widen(' '); |
| 2885 | __os.fill(__sp); |
| 2886 | __os << __x.__x_[__x.__i_]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2887 | for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2888 | __os << __sp << __x.__x_[__j]; |
| 2889 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 2890 | __os << __sp << __x.__x_[__j]; |
| 2891 | __os << __sp << __x.__c_; |
| 2892 | return __os; |
| 2893 | } |
| 2894 | |
| 2895 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2896 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2897 | basic_istream<_CharT, _Traits>& |
| 2898 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2899 | subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2900 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2901 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2902 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 2903 | __is.flags(_Istream::dec | _Istream::skipws); |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2904 | _UInt __t[_Rp+1]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2905 | for (size_t __i = 0; __i < _Rp+1; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2906 | __is >> __t[__i]; |
| 2907 | if (!__is.fail()) |
| 2908 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2909 | for (size_t __i = 0; __i < _Rp; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2910 | __x.__x_[__i] = __t[__i]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2911 | __x.__c_ = __t[_Rp]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2912 | __x.__i_ = 0; |
| 2913 | } |
| 2914 | return __is; |
| 2915 | } |
| 2916 | |
| 2917 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base; |
| 2918 | typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base; |
| 2919 | |
| 2920 | // discard_block_engine |
| 2921 | |
| 2922 | template<class _Engine, size_t __p, size_t __r> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2923 | class _LIBCPP_TEMPLATE_VIS discard_block_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2924 | { |
| 2925 | _Engine __e_; |
| 2926 | int __n_; |
| 2927 | |
| 2928 | static_assert( 0 < __r, "discard_block_engine invalid parameters"); |
| 2929 | static_assert(__r <= __p, "discard_block_engine invalid parameters"); |
Eric Fiselier | 37c2215 | 2016-12-24 00:24:44 +0000 | [diff] [blame] | 2930 | static_assert(__r <= INT_MAX, "discard_block_engine invalid parameters"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2931 | public: |
| 2932 | // types |
| 2933 | typedef typename _Engine::result_type result_type; |
| 2934 | |
| 2935 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2936 | static _LIBCPP_CONSTEXPR const size_t block_size = __p; |
| 2937 | static _LIBCPP_CONSTEXPR const size_t used_block = __r; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2938 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 2939 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2940 | static const result_type _Min = _Engine::_Min; |
| 2941 | static const result_type _Max = _Engine::_Max; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2942 | #else |
| 2943 | static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min(); |
| 2944 | static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max(); |
| 2945 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2946 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2947 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2948 | static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2949 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2950 | static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2951 | |
| 2952 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2953 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2954 | discard_block_engine() : __n_(0) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2955 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2956 | explicit discard_block_engine(const _Engine& __e) |
| 2957 | : __e_(__e), __n_(0) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 2958 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2959 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2960 | explicit discard_block_engine(_Engine&& __e) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2961 | : __e_(_VSTD::move(__e)), __n_(0) {} |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 2962 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2963 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2964 | explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2965 | template<class _Sseq> |
| 2966 | _LIBCPP_INLINE_VISIBILITY |
| 2967 | explicit discard_block_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2968 | typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value && |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2969 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2970 | : __e_(__q), __n_(0) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2971 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2972 | void seed() {__e_.seed(); __n_ = 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2973 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2974 | void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;} |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2975 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2976 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2977 | typename enable_if |
| 2978 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2979 | __is_seed_sequence<_Sseq, discard_block_engine>::value, |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2980 | void |
| 2981 | >::type |
| 2982 | seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2983 | |
| 2984 | // generating functions |
| 2985 | result_type operator()(); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2986 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2987 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2988 | |
| 2989 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2990 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 2991 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2992 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2993 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2994 | friend |
| 2995 | bool |
| 2996 | operator==( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2997 | const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 2998 | const discard_block_engine<_Eng, _Pp, _Rp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2999 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3000 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3001 | friend |
| 3002 | bool |
| 3003 | operator!=( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3004 | const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 3005 | const discard_block_engine<_Eng, _Pp, _Rp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3006 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3007 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3008 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3009 | friend |
| 3010 | basic_ostream<_CharT, _Traits>& |
| 3011 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3012 | const discard_block_engine<_Eng, _Pp, _Rp>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3013 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3014 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3015 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3016 | friend |
| 3017 | basic_istream<_CharT, _Traits>& |
| 3018 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3019 | discard_block_engine<_Eng, _Pp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3020 | }; |
| 3021 | |
| 3022 | template<class _Engine, size_t __p, size_t __r> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 3023 | _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size; |
| 3024 | |
| 3025 | template<class _Engine, size_t __p, size_t __r> |
| 3026 | _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block; |
| 3027 | |
| 3028 | template<class _Engine, size_t __p, size_t __r> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3029 | typename discard_block_engine<_Engine, __p, __r>::result_type |
| 3030 | discard_block_engine<_Engine, __p, __r>::operator()() |
| 3031 | { |
Eric Fiselier | 37c2215 | 2016-12-24 00:24:44 +0000 | [diff] [blame] | 3032 | if (__n_ >= static_cast<int>(__r)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3033 | { |
| 3034 | __e_.discard(__p - __r); |
| 3035 | __n_ = 0; |
| 3036 | } |
| 3037 | ++__n_; |
| 3038 | return __e_(); |
| 3039 | } |
| 3040 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3041 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3042 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3043 | bool |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3044 | operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 3045 | const discard_block_engine<_Eng, _Pp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3046 | { |
| 3047 | return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_; |
| 3048 | } |
| 3049 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3050 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3051 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3052 | bool |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3053 | operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 3054 | const discard_block_engine<_Eng, _Pp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3055 | { |
| 3056 | return !(__x == __y); |
| 3057 | } |
| 3058 | |
| 3059 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3060 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3061 | basic_ostream<_CharT, _Traits>& |
| 3062 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3063 | const discard_block_engine<_Eng, _Pp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3064 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3065 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3066 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
| 3067 | __os.flags(_Ostream::dec | _Ostream::left); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3068 | _CharT __sp = __os.widen(' '); |
| 3069 | __os.fill(__sp); |
| 3070 | return __os << __x.__e_ << __sp << __x.__n_; |
| 3071 | } |
| 3072 | |
| 3073 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3074 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3075 | basic_istream<_CharT, _Traits>& |
| 3076 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3077 | discard_block_engine<_Eng, _Pp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3078 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3079 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3080 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 3081 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3082 | _Eng __e; |
| 3083 | int __n; |
| 3084 | __is >> __e >> __n; |
| 3085 | if (!__is.fail()) |
| 3086 | { |
| 3087 | __x.__e_ = __e; |
| 3088 | __x.__n_ = __n; |
| 3089 | } |
| 3090 | return __is; |
| 3091 | } |
| 3092 | |
| 3093 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
| 3094 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
| 3095 | |
| 3096 | // independent_bits_engine |
| 3097 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3098 | template<class _Engine, size_t __w, class _UIntType> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3099 | class _LIBCPP_TEMPLATE_VIS independent_bits_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3100 | { |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3101 | template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3102 | class __get_n |
| 3103 | { |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3104 | static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3105 | static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0); |
| 3106 | static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np; |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3107 | static _LIBCPP_CONSTEXPR const _UInt _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3108 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3109 | static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3110 | }; |
| 3111 | public: |
| 3112 | // types |
| 3113 | typedef _UIntType result_type; |
| 3114 | |
| 3115 | private: |
| 3116 | _Engine __e_; |
| 3117 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3118 | static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3119 | static_assert( 0 < __w, "independent_bits_engine invalid parameters"); |
| 3120 | static_assert(__w <= _Dt, "independent_bits_engine invalid parameters"); |
| 3121 | |
| 3122 | typedef typename _Engine::result_type _Engine_result_type; |
| 3123 | typedef typename conditional |
| 3124 | < |
| 3125 | sizeof(_Engine_result_type) <= sizeof(result_type), |
| 3126 | result_type, |
| 3127 | _Engine_result_type |
| 3128 | >::type _Working_result_type; |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3129 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3130 | static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3131 | + _Working_result_type(1); |
| 3132 | #else |
| 3133 | static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min() |
| 3134 | + _Working_result_type(1); |
| 3135 | #endif |
| 3136 | static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value; |
| 3137 | static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value; |
| 3138 | static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n; |
| 3139 | static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n; |
| 3140 | static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits; |
| 3141 | static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits; |
| 3142 | static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 : |
| 3143 | (_Rp >> __w0) << __w0; |
| 3144 | static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : |
| 3145 | (_Rp >> (__w0+1)) << (__w0+1); |
| 3146 | static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ? |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3147 | _Engine_result_type(~0) >> (_EDt - __w0) : |
| 3148 | _Engine_result_type(0); |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3149 | static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ? |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3150 | _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : |
| 3151 | _Engine_result_type(~0); |
| 3152 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3153 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 3154 | static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) : |
| 3155 | (result_type(1) << __w) - result_type(1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3156 | static_assert(_Min < _Max, "independent_bits_engine invalid parameters"); |
| 3157 | |
| 3158 | // engine characteristics |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3159 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3160 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3161 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3162 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3163 | |
| 3164 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3165 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3166 | independent_bits_engine() {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3167 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3168 | explicit independent_bits_engine(const _Engine& __e) |
| 3169 | : __e_(__e) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3170 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3171 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3172 | explicit independent_bits_engine(_Engine&& __e) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3173 | : __e_(_VSTD::move(__e)) {} |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 3174 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3175 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3176 | explicit independent_bits_engine(result_type __sd) : __e_(__sd) {} |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3177 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3178 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3179 | explicit independent_bits_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3180 | typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value && |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3181 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3182 | : __e_(__q) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3183 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3184 | void seed() {__e_.seed();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3185 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3186 | void seed(result_type __sd) {__e_.seed(__sd);} |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3187 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3188 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3189 | typename enable_if |
| 3190 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3191 | __is_seed_sequence<_Sseq, independent_bits_engine>::value, |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3192 | void |
| 3193 | >::type |
| 3194 | seed(_Sseq& __q) {__e_.seed(__q);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3195 | |
| 3196 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3197 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3198 | result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3199 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3200 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 3201 | |
| 3202 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3203 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3204 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3205 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3206 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3207 | friend |
| 3208 | bool |
| 3209 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3210 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x, |
| 3211 | const independent_bits_engine<_Eng, _Wp, _UInt>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3212 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3213 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3214 | friend |
| 3215 | bool |
| 3216 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3217 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x, |
| 3218 | const independent_bits_engine<_Eng, _Wp, _UInt>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3219 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3220 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3221 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3222 | friend |
| 3223 | basic_ostream<_CharT, _Traits>& |
| 3224 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3225 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3226 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3227 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3228 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3229 | friend |
| 3230 | basic_istream<_CharT, _Traits>& |
| 3231 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3232 | independent_bits_engine<_Eng, _Wp, _UInt>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3233 | |
| 3234 | private: |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3235 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | fe77858 | 2017-09-20 19:38:43 +0000 | [diff] [blame] | 3236 | result_type __eval(false_type); |
| 3237 | result_type __eval(true_type); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3238 | |
| 3239 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3240 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3241 | static |
| 3242 | typename enable_if |
| 3243 | < |
| 3244 | __count < _Dt, |
| 3245 | result_type |
| 3246 | >::type |
| 3247 | __lshift(result_type __x) {return __x << __count;} |
| 3248 | |
| 3249 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3250 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3251 | static |
| 3252 | typename enable_if |
| 3253 | < |
| 3254 | (__count >= _Dt), |
| 3255 | result_type |
| 3256 | >::type |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3257 | __lshift(result_type) {return result_type(0);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3258 | }; |
| 3259 | |
| 3260 | template<class _Engine, size_t __w, class _UIntType> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3261 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3262 | _UIntType |
Marshall Clow | fe77858 | 2017-09-20 19:38:43 +0000 | [diff] [blame] | 3263 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3264 | { |
| 3265 | return static_cast<result_type>(__e_() & __mask0); |
| 3266 | } |
| 3267 | |
| 3268 | template<class _Engine, size_t __w, class _UIntType> |
| 3269 | _UIntType |
Marshall Clow | fe77858 | 2017-09-20 19:38:43 +0000 | [diff] [blame] | 3270 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3271 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3272 | result_type _Sp = 0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3273 | for (size_t __k = 0; __k < __n0; ++__k) |
| 3274 | { |
| 3275 | _Engine_result_type __u; |
| 3276 | do |
| 3277 | { |
| 3278 | __u = __e_() - _Engine::min(); |
| 3279 | } while (__u >= __y0); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3280 | _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3281 | } |
| 3282 | for (size_t __k = __n0; __k < __n; ++__k) |
| 3283 | { |
| 3284 | _Engine_result_type __u; |
| 3285 | do |
| 3286 | { |
| 3287 | __u = __e_() - _Engine::min(); |
| 3288 | } while (__u >= __y1); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3289 | _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3290 | } |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3291 | return _Sp; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3294 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3295 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3296 | bool |
| 3297 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3298 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x, |
| 3299 | const independent_bits_engine<_Eng, _Wp, _UInt>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3300 | { |
| 3301 | return __x.base() == __y.base(); |
| 3302 | } |
| 3303 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3304 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3305 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3306 | bool |
| 3307 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3308 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x, |
| 3309 | const independent_bits_engine<_Eng, _Wp, _UInt>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3310 | { |
| 3311 | return !(__x == __y); |
| 3312 | } |
| 3313 | |
| 3314 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3315 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3316 | basic_ostream<_CharT, _Traits>& |
| 3317 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3318 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3319 | { |
| 3320 | return __os << __x.base(); |
| 3321 | } |
| 3322 | |
| 3323 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3324 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3325 | basic_istream<_CharT, _Traits>& |
| 3326 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3327 | independent_bits_engine<_Eng, _Wp, _UInt>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3328 | { |
| 3329 | _Eng __e; |
| 3330 | __is >> __e; |
| 3331 | if (!__is.fail()) |
| 3332 | __x.__e_ = __e; |
| 3333 | return __is; |
| 3334 | } |
| 3335 | |
| 3336 | // shuffle_order_engine |
| 3337 | |
| 3338 | template <uint64_t _Xp, uint64_t _Yp> |
| 3339 | struct __ugcd |
| 3340 | { |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3341 | static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3342 | }; |
| 3343 | |
| 3344 | template <uint64_t _Xp> |
| 3345 | struct __ugcd<_Xp, 0> |
| 3346 | { |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3347 | static _LIBCPP_CONSTEXPR const uint64_t value = _Xp; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3348 | }; |
| 3349 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3350 | template <uint64_t _Np, uint64_t _Dp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3351 | class __uratio |
| 3352 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3353 | static_assert(_Dp != 0, "__uratio divide by 0"); |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3354 | static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3355 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3356 | static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd; |
| 3357 | static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3358 | |
| 3359 | typedef __uratio<num, den> type; |
| 3360 | }; |
| 3361 | |
| 3362 | template<class _Engine, size_t __k> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3363 | class _LIBCPP_TEMPLATE_VIS shuffle_order_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3364 | { |
| 3365 | static_assert(0 < __k, "shuffle_order_engine invalid parameters"); |
| 3366 | public: |
| 3367 | // types |
| 3368 | typedef typename _Engine::result_type result_type; |
| 3369 | |
| 3370 | private: |
| 3371 | _Engine __e_; |
| 3372 | result_type _V_[__k]; |
| 3373 | result_type _Y_; |
| 3374 | |
| 3375 | public: |
| 3376 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3377 | static _LIBCPP_CONSTEXPR const size_t table_size = __k; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3378 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3379 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3380 | static const result_type _Min = _Engine::_Min; |
| 3381 | static const result_type _Max = _Engine::_Max; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3382 | #else |
| 3383 | static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min(); |
| 3384 | static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max(); |
| 3385 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3386 | static_assert(_Min < _Max, "shuffle_order_engine invalid parameters"); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3387 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3388 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3389 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3390 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3391 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3392 | static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3393 | |
| 3394 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3395 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3396 | shuffle_order_engine() {__init();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3397 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3398 | explicit shuffle_order_engine(const _Engine& __e) |
| 3399 | : __e_(__e) {__init();} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3400 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3401 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3402 | explicit shuffle_order_engine(_Engine&& __e) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3403 | : __e_(_VSTD::move(__e)) {__init();} |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 3404 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3405 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3406 | explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();} |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3407 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3408 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3409 | explicit shuffle_order_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3410 | typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value && |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3411 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3412 | : __e_(__q) {__init();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3413 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3414 | void seed() {__e_.seed(); __init();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3415 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3416 | void seed(result_type __sd) {__e_.seed(__sd); __init();} |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3417 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3418 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3419 | typename enable_if |
| 3420 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3421 | __is_seed_sequence<_Sseq, shuffle_order_engine>::value, |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3422 | void |
| 3423 | >::type |
| 3424 | seed(_Sseq& __q) {__e_.seed(__q); __init();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3425 | |
| 3426 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3427 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3428 | result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3429 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3430 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 3431 | |
| 3432 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3433 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3434 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3435 | |
| 3436 | private: |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3437 | template<class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3438 | friend |
| 3439 | bool |
| 3440 | operator==( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3441 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3442 | const shuffle_order_engine<_Eng, _Kp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3443 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3444 | template<class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3445 | friend |
| 3446 | bool |
| 3447 | operator!=( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3448 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3449 | const shuffle_order_engine<_Eng, _Kp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3450 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3451 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3452 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3453 | friend |
| 3454 | basic_ostream<_CharT, _Traits>& |
| 3455 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3456 | const shuffle_order_engine<_Eng, _Kp>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3457 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3458 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3459 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3460 | friend |
| 3461 | basic_istream<_CharT, _Traits>& |
| 3462 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3463 | shuffle_order_engine<_Eng, _Kp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3464 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3465 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3466 | void __init() |
| 3467 | { |
| 3468 | for (size_t __i = 0; __i < __k; ++__i) |
| 3469 | _V_[__i] = __e_(); |
| 3470 | _Y_ = __e_(); |
| 3471 | } |
| 3472 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3473 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3474 | result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3475 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3476 | result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3477 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3478 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3479 | result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3480 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3481 | result_type __eval2(true_type) {return __evalf<__k, 0>();} |
| 3482 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3483 | template <uint64_t _Np, uint64_t _Dp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3484 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3485 | typename enable_if |
| 3486 | < |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3487 | (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3488 | result_type |
| 3489 | >::type |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3490 | __eval(__uratio<_Np, _Dp>) |
| 3491 | {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3492 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3493 | template <uint64_t _Np, uint64_t _Dp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3494 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3495 | typename enable_if |
| 3496 | < |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3497 | __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3498 | result_type |
| 3499 | >::type |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3500 | __eval(__uratio<_Np, _Dp>) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3501 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3502 | const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min) |
| 3503 | / __uratio<_Np, _Dp>::den); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3504 | _Y_ = _V_[__j]; |
| 3505 | _V_[__j] = __e_(); |
| 3506 | return _Y_; |
| 3507 | } |
| 3508 | |
| 3509 | template <uint64_t __n, uint64_t __d> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3510 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3511 | result_type __evalf() |
| 3512 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3513 | const double _Fp = __d == 0 ? |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3514 | __n / (2. * 0x8000000000000000ull) : |
| 3515 | __n / (double)__d; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3516 | const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3517 | _Y_ = _V_[__j]; |
| 3518 | _V_[__j] = __e_(); |
| 3519 | return _Y_; |
| 3520 | } |
| 3521 | }; |
| 3522 | |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 3523 | template<class _Engine, size_t __k> |
| 3524 | _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size; |
| 3525 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3526 | template<class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3527 | bool |
| 3528 | operator==( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3529 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3530 | const shuffle_order_engine<_Eng, _Kp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3531 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3532 | return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) && |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3533 | __x.__e_ == __y.__e_; |
| 3534 | } |
| 3535 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3536 | template<class _Eng, size_t _Kp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3537 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3538 | bool |
| 3539 | operator!=( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3540 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3541 | const shuffle_order_engine<_Eng, _Kp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3542 | { |
| 3543 | return !(__x == __y); |
| 3544 | } |
| 3545 | |
| 3546 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3547 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3548 | basic_ostream<_CharT, _Traits>& |
| 3549 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3550 | const shuffle_order_engine<_Eng, _Kp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3551 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3552 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3553 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
| 3554 | __os.flags(_Ostream::dec | _Ostream::left); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3555 | _CharT __sp = __os.widen(' '); |
| 3556 | __os.fill(__sp); |
| 3557 | __os << __x.__e_ << __sp << __x._V_[0]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3558 | for (size_t __i = 1; __i < _Kp; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3559 | __os << __sp << __x._V_[__i]; |
| 3560 | return __os << __sp << __x._Y_; |
| 3561 | } |
| 3562 | |
| 3563 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3564 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3565 | basic_istream<_CharT, _Traits>& |
| 3566 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3567 | shuffle_order_engine<_Eng, _Kp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3568 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3569 | typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3570 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3571 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 3572 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3573 | _Eng __e; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3574 | result_type _Vp[_Kp+1]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3575 | __is >> __e; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3576 | for (size_t __i = 0; __i < _Kp+1; ++__i) |
| 3577 | __is >> _Vp[__i]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3578 | if (!__is.fail()) |
| 3579 | { |
| 3580 | __x.__e_ = __e; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3581 | for (size_t __i = 0; __i < _Kp; ++__i) |
| 3582 | __x._V_[__i] = _Vp[__i]; |
| 3583 | __x._Y_ = _Vp[_Kp]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3584 | } |
| 3585 | return __is; |
| 3586 | } |
| 3587 | |
| 3588 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
| 3589 | |
| 3590 | // random_device |
| 3591 | |
Louis Dionne | 3777e68 | 2020-10-15 10:32:09 -0400 | [diff] [blame] | 3592 | #if !defined(_LIBCPP_HAS_NO_RANDOM_DEVICE) |
| 3593 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 3594 | class _LIBCPP_TYPE_VIS random_device |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3595 | { |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 3596 | #ifdef _LIBCPP_USING_DEV_RANDOM |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3597 | int __f_; |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 3598 | #endif // defined(_LIBCPP_USING_DEV_RANDOM) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3599 | public: |
| 3600 | // types |
| 3601 | typedef unsigned result_type; |
| 3602 | |
| 3603 | // generator characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3604 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 3605 | static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3606 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3607 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 664183b | 2012-04-02 00:40:41 +0000 | [diff] [blame] | 3608 | static _LIBCPP_CONSTEXPR result_type min() { return _Min;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3609 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 664183b | 2012-04-02 00:40:41 +0000 | [diff] [blame] | 3610 | static _LIBCPP_CONSTEXPR result_type max() { return _Max;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3611 | |
| 3612 | // constructors |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 3613 | #ifndef _LIBCPP_CXX03_LANG |
| 3614 | random_device() : random_device("/dev/urandom") {} |
| 3615 | explicit random_device(const string& __token); |
| 3616 | #else |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3617 | explicit random_device(const string& __token = "/dev/urandom"); |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 3618 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3619 | ~random_device(); |
| 3620 | |
| 3621 | // generating functions |
| 3622 | result_type operator()(); |
| 3623 | |
| 3624 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3625 | double entropy() const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3626 | |
| 3627 | private: |
| 3628 | // no copy functions |
| 3629 | random_device(const random_device&); // = delete; |
| 3630 | random_device& operator=(const random_device&); // = delete; |
| 3631 | }; |
| 3632 | |
Louis Dionne | 3777e68 | 2020-10-15 10:32:09 -0400 | [diff] [blame] | 3633 | #endif // !_LIBCPP_HAS_NO_RANDOM_DEVICE |
| 3634 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3635 | // seed_seq |
| 3636 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3637 | class _LIBCPP_TEMPLATE_VIS seed_seq |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3638 | { |
| 3639 | public: |
| 3640 | // types |
| 3641 | typedef uint32_t result_type; |
| 3642 | |
| 3643 | private: |
| 3644 | vector<result_type> __v_; |
| 3645 | |
| 3646 | template<class _InputIterator> |
| 3647 | void init(_InputIterator __first, _InputIterator __last); |
| 3648 | public: |
| 3649 | // constructors |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3650 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 4b6b809 | 2013-10-23 05:56:47 +0000 | [diff] [blame] | 3651 | seed_seq() _NOEXCEPT {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3652 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3653 | template<class _Tp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3654 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3655 | seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());} |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 3656 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3657 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3658 | template<class _InputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3659 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3660 | seed_seq(_InputIterator __first, _InputIterator __last) |
| 3661 | {init(__first, __last);} |
| 3662 | |
| 3663 | // generating functions |
| 3664 | template<class _RandomAccessIterator> |
| 3665 | void generate(_RandomAccessIterator __first, _RandomAccessIterator __last); |
| 3666 | |
| 3667 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3668 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 4b6b809 | 2013-10-23 05:56:47 +0000 | [diff] [blame] | 3669 | size_t size() const _NOEXCEPT {return __v_.size();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3670 | template<class _OutputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3671 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3672 | void param(_OutputIterator __dest) const |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3673 | {_VSTD::copy(__v_.begin(), __v_.end(), __dest);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3674 | |
| 3675 | private: |
| 3676 | // no copy functions |
| 3677 | seed_seq(const seed_seq&); // = delete; |
| 3678 | void operator=(const seed_seq&); // = delete; |
| 3679 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3680 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3681 | static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3682 | }; |
| 3683 | |
| 3684 | template<class _InputIterator> |
| 3685 | void |
| 3686 | seed_seq::init(_InputIterator __first, _InputIterator __last) |
| 3687 | { |
| 3688 | for (_InputIterator __s = __first; __s != __last; ++__s) |
| 3689 | __v_.push_back(*__s & 0xFFFFFFFF); |
| 3690 | } |
| 3691 | |
| 3692 | template<class _RandomAccessIterator> |
| 3693 | void |
| 3694 | seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last) |
| 3695 | { |
| 3696 | if (__first != __last) |
| 3697 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3698 | _VSTD::fill(__first, __last, 0x8b8b8b8b); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3699 | const size_t __n = static_cast<size_t>(__last - __first); |
| 3700 | const size_t __s = __v_.size(); |
| 3701 | const size_t __t = (__n >= 623) ? 11 |
| 3702 | : (__n >= 68) ? 7 |
| 3703 | : (__n >= 39) ? 5 |
| 3704 | : (__n >= 7) ? 3 |
| 3705 | : (__n - 1) / 2; |
| 3706 | const size_t __p = (__n - __t) / 2; |
| 3707 | const size_t __q = __p + __t; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3708 | const size_t __m = _VSTD::max(__s + 1, __n); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3709 | // __k = 0; |
| 3710 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3711 | result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p] |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3712 | ^ __first[__n - 1]); |
| 3713 | __first[__p] += __r; |
| 3714 | __r += __s; |
| 3715 | __first[__q] += __r; |
| 3716 | __first[0] = __r; |
| 3717 | } |
| 3718 | for (size_t __k = 1; __k <= __s; ++__k) |
| 3719 | { |
| 3720 | const size_t __kmodn = __k % __n; |
| 3721 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3722 | result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3723 | ^ __first[(__k - 1) % __n]); |
| 3724 | __first[__kpmodn] += __r; |
| 3725 | __r += __kmodn + __v_[__k-1]; |
| 3726 | __first[(__k + __q) % __n] += __r; |
| 3727 | __first[__kmodn] = __r; |
| 3728 | } |
| 3729 | for (size_t __k = __s + 1; __k < __m; ++__k) |
| 3730 | { |
| 3731 | const size_t __kmodn = __k % __n; |
| 3732 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3733 | result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3734 | ^ __first[(__k - 1) % __n]); |
| 3735 | __first[__kpmodn] += __r; |
| 3736 | __r += __kmodn; |
| 3737 | __first[(__k + __q) % __n] += __r; |
| 3738 | __first[__kmodn] = __r; |
| 3739 | } |
| 3740 | for (size_t __k = __m; __k < __m + __n; ++__k) |
| 3741 | { |
| 3742 | const size_t __kmodn = __k % __n; |
| 3743 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3744 | result_type __r = 1566083941 * _Tp(__first[__kmodn] + |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3745 | __first[__kpmodn] + |
| 3746 | __first[(__k - 1) % __n]); |
| 3747 | __first[__kpmodn] ^= __r; |
| 3748 | __r -= __kmodn; |
| 3749 | __first[(__k + __q) % __n] ^= __r; |
| 3750 | __first[__kmodn] = __r; |
| 3751 | } |
| 3752 | } |
| 3753 | } |
| 3754 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3755 | // generate_canonical |
| 3756 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3757 | template<class _RealType, size_t __bits, class _URNG> |
| 3758 | _RealType |
| 3759 | generate_canonical(_URNG& __g) |
| 3760 | { |
| 3761 | const size_t _Dt = numeric_limits<_RealType>::digits; |
| 3762 | const size_t __b = _Dt < __bits ? _Dt : __bits; |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3763 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3764 | const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3765 | #else |
| 3766 | const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value; |
| 3767 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3768 | const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0); |
Louis Dionne | 2422bdb | 2019-08-20 15:39:20 +0000 | [diff] [blame] | 3769 | const _RealType _Rp = static_cast<_RealType>(_URNG::max() - _URNG::min()) + _RealType(1); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3770 | _RealType __base = _Rp; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3771 | _RealType _Sp = __g() - _URNG::min(); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3772 | for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp) |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3773 | _Sp += (__g() - _URNG::min()) * __base; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3774 | return _Sp / __base; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3777 | // uniform_real_distribution |
| 3778 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3779 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3780 | class _LIBCPP_TEMPLATE_VIS uniform_real_distribution |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3781 | { |
| 3782 | public: |
| 3783 | // types |
| 3784 | typedef _RealType result_type; |
| 3785 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3786 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3787 | { |
| 3788 | result_type __a_; |
| 3789 | result_type __b_; |
| 3790 | public: |
| 3791 | typedef uniform_real_distribution distribution_type; |
| 3792 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3793 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3794 | explicit param_type(result_type __a = 0, |
| 3795 | result_type __b = 1) |
| 3796 | : __a_(__a), __b_(__b) {} |
| 3797 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3798 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3799 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3800 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3801 | result_type b() const {return __b_;} |
| 3802 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3803 | friend _LIBCPP_INLINE_VISIBILITY |
| 3804 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3805 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3806 | friend _LIBCPP_INLINE_VISIBILITY |
| 3807 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3808 | {return !(__x == __y);} |
| 3809 | }; |
| 3810 | |
| 3811 | private: |
| 3812 | param_type __p_; |
| 3813 | |
| 3814 | public: |
| 3815 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 3816 | #ifndef _LIBCPP_CXX03_LANG |
| 3817 | _LIBCPP_INLINE_VISIBILITY |
| 3818 | uniform_real_distribution() : uniform_real_distribution(0) {} |
| 3819 | explicit uniform_real_distribution(result_type __a, result_type __b = 1) |
| 3820 | : __p_(param_type(__a, __b)) {} |
| 3821 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3822 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3823 | explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1) |
| 3824 | : __p_(param_type(__a, __b)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 3825 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3826 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3827 | explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3828 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3829 | void reset() {} |
| 3830 | |
| 3831 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3832 | template<class _URNG> |
| 3833 | _LIBCPP_INLINE_VISIBILITY |
| 3834 | result_type operator()(_URNG& __g) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3835 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3836 | template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3837 | |
| 3838 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3839 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3840 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3841 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3842 | result_type b() const {return __p_.b();} |
| 3843 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3844 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3845 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3846 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3847 | void param(const param_type& __p) {__p_ = __p;} |
| 3848 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3849 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3850 | result_type min() const {return a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3851 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3852 | result_type max() const {return b();} |
| 3853 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3854 | friend _LIBCPP_INLINE_VISIBILITY |
| 3855 | bool operator==(const uniform_real_distribution& __x, |
| 3856 | const uniform_real_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3857 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3858 | friend _LIBCPP_INLINE_VISIBILITY |
| 3859 | bool operator!=(const uniform_real_distribution& __x, |
| 3860 | const uniform_real_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3861 | {return !(__x == __y);} |
| 3862 | }; |
| 3863 | |
| 3864 | template<class _RealType> |
| 3865 | template<class _URNG> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3866 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3867 | typename uniform_real_distribution<_RealType>::result_type |
| 3868 | uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3869 | { |
| 3870 | return (__p.b() - __p.a()) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3871 | * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3872 | + __p.a(); |
| 3873 | } |
| 3874 | |
| 3875 | template <class _CharT, class _Traits, class _RT> |
| 3876 | basic_ostream<_CharT, _Traits>& |
| 3877 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3878 | const uniform_real_distribution<_RT>& __x) |
| 3879 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3880 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3881 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 3882 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 3883 | _OStream::scientific); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3884 | _CharT __sp = __os.widen(' '); |
| 3885 | __os.fill(__sp); |
| 3886 | return __os << __x.a() << __sp << __x.b(); |
| 3887 | } |
| 3888 | |
| 3889 | template <class _CharT, class _Traits, class _RT> |
| 3890 | basic_istream<_CharT, _Traits>& |
| 3891 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3892 | uniform_real_distribution<_RT>& __x) |
| 3893 | { |
| 3894 | typedef uniform_real_distribution<_RT> _Eng; |
| 3895 | typedef typename _Eng::result_type result_type; |
| 3896 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3897 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3898 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 3899 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3900 | result_type __a; |
| 3901 | result_type __b; |
| 3902 | __is >> __a >> __b; |
| 3903 | if (!__is.fail()) |
| 3904 | __x.param(param_type(__a, __b)); |
| 3905 | return __is; |
| 3906 | } |
| 3907 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3908 | // bernoulli_distribution |
| 3909 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3910 | class _LIBCPP_TEMPLATE_VIS bernoulli_distribution |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3911 | { |
| 3912 | public: |
| 3913 | // types |
| 3914 | typedef bool result_type; |
| 3915 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3916 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3917 | { |
| 3918 | double __p_; |
| 3919 | public: |
| 3920 | typedef bernoulli_distribution distribution_type; |
| 3921 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3922 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3923 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 3924 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3925 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3926 | double p() const {return __p_;} |
| 3927 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3928 | friend _LIBCPP_INLINE_VISIBILITY |
| 3929 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3930 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3931 | friend _LIBCPP_INLINE_VISIBILITY |
| 3932 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3933 | {return !(__x == __y);} |
| 3934 | }; |
| 3935 | |
| 3936 | private: |
| 3937 | param_type __p_; |
| 3938 | |
| 3939 | public: |
| 3940 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 3941 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3942 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 3943 | bernoulli_distribution() : bernoulli_distribution(0.5) {} |
| 3944 | _LIBCPP_INLINE_VISIBILITY |
| 3945 | explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {} |
| 3946 | #else |
| 3947 | _LIBCPP_INLINE_VISIBILITY |
| 3948 | explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {} |
| 3949 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3950 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3951 | explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3952 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3953 | void reset() {} |
| 3954 | |
| 3955 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3956 | template<class _URNG> |
| 3957 | _LIBCPP_INLINE_VISIBILITY |
| 3958 | result_type operator()(_URNG& __g) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3959 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3960 | template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3961 | |
| 3962 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3963 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3964 | double p() const {return __p_.p();} |
| 3965 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3966 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3967 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3968 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3969 | void param(const param_type& __p) {__p_ = __p;} |
| 3970 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3971 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3972 | result_type min() const {return false;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3973 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3974 | result_type max() const {return true;} |
| 3975 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3976 | friend _LIBCPP_INLINE_VISIBILITY |
| 3977 | bool operator==(const bernoulli_distribution& __x, |
| 3978 | const bernoulli_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3979 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3980 | friend _LIBCPP_INLINE_VISIBILITY |
| 3981 | bool operator!=(const bernoulli_distribution& __x, |
| 3982 | const bernoulli_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3983 | {return !(__x == __y);} |
| 3984 | }; |
| 3985 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3986 | template<class _URNG> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3987 | inline |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3988 | bernoulli_distribution::result_type |
| 3989 | bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) |
| 3990 | { |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 3991 | uniform_real_distribution<double> __gen; |
| 3992 | return __gen(__g) < __p.p(); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3995 | template <class _CharT, class _Traits> |
| 3996 | basic_ostream<_CharT, _Traits>& |
| 3997 | operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) |
| 3998 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3999 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4000 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 4001 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 4002 | _OStream::scientific); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4003 | _CharT __sp = __os.widen(' '); |
| 4004 | __os.fill(__sp); |
| 4005 | return __os << __x.p(); |
| 4006 | } |
| 4007 | |
| 4008 | template <class _CharT, class _Traits> |
| 4009 | basic_istream<_CharT, _Traits>& |
| 4010 | operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) |
| 4011 | { |
| 4012 | typedef bernoulli_distribution _Eng; |
| 4013 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4014 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4015 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 4016 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4017 | double __p; |
| 4018 | __is >> __p; |
| 4019 | if (!__is.fail()) |
| 4020 | __x.param(param_type(__p)); |
| 4021 | return __is; |
| 4022 | } |
| 4023 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4024 | // binomial_distribution |
| 4025 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4026 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4027 | class _LIBCPP_TEMPLATE_VIS binomial_distribution |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4028 | { |
| 4029 | public: |
| 4030 | // types |
| 4031 | typedef _IntType result_type; |
| 4032 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4033 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4034 | { |
| 4035 | result_type __t_; |
| 4036 | double __p_; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4037 | double __pr_; |
| 4038 | double __odds_ratio_; |
| 4039 | result_type __r0_; |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4040 | public: |
| 4041 | typedef binomial_distribution distribution_type; |
| 4042 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4043 | explicit param_type(result_type __t = 1, double __p = 0.5); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4044 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4045 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4046 | result_type t() const {return __t_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4047 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4048 | double p() const {return __p_;} |
| 4049 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4050 | friend _LIBCPP_INLINE_VISIBILITY |
| 4051 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4052 | {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4053 | friend _LIBCPP_INLINE_VISIBILITY |
| 4054 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4055 | {return !(__x == __y);} |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4056 | |
| 4057 | friend class binomial_distribution; |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4058 | }; |
| 4059 | |
| 4060 | private: |
| 4061 | param_type __p_; |
| 4062 | |
| 4063 | public: |
| 4064 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4065 | #ifndef _LIBCPP_CXX03_LANG |
| 4066 | _LIBCPP_INLINE_VISIBILITY |
| 4067 | binomial_distribution() : binomial_distribution(1) {} |
| 4068 | _LIBCPP_INLINE_VISIBILITY |
| 4069 | explicit binomial_distribution(result_type __t, double __p = 0.5) |
| 4070 | : __p_(param_type(__t, __p)) {} |
| 4071 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4072 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4073 | explicit binomial_distribution(result_type __t = 1, double __p = 0.5) |
| 4074 | : __p_(param_type(__t, __p)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4075 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4076 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4077 | explicit binomial_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4078 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4079 | void reset() {} |
| 4080 | |
| 4081 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4082 | template<class _URNG> |
| 4083 | _LIBCPP_INLINE_VISIBILITY |
| 4084 | result_type operator()(_URNG& __g) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4085 | {return (*this)(__g, __p_);} |
| 4086 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4087 | |
| 4088 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4089 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4090 | result_type t() const {return __p_.t();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4091 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4092 | double p() const {return __p_.p();} |
| 4093 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4094 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4095 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4096 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4097 | void param(const param_type& __p) {__p_ = __p;} |
| 4098 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4099 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4100 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4101 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4102 | result_type max() const {return t();} |
| 4103 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4104 | friend _LIBCPP_INLINE_VISIBILITY |
| 4105 | bool operator==(const binomial_distribution& __x, |
| 4106 | const binomial_distribution& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4107 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4108 | friend _LIBCPP_INLINE_VISIBILITY |
| 4109 | bool operator!=(const binomial_distribution& __x, |
| 4110 | const binomial_distribution& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4111 | {return !(__x == __y);} |
| 4112 | }; |
| 4113 | |
Martin Storsjö | 408cee7 | 2020-12-01 13:37:16 +0200 | [diff] [blame] | 4114 | #ifndef _LIBCPP_MSVCRT_LIKE |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4115 | extern "C" double lgamma_r(double, int *); |
Eric Fiselier | 651ca6e | 2017-05-06 02:58:43 +0000 | [diff] [blame] | 4116 | #endif |
| 4117 | |
| 4118 | inline _LIBCPP_INLINE_VISIBILITY double __libcpp_lgamma(double __d) { |
Martin Storsjö | 408cee7 | 2020-12-01 13:37:16 +0200 | [diff] [blame] | 4119 | #if defined(_LIBCPP_MSVCRT_LIKE) |
Eric Fiselier | 651ca6e | 2017-05-06 02:58:43 +0000 | [diff] [blame] | 4120 | return lgamma(__d); |
| 4121 | #else |
| 4122 | int __sign; |
| 4123 | return lgamma_r(__d, &__sign); |
| 4124 | #endif |
| 4125 | } |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4126 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4127 | template<class _IntType> |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4128 | binomial_distribution<_IntType>::param_type::param_type(const result_type __t, const double __p) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4129 | : __t_(__t), __p_(__p) |
| 4130 | { |
| 4131 | if (0 < __p_ && __p_ < 1) |
| 4132 | { |
| 4133 | __r0_ = static_cast<result_type>((__t_ + 1) * __p_); |
Eric Fiselier | 651ca6e | 2017-05-06 02:58:43 +0000 | [diff] [blame] | 4134 | __pr_ = _VSTD::exp(__libcpp_lgamma(__t_ + 1.) - |
| 4135 | __libcpp_lgamma(__r0_ + 1.) - |
| 4136 | __libcpp_lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) + |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4137 | (__t_ - __r0_) * _VSTD::log(1 - __p_)); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4138 | __odds_ratio_ = __p_ / (1 - __p_); |
| 4139 | } |
| 4140 | } |
| 4141 | |
Marshall Clow | f97a84f | 2014-09-17 18:33:58 +0000 | [diff] [blame] | 4142 | // Reference: Kemp, C.D. (1986). `A modal method for generating binomial |
| 4143 | // variables', Commun. Statist. - Theor. Meth. 15(3), 805-813. |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4144 | template<class _IntType> |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4145 | template<class _URNG> |
| 4146 | _IntType |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4147 | binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4148 | { |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4149 | if (__pr.__t_ == 0 || __pr.__p_ == 0) |
| 4150 | return 0; |
| 4151 | if (__pr.__p_ == 1) |
| 4152 | return __pr.__t_; |
| 4153 | uniform_real_distribution<double> __gen; |
| 4154 | double __u = __gen(__g) - __pr.__pr_; |
| 4155 | if (__u < 0) |
| 4156 | return __pr.__r0_; |
| 4157 | double __pu = __pr.__pr_; |
| 4158 | double __pd = __pu; |
| 4159 | result_type __ru = __pr.__r0_; |
| 4160 | result_type __rd = __ru; |
| 4161 | while (true) |
| 4162 | { |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4163 | bool __break = true; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4164 | if (__rd >= 1) |
| 4165 | { |
| 4166 | __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1)); |
| 4167 | __u -= __pd; |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4168 | __break = false; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4169 | if (__u < 0) |
| 4170 | return __rd - 1; |
| 4171 | } |
Marshall Clow | f97a84f | 2014-09-17 18:33:58 +0000 | [diff] [blame] | 4172 | if ( __rd != 0 ) |
| 4173 | --__rd; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4174 | ++__ru; |
| 4175 | if (__ru <= __pr.__t_) |
| 4176 | { |
| 4177 | __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru; |
| 4178 | __u -= __pu; |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4179 | __break = false; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4180 | if (__u < 0) |
| 4181 | return __ru; |
| 4182 | } |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4183 | if (__break) |
| 4184 | return 0; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4185 | } |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | template <class _CharT, class _Traits, class _IntType> |
| 4189 | basic_ostream<_CharT, _Traits>& |
| 4190 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4191 | const binomial_distribution<_IntType>& __x) |
| 4192 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4193 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4194 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 4195 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 4196 | _OStream::scientific); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4197 | _CharT __sp = __os.widen(' '); |
| 4198 | __os.fill(__sp); |
| 4199 | return __os << __x.t() << __sp << __x.p(); |
| 4200 | } |
| 4201 | |
| 4202 | template <class _CharT, class _Traits, class _IntType> |
| 4203 | basic_istream<_CharT, _Traits>& |
| 4204 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4205 | binomial_distribution<_IntType>& __x) |
| 4206 | { |
| 4207 | typedef binomial_distribution<_IntType> _Eng; |
| 4208 | typedef typename _Eng::result_type result_type; |
| 4209 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4210 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4211 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 4212 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4213 | result_type __t; |
| 4214 | double __p; |
| 4215 | __is >> __t >> __p; |
| 4216 | if (!__is.fail()) |
| 4217 | __x.param(param_type(__t, __p)); |
| 4218 | return __is; |
| 4219 | } |
| 4220 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4221 | // exponential_distribution |
| 4222 | |
| 4223 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4224 | class _LIBCPP_TEMPLATE_VIS exponential_distribution |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4225 | { |
| 4226 | public: |
| 4227 | // types |
| 4228 | typedef _RealType result_type; |
| 4229 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4230 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4231 | { |
| 4232 | result_type __lambda_; |
| 4233 | public: |
| 4234 | typedef exponential_distribution distribution_type; |
| 4235 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4236 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4237 | explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {} |
| 4238 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4239 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4240 | result_type lambda() const {return __lambda_;} |
| 4241 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4242 | friend _LIBCPP_INLINE_VISIBILITY |
| 4243 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4244 | {return __x.__lambda_ == __y.__lambda_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4245 | friend _LIBCPP_INLINE_VISIBILITY |
| 4246 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4247 | {return !(__x == __y);} |
| 4248 | }; |
| 4249 | |
| 4250 | private: |
| 4251 | param_type __p_; |
| 4252 | |
| 4253 | public: |
| 4254 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4255 | #ifndef _LIBCPP_CXX03_LANG |
| 4256 | _LIBCPP_INLINE_VISIBILITY |
| 4257 | exponential_distribution() : exponential_distribution(1) {} |
| 4258 | _LIBCPP_INLINE_VISIBILITY |
| 4259 | explicit exponential_distribution(result_type __lambda) |
| 4260 | : __p_(param_type(__lambda)) {} |
| 4261 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4262 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4263 | explicit exponential_distribution(result_type __lambda = 1) |
| 4264 | : __p_(param_type(__lambda)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4265 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4266 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4267 | explicit exponential_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4268 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4269 | void reset() {} |
| 4270 | |
| 4271 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4272 | template<class _URNG> |
| 4273 | _LIBCPP_INLINE_VISIBILITY |
| 4274 | result_type operator()(_URNG& __g) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4275 | {return (*this)(__g, __p_);} |
| 4276 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4277 | |
| 4278 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4279 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4280 | result_type lambda() const {return __p_.lambda();} |
| 4281 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4282 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4283 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4284 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4285 | void param(const param_type& __p) {__p_ = __p;} |
| 4286 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4287 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4288 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4289 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 021f990 | 2010-05-16 17:56:20 +0000 | [diff] [blame] | 4290 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4291 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4292 | friend _LIBCPP_INLINE_VISIBILITY |
| 4293 | bool operator==(const exponential_distribution& __x, |
| 4294 | const exponential_distribution& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4295 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4296 | friend _LIBCPP_INLINE_VISIBILITY |
| 4297 | bool operator!=(const exponential_distribution& __x, |
| 4298 | const exponential_distribution& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4299 | {return !(__x == __y);} |
| 4300 | }; |
| 4301 | |
| 4302 | template <class _RealType> |
| 4303 | template<class _URNG> |
| 4304 | _RealType |
| 4305 | exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4306 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4307 | return -_VSTD::log |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4308 | ( |
| 4309 | result_type(1) - |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4310 | _VSTD::generate_canonical<result_type, |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4311 | numeric_limits<result_type>::digits>(__g) |
| 4312 | ) |
| 4313 | / __p.lambda(); |
| 4314 | } |
| 4315 | |
| 4316 | template <class _CharT, class _Traits, class _RealType> |
| 4317 | basic_ostream<_CharT, _Traits>& |
| 4318 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4319 | const exponential_distribution<_RealType>& __x) |
| 4320 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4321 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4322 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 4323 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 4324 | _OStream::scientific); |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4325 | return __os << __x.lambda(); |
| 4326 | } |
| 4327 | |
| 4328 | template <class _CharT, class _Traits, class _RealType> |
| 4329 | basic_istream<_CharT, _Traits>& |
| 4330 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4331 | exponential_distribution<_RealType>& __x) |
| 4332 | { |
| 4333 | typedef exponential_distribution<_RealType> _Eng; |
| 4334 | typedef typename _Eng::result_type result_type; |
| 4335 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4336 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4337 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 4338 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4339 | result_type __lambda; |
| 4340 | __is >> __lambda; |
| 4341 | if (!__is.fail()) |
| 4342 | __x.param(param_type(__lambda)); |
| 4343 | return __is; |
| 4344 | } |
| 4345 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4346 | // normal_distribution |
| 4347 | |
| 4348 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4349 | class _LIBCPP_TEMPLATE_VIS normal_distribution |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4350 | { |
| 4351 | public: |
| 4352 | // types |
| 4353 | typedef _RealType result_type; |
| 4354 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4355 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4356 | { |
| 4357 | result_type __mean_; |
| 4358 | result_type __stddev_; |
| 4359 | public: |
| 4360 | typedef normal_distribution distribution_type; |
| 4361 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4362 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4363 | explicit param_type(result_type __mean = 0, result_type __stddev = 1) |
| 4364 | : __mean_(__mean), __stddev_(__stddev) {} |
| 4365 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4366 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4367 | result_type mean() const {return __mean_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4368 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4369 | result_type stddev() const {return __stddev_;} |
| 4370 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4371 | friend _LIBCPP_INLINE_VISIBILITY |
| 4372 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4373 | {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4374 | friend _LIBCPP_INLINE_VISIBILITY |
| 4375 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4376 | {return !(__x == __y);} |
| 4377 | }; |
| 4378 | |
| 4379 | private: |
| 4380 | param_type __p_; |
| 4381 | result_type _V_; |
| 4382 | bool _V_hot_; |
| 4383 | |
| 4384 | public: |
| 4385 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4386 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4387 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4388 | normal_distribution() : normal_distribution(0) {} |
| 4389 | _LIBCPP_INLINE_VISIBILITY |
| 4390 | explicit normal_distribution(result_type __mean, result_type __stddev = 1) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4391 | : __p_(param_type(__mean, __stddev)), _V_hot_(false) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4392 | #else |
| 4393 | _LIBCPP_INLINE_VISIBILITY |
| 4394 | explicit normal_distribution(result_type __mean = 0, |
| 4395 | result_type __stddev = 1) |
| 4396 | : __p_(param_type(__mean, __stddev)), _V_hot_(false) {} |
| 4397 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4398 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4399 | explicit normal_distribution(const param_type& __p) |
| 4400 | : __p_(__p), _V_hot_(false) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4401 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4402 | void reset() {_V_hot_ = false;} |
| 4403 | |
| 4404 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4405 | template<class _URNG> |
| 4406 | _LIBCPP_INLINE_VISIBILITY |
| 4407 | result_type operator()(_URNG& __g) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4408 | {return (*this)(__g, __p_);} |
| 4409 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4410 | |
| 4411 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4412 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4413 | result_type mean() const {return __p_.mean();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4414 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4415 | result_type stddev() const {return __p_.stddev();} |
| 4416 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4417 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4418 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4419 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4420 | void param(const param_type& __p) {__p_ = __p;} |
| 4421 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4422 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4423 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4424 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4425 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4426 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4427 | friend _LIBCPP_INLINE_VISIBILITY |
| 4428 | bool operator==(const normal_distribution& __x, |
| 4429 | const normal_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4430 | {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ && |
| 4431 | (!__x._V_hot_ || __x._V_ == __y._V_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4432 | friend _LIBCPP_INLINE_VISIBILITY |
| 4433 | bool operator!=(const normal_distribution& __x, |
| 4434 | const normal_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4435 | {return !(__x == __y);} |
| 4436 | |
| 4437 | template <class _CharT, class _Traits, class _RT> |
| 4438 | friend |
| 4439 | basic_ostream<_CharT, _Traits>& |
| 4440 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4441 | const normal_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4442 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4443 | template <class _CharT, class _Traits, class _RT> |
| 4444 | friend |
| 4445 | basic_istream<_CharT, _Traits>& |
| 4446 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4447 | normal_distribution<_RT>& __x); |
| 4448 | }; |
| 4449 | |
| 4450 | template <class _RealType> |
| 4451 | template<class _URNG> |
| 4452 | _RealType |
| 4453 | normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4454 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4455 | result_type _Up; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4456 | if (_V_hot_) |
| 4457 | { |
| 4458 | _V_hot_ = false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4459 | _Up = _V_; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4460 | } |
| 4461 | else |
| 4462 | { |
| 4463 | uniform_real_distribution<result_type> _Uni(-1, 1); |
| 4464 | result_type __u; |
| 4465 | result_type __v; |
| 4466 | result_type __s; |
| 4467 | do |
| 4468 | { |
| 4469 | __u = _Uni(__g); |
| 4470 | __v = _Uni(__g); |
| 4471 | __s = __u * __u + __v * __v; |
| 4472 | } while (__s > 1 || __s == 0); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4473 | result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s); |
| 4474 | _V_ = __v * _Fp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4475 | _V_hot_ = true; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4476 | _Up = __u * _Fp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4477 | } |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4478 | return _Up * __p.stddev() + __p.mean(); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | template <class _CharT, class _Traits, class _RT> |
| 4482 | basic_ostream<_CharT, _Traits>& |
| 4483 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4484 | const normal_distribution<_RT>& __x) |
| 4485 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4486 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4487 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 4488 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 4489 | _OStream::scientific); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4490 | _CharT __sp = __os.widen(' '); |
| 4491 | __os.fill(__sp); |
| 4492 | __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_; |
| 4493 | if (__x._V_hot_) |
| 4494 | __os << __sp << __x._V_; |
| 4495 | return __os; |
| 4496 | } |
| 4497 | |
| 4498 | template <class _CharT, class _Traits, class _RT> |
| 4499 | basic_istream<_CharT, _Traits>& |
| 4500 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4501 | normal_distribution<_RT>& __x) |
| 4502 | { |
| 4503 | typedef normal_distribution<_RT> _Eng; |
| 4504 | typedef typename _Eng::result_type result_type; |
| 4505 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4506 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4507 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 4508 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4509 | result_type __mean; |
| 4510 | result_type __stddev; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4511 | result_type _Vp = 0; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4512 | bool _V_hot = false; |
| 4513 | __is >> __mean >> __stddev >> _V_hot; |
| 4514 | if (_V_hot) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4515 | __is >> _Vp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4516 | if (!__is.fail()) |
| 4517 | { |
| 4518 | __x.param(param_type(__mean, __stddev)); |
| 4519 | __x._V_hot_ = _V_hot; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4520 | __x._V_ = _Vp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4521 | } |
| 4522 | return __is; |
| 4523 | } |
| 4524 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4525 | // lognormal_distribution |
| 4526 | |
| 4527 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4528 | class _LIBCPP_TEMPLATE_VIS lognormal_distribution |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4529 | { |
| 4530 | public: |
| 4531 | // types |
| 4532 | typedef _RealType result_type; |
| 4533 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4534 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4535 | { |
| 4536 | normal_distribution<result_type> __nd_; |
| 4537 | public: |
| 4538 | typedef lognormal_distribution distribution_type; |
| 4539 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4540 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4541 | explicit param_type(result_type __m = 0, result_type __s = 1) |
| 4542 | : __nd_(__m, __s) {} |
| 4543 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4544 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4545 | result_type m() const {return __nd_.mean();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4546 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4547 | result_type s() const {return __nd_.stddev();} |
| 4548 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4549 | friend _LIBCPP_INLINE_VISIBILITY |
| 4550 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4551 | {return __x.__nd_ == __y.__nd_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4552 | friend _LIBCPP_INLINE_VISIBILITY |
| 4553 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4554 | {return !(__x == __y);} |
| 4555 | friend class lognormal_distribution; |
| 4556 | |
| 4557 | template <class _CharT, class _Traits, class _RT> |
| 4558 | friend |
| 4559 | basic_ostream<_CharT, _Traits>& |
| 4560 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4561 | const lognormal_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4562 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4563 | template <class _CharT, class _Traits, class _RT> |
| 4564 | friend |
| 4565 | basic_istream<_CharT, _Traits>& |
| 4566 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4567 | lognormal_distribution<_RT>& __x); |
| 4568 | }; |
| 4569 | |
| 4570 | private: |
| 4571 | param_type __p_; |
| 4572 | |
| 4573 | public: |
| 4574 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4575 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4576 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4577 | lognormal_distribution() : lognormal_distribution(0) {} |
| 4578 | _LIBCPP_INLINE_VISIBILITY |
| 4579 | explicit lognormal_distribution(result_type __m, result_type __s = 1) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4580 | : __p_(param_type(__m, __s)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4581 | #else |
| 4582 | _LIBCPP_INLINE_VISIBILITY |
| 4583 | explicit lognormal_distribution(result_type __m = 0, |
| 4584 | result_type __s = 1) |
| 4585 | : __p_(param_type(__m, __s)) {} |
| 4586 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4587 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4588 | explicit lognormal_distribution(const param_type& __p) |
| 4589 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4590 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4591 | void reset() {__p_.__nd_.reset();} |
| 4592 | |
| 4593 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4594 | template<class _URNG> |
| 4595 | _LIBCPP_INLINE_VISIBILITY |
| 4596 | result_type operator()(_URNG& __g) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4597 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4598 | template<class _URNG> |
| 4599 | _LIBCPP_INLINE_VISIBILITY |
| 4600 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4601 | {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));} |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4602 | |
| 4603 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4604 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4605 | result_type m() const {return __p_.m();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4606 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4607 | result_type s() const {return __p_.s();} |
| 4608 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4609 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4610 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4611 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 4612 | void param(const param_type& __p) {__p_ = __p;} |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4613 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4614 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4615 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4616 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4617 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4618 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4619 | friend _LIBCPP_INLINE_VISIBILITY |
| 4620 | bool operator==(const lognormal_distribution& __x, |
| 4621 | const lognormal_distribution& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4622 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4623 | friend _LIBCPP_INLINE_VISIBILITY |
| 4624 | bool operator!=(const lognormal_distribution& __x, |
| 4625 | const lognormal_distribution& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4626 | {return !(__x == __y);} |
| 4627 | |
| 4628 | template <class _CharT, class _Traits, class _RT> |
| 4629 | friend |
| 4630 | basic_ostream<_CharT, _Traits>& |
| 4631 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4632 | const lognormal_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4633 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4634 | template <class _CharT, class _Traits, class _RT> |
| 4635 | friend |
| 4636 | basic_istream<_CharT, _Traits>& |
| 4637 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4638 | lognormal_distribution<_RT>& __x); |
| 4639 | }; |
| 4640 | |
| 4641 | template <class _CharT, class _Traits, class _RT> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4642 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4643 | basic_ostream<_CharT, _Traits>& |
| 4644 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4645 | const lognormal_distribution<_RT>& __x) |
| 4646 | { |
| 4647 | return __os << __x.__p_.__nd_; |
| 4648 | } |
| 4649 | |
| 4650 | template <class _CharT, class _Traits, class _RT> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4651 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4652 | basic_istream<_CharT, _Traits>& |
| 4653 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4654 | lognormal_distribution<_RT>& __x) |
| 4655 | { |
| 4656 | return __is >> __x.__p_.__nd_; |
| 4657 | } |
| 4658 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4659 | // poisson_distribution |
| 4660 | |
| 4661 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4662 | class _LIBCPP_TEMPLATE_VIS poisson_distribution |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4663 | { |
| 4664 | public: |
| 4665 | // types |
| 4666 | typedef _IntType result_type; |
| 4667 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4668 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4669 | { |
| 4670 | double __mean_; |
| 4671 | double __s_; |
| 4672 | double __d_; |
| 4673 | double __l_; |
| 4674 | double __omega_; |
| 4675 | double __c0_; |
| 4676 | double __c1_; |
| 4677 | double __c2_; |
| 4678 | double __c3_; |
| 4679 | double __c_; |
| 4680 | |
| 4681 | public: |
| 4682 | typedef poisson_distribution distribution_type; |
| 4683 | |
| 4684 | explicit param_type(double __mean = 1.0); |
| 4685 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4686 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4687 | double mean() const {return __mean_;} |
| 4688 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4689 | friend _LIBCPP_INLINE_VISIBILITY |
| 4690 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4691 | {return __x.__mean_ == __y.__mean_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4692 | friend _LIBCPP_INLINE_VISIBILITY |
| 4693 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4694 | {return !(__x == __y);} |
| 4695 | |
| 4696 | friend class poisson_distribution; |
| 4697 | }; |
| 4698 | |
| 4699 | private: |
| 4700 | param_type __p_; |
| 4701 | |
| 4702 | public: |
| 4703 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4704 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4705 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4706 | poisson_distribution() : poisson_distribution(1.0) {} |
| 4707 | _LIBCPP_INLINE_VISIBILITY |
| 4708 | explicit poisson_distribution(double __mean) |
| 4709 | : __p_(__mean) {} |
| 4710 | #else |
| 4711 | _LIBCPP_INLINE_VISIBILITY |
| 4712 | explicit poisson_distribution(double __mean = 1.0) |
| 4713 | : __p_(__mean) {} |
| 4714 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4715 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4716 | explicit poisson_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4717 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4718 | void reset() {} |
| 4719 | |
| 4720 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4721 | template<class _URNG> |
| 4722 | _LIBCPP_INLINE_VISIBILITY |
| 4723 | result_type operator()(_URNG& __g) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4724 | {return (*this)(__g, __p_);} |
| 4725 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4726 | |
| 4727 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4728 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4729 | double mean() const {return __p_.mean();} |
| 4730 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4731 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4732 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4733 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4734 | void param(const param_type& __p) {__p_ = __p;} |
| 4735 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4736 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4737 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4738 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4739 | result_type max() const {return numeric_limits<result_type>::max();} |
| 4740 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4741 | friend _LIBCPP_INLINE_VISIBILITY |
| 4742 | bool operator==(const poisson_distribution& __x, |
| 4743 | const poisson_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4744 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4745 | friend _LIBCPP_INLINE_VISIBILITY |
| 4746 | bool operator!=(const poisson_distribution& __x, |
| 4747 | const poisson_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4748 | {return !(__x == __y);} |
| 4749 | }; |
| 4750 | |
| 4751 | template<class _IntType> |
| 4752 | poisson_distribution<_IntType>::param_type::param_type(double __mean) |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4753 | // According to the standard `inf` is a valid input, but it causes the |
| 4754 | // distribution to hang, so we replace it with the maximum representable |
| 4755 | // mean. |
| 4756 | : __mean_(isinf(__mean) ? numeric_limits<double>::max() : __mean) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4757 | { |
| 4758 | if (__mean_ < 10) |
| 4759 | { |
| 4760 | __s_ = 0; |
| 4761 | __d_ = 0; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4762 | __l_ = _VSTD::exp(-__mean_); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4763 | __omega_ = 0; |
| 4764 | __c3_ = 0; |
| 4765 | __c2_ = 0; |
| 4766 | __c1_ = 0; |
| 4767 | __c0_ = 0; |
| 4768 | __c_ = 0; |
| 4769 | } |
| 4770 | else |
| 4771 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4772 | __s_ = _VSTD::sqrt(__mean_); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4773 | __d_ = 6 * __mean_ * __mean_; |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4774 | __l_ = _VSTD::trunc(__mean_ - 1.1484); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4775 | __omega_ = .3989423 / __s_; |
| 4776 | double __b1_ = .4166667E-1 / __mean_; |
| 4777 | double __b2_ = .3 * __b1_ * __b1_; |
| 4778 | __c3_ = .1428571 * __b1_ * __b2_; |
| 4779 | __c2_ = __b2_ - 15. * __c3_; |
| 4780 | __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_; |
| 4781 | __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_; |
| 4782 | __c_ = .1069 / __mean_; |
| 4783 | } |
| 4784 | } |
| 4785 | |
| 4786 | template <class _IntType> |
| 4787 | template<class _URNG> |
| 4788 | _IntType |
| 4789 | poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 4790 | { |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4791 | double __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4792 | uniform_real_distribution<double> __urd; |
Howard Hinnant | a5fb7ac | 2011-04-14 15:59:22 +0000 | [diff] [blame] | 4793 | if (__pr.__mean_ < 10) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4794 | { |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4795 | __tx = 0; |
| 4796 | for (double __p = __urd(__urng); __p > __pr.__l_; ++__tx) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4797 | __p *= __urd(__urng); |
| 4798 | } |
| 4799 | else |
| 4800 | { |
| 4801 | double __difmuk; |
| 4802 | double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng); |
| 4803 | double __u; |
| 4804 | if (__g > 0) |
| 4805 | { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4806 | __tx = _VSTD::trunc(__g); |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4807 | if (__tx >= __pr.__l_) |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4808 | return _VSTD::__clamp_to_integral<result_type>(__tx); |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4809 | __difmuk = __pr.__mean_ - __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4810 | __u = __urd(__urng); |
| 4811 | if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk) |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4812 | return _VSTD::__clamp_to_integral<result_type>(__tx); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4813 | } |
| 4814 | exponential_distribution<double> __edist; |
| 4815 | for (bool __using_exp_dist = false; true; __using_exp_dist = true) |
| 4816 | { |
| 4817 | double __e; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4818 | if (__using_exp_dist || __g <= 0) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4819 | { |
| 4820 | double __t; |
| 4821 | do |
| 4822 | { |
| 4823 | __e = __edist(__urng); |
| 4824 | __u = __urd(__urng); |
| 4825 | __u += __u - 1; |
| 4826 | __t = 1.8 + (__u < 0 ? -__e : __e); |
| 4827 | } while (__t <= -.6744); |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4828 | __tx = _VSTD::trunc(__pr.__mean_ + __pr.__s_ * __t); |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4829 | __difmuk = __pr.__mean_ - __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4830 | __using_exp_dist = true; |
| 4831 | } |
| 4832 | double __px; |
| 4833 | double __py; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4834 | if (__tx < 10 && __tx >= 0) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4835 | { |
Marshall Clow | c7e36e8 | 2018-01-16 14:54:36 +0000 | [diff] [blame] | 4836 | const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4837 | 40320, 362880}; |
| 4838 | __px = -__pr.__mean_; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4839 | __py = _VSTD::pow(__pr.__mean_, (double)__tx) / __fac[static_cast<int>(__tx)]; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4840 | } |
| 4841 | else |
| 4842 | { |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4843 | double __del = .8333333E-1 / __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4844 | __del -= 4.8 * __del * __del * __del; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4845 | double __v = __difmuk / __tx; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4846 | if (_VSTD::abs(__v) > 0.25) |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4847 | __px = __tx * _VSTD::log(1 + __v) - __difmuk - __del; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4848 | else |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4849 | __px = __tx * __v * __v * (((((((.1250060 * __v + -.1384794) * |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4850 | __v + .1421878) * __v + -.1661269) * __v + .2000118) * |
| 4851 | __v + -.2500068) * __v + .3333333) * __v + -.5) - __del; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4852 | __py = .3989423 / _VSTD::sqrt(__tx); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4853 | } |
| 4854 | double __r = (0.5 - __difmuk) / __pr.__s_; |
| 4855 | double __r2 = __r * __r; |
| 4856 | double __fx = -0.5 * __r2; |
| 4857 | double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) * |
| 4858 | __r2 + __pr.__c1_) * __r2 + __pr.__c0_); |
| 4859 | if (__using_exp_dist) |
| 4860 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4861 | if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) - |
| 4862 | __fy * _VSTD::exp(__fx + __e)) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4863 | break; |
| 4864 | } |
| 4865 | else |
| 4866 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4867 | if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx)) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4868 | break; |
| 4869 | } |
| 4870 | } |
| 4871 | } |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4872 | return _VSTD::__clamp_to_integral<result_type>(__tx); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | template <class _CharT, class _Traits, class _IntType> |
| 4876 | basic_ostream<_CharT, _Traits>& |
| 4877 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4878 | const poisson_distribution<_IntType>& __x) |
| 4879 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4880 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4881 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 4882 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 4883 | _OStream::scientific); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4884 | return __os << __x.mean(); |
| 4885 | } |
| 4886 | |
| 4887 | template <class _CharT, class _Traits, class _IntType> |
| 4888 | basic_istream<_CharT, _Traits>& |
| 4889 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4890 | poisson_distribution<_IntType>& __x) |
| 4891 | { |
| 4892 | typedef poisson_distribution<_IntType> _Eng; |
| 4893 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4894 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4895 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 4896 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4897 | double __mean; |
| 4898 | __is >> __mean; |
| 4899 | if (!__is.fail()) |
| 4900 | __x.param(param_type(__mean)); |
| 4901 | return __is; |
| 4902 | } |
| 4903 | |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4904 | // weibull_distribution |
| 4905 | |
| 4906 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4907 | class _LIBCPP_TEMPLATE_VIS weibull_distribution |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4908 | { |
| 4909 | public: |
| 4910 | // types |
| 4911 | typedef _RealType result_type; |
| 4912 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4913 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4914 | { |
| 4915 | result_type __a_; |
| 4916 | result_type __b_; |
| 4917 | public: |
| 4918 | typedef weibull_distribution distribution_type; |
| 4919 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4920 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4921 | explicit param_type(result_type __a = 1, result_type __b = 1) |
| 4922 | : __a_(__a), __b_(__b) {} |
| 4923 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4924 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4925 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4926 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4927 | result_type b() const {return __b_;} |
| 4928 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4929 | friend _LIBCPP_INLINE_VISIBILITY |
| 4930 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4931 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4932 | friend _LIBCPP_INLINE_VISIBILITY |
| 4933 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4934 | {return !(__x == __y);} |
| 4935 | }; |
| 4936 | |
| 4937 | private: |
| 4938 | param_type __p_; |
| 4939 | |
| 4940 | public: |
| 4941 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4942 | #ifndef _LIBCPP_CXX03_LANG |
| 4943 | _LIBCPP_INLINE_VISIBILITY |
| 4944 | weibull_distribution() : weibull_distribution(1) {} |
| 4945 | _LIBCPP_INLINE_VISIBILITY |
| 4946 | explicit weibull_distribution(result_type __a, result_type __b = 1) |
| 4947 | : __p_(param_type(__a, __b)) {} |
| 4948 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4949 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4950 | explicit weibull_distribution(result_type __a = 1, result_type __b = 1) |
| 4951 | : __p_(param_type(__a, __b)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 4952 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4953 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4954 | explicit weibull_distribution(const param_type& __p) |
| 4955 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4956 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4957 | void reset() {} |
| 4958 | |
| 4959 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4960 | template<class _URNG> |
| 4961 | _LIBCPP_INLINE_VISIBILITY |
| 4962 | result_type operator()(_URNG& __g) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4963 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4964 | template<class _URNG> |
| 4965 | _LIBCPP_INLINE_VISIBILITY |
| 4966 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4967 | {return __p.b() * |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4968 | _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());} |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4969 | |
| 4970 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4971 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4972 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4973 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4974 | result_type b() const {return __p_.b();} |
| 4975 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4976 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4977 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4978 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4979 | void param(const param_type& __p) {__p_ = __p;} |
| 4980 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4981 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4982 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4983 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4984 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4985 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4986 | friend _LIBCPP_INLINE_VISIBILITY |
| 4987 | bool operator==(const weibull_distribution& __x, |
| 4988 | const weibull_distribution& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4989 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4990 | friend _LIBCPP_INLINE_VISIBILITY |
| 4991 | bool operator!=(const weibull_distribution& __x, |
| 4992 | const weibull_distribution& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4993 | {return !(__x == __y);} |
| 4994 | }; |
| 4995 | |
| 4996 | template <class _CharT, class _Traits, class _RT> |
| 4997 | basic_ostream<_CharT, _Traits>& |
| 4998 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4999 | const weibull_distribution<_RT>& __x) |
| 5000 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5001 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5002 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5003 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5004 | _OStream::scientific); |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 5005 | _CharT __sp = __os.widen(' '); |
| 5006 | __os.fill(__sp); |
| 5007 | __os << __x.a() << __sp << __x.b(); |
| 5008 | return __os; |
| 5009 | } |
| 5010 | |
| 5011 | template <class _CharT, class _Traits, class _RT> |
| 5012 | basic_istream<_CharT, _Traits>& |
| 5013 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5014 | weibull_distribution<_RT>& __x) |
| 5015 | { |
| 5016 | typedef weibull_distribution<_RT> _Eng; |
| 5017 | typedef typename _Eng::result_type result_type; |
| 5018 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5019 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5020 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5021 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 5022 | result_type __a; |
| 5023 | result_type __b; |
| 5024 | __is >> __a >> __b; |
| 5025 | if (!__is.fail()) |
| 5026 | __x.param(param_type(__a, __b)); |
| 5027 | return __is; |
| 5028 | } |
| 5029 | |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5030 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5031 | class _LIBCPP_TEMPLATE_VIS extreme_value_distribution |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5032 | { |
| 5033 | public: |
| 5034 | // types |
| 5035 | typedef _RealType result_type; |
| 5036 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5037 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5038 | { |
| 5039 | result_type __a_; |
| 5040 | result_type __b_; |
| 5041 | public: |
| 5042 | typedef extreme_value_distribution distribution_type; |
| 5043 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5044 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5045 | explicit param_type(result_type __a = 0, result_type __b = 1) |
| 5046 | : __a_(__a), __b_(__b) {} |
| 5047 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5048 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5049 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5050 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5051 | result_type b() const {return __b_;} |
| 5052 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5053 | friend _LIBCPP_INLINE_VISIBILITY |
| 5054 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5055 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5056 | friend _LIBCPP_INLINE_VISIBILITY |
| 5057 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5058 | {return !(__x == __y);} |
| 5059 | }; |
| 5060 | |
| 5061 | private: |
| 5062 | param_type __p_; |
| 5063 | |
| 5064 | public: |
| 5065 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5066 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5067 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5068 | extreme_value_distribution() : extreme_value_distribution(0) {} |
| 5069 | _LIBCPP_INLINE_VISIBILITY |
| 5070 | explicit extreme_value_distribution(result_type __a, result_type __b = 1) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5071 | : __p_(param_type(__a, __b)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5072 | #else |
| 5073 | _LIBCPP_INLINE_VISIBILITY |
| 5074 | explicit extreme_value_distribution(result_type __a = 0, |
| 5075 | result_type __b = 1) |
| 5076 | : __p_(param_type(__a, __b)) {} |
| 5077 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5078 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5079 | explicit extreme_value_distribution(const param_type& __p) |
| 5080 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5081 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5082 | void reset() {} |
| 5083 | |
| 5084 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5085 | template<class _URNG> |
| 5086 | _LIBCPP_INLINE_VISIBILITY |
| 5087 | result_type operator()(_URNG& __g) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5088 | {return (*this)(__g, __p_);} |
| 5089 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5090 | |
| 5091 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5092 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5093 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5094 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5095 | result_type b() const {return __p_.b();} |
| 5096 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5097 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5098 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5099 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5100 | void param(const param_type& __p) {__p_ = __p;} |
| 5101 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5102 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5103 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5104 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5105 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 5106 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5107 | friend _LIBCPP_INLINE_VISIBILITY |
| 5108 | bool operator==(const extreme_value_distribution& __x, |
| 5109 | const extreme_value_distribution& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5110 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5111 | friend _LIBCPP_INLINE_VISIBILITY |
| 5112 | bool operator!=(const extreme_value_distribution& __x, |
| 5113 | const extreme_value_distribution& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5114 | {return !(__x == __y);} |
| 5115 | }; |
| 5116 | |
| 5117 | template<class _RealType> |
| 5118 | template<class _URNG> |
| 5119 | _RealType |
| 5120 | extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5121 | { |
| 5122 | return __p.a() - __p.b() * |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5123 | _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g))); |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | template <class _CharT, class _Traits, class _RT> |
| 5127 | basic_ostream<_CharT, _Traits>& |
| 5128 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5129 | const extreme_value_distribution<_RT>& __x) |
| 5130 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5131 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5132 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5133 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5134 | _OStream::scientific); |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5135 | _CharT __sp = __os.widen(' '); |
| 5136 | __os.fill(__sp); |
| 5137 | __os << __x.a() << __sp << __x.b(); |
| 5138 | return __os; |
| 5139 | } |
| 5140 | |
| 5141 | template <class _CharT, class _Traits, class _RT> |
| 5142 | basic_istream<_CharT, _Traits>& |
| 5143 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5144 | extreme_value_distribution<_RT>& __x) |
| 5145 | { |
| 5146 | typedef extreme_value_distribution<_RT> _Eng; |
| 5147 | typedef typename _Eng::result_type result_type; |
| 5148 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5149 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5150 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5151 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5152 | result_type __a; |
| 5153 | result_type __b; |
| 5154 | __is >> __a >> __b; |
| 5155 | if (!__is.fail()) |
| 5156 | __x.param(param_type(__a, __b)); |
| 5157 | return __is; |
| 5158 | } |
| 5159 | |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5160 | // gamma_distribution |
| 5161 | |
| 5162 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5163 | class _LIBCPP_TEMPLATE_VIS gamma_distribution |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5164 | { |
| 5165 | public: |
| 5166 | // types |
| 5167 | typedef _RealType result_type; |
| 5168 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5169 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5170 | { |
| 5171 | result_type __alpha_; |
| 5172 | result_type __beta_; |
| 5173 | public: |
| 5174 | typedef gamma_distribution distribution_type; |
| 5175 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5176 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5177 | explicit param_type(result_type __alpha = 1, result_type __beta = 1) |
| 5178 | : __alpha_(__alpha), __beta_(__beta) {} |
| 5179 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5180 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5181 | result_type alpha() const {return __alpha_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5182 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5183 | result_type beta() const {return __beta_;} |
| 5184 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5185 | friend _LIBCPP_INLINE_VISIBILITY |
| 5186 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5187 | {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5188 | friend _LIBCPP_INLINE_VISIBILITY |
| 5189 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5190 | {return !(__x == __y);} |
| 5191 | }; |
| 5192 | |
| 5193 | private: |
| 5194 | param_type __p_; |
| 5195 | |
| 5196 | public: |
| 5197 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5198 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5199 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5200 | gamma_distribution() : gamma_distribution(1) {} |
| 5201 | _LIBCPP_INLINE_VISIBILITY |
| 5202 | explicit gamma_distribution(result_type __alpha, result_type __beta = 1) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5203 | : __p_(param_type(__alpha, __beta)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5204 | #else |
| 5205 | _LIBCPP_INLINE_VISIBILITY |
| 5206 | explicit gamma_distribution(result_type __alpha = 1, |
| 5207 | result_type __beta = 1) |
| 5208 | : __p_(param_type(__alpha, __beta)) {} |
| 5209 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5210 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5211 | explicit gamma_distribution(const param_type& __p) |
| 5212 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5213 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5214 | void reset() {} |
| 5215 | |
| 5216 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5217 | template<class _URNG> |
| 5218 | _LIBCPP_INLINE_VISIBILITY |
| 5219 | result_type operator()(_URNG& __g) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5220 | {return (*this)(__g, __p_);} |
| 5221 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5222 | |
| 5223 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5224 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5225 | result_type alpha() const {return __p_.alpha();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5226 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5227 | result_type beta() const {return __p_.beta();} |
| 5228 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5229 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5230 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5231 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5232 | void param(const param_type& __p) {__p_ = __p;} |
| 5233 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5234 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5235 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5236 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5237 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5238 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5239 | friend _LIBCPP_INLINE_VISIBILITY |
| 5240 | bool operator==(const gamma_distribution& __x, |
| 5241 | const gamma_distribution& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5242 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5243 | friend _LIBCPP_INLINE_VISIBILITY |
| 5244 | bool operator!=(const gamma_distribution& __x, |
| 5245 | const gamma_distribution& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5246 | {return !(__x == __y);} |
| 5247 | }; |
| 5248 | |
| 5249 | template <class _RealType> |
| 5250 | template<class _URNG> |
| 5251 | _RealType |
| 5252 | gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5253 | { |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5254 | result_type __a = __p.alpha(); |
| 5255 | uniform_real_distribution<result_type> __gen(0, 1); |
| 5256 | exponential_distribution<result_type> __egen; |
| 5257 | result_type __x; |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5258 | if (__a == 1) |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5259 | __x = __egen(__g); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5260 | else if (__a > 1) |
| 5261 | { |
| 5262 | const result_type __b = __a - 1; |
| 5263 | const result_type __c = 3 * __a - result_type(0.75); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5264 | while (true) |
| 5265 | { |
| 5266 | const result_type __u = __gen(__g); |
| 5267 | const result_type __v = __gen(__g); |
| 5268 | const result_type __w = __u * (1 - __u); |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5269 | if (__w != 0) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5270 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5271 | const result_type __y = _VSTD::sqrt(__c / __w) * |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5272 | (__u - result_type(0.5)); |
| 5273 | __x = __b + __y; |
| 5274 | if (__x >= 0) |
| 5275 | { |
| 5276 | const result_type __z = 64 * __w * __w * __w * __v * __v; |
| 5277 | if (__z <= 1 - 2 * __y * __y / __x) |
| 5278 | break; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5279 | if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y)) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5280 | break; |
| 5281 | } |
| 5282 | } |
| 5283 | } |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5284 | } |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5285 | else // __a < 1 |
| 5286 | { |
| 5287 | while (true) |
| 5288 | { |
| 5289 | const result_type __u = __gen(__g); |
| 5290 | const result_type __es = __egen(__g); |
| 5291 | if (__u <= 1 - __a) |
| 5292 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5293 | __x = _VSTD::pow(__u, 1 / __a); |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5294 | if (__x <= __es) |
| 5295 | break; |
| 5296 | } |
| 5297 | else |
| 5298 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5299 | const result_type __e = -_VSTD::log((1-__u)/__a); |
| 5300 | __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a); |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5301 | if (__x <= __e + __es) |
| 5302 | break; |
| 5303 | } |
| 5304 | } |
| 5305 | } |
| 5306 | return __x * __p.beta(); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5307 | } |
| 5308 | |
| 5309 | template <class _CharT, class _Traits, class _RT> |
| 5310 | basic_ostream<_CharT, _Traits>& |
| 5311 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5312 | const gamma_distribution<_RT>& __x) |
| 5313 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5314 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5315 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5316 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5317 | _OStream::scientific); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5318 | _CharT __sp = __os.widen(' '); |
| 5319 | __os.fill(__sp); |
| 5320 | __os << __x.alpha() << __sp << __x.beta(); |
| 5321 | return __os; |
| 5322 | } |
| 5323 | |
| 5324 | template <class _CharT, class _Traits, class _RT> |
| 5325 | basic_istream<_CharT, _Traits>& |
| 5326 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5327 | gamma_distribution<_RT>& __x) |
| 5328 | { |
| 5329 | typedef gamma_distribution<_RT> _Eng; |
| 5330 | typedef typename _Eng::result_type result_type; |
| 5331 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5332 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5333 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5334 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5335 | result_type __alpha; |
| 5336 | result_type __beta; |
| 5337 | __is >> __alpha >> __beta; |
| 5338 | if (!__is.fail()) |
| 5339 | __x.param(param_type(__alpha, __beta)); |
| 5340 | return __is; |
| 5341 | } |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 5342 | |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5343 | // negative_binomial_distribution |
| 5344 | |
| 5345 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5346 | class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5347 | { |
| 5348 | public: |
| 5349 | // types |
| 5350 | typedef _IntType result_type; |
| 5351 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5352 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5353 | { |
| 5354 | result_type __k_; |
| 5355 | double __p_; |
| 5356 | public: |
| 5357 | typedef negative_binomial_distribution distribution_type; |
| 5358 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5359 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5360 | explicit param_type(result_type __k = 1, double __p = 0.5) |
| 5361 | : __k_(__k), __p_(__p) {} |
| 5362 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5363 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5364 | result_type k() const {return __k_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5365 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5366 | double p() const {return __p_;} |
| 5367 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5368 | friend _LIBCPP_INLINE_VISIBILITY |
| 5369 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5370 | {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5371 | friend _LIBCPP_INLINE_VISIBILITY |
| 5372 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5373 | {return !(__x == __y);} |
| 5374 | }; |
| 5375 | |
| 5376 | private: |
| 5377 | param_type __p_; |
| 5378 | |
| 5379 | public: |
| 5380 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5381 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5382 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5383 | negative_binomial_distribution() : negative_binomial_distribution(1) {} |
| 5384 | _LIBCPP_INLINE_VISIBILITY |
| 5385 | explicit negative_binomial_distribution(result_type __k, double __p = 0.5) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5386 | : __p_(__k, __p) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5387 | #else |
| 5388 | _LIBCPP_INLINE_VISIBILITY |
| 5389 | explicit negative_binomial_distribution(result_type __k = 1, |
| 5390 | double __p = 0.5) |
| 5391 | : __p_(__k, __p) {} |
| 5392 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5393 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5394 | explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5395 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5396 | void reset() {} |
| 5397 | |
| 5398 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5399 | template<class _URNG> |
| 5400 | _LIBCPP_INLINE_VISIBILITY |
| 5401 | result_type operator()(_URNG& __g) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5402 | {return (*this)(__g, __p_);} |
| 5403 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5404 | |
| 5405 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5406 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5407 | result_type k() const {return __p_.k();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5408 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5409 | double p() const {return __p_.p();} |
| 5410 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5411 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5412 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5413 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5414 | void param(const param_type& __p) {__p_ = __p;} |
| 5415 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5416 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5417 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5418 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5419 | result_type max() const {return numeric_limits<result_type>::max();} |
| 5420 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5421 | friend _LIBCPP_INLINE_VISIBILITY |
| 5422 | bool operator==(const negative_binomial_distribution& __x, |
| 5423 | const negative_binomial_distribution& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5424 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5425 | friend _LIBCPP_INLINE_VISIBILITY |
| 5426 | bool operator!=(const negative_binomial_distribution& __x, |
| 5427 | const negative_binomial_distribution& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5428 | {return !(__x == __y);} |
| 5429 | }; |
| 5430 | |
| 5431 | template <class _IntType> |
| 5432 | template<class _URNG> |
| 5433 | _IntType |
| 5434 | negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 5435 | { |
| 5436 | result_type __k = __pr.k(); |
| 5437 | double __p = __pr.p(); |
| 5438 | if (__k <= 21 * __p) |
| 5439 | { |
| 5440 | bernoulli_distribution __gen(__p); |
| 5441 | result_type __f = 0; |
| 5442 | result_type __s = 0; |
| 5443 | while (__s < __k) |
| 5444 | { |
| 5445 | if (__gen(__urng)) |
| 5446 | ++__s; |
| 5447 | else |
| 5448 | ++__f; |
| 5449 | } |
| 5450 | return __f; |
| 5451 | } |
| 5452 | return poisson_distribution<result_type>(gamma_distribution<double> |
| 5453 | (__k, (1-__p)/__p)(__urng))(__urng); |
| 5454 | } |
| 5455 | |
| 5456 | template <class _CharT, class _Traits, class _IntType> |
| 5457 | basic_ostream<_CharT, _Traits>& |
| 5458 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5459 | const negative_binomial_distribution<_IntType>& __x) |
| 5460 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5461 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5462 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5463 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5464 | _OStream::scientific); |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5465 | _CharT __sp = __os.widen(' '); |
| 5466 | __os.fill(__sp); |
| 5467 | return __os << __x.k() << __sp << __x.p(); |
| 5468 | } |
| 5469 | |
| 5470 | template <class _CharT, class _Traits, class _IntType> |
| 5471 | basic_istream<_CharT, _Traits>& |
| 5472 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5473 | negative_binomial_distribution<_IntType>& __x) |
| 5474 | { |
| 5475 | typedef negative_binomial_distribution<_IntType> _Eng; |
| 5476 | typedef typename _Eng::result_type result_type; |
| 5477 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5478 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5479 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5480 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5481 | result_type __k; |
| 5482 | double __p; |
| 5483 | __is >> __k >> __p; |
| 5484 | if (!__is.fail()) |
| 5485 | __x.param(param_type(__k, __p)); |
| 5486 | return __is; |
| 5487 | } |
| 5488 | |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5489 | // geometric_distribution |
| 5490 | |
| 5491 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5492 | class _LIBCPP_TEMPLATE_VIS geometric_distribution |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5493 | { |
| 5494 | public: |
| 5495 | // types |
| 5496 | typedef _IntType result_type; |
| 5497 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5498 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5499 | { |
| 5500 | double __p_; |
| 5501 | public: |
| 5502 | typedef geometric_distribution distribution_type; |
| 5503 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5504 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5505 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 5506 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5507 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5508 | double p() const {return __p_;} |
| 5509 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5510 | friend _LIBCPP_INLINE_VISIBILITY |
| 5511 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5512 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5513 | friend _LIBCPP_INLINE_VISIBILITY |
| 5514 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5515 | {return !(__x == __y);} |
| 5516 | }; |
| 5517 | |
| 5518 | private: |
| 5519 | param_type __p_; |
| 5520 | |
| 5521 | public: |
| 5522 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5523 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5524 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5525 | geometric_distribution() : geometric_distribution(0.5) {} |
| 5526 | _LIBCPP_INLINE_VISIBILITY |
| 5527 | explicit geometric_distribution(double __p) |
| 5528 | : __p_(__p) {} |
| 5529 | #else |
| 5530 | _LIBCPP_INLINE_VISIBILITY |
| 5531 | explicit geometric_distribution(double __p = 0.5) |
| 5532 | : __p_(__p) {} |
| 5533 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5534 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5535 | explicit geometric_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5536 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5537 | void reset() {} |
| 5538 | |
| 5539 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5540 | template<class _URNG> |
| 5541 | _LIBCPP_INLINE_VISIBILITY |
| 5542 | result_type operator()(_URNG& __g) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5543 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5544 | template<class _URNG> |
| 5545 | _LIBCPP_INLINE_VISIBILITY |
| 5546 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5547 | {return negative_binomial_distribution<result_type>(1, __p.p())(__g);} |
| 5548 | |
| 5549 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5550 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5551 | double p() const {return __p_.p();} |
| 5552 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5553 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5554 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5555 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5556 | void param(const param_type& __p) {__p_ = __p;} |
| 5557 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5558 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5559 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5560 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5561 | result_type max() const {return numeric_limits<result_type>::max();} |
| 5562 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5563 | friend _LIBCPP_INLINE_VISIBILITY |
| 5564 | bool operator==(const geometric_distribution& __x, |
| 5565 | const geometric_distribution& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5566 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5567 | friend _LIBCPP_INLINE_VISIBILITY |
| 5568 | bool operator!=(const geometric_distribution& __x, |
| 5569 | const geometric_distribution& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5570 | {return !(__x == __y);} |
| 5571 | }; |
| 5572 | |
| 5573 | template <class _CharT, class _Traits, class _IntType> |
| 5574 | basic_ostream<_CharT, _Traits>& |
| 5575 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5576 | const geometric_distribution<_IntType>& __x) |
| 5577 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5578 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5579 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5580 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5581 | _OStream::scientific); |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5582 | return __os << __x.p(); |
| 5583 | } |
| 5584 | |
| 5585 | template <class _CharT, class _Traits, class _IntType> |
| 5586 | basic_istream<_CharT, _Traits>& |
| 5587 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5588 | geometric_distribution<_IntType>& __x) |
| 5589 | { |
| 5590 | typedef geometric_distribution<_IntType> _Eng; |
| 5591 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5592 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5593 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5594 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5595 | double __p; |
| 5596 | __is >> __p; |
| 5597 | if (!__is.fail()) |
| 5598 | __x.param(param_type(__p)); |
| 5599 | return __is; |
| 5600 | } |
| 5601 | |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5602 | // chi_squared_distribution |
| 5603 | |
| 5604 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5605 | class _LIBCPP_TEMPLATE_VIS chi_squared_distribution |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5606 | { |
| 5607 | public: |
| 5608 | // types |
| 5609 | typedef _RealType result_type; |
| 5610 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5611 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5612 | { |
| 5613 | result_type __n_; |
| 5614 | public: |
| 5615 | typedef chi_squared_distribution distribution_type; |
| 5616 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5617 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5618 | explicit param_type(result_type __n = 1) : __n_(__n) {} |
| 5619 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5620 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5621 | result_type n() const {return __n_;} |
| 5622 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5623 | friend _LIBCPP_INLINE_VISIBILITY |
| 5624 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5625 | {return __x.__n_ == __y.__n_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5626 | friend _LIBCPP_INLINE_VISIBILITY |
| 5627 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5628 | {return !(__x == __y);} |
| 5629 | }; |
| 5630 | |
| 5631 | private: |
| 5632 | param_type __p_; |
| 5633 | |
| 5634 | public: |
| 5635 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5636 | #ifndef _LIBCPP_CXX03_LANG |
| 5637 | _LIBCPP_INLINE_VISIBILITY |
| 5638 | chi_squared_distribution() : chi_squared_distribution(1) {} |
| 5639 | _LIBCPP_INLINE_VISIBILITY |
| 5640 | explicit chi_squared_distribution(result_type __n) |
| 5641 | : __p_(param_type(__n)) {} |
| 5642 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5643 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5644 | explicit chi_squared_distribution(result_type __n = 1) |
| 5645 | : __p_(param_type(__n)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5646 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5647 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5648 | explicit chi_squared_distribution(const param_type& __p) |
| 5649 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5650 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5651 | void reset() {} |
| 5652 | |
| 5653 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5654 | template<class _URNG> |
| 5655 | _LIBCPP_INLINE_VISIBILITY |
| 5656 | result_type operator()(_URNG& __g) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5657 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5658 | template<class _URNG> |
| 5659 | _LIBCPP_INLINE_VISIBILITY |
| 5660 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5661 | {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);} |
| 5662 | |
| 5663 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5664 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5665 | result_type n() const {return __p_.n();} |
| 5666 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5667 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5668 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5669 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5670 | void param(const param_type& __p) {__p_ = __p;} |
| 5671 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5672 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5673 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5674 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5675 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5676 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5677 | friend _LIBCPP_INLINE_VISIBILITY |
| 5678 | bool operator==(const chi_squared_distribution& __x, |
| 5679 | const chi_squared_distribution& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5680 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5681 | friend _LIBCPP_INLINE_VISIBILITY |
| 5682 | bool operator!=(const chi_squared_distribution& __x, |
| 5683 | const chi_squared_distribution& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5684 | {return !(__x == __y);} |
| 5685 | }; |
| 5686 | |
| 5687 | template <class _CharT, class _Traits, class _RT> |
| 5688 | basic_ostream<_CharT, _Traits>& |
| 5689 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5690 | const chi_squared_distribution<_RT>& __x) |
| 5691 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5692 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5693 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5694 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5695 | _OStream::scientific); |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5696 | __os << __x.n(); |
| 5697 | return __os; |
| 5698 | } |
| 5699 | |
| 5700 | template <class _CharT, class _Traits, class _RT> |
| 5701 | basic_istream<_CharT, _Traits>& |
| 5702 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5703 | chi_squared_distribution<_RT>& __x) |
| 5704 | { |
| 5705 | typedef chi_squared_distribution<_RT> _Eng; |
| 5706 | typedef typename _Eng::result_type result_type; |
| 5707 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5708 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5709 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5710 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5711 | result_type __n; |
| 5712 | __is >> __n; |
| 5713 | if (!__is.fail()) |
| 5714 | __x.param(param_type(__n)); |
| 5715 | return __is; |
| 5716 | } |
| 5717 | |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5718 | // cauchy_distribution |
| 5719 | |
| 5720 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5721 | class _LIBCPP_TEMPLATE_VIS cauchy_distribution |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5722 | { |
| 5723 | public: |
| 5724 | // types |
| 5725 | typedef _RealType result_type; |
| 5726 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5727 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5728 | { |
| 5729 | result_type __a_; |
| 5730 | result_type __b_; |
| 5731 | public: |
| 5732 | typedef cauchy_distribution distribution_type; |
| 5733 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5734 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5735 | explicit param_type(result_type __a = 0, result_type __b = 1) |
| 5736 | : __a_(__a), __b_(__b) {} |
| 5737 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5738 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5739 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5740 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5741 | result_type b() const {return __b_;} |
| 5742 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5743 | friend _LIBCPP_INLINE_VISIBILITY |
| 5744 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5745 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5746 | friend _LIBCPP_INLINE_VISIBILITY |
| 5747 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5748 | {return !(__x == __y);} |
| 5749 | }; |
| 5750 | |
| 5751 | private: |
| 5752 | param_type __p_; |
| 5753 | |
| 5754 | public: |
| 5755 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5756 | #ifndef _LIBCPP_CXX03_LANG |
| 5757 | _LIBCPP_INLINE_VISIBILITY |
| 5758 | cauchy_distribution() : cauchy_distribution(0) {} |
| 5759 | _LIBCPP_INLINE_VISIBILITY |
| 5760 | explicit cauchy_distribution(result_type __a, result_type __b = 1) |
| 5761 | : __p_(param_type(__a, __b)) {} |
| 5762 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5763 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5764 | explicit cauchy_distribution(result_type __a = 0, result_type __b = 1) |
| 5765 | : __p_(param_type(__a, __b)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5766 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5767 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5768 | explicit cauchy_distribution(const param_type& __p) |
| 5769 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5770 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5771 | void reset() {} |
| 5772 | |
| 5773 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5774 | template<class _URNG> |
| 5775 | _LIBCPP_INLINE_VISIBILITY |
| 5776 | result_type operator()(_URNG& __g) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5777 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 5778 | template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p); |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5779 | |
| 5780 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5781 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5782 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5783 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5784 | result_type b() const {return __p_.b();} |
| 5785 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5786 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5787 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5788 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5789 | void param(const param_type& __p) {__p_ = __p;} |
| 5790 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5791 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5792 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5793 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5794 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5795 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5796 | friend _LIBCPP_INLINE_VISIBILITY |
| 5797 | bool operator==(const cauchy_distribution& __x, |
| 5798 | const cauchy_distribution& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5799 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5800 | friend _LIBCPP_INLINE_VISIBILITY |
| 5801 | bool operator!=(const cauchy_distribution& __x, |
| 5802 | const cauchy_distribution& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5803 | {return !(__x == __y);} |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5804 | }; |
| 5805 | |
| 5806 | template <class _RealType> |
| 5807 | template<class _URNG> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 5808 | inline |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5809 | _RealType |
| 5810 | cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5811 | { |
| 5812 | uniform_real_distribution<result_type> __gen; |
| 5813 | // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5814 | return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g)); |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5815 | } |
| 5816 | |
| 5817 | template <class _CharT, class _Traits, class _RT> |
| 5818 | basic_ostream<_CharT, _Traits>& |
| 5819 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5820 | const cauchy_distribution<_RT>& __x) |
| 5821 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5822 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5823 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5824 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5825 | _OStream::scientific); |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5826 | _CharT __sp = __os.widen(' '); |
| 5827 | __os.fill(__sp); |
| 5828 | __os << __x.a() << __sp << __x.b(); |
| 5829 | return __os; |
| 5830 | } |
| 5831 | |
| 5832 | template <class _CharT, class _Traits, class _RT> |
| 5833 | basic_istream<_CharT, _Traits>& |
| 5834 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5835 | cauchy_distribution<_RT>& __x) |
| 5836 | { |
| 5837 | typedef cauchy_distribution<_RT> _Eng; |
| 5838 | typedef typename _Eng::result_type result_type; |
| 5839 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5840 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5841 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5842 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5843 | result_type __a; |
| 5844 | result_type __b; |
| 5845 | __is >> __a >> __b; |
| 5846 | if (!__is.fail()) |
| 5847 | __x.param(param_type(__a, __b)); |
| 5848 | return __is; |
| 5849 | } |
| 5850 | |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5851 | // fisher_f_distribution |
| 5852 | |
| 5853 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5854 | class _LIBCPP_TEMPLATE_VIS fisher_f_distribution |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5855 | { |
| 5856 | public: |
| 5857 | // types |
| 5858 | typedef _RealType result_type; |
| 5859 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5860 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5861 | { |
| 5862 | result_type __m_; |
| 5863 | result_type __n_; |
| 5864 | public: |
| 5865 | typedef fisher_f_distribution distribution_type; |
| 5866 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5867 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5868 | explicit param_type(result_type __m = 1, result_type __n = 1) |
| 5869 | : __m_(__m), __n_(__n) {} |
| 5870 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5871 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5872 | result_type m() const {return __m_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5873 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5874 | result_type n() const {return __n_;} |
| 5875 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5876 | friend _LIBCPP_INLINE_VISIBILITY |
| 5877 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5878 | {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5879 | friend _LIBCPP_INLINE_VISIBILITY |
| 5880 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5881 | {return !(__x == __y);} |
| 5882 | }; |
| 5883 | |
| 5884 | private: |
| 5885 | param_type __p_; |
| 5886 | |
| 5887 | public: |
| 5888 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5889 | #ifndef _LIBCPP_CXX03_LANG |
| 5890 | _LIBCPP_INLINE_VISIBILITY |
| 5891 | fisher_f_distribution() : fisher_f_distribution(1) {} |
| 5892 | _LIBCPP_INLINE_VISIBILITY |
| 5893 | explicit fisher_f_distribution(result_type __m, result_type __n = 1) |
| 5894 | : __p_(param_type(__m, __n)) {} |
| 5895 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5896 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5897 | explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1) |
| 5898 | : __p_(param_type(__m, __n)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 5899 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5900 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5901 | explicit fisher_f_distribution(const param_type& __p) |
| 5902 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5903 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5904 | void reset() {} |
| 5905 | |
| 5906 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5907 | template<class _URNG> |
| 5908 | _LIBCPP_INLINE_VISIBILITY |
| 5909 | result_type operator()(_URNG& __g) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5910 | {return (*this)(__g, __p_);} |
| 5911 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5912 | |
| 5913 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5914 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5915 | result_type m() const {return __p_.m();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5916 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5917 | result_type n() const {return __p_.n();} |
| 5918 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5919 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5920 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5921 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5922 | void param(const param_type& __p) {__p_ = __p;} |
| 5923 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5924 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5925 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5926 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5927 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5928 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5929 | friend _LIBCPP_INLINE_VISIBILITY |
| 5930 | bool operator==(const fisher_f_distribution& __x, |
| 5931 | const fisher_f_distribution& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5932 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5933 | friend _LIBCPP_INLINE_VISIBILITY |
| 5934 | bool operator!=(const fisher_f_distribution& __x, |
| 5935 | const fisher_f_distribution& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5936 | {return !(__x == __y);} |
| 5937 | }; |
| 5938 | |
| 5939 | template <class _RealType> |
| 5940 | template<class _URNG> |
| 5941 | _RealType |
| 5942 | fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5943 | { |
| 5944 | gamma_distribution<result_type> __gdm(__p.m() * result_type(.5)); |
| 5945 | gamma_distribution<result_type> __gdn(__p.n() * result_type(.5)); |
| 5946 | return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g)); |
| 5947 | } |
| 5948 | |
| 5949 | template <class _CharT, class _Traits, class _RT> |
| 5950 | basic_ostream<_CharT, _Traits>& |
| 5951 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5952 | const fisher_f_distribution<_RT>& __x) |
| 5953 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5954 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5955 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5956 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5957 | _OStream::scientific); |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5958 | _CharT __sp = __os.widen(' '); |
| 5959 | __os.fill(__sp); |
| 5960 | __os << __x.m() << __sp << __x.n(); |
| 5961 | return __os; |
| 5962 | } |
| 5963 | |
| 5964 | template <class _CharT, class _Traits, class _RT> |
| 5965 | basic_istream<_CharT, _Traits>& |
| 5966 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5967 | fisher_f_distribution<_RT>& __x) |
| 5968 | { |
| 5969 | typedef fisher_f_distribution<_RT> _Eng; |
| 5970 | typedef typename _Eng::result_type result_type; |
| 5971 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5972 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5973 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5974 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5975 | result_type __m; |
| 5976 | result_type __n; |
| 5977 | __is >> __m >> __n; |
| 5978 | if (!__is.fail()) |
| 5979 | __x.param(param_type(__m, __n)); |
| 5980 | return __is; |
| 5981 | } |
| 5982 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5983 | // student_t_distribution |
| 5984 | |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5985 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5986 | class _LIBCPP_TEMPLATE_VIS student_t_distribution |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5987 | { |
| 5988 | public: |
| 5989 | // types |
| 5990 | typedef _RealType result_type; |
| 5991 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5992 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5993 | { |
| 5994 | result_type __n_; |
| 5995 | public: |
| 5996 | typedef student_t_distribution distribution_type; |
| 5997 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5998 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5999 | explicit param_type(result_type __n = 1) : __n_(__n) {} |
| 6000 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6001 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6002 | result_type n() const {return __n_;} |
| 6003 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6004 | friend _LIBCPP_INLINE_VISIBILITY |
| 6005 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6006 | {return __x.__n_ == __y.__n_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6007 | friend _LIBCPP_INLINE_VISIBILITY |
| 6008 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6009 | {return !(__x == __y);} |
| 6010 | }; |
| 6011 | |
| 6012 | private: |
| 6013 | param_type __p_; |
| 6014 | normal_distribution<result_type> __nd_; |
| 6015 | |
| 6016 | public: |
| 6017 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 6018 | #ifndef _LIBCPP_CXX03_LANG |
| 6019 | _LIBCPP_INLINE_VISIBILITY |
| 6020 | student_t_distribution() : student_t_distribution(1) {} |
| 6021 | _LIBCPP_INLINE_VISIBILITY |
| 6022 | explicit student_t_distribution(result_type __n) |
| 6023 | : __p_(param_type(__n)) {} |
| 6024 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6025 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6026 | explicit student_t_distribution(result_type __n = 1) |
| 6027 | : __p_(param_type(__n)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame] | 6028 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6029 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6030 | explicit student_t_distribution(const param_type& __p) |
| 6031 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6032 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6033 | void reset() {__nd_.reset();} |
| 6034 | |
| 6035 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6036 | template<class _URNG> |
| 6037 | _LIBCPP_INLINE_VISIBILITY |
| 6038 | result_type operator()(_URNG& __g) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6039 | {return (*this)(__g, __p_);} |
| 6040 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6041 | |
| 6042 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6043 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6044 | result_type n() const {return __p_.n();} |
| 6045 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6046 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6047 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6048 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6049 | void param(const param_type& __p) {__p_ = __p;} |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6050 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6051 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6052 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6053 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6054 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 6055 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6056 | friend _LIBCPP_INLINE_VISIBILITY |
| 6057 | bool operator==(const student_t_distribution& __x, |
| 6058 | const student_t_distribution& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6059 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6060 | friend _LIBCPP_INLINE_VISIBILITY |
| 6061 | bool operator!=(const student_t_distribution& __x, |
| 6062 | const student_t_distribution& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6063 | {return !(__x == __y);} |
| 6064 | }; |
| 6065 | |
| 6066 | template <class _RealType> |
| 6067 | template<class _URNG> |
| 6068 | _RealType |
| 6069 | student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6070 | { |
| 6071 | gamma_distribution<result_type> __gd(__p.n() * .5, 2); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6072 | return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g)); |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | template <class _CharT, class _Traits, class _RT> |
| 6076 | basic_ostream<_CharT, _Traits>& |
| 6077 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6078 | const student_t_distribution<_RT>& __x) |
| 6079 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6080 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6081 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 6082 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 6083 | _OStream::scientific); |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6084 | __os << __x.n(); |
| 6085 | return __os; |
| 6086 | } |
| 6087 | |
| 6088 | template <class _CharT, class _Traits, class _RT> |
| 6089 | basic_istream<_CharT, _Traits>& |
| 6090 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6091 | student_t_distribution<_RT>& __x) |
| 6092 | { |
| 6093 | typedef student_t_distribution<_RT> _Eng; |
| 6094 | typedef typename _Eng::result_type result_type; |
| 6095 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6096 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6097 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 6098 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6099 | result_type __n; |
| 6100 | __is >> __n; |
| 6101 | if (!__is.fail()) |
| 6102 | __x.param(param_type(__n)); |
| 6103 | return __is; |
| 6104 | } |
| 6105 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6106 | // discrete_distribution |
| 6107 | |
| 6108 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6109 | class _LIBCPP_TEMPLATE_VIS discrete_distribution |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6110 | { |
| 6111 | public: |
| 6112 | // types |
| 6113 | typedef _IntType result_type; |
| 6114 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6115 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6116 | { |
| 6117 | vector<double> __p_; |
| 6118 | public: |
| 6119 | typedef discrete_distribution distribution_type; |
| 6120 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6121 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6122 | param_type() {} |
| 6123 | template<class _InputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6124 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6125 | param_type(_InputIterator __f, _InputIterator __l) |
| 6126 | : __p_(__f, __l) {__init();} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6127 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6128 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6129 | param_type(initializer_list<double> __wl) |
| 6130 | : __p_(__wl.begin(), __wl.end()) {__init();} |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 6131 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6132 | template<class _UnaryOperation> |
| 6133 | param_type(size_t __nw, double __xmin, double __xmax, |
| 6134 | _UnaryOperation __fw); |
| 6135 | |
| 6136 | vector<double> probabilities() const; |
| 6137 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6138 | friend _LIBCPP_INLINE_VISIBILITY |
| 6139 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6140 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6141 | friend _LIBCPP_INLINE_VISIBILITY |
| 6142 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6143 | {return !(__x == __y);} |
| 6144 | |
| 6145 | private: |
| 6146 | void __init(); |
| 6147 | |
| 6148 | friend class discrete_distribution; |
| 6149 | |
| 6150 | template <class _CharT, class _Traits, class _IT> |
| 6151 | friend |
| 6152 | basic_ostream<_CharT, _Traits>& |
| 6153 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6154 | const discrete_distribution<_IT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6155 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6156 | template <class _CharT, class _Traits, class _IT> |
| 6157 | friend |
| 6158 | basic_istream<_CharT, _Traits>& |
| 6159 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6160 | discrete_distribution<_IT>& __x); |
| 6161 | }; |
| 6162 | |
| 6163 | private: |
| 6164 | param_type __p_; |
| 6165 | |
| 6166 | public: |
| 6167 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6168 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6169 | discrete_distribution() {} |
| 6170 | template<class _InputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6171 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6172 | discrete_distribution(_InputIterator __f, _InputIterator __l) |
| 6173 | : __p_(__f, __l) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6174 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6175 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6176 | discrete_distribution(initializer_list<double> __wl) |
| 6177 | : __p_(__wl) {} |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 6178 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6179 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6180 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6181 | discrete_distribution(size_t __nw, double __xmin, double __xmax, |
| 6182 | _UnaryOperation __fw) |
| 6183 | : __p_(__nw, __xmin, __xmax, __fw) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6184 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ea38295 | 2013-08-14 18:00:20 +0000 | [diff] [blame] | 6185 | explicit discrete_distribution(const param_type& __p) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6186 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6187 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6188 | void reset() {} |
| 6189 | |
| 6190 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6191 | template<class _URNG> |
| 6192 | _LIBCPP_INLINE_VISIBILITY |
| 6193 | result_type operator()(_URNG& __g) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6194 | {return (*this)(__g, __p_);} |
| 6195 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6196 | |
| 6197 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6198 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6199 | vector<double> probabilities() const {return __p_.probabilities();} |
| 6200 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6201 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6202 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6203 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6204 | void param(const param_type& __p) {__p_ = __p;} |
| 6205 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6206 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6207 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6208 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6209 | result_type max() const {return __p_.__p_.size();} |
| 6210 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6211 | friend _LIBCPP_INLINE_VISIBILITY |
| 6212 | bool operator==(const discrete_distribution& __x, |
| 6213 | const discrete_distribution& __y) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6214 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6215 | friend _LIBCPP_INLINE_VISIBILITY |
| 6216 | bool operator!=(const discrete_distribution& __x, |
| 6217 | const discrete_distribution& __y) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6218 | {return !(__x == __y);} |
| 6219 | |
| 6220 | template <class _CharT, class _Traits, class _IT> |
| 6221 | friend |
| 6222 | basic_ostream<_CharT, _Traits>& |
| 6223 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6224 | const discrete_distribution<_IT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6225 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6226 | template <class _CharT, class _Traits, class _IT> |
| 6227 | friend |
| 6228 | basic_istream<_CharT, _Traits>& |
| 6229 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6230 | discrete_distribution<_IT>& __x); |
| 6231 | }; |
| 6232 | |
| 6233 | template<class _IntType> |
| 6234 | template<class _UnaryOperation> |
| 6235 | discrete_distribution<_IntType>::param_type::param_type(size_t __nw, |
| 6236 | double __xmin, |
| 6237 | double __xmax, |
| 6238 | _UnaryOperation __fw) |
| 6239 | { |
| 6240 | if (__nw > 1) |
| 6241 | { |
| 6242 | __p_.reserve(__nw - 1); |
| 6243 | double __d = (__xmax - __xmin) / __nw; |
| 6244 | double __d2 = __d / 2; |
| 6245 | for (size_t __k = 0; __k < __nw; ++__k) |
| 6246 | __p_.push_back(__fw(__xmin + __k * __d + __d2)); |
| 6247 | __init(); |
| 6248 | } |
| 6249 | } |
| 6250 | |
| 6251 | template<class _IntType> |
| 6252 | void |
| 6253 | discrete_distribution<_IntType>::param_type::__init() |
| 6254 | { |
| 6255 | if (!__p_.empty()) |
| 6256 | { |
| 6257 | if (__p_.size() > 1) |
| 6258 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6259 | double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0); |
Arthur O'Dwyer | d8dddf5 | 2021-05-10 13:13:04 -0400 | [diff] [blame] | 6260 | for (vector<double>::iterator __i = __p_.begin(), __e = __p_.end(); __i < __e; ++__i) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6261 | *__i /= __s; |
| 6262 | vector<double> __t(__p_.size() - 1); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6263 | _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin()); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6264 | swap(__p_, __t); |
| 6265 | } |
| 6266 | else |
| 6267 | { |
| 6268 | __p_.clear(); |
| 6269 | __p_.shrink_to_fit(); |
| 6270 | } |
| 6271 | } |
| 6272 | } |
| 6273 | |
| 6274 | template<class _IntType> |
| 6275 | vector<double> |
| 6276 | discrete_distribution<_IntType>::param_type::probabilities() const |
| 6277 | { |
| 6278 | size_t __n = __p_.size(); |
Arthur O'Dwyer | d8dddf5 | 2021-05-10 13:13:04 -0400 | [diff] [blame] | 6279 | vector<double> __p(__n+1); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6280 | _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin()); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6281 | if (__n > 0) |
| 6282 | __p[__n] = 1 - __p_[__n-1]; |
| 6283 | else |
| 6284 | __p[0] = 1; |
| 6285 | return __p; |
| 6286 | } |
| 6287 | |
| 6288 | template<class _IntType> |
| 6289 | template<class _URNG> |
| 6290 | _IntType |
| 6291 | discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) |
| 6292 | { |
| 6293 | uniform_real_distribution<double> __gen; |
| 6294 | return static_cast<_IntType>( |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6295 | _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) - |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6296 | __p.__p_.begin()); |
| 6297 | } |
| 6298 | |
| 6299 | template <class _CharT, class _Traits, class _IT> |
| 6300 | basic_ostream<_CharT, _Traits>& |
| 6301 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6302 | const discrete_distribution<_IT>& __x) |
| 6303 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6304 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6305 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 6306 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 6307 | _OStream::scientific); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6308 | _CharT __sp = __os.widen(' '); |
| 6309 | __os.fill(__sp); |
| 6310 | size_t __n = __x.__p_.__p_.size(); |
| 6311 | __os << __n; |
| 6312 | for (size_t __i = 0; __i < __n; ++__i) |
| 6313 | __os << __sp << __x.__p_.__p_[__i]; |
| 6314 | return __os; |
| 6315 | } |
| 6316 | |
| 6317 | template <class _CharT, class _Traits, class _IT> |
| 6318 | basic_istream<_CharT, _Traits>& |
| 6319 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6320 | discrete_distribution<_IT>& __x) |
| 6321 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6322 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6323 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 6324 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6325 | size_t __n; |
| 6326 | __is >> __n; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6327 | vector<double> __p(__n); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6328 | for (size_t __i = 0; __i < __n; ++__i) |
| 6329 | __is >> __p[__i]; |
| 6330 | if (!__is.fail()) |
| 6331 | swap(__x.__p_.__p_, __p); |
| 6332 | return __is; |
| 6333 | } |
| 6334 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6335 | // piecewise_constant_distribution |
| 6336 | |
| 6337 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6338 | class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6339 | { |
| 6340 | public: |
| 6341 | // types |
| 6342 | typedef _RealType result_type; |
| 6343 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6344 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6345 | { |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6346 | vector<result_type> __b_; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6347 | vector<result_type> __densities_; |
| 6348 | vector<result_type> __areas_; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6349 | public: |
| 6350 | typedef piecewise_constant_distribution distribution_type; |
| 6351 | |
| 6352 | param_type(); |
| 6353 | template<class _InputIteratorB, class _InputIteratorW> |
| 6354 | param_type(_InputIteratorB __fB, _InputIteratorB __lB, |
| 6355 | _InputIteratorW __fW); |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6356 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6357 | template<class _UnaryOperation> |
| 6358 | param_type(initializer_list<result_type> __bl, _UnaryOperation __fw); |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 6359 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6360 | template<class _UnaryOperation> |
| 6361 | param_type(size_t __nw, result_type __xmin, result_type __xmax, |
| 6362 | _UnaryOperation __fw); |
Eric Fiselier | b9f37ca | 2019-12-12 20:48:11 -0500 | [diff] [blame] | 6363 | param_type(param_type const&) = default; |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6364 | param_type & operator=(const param_type& __rhs); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6365 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6366 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6367 | vector<result_type> intervals() const {return __b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6368 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6369 | vector<result_type> densities() const {return __densities_;} |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6370 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6371 | friend _LIBCPP_INLINE_VISIBILITY |
| 6372 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6373 | {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6374 | friend _LIBCPP_INLINE_VISIBILITY |
| 6375 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6376 | {return !(__x == __y);} |
| 6377 | |
| 6378 | private: |
| 6379 | void __init(); |
| 6380 | |
| 6381 | friend class piecewise_constant_distribution; |
| 6382 | |
| 6383 | template <class _CharT, class _Traits, class _RT> |
| 6384 | friend |
| 6385 | basic_ostream<_CharT, _Traits>& |
| 6386 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6387 | const piecewise_constant_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6388 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6389 | template <class _CharT, class _Traits, class _RT> |
| 6390 | friend |
| 6391 | basic_istream<_CharT, _Traits>& |
| 6392 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6393 | piecewise_constant_distribution<_RT>& __x); |
| 6394 | }; |
| 6395 | |
| 6396 | private: |
| 6397 | param_type __p_; |
| 6398 | |
| 6399 | public: |
| 6400 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6401 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6402 | piecewise_constant_distribution() {} |
| 6403 | template<class _InputIteratorB, class _InputIteratorW> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6404 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6405 | piecewise_constant_distribution(_InputIteratorB __fB, |
| 6406 | _InputIteratorB __lB, |
| 6407 | _InputIteratorW __fW) |
| 6408 | : __p_(__fB, __lB, __fW) {} |
| 6409 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6410 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6411 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6412 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6413 | piecewise_constant_distribution(initializer_list<result_type> __bl, |
| 6414 | _UnaryOperation __fw) |
| 6415 | : __p_(__bl, __fw) {} |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 6416 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6417 | |
| 6418 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6419 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6420 | piecewise_constant_distribution(size_t __nw, result_type __xmin, |
| 6421 | result_type __xmax, _UnaryOperation __fw) |
| 6422 | : __p_(__nw, __xmin, __xmax, __fw) {} |
| 6423 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6424 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6425 | explicit piecewise_constant_distribution(const param_type& __p) |
| 6426 | : __p_(__p) {} |
| 6427 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6428 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6429 | void reset() {} |
| 6430 | |
| 6431 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6432 | template<class _URNG> |
| 6433 | _LIBCPP_INLINE_VISIBILITY |
| 6434 | result_type operator()(_URNG& __g) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6435 | {return (*this)(__g, __p_);} |
| 6436 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6437 | |
| 6438 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6439 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6440 | vector<result_type> intervals() const {return __p_.intervals();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6441 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6442 | vector<result_type> densities() const {return __p_.densities();} |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6443 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6444 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6445 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6446 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6447 | void param(const param_type& __p) {__p_ = __p;} |
| 6448 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6449 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6450 | result_type min() const {return __p_.__b_.front();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6451 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6452 | result_type max() const {return __p_.__b_.back();} |
| 6453 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6454 | friend _LIBCPP_INLINE_VISIBILITY |
| 6455 | bool operator==(const piecewise_constant_distribution& __x, |
| 6456 | const piecewise_constant_distribution& __y) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6457 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6458 | friend _LIBCPP_INLINE_VISIBILITY |
| 6459 | bool operator!=(const piecewise_constant_distribution& __x, |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6460 | const piecewise_constant_distribution& __y) |
| 6461 | {return !(__x == __y);} |
| 6462 | |
| 6463 | template <class _CharT, class _Traits, class _RT> |
| 6464 | friend |
| 6465 | basic_ostream<_CharT, _Traits>& |
| 6466 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6467 | const piecewise_constant_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6468 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6469 | template <class _CharT, class _Traits, class _RT> |
| 6470 | friend |
| 6471 | basic_istream<_CharT, _Traits>& |
| 6472 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6473 | piecewise_constant_distribution<_RT>& __x); |
| 6474 | }; |
| 6475 | |
| 6476 | template<class _RealType> |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6477 | typename piecewise_constant_distribution<_RealType>::param_type & |
| 6478 | piecewise_constant_distribution<_RealType>::param_type::operator= |
| 6479 | (const param_type& __rhs) |
| 6480 | { |
| 6481 | // These can throw |
| 6482 | __b_.reserve (__rhs.__b_.size ()); |
| 6483 | __densities_.reserve(__rhs.__densities_.size()); |
| 6484 | __areas_.reserve (__rhs.__areas_.size()); |
| 6485 | |
| 6486 | // These can not throw |
| 6487 | __b_ = __rhs.__b_; |
| 6488 | __densities_ = __rhs.__densities_; |
| 6489 | __areas_ = __rhs.__areas_; |
| 6490 | return *this; |
| 6491 | } |
| 6492 | |
| 6493 | template<class _RealType> |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6494 | void |
| 6495 | piecewise_constant_distribution<_RealType>::param_type::__init() |
| 6496 | { |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6497 | // __densities_ contains non-normalized areas |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6498 | result_type __total_area = _VSTD::accumulate(__densities_.begin(), |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6499 | __densities_.end(), |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6500 | result_type()); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6501 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
| 6502 | __densities_[__i] /= __total_area; |
| 6503 | // __densities_ contains normalized areas |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6504 | __areas_.assign(__densities_.size(), result_type()); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6505 | _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1, |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6506 | __areas_.begin() + 1); |
| 6507 | // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1] |
| 6508 | __densities_.back() = 1 - __areas_.back(); // correct round off error |
| 6509 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
| 6510 | __densities_[__i] /= (__b_[__i+1] - __b_[__i]); |
| 6511 | // __densities_ now contains __densities_ |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6512 | } |
| 6513 | |
| 6514 | template<class _RealType> |
| 6515 | piecewise_constant_distribution<_RealType>::param_type::param_type() |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6516 | : __b_(2), |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6517 | __densities_(1, 1.0), |
| 6518 | __areas_(1, 0.0) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6519 | { |
| 6520 | __b_[1] = 1; |
| 6521 | } |
| 6522 | |
| 6523 | template<class _RealType> |
| 6524 | template<class _InputIteratorB, class _InputIteratorW> |
| 6525 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6526 | _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW) |
| 6527 | : __b_(__fB, __lB) |
| 6528 | { |
| 6529 | if (__b_.size() < 2) |
| 6530 | { |
| 6531 | __b_.resize(2); |
| 6532 | __b_[0] = 0; |
| 6533 | __b_[1] = 1; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6534 | __densities_.assign(1, 1.0); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6535 | __areas_.assign(1, 0.0); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6536 | } |
| 6537 | else |
| 6538 | { |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6539 | __densities_.reserve(__b_.size() - 1); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6540 | for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6541 | __densities_.push_back(*__fW); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6542 | __init(); |
| 6543 | } |
| 6544 | } |
| 6545 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6546 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6547 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6548 | template<class _RealType> |
| 6549 | template<class _UnaryOperation> |
| 6550 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6551 | initializer_list<result_type> __bl, _UnaryOperation __fw) |
| 6552 | : __b_(__bl.begin(), __bl.end()) |
| 6553 | { |
| 6554 | if (__b_.size() < 2) |
| 6555 | { |
| 6556 | __b_.resize(2); |
| 6557 | __b_[0] = 0; |
| 6558 | __b_[1] = 1; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6559 | __densities_.assign(1, 1.0); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6560 | __areas_.assign(1, 0.0); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6561 | } |
| 6562 | else |
| 6563 | { |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6564 | __densities_.reserve(__b_.size() - 1); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6565 | for (size_t __i = 0; __i < __b_.size() - 1; ++__i) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6566 | __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5)); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6567 | __init(); |
| 6568 | } |
| 6569 | } |
| 6570 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 6571 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6572 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6573 | template<class _RealType> |
| 6574 | template<class _UnaryOperation> |
| 6575 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6576 | size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw) |
| 6577 | : __b_(__nw == 0 ? 2 : __nw + 1) |
| 6578 | { |
| 6579 | size_t __n = __b_.size() - 1; |
| 6580 | result_type __d = (__xmax - __xmin) / __n; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6581 | __densities_.reserve(__n); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6582 | for (size_t __i = 0; __i < __n; ++__i) |
| 6583 | { |
| 6584 | __b_[__i] = __xmin + __i * __d; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6585 | __densities_.push_back(__fw(__b_[__i] + __d*.5)); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6586 | } |
| 6587 | __b_[__n] = __xmax; |
| 6588 | __init(); |
| 6589 | } |
| 6590 | |
| 6591 | template<class _RealType> |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6592 | template<class _URNG> |
| 6593 | _RealType |
| 6594 | piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6595 | { |
| 6596 | typedef uniform_real_distribution<result_type> _Gen; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6597 | result_type __u = _Gen()(__g); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6598 | ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6599 | __u) - __p.__areas_.begin() - 1; |
| 6600 | return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k]; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6601 | } |
| 6602 | |
| 6603 | template <class _CharT, class _Traits, class _RT> |
| 6604 | basic_ostream<_CharT, _Traits>& |
| 6605 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6606 | const piecewise_constant_distribution<_RT>& __x) |
| 6607 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6608 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6609 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 6610 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 6611 | _OStream::scientific); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6612 | _CharT __sp = __os.widen(' '); |
| 6613 | __os.fill(__sp); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6614 | size_t __n = __x.__p_.__b_.size(); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6615 | __os << __n; |
| 6616 | for (size_t __i = 0; __i < __n; ++__i) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6617 | __os << __sp << __x.__p_.__b_[__i]; |
| 6618 | __n = __x.__p_.__densities_.size(); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6619 | __os << __sp << __n; |
| 6620 | for (size_t __i = 0; __i < __n; ++__i) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6621 | __os << __sp << __x.__p_.__densities_[__i]; |
| 6622 | __n = __x.__p_.__areas_.size(); |
| 6623 | __os << __sp << __n; |
| 6624 | for (size_t __i = 0; __i < __n; ++__i) |
| 6625 | __os << __sp << __x.__p_.__areas_[__i]; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6626 | return __os; |
| 6627 | } |
| 6628 | |
| 6629 | template <class _CharT, class _Traits, class _RT> |
| 6630 | basic_istream<_CharT, _Traits>& |
| 6631 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6632 | piecewise_constant_distribution<_RT>& __x) |
| 6633 | { |
| 6634 | typedef piecewise_constant_distribution<_RT> _Eng; |
| 6635 | typedef typename _Eng::result_type result_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6636 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6637 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 6638 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6639 | size_t __n; |
| 6640 | __is >> __n; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6641 | vector<result_type> __b(__n); |
| 6642 | for (size_t __i = 0; __i < __n; ++__i) |
| 6643 | __is >> __b[__i]; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6644 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6645 | vector<result_type> __densities(__n); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6646 | for (size_t __i = 0; __i < __n; ++__i) |
| 6647 | __is >> __densities[__i]; |
| 6648 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6649 | vector<result_type> __areas(__n); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6650 | for (size_t __i = 0; __i < __n; ++__i) |
| 6651 | __is >> __areas[__i]; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6652 | if (!__is.fail()) |
| 6653 | { |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6654 | swap(__x.__p_.__b_, __b); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6655 | swap(__x.__p_.__densities_, __densities); |
| 6656 | swap(__x.__p_.__areas_, __areas); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6657 | } |
| 6658 | return __is; |
| 6659 | } |
| 6660 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6661 | // piecewise_linear_distribution |
| 6662 | |
| 6663 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6664 | class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6665 | { |
| 6666 | public: |
| 6667 | // types |
| 6668 | typedef _RealType result_type; |
| 6669 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6670 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6671 | { |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6672 | vector<result_type> __b_; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6673 | vector<result_type> __densities_; |
| 6674 | vector<result_type> __areas_; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6675 | public: |
| 6676 | typedef piecewise_linear_distribution distribution_type; |
| 6677 | |
| 6678 | param_type(); |
| 6679 | template<class _InputIteratorB, class _InputIteratorW> |
| 6680 | param_type(_InputIteratorB __fB, _InputIteratorB __lB, |
| 6681 | _InputIteratorW __fW); |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6682 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6683 | template<class _UnaryOperation> |
| 6684 | param_type(initializer_list<result_type> __bl, _UnaryOperation __fw); |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 6685 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6686 | template<class _UnaryOperation> |
| 6687 | param_type(size_t __nw, result_type __xmin, result_type __xmax, |
| 6688 | _UnaryOperation __fw); |
Eric Fiselier | b9f37ca | 2019-12-12 20:48:11 -0500 | [diff] [blame] | 6689 | param_type(param_type const&) = default; |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6690 | param_type & operator=(const param_type& __rhs); |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 6691 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6692 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6693 | vector<result_type> intervals() const {return __b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6694 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6695 | vector<result_type> densities() const {return __densities_;} |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6696 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6697 | friend _LIBCPP_INLINE_VISIBILITY |
| 6698 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6699 | {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6700 | friend _LIBCPP_INLINE_VISIBILITY |
| 6701 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6702 | {return !(__x == __y);} |
| 6703 | |
| 6704 | private: |
| 6705 | void __init(); |
| 6706 | |
| 6707 | friend class piecewise_linear_distribution; |
| 6708 | |
| 6709 | template <class _CharT, class _Traits, class _RT> |
| 6710 | friend |
| 6711 | basic_ostream<_CharT, _Traits>& |
| 6712 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6713 | const piecewise_linear_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6714 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6715 | template <class _CharT, class _Traits, class _RT> |
| 6716 | friend |
| 6717 | basic_istream<_CharT, _Traits>& |
| 6718 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6719 | piecewise_linear_distribution<_RT>& __x); |
| 6720 | }; |
| 6721 | |
| 6722 | private: |
| 6723 | param_type __p_; |
| 6724 | |
| 6725 | public: |
| 6726 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6727 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6728 | piecewise_linear_distribution() {} |
| 6729 | template<class _InputIteratorB, class _InputIteratorW> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6730 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6731 | piecewise_linear_distribution(_InputIteratorB __fB, |
| 6732 | _InputIteratorB __lB, |
| 6733 | _InputIteratorW __fW) |
| 6734 | : __p_(__fB, __lB, __fW) {} |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6735 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6736 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6737 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6738 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6739 | piecewise_linear_distribution(initializer_list<result_type> __bl, |
| 6740 | _UnaryOperation __fw) |
| 6741 | : __p_(__bl, __fw) {} |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 6742 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6743 | |
| 6744 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6745 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6746 | piecewise_linear_distribution(size_t __nw, result_type __xmin, |
| 6747 | result_type __xmax, _UnaryOperation __fw) |
| 6748 | : __p_(__nw, __xmin, __xmax, __fw) {} |
| 6749 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6750 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6751 | explicit piecewise_linear_distribution(const param_type& __p) |
| 6752 | : __p_(__p) {} |
| 6753 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6754 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6755 | void reset() {} |
| 6756 | |
| 6757 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6758 | template<class _URNG> |
| 6759 | _LIBCPP_INLINE_VISIBILITY |
| 6760 | result_type operator()(_URNG& __g) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6761 | {return (*this)(__g, __p_);} |
| 6762 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6763 | |
| 6764 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6765 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6766 | vector<result_type> intervals() const {return __p_.intervals();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6767 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6768 | vector<result_type> densities() const {return __p_.densities();} |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6769 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6770 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6771 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6772 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6773 | void param(const param_type& __p) {__p_ = __p;} |
| 6774 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6775 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6776 | result_type min() const {return __p_.__b_.front();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6777 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6778 | result_type max() const {return __p_.__b_.back();} |
| 6779 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6780 | friend _LIBCPP_INLINE_VISIBILITY |
| 6781 | bool operator==(const piecewise_linear_distribution& __x, |
| 6782 | const piecewise_linear_distribution& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6783 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6784 | friend _LIBCPP_INLINE_VISIBILITY |
| 6785 | bool operator!=(const piecewise_linear_distribution& __x, |
| 6786 | const piecewise_linear_distribution& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6787 | {return !(__x == __y);} |
| 6788 | |
| 6789 | template <class _CharT, class _Traits, class _RT> |
| 6790 | friend |
| 6791 | basic_ostream<_CharT, _Traits>& |
| 6792 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6793 | const piecewise_linear_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6794 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6795 | template <class _CharT, class _Traits, class _RT> |
| 6796 | friend |
| 6797 | basic_istream<_CharT, _Traits>& |
| 6798 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6799 | piecewise_linear_distribution<_RT>& __x); |
| 6800 | }; |
| 6801 | |
| 6802 | template<class _RealType> |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6803 | typename piecewise_linear_distribution<_RealType>::param_type & |
| 6804 | piecewise_linear_distribution<_RealType>::param_type::operator= |
| 6805 | (const param_type& __rhs) |
| 6806 | { |
| 6807 | // These can throw |
| 6808 | __b_.reserve (__rhs.__b_.size ()); |
| 6809 | __densities_.reserve(__rhs.__densities_.size()); |
| 6810 | __areas_.reserve (__rhs.__areas_.size()); |
| 6811 | |
| 6812 | // These can not throw |
| 6813 | __b_ = __rhs.__b_; |
| 6814 | __densities_ = __rhs.__densities_; |
| 6815 | __areas_ = __rhs.__areas_; |
| 6816 | return *this; |
| 6817 | } |
| 6818 | |
| 6819 | |
| 6820 | template<class _RealType> |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6821 | void |
| 6822 | piecewise_linear_distribution<_RealType>::param_type::__init() |
| 6823 | { |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6824 | __areas_.assign(__densities_.size() - 1, result_type()); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6825 | result_type _Sp = 0; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6826 | for (size_t __i = 0; __i < __areas_.size(); ++__i) |
| 6827 | { |
| 6828 | __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) * |
| 6829 | (__b_[__i+1] - __b_[__i]) * .5; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6830 | _Sp += __areas_[__i]; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6831 | } |
| 6832 | for (size_t __i = __areas_.size(); __i > 1;) |
| 6833 | { |
| 6834 | --__i; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6835 | __areas_[__i] = __areas_[__i-1] / _Sp; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6836 | } |
| 6837 | __areas_[0] = 0; |
| 6838 | for (size_t __i = 1; __i < __areas_.size(); ++__i) |
| 6839 | __areas_[__i] += __areas_[__i-1]; |
| 6840 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6841 | __densities_[__i] /= _Sp; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6842 | } |
| 6843 | |
| 6844 | template<class _RealType> |
| 6845 | piecewise_linear_distribution<_RealType>::param_type::param_type() |
| 6846 | : __b_(2), |
| 6847 | __densities_(2, 1.0), |
| 6848 | __areas_(1, 0.0) |
| 6849 | { |
| 6850 | __b_[1] = 1; |
| 6851 | } |
| 6852 | |
| 6853 | template<class _RealType> |
| 6854 | template<class _InputIteratorB, class _InputIteratorW> |
| 6855 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6856 | _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW) |
| 6857 | : __b_(__fB, __lB) |
| 6858 | { |
| 6859 | if (__b_.size() < 2) |
| 6860 | { |
| 6861 | __b_.resize(2); |
| 6862 | __b_[0] = 0; |
| 6863 | __b_[1] = 1; |
| 6864 | __densities_.assign(2, 1.0); |
| 6865 | __areas_.assign(1, 0.0); |
| 6866 | } |
| 6867 | else |
| 6868 | { |
| 6869 | __densities_.reserve(__b_.size()); |
| 6870 | for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW) |
| 6871 | __densities_.push_back(*__fW); |
| 6872 | __init(); |
| 6873 | } |
| 6874 | } |
| 6875 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6876 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6877 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6878 | template<class _RealType> |
| 6879 | template<class _UnaryOperation> |
| 6880 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6881 | initializer_list<result_type> __bl, _UnaryOperation __fw) |
| 6882 | : __b_(__bl.begin(), __bl.end()) |
| 6883 | { |
| 6884 | if (__b_.size() < 2) |
| 6885 | { |
| 6886 | __b_.resize(2); |
| 6887 | __b_[0] = 0; |
| 6888 | __b_[1] = 1; |
| 6889 | __densities_.assign(2, 1.0); |
| 6890 | __areas_.assign(1, 0.0); |
| 6891 | } |
| 6892 | else |
| 6893 | { |
| 6894 | __densities_.reserve(__b_.size()); |
| 6895 | for (size_t __i = 0; __i < __b_.size(); ++__i) |
| 6896 | __densities_.push_back(__fw(__b_[__i])); |
| 6897 | __init(); |
| 6898 | } |
| 6899 | } |
| 6900 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 6901 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6902 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6903 | template<class _RealType> |
| 6904 | template<class _UnaryOperation> |
| 6905 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6906 | size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw) |
| 6907 | : __b_(__nw == 0 ? 2 : __nw + 1) |
| 6908 | { |
| 6909 | size_t __n = __b_.size() - 1; |
| 6910 | result_type __d = (__xmax - __xmin) / __n; |
| 6911 | __densities_.reserve(__b_.size()); |
| 6912 | for (size_t __i = 0; __i < __n; ++__i) |
| 6913 | { |
| 6914 | __b_[__i] = __xmin + __i * __d; |
| 6915 | __densities_.push_back(__fw(__b_[__i])); |
| 6916 | } |
| 6917 | __b_[__n] = __xmax; |
| 6918 | __densities_.push_back(__fw(__b_[__n])); |
| 6919 | __init(); |
| 6920 | } |
| 6921 | |
| 6922 | template<class _RealType> |
| 6923 | template<class _URNG> |
| 6924 | _RealType |
| 6925 | piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6926 | { |
| 6927 | typedef uniform_real_distribution<result_type> _Gen; |
| 6928 | result_type __u = _Gen()(__g); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6929 | ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6930 | __u) - __p.__areas_.begin() - 1; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6931 | __u -= __p.__areas_[__k]; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6932 | const result_type __dk = __p.__densities_[__k]; |
| 6933 | const result_type __dk1 = __p.__densities_[__k+1]; |
| 6934 | const result_type __deltad = __dk1 - __dk; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6935 | const result_type __bk = __p.__b_[__k]; |
| 6936 | if (__deltad == 0) |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6937 | return __u / __dk + __bk; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6938 | const result_type __bk1 = __p.__b_[__k+1]; |
| 6939 | const result_type __deltab = __bk1 - __bk; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6940 | return (__bk * __dk1 - __bk1 * __dk + |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6941 | _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) / |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6942 | __deltad; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6943 | } |
| 6944 | |
| 6945 | template <class _CharT, class _Traits, class _RT> |
| 6946 | basic_ostream<_CharT, _Traits>& |
| 6947 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6948 | const piecewise_linear_distribution<_RT>& __x) |
| 6949 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6950 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6951 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 6952 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 6953 | _OStream::scientific); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6954 | _CharT __sp = __os.widen(' '); |
| 6955 | __os.fill(__sp); |
| 6956 | size_t __n = __x.__p_.__b_.size(); |
| 6957 | __os << __n; |
| 6958 | for (size_t __i = 0; __i < __n; ++__i) |
| 6959 | __os << __sp << __x.__p_.__b_[__i]; |
| 6960 | __n = __x.__p_.__densities_.size(); |
| 6961 | __os << __sp << __n; |
| 6962 | for (size_t __i = 0; __i < __n; ++__i) |
| 6963 | __os << __sp << __x.__p_.__densities_[__i]; |
| 6964 | __n = __x.__p_.__areas_.size(); |
| 6965 | __os << __sp << __n; |
| 6966 | for (size_t __i = 0; __i < __n; ++__i) |
| 6967 | __os << __sp << __x.__p_.__areas_[__i]; |
| 6968 | return __os; |
| 6969 | } |
| 6970 | |
| 6971 | template <class _CharT, class _Traits, class _RT> |
| 6972 | basic_istream<_CharT, _Traits>& |
| 6973 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6974 | piecewise_linear_distribution<_RT>& __x) |
| 6975 | { |
| 6976 | typedef piecewise_linear_distribution<_RT> _Eng; |
| 6977 | typedef typename _Eng::result_type result_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6978 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6979 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 6980 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6981 | size_t __n; |
| 6982 | __is >> __n; |
| 6983 | vector<result_type> __b(__n); |
| 6984 | for (size_t __i = 0; __i < __n; ++__i) |
| 6985 | __is >> __b[__i]; |
| 6986 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6987 | vector<result_type> __densities(__n); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6988 | for (size_t __i = 0; __i < __n; ++__i) |
| 6989 | __is >> __densities[__i]; |
| 6990 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6991 | vector<result_type> __areas(__n); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6992 | for (size_t __i = 0; __i < __n; ++__i) |
| 6993 | __is >> __areas[__i]; |
| 6994 | if (!__is.fail()) |
| 6995 | { |
| 6996 | swap(__x.__p_.__b_, __b); |
| 6997 | swap(__x.__p_.__densities_, __densities); |
| 6998 | swap(__x.__p_.__areas_, __areas); |
| 6999 | } |
| 7000 | return __is; |
| 7001 | } |
| 7002 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7003 | _LIBCPP_END_NAMESPACE_STD |
| 7004 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 7005 | _LIBCPP_POP_MACROS |
| 7006 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 7007 | #endif // _LIBCPP_RANDOM |