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 | { |
| 20 | |
| 21 | // Engines |
| 22 | |
| 23 | template <class UIntType, UIntType a, UIntType c, UIntType m> |
| 24 | class linear_congruential_engine |
| 25 | { |
| 26 | public: |
| 27 | // types |
| 28 | typedef UIntType result_type; |
| 29 | |
| 30 | // engine characteristics |
| 31 | static constexpr result_type multiplier = a; |
| 32 | static constexpr result_type increment = c; |
| 33 | static constexpr result_type modulus = m; |
| 34 | static constexpr result_type min() { return c == 0u ? 1u: 0u;} |
| 35 | static constexpr result_type max() { return m - 1u;} |
| 36 | static constexpr result_type default_seed = 1u; |
| 37 | |
| 38 | // constructors and seeding functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 39 | explicit linear_congruential_engine(result_type s = default_seed); // before C++20 |
| 40 | linear_congruential_engine() : linear_congruential_engine(default_seed) {} // C++20 |
| 41 | explicit linear_congruential_engine(result_type s); // C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | template<class Sseq> explicit linear_congruential_engine(Sseq& q); |
| 43 | void seed(result_type s = default_seed); |
| 44 | template<class Sseq> void seed(Sseq& q); |
| 45 | |
| 46 | // generating functions |
| 47 | result_type operator()(); |
| 48 | void discard(unsigned long long z); |
| 49 | }; |
| 50 | |
| 51 | template <class UIntType, UIntType a, UIntType c, UIntType m> |
| 52 | bool |
| 53 | operator==(const linear_congruential_engine<UIntType, a, c, m>& x, |
| 54 | const linear_congruential_engine<UIntType, a, c, m>& y); |
| 55 | |
| 56 | template <class UIntType, UIntType a, UIntType c, UIntType m> |
| 57 | bool |
| 58 | operator!=(const linear_congruential_engine<UIntType, a, c, m>& x, |
| 59 | const linear_congruential_engine<UIntType, a, c, m>& y); |
| 60 | |
| 61 | template <class charT, class traits, |
| 62 | class UIntType, UIntType a, UIntType c, UIntType m> |
| 63 | basic_ostream<charT, traits>& |
| 64 | operator<<(basic_ostream<charT, traits>& os, |
| 65 | const linear_congruential_engine<UIntType, a, c, m>& x); |
| 66 | |
| 67 | template <class charT, class traits, |
| 68 | class UIntType, UIntType a, UIntType c, UIntType m> |
| 69 | basic_istream<charT, traits>& |
| 70 | operator>>(basic_istream<charT, traits>& is, |
| 71 | linear_congruential_engine<UIntType, a, c, m>& x); |
| 72 | |
| 73 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 74 | UIntType a, size_t u, UIntType d, size_t s, |
| 75 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 76 | class mersenne_twister_engine |
| 77 | { |
| 78 | public: |
| 79 | // types |
| 80 | typedef UIntType result_type; |
| 81 | |
| 82 | // engine characteristics |
| 83 | static constexpr size_t word_size = w; |
| 84 | static constexpr size_t state_size = n; |
| 85 | static constexpr size_t shift_size = m; |
| 86 | static constexpr size_t mask_bits = r; |
| 87 | static constexpr result_type xor_mask = a; |
| 88 | static constexpr size_t tempering_u = u; |
| 89 | static constexpr result_type tempering_d = d; |
| 90 | static constexpr size_t tempering_s = s; |
| 91 | static constexpr result_type tempering_b = b; |
| 92 | static constexpr size_t tempering_t = t; |
| 93 | static constexpr result_type tempering_c = c; |
| 94 | static constexpr size_t tempering_l = l; |
| 95 | static constexpr result_type initialization_multiplier = f; |
| 96 | static constexpr result_type min () { return 0; } |
| 97 | static constexpr result_type max() { return 2^w - 1; } |
| 98 | static constexpr result_type default_seed = 5489u; |
| 99 | |
| 100 | // constructors and seeding functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 101 | explicit mersenne_twister_engine(result_type s = default_seed); // before C++20 |
| 102 | mersenne_twister_engine() : mersenne_twister_engine(default_seed) {} // C++20 |
| 103 | explicit mersenne_twister_engine(result_type s); // C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 104 | template<class Sseq> explicit mersenne_twister_engine(Sseq& q); |
| 105 | void seed(result_type value = default_seed); |
| 106 | template<class Sseq> void seed(Sseq& q); |
| 107 | |
| 108 | // generating functions |
| 109 | result_type operator()(); |
| 110 | void discard(unsigned long long z); |
| 111 | }; |
| 112 | |
| 113 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 114 | UIntType a, size_t u, UIntType d, size_t s, |
| 115 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 116 | bool |
| 117 | operator==( |
| 118 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x, |
| 119 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y); |
| 120 | |
| 121 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 122 | UIntType a, size_t u, UIntType d, size_t s, |
| 123 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 124 | bool |
| 125 | operator!=( |
| 126 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x, |
| 127 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y); |
| 128 | |
| 129 | template <class charT, class traits, |
| 130 | class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 131 | UIntType a, size_t u, UIntType d, size_t s, |
| 132 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 133 | basic_ostream<charT, traits>& |
| 134 | operator<<(basic_ostream<charT, traits>& os, |
| 135 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x); |
| 136 | |
| 137 | template <class charT, class traits, |
| 138 | class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 139 | UIntType a, size_t u, UIntType d, size_t s, |
| 140 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 141 | basic_istream<charT, traits>& |
| 142 | operator>>(basic_istream<charT, traits>& is, |
| 143 | mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x); |
| 144 | |
| 145 | template<class UIntType, size_t w, size_t s, size_t r> |
| 146 | class subtract_with_carry_engine |
| 147 | { |
| 148 | public: |
| 149 | // types |
| 150 | typedef UIntType result_type; |
| 151 | |
| 152 | // engine characteristics |
| 153 | static constexpr size_t word_size = w; |
| 154 | static constexpr size_t short_lag = s; |
| 155 | static constexpr size_t long_lag = r; |
| 156 | static constexpr result_type min() { return 0; } |
| 157 | static constexpr result_type max() { return m-1; } |
| 158 | static constexpr result_type default_seed = 19780503u; |
| 159 | |
| 160 | // constructors and seeding functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 161 | explicit subtract_with_carry_engine(result_type value = default_seed); // before C++20 |
| 162 | subtract_with_carry_engine() : subtract_with_carry_engine(default_seed) {} // C++20 |
| 163 | explicit subtract_with_carry_engine(result_type value); // C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 164 | template<class Sseq> explicit subtract_with_carry_engine(Sseq& q); |
| 165 | void seed(result_type value = default_seed); |
| 166 | template<class Sseq> void seed(Sseq& q); |
| 167 | |
| 168 | // generating functions |
| 169 | result_type operator()(); |
| 170 | void discard(unsigned long long z); |
| 171 | }; |
| 172 | |
| 173 | template<class UIntType, size_t w, size_t s, size_t r> |
| 174 | bool |
| 175 | operator==( |
| 176 | const subtract_with_carry_engine<UIntType, w, s, r>& x, |
| 177 | const subtract_with_carry_engine<UIntType, w, s, r>& y); |
| 178 | |
| 179 | template<class UIntType, size_t w, size_t s, size_t r> |
| 180 | bool |
| 181 | operator!=( |
| 182 | const subtract_with_carry_engine<UIntType, w, s, r>& x, |
| 183 | const subtract_with_carry_engine<UIntType, w, s, r>& y); |
| 184 | |
| 185 | template <class charT, class traits, |
| 186 | class UIntType, size_t w, size_t s, size_t r> |
| 187 | basic_ostream<charT, traits>& |
| 188 | operator<<(basic_ostream<charT, traits>& os, |
| 189 | const subtract_with_carry_engine<UIntType, w, s, r>& x); |
| 190 | |
| 191 | template <class charT, class traits, |
| 192 | class UIntType, size_t w, size_t s, size_t r> |
| 193 | basic_istream<charT, traits>& |
| 194 | operator>>(basic_istream<charT, traits>& is, |
| 195 | subtract_with_carry_engine<UIntType, w, s, r>& x); |
| 196 | |
| 197 | template<class Engine, size_t p, size_t r> |
| 198 | class discard_block_engine |
| 199 | { |
| 200 | public: |
| 201 | // types |
| 202 | typedef typename Engine::result_type result_type; |
| 203 | |
| 204 | // engine characteristics |
| 205 | static constexpr size_t block_size = p; |
| 206 | static constexpr size_t used_block = r; |
| 207 | static constexpr result_type min() { return Engine::min(); } |
| 208 | static constexpr result_type max() { return Engine::max(); } |
| 209 | |
| 210 | // constructors and seeding functions |
| 211 | discard_block_engine(); |
| 212 | explicit discard_block_engine(const Engine& e); |
| 213 | explicit discard_block_engine(Engine&& e); |
| 214 | explicit discard_block_engine(result_type s); |
| 215 | template<class Sseq> explicit discard_block_engine(Sseq& q); |
| 216 | void seed(); |
| 217 | void seed(result_type s); |
| 218 | template<class Sseq> void seed(Sseq& q); |
| 219 | |
| 220 | // generating functions |
| 221 | result_type operator()(); |
| 222 | void discard(unsigned long long z); |
| 223 | |
| 224 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 225 | const Engine& base() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | template<class Engine, size_t p, size_t r> |
| 229 | bool |
| 230 | operator==( |
| 231 | const discard_block_engine<Engine, p, r>& x, |
| 232 | const discard_block_engine<Engine, p, r>& y); |
| 233 | |
| 234 | template<class Engine, size_t p, size_t r> |
| 235 | bool |
| 236 | operator!=( |
| 237 | const discard_block_engine<Engine, p, r>& x, |
| 238 | const discard_block_engine<Engine, p, r>& y); |
| 239 | |
| 240 | template <class charT, class traits, |
| 241 | class Engine, size_t p, size_t r> |
| 242 | basic_ostream<charT, traits>& |
| 243 | operator<<(basic_ostream<charT, traits>& os, |
| 244 | const discard_block_engine<Engine, p, r>& x); |
| 245 | |
| 246 | template <class charT, class traits, |
| 247 | class Engine, size_t p, size_t r> |
| 248 | basic_istream<charT, traits>& |
| 249 | operator>>(basic_istream<charT, traits>& is, |
| 250 | discard_block_engine<Engine, p, r>& x); |
| 251 | |
| 252 | template<class Engine, size_t w, class UIntType> |
| 253 | class independent_bits_engine |
| 254 | { |
| 255 | public: |
| 256 | // types |
| 257 | typedef UIntType result_type; |
| 258 | |
| 259 | // engine characteristics |
| 260 | static constexpr result_type min() { return 0; } |
| 261 | static constexpr result_type max() { return 2^w - 1; } |
| 262 | |
| 263 | // constructors and seeding functions |
| 264 | independent_bits_engine(); |
| 265 | explicit independent_bits_engine(const Engine& e); |
| 266 | explicit independent_bits_engine(Engine&& e); |
| 267 | explicit independent_bits_engine(result_type s); |
| 268 | template<class Sseq> explicit independent_bits_engine(Sseq& q); |
| 269 | void seed(); |
| 270 | void seed(result_type s); |
| 271 | template<class Sseq> void seed(Sseq& q); |
| 272 | |
| 273 | // generating functions |
| 274 | result_type operator()(); void discard(unsigned long long z); |
| 275 | |
| 276 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 277 | const Engine& base() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | template<class Engine, size_t w, class UIntType> |
| 281 | bool |
| 282 | operator==( |
| 283 | const independent_bits_engine<Engine, w, UIntType>& x, |
| 284 | const independent_bits_engine<Engine, w, UIntType>& y); |
| 285 | |
| 286 | template<class Engine, size_t w, class UIntType> |
| 287 | bool |
| 288 | operator!=( |
| 289 | const independent_bits_engine<Engine, w, UIntType>& x, |
| 290 | const independent_bits_engine<Engine, w, UIntType>& y); |
| 291 | |
| 292 | template <class charT, class traits, |
| 293 | class Engine, size_t w, class UIntType> |
| 294 | basic_ostream<charT, traits>& |
| 295 | operator<<(basic_ostream<charT, traits>& os, |
| 296 | const independent_bits_engine<Engine, w, UIntType>& x); |
| 297 | |
| 298 | template <class charT, class traits, |
| 299 | class Engine, size_t w, class UIntType> |
| 300 | basic_istream<charT, traits>& |
| 301 | operator>>(basic_istream<charT, traits>& is, |
| 302 | independent_bits_engine<Engine, w, UIntType>& x); |
| 303 | |
| 304 | template<class Engine, size_t k> |
| 305 | class shuffle_order_engine |
| 306 | { |
| 307 | public: |
| 308 | // types |
| 309 | typedef typename Engine::result_type result_type; |
| 310 | |
| 311 | // engine characteristics |
| 312 | static constexpr size_t table_size = k; |
| 313 | static constexpr result_type min() { return Engine::min; } |
| 314 | static constexpr result_type max() { return Engine::max; } |
| 315 | |
| 316 | // constructors and seeding functions |
| 317 | shuffle_order_engine(); |
| 318 | explicit shuffle_order_engine(const Engine& e); |
| 319 | explicit shuffle_order_engine(Engine&& e); |
| 320 | explicit shuffle_order_engine(result_type s); |
| 321 | template<class Sseq> explicit shuffle_order_engine(Sseq& q); |
| 322 | void seed(); |
| 323 | void seed(result_type s); |
| 324 | template<class Sseq> void seed(Sseq& q); |
| 325 | |
| 326 | // generating functions |
| 327 | result_type operator()(); |
| 328 | void discard(unsigned long long z); |
| 329 | |
| 330 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 331 | const Engine& base() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | template<class Engine, size_t k> |
| 335 | bool |
| 336 | operator==( |
| 337 | const shuffle_order_engine<Engine, k>& x, |
| 338 | const shuffle_order_engine<Engine, k>& y); |
| 339 | |
| 340 | template<class Engine, size_t k> |
| 341 | bool |
| 342 | operator!=( |
| 343 | const shuffle_order_engine<Engine, k>& x, |
| 344 | const shuffle_order_engine<Engine, k>& y); |
| 345 | |
| 346 | template <class charT, class traits, |
| 347 | class Engine, size_t k> |
| 348 | basic_ostream<charT, traits>& |
| 349 | operator<<(basic_ostream<charT, traits>& os, |
| 350 | const shuffle_order_engine<Engine, k>& x); |
| 351 | |
| 352 | template <class charT, class traits, |
| 353 | class Engine, size_t k> |
| 354 | basic_istream<charT, traits>& |
| 355 | operator>>(basic_istream<charT, traits>& is, |
| 356 | shuffle_order_engine<Engine, k>& x); |
| 357 | |
| 358 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> |
| 359 | minstd_rand0; |
| 360 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> |
| 361 | minstd_rand; |
| 362 | typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, |
| 363 | 0x9908b0df, |
| 364 | 11, 0xffffffff, |
| 365 | 7, 0x9d2c5680, |
| 366 | 15, 0xefc60000, |
| 367 | 18, 1812433253> mt19937; |
| 368 | typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31, |
| 369 | 0xb5026f5aa96619e9, |
| 370 | 29, 0x5555555555555555, |
| 371 | 17, 0x71d67fffeda60000, |
| 372 | 37, 0xfff7eee000000000, |
| 373 | 43, 6364136223846793005> mt19937_64; |
| 374 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base; |
| 375 | typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base; |
| 376 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
| 377 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
| 378 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 379 | typedef minstd_rand default_random_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 380 | |
| 381 | // Generators |
| 382 | |
| 383 | class random_device |
| 384 | { |
| 385 | public: |
| 386 | // types |
| 387 | typedef unsigned int result_type; |
| 388 | |
| 389 | // generator characteristics |
| 390 | static constexpr result_type min() { return numeric_limits<result_type>::min(); } |
| 391 | static constexpr result_type max() { return numeric_limits<result_type>::max(); } |
| 392 | |
| 393 | // constructors |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 394 | explicit random_device(const string& token = implementation-defined); // before C++20 |
| 395 | random_device() : random_device(implementation-defined) {} // C++20 |
| 396 | explicit random_device(const string& token); // C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 397 | |
| 398 | // generating functions |
| 399 | result_type operator()(); |
| 400 | |
| 401 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 402 | double entropy() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 403 | |
| 404 | // no copy functions |
| 405 | random_device(const random_device& ) = delete; |
| 406 | void operator=(const random_device& ) = delete; |
| 407 | }; |
| 408 | |
| 409 | // Utilities |
| 410 | |
| 411 | class seed_seq |
| 412 | { |
| 413 | public: |
| 414 | // types |
| 415 | typedef uint_least32_t result_type; |
| 416 | |
| 417 | // constructors |
| 418 | seed_seq(); |
| 419 | template<class T> |
| 420 | seed_seq(initializer_list<T> il); |
| 421 | template<class InputIterator> |
| 422 | seed_seq(InputIterator begin, InputIterator end); |
| 423 | |
| 424 | // generating functions |
| 425 | template<class RandomAccessIterator> |
| 426 | void generate(RandomAccessIterator begin, RandomAccessIterator end); |
| 427 | |
| 428 | // property functions |
| 429 | size_t size() const; |
| 430 | template<class OutputIterator> |
| 431 | void param(OutputIterator dest) const; |
| 432 | |
| 433 | // no copy functions |
| 434 | seed_seq(const seed_seq&) = delete; |
| 435 | void operator=(const seed_seq& ) = delete; |
| 436 | }; |
| 437 | |
| 438 | template<class RealType, size_t bits, class URNG> |
| 439 | RealType generate_canonical(URNG& g); |
| 440 | |
| 441 | // Distributions |
| 442 | |
| 443 | template<class IntType = int> |
| 444 | class uniform_int_distribution |
| 445 | { |
| 446 | public: |
| 447 | // types |
| 448 | typedef IntType result_type; |
| 449 | |
| 450 | class param_type |
| 451 | { |
| 452 | public: |
| 453 | typedef uniform_int_distribution distribution_type; |
| 454 | |
| 455 | explicit param_type(IntType a = 0, |
| 456 | IntType b = numeric_limits<IntType>::max()); |
| 457 | |
| 458 | result_type a() const; |
| 459 | result_type b() const; |
| 460 | |
| 461 | friend bool operator==(const param_type& x, const param_type& y); |
| 462 | friend bool operator!=(const param_type& x, const param_type& y); |
| 463 | }; |
| 464 | |
| 465 | // constructors and reset functions |
| 466 | explicit uniform_int_distribution(IntType a = 0, |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 467 | IntType b = numeric_limits<IntType>::max()); // before C++20 |
| 468 | uniform_int_distribution() : uniform_int_distribution(0) {} // C++20 |
| 469 | explicit uniform_int_distribution(IntType a, |
| 470 | IntType b = numeric_limits<IntType>::max()); // C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 471 | explicit uniform_int_distribution(const param_type& parm); |
| 472 | void reset(); |
| 473 | |
| 474 | // generating functions |
| 475 | template<class URNG> result_type operator()(URNG& g); |
| 476 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 477 | |
| 478 | // property functions |
| 479 | result_type a() const; |
| 480 | result_type b() const; |
| 481 | |
| 482 | param_type param() const; |
| 483 | void param(const param_type& parm); |
| 484 | |
| 485 | result_type min() const; |
| 486 | result_type max() const; |
| 487 | |
| 488 | friend bool operator==(const uniform_int_distribution& x, |
| 489 | const uniform_int_distribution& y); |
| 490 | friend bool operator!=(const uniform_int_distribution& x, |
| 491 | const uniform_int_distribution& y); |
| 492 | |
| 493 | template <class charT, class traits> |
| 494 | friend |
| 495 | basic_ostream<charT, traits>& |
| 496 | operator<<(basic_ostream<charT, traits>& os, |
| 497 | const uniform_int_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 498 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 499 | template <class charT, class traits> |
| 500 | friend |
| 501 | basic_istream<charT, traits>& |
| 502 | operator>>(basic_istream<charT, traits>& is, |
| 503 | uniform_int_distribution& x); |
| 504 | }; |
| 505 | |
| 506 | template<class RealType = double> |
| 507 | class uniform_real_distribution |
| 508 | { |
| 509 | public: |
| 510 | // types |
| 511 | typedef RealType result_type; |
| 512 | |
| 513 | class param_type |
| 514 | { |
| 515 | public: |
| 516 | typedef uniform_real_distribution distribution_type; |
| 517 | |
| 518 | explicit param_type(RealType a = 0, |
| 519 | RealType b = 1); |
| 520 | |
| 521 | result_type a() const; |
| 522 | result_type b() const; |
| 523 | |
| 524 | friend bool operator==(const param_type& x, const param_type& y); |
| 525 | friend bool operator!=(const param_type& x, const param_type& y); |
| 526 | }; |
| 527 | |
| 528 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 529 | explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0); // before C++20 |
| 530 | uniform_real_distribution() : uniform_real_distribution(0.0) {} // C++20 |
| 531 | explicit uniform_real_distribution(RealType a, RealType b = 1.0); // C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 532 | explicit uniform_real_distribution(const param_type& parm); |
| 533 | void reset(); |
| 534 | |
| 535 | // generating functions |
| 536 | template<class URNG> result_type operator()(URNG& g); |
| 537 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 538 | |
| 539 | // property functions |
| 540 | result_type a() const; |
| 541 | result_type b() const; |
| 542 | |
| 543 | param_type param() const; |
| 544 | void param(const param_type& parm); |
| 545 | |
| 546 | result_type min() const; |
| 547 | result_type max() const; |
| 548 | |
| 549 | friend bool operator==(const uniform_real_distribution& x, |
| 550 | const uniform_real_distribution& y); |
| 551 | friend bool operator!=(const uniform_real_distribution& x, |
| 552 | const uniform_real_distribution& y); |
| 553 | |
| 554 | template <class charT, class traits> |
| 555 | friend |
| 556 | basic_ostream<charT, traits>& |
| 557 | operator<<(basic_ostream<charT, traits>& os, |
| 558 | const uniform_real_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 559 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 560 | template <class charT, class traits> |
| 561 | friend |
| 562 | basic_istream<charT, traits>& |
| 563 | operator>>(basic_istream<charT, traits>& is, |
| 564 | uniform_real_distribution& x); |
| 565 | }; |
| 566 | |
| 567 | class bernoulli_distribution |
| 568 | { |
| 569 | public: |
| 570 | // types |
| 571 | typedef bool result_type; |
| 572 | |
| 573 | class param_type |
| 574 | { |
| 575 | public: |
| 576 | typedef bernoulli_distribution distribution_type; |
| 577 | |
| 578 | explicit param_type(double p = 0.5); |
| 579 | |
| 580 | double p() const; |
| 581 | |
| 582 | friend bool operator==(const param_type& x, const param_type& y); |
| 583 | friend bool operator!=(const param_type& x, const param_type& y); |
| 584 | }; |
| 585 | |
| 586 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 587 | explicit bernoulli_distribution(double p = 0.5); // before C++20 |
| 588 | bernoulli_distribution() : bernoulli_distribution(0.5) {} // C++20 |
| 589 | explicit bernoulli_distribution(double p); // C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 590 | explicit bernoulli_distribution(const param_type& parm); |
| 591 | void reset(); |
| 592 | |
| 593 | // generating functions |
| 594 | template<class URNG> result_type operator()(URNG& g); |
| 595 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 596 | |
| 597 | // property functions |
| 598 | double p() const; |
| 599 | |
| 600 | param_type param() const; |
| 601 | void param(const param_type& parm); |
| 602 | |
| 603 | result_type min() const; |
| 604 | result_type max() const; |
| 605 | |
| 606 | friend bool operator==(const bernoulli_distribution& x, |
| 607 | const bernoulli_distribution& y); |
| 608 | friend bool operator!=(const bernoulli_distribution& x, |
| 609 | const bernoulli_distribution& y); |
| 610 | |
| 611 | template <class charT, class traits> |
| 612 | friend |
| 613 | basic_ostream<charT, traits>& |
| 614 | operator<<(basic_ostream<charT, traits>& os, |
| 615 | const bernoulli_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 616 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 617 | template <class charT, class traits> |
| 618 | friend |
| 619 | basic_istream<charT, traits>& |
| 620 | operator>>(basic_istream<charT, traits>& is, |
| 621 | bernoulli_distribution& x); |
| 622 | }; |
| 623 | |
| 624 | template<class IntType = int> |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 625 | class binomial_distribution |
| 626 | { |
| 627 | public: |
| 628 | // types |
| 629 | typedef IntType result_type; |
| 630 | |
| 631 | class param_type |
| 632 | { |
| 633 | public: |
| 634 | typedef binomial_distribution distribution_type; |
| 635 | |
| 636 | explicit param_type(IntType t = 1, double p = 0.5); |
| 637 | |
| 638 | IntType t() const; |
| 639 | double p() const; |
| 640 | |
| 641 | friend bool operator==(const param_type& x, const param_type& y); |
| 642 | friend bool operator!=(const param_type& x, const param_type& y); |
| 643 | }; |
| 644 | |
| 645 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 646 | explicit binomial_distribution(IntType t = 1, double p = 0.5); // before C++20 |
| 647 | binomial_distribution() : binomial_distribution(1) {} // C++20 |
| 648 | explicit binomial_distribution(IntType t, double p = 0.5); // C++20 |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 649 | explicit binomial_distribution(const param_type& parm); |
| 650 | void reset(); |
| 651 | |
| 652 | // generating functions |
| 653 | template<class URNG> result_type operator()(URNG& g); |
| 654 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 655 | |
| 656 | // property functions |
| 657 | IntType t() const; |
| 658 | double p() const; |
| 659 | |
| 660 | param_type param() const; |
| 661 | void param(const param_type& parm); |
| 662 | |
| 663 | result_type min() const; |
| 664 | result_type max() const; |
| 665 | |
| 666 | friend bool operator==(const binomial_distribution& x, |
| 667 | const binomial_distribution& y); |
| 668 | friend bool operator!=(const binomial_distribution& x, |
| 669 | const binomial_distribution& y); |
| 670 | |
| 671 | template <class charT, class traits> |
| 672 | friend |
| 673 | basic_ostream<charT, traits>& |
| 674 | operator<<(basic_ostream<charT, traits>& os, |
| 675 | const binomial_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 676 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 677 | template <class charT, class traits> |
| 678 | friend |
| 679 | basic_istream<charT, traits>& |
| 680 | operator>>(basic_istream<charT, traits>& is, |
| 681 | binomial_distribution& x); |
| 682 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 683 | |
| 684 | template<class IntType = int> |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 685 | class geometric_distribution |
| 686 | { |
| 687 | public: |
| 688 | // types |
| 689 | typedef IntType result_type; |
| 690 | |
| 691 | class param_type |
| 692 | { |
| 693 | public: |
| 694 | typedef geometric_distribution distribution_type; |
| 695 | |
| 696 | explicit param_type(double p = 0.5); |
| 697 | |
| 698 | double p() const; |
| 699 | |
| 700 | friend bool operator==(const param_type& x, const param_type& y); |
| 701 | friend bool operator!=(const param_type& x, const param_type& y); |
| 702 | }; |
| 703 | |
| 704 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 705 | explicit geometric_distribution(double p = 0.5); // before C++20 |
| 706 | geometric_distribution() : geometric_distribution(0.5) {} // C++20 |
| 707 | explicit geometric_distribution(double p); // C++20 |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 708 | explicit geometric_distribution(const param_type& parm); |
| 709 | void reset(); |
| 710 | |
| 711 | // generating functions |
| 712 | template<class URNG> result_type operator()(URNG& g); |
| 713 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 714 | |
| 715 | // property functions |
| 716 | double p() const; |
| 717 | |
| 718 | param_type param() const; |
| 719 | void param(const param_type& parm); |
| 720 | |
| 721 | result_type min() const; |
| 722 | result_type max() const; |
| 723 | |
| 724 | friend bool operator==(const geometric_distribution& x, |
| 725 | const geometric_distribution& y); |
| 726 | friend bool operator!=(const geometric_distribution& x, |
| 727 | const geometric_distribution& y); |
| 728 | |
| 729 | template <class charT, class traits> |
| 730 | friend |
| 731 | basic_ostream<charT, traits>& |
| 732 | operator<<(basic_ostream<charT, traits>& os, |
| 733 | const geometric_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 734 | |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 735 | template <class charT, class traits> |
| 736 | friend |
| 737 | basic_istream<charT, traits>& |
| 738 | operator>>(basic_istream<charT, traits>& is, |
| 739 | geometric_distribution& x); |
| 740 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 741 | |
| 742 | template<class IntType = int> |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 743 | class negative_binomial_distribution |
| 744 | { |
| 745 | public: |
| 746 | // types |
| 747 | typedef IntType result_type; |
| 748 | |
| 749 | class param_type |
| 750 | { |
| 751 | public: |
| 752 | typedef negative_binomial_distribution distribution_type; |
| 753 | |
| 754 | explicit param_type(result_type k = 1, double p = 0.5); |
| 755 | |
| 756 | result_type k() const; |
| 757 | double p() const; |
| 758 | |
| 759 | friend bool operator==(const param_type& x, const param_type& y); |
| 760 | friend bool operator!=(const param_type& x, const param_type& y); |
| 761 | }; |
| 762 | |
| 763 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 764 | explicit negative_binomial_distribution(IntType k = 1, double p = 0.5); // before C++20 |
| 765 | negative_binomial_distribution() : negative_binomial_distribution(1) {} // C++20 |
| 766 | explicit negative_binomial_distribution(IntType k, double p = 0.5); // C++20 |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 767 | explicit negative_binomial_distribution(const param_type& parm); |
| 768 | void reset(); |
| 769 | |
| 770 | // generating functions |
| 771 | template<class URNG> result_type operator()(URNG& g); |
| 772 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 773 | |
| 774 | // property functions |
| 775 | result_type k() const; |
| 776 | double p() const; |
| 777 | |
| 778 | param_type param() const; |
| 779 | void param(const param_type& parm); |
| 780 | |
| 781 | result_type min() const; |
| 782 | result_type max() const; |
| 783 | |
| 784 | friend bool operator==(const negative_binomial_distribution& x, |
| 785 | const negative_binomial_distribution& y); |
| 786 | friend bool operator!=(const negative_binomial_distribution& x, |
| 787 | const negative_binomial_distribution& y); |
| 788 | |
| 789 | template <class charT, class traits> |
| 790 | friend |
| 791 | basic_ostream<charT, traits>& |
| 792 | operator<<(basic_ostream<charT, traits>& os, |
| 793 | const negative_binomial_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 794 | |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 795 | template <class charT, class traits> |
| 796 | friend |
| 797 | basic_istream<charT, traits>& |
| 798 | operator>>(basic_istream<charT, traits>& is, |
| 799 | negative_binomial_distribution& x); |
| 800 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 801 | |
| 802 | template<class IntType = int> |
Howard Hinnant | 24a5f2a | 2010-05-14 21:38:54 +0000 | [diff] [blame] | 803 | class poisson_distribution |
| 804 | { |
| 805 | public: |
| 806 | // types |
| 807 | typedef IntType result_type; |
| 808 | |
| 809 | class param_type |
| 810 | { |
| 811 | public: |
| 812 | typedef poisson_distribution distribution_type; |
| 813 | |
| 814 | explicit param_type(double mean = 1.0); |
| 815 | |
| 816 | double mean() const; |
| 817 | |
| 818 | friend bool operator==(const param_type& x, const param_type& y); |
| 819 | friend bool operator!=(const param_type& x, const param_type& y); |
| 820 | }; |
| 821 | |
| 822 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 823 | explicit poisson_distribution(double mean = 1.0); // before C++20 |
| 824 | poisson_distribution() : poisson_distribution(1.0) {} // C++20 |
| 825 | explicit poisson_distribution(double mean); // C++20 |
Howard Hinnant | 24a5f2a | 2010-05-14 21:38:54 +0000 | [diff] [blame] | 826 | explicit poisson_distribution(const param_type& parm); |
| 827 | void reset(); |
| 828 | |
| 829 | // generating functions |
| 830 | template<class URNG> result_type operator()(URNG& g); |
| 831 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 832 | |
| 833 | // property functions |
| 834 | double mean() const; |
| 835 | |
| 836 | param_type param() const; |
| 837 | void param(const param_type& parm); |
| 838 | |
| 839 | result_type min() const; |
| 840 | result_type max() const; |
| 841 | |
| 842 | friend bool operator==(const poisson_distribution& x, |
| 843 | const poisson_distribution& y); |
| 844 | friend bool operator!=(const poisson_distribution& x, |
| 845 | const poisson_distribution& y); |
| 846 | |
| 847 | template <class charT, class traits> |
| 848 | friend |
| 849 | basic_ostream<charT, traits>& |
| 850 | operator<<(basic_ostream<charT, traits>& os, |
| 851 | const poisson_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 852 | |
Howard Hinnant | 24a5f2a | 2010-05-14 21:38:54 +0000 | [diff] [blame] | 853 | template <class charT, class traits> |
| 854 | friend |
| 855 | basic_istream<charT, traits>& |
| 856 | operator>>(basic_istream<charT, traits>& is, |
| 857 | poisson_distribution& x); |
| 858 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 859 | |
| 860 | template<class RealType = double> |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 861 | class exponential_distribution |
| 862 | { |
| 863 | public: |
| 864 | // types |
| 865 | typedef RealType result_type; |
| 866 | |
| 867 | class param_type |
| 868 | { |
| 869 | public: |
| 870 | typedef exponential_distribution distribution_type; |
| 871 | |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 872 | explicit param_type(result_type lambda = 1.0); |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 873 | |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 874 | result_type lambda() const; |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 875 | |
| 876 | friend bool operator==(const param_type& x, const param_type& y); |
| 877 | friend bool operator!=(const param_type& x, const param_type& y); |
| 878 | }; |
| 879 | |
| 880 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 881 | explicit exponential_distribution(RealType lambda = 1.0); // before C++20 |
| 882 | exponential_distribution() : exponential_distribution(1.0) {} // C++20 |
| 883 | explicit exponential_distribution(RealType lambda); // C++20 |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 884 | explicit exponential_distribution(const param_type& parm); |
| 885 | void reset(); |
| 886 | |
| 887 | // generating functions |
| 888 | template<class URNG> result_type operator()(URNG& g); |
| 889 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 890 | |
| 891 | // property functions |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 892 | result_type lambda() const; |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 893 | |
| 894 | param_type param() const; |
| 895 | void param(const param_type& parm); |
| 896 | |
| 897 | result_type min() const; |
| 898 | result_type max() const; |
| 899 | |
| 900 | friend bool operator==(const exponential_distribution& x, |
| 901 | const exponential_distribution& y); |
| 902 | friend bool operator!=(const exponential_distribution& x, |
| 903 | const exponential_distribution& y); |
| 904 | |
| 905 | template <class charT, class traits> |
| 906 | friend |
| 907 | basic_ostream<charT, traits>& |
| 908 | operator<<(basic_ostream<charT, traits>& os, |
| 909 | const exponential_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 910 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 911 | template <class charT, class traits> |
| 912 | friend |
| 913 | basic_istream<charT, traits>& |
| 914 | operator>>(basic_istream<charT, traits>& is, |
| 915 | exponential_distribution& x); |
| 916 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 917 | |
| 918 | template<class RealType = double> |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 919 | class gamma_distribution |
| 920 | { |
| 921 | public: |
| 922 | // types |
| 923 | typedef RealType result_type; |
| 924 | |
| 925 | class param_type |
| 926 | { |
| 927 | public: |
| 928 | typedef gamma_distribution distribution_type; |
| 929 | |
| 930 | explicit param_type(result_type alpha = 1, result_type beta = 1); |
| 931 | |
| 932 | result_type alpha() const; |
| 933 | result_type beta() const; |
| 934 | |
| 935 | friend bool operator==(const param_type& x, const param_type& y); |
| 936 | friend bool operator!=(const param_type& x, const param_type& y); |
| 937 | }; |
| 938 | |
| 939 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 940 | explicit gamma_distribution(RealType alpha = 0.0, RealType beta = 1.0); // before C++20 |
| 941 | gamma_distribution() : gamma_distribution(0.0) {} // C++20 |
| 942 | explicit gamma_distribution(RealType alpha, RealType beta = 1.0); // C++20 |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 943 | explicit gamma_distribution(const param_type& parm); |
| 944 | void reset(); |
| 945 | |
| 946 | // generating functions |
| 947 | template<class URNG> result_type operator()(URNG& g); |
| 948 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 949 | |
| 950 | // property functions |
| 951 | result_type alpha() const; |
| 952 | result_type beta() const; |
| 953 | |
| 954 | param_type param() const; |
| 955 | void param(const param_type& parm); |
| 956 | |
| 957 | result_type min() const; |
| 958 | result_type max() const; |
| 959 | |
| 960 | friend bool operator==(const gamma_distribution& x, |
| 961 | const gamma_distribution& y); |
| 962 | friend bool operator!=(const gamma_distribution& x, |
| 963 | const gamma_distribution& y); |
| 964 | |
| 965 | template <class charT, class traits> |
| 966 | friend |
| 967 | basic_ostream<charT, traits>& |
| 968 | operator<<(basic_ostream<charT, traits>& os, |
| 969 | const gamma_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 970 | |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 971 | template <class charT, class traits> |
| 972 | friend |
| 973 | basic_istream<charT, traits>& |
| 974 | operator>>(basic_istream<charT, traits>& is, |
| 975 | gamma_distribution& x); |
| 976 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 977 | |
| 978 | template<class RealType = double> |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 979 | class weibull_distribution |
| 980 | { |
| 981 | public: |
| 982 | // types |
| 983 | typedef RealType result_type; |
| 984 | |
| 985 | class param_type |
| 986 | { |
| 987 | public: |
| 988 | typedef weibull_distribution distribution_type; |
| 989 | |
| 990 | explicit param_type(result_type alpha = 1, result_type beta = 1); |
| 991 | |
| 992 | result_type a() const; |
| 993 | result_type b() const; |
| 994 | |
| 995 | friend bool operator==(const param_type& x, const param_type& y); |
| 996 | friend bool operator!=(const param_type& x, const param_type& y); |
| 997 | }; |
| 998 | |
| 999 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 1000 | explicit weibull_distribution(RealType a = 1.0, RealType b = 1.0); // before C++20 |
| 1001 | weibull_distribution() : weibull_distribution(1.0) {} // C++20 |
| 1002 | explicit weibull_distribution(RealType a, RealType b = 1.0); // C++20 |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 1003 | explicit weibull_distribution(const param_type& parm); |
| 1004 | void reset(); |
| 1005 | |
| 1006 | // generating functions |
| 1007 | template<class URNG> result_type operator()(URNG& g); |
| 1008 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1009 | |
| 1010 | // property functions |
| 1011 | result_type a() const; |
| 1012 | result_type b() const; |
| 1013 | |
| 1014 | param_type param() const; |
| 1015 | void param(const param_type& parm); |
| 1016 | |
| 1017 | result_type min() const; |
| 1018 | result_type max() const; |
| 1019 | |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 1020 | friend bool operator==(const weibull_distribution& x, |
| 1021 | const weibull_distribution& y); |
| 1022 | friend bool operator!=(const weibull_distribution& x, |
| 1023 | const weibull_distribution& y); |
| 1024 | |
| 1025 | template <class charT, class traits> |
| 1026 | friend |
| 1027 | basic_ostream<charT, traits>& |
| 1028 | operator<<(basic_ostream<charT, traits>& os, |
| 1029 | const weibull_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1030 | |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 1031 | template <class charT, class traits> |
| 1032 | friend |
| 1033 | basic_istream<charT, traits>& |
| 1034 | operator>>(basic_istream<charT, traits>& is, |
| 1035 | weibull_distribution& x); |
| 1036 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1037 | |
| 1038 | template<class RealType = double> |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 1039 | class extreme_value_distribution |
| 1040 | { |
| 1041 | public: |
| 1042 | // types |
| 1043 | typedef RealType result_type; |
| 1044 | |
| 1045 | class param_type |
| 1046 | { |
| 1047 | public: |
| 1048 | typedef extreme_value_distribution distribution_type; |
| 1049 | |
| 1050 | explicit param_type(result_type a = 0, result_type b = 1); |
| 1051 | |
| 1052 | result_type a() const; |
| 1053 | result_type b() const; |
| 1054 | |
| 1055 | friend bool operator==(const param_type& x, const param_type& y); |
| 1056 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1057 | }; |
| 1058 | |
| 1059 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 1060 | explicit extreme_value_distribution(RealType a = 0.0, RealType b = 1.0); // before C++20 |
| 1061 | extreme_value_distribution() : extreme_value_distribution(0.0) {} // C++20 |
| 1062 | explicit extreme_value_distribution(RealType a, RealType b = 1.0); // C++20 |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 1063 | explicit extreme_value_distribution(const param_type& parm); |
| 1064 | void reset(); |
| 1065 | |
| 1066 | // generating functions |
| 1067 | template<class URNG> result_type operator()(URNG& g); |
| 1068 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1069 | |
| 1070 | // property functions |
| 1071 | result_type a() const; |
| 1072 | result_type b() const; |
| 1073 | |
| 1074 | param_type param() const; |
| 1075 | void param(const param_type& parm); |
| 1076 | |
| 1077 | result_type min() const; |
| 1078 | result_type max() const; |
| 1079 | |
| 1080 | friend bool operator==(const extreme_value_distribution& x, |
| 1081 | const extreme_value_distribution& y); |
| 1082 | friend bool operator!=(const extreme_value_distribution& x, |
| 1083 | const extreme_value_distribution& y); |
| 1084 | |
| 1085 | template <class charT, class traits> |
| 1086 | friend |
| 1087 | basic_ostream<charT, traits>& |
| 1088 | operator<<(basic_ostream<charT, traits>& os, |
| 1089 | const extreme_value_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1090 | |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 1091 | template <class charT, class traits> |
| 1092 | friend |
| 1093 | basic_istream<charT, traits>& |
| 1094 | operator>>(basic_istream<charT, traits>& is, |
| 1095 | extreme_value_distribution& x); |
| 1096 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1097 | |
| 1098 | template<class RealType = double> |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 1099 | class normal_distribution |
| 1100 | { |
| 1101 | public: |
| 1102 | // types |
| 1103 | typedef RealType result_type; |
| 1104 | |
| 1105 | class param_type |
| 1106 | { |
| 1107 | public: |
| 1108 | typedef normal_distribution distribution_type; |
| 1109 | |
| 1110 | explicit param_type(result_type mean = 0, result_type stddev = 1); |
| 1111 | |
| 1112 | result_type mean() const; |
| 1113 | result_type stddev() const; |
| 1114 | |
| 1115 | friend bool operator==(const param_type& x, const param_type& y); |
| 1116 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1117 | }; |
| 1118 | |
| 1119 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 1120 | explicit normal_distribution(RealType mean = 0.0, RealType stddev = 1.0); // before C++20 |
| 1121 | normal_distribution() : normal_distribution(0.0) {} // C++20 |
| 1122 | explicit normal_distribution(RealType mean, RealType stddev = 1.0); // C++20 |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 1123 | explicit normal_distribution(const param_type& parm); |
| 1124 | void reset(); |
| 1125 | |
| 1126 | // generating functions |
| 1127 | template<class URNG> result_type operator()(URNG& g); |
| 1128 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1129 | |
| 1130 | // property functions |
| 1131 | result_type mean() const; |
| 1132 | result_type stddev() const; |
| 1133 | |
| 1134 | param_type param() const; |
| 1135 | void param(const param_type& parm); |
| 1136 | |
| 1137 | result_type min() const; |
| 1138 | result_type max() const; |
| 1139 | |
| 1140 | friend bool operator==(const normal_distribution& x, |
| 1141 | const normal_distribution& y); |
| 1142 | friend bool operator!=(const normal_distribution& x, |
| 1143 | const normal_distribution& y); |
| 1144 | |
| 1145 | template <class charT, class traits> |
| 1146 | friend |
| 1147 | basic_ostream<charT, traits>& |
| 1148 | operator<<(basic_ostream<charT, traits>& os, |
| 1149 | const normal_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1150 | |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 1151 | template <class charT, class traits> |
| 1152 | friend |
| 1153 | basic_istream<charT, traits>& |
| 1154 | operator>>(basic_istream<charT, traits>& is, |
| 1155 | normal_distribution& x); |
| 1156 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1157 | |
| 1158 | template<class RealType = double> |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 1159 | class lognormal_distribution |
| 1160 | { |
| 1161 | public: |
| 1162 | // types |
| 1163 | typedef RealType result_type; |
| 1164 | |
| 1165 | class param_type |
| 1166 | { |
| 1167 | public: |
| 1168 | typedef lognormal_distribution distribution_type; |
| 1169 | |
| 1170 | explicit param_type(result_type m = 0, result_type s = 1); |
| 1171 | |
| 1172 | result_type m() const; |
| 1173 | result_type s() const; |
| 1174 | |
| 1175 | friend bool operator==(const param_type& x, const param_type& y); |
| 1176 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1177 | }; |
| 1178 | |
| 1179 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 1180 | explicit lognormal_distribution(RealType mean = 0.0, RealType stddev = 1.0); // before C++20 |
| 1181 | lognormal_distribution() : lognormal_distribution(0.0) {} // C++20 |
| 1182 | explicit lognormal_distribution(RealType mean, RealType stddev = 1.0); // C++20 |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 1183 | explicit lognormal_distribution(const param_type& parm); |
| 1184 | void reset(); |
| 1185 | |
| 1186 | // generating functions |
| 1187 | template<class URNG> result_type operator()(URNG& g); |
| 1188 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1189 | |
| 1190 | // property functions |
| 1191 | result_type m() const; |
| 1192 | result_type s() const; |
| 1193 | |
| 1194 | param_type param() const; |
| 1195 | void param(const param_type& parm); |
| 1196 | |
| 1197 | result_type min() const; |
| 1198 | result_type max() const; |
| 1199 | |
| 1200 | friend bool operator==(const lognormal_distribution& x, |
| 1201 | const lognormal_distribution& y); |
| 1202 | friend bool operator!=(const lognormal_distribution& x, |
| 1203 | const lognormal_distribution& y); |
| 1204 | |
| 1205 | template <class charT, class traits> |
| 1206 | friend |
| 1207 | basic_ostream<charT, traits>& |
| 1208 | operator<<(basic_ostream<charT, traits>& os, |
| 1209 | const lognormal_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1210 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 1211 | template <class charT, class traits> |
| 1212 | friend |
| 1213 | basic_istream<charT, traits>& |
| 1214 | operator>>(basic_istream<charT, traits>& is, |
| 1215 | lognormal_distribution& x); |
| 1216 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1217 | |
| 1218 | template<class RealType = double> |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1219 | class chi_squared_distribution |
| 1220 | { |
| 1221 | public: |
| 1222 | // types |
| 1223 | typedef RealType result_type; |
| 1224 | |
| 1225 | class param_type |
| 1226 | { |
| 1227 | public: |
| 1228 | typedef chi_squared_distribution distribution_type; |
| 1229 | |
| 1230 | explicit param_type(result_type n = 1); |
| 1231 | |
| 1232 | result_type n() const; |
| 1233 | |
| 1234 | friend bool operator==(const param_type& x, const param_type& y); |
| 1235 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1236 | }; |
| 1237 | |
| 1238 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 1239 | explicit chi_squared_distribution(RealType n = 1.0); // before C++20 |
| 1240 | chi_squared_distribution() : chi_squared_distribution(1.0) {} // C++20 |
| 1241 | explicit chi_squared_distribution(RealType n); // C++20 |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1242 | explicit chi_squared_distribution(const param_type& parm); |
| 1243 | void reset(); |
| 1244 | |
| 1245 | // generating functions |
| 1246 | template<class URNG> result_type operator()(URNG& g); |
| 1247 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1248 | |
| 1249 | // property functions |
| 1250 | result_type n() const; |
| 1251 | |
| 1252 | param_type param() const; |
| 1253 | void param(const param_type& parm); |
| 1254 | |
| 1255 | result_type min() const; |
| 1256 | result_type max() const; |
| 1257 | |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1258 | friend bool operator==(const chi_squared_distribution& x, |
| 1259 | const chi_squared_distribution& y); |
| 1260 | friend bool operator!=(const chi_squared_distribution& x, |
| 1261 | const chi_squared_distribution& y); |
| 1262 | |
| 1263 | template <class charT, class traits> |
| 1264 | friend |
| 1265 | basic_ostream<charT, traits>& |
| 1266 | operator<<(basic_ostream<charT, traits>& os, |
| 1267 | const chi_squared_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1268 | |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1269 | template <class charT, class traits> |
| 1270 | friend |
| 1271 | basic_istream<charT, traits>& |
| 1272 | operator>>(basic_istream<charT, traits>& is, |
| 1273 | chi_squared_distribution& x); |
| 1274 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1275 | |
| 1276 | template<class RealType = double> |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 1277 | class cauchy_distribution |
| 1278 | { |
| 1279 | public: |
| 1280 | // types |
| 1281 | typedef RealType result_type; |
| 1282 | |
| 1283 | class param_type |
| 1284 | { |
| 1285 | public: |
| 1286 | typedef cauchy_distribution distribution_type; |
| 1287 | |
| 1288 | explicit param_type(result_type a = 0, result_type b = 1); |
| 1289 | |
| 1290 | result_type a() const; |
| 1291 | result_type b() const; |
| 1292 | |
| 1293 | friend bool operator==(const param_type& x, const param_type& y); |
| 1294 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1295 | }; |
| 1296 | |
| 1297 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 1298 | explicit cauchy_distribution(RealType a = 0.0, RealType b = 1.0); // before C++20 |
| 1299 | cauchy_distribution() : cauchy_distribution(0.0) {} // C++20 |
| 1300 | explicit cauchy_distribution(RealType a, RealType b = 1.0); // C++20 |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 1301 | explicit cauchy_distribution(const param_type& parm); |
| 1302 | void reset(); |
| 1303 | |
| 1304 | // generating functions |
| 1305 | template<class URNG> result_type operator()(URNG& g); |
| 1306 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1307 | |
| 1308 | // property functions |
| 1309 | result_type a() const; |
| 1310 | result_type b() const; |
| 1311 | |
| 1312 | param_type param() const; |
| 1313 | void param(const param_type& parm); |
| 1314 | |
| 1315 | result_type min() const; |
| 1316 | result_type max() const; |
| 1317 | |
| 1318 | friend bool operator==(const cauchy_distribution& x, |
| 1319 | const cauchy_distribution& y); |
| 1320 | friend bool operator!=(const cauchy_distribution& x, |
| 1321 | const cauchy_distribution& y); |
| 1322 | |
| 1323 | template <class charT, class traits> |
| 1324 | friend |
| 1325 | basic_ostream<charT, traits>& |
| 1326 | operator<<(basic_ostream<charT, traits>& os, |
| 1327 | const cauchy_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1328 | |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 1329 | template <class charT, class traits> |
| 1330 | friend |
| 1331 | basic_istream<charT, traits>& |
| 1332 | operator>>(basic_istream<charT, traits>& is, |
| 1333 | cauchy_distribution& x); |
| 1334 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1335 | |
| 1336 | template<class RealType = double> |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1337 | class fisher_f_distribution |
| 1338 | { |
| 1339 | public: |
| 1340 | // types |
| 1341 | typedef RealType result_type; |
| 1342 | |
| 1343 | class param_type |
| 1344 | { |
| 1345 | public: |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1346 | typedef fisher_f_distribution distribution_type; |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1347 | |
| 1348 | explicit param_type(result_type m = 1, result_type n = 1); |
| 1349 | |
| 1350 | result_type m() const; |
| 1351 | result_type n() const; |
| 1352 | |
| 1353 | friend bool operator==(const param_type& x, const param_type& y); |
| 1354 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1355 | }; |
| 1356 | |
| 1357 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 1358 | explicit fisher_f_distribution(RealType m = 1.0, RealType n = 1.0); // before C++20 |
| 1359 | fisher_f_distribution() : fisher_f_distribution(1.0) {} // C++20 |
| 1360 | explicit fisher_f_distribution(RealType m, RealType n = 1.0); // C++20 |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1361 | explicit fisher_f_distribution(const param_type& parm); |
| 1362 | void reset(); |
| 1363 | |
| 1364 | // generating functions |
| 1365 | template<class URNG> result_type operator()(URNG& g); |
| 1366 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1367 | |
| 1368 | // property functions |
| 1369 | result_type m() const; |
| 1370 | result_type n() const; |
| 1371 | |
| 1372 | param_type param() const; |
| 1373 | void param(const param_type& parm); |
| 1374 | |
| 1375 | result_type min() const; |
| 1376 | result_type max() const; |
| 1377 | |
| 1378 | friend bool operator==(const fisher_f_distribution& x, |
| 1379 | const fisher_f_distribution& y); |
| 1380 | friend bool operator!=(const fisher_f_distribution& x, |
| 1381 | const fisher_f_distribution& y); |
| 1382 | |
| 1383 | template <class charT, class traits> |
| 1384 | friend |
| 1385 | basic_ostream<charT, traits>& |
| 1386 | operator<<(basic_ostream<charT, traits>& os, |
| 1387 | const fisher_f_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1388 | |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1389 | template <class charT, class traits> |
| 1390 | friend |
| 1391 | basic_istream<charT, traits>& |
| 1392 | operator>>(basic_istream<charT, traits>& is, |
| 1393 | fisher_f_distribution& x); |
| 1394 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1395 | |
| 1396 | template<class RealType = double> |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1397 | class student_t_distribution |
| 1398 | { |
| 1399 | public: |
| 1400 | // types |
| 1401 | typedef RealType result_type; |
| 1402 | |
| 1403 | class param_type |
| 1404 | { |
| 1405 | public: |
| 1406 | typedef student_t_distribution distribution_type; |
| 1407 | |
| 1408 | explicit param_type(result_type n = 1); |
| 1409 | |
| 1410 | result_type n() const; |
| 1411 | |
| 1412 | friend bool operator==(const param_type& x, const param_type& y); |
| 1413 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1414 | }; |
| 1415 | |
| 1416 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 1417 | explicit student_t_distribution(RealType n = 1.0); // before C++20 |
| 1418 | student_t_distribution() : student_t_distribution(1.0) {} // C++20 |
| 1419 | explicit student_t_distribution(RealType n); // C++20 |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1420 | explicit student_t_distribution(const param_type& parm); |
| 1421 | void reset(); |
| 1422 | |
| 1423 | // generating functions |
| 1424 | template<class URNG> result_type operator()(URNG& g); |
| 1425 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1426 | |
| 1427 | // property functions |
| 1428 | result_type n() const; |
| 1429 | |
| 1430 | param_type param() const; |
| 1431 | void param(const param_type& parm); |
| 1432 | |
| 1433 | result_type min() const; |
| 1434 | result_type max() const; |
| 1435 | |
| 1436 | friend bool operator==(const student_t_distribution& x, |
| 1437 | const student_t_distribution& y); |
| 1438 | friend bool operator!=(const student_t_distribution& x, |
| 1439 | const student_t_distribution& y); |
| 1440 | |
| 1441 | template <class charT, class traits> |
| 1442 | friend |
| 1443 | basic_ostream<charT, traits>& |
| 1444 | operator<<(basic_ostream<charT, traits>& os, |
| 1445 | const student_t_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1446 | |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1447 | template <class charT, class traits> |
| 1448 | friend |
| 1449 | basic_istream<charT, traits>& |
| 1450 | operator>>(basic_istream<charT, traits>& is, |
| 1451 | student_t_distribution& x); |
| 1452 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1453 | |
| 1454 | template<class IntType = int> |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1455 | class discrete_distribution |
| 1456 | { |
| 1457 | public: |
| 1458 | // types |
| 1459 | typedef IntType result_type; |
| 1460 | |
| 1461 | class param_type |
| 1462 | { |
| 1463 | public: |
| 1464 | typedef discrete_distribution distribution_type; |
| 1465 | |
| 1466 | param_type(); |
| 1467 | template<class InputIterator> |
| 1468 | param_type(InputIterator firstW, InputIterator lastW); |
| 1469 | param_type(initializer_list<double> wl); |
| 1470 | template<class UnaryOperation> |
| 1471 | param_type(size_t nw, double xmin, double xmax, UnaryOperation fw); |
| 1472 | |
| 1473 | vector<double> probabilities() const; |
| 1474 | |
| 1475 | friend bool operator==(const param_type& x, const param_type& y); |
| 1476 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1477 | }; |
| 1478 | |
| 1479 | // constructor and reset functions |
| 1480 | discrete_distribution(); |
| 1481 | template<class InputIterator> |
| 1482 | discrete_distribution(InputIterator firstW, InputIterator lastW); |
| 1483 | discrete_distribution(initializer_list<double> wl); |
| 1484 | template<class UnaryOperation> |
| 1485 | discrete_distribution(size_t nw, double xmin, double xmax, |
| 1486 | UnaryOperation fw); |
| 1487 | explicit discrete_distribution(const param_type& parm); |
| 1488 | void reset(); |
| 1489 | |
| 1490 | // generating functions |
| 1491 | template<class URNG> result_type operator()(URNG& g); |
| 1492 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1493 | |
| 1494 | // property functions |
| 1495 | vector<double> probabilities() const; |
| 1496 | |
| 1497 | param_type param() const; |
| 1498 | void param(const param_type& parm); |
| 1499 | |
| 1500 | result_type min() const; |
| 1501 | result_type max() const; |
| 1502 | |
| 1503 | friend bool operator==(const discrete_distribution& x, |
| 1504 | const discrete_distribution& y); |
| 1505 | friend bool operator!=(const discrete_distribution& x, |
| 1506 | const discrete_distribution& y); |
| 1507 | |
| 1508 | template <class charT, class traits> |
| 1509 | friend |
| 1510 | basic_ostream<charT, traits>& |
| 1511 | operator<<(basic_ostream<charT, traits>& os, |
| 1512 | const discrete_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1513 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1514 | template <class charT, class traits> |
| 1515 | friend |
| 1516 | basic_istream<charT, traits>& |
| 1517 | operator>>(basic_istream<charT, traits>& is, |
| 1518 | discrete_distribution& x); |
| 1519 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1520 | |
| 1521 | template<class RealType = double> |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1522 | class piecewise_constant_distribution |
| 1523 | { |
| 1524 | // types |
| 1525 | typedef RealType result_type; |
| 1526 | |
| 1527 | class param_type |
| 1528 | { |
| 1529 | public: |
| 1530 | typedef piecewise_constant_distribution distribution_type; |
| 1531 | |
| 1532 | param_type(); |
| 1533 | template<class InputIteratorB, class InputIteratorW> |
| 1534 | param_type(InputIteratorB firstB, InputIteratorB lastB, |
| 1535 | InputIteratorW firstW); |
| 1536 | template<class UnaryOperation> |
| 1537 | param_type(initializer_list<result_type> bl, UnaryOperation fw); |
| 1538 | template<class UnaryOperation> |
| 1539 | param_type(size_t nw, result_type xmin, result_type xmax, |
| 1540 | UnaryOperation fw); |
| 1541 | |
| 1542 | vector<result_type> intervals() const; |
Howard Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1543 | vector<result_type> densities() const; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1544 | |
| 1545 | friend bool operator==(const param_type& x, const param_type& y); |
| 1546 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1547 | }; |
| 1548 | |
| 1549 | // constructor and reset functions |
| 1550 | piecewise_constant_distribution(); |
| 1551 | template<class InputIteratorB, class InputIteratorW> |
| 1552 | piecewise_constant_distribution(InputIteratorB firstB, |
| 1553 | InputIteratorB lastB, |
| 1554 | InputIteratorW firstW); |
| 1555 | template<class UnaryOperation> |
| 1556 | piecewise_constant_distribution(initializer_list<result_type> bl, |
| 1557 | UnaryOperation fw); |
| 1558 | template<class UnaryOperation> |
| 1559 | piecewise_constant_distribution(size_t nw, result_type xmin, |
| 1560 | result_type xmax, UnaryOperation fw); |
| 1561 | explicit piecewise_constant_distribution(const param_type& parm); |
| 1562 | void reset(); |
| 1563 | |
| 1564 | // generating functions |
| 1565 | template<class URNG> result_type operator()(URNG& g); |
| 1566 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1567 | |
| 1568 | // property functions |
| 1569 | vector<result_type> intervals() const; |
Howard Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1570 | vector<result_type> densities() const; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1571 | |
| 1572 | param_type param() const; |
| 1573 | void param(const param_type& parm); |
| 1574 | |
| 1575 | result_type min() const; |
| 1576 | result_type max() const; |
| 1577 | |
| 1578 | friend bool operator==(const piecewise_constant_distribution& x, |
| 1579 | const piecewise_constant_distribution& y); |
| 1580 | friend bool operator!=(const piecewise_constant_distribution& x, |
| 1581 | const piecewise_constant_distribution& y); |
| 1582 | |
| 1583 | template <class charT, class traits> |
| 1584 | friend |
| 1585 | basic_ostream<charT, traits>& |
| 1586 | operator<<(basic_ostream<charT, traits>& os, |
| 1587 | const piecewise_constant_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1588 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1589 | template <class charT, class traits> |
| 1590 | friend |
| 1591 | basic_istream<charT, traits>& |
| 1592 | operator>>(basic_istream<charT, traits>& is, |
| 1593 | piecewise_constant_distribution& x); |
| 1594 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1595 | |
| 1596 | template<class RealType = double> |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1597 | class piecewise_linear_distribution |
| 1598 | { |
| 1599 | // types |
| 1600 | typedef RealType result_type; |
| 1601 | |
| 1602 | class param_type |
| 1603 | { |
| 1604 | public: |
| 1605 | typedef piecewise_linear_distribution distribution_type; |
| 1606 | |
| 1607 | param_type(); |
| 1608 | template<class InputIteratorB, class InputIteratorW> |
| 1609 | param_type(InputIteratorB firstB, InputIteratorB lastB, |
| 1610 | InputIteratorW firstW); |
| 1611 | template<class UnaryOperation> |
| 1612 | param_type(initializer_list<result_type> bl, UnaryOperation fw); |
| 1613 | template<class UnaryOperation> |
| 1614 | param_type(size_t nw, result_type xmin, result_type xmax, |
| 1615 | UnaryOperation fw); |
| 1616 | |
| 1617 | vector<result_type> intervals() const; |
Howard Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1618 | vector<result_type> densities() const; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1619 | |
| 1620 | friend bool operator==(const param_type& x, const param_type& y); |
| 1621 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1622 | }; |
| 1623 | |
| 1624 | // constructor and reset functions |
| 1625 | piecewise_linear_distribution(); |
| 1626 | template<class InputIteratorB, class InputIteratorW> |
| 1627 | piecewise_linear_distribution(InputIteratorB firstB, |
| 1628 | InputIteratorB lastB, |
| 1629 | InputIteratorW firstW); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1630 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1631 | template<class UnaryOperation> |
| 1632 | piecewise_linear_distribution(initializer_list<result_type> bl, |
| 1633 | UnaryOperation fw); |
| 1634 | |
| 1635 | template<class UnaryOperation> |
| 1636 | piecewise_linear_distribution(size_t nw, result_type xmin, |
| 1637 | result_type xmax, UnaryOperation fw); |
| 1638 | |
| 1639 | explicit piecewise_linear_distribution(const param_type& parm); |
| 1640 | void reset(); |
| 1641 | |
| 1642 | // generating functions |
| 1643 | template<class URNG> result_type operator()(URNG& g); |
| 1644 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1645 | |
| 1646 | // property functions |
| 1647 | vector<result_type> intervals() const; |
Howard Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1648 | vector<result_type> densities() const; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1649 | |
| 1650 | param_type param() const; |
| 1651 | void param(const param_type& parm); |
| 1652 | |
| 1653 | result_type min() const; |
| 1654 | result_type max() const; |
| 1655 | |
| 1656 | friend bool operator==(const piecewise_linear_distribution& x, |
| 1657 | const piecewise_linear_distribution& y); |
| 1658 | friend bool operator!=(const piecewise_linear_distribution& x, |
| 1659 | const piecewise_linear_distribution& y); |
| 1660 | |
| 1661 | template <class charT, class traits> |
| 1662 | friend |
| 1663 | basic_ostream<charT, traits>& |
| 1664 | operator<<(basic_ostream<charT, traits>& os, |
| 1665 | const piecewise_linear_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1666 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1667 | template <class charT, class traits> |
| 1668 | friend |
| 1669 | basic_istream<charT, traits>& |
| 1670 | operator>>(basic_istream<charT, traits>& is, |
| 1671 | piecewise_linear_distribution& x); |
| 1672 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1673 | |
| 1674 | } // std |
| 1675 | */ |
| 1676 | |
| 1677 | #include <__config> |
| 1678 | #include <cstddef> |
Eric Fiselier | 90adc20 | 2015-01-23 22:22:36 +0000 | [diff] [blame] | 1679 | #include <cstdint> |
| 1680 | #include <cmath> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1681 | #include <type_traits> |
| 1682 | #include <initializer_list> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1683 | #include <limits> |
| 1684 | #include <algorithm> |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1685 | #include <numeric> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1686 | #include <vector> |
| 1687 | #include <string> |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 1688 | #include <iosfwd> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1689 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 1690 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1691 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 1692 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1693 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 1694 | _LIBCPP_PUSH_MACROS |
| 1695 | #include <__undef_macros> |
| 1696 | |
| 1697 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1698 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 1699 | |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1700 | // __is_seed_sequence |
| 1701 | |
| 1702 | template <class _Sseq, class _Engine> |
| 1703 | struct __is_seed_sequence |
| 1704 | { |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1705 | static _LIBCPP_CONSTEXPR const bool value = |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1706 | !is_convertible<_Sseq, typename _Engine::result_type>::value && |
| 1707 | !is_same<typename remove_cv<_Sseq>::type, _Engine>::value; |
| 1708 | }; |
| 1709 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1710 | // linear_congruential_engine |
| 1711 | |
| 1712 | template <unsigned long long __a, unsigned long long __c, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1713 | unsigned long long __m, unsigned long long _Mp, |
zoecarver | 68c3702 | 2020-11-10 18:23:22 -0800 | [diff] [blame] | 1714 | bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_Mp-__c)/__a), |
| 1715 | bool _OverflowOK = ((__m|__m-1) > __m), // m = 2^n |
| 1716 | bool _SchrageOK = (__a != 0 && __m != 0 && __m % __a <= __m / __a)> // r <= q |
| 1717 | struct __lce_alg_picker |
| 1718 | { |
| 1719 | static_assert(__a != 0 || __m != 0 || !_MightOverflow || _OverflowOK || _SchrageOK, |
| 1720 | "The current values of a, c, and m cannot generate a number " |
| 1721 | "within bounds of linear_congruential_engine."); |
| 1722 | |
| 1723 | static _LIBCPP_CONSTEXPR const bool __use_schrage = _MightOverflow && |
| 1724 | !_OverflowOK && |
| 1725 | _SchrageOK; |
| 1726 | }; |
| 1727 | |
| 1728 | template <unsigned long long __a, unsigned long long __c, |
| 1729 | unsigned long long __m, unsigned long long _Mp, |
| 1730 | bool _UseSchrage = __lce_alg_picker<__a, __c, __m, _Mp>::__use_schrage> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1731 | struct __lce_ta; |
| 1732 | |
| 1733 | // 64 |
| 1734 | |
| 1735 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 1736 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true> |
| 1737 | { |
| 1738 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1739 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1740 | static result_type next(result_type __x) |
| 1741 | { |
| 1742 | // Schrage's algorithm |
| 1743 | const result_type __q = __m / __a; |
| 1744 | const result_type __r = __m % __a; |
| 1745 | const result_type __t0 = __a * (__x % __q); |
| 1746 | const result_type __t1 = __r * (__x / __q); |
| 1747 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1748 | __x += __c - (__x >= __m - __c) * __m; |
| 1749 | return __x; |
| 1750 | } |
| 1751 | }; |
| 1752 | |
| 1753 | template <unsigned long long __a, unsigned long long __m> |
| 1754 | struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true> |
| 1755 | { |
| 1756 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1757 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1758 | static result_type next(result_type __x) |
| 1759 | { |
| 1760 | // Schrage's algorithm |
| 1761 | const result_type __q = __m / __a; |
| 1762 | const result_type __r = __m % __a; |
| 1763 | const result_type __t0 = __a * (__x % __q); |
| 1764 | const result_type __t1 = __r * (__x / __q); |
| 1765 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1766 | return __x; |
| 1767 | } |
| 1768 | }; |
| 1769 | |
| 1770 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 1771 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false> |
| 1772 | { |
| 1773 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1774 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1775 | static result_type next(result_type __x) |
| 1776 | { |
| 1777 | return (__a * __x + __c) % __m; |
| 1778 | } |
| 1779 | }; |
| 1780 | |
| 1781 | template <unsigned long long __a, unsigned long long __c> |
| 1782 | struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false> |
| 1783 | { |
| 1784 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1785 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1786 | static result_type next(result_type __x) |
| 1787 | { |
| 1788 | return __a * __x + __c; |
| 1789 | } |
| 1790 | }; |
| 1791 | |
| 1792 | // 32 |
| 1793 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1794 | template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp> |
| 1795 | struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), true> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1796 | { |
| 1797 | typedef unsigned result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1798 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1799 | static result_type next(result_type __x) |
| 1800 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1801 | const result_type __a = static_cast<result_type>(_Ap); |
| 1802 | const result_type __c = static_cast<result_type>(_Cp); |
| 1803 | const result_type __m = static_cast<result_type>(_Mp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1804 | // Schrage's algorithm |
| 1805 | const result_type __q = __m / __a; |
| 1806 | const result_type __r = __m % __a; |
| 1807 | const result_type __t0 = __a * (__x % __q); |
| 1808 | const result_type __t1 = __r * (__x / __q); |
| 1809 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1810 | __x += __c - (__x >= __m - __c) * __m; |
| 1811 | return __x; |
| 1812 | } |
| 1813 | }; |
| 1814 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1815 | template <unsigned long long _Ap, unsigned long long _Mp> |
| 1816 | struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1817 | { |
| 1818 | typedef unsigned result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1819 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1820 | static result_type next(result_type __x) |
| 1821 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1822 | const result_type __a = static_cast<result_type>(_Ap); |
| 1823 | const result_type __m = static_cast<result_type>(_Mp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1824 | // Schrage's algorithm |
| 1825 | const result_type __q = __m / __a; |
| 1826 | const result_type __r = __m % __a; |
| 1827 | const result_type __t0 = __a * (__x % __q); |
| 1828 | const result_type __t1 = __r * (__x / __q); |
| 1829 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1830 | return __x; |
| 1831 | } |
| 1832 | }; |
| 1833 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1834 | template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp> |
| 1835 | struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), false> |
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 __c = static_cast<result_type>(_Cp); |
| 1843 | const result_type __m = static_cast<result_type>(_Mp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1844 | return (__a * __x + __c) % __m; |
| 1845 | } |
| 1846 | }; |
| 1847 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1848 | template <unsigned long long _Ap, unsigned long long _Cp> |
| 1849 | struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1850 | { |
| 1851 | typedef unsigned result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1852 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1853 | static result_type next(result_type __x) |
| 1854 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1855 | const result_type __a = static_cast<result_type>(_Ap); |
| 1856 | const result_type __c = static_cast<result_type>(_Cp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1857 | return __a * __x + __c; |
| 1858 | } |
| 1859 | }; |
| 1860 | |
| 1861 | // 16 |
| 1862 | |
| 1863 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b> |
| 1864 | struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b> |
| 1865 | { |
| 1866 | typedef unsigned short result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1867 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1868 | static result_type next(result_type __x) |
| 1869 | { |
| 1870 | return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x)); |
| 1871 | } |
| 1872 | }; |
| 1873 | |
| 1874 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1875 | class _LIBCPP_TEMPLATE_VIS linear_congruential_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1876 | |
| 1877 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1878 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 1879 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1880 | basic_ostream<_CharT, _Traits>& |
| 1881 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1882 | const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1883 | |
| 1884 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1885 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1886 | basic_istream<_CharT, _Traits>& |
| 1887 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1888 | linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1889 | |
| 1890 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1891 | class _LIBCPP_TEMPLATE_VIS linear_congruential_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1892 | { |
| 1893 | public: |
| 1894 | // types |
| 1895 | typedef _UIntType result_type; |
| 1896 | |
Howard Hinnant | a741b24 | 2013-05-21 21:19:35 +0000 | [diff] [blame] | 1897 | private: |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1898 | result_type __x_; |
| 1899 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1900 | static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1901 | |
| 1902 | static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters"); |
| 1903 | static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters"); |
zoecarver | 68c3702 | 2020-11-10 18:23:22 -0800 | [diff] [blame] | 1904 | static_assert(_VSTD::is_unsigned<_UIntType>::value, "_UIntType must be unsigned type"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1905 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1906 | static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u: 0u; |
| 1907 | static _LIBCPP_CONSTEXPR const result_type _Max = __m - 1u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1908 | static_assert(_Min < _Max, "linear_congruential_engine invalid parameters"); |
| 1909 | |
| 1910 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1911 | static _LIBCPP_CONSTEXPR const result_type multiplier = __a; |
| 1912 | static _LIBCPP_CONSTEXPR const result_type increment = __c; |
| 1913 | static _LIBCPP_CONSTEXPR const result_type modulus = __m; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1914 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1915 | static _LIBCPP_CONSTEXPR result_type min() {return _Min;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1916 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1917 | static _LIBCPP_CONSTEXPR result_type max() {return _Max;} |
| 1918 | static _LIBCPP_CONSTEXPR const result_type default_seed = 1u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1919 | |
| 1920 | // constructors and seeding functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 1921 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1922 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 1923 | linear_congruential_engine() : linear_congruential_engine(default_seed) {} |
| 1924 | _LIBCPP_INLINE_VISIBILITY |
| 1925 | explicit linear_congruential_engine(result_type __s) { seed(__s); } |
| 1926 | #else |
| 1927 | _LIBCPP_INLINE_VISIBILITY |
| 1928 | explicit linear_congruential_engine(result_type __s = default_seed) { |
| 1929 | seed(__s); |
| 1930 | } |
| 1931 | #endif |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1932 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1933 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1934 | explicit linear_congruential_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1935 | 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] | 1936 | {seed(__q);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1937 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1938 | void seed(result_type __s = default_seed) |
| 1939 | {seed(integral_constant<bool, __m == 0>(), |
| 1940 | integral_constant<bool, __c == 0>(), __s);} |
| 1941 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1942 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1943 | typename enable_if |
| 1944 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1945 | __is_seed_sequence<_Sseq, linear_congruential_engine>::value, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1946 | void |
| 1947 | >::type |
| 1948 | seed(_Sseq& __q) |
| 1949 | {__seed(__q, integral_constant<unsigned, |
| 1950 | 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32 |
Howard Hinnant | a0c4f7c | 2013-05-21 21:05:12 +0000 | [diff] [blame] | 1951 | : (__m > 0x100000000ull))>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1952 | |
| 1953 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1954 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1955 | result_type operator()() |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1956 | {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] | 1957 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1958 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 1959 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1960 | friend _LIBCPP_INLINE_VISIBILITY |
| 1961 | bool operator==(const linear_congruential_engine& __x, |
| 1962 | const linear_congruential_engine& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1963 | {return __x.__x_ == __y.__x_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1964 | friend _LIBCPP_INLINE_VISIBILITY |
| 1965 | bool operator!=(const linear_congruential_engine& __x, |
| 1966 | const linear_congruential_engine& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1967 | {return !(__x == __y);} |
| 1968 | |
| 1969 | private: |
| 1970 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1971 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1972 | 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] | 1973 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1974 | void seed(true_type, false_type, result_type __s) {__x_ = __s;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1975 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1976 | void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ? |
| 1977 | 1 : __s % __m;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1978 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1979 | void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;} |
| 1980 | |
| 1981 | template<class _Sseq> |
| 1982 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 1983 | template<class _Sseq> |
| 1984 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 1985 | |
| 1986 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1987 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1988 | friend |
| 1989 | basic_ostream<_CharT, _Traits>& |
| 1990 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1991 | const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1992 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1993 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1994 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1995 | friend |
| 1996 | basic_istream<_CharT, _Traits>& |
| 1997 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1998 | linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1999 | }; |
| 2000 | |
| 2001 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2002 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 2003 | linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier; |
| 2004 | |
| 2005 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 2006 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 2007 | linear_congruential_engine<_UIntType, __a, __c, __m>::increment; |
| 2008 | |
| 2009 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 2010 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 2011 | linear_congruential_engine<_UIntType, __a, __c, __m>::modulus; |
| 2012 | |
| 2013 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 2014 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 2015 | linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed; |
| 2016 | |
| 2017 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2018 | template<class _Sseq> |
| 2019 | void |
| 2020 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 2021 | integral_constant<unsigned, 1>) |
| 2022 | { |
| 2023 | const unsigned __k = 1; |
| 2024 | uint32_t __ar[__k+3]; |
| 2025 | __q.generate(__ar, __ar + __k + 3); |
| 2026 | result_type __s = static_cast<result_type>(__ar[3] % __m); |
| 2027 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 2028 | } |
| 2029 | |
| 2030 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 2031 | template<class _Sseq> |
| 2032 | void |
| 2033 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 2034 | integral_constant<unsigned, 2>) |
| 2035 | { |
| 2036 | const unsigned __k = 2; |
| 2037 | uint32_t __ar[__k+3]; |
| 2038 | __q.generate(__ar, __ar + __k + 3); |
| 2039 | result_type __s = static_cast<result_type>((__ar[3] + |
Howard Hinnant | a0c4f7c | 2013-05-21 21:05:12 +0000 | [diff] [blame] | 2040 | ((uint64_t)__ar[4] << 32)) % __m); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2041 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 2042 | } |
| 2043 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2044 | template <class _CharT, class _Traits, |
| 2045 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2046 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2047 | basic_ostream<_CharT, _Traits>& |
| 2048 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2049 | const linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 2050 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2051 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2052 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
| 2053 | __os.flags(_Ostream::dec | _Ostream::left); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2054 | __os.fill(__os.widen(' ')); |
| 2055 | return __os << __x.__x_; |
| 2056 | } |
| 2057 | |
| 2058 | template <class _CharT, class _Traits, |
| 2059 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 2060 | basic_istream<_CharT, _Traits>& |
| 2061 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2062 | linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 2063 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2064 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2065 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 2066 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2067 | _UIntType __t; |
| 2068 | __is >> __t; |
| 2069 | if (!__is.fail()) |
| 2070 | __x.__x_ = __t; |
| 2071 | return __is; |
| 2072 | } |
| 2073 | |
| 2074 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> |
| 2075 | minstd_rand0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2076 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> |
| 2077 | minstd_rand; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 2078 | typedef minstd_rand default_random_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2079 | // mersenne_twister_engine |
| 2080 | |
| 2081 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2082 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2083 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2084 | class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2085 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2086 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2087 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2088 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2089 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2090 | 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] | 2091 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2092 | 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] | 2093 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2094 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2095 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2096 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2097 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 2098 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2099 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2100 | 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] | 2101 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2102 | 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] | 2103 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2104 | |
| 2105 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2106 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2107 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2108 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2109 | basic_ostream<_CharT, _Traits>& |
| 2110 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
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>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2113 | |
| 2114 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2115 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2116 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2117 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2118 | basic_istream<_CharT, _Traits>& |
| 2119 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2120 | mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2121 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2122 | |
| 2123 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2124 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2125 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2126 | class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2127 | { |
| 2128 | public: |
| 2129 | // types |
| 2130 | typedef _UIntType result_type; |
| 2131 | |
| 2132 | private: |
| 2133 | result_type __x_[__n]; |
| 2134 | size_t __i_; |
| 2135 | |
| 2136 | static_assert( 0 < __m, "mersenne_twister_engine invalid parameters"); |
| 2137 | static_assert(__m <= __n, "mersenne_twister_engine invalid parameters"); |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2138 | static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2139 | static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters"); |
| 2140 | static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters"); |
| 2141 | static_assert(__r <= __w, "mersenne_twister_engine invalid parameters"); |
| 2142 | static_assert(__u <= __w, "mersenne_twister_engine invalid parameters"); |
| 2143 | static_assert(__s <= __w, "mersenne_twister_engine invalid parameters"); |
| 2144 | static_assert(__t <= __w, "mersenne_twister_engine invalid parameters"); |
| 2145 | static_assert(__l <= __w, "mersenne_twister_engine invalid parameters"); |
| 2146 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2147 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 2148 | static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2149 | (result_type(1) << __w) - result_type(1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2150 | static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters"); |
| 2151 | static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2152 | static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2153 | static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2154 | static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2155 | static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2156 | |
| 2157 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2158 | static _LIBCPP_CONSTEXPR const size_t word_size = __w; |
| 2159 | static _LIBCPP_CONSTEXPR const size_t state_size = __n; |
| 2160 | static _LIBCPP_CONSTEXPR const size_t shift_size = __m; |
| 2161 | static _LIBCPP_CONSTEXPR const size_t mask_bits = __r; |
| 2162 | static _LIBCPP_CONSTEXPR const result_type xor_mask = __a; |
| 2163 | static _LIBCPP_CONSTEXPR const size_t tempering_u = __u; |
| 2164 | static _LIBCPP_CONSTEXPR const result_type tempering_d = __d; |
| 2165 | static _LIBCPP_CONSTEXPR const size_t tempering_s = __s; |
| 2166 | static _LIBCPP_CONSTEXPR const result_type tempering_b = __b; |
| 2167 | static _LIBCPP_CONSTEXPR const size_t tempering_t = __t; |
| 2168 | static _LIBCPP_CONSTEXPR const result_type tempering_c = __c; |
| 2169 | static _LIBCPP_CONSTEXPR const size_t tempering_l = __l; |
| 2170 | static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2171 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2172 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2173 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2174 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
| 2175 | static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2176 | |
| 2177 | // constructors and seeding functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 2178 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2179 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 2180 | mersenne_twister_engine() : mersenne_twister_engine(default_seed) {} |
| 2181 | _LIBCPP_INLINE_VISIBILITY |
| 2182 | explicit mersenne_twister_engine(result_type __sd) { seed(__sd); } |
| 2183 | #else |
| 2184 | _LIBCPP_INLINE_VISIBILITY |
| 2185 | explicit mersenne_twister_engine(result_type __sd = default_seed) { |
| 2186 | seed(__sd); |
| 2187 | } |
| 2188 | #endif |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2189 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2190 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2191 | explicit mersenne_twister_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2192 | 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] | 2193 | {seed(__q);} |
| 2194 | void seed(result_type __sd = default_seed); |
| 2195 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2196 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2197 | typename enable_if |
| 2198 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2199 | __is_seed_sequence<_Sseq, mersenne_twister_engine>::value, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2200 | void |
| 2201 | >::type |
| 2202 | seed(_Sseq& __q) |
| 2203 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2204 | |
| 2205 | // generating functions |
| 2206 | result_type operator()(); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2207 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2208 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2209 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2210 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2211 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2212 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2213 | friend |
| 2214 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2215 | 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] | 2216 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2217 | 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] | 2218 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2219 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2220 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2221 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2222 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2223 | friend |
| 2224 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2225 | 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] | 2226 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2227 | 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] | 2228 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2229 | |
| 2230 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2231 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2232 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2233 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2234 | friend |
| 2235 | basic_ostream<_CharT, _Traits>& |
| 2236 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2237 | 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] | 2238 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2239 | |
| 2240 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2241 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2242 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2243 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2244 | friend |
| 2245 | basic_istream<_CharT, _Traits>& |
| 2246 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2247 | mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2248 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2249 | private: |
| 2250 | |
| 2251 | template<class _Sseq> |
| 2252 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 2253 | template<class _Sseq> |
| 2254 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 2255 | |
| 2256 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2257 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2258 | static |
| 2259 | typename enable_if |
| 2260 | < |
| 2261 | __count < __w, |
| 2262 | result_type |
| 2263 | >::type |
| 2264 | __lshift(result_type __x) {return (__x << __count) & _Max;} |
| 2265 | |
| 2266 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2267 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2268 | static |
| 2269 | typename enable_if |
| 2270 | < |
| 2271 | (__count >= __w), |
| 2272 | result_type |
| 2273 | >::type |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2274 | __lshift(result_type) {return result_type(0);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2275 | |
| 2276 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2277 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2278 | static |
| 2279 | typename enable_if |
| 2280 | < |
| 2281 | __count < _Dt, |
| 2282 | result_type |
| 2283 | >::type |
| 2284 | __rshift(result_type __x) {return __x >> __count;} |
| 2285 | |
| 2286 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2287 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2288 | static |
| 2289 | typename enable_if |
| 2290 | < |
| 2291 | (__count >= _Dt), |
| 2292 | result_type |
| 2293 | >::type |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2294 | __rshift(result_type) {return result_type(0);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2295 | }; |
| 2296 | |
| 2297 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2298 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2299 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2300 | _LIBCPP_CONSTEXPR const size_t |
| 2301 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size; |
| 2302 | |
| 2303 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2304 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2305 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2306 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2307 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size; |
| 2308 | |
| 2309 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2310 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2311 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2312 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2313 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size; |
| 2314 | |
| 2315 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2316 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2317 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2318 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2319 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits; |
| 2320 | |
| 2321 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2322 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2323 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2324 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2325 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask; |
| 2326 | |
| 2327 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2328 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2329 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2330 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2331 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u; |
| 2332 | |
| 2333 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2334 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2335 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2336 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2337 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d; |
| 2338 | |
| 2339 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2340 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2341 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2342 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2343 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s; |
| 2344 | |
| 2345 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2346 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2347 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2348 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2349 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b; |
| 2350 | |
| 2351 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2352 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2353 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2354 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2355 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t; |
| 2356 | |
| 2357 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2358 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2359 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2360 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2361 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c; |
| 2362 | |
| 2363 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2364 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2365 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2366 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2367 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l; |
| 2368 | |
| 2369 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2370 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2371 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2372 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2373 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier; |
| 2374 | |
| 2375 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2376 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2377 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2378 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2379 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed; |
| 2380 | |
| 2381 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2382 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2383 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2384 | void |
| 2385 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2386 | __t, __c, __l, __f>::seed(result_type __sd) |
Marshall Clow | c189463 | 2017-09-11 18:10:33 +0000 | [diff] [blame] | 2387 | _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2388 | { // __w >= 2 |
| 2389 | __x_[0] = __sd & _Max; |
| 2390 | for (size_t __i = 1; __i < __n; ++__i) |
| 2391 | __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max; |
| 2392 | __i_ = 0; |
| 2393 | } |
| 2394 | |
| 2395 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2396 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2397 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2398 | template<class _Sseq> |
| 2399 | void |
| 2400 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2401 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>) |
| 2402 | { |
| 2403 | const unsigned __k = 1; |
| 2404 | uint32_t __ar[__n * __k]; |
| 2405 | __q.generate(__ar, __ar + __n * __k); |
| 2406 | for (size_t __i = 0; __i < __n; ++__i) |
| 2407 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 2408 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2409 | (result_type(1) << __r) - result_type(1); |
| 2410 | __i_ = 0; |
| 2411 | if ((__x_[0] & ~__mask) == 0) |
| 2412 | { |
| 2413 | for (size_t __i = 1; __i < __n; ++__i) |
| 2414 | if (__x_[__i] != 0) |
| 2415 | return; |
Hubert Tong | ca5b776 | 2018-08-16 23:56:54 +0000 | [diff] [blame] | 2416 | __x_[0] = result_type(1) << (__w - 1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2421 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2422 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2423 | template<class _Sseq> |
| 2424 | void |
| 2425 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2426 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>) |
| 2427 | { |
| 2428 | const unsigned __k = 2; |
| 2429 | uint32_t __ar[__n * __k]; |
| 2430 | __q.generate(__ar, __ar + __n * __k); |
| 2431 | for (size_t __i = 0; __i < __n; ++__i) |
| 2432 | __x_[__i] = static_cast<result_type>( |
| 2433 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 2434 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2435 | (result_type(1) << __r) - result_type(1); |
| 2436 | __i_ = 0; |
| 2437 | if ((__x_[0] & ~__mask) == 0) |
| 2438 | { |
| 2439 | for (size_t __i = 1; __i < __n; ++__i) |
| 2440 | if (__x_[__i] != 0) |
| 2441 | return; |
Hubert Tong | ca5b776 | 2018-08-16 23:56:54 +0000 | [diff] [blame] | 2442 | __x_[0] = result_type(1) << (__w - 1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2447 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2448 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2449 | _UIntType |
| 2450 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2451 | __t, __c, __l, __f>::operator()() |
| 2452 | { |
| 2453 | const size_t __j = (__i_ + 1) % __n; |
| 2454 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2455 | (result_type(1) << __r) - result_type(1); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2456 | const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2457 | const size_t __k = (__i_ + __m) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2458 | __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2459 | result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d); |
| 2460 | __i_ = __j; |
| 2461 | __z ^= __lshift<__s>(__z) & __b; |
| 2462 | __z ^= __lshift<__t>(__z) & __c; |
| 2463 | return __z ^ __rshift<__l>(__z); |
| 2464 | } |
| 2465 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2466 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2467 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2468 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2469 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2470 | 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] | 2471 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2472 | 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] | 2473 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2474 | { |
| 2475 | if (__x.__i_ == __y.__i_) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2476 | return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2477 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 2478 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2479 | size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2480 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2481 | __y.__x_ + __y.__i_)) |
| 2482 | return false; |
| 2483 | if (__x.__i_ == 0) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2484 | return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_); |
| 2485 | return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2486 | } |
| 2487 | if (__x.__i_ < __y.__i_) |
| 2488 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2489 | size_t __j = _Np - __y.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2490 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2491 | __y.__x_ + __y.__i_)) |
| 2492 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2493 | if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2494 | __y.__x_)) |
| 2495 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2496 | return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2497 | __y.__x_ + (_Np - (__x.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2498 | } |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2499 | size_t __j = _Np - __x.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2500 | if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2501 | __x.__x_ + __x.__i_)) |
| 2502 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2503 | if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2504 | __x.__x_)) |
| 2505 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2506 | return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2507 | __x.__x_ + (_Np - (__y.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2510 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2511 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2512 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2513 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2514 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2515 | 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] | 2516 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2517 | 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] | 2518 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2519 | { |
| 2520 | return !(__x == __y); |
| 2521 | } |
| 2522 | |
| 2523 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2524 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2525 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2526 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2527 | basic_ostream<_CharT, _Traits>& |
| 2528 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2529 | 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] | 2530 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2531 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2532 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2533 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
| 2534 | __os.flags(_Ostream::dec | _Ostream::left); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2535 | _CharT __sp = __os.widen(' '); |
| 2536 | __os.fill(__sp); |
| 2537 | __os << __x.__x_[__x.__i_]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2538 | for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2539 | __os << __sp << __x.__x_[__j]; |
| 2540 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 2541 | __os << __sp << __x.__x_[__j]; |
| 2542 | return __os; |
| 2543 | } |
| 2544 | |
| 2545 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2546 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2547 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2548 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2549 | basic_istream<_CharT, _Traits>& |
| 2550 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2551 | mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2552 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2553 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2554 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2555 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 2556 | __is.flags(_Istream::dec | _Istream::skipws); |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2557 | _UInt __t[_Np]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2558 | for (size_t __i = 0; __i < _Np; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2559 | __is >> __t[__i]; |
| 2560 | if (!__is.fail()) |
| 2561 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2562 | for (size_t __i = 0; __i < _Np; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2563 | __x.__x_[__i] = __t[__i]; |
| 2564 | __x.__i_ = 0; |
| 2565 | } |
| 2566 | return __is; |
| 2567 | } |
| 2568 | |
| 2569 | typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, |
| 2570 | 0x9908b0df, 11, 0xffffffff, |
| 2571 | 7, 0x9d2c5680, |
| 2572 | 15, 0xefc60000, |
| 2573 | 18, 1812433253> mt19937; |
| 2574 | typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31, |
| 2575 | 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL, |
| 2576 | 17, 0x71d67fffeda60000ULL, |
| 2577 | 37, 0xfff7eee000000000ULL, |
| 2578 | 43, 6364136223846793005ULL> mt19937_64; |
| 2579 | |
| 2580 | // subtract_with_carry_engine |
| 2581 | |
| 2582 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2583 | class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2584 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2585 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2586 | bool |
| 2587 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2588 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2589 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2590 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2591 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 2592 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2593 | bool |
| 2594 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2595 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2596 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2597 | |
| 2598 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2599 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2600 | basic_ostream<_CharT, _Traits>& |
| 2601 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2602 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2603 | |
| 2604 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2605 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2606 | basic_istream<_CharT, _Traits>& |
| 2607 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2608 | subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2609 | |
| 2610 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2611 | class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2612 | { |
| 2613 | public: |
| 2614 | // types |
| 2615 | typedef _UIntType result_type; |
| 2616 | |
| 2617 | private: |
| 2618 | result_type __x_[__r]; |
| 2619 | result_type __c_; |
| 2620 | size_t __i_; |
| 2621 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2622 | static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2623 | static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters"); |
| 2624 | static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters"); |
| 2625 | static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters"); |
| 2626 | static_assert(__s < __r, "subtract_with_carry_engine invalid parameters"); |
| 2627 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2628 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 2629 | static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2630 | (result_type(1) << __w) - result_type(1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2631 | static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters"); |
| 2632 | |
| 2633 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2634 | static _LIBCPP_CONSTEXPR const size_t word_size = __w; |
| 2635 | static _LIBCPP_CONSTEXPR const size_t short_lag = __s; |
| 2636 | static _LIBCPP_CONSTEXPR const size_t long_lag = __r; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2637 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2638 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2639 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2640 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
| 2641 | static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2642 | |
| 2643 | // constructors and seeding functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 2644 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2645 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 2646 | subtract_with_carry_engine() : subtract_with_carry_engine(default_seed) {} |
| 2647 | _LIBCPP_INLINE_VISIBILITY |
| 2648 | explicit subtract_with_carry_engine(result_type __sd) { seed(__sd); } |
| 2649 | #else |
| 2650 | _LIBCPP_INLINE_VISIBILITY |
| 2651 | explicit subtract_with_carry_engine(result_type __sd = default_seed) { |
| 2652 | seed(__sd); |
| 2653 | } |
| 2654 | #endif |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2655 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2656 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2657 | explicit subtract_with_carry_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2658 | 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] | 2659 | {seed(__q);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2660 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2661 | void seed(result_type __sd = default_seed) |
| 2662 | {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2663 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2664 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2665 | typename enable_if |
| 2666 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2667 | __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2668 | void |
| 2669 | >::type |
| 2670 | seed(_Sseq& __q) |
| 2671 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2672 | |
| 2673 | // generating functions |
| 2674 | result_type operator()(); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2675 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2676 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2677 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2678 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2679 | friend |
| 2680 | bool |
| 2681 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2682 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2683 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2684 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2685 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2686 | friend |
| 2687 | bool |
| 2688 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2689 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2690 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2691 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2692 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2693 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2694 | friend |
| 2695 | basic_ostream<_CharT, _Traits>& |
| 2696 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2697 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2698 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2699 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2700 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2701 | friend |
| 2702 | basic_istream<_CharT, _Traits>& |
| 2703 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2704 | subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2705 | |
| 2706 | private: |
| 2707 | |
| 2708 | void seed(result_type __sd, integral_constant<unsigned, 1>); |
| 2709 | void seed(result_type __sd, integral_constant<unsigned, 2>); |
| 2710 | template<class _Sseq> |
| 2711 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 2712 | template<class _Sseq> |
| 2713 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 2714 | }; |
| 2715 | |
| 2716 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2717 | _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size; |
| 2718 | |
| 2719 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2720 | _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag; |
| 2721 | |
| 2722 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2723 | _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag; |
| 2724 | |
| 2725 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2726 | _LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type |
| 2727 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed; |
| 2728 | |
| 2729 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2730 | void |
| 2731 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 2732 | integral_constant<unsigned, 1>) |
| 2733 | { |
| 2734 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 2735 | __e(__sd == 0u ? default_seed : __sd); |
| 2736 | for (size_t __i = 0; __i < __r; ++__i) |
| 2737 | __x_[__i] = static_cast<result_type>(__e() & _Max); |
| 2738 | __c_ = __x_[__r-1] == 0; |
| 2739 | __i_ = 0; |
| 2740 | } |
| 2741 | |
| 2742 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2743 | void |
| 2744 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 2745 | integral_constant<unsigned, 2>) |
| 2746 | { |
| 2747 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 2748 | __e(__sd == 0u ? default_seed : __sd); |
| 2749 | for (size_t __i = 0; __i < __r; ++__i) |
Howard Hinnant | 93072c1 | 2011-08-15 17:22:22 +0000 | [diff] [blame] | 2750 | { |
| 2751 | result_type __e0 = __e(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2752 | __x_[__i] = static_cast<result_type>( |
Howard Hinnant | 93072c1 | 2011-08-15 17:22:22 +0000 | [diff] [blame] | 2753 | (__e0 + ((uint64_t)__e() << 32)) & _Max); |
| 2754 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2755 | __c_ = __x_[__r-1] == 0; |
| 2756 | __i_ = 0; |
| 2757 | } |
| 2758 | |
| 2759 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2760 | template<class _Sseq> |
| 2761 | void |
| 2762 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 2763 | integral_constant<unsigned, 1>) |
| 2764 | { |
| 2765 | const unsigned __k = 1; |
| 2766 | uint32_t __ar[__r * __k]; |
| 2767 | __q.generate(__ar, __ar + __r * __k); |
| 2768 | for (size_t __i = 0; __i < __r; ++__i) |
| 2769 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 2770 | __c_ = __x_[__r-1] == 0; |
| 2771 | __i_ = 0; |
| 2772 | } |
| 2773 | |
| 2774 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2775 | template<class _Sseq> |
| 2776 | void |
| 2777 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 2778 | integral_constant<unsigned, 2>) |
| 2779 | { |
| 2780 | const unsigned __k = 2; |
| 2781 | uint32_t __ar[__r * __k]; |
| 2782 | __q.generate(__ar, __ar + __r * __k); |
| 2783 | for (size_t __i = 0; __i < __r; ++__i) |
| 2784 | __x_[__i] = static_cast<result_type>( |
| 2785 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 2786 | __c_ = __x_[__r-1] == 0; |
| 2787 | __i_ = 0; |
| 2788 | } |
| 2789 | |
| 2790 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2791 | _UIntType |
| 2792 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()() |
| 2793 | { |
| 2794 | const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r]; |
| 2795 | result_type& __xr = __x_[__i_]; |
| 2796 | result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1; |
| 2797 | __xr = (__xs - __xr - __c_) & _Max; |
| 2798 | __c_ = __new_c; |
| 2799 | __i_ = (__i_ + 1) % __r; |
| 2800 | return __xr; |
| 2801 | } |
| 2802 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2803 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2804 | bool |
| 2805 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2806 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2807 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2808 | { |
| 2809 | if (__x.__c_ != __y.__c_) |
| 2810 | return false; |
| 2811 | if (__x.__i_ == __y.__i_) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2812 | return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2813 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 2814 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2815 | size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2816 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2817 | __y.__x_ + __y.__i_)) |
| 2818 | return false; |
| 2819 | if (__x.__i_ == 0) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2820 | return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_); |
| 2821 | return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2822 | } |
| 2823 | if (__x.__i_ < __y.__i_) |
| 2824 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2825 | size_t __j = _Rp - __y.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2826 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2827 | __y.__x_ + __y.__i_)) |
| 2828 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2829 | if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2830 | __y.__x_)) |
| 2831 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2832 | return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2833 | __y.__x_ + (_Rp - (__x.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2834 | } |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2835 | size_t __j = _Rp - __x.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2836 | if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2837 | __x.__x_ + __x.__i_)) |
| 2838 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2839 | if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2840 | __x.__x_)) |
| 2841 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2842 | return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2843 | __x.__x_ + (_Rp - (__y.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2846 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2847 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2848 | bool |
| 2849 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2850 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2851 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2852 | { |
| 2853 | return !(__x == __y); |
| 2854 | } |
| 2855 | |
| 2856 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2857 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2858 | basic_ostream<_CharT, _Traits>& |
| 2859 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2860 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2861 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2862 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2863 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
| 2864 | __os.flags(_Ostream::dec | _Ostream::left); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2865 | _CharT __sp = __os.widen(' '); |
| 2866 | __os.fill(__sp); |
| 2867 | __os << __x.__x_[__x.__i_]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2868 | for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2869 | __os << __sp << __x.__x_[__j]; |
| 2870 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 2871 | __os << __sp << __x.__x_[__j]; |
| 2872 | __os << __sp << __x.__c_; |
| 2873 | return __os; |
| 2874 | } |
| 2875 | |
| 2876 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2877 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2878 | basic_istream<_CharT, _Traits>& |
| 2879 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2880 | subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2881 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2882 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 2883 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 2884 | __is.flags(_Istream::dec | _Istream::skipws); |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2885 | _UInt __t[_Rp+1]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2886 | for (size_t __i = 0; __i < _Rp+1; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2887 | __is >> __t[__i]; |
| 2888 | if (!__is.fail()) |
| 2889 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2890 | for (size_t __i = 0; __i < _Rp; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2891 | __x.__x_[__i] = __t[__i]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2892 | __x.__c_ = __t[_Rp]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2893 | __x.__i_ = 0; |
| 2894 | } |
| 2895 | return __is; |
| 2896 | } |
| 2897 | |
| 2898 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base; |
| 2899 | typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base; |
| 2900 | |
| 2901 | // discard_block_engine |
| 2902 | |
| 2903 | template<class _Engine, size_t __p, size_t __r> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2904 | class _LIBCPP_TEMPLATE_VIS discard_block_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2905 | { |
| 2906 | _Engine __e_; |
| 2907 | int __n_; |
| 2908 | |
| 2909 | static_assert( 0 < __r, "discard_block_engine invalid parameters"); |
| 2910 | static_assert(__r <= __p, "discard_block_engine invalid parameters"); |
Eric Fiselier | 37c2215 | 2016-12-24 00:24:44 +0000 | [diff] [blame] | 2911 | static_assert(__r <= INT_MAX, "discard_block_engine invalid parameters"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2912 | public: |
| 2913 | // types |
| 2914 | typedef typename _Engine::result_type result_type; |
| 2915 | |
| 2916 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2917 | static _LIBCPP_CONSTEXPR const size_t block_size = __p; |
| 2918 | static _LIBCPP_CONSTEXPR const size_t used_block = __r; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2919 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 2920 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2921 | static const result_type _Min = _Engine::_Min; |
| 2922 | static const result_type _Max = _Engine::_Max; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2923 | #else |
| 2924 | static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min(); |
| 2925 | static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max(); |
| 2926 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2927 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2928 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2929 | static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2930 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2931 | static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2932 | |
| 2933 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2934 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2935 | discard_block_engine() : __n_(0) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2936 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2937 | explicit discard_block_engine(const _Engine& __e) |
| 2938 | : __e_(__e), __n_(0) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 2939 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2940 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2941 | explicit discard_block_engine(_Engine&& __e) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2942 | : __e_(_VSTD::move(__e)), __n_(0) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 2943 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2944 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2945 | explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2946 | template<class _Sseq> |
| 2947 | _LIBCPP_INLINE_VISIBILITY |
| 2948 | explicit discard_block_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2949 | typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value && |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2950 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2951 | : __e_(__q), __n_(0) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2952 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2953 | void seed() {__e_.seed(); __n_ = 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2954 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2955 | void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;} |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2956 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2957 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2958 | typename enable_if |
| 2959 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2960 | __is_seed_sequence<_Sseq, discard_block_engine>::value, |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2961 | void |
| 2962 | >::type |
| 2963 | seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2964 | |
| 2965 | // generating functions |
| 2966 | result_type operator()(); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2967 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2968 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2969 | |
| 2970 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2971 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 2972 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2973 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2974 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2975 | friend |
| 2976 | bool |
| 2977 | operator==( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2978 | const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 2979 | const discard_block_engine<_Eng, _Pp, _Rp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2980 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2981 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2982 | friend |
| 2983 | bool |
| 2984 | operator!=( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2985 | const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 2986 | const discard_block_engine<_Eng, _Pp, _Rp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2987 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2988 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2989 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2990 | friend |
| 2991 | basic_ostream<_CharT, _Traits>& |
| 2992 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2993 | const discard_block_engine<_Eng, _Pp, _Rp>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2994 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2995 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2996 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2997 | friend |
| 2998 | basic_istream<_CharT, _Traits>& |
| 2999 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3000 | discard_block_engine<_Eng, _Pp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3001 | }; |
| 3002 | |
| 3003 | template<class _Engine, size_t __p, size_t __r> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 3004 | _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size; |
| 3005 | |
| 3006 | template<class _Engine, size_t __p, size_t __r> |
| 3007 | _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block; |
| 3008 | |
| 3009 | template<class _Engine, size_t __p, size_t __r> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3010 | typename discard_block_engine<_Engine, __p, __r>::result_type |
| 3011 | discard_block_engine<_Engine, __p, __r>::operator()() |
| 3012 | { |
Eric Fiselier | 37c2215 | 2016-12-24 00:24:44 +0000 | [diff] [blame] | 3013 | if (__n_ >= static_cast<int>(__r)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3014 | { |
| 3015 | __e_.discard(__p - __r); |
| 3016 | __n_ = 0; |
| 3017 | } |
| 3018 | ++__n_; |
| 3019 | return __e_(); |
| 3020 | } |
| 3021 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3022 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3023 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3024 | bool |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3025 | operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 3026 | const discard_block_engine<_Eng, _Pp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3027 | { |
| 3028 | return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_; |
| 3029 | } |
| 3030 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3031 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3032 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3033 | bool |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3034 | operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 3035 | const discard_block_engine<_Eng, _Pp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3036 | { |
| 3037 | return !(__x == __y); |
| 3038 | } |
| 3039 | |
| 3040 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3041 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3042 | basic_ostream<_CharT, _Traits>& |
| 3043 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3044 | const discard_block_engine<_Eng, _Pp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3045 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3046 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3047 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
| 3048 | __os.flags(_Ostream::dec | _Ostream::left); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3049 | _CharT __sp = __os.widen(' '); |
| 3050 | __os.fill(__sp); |
| 3051 | return __os << __x.__e_ << __sp << __x.__n_; |
| 3052 | } |
| 3053 | |
| 3054 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3055 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3056 | basic_istream<_CharT, _Traits>& |
| 3057 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3058 | discard_block_engine<_Eng, _Pp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3059 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3060 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3061 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 3062 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3063 | _Eng __e; |
| 3064 | int __n; |
| 3065 | __is >> __e >> __n; |
| 3066 | if (!__is.fail()) |
| 3067 | { |
| 3068 | __x.__e_ = __e; |
| 3069 | __x.__n_ = __n; |
| 3070 | } |
| 3071 | return __is; |
| 3072 | } |
| 3073 | |
| 3074 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
| 3075 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
| 3076 | |
| 3077 | // independent_bits_engine |
| 3078 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3079 | template<class _Engine, size_t __w, class _UIntType> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3080 | class _LIBCPP_TEMPLATE_VIS independent_bits_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3081 | { |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3082 | template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3083 | class __get_n |
| 3084 | { |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3085 | static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3086 | static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0); |
| 3087 | static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np; |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3088 | static _LIBCPP_CONSTEXPR const _UInt _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3089 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3090 | 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] | 3091 | }; |
| 3092 | public: |
| 3093 | // types |
| 3094 | typedef _UIntType result_type; |
| 3095 | |
| 3096 | private: |
| 3097 | _Engine __e_; |
| 3098 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3099 | static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3100 | static_assert( 0 < __w, "independent_bits_engine invalid parameters"); |
| 3101 | static_assert(__w <= _Dt, "independent_bits_engine invalid parameters"); |
| 3102 | |
| 3103 | typedef typename _Engine::result_type _Engine_result_type; |
| 3104 | typedef typename conditional |
| 3105 | < |
| 3106 | sizeof(_Engine_result_type) <= sizeof(result_type), |
| 3107 | result_type, |
| 3108 | _Engine_result_type |
| 3109 | >::type _Working_result_type; |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3110 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3111 | static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3112 | + _Working_result_type(1); |
| 3113 | #else |
| 3114 | static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min() |
| 3115 | + _Working_result_type(1); |
| 3116 | #endif |
| 3117 | static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value; |
| 3118 | static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value; |
| 3119 | static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n; |
| 3120 | static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n; |
| 3121 | static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits; |
| 3122 | static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits; |
| 3123 | static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 : |
| 3124 | (_Rp >> __w0) << __w0; |
| 3125 | static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : |
| 3126 | (_Rp >> (__w0+1)) << (__w0+1); |
| 3127 | static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ? |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3128 | _Engine_result_type(~0) >> (_EDt - __w0) : |
| 3129 | _Engine_result_type(0); |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3130 | static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ? |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3131 | _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : |
| 3132 | _Engine_result_type(~0); |
| 3133 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3134 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 3135 | static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) : |
| 3136 | (result_type(1) << __w) - result_type(1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3137 | static_assert(_Min < _Max, "independent_bits_engine invalid parameters"); |
| 3138 | |
| 3139 | // engine characteristics |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3140 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3141 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3142 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3143 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3144 | |
| 3145 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3146 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3147 | independent_bits_engine() {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3148 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3149 | explicit independent_bits_engine(const _Engine& __e) |
| 3150 | : __e_(__e) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3151 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3152 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3153 | explicit independent_bits_engine(_Engine&& __e) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3154 | : __e_(_VSTD::move(__e)) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3155 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3156 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3157 | explicit independent_bits_engine(result_type __sd) : __e_(__sd) {} |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3158 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3159 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3160 | explicit independent_bits_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3161 | typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value && |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3162 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3163 | : __e_(__q) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3164 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3165 | void seed() {__e_.seed();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3166 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3167 | void seed(result_type __sd) {__e_.seed(__sd);} |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3168 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3169 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3170 | typename enable_if |
| 3171 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3172 | __is_seed_sequence<_Sseq, independent_bits_engine>::value, |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3173 | void |
| 3174 | >::type |
| 3175 | seed(_Sseq& __q) {__e_.seed(__q);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3176 | |
| 3177 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3178 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3179 | result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3180 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3181 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 3182 | |
| 3183 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3184 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3185 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3186 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3187 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3188 | friend |
| 3189 | bool |
| 3190 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3191 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x, |
| 3192 | const independent_bits_engine<_Eng, _Wp, _UInt>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3193 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3194 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3195 | friend |
| 3196 | bool |
| 3197 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3198 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x, |
| 3199 | const independent_bits_engine<_Eng, _Wp, _UInt>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3200 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3201 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3202 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3203 | friend |
| 3204 | basic_ostream<_CharT, _Traits>& |
| 3205 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3206 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3207 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3208 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3209 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3210 | friend |
| 3211 | basic_istream<_CharT, _Traits>& |
| 3212 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3213 | independent_bits_engine<_Eng, _Wp, _UInt>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3214 | |
| 3215 | private: |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3216 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | fe77858 | 2017-09-20 19:38:43 +0000 | [diff] [blame] | 3217 | result_type __eval(false_type); |
| 3218 | result_type __eval(true_type); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3219 | |
| 3220 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3221 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3222 | static |
| 3223 | typename enable_if |
| 3224 | < |
| 3225 | __count < _Dt, |
| 3226 | result_type |
| 3227 | >::type |
| 3228 | __lshift(result_type __x) {return __x << __count;} |
| 3229 | |
| 3230 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3231 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3232 | static |
| 3233 | typename enable_if |
| 3234 | < |
| 3235 | (__count >= _Dt), |
| 3236 | result_type |
| 3237 | >::type |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3238 | __lshift(result_type) {return result_type(0);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3239 | }; |
| 3240 | |
| 3241 | template<class _Engine, size_t __w, class _UIntType> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3242 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3243 | _UIntType |
Marshall Clow | fe77858 | 2017-09-20 19:38:43 +0000 | [diff] [blame] | 3244 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3245 | { |
| 3246 | return static_cast<result_type>(__e_() & __mask0); |
| 3247 | } |
| 3248 | |
| 3249 | template<class _Engine, size_t __w, class _UIntType> |
| 3250 | _UIntType |
Marshall Clow | fe77858 | 2017-09-20 19:38:43 +0000 | [diff] [blame] | 3251 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3252 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3253 | result_type _Sp = 0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3254 | for (size_t __k = 0; __k < __n0; ++__k) |
| 3255 | { |
| 3256 | _Engine_result_type __u; |
| 3257 | do |
| 3258 | { |
| 3259 | __u = __e_() - _Engine::min(); |
| 3260 | } while (__u >= __y0); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3261 | _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3262 | } |
| 3263 | for (size_t __k = __n0; __k < __n; ++__k) |
| 3264 | { |
| 3265 | _Engine_result_type __u; |
| 3266 | do |
| 3267 | { |
| 3268 | __u = __e_() - _Engine::min(); |
| 3269 | } while (__u >= __y1); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3270 | _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1)); |
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 | return _Sp; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3275 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3276 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3277 | bool |
| 3278 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3279 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x, |
| 3280 | const independent_bits_engine<_Eng, _Wp, _UInt>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3281 | { |
| 3282 | return __x.base() == __y.base(); |
| 3283 | } |
| 3284 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3285 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3286 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3287 | bool |
| 3288 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3289 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x, |
| 3290 | const independent_bits_engine<_Eng, _Wp, _UInt>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3291 | { |
| 3292 | return !(__x == __y); |
| 3293 | } |
| 3294 | |
| 3295 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3296 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3297 | basic_ostream<_CharT, _Traits>& |
| 3298 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3299 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3300 | { |
| 3301 | return __os << __x.base(); |
| 3302 | } |
| 3303 | |
| 3304 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3305 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3306 | basic_istream<_CharT, _Traits>& |
| 3307 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3308 | independent_bits_engine<_Eng, _Wp, _UInt>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3309 | { |
| 3310 | _Eng __e; |
| 3311 | __is >> __e; |
| 3312 | if (!__is.fail()) |
| 3313 | __x.__e_ = __e; |
| 3314 | return __is; |
| 3315 | } |
| 3316 | |
| 3317 | // shuffle_order_engine |
| 3318 | |
| 3319 | template <uint64_t _Xp, uint64_t _Yp> |
| 3320 | struct __ugcd |
| 3321 | { |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3322 | static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3323 | }; |
| 3324 | |
| 3325 | template <uint64_t _Xp> |
| 3326 | struct __ugcd<_Xp, 0> |
| 3327 | { |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3328 | static _LIBCPP_CONSTEXPR const uint64_t value = _Xp; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3329 | }; |
| 3330 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3331 | template <uint64_t _Np, uint64_t _Dp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3332 | class __uratio |
| 3333 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3334 | static_assert(_Dp != 0, "__uratio divide by 0"); |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3335 | static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3336 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3337 | static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd; |
| 3338 | static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3339 | |
| 3340 | typedef __uratio<num, den> type; |
| 3341 | }; |
| 3342 | |
| 3343 | template<class _Engine, size_t __k> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3344 | class _LIBCPP_TEMPLATE_VIS shuffle_order_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3345 | { |
| 3346 | static_assert(0 < __k, "shuffle_order_engine invalid parameters"); |
| 3347 | public: |
| 3348 | // types |
| 3349 | typedef typename _Engine::result_type result_type; |
| 3350 | |
| 3351 | private: |
| 3352 | _Engine __e_; |
| 3353 | result_type _V_[__k]; |
| 3354 | result_type _Y_; |
| 3355 | |
| 3356 | public: |
| 3357 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3358 | static _LIBCPP_CONSTEXPR const size_t table_size = __k; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3359 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3360 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3361 | static const result_type _Min = _Engine::_Min; |
| 3362 | static const result_type _Max = _Engine::_Max; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3363 | #else |
| 3364 | static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min(); |
| 3365 | static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max(); |
| 3366 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3367 | static_assert(_Min < _Max, "shuffle_order_engine invalid parameters"); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3368 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3369 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3370 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3371 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3372 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3373 | static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3374 | |
| 3375 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3376 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3377 | shuffle_order_engine() {__init();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3378 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3379 | explicit shuffle_order_engine(const _Engine& __e) |
| 3380 | : __e_(__e) {__init();} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3381 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3382 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3383 | explicit shuffle_order_engine(_Engine&& __e) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3384 | : __e_(_VSTD::move(__e)) {__init();} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3385 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3386 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3387 | explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();} |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3388 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3389 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3390 | explicit shuffle_order_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3391 | typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value && |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3392 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3393 | : __e_(__q) {__init();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3394 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3395 | void seed() {__e_.seed(); __init();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3396 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3397 | void seed(result_type __sd) {__e_.seed(__sd); __init();} |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3398 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3399 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3400 | typename enable_if |
| 3401 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3402 | __is_seed_sequence<_Sseq, shuffle_order_engine>::value, |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3403 | void |
| 3404 | >::type |
| 3405 | seed(_Sseq& __q) {__e_.seed(__q); __init();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3406 | |
| 3407 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3408 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3409 | result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3410 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3411 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 3412 | |
| 3413 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3414 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3415 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3416 | |
| 3417 | private: |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3418 | template<class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3419 | friend |
| 3420 | bool |
| 3421 | operator==( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3422 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3423 | const shuffle_order_engine<_Eng, _Kp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3424 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3425 | template<class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3426 | friend |
| 3427 | bool |
| 3428 | operator!=( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3429 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3430 | const shuffle_order_engine<_Eng, _Kp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3431 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3432 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3433 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3434 | friend |
| 3435 | basic_ostream<_CharT, _Traits>& |
| 3436 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3437 | const shuffle_order_engine<_Eng, _Kp>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3438 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3439 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3440 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3441 | friend |
| 3442 | basic_istream<_CharT, _Traits>& |
| 3443 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3444 | shuffle_order_engine<_Eng, _Kp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3445 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3446 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3447 | void __init() |
| 3448 | { |
| 3449 | for (size_t __i = 0; __i < __k; ++__i) |
| 3450 | _V_[__i] = __e_(); |
| 3451 | _Y_ = __e_(); |
| 3452 | } |
| 3453 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3454 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3455 | result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3456 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3457 | result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3458 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3459 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3460 | result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3461 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3462 | result_type __eval2(true_type) {return __evalf<__k, 0>();} |
| 3463 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3464 | template <uint64_t _Np, uint64_t _Dp> |
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 | typename enable_if |
| 3467 | < |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3468 | (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3469 | result_type |
| 3470 | >::type |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3471 | __eval(__uratio<_Np, _Dp>) |
| 3472 | {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3473 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3474 | template <uint64_t _Np, uint64_t _Dp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3475 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3476 | typename enable_if |
| 3477 | < |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3478 | __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3479 | result_type |
| 3480 | >::type |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3481 | __eval(__uratio<_Np, _Dp>) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3482 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3483 | const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min) |
| 3484 | / __uratio<_Np, _Dp>::den); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3485 | _Y_ = _V_[__j]; |
| 3486 | _V_[__j] = __e_(); |
| 3487 | return _Y_; |
| 3488 | } |
| 3489 | |
| 3490 | template <uint64_t __n, uint64_t __d> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3491 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3492 | result_type __evalf() |
| 3493 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3494 | const double _Fp = __d == 0 ? |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3495 | __n / (2. * 0x8000000000000000ull) : |
| 3496 | __n / (double)__d; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3497 | const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3498 | _Y_ = _V_[__j]; |
| 3499 | _V_[__j] = __e_(); |
| 3500 | return _Y_; |
| 3501 | } |
| 3502 | }; |
| 3503 | |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 3504 | template<class _Engine, size_t __k> |
| 3505 | _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size; |
| 3506 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3507 | template<class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3508 | bool |
| 3509 | operator==( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3510 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3511 | const shuffle_order_engine<_Eng, _Kp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3512 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3513 | 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] | 3514 | __x.__e_ == __y.__e_; |
| 3515 | } |
| 3516 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3517 | template<class _Eng, size_t _Kp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3518 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3519 | bool |
| 3520 | operator!=( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3521 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3522 | const shuffle_order_engine<_Eng, _Kp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3523 | { |
| 3524 | return !(__x == __y); |
| 3525 | } |
| 3526 | |
| 3527 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3528 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3529 | basic_ostream<_CharT, _Traits>& |
| 3530 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3531 | const shuffle_order_engine<_Eng, _Kp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3532 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3533 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3534 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
| 3535 | __os.flags(_Ostream::dec | _Ostream::left); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3536 | _CharT __sp = __os.widen(' '); |
| 3537 | __os.fill(__sp); |
| 3538 | __os << __x.__e_ << __sp << __x._V_[0]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3539 | for (size_t __i = 1; __i < _Kp; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3540 | __os << __sp << __x._V_[__i]; |
| 3541 | return __os << __sp << __x._Y_; |
| 3542 | } |
| 3543 | |
| 3544 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3545 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3546 | basic_istream<_CharT, _Traits>& |
| 3547 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3548 | shuffle_order_engine<_Eng, _Kp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3549 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3550 | typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3551 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3552 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 3553 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3554 | _Eng __e; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3555 | result_type _Vp[_Kp+1]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3556 | __is >> __e; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3557 | for (size_t __i = 0; __i < _Kp+1; ++__i) |
| 3558 | __is >> _Vp[__i]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3559 | if (!__is.fail()) |
| 3560 | { |
| 3561 | __x.__e_ = __e; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3562 | for (size_t __i = 0; __i < _Kp; ++__i) |
| 3563 | __x._V_[__i] = _Vp[__i]; |
| 3564 | __x._Y_ = _Vp[_Kp]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3565 | } |
| 3566 | return __is; |
| 3567 | } |
| 3568 | |
| 3569 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
| 3570 | |
| 3571 | // random_device |
| 3572 | |
Louis Dionne | 3777e68 | 2020-10-15 10:32:09 -0400 | [diff] [blame] | 3573 | #if !defined(_LIBCPP_HAS_NO_RANDOM_DEVICE) |
| 3574 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 3575 | class _LIBCPP_TYPE_VIS random_device |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3576 | { |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 3577 | #ifdef _LIBCPP_USING_DEV_RANDOM |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3578 | int __f_; |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 3579 | #endif // defined(_LIBCPP_USING_DEV_RANDOM) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3580 | public: |
| 3581 | // types |
| 3582 | typedef unsigned result_type; |
| 3583 | |
| 3584 | // generator characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3585 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 3586 | static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3587 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3588 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 664183b | 2012-04-02 00:40:41 +0000 | [diff] [blame] | 3589 | static _LIBCPP_CONSTEXPR result_type min() { return _Min;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3590 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 664183b | 2012-04-02 00:40:41 +0000 | [diff] [blame] | 3591 | static _LIBCPP_CONSTEXPR result_type max() { return _Max;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3592 | |
| 3593 | // constructors |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 3594 | #ifndef _LIBCPP_CXX03_LANG |
| 3595 | random_device() : random_device("/dev/urandom") {} |
| 3596 | explicit random_device(const string& __token); |
| 3597 | #else |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3598 | explicit random_device(const string& __token = "/dev/urandom"); |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 3599 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3600 | ~random_device(); |
| 3601 | |
| 3602 | // generating functions |
| 3603 | result_type operator()(); |
| 3604 | |
| 3605 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3606 | double entropy() const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3607 | |
| 3608 | private: |
| 3609 | // no copy functions |
| 3610 | random_device(const random_device&); // = delete; |
| 3611 | random_device& operator=(const random_device&); // = delete; |
| 3612 | }; |
| 3613 | |
Louis Dionne | 3777e68 | 2020-10-15 10:32:09 -0400 | [diff] [blame] | 3614 | #endif // !_LIBCPP_HAS_NO_RANDOM_DEVICE |
| 3615 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3616 | // seed_seq |
| 3617 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3618 | class _LIBCPP_TEMPLATE_VIS seed_seq |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3619 | { |
| 3620 | public: |
| 3621 | // types |
| 3622 | typedef uint32_t result_type; |
| 3623 | |
| 3624 | private: |
| 3625 | vector<result_type> __v_; |
| 3626 | |
| 3627 | template<class _InputIterator> |
| 3628 | void init(_InputIterator __first, _InputIterator __last); |
| 3629 | public: |
| 3630 | // constructors |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3631 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 4b6b809 | 2013-10-23 05:56:47 +0000 | [diff] [blame] | 3632 | seed_seq() _NOEXCEPT {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3633 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3634 | template<class _Tp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3635 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3636 | seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3637 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3638 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3639 | template<class _InputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3640 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3641 | seed_seq(_InputIterator __first, _InputIterator __last) |
| 3642 | {init(__first, __last);} |
| 3643 | |
| 3644 | // generating functions |
| 3645 | template<class _RandomAccessIterator> |
| 3646 | void generate(_RandomAccessIterator __first, _RandomAccessIterator __last); |
| 3647 | |
| 3648 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3649 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 4b6b809 | 2013-10-23 05:56:47 +0000 | [diff] [blame] | 3650 | size_t size() const _NOEXCEPT {return __v_.size();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3651 | template<class _OutputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3652 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3653 | void param(_OutputIterator __dest) const |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3654 | {_VSTD::copy(__v_.begin(), __v_.end(), __dest);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3655 | |
| 3656 | private: |
| 3657 | // no copy functions |
| 3658 | seed_seq(const seed_seq&); // = delete; |
| 3659 | void operator=(const seed_seq&); // = delete; |
| 3660 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3661 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3662 | static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3663 | }; |
| 3664 | |
| 3665 | template<class _InputIterator> |
| 3666 | void |
| 3667 | seed_seq::init(_InputIterator __first, _InputIterator __last) |
| 3668 | { |
| 3669 | for (_InputIterator __s = __first; __s != __last; ++__s) |
| 3670 | __v_.push_back(*__s & 0xFFFFFFFF); |
| 3671 | } |
| 3672 | |
| 3673 | template<class _RandomAccessIterator> |
| 3674 | void |
| 3675 | seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last) |
| 3676 | { |
| 3677 | if (__first != __last) |
| 3678 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3679 | _VSTD::fill(__first, __last, 0x8b8b8b8b); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3680 | const size_t __n = static_cast<size_t>(__last - __first); |
| 3681 | const size_t __s = __v_.size(); |
| 3682 | const size_t __t = (__n >= 623) ? 11 |
| 3683 | : (__n >= 68) ? 7 |
| 3684 | : (__n >= 39) ? 5 |
| 3685 | : (__n >= 7) ? 3 |
| 3686 | : (__n - 1) / 2; |
| 3687 | const size_t __p = (__n - __t) / 2; |
| 3688 | const size_t __q = __p + __t; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3689 | const size_t __m = _VSTD::max(__s + 1, __n); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3690 | // __k = 0; |
| 3691 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3692 | result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p] |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3693 | ^ __first[__n - 1]); |
| 3694 | __first[__p] += __r; |
| 3695 | __r += __s; |
| 3696 | __first[__q] += __r; |
| 3697 | __first[0] = __r; |
| 3698 | } |
| 3699 | for (size_t __k = 1; __k <= __s; ++__k) |
| 3700 | { |
| 3701 | const size_t __kmodn = __k % __n; |
| 3702 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3703 | result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3704 | ^ __first[(__k - 1) % __n]); |
| 3705 | __first[__kpmodn] += __r; |
| 3706 | __r += __kmodn + __v_[__k-1]; |
| 3707 | __first[(__k + __q) % __n] += __r; |
| 3708 | __first[__kmodn] = __r; |
| 3709 | } |
| 3710 | for (size_t __k = __s + 1; __k < __m; ++__k) |
| 3711 | { |
| 3712 | const size_t __kmodn = __k % __n; |
| 3713 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3714 | result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3715 | ^ __first[(__k - 1) % __n]); |
| 3716 | __first[__kpmodn] += __r; |
| 3717 | __r += __kmodn; |
| 3718 | __first[(__k + __q) % __n] += __r; |
| 3719 | __first[__kmodn] = __r; |
| 3720 | } |
| 3721 | for (size_t __k = __m; __k < __m + __n; ++__k) |
| 3722 | { |
| 3723 | const size_t __kmodn = __k % __n; |
| 3724 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3725 | result_type __r = 1566083941 * _Tp(__first[__kmodn] + |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3726 | __first[__kpmodn] + |
| 3727 | __first[(__k - 1) % __n]); |
| 3728 | __first[__kpmodn] ^= __r; |
| 3729 | __r -= __kmodn; |
| 3730 | __first[(__k + __q) % __n] ^= __r; |
| 3731 | __first[__kmodn] = __r; |
| 3732 | } |
| 3733 | } |
| 3734 | } |
| 3735 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3736 | // generate_canonical |
| 3737 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3738 | template<class _RealType, size_t __bits, class _URNG> |
| 3739 | _RealType |
| 3740 | generate_canonical(_URNG& __g) |
| 3741 | { |
| 3742 | const size_t _Dt = numeric_limits<_RealType>::digits; |
| 3743 | const size_t __b = _Dt < __bits ? _Dt : __bits; |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3744 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3745 | 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] | 3746 | #else |
| 3747 | const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value; |
| 3748 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3749 | const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0); |
Louis Dionne | 2422bdb | 2019-08-20 15:39:20 +0000 | [diff] [blame] | 3750 | const _RealType _Rp = static_cast<_RealType>(_URNG::max() - _URNG::min()) + _RealType(1); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3751 | _RealType __base = _Rp; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3752 | _RealType _Sp = __g() - _URNG::min(); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3753 | for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp) |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3754 | _Sp += (__g() - _URNG::min()) * __base; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3755 | return _Sp / __base; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3756 | } |
| 3757 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3758 | // uniform_int_distribution |
| 3759 | |
Howard Hinnant | 578ac0f | 2010-05-26 17:49:34 +0000 | [diff] [blame] | 3760 | // in <algorithm> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3761 | |
| 3762 | template <class _CharT, class _Traits, class _IT> |
| 3763 | basic_ostream<_CharT, _Traits>& |
| 3764 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3765 | const uniform_int_distribution<_IT>& __x) |
| 3766 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3767 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3768 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
| 3769 | __os.flags(_Ostream::dec | _Ostream::left); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3770 | _CharT __sp = __os.widen(' '); |
| 3771 | __os.fill(__sp); |
| 3772 | return __os << __x.a() << __sp << __x.b(); |
| 3773 | } |
| 3774 | |
| 3775 | template <class _CharT, class _Traits, class _IT> |
| 3776 | basic_istream<_CharT, _Traits>& |
| 3777 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3778 | uniform_int_distribution<_IT>& __x) |
| 3779 | { |
| 3780 | typedef uniform_int_distribution<_IT> _Eng; |
| 3781 | typedef typename _Eng::result_type result_type; |
| 3782 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3783 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3784 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 3785 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3786 | result_type __a; |
| 3787 | result_type __b; |
| 3788 | __is >> __a >> __b; |
| 3789 | if (!__is.fail()) |
| 3790 | __x.param(param_type(__a, __b)); |
| 3791 | return __is; |
| 3792 | } |
| 3793 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3794 | // uniform_real_distribution |
| 3795 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3796 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3797 | class _LIBCPP_TEMPLATE_VIS uniform_real_distribution |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3798 | { |
| 3799 | public: |
| 3800 | // types |
| 3801 | typedef _RealType result_type; |
| 3802 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3803 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3804 | { |
| 3805 | result_type __a_; |
| 3806 | result_type __b_; |
| 3807 | public: |
| 3808 | typedef uniform_real_distribution distribution_type; |
| 3809 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3810 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3811 | explicit param_type(result_type __a = 0, |
| 3812 | result_type __b = 1) |
| 3813 | : __a_(__a), __b_(__b) {} |
| 3814 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3815 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3816 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3817 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3818 | result_type b() const {return __b_;} |
| 3819 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3820 | friend _LIBCPP_INLINE_VISIBILITY |
| 3821 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3822 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3823 | friend _LIBCPP_INLINE_VISIBILITY |
| 3824 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3825 | {return !(__x == __y);} |
| 3826 | }; |
| 3827 | |
| 3828 | private: |
| 3829 | param_type __p_; |
| 3830 | |
| 3831 | public: |
| 3832 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 3833 | #ifndef _LIBCPP_CXX03_LANG |
| 3834 | _LIBCPP_INLINE_VISIBILITY |
| 3835 | uniform_real_distribution() : uniform_real_distribution(0) {} |
| 3836 | explicit uniform_real_distribution(result_type __a, result_type __b = 1) |
| 3837 | : __p_(param_type(__a, __b)) {} |
| 3838 | #else |
Howard 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 | explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1) |
| 3841 | : __p_(param_type(__a, __b)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 3842 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3843 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3844 | explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3845 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3846 | void reset() {} |
| 3847 | |
| 3848 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3849 | template<class _URNG> |
| 3850 | _LIBCPP_INLINE_VISIBILITY |
| 3851 | result_type operator()(_URNG& __g) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3852 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3853 | 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] | 3854 | |
| 3855 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3856 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3857 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3858 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3859 | result_type b() const {return __p_.b();} |
| 3860 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3861 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3862 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3863 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3864 | void param(const param_type& __p) {__p_ = __p;} |
| 3865 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3866 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3867 | result_type min() const {return a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3868 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3869 | result_type max() const {return b();} |
| 3870 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3871 | friend _LIBCPP_INLINE_VISIBILITY |
| 3872 | bool operator==(const uniform_real_distribution& __x, |
| 3873 | const uniform_real_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3874 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3875 | friend _LIBCPP_INLINE_VISIBILITY |
| 3876 | bool operator!=(const uniform_real_distribution& __x, |
| 3877 | const uniform_real_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3878 | {return !(__x == __y);} |
| 3879 | }; |
| 3880 | |
| 3881 | template<class _RealType> |
| 3882 | template<class _URNG> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3883 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3884 | typename uniform_real_distribution<_RealType>::result_type |
| 3885 | uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3886 | { |
| 3887 | return (__p.b() - __p.a()) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3888 | * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3889 | + __p.a(); |
| 3890 | } |
| 3891 | |
| 3892 | template <class _CharT, class _Traits, class _RT> |
| 3893 | basic_ostream<_CharT, _Traits>& |
| 3894 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3895 | const uniform_real_distribution<_RT>& __x) |
| 3896 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3897 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3898 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 3899 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 3900 | _OStream::scientific); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3901 | _CharT __sp = __os.widen(' '); |
| 3902 | __os.fill(__sp); |
| 3903 | return __os << __x.a() << __sp << __x.b(); |
| 3904 | } |
| 3905 | |
| 3906 | template <class _CharT, class _Traits, class _RT> |
| 3907 | basic_istream<_CharT, _Traits>& |
| 3908 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3909 | uniform_real_distribution<_RT>& __x) |
| 3910 | { |
| 3911 | typedef uniform_real_distribution<_RT> _Eng; |
| 3912 | typedef typename _Eng::result_type result_type; |
| 3913 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3914 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 3915 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 3916 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3917 | result_type __a; |
| 3918 | result_type __b; |
| 3919 | __is >> __a >> __b; |
| 3920 | if (!__is.fail()) |
| 3921 | __x.param(param_type(__a, __b)); |
| 3922 | return __is; |
| 3923 | } |
| 3924 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3925 | // bernoulli_distribution |
| 3926 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3927 | class _LIBCPP_TEMPLATE_VIS bernoulli_distribution |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3928 | { |
| 3929 | public: |
| 3930 | // types |
| 3931 | typedef bool result_type; |
| 3932 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3933 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3934 | { |
| 3935 | double __p_; |
| 3936 | public: |
| 3937 | typedef bernoulli_distribution distribution_type; |
| 3938 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3939 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3940 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 3941 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3942 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3943 | double p() const {return __p_;} |
| 3944 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3945 | friend _LIBCPP_INLINE_VISIBILITY |
| 3946 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3947 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3948 | friend _LIBCPP_INLINE_VISIBILITY |
| 3949 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3950 | {return !(__x == __y);} |
| 3951 | }; |
| 3952 | |
| 3953 | private: |
| 3954 | param_type __p_; |
| 3955 | |
| 3956 | public: |
| 3957 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 3958 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3959 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 3960 | bernoulli_distribution() : bernoulli_distribution(0.5) {} |
| 3961 | _LIBCPP_INLINE_VISIBILITY |
| 3962 | explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {} |
| 3963 | #else |
| 3964 | _LIBCPP_INLINE_VISIBILITY |
| 3965 | explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {} |
| 3966 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3967 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3968 | explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3969 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3970 | void reset() {} |
| 3971 | |
| 3972 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3973 | template<class _URNG> |
| 3974 | _LIBCPP_INLINE_VISIBILITY |
| 3975 | result_type operator()(_URNG& __g) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3976 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3977 | 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] | 3978 | |
| 3979 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3980 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3981 | double p() const {return __p_.p();} |
| 3982 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3983 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3984 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3985 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3986 | void param(const param_type& __p) {__p_ = __p;} |
| 3987 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3988 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3989 | result_type min() const {return false;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3990 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3991 | result_type max() const {return true;} |
| 3992 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3993 | friend _LIBCPP_INLINE_VISIBILITY |
| 3994 | bool operator==(const bernoulli_distribution& __x, |
| 3995 | const bernoulli_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3996 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3997 | friend _LIBCPP_INLINE_VISIBILITY |
| 3998 | bool operator!=(const bernoulli_distribution& __x, |
| 3999 | const bernoulli_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4000 | {return !(__x == __y);} |
| 4001 | }; |
| 4002 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4003 | template<class _URNG> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 4004 | inline |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4005 | bernoulli_distribution::result_type |
| 4006 | bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) |
| 4007 | { |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 4008 | uniform_real_distribution<double> __gen; |
| 4009 | return __gen(__g) < __p.p(); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4012 | template <class _CharT, class _Traits> |
| 4013 | basic_ostream<_CharT, _Traits>& |
| 4014 | operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) |
| 4015 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4016 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4017 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 4018 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 4019 | _OStream::scientific); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4020 | _CharT __sp = __os.widen(' '); |
| 4021 | __os.fill(__sp); |
| 4022 | return __os << __x.p(); |
| 4023 | } |
| 4024 | |
| 4025 | template <class _CharT, class _Traits> |
| 4026 | basic_istream<_CharT, _Traits>& |
| 4027 | operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) |
| 4028 | { |
| 4029 | typedef bernoulli_distribution _Eng; |
| 4030 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4031 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4032 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 4033 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4034 | double __p; |
| 4035 | __is >> __p; |
| 4036 | if (!__is.fail()) |
| 4037 | __x.param(param_type(__p)); |
| 4038 | return __is; |
| 4039 | } |
| 4040 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4041 | // binomial_distribution |
| 4042 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4043 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4044 | class _LIBCPP_TEMPLATE_VIS binomial_distribution |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4045 | { |
| 4046 | public: |
| 4047 | // types |
| 4048 | typedef _IntType result_type; |
| 4049 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4050 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4051 | { |
| 4052 | result_type __t_; |
| 4053 | double __p_; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4054 | double __pr_; |
| 4055 | double __odds_ratio_; |
| 4056 | result_type __r0_; |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4057 | public: |
| 4058 | typedef binomial_distribution distribution_type; |
| 4059 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4060 | explicit param_type(result_type __t = 1, double __p = 0.5); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4061 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4062 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4063 | result_type t() const {return __t_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4064 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4065 | double p() const {return __p_;} |
| 4066 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4067 | friend _LIBCPP_INLINE_VISIBILITY |
| 4068 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4069 | {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4070 | friend _LIBCPP_INLINE_VISIBILITY |
| 4071 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4072 | {return !(__x == __y);} |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4073 | |
| 4074 | friend class binomial_distribution; |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4075 | }; |
| 4076 | |
| 4077 | private: |
| 4078 | param_type __p_; |
| 4079 | |
| 4080 | public: |
| 4081 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4082 | #ifndef _LIBCPP_CXX03_LANG |
| 4083 | _LIBCPP_INLINE_VISIBILITY |
| 4084 | binomial_distribution() : binomial_distribution(1) {} |
| 4085 | _LIBCPP_INLINE_VISIBILITY |
| 4086 | explicit binomial_distribution(result_type __t, double __p = 0.5) |
| 4087 | : __p_(param_type(__t, __p)) {} |
| 4088 | #else |
Howard 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 | explicit binomial_distribution(result_type __t = 1, double __p = 0.5) |
| 4091 | : __p_(param_type(__t, __p)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4092 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4093 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4094 | explicit binomial_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4095 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4096 | void reset() {} |
| 4097 | |
| 4098 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4099 | template<class _URNG> |
| 4100 | _LIBCPP_INLINE_VISIBILITY |
| 4101 | result_type operator()(_URNG& __g) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4102 | {return (*this)(__g, __p_);} |
| 4103 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4104 | |
| 4105 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4106 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4107 | result_type t() const {return __p_.t();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4108 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4109 | double p() const {return __p_.p();} |
| 4110 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4111 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4112 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4113 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4114 | void param(const param_type& __p) {__p_ = __p;} |
| 4115 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4116 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4117 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4118 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4119 | result_type max() const {return t();} |
| 4120 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4121 | friend _LIBCPP_INLINE_VISIBILITY |
| 4122 | bool operator==(const binomial_distribution& __x, |
| 4123 | const binomial_distribution& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4124 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4125 | friend _LIBCPP_INLINE_VISIBILITY |
| 4126 | bool operator!=(const binomial_distribution& __x, |
| 4127 | const binomial_distribution& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4128 | {return !(__x == __y);} |
| 4129 | }; |
| 4130 | |
Martin Storsjö | 408cee7 | 2020-12-01 13:37:16 +0200 | [diff] [blame] | 4131 | #ifndef _LIBCPP_MSVCRT_LIKE |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4132 | extern "C" double lgamma_r(double, int *); |
Eric Fiselier | 651ca6e | 2017-05-06 02:58:43 +0000 | [diff] [blame] | 4133 | #endif |
| 4134 | |
| 4135 | inline _LIBCPP_INLINE_VISIBILITY double __libcpp_lgamma(double __d) { |
Martin Storsjö | 408cee7 | 2020-12-01 13:37:16 +0200 | [diff] [blame] | 4136 | #if defined(_LIBCPP_MSVCRT_LIKE) |
Eric Fiselier | 651ca6e | 2017-05-06 02:58:43 +0000 | [diff] [blame] | 4137 | return lgamma(__d); |
| 4138 | #else |
| 4139 | int __sign; |
| 4140 | return lgamma_r(__d, &__sign); |
| 4141 | #endif |
| 4142 | } |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4143 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4144 | template<class _IntType> |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4145 | 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] | 4146 | : __t_(__t), __p_(__p) |
| 4147 | { |
| 4148 | if (0 < __p_ && __p_ < 1) |
| 4149 | { |
| 4150 | __r0_ = static_cast<result_type>((__t_ + 1) * __p_); |
Eric Fiselier | 651ca6e | 2017-05-06 02:58:43 +0000 | [diff] [blame] | 4151 | __pr_ = _VSTD::exp(__libcpp_lgamma(__t_ + 1.) - |
| 4152 | __libcpp_lgamma(__r0_ + 1.) - |
| 4153 | __libcpp_lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) + |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4154 | (__t_ - __r0_) * _VSTD::log(1 - __p_)); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4155 | __odds_ratio_ = __p_ / (1 - __p_); |
| 4156 | } |
| 4157 | } |
| 4158 | |
Marshall Clow | f97a84f | 2014-09-17 18:33:58 +0000 | [diff] [blame] | 4159 | // Reference: Kemp, C.D. (1986). `A modal method for generating binomial |
| 4160 | // variables', Commun. Statist. - Theor. Meth. 15(3), 805-813. |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4161 | template<class _IntType> |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4162 | template<class _URNG> |
| 4163 | _IntType |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4164 | binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4165 | { |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4166 | if (__pr.__t_ == 0 || __pr.__p_ == 0) |
| 4167 | return 0; |
| 4168 | if (__pr.__p_ == 1) |
| 4169 | return __pr.__t_; |
| 4170 | uniform_real_distribution<double> __gen; |
| 4171 | double __u = __gen(__g) - __pr.__pr_; |
| 4172 | if (__u < 0) |
| 4173 | return __pr.__r0_; |
| 4174 | double __pu = __pr.__pr_; |
| 4175 | double __pd = __pu; |
| 4176 | result_type __ru = __pr.__r0_; |
| 4177 | result_type __rd = __ru; |
| 4178 | while (true) |
| 4179 | { |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4180 | bool __break = true; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4181 | if (__rd >= 1) |
| 4182 | { |
| 4183 | __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1)); |
| 4184 | __u -= __pd; |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4185 | __break = false; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4186 | if (__u < 0) |
| 4187 | return __rd - 1; |
| 4188 | } |
Marshall Clow | f97a84f | 2014-09-17 18:33:58 +0000 | [diff] [blame] | 4189 | if ( __rd != 0 ) |
| 4190 | --__rd; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4191 | ++__ru; |
| 4192 | if (__ru <= __pr.__t_) |
| 4193 | { |
| 4194 | __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru; |
| 4195 | __u -= __pu; |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4196 | __break = false; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4197 | if (__u < 0) |
| 4198 | return __ru; |
| 4199 | } |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4200 | if (__break) |
| 4201 | return 0; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4202 | } |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | template <class _CharT, class _Traits, class _IntType> |
| 4206 | basic_ostream<_CharT, _Traits>& |
| 4207 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4208 | const binomial_distribution<_IntType>& __x) |
| 4209 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4210 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4211 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 4212 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 4213 | _OStream::scientific); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4214 | _CharT __sp = __os.widen(' '); |
| 4215 | __os.fill(__sp); |
| 4216 | return __os << __x.t() << __sp << __x.p(); |
| 4217 | } |
| 4218 | |
| 4219 | template <class _CharT, class _Traits, class _IntType> |
| 4220 | basic_istream<_CharT, _Traits>& |
| 4221 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4222 | binomial_distribution<_IntType>& __x) |
| 4223 | { |
| 4224 | typedef binomial_distribution<_IntType> _Eng; |
| 4225 | typedef typename _Eng::result_type result_type; |
| 4226 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4227 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4228 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 4229 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4230 | result_type __t; |
| 4231 | double __p; |
| 4232 | __is >> __t >> __p; |
| 4233 | if (!__is.fail()) |
| 4234 | __x.param(param_type(__t, __p)); |
| 4235 | return __is; |
| 4236 | } |
| 4237 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4238 | // exponential_distribution |
| 4239 | |
| 4240 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4241 | class _LIBCPP_TEMPLATE_VIS exponential_distribution |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4242 | { |
| 4243 | public: |
| 4244 | // types |
| 4245 | typedef _RealType result_type; |
| 4246 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4247 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4248 | { |
| 4249 | result_type __lambda_; |
| 4250 | public: |
| 4251 | typedef exponential_distribution distribution_type; |
| 4252 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4253 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4254 | explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {} |
| 4255 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4256 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4257 | result_type lambda() const {return __lambda_;} |
| 4258 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4259 | friend _LIBCPP_INLINE_VISIBILITY |
| 4260 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4261 | {return __x.__lambda_ == __y.__lambda_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4262 | friend _LIBCPP_INLINE_VISIBILITY |
| 4263 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4264 | {return !(__x == __y);} |
| 4265 | }; |
| 4266 | |
| 4267 | private: |
| 4268 | param_type __p_; |
| 4269 | |
| 4270 | public: |
| 4271 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4272 | #ifndef _LIBCPP_CXX03_LANG |
| 4273 | _LIBCPP_INLINE_VISIBILITY |
| 4274 | exponential_distribution() : exponential_distribution(1) {} |
| 4275 | _LIBCPP_INLINE_VISIBILITY |
| 4276 | explicit exponential_distribution(result_type __lambda) |
| 4277 | : __p_(param_type(__lambda)) {} |
| 4278 | #else |
Howard 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 | explicit exponential_distribution(result_type __lambda = 1) |
| 4281 | : __p_(param_type(__lambda)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4282 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4283 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4284 | explicit exponential_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4285 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4286 | void reset() {} |
| 4287 | |
| 4288 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4289 | template<class _URNG> |
| 4290 | _LIBCPP_INLINE_VISIBILITY |
| 4291 | result_type operator()(_URNG& __g) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4292 | {return (*this)(__g, __p_);} |
| 4293 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4294 | |
| 4295 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4296 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4297 | result_type lambda() const {return __p_.lambda();} |
| 4298 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4299 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4300 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4301 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4302 | void param(const param_type& __p) {__p_ = __p;} |
| 4303 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4304 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4305 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4306 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 021f990 | 2010-05-16 17:56:20 +0000 | [diff] [blame] | 4307 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4308 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4309 | friend _LIBCPP_INLINE_VISIBILITY |
| 4310 | bool operator==(const exponential_distribution& __x, |
| 4311 | const exponential_distribution& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4312 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4313 | friend _LIBCPP_INLINE_VISIBILITY |
| 4314 | bool operator!=(const exponential_distribution& __x, |
| 4315 | const exponential_distribution& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4316 | {return !(__x == __y);} |
| 4317 | }; |
| 4318 | |
| 4319 | template <class _RealType> |
| 4320 | template<class _URNG> |
| 4321 | _RealType |
| 4322 | exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4323 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4324 | return -_VSTD::log |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4325 | ( |
| 4326 | result_type(1) - |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4327 | _VSTD::generate_canonical<result_type, |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4328 | numeric_limits<result_type>::digits>(__g) |
| 4329 | ) |
| 4330 | / __p.lambda(); |
| 4331 | } |
| 4332 | |
| 4333 | template <class _CharT, class _Traits, class _RealType> |
| 4334 | basic_ostream<_CharT, _Traits>& |
| 4335 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4336 | const exponential_distribution<_RealType>& __x) |
| 4337 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4338 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4339 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 4340 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 4341 | _OStream::scientific); |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4342 | return __os << __x.lambda(); |
| 4343 | } |
| 4344 | |
| 4345 | template <class _CharT, class _Traits, class _RealType> |
| 4346 | basic_istream<_CharT, _Traits>& |
| 4347 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4348 | exponential_distribution<_RealType>& __x) |
| 4349 | { |
| 4350 | typedef exponential_distribution<_RealType> _Eng; |
| 4351 | typedef typename _Eng::result_type result_type; |
| 4352 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4353 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4354 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 4355 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4356 | result_type __lambda; |
| 4357 | __is >> __lambda; |
| 4358 | if (!__is.fail()) |
| 4359 | __x.param(param_type(__lambda)); |
| 4360 | return __is; |
| 4361 | } |
| 4362 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4363 | // normal_distribution |
| 4364 | |
| 4365 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4366 | class _LIBCPP_TEMPLATE_VIS normal_distribution |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4367 | { |
| 4368 | public: |
| 4369 | // types |
| 4370 | typedef _RealType result_type; |
| 4371 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4372 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4373 | { |
| 4374 | result_type __mean_; |
| 4375 | result_type __stddev_; |
| 4376 | public: |
| 4377 | typedef normal_distribution distribution_type; |
| 4378 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4379 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4380 | explicit param_type(result_type __mean = 0, result_type __stddev = 1) |
| 4381 | : __mean_(__mean), __stddev_(__stddev) {} |
| 4382 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4383 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4384 | result_type mean() const {return __mean_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4385 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4386 | result_type stddev() const {return __stddev_;} |
| 4387 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4388 | friend _LIBCPP_INLINE_VISIBILITY |
| 4389 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4390 | {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4391 | friend _LIBCPP_INLINE_VISIBILITY |
| 4392 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4393 | {return !(__x == __y);} |
| 4394 | }; |
| 4395 | |
| 4396 | private: |
| 4397 | param_type __p_; |
| 4398 | result_type _V_; |
| 4399 | bool _V_hot_; |
| 4400 | |
| 4401 | public: |
| 4402 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4403 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4404 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4405 | normal_distribution() : normal_distribution(0) {} |
| 4406 | _LIBCPP_INLINE_VISIBILITY |
| 4407 | explicit normal_distribution(result_type __mean, result_type __stddev = 1) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4408 | : __p_(param_type(__mean, __stddev)), _V_hot_(false) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4409 | #else |
| 4410 | _LIBCPP_INLINE_VISIBILITY |
| 4411 | explicit normal_distribution(result_type __mean = 0, |
| 4412 | result_type __stddev = 1) |
| 4413 | : __p_(param_type(__mean, __stddev)), _V_hot_(false) {} |
| 4414 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4415 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4416 | explicit normal_distribution(const param_type& __p) |
| 4417 | : __p_(__p), _V_hot_(false) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4418 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4419 | void reset() {_V_hot_ = false;} |
| 4420 | |
| 4421 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4422 | template<class _URNG> |
| 4423 | _LIBCPP_INLINE_VISIBILITY |
| 4424 | result_type operator()(_URNG& __g) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4425 | {return (*this)(__g, __p_);} |
| 4426 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4427 | |
| 4428 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4429 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4430 | result_type mean() const {return __p_.mean();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4431 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4432 | result_type stddev() const {return __p_.stddev();} |
| 4433 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4434 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4435 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4436 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4437 | void param(const param_type& __p) {__p_ = __p;} |
| 4438 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4439 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4440 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4441 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4442 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4443 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4444 | friend _LIBCPP_INLINE_VISIBILITY |
| 4445 | bool operator==(const normal_distribution& __x, |
| 4446 | const normal_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4447 | {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ && |
| 4448 | (!__x._V_hot_ || __x._V_ == __y._V_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4449 | friend _LIBCPP_INLINE_VISIBILITY |
| 4450 | bool operator!=(const normal_distribution& __x, |
| 4451 | const normal_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4452 | {return !(__x == __y);} |
| 4453 | |
| 4454 | template <class _CharT, class _Traits, class _RT> |
| 4455 | friend |
| 4456 | basic_ostream<_CharT, _Traits>& |
| 4457 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4458 | const normal_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4459 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4460 | template <class _CharT, class _Traits, class _RT> |
| 4461 | friend |
| 4462 | basic_istream<_CharT, _Traits>& |
| 4463 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4464 | normal_distribution<_RT>& __x); |
| 4465 | }; |
| 4466 | |
| 4467 | template <class _RealType> |
| 4468 | template<class _URNG> |
| 4469 | _RealType |
| 4470 | normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4471 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4472 | result_type _Up; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4473 | if (_V_hot_) |
| 4474 | { |
| 4475 | _V_hot_ = false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4476 | _Up = _V_; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4477 | } |
| 4478 | else |
| 4479 | { |
| 4480 | uniform_real_distribution<result_type> _Uni(-1, 1); |
| 4481 | result_type __u; |
| 4482 | result_type __v; |
| 4483 | result_type __s; |
| 4484 | do |
| 4485 | { |
| 4486 | __u = _Uni(__g); |
| 4487 | __v = _Uni(__g); |
| 4488 | __s = __u * __u + __v * __v; |
| 4489 | } while (__s > 1 || __s == 0); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4490 | result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s); |
| 4491 | _V_ = __v * _Fp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4492 | _V_hot_ = true; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4493 | _Up = __u * _Fp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4494 | } |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4495 | return _Up * __p.stddev() + __p.mean(); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | template <class _CharT, class _Traits, class _RT> |
| 4499 | basic_ostream<_CharT, _Traits>& |
| 4500 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4501 | const normal_distribution<_RT>& __x) |
| 4502 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4503 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4504 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 4505 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 4506 | _OStream::scientific); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4507 | _CharT __sp = __os.widen(' '); |
| 4508 | __os.fill(__sp); |
| 4509 | __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_; |
| 4510 | if (__x._V_hot_) |
| 4511 | __os << __sp << __x._V_; |
| 4512 | return __os; |
| 4513 | } |
| 4514 | |
| 4515 | template <class _CharT, class _Traits, class _RT> |
| 4516 | basic_istream<_CharT, _Traits>& |
| 4517 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4518 | normal_distribution<_RT>& __x) |
| 4519 | { |
| 4520 | typedef normal_distribution<_RT> _Eng; |
| 4521 | typedef typename _Eng::result_type result_type; |
| 4522 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4523 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4524 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 4525 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4526 | result_type __mean; |
| 4527 | result_type __stddev; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4528 | result_type _Vp = 0; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4529 | bool _V_hot = false; |
| 4530 | __is >> __mean >> __stddev >> _V_hot; |
| 4531 | if (_V_hot) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4532 | __is >> _Vp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4533 | if (!__is.fail()) |
| 4534 | { |
| 4535 | __x.param(param_type(__mean, __stddev)); |
| 4536 | __x._V_hot_ = _V_hot; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4537 | __x._V_ = _Vp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4538 | } |
| 4539 | return __is; |
| 4540 | } |
| 4541 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4542 | // lognormal_distribution |
| 4543 | |
| 4544 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4545 | class _LIBCPP_TEMPLATE_VIS lognormal_distribution |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4546 | { |
| 4547 | public: |
| 4548 | // types |
| 4549 | typedef _RealType result_type; |
| 4550 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4551 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4552 | { |
| 4553 | normal_distribution<result_type> __nd_; |
| 4554 | public: |
| 4555 | typedef lognormal_distribution distribution_type; |
| 4556 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4557 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4558 | explicit param_type(result_type __m = 0, result_type __s = 1) |
| 4559 | : __nd_(__m, __s) {} |
| 4560 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4561 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4562 | result_type m() const {return __nd_.mean();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4563 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4564 | result_type s() const {return __nd_.stddev();} |
| 4565 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4566 | friend _LIBCPP_INLINE_VISIBILITY |
| 4567 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4568 | {return __x.__nd_ == __y.__nd_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4569 | friend _LIBCPP_INLINE_VISIBILITY |
| 4570 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4571 | {return !(__x == __y);} |
| 4572 | friend class lognormal_distribution; |
| 4573 | |
| 4574 | template <class _CharT, class _Traits, class _RT> |
| 4575 | friend |
| 4576 | basic_ostream<_CharT, _Traits>& |
| 4577 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4578 | const lognormal_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4579 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4580 | template <class _CharT, class _Traits, class _RT> |
| 4581 | friend |
| 4582 | basic_istream<_CharT, _Traits>& |
| 4583 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4584 | lognormal_distribution<_RT>& __x); |
| 4585 | }; |
| 4586 | |
| 4587 | private: |
| 4588 | param_type __p_; |
| 4589 | |
| 4590 | public: |
| 4591 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4592 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4593 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4594 | lognormal_distribution() : lognormal_distribution(0) {} |
| 4595 | _LIBCPP_INLINE_VISIBILITY |
| 4596 | explicit lognormal_distribution(result_type __m, result_type __s = 1) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4597 | : __p_(param_type(__m, __s)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4598 | #else |
| 4599 | _LIBCPP_INLINE_VISIBILITY |
| 4600 | explicit lognormal_distribution(result_type __m = 0, |
| 4601 | result_type __s = 1) |
| 4602 | : __p_(param_type(__m, __s)) {} |
| 4603 | #endif |
Howard 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 | explicit lognormal_distribution(const param_type& __p) |
| 4606 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4607 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4608 | void reset() {__p_.__nd_.reset();} |
| 4609 | |
| 4610 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4611 | template<class _URNG> |
| 4612 | _LIBCPP_INLINE_VISIBILITY |
| 4613 | result_type operator()(_URNG& __g) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4614 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4615 | template<class _URNG> |
| 4616 | _LIBCPP_INLINE_VISIBILITY |
| 4617 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4618 | {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));} |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4619 | |
| 4620 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4621 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4622 | result_type m() const {return __p_.m();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4623 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4624 | result_type s() const {return __p_.s();} |
| 4625 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4626 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4627 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4628 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 4629 | void param(const param_type& __p) {__p_ = __p;} |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4630 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4631 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4632 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4633 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4634 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4635 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4636 | friend _LIBCPP_INLINE_VISIBILITY |
| 4637 | bool operator==(const lognormal_distribution& __x, |
| 4638 | const lognormal_distribution& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4639 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4640 | friend _LIBCPP_INLINE_VISIBILITY |
| 4641 | bool operator!=(const lognormal_distribution& __x, |
| 4642 | const lognormal_distribution& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4643 | {return !(__x == __y);} |
| 4644 | |
| 4645 | template <class _CharT, class _Traits, class _RT> |
| 4646 | friend |
| 4647 | basic_ostream<_CharT, _Traits>& |
| 4648 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4649 | const lognormal_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4650 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4651 | template <class _CharT, class _Traits, class _RT> |
| 4652 | friend |
| 4653 | basic_istream<_CharT, _Traits>& |
| 4654 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4655 | lognormal_distribution<_RT>& __x); |
| 4656 | }; |
| 4657 | |
| 4658 | template <class _CharT, class _Traits, class _RT> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4659 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4660 | basic_ostream<_CharT, _Traits>& |
| 4661 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4662 | const lognormal_distribution<_RT>& __x) |
| 4663 | { |
| 4664 | return __os << __x.__p_.__nd_; |
| 4665 | } |
| 4666 | |
| 4667 | template <class _CharT, class _Traits, class _RT> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4668 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4669 | basic_istream<_CharT, _Traits>& |
| 4670 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4671 | lognormal_distribution<_RT>& __x) |
| 4672 | { |
| 4673 | return __is >> __x.__p_.__nd_; |
| 4674 | } |
| 4675 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4676 | // poisson_distribution |
| 4677 | |
| 4678 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4679 | class _LIBCPP_TEMPLATE_VIS poisson_distribution |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4680 | { |
| 4681 | public: |
| 4682 | // types |
| 4683 | typedef _IntType result_type; |
| 4684 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4685 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4686 | { |
| 4687 | double __mean_; |
| 4688 | double __s_; |
| 4689 | double __d_; |
| 4690 | double __l_; |
| 4691 | double __omega_; |
| 4692 | double __c0_; |
| 4693 | double __c1_; |
| 4694 | double __c2_; |
| 4695 | double __c3_; |
| 4696 | double __c_; |
| 4697 | |
| 4698 | public: |
| 4699 | typedef poisson_distribution distribution_type; |
| 4700 | |
| 4701 | explicit param_type(double __mean = 1.0); |
| 4702 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4703 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4704 | double mean() const {return __mean_;} |
| 4705 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4706 | friend _LIBCPP_INLINE_VISIBILITY |
| 4707 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4708 | {return __x.__mean_ == __y.__mean_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4709 | friend _LIBCPP_INLINE_VISIBILITY |
| 4710 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4711 | {return !(__x == __y);} |
| 4712 | |
| 4713 | friend class poisson_distribution; |
| 4714 | }; |
| 4715 | |
| 4716 | private: |
| 4717 | param_type __p_; |
| 4718 | |
| 4719 | public: |
| 4720 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4721 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4722 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4723 | poisson_distribution() : poisson_distribution(1.0) {} |
| 4724 | _LIBCPP_INLINE_VISIBILITY |
| 4725 | explicit poisson_distribution(double __mean) |
| 4726 | : __p_(__mean) {} |
| 4727 | #else |
| 4728 | _LIBCPP_INLINE_VISIBILITY |
| 4729 | explicit poisson_distribution(double __mean = 1.0) |
| 4730 | : __p_(__mean) {} |
| 4731 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4732 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4733 | explicit poisson_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4734 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4735 | void reset() {} |
| 4736 | |
| 4737 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4738 | template<class _URNG> |
| 4739 | _LIBCPP_INLINE_VISIBILITY |
| 4740 | result_type operator()(_URNG& __g) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4741 | {return (*this)(__g, __p_);} |
| 4742 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4743 | |
| 4744 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4745 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4746 | double mean() const {return __p_.mean();} |
| 4747 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4748 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4749 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4750 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4751 | void param(const param_type& __p) {__p_ = __p;} |
| 4752 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4753 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4754 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4755 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4756 | result_type max() const {return numeric_limits<result_type>::max();} |
| 4757 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4758 | friend _LIBCPP_INLINE_VISIBILITY |
| 4759 | bool operator==(const poisson_distribution& __x, |
| 4760 | const poisson_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4761 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4762 | friend _LIBCPP_INLINE_VISIBILITY |
| 4763 | bool operator!=(const poisson_distribution& __x, |
| 4764 | const poisson_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4765 | {return !(__x == __y);} |
| 4766 | }; |
| 4767 | |
| 4768 | template<class _IntType> |
| 4769 | poisson_distribution<_IntType>::param_type::param_type(double __mean) |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4770 | // According to the standard `inf` is a valid input, but it causes the |
| 4771 | // distribution to hang, so we replace it with the maximum representable |
| 4772 | // mean. |
| 4773 | : __mean_(isinf(__mean) ? numeric_limits<double>::max() : __mean) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4774 | { |
| 4775 | if (__mean_ < 10) |
| 4776 | { |
| 4777 | __s_ = 0; |
| 4778 | __d_ = 0; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4779 | __l_ = _VSTD::exp(-__mean_); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4780 | __omega_ = 0; |
| 4781 | __c3_ = 0; |
| 4782 | __c2_ = 0; |
| 4783 | __c1_ = 0; |
| 4784 | __c0_ = 0; |
| 4785 | __c_ = 0; |
| 4786 | } |
| 4787 | else |
| 4788 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4789 | __s_ = _VSTD::sqrt(__mean_); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4790 | __d_ = 6 * __mean_ * __mean_; |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4791 | __l_ = _VSTD::trunc(__mean_ - 1.1484); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4792 | __omega_ = .3989423 / __s_; |
| 4793 | double __b1_ = .4166667E-1 / __mean_; |
| 4794 | double __b2_ = .3 * __b1_ * __b1_; |
| 4795 | __c3_ = .1428571 * __b1_ * __b2_; |
| 4796 | __c2_ = __b2_ - 15. * __c3_; |
| 4797 | __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_; |
| 4798 | __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_; |
| 4799 | __c_ = .1069 / __mean_; |
| 4800 | } |
| 4801 | } |
| 4802 | |
| 4803 | template <class _IntType> |
| 4804 | template<class _URNG> |
| 4805 | _IntType |
| 4806 | poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 4807 | { |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4808 | double __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4809 | uniform_real_distribution<double> __urd; |
Howard Hinnant | a5fb7ac | 2011-04-14 15:59:22 +0000 | [diff] [blame] | 4810 | if (__pr.__mean_ < 10) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4811 | { |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4812 | __tx = 0; |
| 4813 | for (double __p = __urd(__urng); __p > __pr.__l_; ++__tx) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4814 | __p *= __urd(__urng); |
| 4815 | } |
| 4816 | else |
| 4817 | { |
| 4818 | double __difmuk; |
| 4819 | double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng); |
| 4820 | double __u; |
| 4821 | if (__g > 0) |
| 4822 | { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4823 | __tx = _VSTD::trunc(__g); |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4824 | if (__tx >= __pr.__l_) |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4825 | return _VSTD::__clamp_to_integral<result_type>(__tx); |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4826 | __difmuk = __pr.__mean_ - __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4827 | __u = __urd(__urng); |
| 4828 | if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk) |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4829 | return _VSTD::__clamp_to_integral<result_type>(__tx); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4830 | } |
| 4831 | exponential_distribution<double> __edist; |
| 4832 | for (bool __using_exp_dist = false; true; __using_exp_dist = true) |
| 4833 | { |
| 4834 | double __e; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4835 | if (__using_exp_dist || __g <= 0) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4836 | { |
| 4837 | double __t; |
| 4838 | do |
| 4839 | { |
| 4840 | __e = __edist(__urng); |
| 4841 | __u = __urd(__urng); |
| 4842 | __u += __u - 1; |
| 4843 | __t = 1.8 + (__u < 0 ? -__e : __e); |
| 4844 | } while (__t <= -.6744); |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4845 | __tx = _VSTD::trunc(__pr.__mean_ + __pr.__s_ * __t); |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4846 | __difmuk = __pr.__mean_ - __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4847 | __using_exp_dist = true; |
| 4848 | } |
| 4849 | double __px; |
| 4850 | double __py; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4851 | if (__tx < 10 && __tx >= 0) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4852 | { |
Marshall Clow | c7e36e8 | 2018-01-16 14:54:36 +0000 | [diff] [blame] | 4853 | const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4854 | 40320, 362880}; |
| 4855 | __px = -__pr.__mean_; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4856 | __py = _VSTD::pow(__pr.__mean_, (double)__tx) / __fac[static_cast<int>(__tx)]; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4857 | } |
| 4858 | else |
| 4859 | { |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4860 | double __del = .8333333E-1 / __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4861 | __del -= 4.8 * __del * __del * __del; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4862 | double __v = __difmuk / __tx; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4863 | if (_VSTD::abs(__v) > 0.25) |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4864 | __px = __tx * _VSTD::log(1 + __v) - __difmuk - __del; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4865 | else |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4866 | __px = __tx * __v * __v * (((((((.1250060 * __v + -.1384794) * |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4867 | __v + .1421878) * __v + -.1661269) * __v + .2000118) * |
| 4868 | __v + -.2500068) * __v + .3333333) * __v + -.5) - __del; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4869 | __py = .3989423 / _VSTD::sqrt(__tx); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4870 | } |
| 4871 | double __r = (0.5 - __difmuk) / __pr.__s_; |
| 4872 | double __r2 = __r * __r; |
| 4873 | double __fx = -0.5 * __r2; |
| 4874 | double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) * |
| 4875 | __r2 + __pr.__c1_) * __r2 + __pr.__c0_); |
| 4876 | if (__using_exp_dist) |
| 4877 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4878 | if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) - |
| 4879 | __fy * _VSTD::exp(__fx + __e)) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4880 | break; |
| 4881 | } |
| 4882 | else |
| 4883 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4884 | if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx)) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4885 | break; |
| 4886 | } |
| 4887 | } |
| 4888 | } |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4889 | return _VSTD::__clamp_to_integral<result_type>(__tx); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | template <class _CharT, class _Traits, class _IntType> |
| 4893 | basic_ostream<_CharT, _Traits>& |
| 4894 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4895 | const poisson_distribution<_IntType>& __x) |
| 4896 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4897 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4898 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 4899 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 4900 | _OStream::scientific); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4901 | return __os << __x.mean(); |
| 4902 | } |
| 4903 | |
| 4904 | template <class _CharT, class _Traits, class _IntType> |
| 4905 | basic_istream<_CharT, _Traits>& |
| 4906 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4907 | poisson_distribution<_IntType>& __x) |
| 4908 | { |
| 4909 | typedef poisson_distribution<_IntType> _Eng; |
| 4910 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4911 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 4912 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 4913 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4914 | double __mean; |
| 4915 | __is >> __mean; |
| 4916 | if (!__is.fail()) |
| 4917 | __x.param(param_type(__mean)); |
| 4918 | return __is; |
| 4919 | } |
| 4920 | |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4921 | // weibull_distribution |
| 4922 | |
| 4923 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4924 | class _LIBCPP_TEMPLATE_VIS weibull_distribution |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4925 | { |
| 4926 | public: |
| 4927 | // types |
| 4928 | typedef _RealType result_type; |
| 4929 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4930 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4931 | { |
| 4932 | result_type __a_; |
| 4933 | result_type __b_; |
| 4934 | public: |
| 4935 | typedef weibull_distribution distribution_type; |
| 4936 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4937 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4938 | explicit param_type(result_type __a = 1, result_type __b = 1) |
| 4939 | : __a_(__a), __b_(__b) {} |
| 4940 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4941 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4942 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4943 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4944 | result_type b() const {return __b_;} |
| 4945 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4946 | friend _LIBCPP_INLINE_VISIBILITY |
| 4947 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4948 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4949 | friend _LIBCPP_INLINE_VISIBILITY |
| 4950 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4951 | {return !(__x == __y);} |
| 4952 | }; |
| 4953 | |
| 4954 | private: |
| 4955 | param_type __p_; |
| 4956 | |
| 4957 | public: |
| 4958 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4959 | #ifndef _LIBCPP_CXX03_LANG |
| 4960 | _LIBCPP_INLINE_VISIBILITY |
| 4961 | weibull_distribution() : weibull_distribution(1) {} |
| 4962 | _LIBCPP_INLINE_VISIBILITY |
| 4963 | explicit weibull_distribution(result_type __a, result_type __b = 1) |
| 4964 | : __p_(param_type(__a, __b)) {} |
| 4965 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4966 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4967 | explicit weibull_distribution(result_type __a = 1, result_type __b = 1) |
| 4968 | : __p_(param_type(__a, __b)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 4969 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4970 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4971 | explicit weibull_distribution(const param_type& __p) |
| 4972 | : __p_(__p) {} |
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 | void reset() {} |
| 4975 | |
| 4976 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4977 | template<class _URNG> |
| 4978 | _LIBCPP_INLINE_VISIBILITY |
| 4979 | result_type operator()(_URNG& __g) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4980 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4981 | template<class _URNG> |
| 4982 | _LIBCPP_INLINE_VISIBILITY |
| 4983 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4984 | {return __p.b() * |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4985 | _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());} |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4986 | |
| 4987 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4988 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4989 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4990 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4991 | result_type b() const {return __p_.b();} |
| 4992 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4993 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4994 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4995 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4996 | void param(const param_type& __p) {__p_ = __p;} |
| 4997 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4998 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4999 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5000 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 5001 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 5002 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5003 | friend _LIBCPP_INLINE_VISIBILITY |
| 5004 | bool operator==(const weibull_distribution& __x, |
| 5005 | const weibull_distribution& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 5006 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5007 | friend _LIBCPP_INLINE_VISIBILITY |
| 5008 | bool operator!=(const weibull_distribution& __x, |
| 5009 | const weibull_distribution& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 5010 | {return !(__x == __y);} |
| 5011 | }; |
| 5012 | |
| 5013 | template <class _CharT, class _Traits, class _RT> |
| 5014 | basic_ostream<_CharT, _Traits>& |
| 5015 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5016 | const weibull_distribution<_RT>& __x) |
| 5017 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5018 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5019 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5020 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5021 | _OStream::scientific); |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 5022 | _CharT __sp = __os.widen(' '); |
| 5023 | __os.fill(__sp); |
| 5024 | __os << __x.a() << __sp << __x.b(); |
| 5025 | return __os; |
| 5026 | } |
| 5027 | |
| 5028 | template <class _CharT, class _Traits, class _RT> |
| 5029 | basic_istream<_CharT, _Traits>& |
| 5030 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5031 | weibull_distribution<_RT>& __x) |
| 5032 | { |
| 5033 | typedef weibull_distribution<_RT> _Eng; |
| 5034 | typedef typename _Eng::result_type result_type; |
| 5035 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5036 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5037 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5038 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 5039 | result_type __a; |
| 5040 | result_type __b; |
| 5041 | __is >> __a >> __b; |
| 5042 | if (!__is.fail()) |
| 5043 | __x.param(param_type(__a, __b)); |
| 5044 | return __is; |
| 5045 | } |
| 5046 | |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5047 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5048 | class _LIBCPP_TEMPLATE_VIS extreme_value_distribution |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5049 | { |
| 5050 | public: |
| 5051 | // types |
| 5052 | typedef _RealType result_type; |
| 5053 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5054 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5055 | { |
| 5056 | result_type __a_; |
| 5057 | result_type __b_; |
| 5058 | public: |
| 5059 | typedef extreme_value_distribution distribution_type; |
| 5060 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5061 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5062 | explicit param_type(result_type __a = 0, result_type __b = 1) |
| 5063 | : __a_(__a), __b_(__b) {} |
| 5064 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5065 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5066 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5067 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5068 | result_type b() const {return __b_;} |
| 5069 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5070 | friend _LIBCPP_INLINE_VISIBILITY |
| 5071 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5072 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5073 | friend _LIBCPP_INLINE_VISIBILITY |
| 5074 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5075 | {return !(__x == __y);} |
| 5076 | }; |
| 5077 | |
| 5078 | private: |
| 5079 | param_type __p_; |
| 5080 | |
| 5081 | public: |
| 5082 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5083 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5084 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5085 | extreme_value_distribution() : extreme_value_distribution(0) {} |
| 5086 | _LIBCPP_INLINE_VISIBILITY |
| 5087 | explicit extreme_value_distribution(result_type __a, result_type __b = 1) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5088 | : __p_(param_type(__a, __b)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5089 | #else |
| 5090 | _LIBCPP_INLINE_VISIBILITY |
| 5091 | explicit extreme_value_distribution(result_type __a = 0, |
| 5092 | result_type __b = 1) |
| 5093 | : __p_(param_type(__a, __b)) {} |
| 5094 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5095 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5096 | explicit extreme_value_distribution(const param_type& __p) |
| 5097 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5098 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5099 | void reset() {} |
| 5100 | |
| 5101 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5102 | template<class _URNG> |
| 5103 | _LIBCPP_INLINE_VISIBILITY |
| 5104 | result_type operator()(_URNG& __g) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5105 | {return (*this)(__g, __p_);} |
| 5106 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5107 | |
| 5108 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5109 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5110 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5111 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5112 | result_type b() const {return __p_.b();} |
| 5113 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5114 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5115 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5116 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5117 | void param(const param_type& __p) {__p_ = __p;} |
| 5118 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5119 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5120 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5121 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5122 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 5123 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5124 | friend _LIBCPP_INLINE_VISIBILITY |
| 5125 | bool operator==(const extreme_value_distribution& __x, |
| 5126 | const extreme_value_distribution& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5127 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5128 | friend _LIBCPP_INLINE_VISIBILITY |
| 5129 | bool operator!=(const extreme_value_distribution& __x, |
| 5130 | const extreme_value_distribution& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5131 | {return !(__x == __y);} |
| 5132 | }; |
| 5133 | |
| 5134 | template<class _RealType> |
| 5135 | template<class _URNG> |
| 5136 | _RealType |
| 5137 | extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5138 | { |
| 5139 | return __p.a() - __p.b() * |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5140 | _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g))); |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5141 | } |
| 5142 | |
| 5143 | template <class _CharT, class _Traits, class _RT> |
| 5144 | basic_ostream<_CharT, _Traits>& |
| 5145 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5146 | const extreme_value_distribution<_RT>& __x) |
| 5147 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5148 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5149 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5150 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5151 | _OStream::scientific); |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5152 | _CharT __sp = __os.widen(' '); |
| 5153 | __os.fill(__sp); |
| 5154 | __os << __x.a() << __sp << __x.b(); |
| 5155 | return __os; |
| 5156 | } |
| 5157 | |
| 5158 | template <class _CharT, class _Traits, class _RT> |
| 5159 | basic_istream<_CharT, _Traits>& |
| 5160 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5161 | extreme_value_distribution<_RT>& __x) |
| 5162 | { |
| 5163 | typedef extreme_value_distribution<_RT> _Eng; |
| 5164 | typedef typename _Eng::result_type result_type; |
| 5165 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5166 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5167 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5168 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 5169 | result_type __a; |
| 5170 | result_type __b; |
| 5171 | __is >> __a >> __b; |
| 5172 | if (!__is.fail()) |
| 5173 | __x.param(param_type(__a, __b)); |
| 5174 | return __is; |
| 5175 | } |
| 5176 | |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5177 | // gamma_distribution |
| 5178 | |
| 5179 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5180 | class _LIBCPP_TEMPLATE_VIS gamma_distribution |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5181 | { |
| 5182 | public: |
| 5183 | // types |
| 5184 | typedef _RealType result_type; |
| 5185 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5186 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5187 | { |
| 5188 | result_type __alpha_; |
| 5189 | result_type __beta_; |
| 5190 | public: |
| 5191 | typedef gamma_distribution distribution_type; |
| 5192 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5193 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5194 | explicit param_type(result_type __alpha = 1, result_type __beta = 1) |
| 5195 | : __alpha_(__alpha), __beta_(__beta) {} |
| 5196 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5197 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5198 | result_type alpha() const {return __alpha_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5199 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5200 | result_type beta() const {return __beta_;} |
| 5201 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5202 | friend _LIBCPP_INLINE_VISIBILITY |
| 5203 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5204 | {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5205 | friend _LIBCPP_INLINE_VISIBILITY |
| 5206 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5207 | {return !(__x == __y);} |
| 5208 | }; |
| 5209 | |
| 5210 | private: |
| 5211 | param_type __p_; |
| 5212 | |
| 5213 | public: |
| 5214 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5215 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5216 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5217 | gamma_distribution() : gamma_distribution(1) {} |
| 5218 | _LIBCPP_INLINE_VISIBILITY |
| 5219 | explicit gamma_distribution(result_type __alpha, result_type __beta = 1) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5220 | : __p_(param_type(__alpha, __beta)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5221 | #else |
| 5222 | _LIBCPP_INLINE_VISIBILITY |
| 5223 | explicit gamma_distribution(result_type __alpha = 1, |
| 5224 | result_type __beta = 1) |
| 5225 | : __p_(param_type(__alpha, __beta)) {} |
| 5226 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5227 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5228 | explicit gamma_distribution(const param_type& __p) |
| 5229 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5230 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5231 | void reset() {} |
| 5232 | |
| 5233 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5234 | template<class _URNG> |
| 5235 | _LIBCPP_INLINE_VISIBILITY |
| 5236 | result_type operator()(_URNG& __g) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5237 | {return (*this)(__g, __p_);} |
| 5238 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5239 | |
| 5240 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5241 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5242 | result_type alpha() const {return __p_.alpha();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5243 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5244 | result_type beta() const {return __p_.beta();} |
| 5245 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5246 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5247 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5248 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5249 | void param(const param_type& __p) {__p_ = __p;} |
| 5250 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5251 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5252 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5253 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5254 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5255 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5256 | friend _LIBCPP_INLINE_VISIBILITY |
| 5257 | bool operator==(const gamma_distribution& __x, |
| 5258 | const gamma_distribution& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5259 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5260 | friend _LIBCPP_INLINE_VISIBILITY |
| 5261 | bool operator!=(const gamma_distribution& __x, |
| 5262 | const gamma_distribution& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5263 | {return !(__x == __y);} |
| 5264 | }; |
| 5265 | |
| 5266 | template <class _RealType> |
| 5267 | template<class _URNG> |
| 5268 | _RealType |
| 5269 | gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5270 | { |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5271 | result_type __a = __p.alpha(); |
| 5272 | uniform_real_distribution<result_type> __gen(0, 1); |
| 5273 | exponential_distribution<result_type> __egen; |
| 5274 | result_type __x; |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5275 | if (__a == 1) |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5276 | __x = __egen(__g); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5277 | else if (__a > 1) |
| 5278 | { |
| 5279 | const result_type __b = __a - 1; |
| 5280 | const result_type __c = 3 * __a - result_type(0.75); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5281 | while (true) |
| 5282 | { |
| 5283 | const result_type __u = __gen(__g); |
| 5284 | const result_type __v = __gen(__g); |
| 5285 | const result_type __w = __u * (1 - __u); |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5286 | if (__w != 0) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5287 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5288 | const result_type __y = _VSTD::sqrt(__c / __w) * |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5289 | (__u - result_type(0.5)); |
| 5290 | __x = __b + __y; |
| 5291 | if (__x >= 0) |
| 5292 | { |
| 5293 | const result_type __z = 64 * __w * __w * __w * __v * __v; |
| 5294 | if (__z <= 1 - 2 * __y * __y / __x) |
| 5295 | break; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5296 | if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y)) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5297 | break; |
| 5298 | } |
| 5299 | } |
| 5300 | } |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5301 | } |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5302 | else // __a < 1 |
| 5303 | { |
| 5304 | while (true) |
| 5305 | { |
| 5306 | const result_type __u = __gen(__g); |
| 5307 | const result_type __es = __egen(__g); |
| 5308 | if (__u <= 1 - __a) |
| 5309 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5310 | __x = _VSTD::pow(__u, 1 / __a); |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5311 | if (__x <= __es) |
| 5312 | break; |
| 5313 | } |
| 5314 | else |
| 5315 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5316 | const result_type __e = -_VSTD::log((1-__u)/__a); |
| 5317 | __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a); |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5318 | if (__x <= __e + __es) |
| 5319 | break; |
| 5320 | } |
| 5321 | } |
| 5322 | } |
| 5323 | return __x * __p.beta(); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | template <class _CharT, class _Traits, class _RT> |
| 5327 | basic_ostream<_CharT, _Traits>& |
| 5328 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5329 | const gamma_distribution<_RT>& __x) |
| 5330 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5331 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5332 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5333 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5334 | _OStream::scientific); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5335 | _CharT __sp = __os.widen(' '); |
| 5336 | __os.fill(__sp); |
| 5337 | __os << __x.alpha() << __sp << __x.beta(); |
| 5338 | return __os; |
| 5339 | } |
| 5340 | |
| 5341 | template <class _CharT, class _Traits, class _RT> |
| 5342 | basic_istream<_CharT, _Traits>& |
| 5343 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5344 | gamma_distribution<_RT>& __x) |
| 5345 | { |
| 5346 | typedef gamma_distribution<_RT> _Eng; |
| 5347 | typedef typename _Eng::result_type result_type; |
| 5348 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5349 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5350 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5351 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5352 | result_type __alpha; |
| 5353 | result_type __beta; |
| 5354 | __is >> __alpha >> __beta; |
| 5355 | if (!__is.fail()) |
| 5356 | __x.param(param_type(__alpha, __beta)); |
| 5357 | return __is; |
| 5358 | } |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 5359 | |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5360 | // negative_binomial_distribution |
| 5361 | |
| 5362 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5363 | class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5364 | { |
| 5365 | public: |
| 5366 | // types |
| 5367 | typedef _IntType result_type; |
| 5368 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5369 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5370 | { |
| 5371 | result_type __k_; |
| 5372 | double __p_; |
| 5373 | public: |
| 5374 | typedef negative_binomial_distribution distribution_type; |
| 5375 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5376 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5377 | explicit param_type(result_type __k = 1, double __p = 0.5) |
| 5378 | : __k_(__k), __p_(__p) {} |
| 5379 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5380 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5381 | result_type k() const {return __k_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5382 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5383 | double p() const {return __p_;} |
| 5384 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5385 | friend _LIBCPP_INLINE_VISIBILITY |
| 5386 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5387 | {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5388 | friend _LIBCPP_INLINE_VISIBILITY |
| 5389 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5390 | {return !(__x == __y);} |
| 5391 | }; |
| 5392 | |
| 5393 | private: |
| 5394 | param_type __p_; |
| 5395 | |
| 5396 | public: |
| 5397 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5398 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5399 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5400 | negative_binomial_distribution() : negative_binomial_distribution(1) {} |
| 5401 | _LIBCPP_INLINE_VISIBILITY |
| 5402 | explicit negative_binomial_distribution(result_type __k, double __p = 0.5) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5403 | : __p_(__k, __p) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5404 | #else |
| 5405 | _LIBCPP_INLINE_VISIBILITY |
| 5406 | explicit negative_binomial_distribution(result_type __k = 1, |
| 5407 | double __p = 0.5) |
| 5408 | : __p_(__k, __p) {} |
| 5409 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5410 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5411 | explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5412 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5413 | void reset() {} |
| 5414 | |
| 5415 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5416 | template<class _URNG> |
| 5417 | _LIBCPP_INLINE_VISIBILITY |
| 5418 | result_type operator()(_URNG& __g) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5419 | {return (*this)(__g, __p_);} |
| 5420 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5421 | |
| 5422 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5423 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5424 | result_type k() const {return __p_.k();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5425 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5426 | double p() const {return __p_.p();} |
| 5427 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5428 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5429 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5430 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5431 | void param(const param_type& __p) {__p_ = __p;} |
| 5432 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5433 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5434 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5435 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5436 | result_type max() const {return numeric_limits<result_type>::max();} |
| 5437 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5438 | friend _LIBCPP_INLINE_VISIBILITY |
| 5439 | bool operator==(const negative_binomial_distribution& __x, |
| 5440 | const negative_binomial_distribution& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5441 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5442 | friend _LIBCPP_INLINE_VISIBILITY |
| 5443 | bool operator!=(const negative_binomial_distribution& __x, |
| 5444 | const negative_binomial_distribution& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5445 | {return !(__x == __y);} |
| 5446 | }; |
| 5447 | |
| 5448 | template <class _IntType> |
| 5449 | template<class _URNG> |
| 5450 | _IntType |
| 5451 | negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 5452 | { |
| 5453 | result_type __k = __pr.k(); |
| 5454 | double __p = __pr.p(); |
| 5455 | if (__k <= 21 * __p) |
| 5456 | { |
| 5457 | bernoulli_distribution __gen(__p); |
| 5458 | result_type __f = 0; |
| 5459 | result_type __s = 0; |
| 5460 | while (__s < __k) |
| 5461 | { |
| 5462 | if (__gen(__urng)) |
| 5463 | ++__s; |
| 5464 | else |
| 5465 | ++__f; |
| 5466 | } |
| 5467 | return __f; |
| 5468 | } |
| 5469 | return poisson_distribution<result_type>(gamma_distribution<double> |
| 5470 | (__k, (1-__p)/__p)(__urng))(__urng); |
| 5471 | } |
| 5472 | |
| 5473 | template <class _CharT, class _Traits, class _IntType> |
| 5474 | basic_ostream<_CharT, _Traits>& |
| 5475 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5476 | const negative_binomial_distribution<_IntType>& __x) |
| 5477 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5478 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5479 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5480 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5481 | _OStream::scientific); |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5482 | _CharT __sp = __os.widen(' '); |
| 5483 | __os.fill(__sp); |
| 5484 | return __os << __x.k() << __sp << __x.p(); |
| 5485 | } |
| 5486 | |
| 5487 | template <class _CharT, class _Traits, class _IntType> |
| 5488 | basic_istream<_CharT, _Traits>& |
| 5489 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5490 | negative_binomial_distribution<_IntType>& __x) |
| 5491 | { |
| 5492 | typedef negative_binomial_distribution<_IntType> _Eng; |
| 5493 | typedef typename _Eng::result_type result_type; |
| 5494 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5495 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5496 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5497 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5498 | result_type __k; |
| 5499 | double __p; |
| 5500 | __is >> __k >> __p; |
| 5501 | if (!__is.fail()) |
| 5502 | __x.param(param_type(__k, __p)); |
| 5503 | return __is; |
| 5504 | } |
| 5505 | |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5506 | // geometric_distribution |
| 5507 | |
| 5508 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5509 | class _LIBCPP_TEMPLATE_VIS geometric_distribution |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5510 | { |
| 5511 | public: |
| 5512 | // types |
| 5513 | typedef _IntType result_type; |
| 5514 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5515 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5516 | { |
| 5517 | double __p_; |
| 5518 | public: |
| 5519 | typedef geometric_distribution distribution_type; |
| 5520 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5521 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5522 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 5523 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5524 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5525 | double p() const {return __p_;} |
| 5526 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5527 | friend _LIBCPP_INLINE_VISIBILITY |
| 5528 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5529 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5530 | friend _LIBCPP_INLINE_VISIBILITY |
| 5531 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5532 | {return !(__x == __y);} |
| 5533 | }; |
| 5534 | |
| 5535 | private: |
| 5536 | param_type __p_; |
| 5537 | |
| 5538 | public: |
| 5539 | // constructors and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5540 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5541 | _LIBCPP_INLINE_VISIBILITY |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5542 | geometric_distribution() : geometric_distribution(0.5) {} |
| 5543 | _LIBCPP_INLINE_VISIBILITY |
| 5544 | explicit geometric_distribution(double __p) |
| 5545 | : __p_(__p) {} |
| 5546 | #else |
| 5547 | _LIBCPP_INLINE_VISIBILITY |
| 5548 | explicit geometric_distribution(double __p = 0.5) |
| 5549 | : __p_(__p) {} |
| 5550 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5551 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5552 | explicit geometric_distribution(const param_type& __p) : __p_(__p) {} |
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 | void reset() {} |
| 5555 | |
| 5556 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5557 | template<class _URNG> |
| 5558 | _LIBCPP_INLINE_VISIBILITY |
| 5559 | result_type operator()(_URNG& __g) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5560 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5561 | template<class _URNG> |
| 5562 | _LIBCPP_INLINE_VISIBILITY |
| 5563 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5564 | {return negative_binomial_distribution<result_type>(1, __p.p())(__g);} |
| 5565 | |
| 5566 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5567 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5568 | double p() const {return __p_.p();} |
| 5569 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5570 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5571 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5572 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5573 | void param(const param_type& __p) {__p_ = __p;} |
| 5574 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5575 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5576 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5577 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5578 | result_type max() const {return numeric_limits<result_type>::max();} |
| 5579 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5580 | friend _LIBCPP_INLINE_VISIBILITY |
| 5581 | bool operator==(const geometric_distribution& __x, |
| 5582 | const geometric_distribution& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5583 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5584 | friend _LIBCPP_INLINE_VISIBILITY |
| 5585 | bool operator!=(const geometric_distribution& __x, |
| 5586 | const geometric_distribution& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5587 | {return !(__x == __y);} |
| 5588 | }; |
| 5589 | |
| 5590 | template <class _CharT, class _Traits, class _IntType> |
| 5591 | basic_ostream<_CharT, _Traits>& |
| 5592 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5593 | const geometric_distribution<_IntType>& __x) |
| 5594 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5595 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5596 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5597 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5598 | _OStream::scientific); |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5599 | return __os << __x.p(); |
| 5600 | } |
| 5601 | |
| 5602 | template <class _CharT, class _Traits, class _IntType> |
| 5603 | basic_istream<_CharT, _Traits>& |
| 5604 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5605 | geometric_distribution<_IntType>& __x) |
| 5606 | { |
| 5607 | typedef geometric_distribution<_IntType> _Eng; |
| 5608 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5609 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5610 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5611 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5612 | double __p; |
| 5613 | __is >> __p; |
| 5614 | if (!__is.fail()) |
| 5615 | __x.param(param_type(__p)); |
| 5616 | return __is; |
| 5617 | } |
| 5618 | |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5619 | // chi_squared_distribution |
| 5620 | |
| 5621 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5622 | class _LIBCPP_TEMPLATE_VIS chi_squared_distribution |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5623 | { |
| 5624 | public: |
| 5625 | // types |
| 5626 | typedef _RealType result_type; |
| 5627 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5628 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5629 | { |
| 5630 | result_type __n_; |
| 5631 | public: |
| 5632 | typedef chi_squared_distribution distribution_type; |
| 5633 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5634 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5635 | explicit param_type(result_type __n = 1) : __n_(__n) {} |
| 5636 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5637 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5638 | result_type n() const {return __n_;} |
| 5639 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5640 | friend _LIBCPP_INLINE_VISIBILITY |
| 5641 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5642 | {return __x.__n_ == __y.__n_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5643 | friend _LIBCPP_INLINE_VISIBILITY |
| 5644 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5645 | {return !(__x == __y);} |
| 5646 | }; |
| 5647 | |
| 5648 | private: |
| 5649 | param_type __p_; |
| 5650 | |
| 5651 | public: |
| 5652 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5653 | #ifndef _LIBCPP_CXX03_LANG |
| 5654 | _LIBCPP_INLINE_VISIBILITY |
| 5655 | chi_squared_distribution() : chi_squared_distribution(1) {} |
| 5656 | _LIBCPP_INLINE_VISIBILITY |
| 5657 | explicit chi_squared_distribution(result_type __n) |
| 5658 | : __p_(param_type(__n)) {} |
| 5659 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5660 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5661 | explicit chi_squared_distribution(result_type __n = 1) |
| 5662 | : __p_(param_type(__n)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5663 | #endif |
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 | explicit chi_squared_distribution(const param_type& __p) |
| 5666 | : __p_(__p) {} |
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 | void reset() {} |
| 5669 | |
| 5670 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5671 | template<class _URNG> |
| 5672 | _LIBCPP_INLINE_VISIBILITY |
| 5673 | result_type operator()(_URNG& __g) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5674 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5675 | template<class _URNG> |
| 5676 | _LIBCPP_INLINE_VISIBILITY |
| 5677 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5678 | {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);} |
| 5679 | |
| 5680 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5681 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5682 | result_type n() const {return __p_.n();} |
| 5683 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5684 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5685 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5686 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5687 | void param(const param_type& __p) {__p_ = __p;} |
| 5688 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5689 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5690 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5691 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5692 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5693 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5694 | friend _LIBCPP_INLINE_VISIBILITY |
| 5695 | bool operator==(const chi_squared_distribution& __x, |
| 5696 | const chi_squared_distribution& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5697 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5698 | friend _LIBCPP_INLINE_VISIBILITY |
| 5699 | bool operator!=(const chi_squared_distribution& __x, |
| 5700 | const chi_squared_distribution& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5701 | {return !(__x == __y);} |
| 5702 | }; |
| 5703 | |
| 5704 | template <class _CharT, class _Traits, class _RT> |
| 5705 | basic_ostream<_CharT, _Traits>& |
| 5706 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5707 | const chi_squared_distribution<_RT>& __x) |
| 5708 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5709 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5710 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5711 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5712 | _OStream::scientific); |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5713 | __os << __x.n(); |
| 5714 | return __os; |
| 5715 | } |
| 5716 | |
| 5717 | template <class _CharT, class _Traits, class _RT> |
| 5718 | basic_istream<_CharT, _Traits>& |
| 5719 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5720 | chi_squared_distribution<_RT>& __x) |
| 5721 | { |
| 5722 | typedef chi_squared_distribution<_RT> _Eng; |
| 5723 | typedef typename _Eng::result_type result_type; |
| 5724 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5725 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5726 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5727 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5728 | result_type __n; |
| 5729 | __is >> __n; |
| 5730 | if (!__is.fail()) |
| 5731 | __x.param(param_type(__n)); |
| 5732 | return __is; |
| 5733 | } |
| 5734 | |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5735 | // cauchy_distribution |
| 5736 | |
| 5737 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5738 | class _LIBCPP_TEMPLATE_VIS cauchy_distribution |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5739 | { |
| 5740 | public: |
| 5741 | // types |
| 5742 | typedef _RealType result_type; |
| 5743 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5744 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5745 | { |
| 5746 | result_type __a_; |
| 5747 | result_type __b_; |
| 5748 | public: |
| 5749 | typedef cauchy_distribution distribution_type; |
| 5750 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5751 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5752 | explicit param_type(result_type __a = 0, result_type __b = 1) |
| 5753 | : __a_(__a), __b_(__b) {} |
| 5754 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5755 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5756 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5757 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5758 | result_type b() const {return __b_;} |
| 5759 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5760 | friend _LIBCPP_INLINE_VISIBILITY |
| 5761 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5762 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5763 | friend _LIBCPP_INLINE_VISIBILITY |
| 5764 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5765 | {return !(__x == __y);} |
| 5766 | }; |
| 5767 | |
| 5768 | private: |
| 5769 | param_type __p_; |
| 5770 | |
| 5771 | public: |
| 5772 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5773 | #ifndef _LIBCPP_CXX03_LANG |
| 5774 | _LIBCPP_INLINE_VISIBILITY |
| 5775 | cauchy_distribution() : cauchy_distribution(0) {} |
| 5776 | _LIBCPP_INLINE_VISIBILITY |
| 5777 | explicit cauchy_distribution(result_type __a, result_type __b = 1) |
| 5778 | : __p_(param_type(__a, __b)) {} |
| 5779 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5780 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5781 | explicit cauchy_distribution(result_type __a = 0, result_type __b = 1) |
| 5782 | : __p_(param_type(__a, __b)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5783 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5784 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5785 | explicit cauchy_distribution(const param_type& __p) |
| 5786 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5787 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5788 | void reset() {} |
| 5789 | |
| 5790 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5791 | template<class _URNG> |
| 5792 | _LIBCPP_INLINE_VISIBILITY |
| 5793 | result_type operator()(_URNG& __g) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5794 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 5795 | 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] | 5796 | |
| 5797 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5798 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5799 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5800 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5801 | result_type b() const {return __p_.b();} |
| 5802 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5803 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5804 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5805 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5806 | void param(const param_type& __p) {__p_ = __p;} |
| 5807 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5808 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5809 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5810 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5811 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5812 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5813 | friend _LIBCPP_INLINE_VISIBILITY |
| 5814 | bool operator==(const cauchy_distribution& __x, |
| 5815 | const cauchy_distribution& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5816 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5817 | friend _LIBCPP_INLINE_VISIBILITY |
| 5818 | bool operator!=(const cauchy_distribution& __x, |
| 5819 | const cauchy_distribution& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5820 | {return !(__x == __y);} |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5821 | }; |
| 5822 | |
| 5823 | template <class _RealType> |
| 5824 | template<class _URNG> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 5825 | inline |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5826 | _RealType |
| 5827 | cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5828 | { |
| 5829 | uniform_real_distribution<result_type> __gen; |
| 5830 | // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5831 | return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g)); |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5832 | } |
| 5833 | |
| 5834 | template <class _CharT, class _Traits, class _RT> |
| 5835 | basic_ostream<_CharT, _Traits>& |
| 5836 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5837 | const cauchy_distribution<_RT>& __x) |
| 5838 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5839 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5840 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5841 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5842 | _OStream::scientific); |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5843 | _CharT __sp = __os.widen(' '); |
| 5844 | __os.fill(__sp); |
| 5845 | __os << __x.a() << __sp << __x.b(); |
| 5846 | return __os; |
| 5847 | } |
| 5848 | |
| 5849 | template <class _CharT, class _Traits, class _RT> |
| 5850 | basic_istream<_CharT, _Traits>& |
| 5851 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5852 | cauchy_distribution<_RT>& __x) |
| 5853 | { |
| 5854 | typedef cauchy_distribution<_RT> _Eng; |
| 5855 | typedef typename _Eng::result_type result_type; |
| 5856 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5857 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5858 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5859 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5860 | result_type __a; |
| 5861 | result_type __b; |
| 5862 | __is >> __a >> __b; |
| 5863 | if (!__is.fail()) |
| 5864 | __x.param(param_type(__a, __b)); |
| 5865 | return __is; |
| 5866 | } |
| 5867 | |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5868 | // fisher_f_distribution |
| 5869 | |
| 5870 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5871 | class _LIBCPP_TEMPLATE_VIS fisher_f_distribution |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5872 | { |
| 5873 | public: |
| 5874 | // types |
| 5875 | typedef _RealType result_type; |
| 5876 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5877 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5878 | { |
| 5879 | result_type __m_; |
| 5880 | result_type __n_; |
| 5881 | public: |
| 5882 | typedef fisher_f_distribution distribution_type; |
| 5883 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5884 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5885 | explicit param_type(result_type __m = 1, result_type __n = 1) |
| 5886 | : __m_(__m), __n_(__n) {} |
| 5887 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5888 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5889 | result_type m() const {return __m_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5890 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5891 | result_type n() const {return __n_;} |
| 5892 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5893 | friend _LIBCPP_INLINE_VISIBILITY |
| 5894 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5895 | {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5896 | friend _LIBCPP_INLINE_VISIBILITY |
| 5897 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5898 | {return !(__x == __y);} |
| 5899 | }; |
| 5900 | |
| 5901 | private: |
| 5902 | param_type __p_; |
| 5903 | |
| 5904 | public: |
| 5905 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5906 | #ifndef _LIBCPP_CXX03_LANG |
| 5907 | _LIBCPP_INLINE_VISIBILITY |
| 5908 | fisher_f_distribution() : fisher_f_distribution(1) {} |
| 5909 | _LIBCPP_INLINE_VISIBILITY |
| 5910 | explicit fisher_f_distribution(result_type __m, result_type __n = 1) |
| 5911 | : __p_(param_type(__m, __n)) {} |
| 5912 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5913 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5914 | explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1) |
| 5915 | : __p_(param_type(__m, __n)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 5916 | #endif |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5917 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5918 | explicit fisher_f_distribution(const param_type& __p) |
| 5919 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5920 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5921 | void reset() {} |
| 5922 | |
| 5923 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5924 | template<class _URNG> |
| 5925 | _LIBCPP_INLINE_VISIBILITY |
| 5926 | result_type operator()(_URNG& __g) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5927 | {return (*this)(__g, __p_);} |
| 5928 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5929 | |
| 5930 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5931 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5932 | result_type m() const {return __p_.m();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5933 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5934 | result_type n() const {return __p_.n();} |
| 5935 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5936 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5937 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5938 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5939 | void param(const param_type& __p) {__p_ = __p;} |
| 5940 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5941 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5942 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5943 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5944 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5945 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5946 | friend _LIBCPP_INLINE_VISIBILITY |
| 5947 | bool operator==(const fisher_f_distribution& __x, |
| 5948 | const fisher_f_distribution& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5949 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5950 | friend _LIBCPP_INLINE_VISIBILITY |
| 5951 | bool operator!=(const fisher_f_distribution& __x, |
| 5952 | const fisher_f_distribution& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5953 | {return !(__x == __y);} |
| 5954 | }; |
| 5955 | |
| 5956 | template <class _RealType> |
| 5957 | template<class _URNG> |
| 5958 | _RealType |
| 5959 | fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5960 | { |
| 5961 | gamma_distribution<result_type> __gdm(__p.m() * result_type(.5)); |
| 5962 | gamma_distribution<result_type> __gdn(__p.n() * result_type(.5)); |
| 5963 | return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g)); |
| 5964 | } |
| 5965 | |
| 5966 | template <class _CharT, class _Traits, class _RT> |
| 5967 | basic_ostream<_CharT, _Traits>& |
| 5968 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5969 | const fisher_f_distribution<_RT>& __x) |
| 5970 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5971 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5972 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 5973 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 5974 | _OStream::scientific); |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5975 | _CharT __sp = __os.widen(' '); |
| 5976 | __os.fill(__sp); |
| 5977 | __os << __x.m() << __sp << __x.n(); |
| 5978 | return __os; |
| 5979 | } |
| 5980 | |
| 5981 | template <class _CharT, class _Traits, class _RT> |
| 5982 | basic_istream<_CharT, _Traits>& |
| 5983 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5984 | fisher_f_distribution<_RT>& __x) |
| 5985 | { |
| 5986 | typedef fisher_f_distribution<_RT> _Eng; |
| 5987 | typedef typename _Eng::result_type result_type; |
| 5988 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5989 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 5990 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 5991 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5992 | result_type __m; |
| 5993 | result_type __n; |
| 5994 | __is >> __m >> __n; |
| 5995 | if (!__is.fail()) |
| 5996 | __x.param(param_type(__m, __n)); |
| 5997 | return __is; |
| 5998 | } |
| 5999 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6000 | // student_t_distribution |
| 6001 | |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6002 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6003 | class _LIBCPP_TEMPLATE_VIS student_t_distribution |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6004 | { |
| 6005 | public: |
| 6006 | // types |
| 6007 | typedef _RealType result_type; |
| 6008 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6009 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6010 | { |
| 6011 | result_type __n_; |
| 6012 | public: |
| 6013 | typedef student_t_distribution distribution_type; |
| 6014 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6015 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6016 | explicit param_type(result_type __n = 1) : __n_(__n) {} |
| 6017 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6018 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6019 | result_type n() const {return __n_;} |
| 6020 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6021 | friend _LIBCPP_INLINE_VISIBILITY |
| 6022 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6023 | {return __x.__n_ == __y.__n_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6024 | friend _LIBCPP_INLINE_VISIBILITY |
| 6025 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6026 | {return !(__x == __y);} |
| 6027 | }; |
| 6028 | |
| 6029 | private: |
| 6030 | param_type __p_; |
| 6031 | normal_distribution<result_type> __nd_; |
| 6032 | |
| 6033 | public: |
| 6034 | // constructor and reset functions |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 6035 | #ifndef _LIBCPP_CXX03_LANG |
| 6036 | _LIBCPP_INLINE_VISIBILITY |
| 6037 | student_t_distribution() : student_t_distribution(1) {} |
| 6038 | _LIBCPP_INLINE_VISIBILITY |
| 6039 | explicit student_t_distribution(result_type __n) |
| 6040 | : __p_(param_type(__n)) {} |
| 6041 | #else |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6042 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6043 | explicit student_t_distribution(result_type __n = 1) |
| 6044 | : __p_(param_type(__n)) {} |
Marek Kurdej | cd0bd6a | 2021-01-19 08:21:09 +0100 | [diff] [blame^] | 6045 | #endif |
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 | explicit student_t_distribution(const param_type& __p) |
| 6048 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6049 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6050 | void reset() {__nd_.reset();} |
| 6051 | |
| 6052 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6053 | template<class _URNG> |
| 6054 | _LIBCPP_INLINE_VISIBILITY |
| 6055 | result_type operator()(_URNG& __g) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6056 | {return (*this)(__g, __p_);} |
| 6057 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6058 | |
| 6059 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6060 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6061 | result_type n() const {return __p_.n();} |
| 6062 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6063 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6064 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6065 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6066 | void param(const param_type& __p) {__p_ = __p;} |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6067 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6068 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6069 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6070 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6071 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 6072 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6073 | friend _LIBCPP_INLINE_VISIBILITY |
| 6074 | bool operator==(const student_t_distribution& __x, |
| 6075 | const student_t_distribution& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6076 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6077 | friend _LIBCPP_INLINE_VISIBILITY |
| 6078 | bool operator!=(const student_t_distribution& __x, |
| 6079 | const student_t_distribution& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6080 | {return !(__x == __y);} |
| 6081 | }; |
| 6082 | |
| 6083 | template <class _RealType> |
| 6084 | template<class _URNG> |
| 6085 | _RealType |
| 6086 | student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6087 | { |
| 6088 | gamma_distribution<result_type> __gd(__p.n() * .5, 2); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6089 | return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g)); |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6090 | } |
| 6091 | |
| 6092 | template <class _CharT, class _Traits, class _RT> |
| 6093 | basic_ostream<_CharT, _Traits>& |
| 6094 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6095 | const student_t_distribution<_RT>& __x) |
| 6096 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6097 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6098 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 6099 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 6100 | _OStream::scientific); |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6101 | __os << __x.n(); |
| 6102 | return __os; |
| 6103 | } |
| 6104 | |
| 6105 | template <class _CharT, class _Traits, class _RT> |
| 6106 | basic_istream<_CharT, _Traits>& |
| 6107 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6108 | student_t_distribution<_RT>& __x) |
| 6109 | { |
| 6110 | typedef student_t_distribution<_RT> _Eng; |
| 6111 | typedef typename _Eng::result_type result_type; |
| 6112 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6113 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6114 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 6115 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 6116 | result_type __n; |
| 6117 | __is >> __n; |
| 6118 | if (!__is.fail()) |
| 6119 | __x.param(param_type(__n)); |
| 6120 | return __is; |
| 6121 | } |
| 6122 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6123 | // discrete_distribution |
| 6124 | |
| 6125 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6126 | class _LIBCPP_TEMPLATE_VIS discrete_distribution |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6127 | { |
| 6128 | public: |
| 6129 | // types |
| 6130 | typedef _IntType result_type; |
| 6131 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6132 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6133 | { |
| 6134 | vector<double> __p_; |
| 6135 | public: |
| 6136 | typedef discrete_distribution distribution_type; |
| 6137 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6138 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6139 | param_type() {} |
| 6140 | template<class _InputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6141 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6142 | param_type(_InputIterator __f, _InputIterator __l) |
| 6143 | : __p_(__f, __l) {__init();} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6144 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6145 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6146 | param_type(initializer_list<double> __wl) |
| 6147 | : __p_(__wl.begin(), __wl.end()) {__init();} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6148 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6149 | template<class _UnaryOperation> |
| 6150 | param_type(size_t __nw, double __xmin, double __xmax, |
| 6151 | _UnaryOperation __fw); |
| 6152 | |
| 6153 | vector<double> probabilities() const; |
| 6154 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6155 | friend _LIBCPP_INLINE_VISIBILITY |
| 6156 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6157 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6158 | friend _LIBCPP_INLINE_VISIBILITY |
| 6159 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6160 | {return !(__x == __y);} |
| 6161 | |
| 6162 | private: |
| 6163 | void __init(); |
| 6164 | |
| 6165 | friend class discrete_distribution; |
| 6166 | |
| 6167 | template <class _CharT, class _Traits, class _IT> |
| 6168 | friend |
| 6169 | basic_ostream<_CharT, _Traits>& |
| 6170 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6171 | const discrete_distribution<_IT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6172 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6173 | template <class _CharT, class _Traits, class _IT> |
| 6174 | friend |
| 6175 | basic_istream<_CharT, _Traits>& |
| 6176 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6177 | discrete_distribution<_IT>& __x); |
| 6178 | }; |
| 6179 | |
| 6180 | private: |
| 6181 | param_type __p_; |
| 6182 | |
| 6183 | public: |
| 6184 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6185 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6186 | discrete_distribution() {} |
| 6187 | template<class _InputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6188 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6189 | discrete_distribution(_InputIterator __f, _InputIterator __l) |
| 6190 | : __p_(__f, __l) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6191 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6192 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6193 | discrete_distribution(initializer_list<double> __wl) |
| 6194 | : __p_(__wl) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6195 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6196 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6197 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6198 | discrete_distribution(size_t __nw, double __xmin, double __xmax, |
| 6199 | _UnaryOperation __fw) |
| 6200 | : __p_(__nw, __xmin, __xmax, __fw) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6201 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ea38295 | 2013-08-14 18:00:20 +0000 | [diff] [blame] | 6202 | explicit discrete_distribution(const param_type& __p) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6203 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6204 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6205 | void reset() {} |
| 6206 | |
| 6207 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6208 | template<class _URNG> |
| 6209 | _LIBCPP_INLINE_VISIBILITY |
| 6210 | result_type operator()(_URNG& __g) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6211 | {return (*this)(__g, __p_);} |
| 6212 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6213 | |
| 6214 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6215 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6216 | vector<double> probabilities() const {return __p_.probabilities();} |
| 6217 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6218 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6219 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6220 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6221 | void param(const param_type& __p) {__p_ = __p;} |
| 6222 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6223 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6224 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6225 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6226 | result_type max() const {return __p_.__p_.size();} |
| 6227 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6228 | friend _LIBCPP_INLINE_VISIBILITY |
| 6229 | bool operator==(const discrete_distribution& __x, |
| 6230 | const discrete_distribution& __y) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6231 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6232 | friend _LIBCPP_INLINE_VISIBILITY |
| 6233 | bool operator!=(const discrete_distribution& __x, |
| 6234 | const discrete_distribution& __y) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6235 | {return !(__x == __y);} |
| 6236 | |
| 6237 | template <class _CharT, class _Traits, class _IT> |
| 6238 | friend |
| 6239 | basic_ostream<_CharT, _Traits>& |
| 6240 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6241 | const discrete_distribution<_IT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6242 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6243 | template <class _CharT, class _Traits, class _IT> |
| 6244 | friend |
| 6245 | basic_istream<_CharT, _Traits>& |
| 6246 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6247 | discrete_distribution<_IT>& __x); |
| 6248 | }; |
| 6249 | |
| 6250 | template<class _IntType> |
| 6251 | template<class _UnaryOperation> |
| 6252 | discrete_distribution<_IntType>::param_type::param_type(size_t __nw, |
| 6253 | double __xmin, |
| 6254 | double __xmax, |
| 6255 | _UnaryOperation __fw) |
| 6256 | { |
| 6257 | if (__nw > 1) |
| 6258 | { |
| 6259 | __p_.reserve(__nw - 1); |
| 6260 | double __d = (__xmax - __xmin) / __nw; |
| 6261 | double __d2 = __d / 2; |
| 6262 | for (size_t __k = 0; __k < __nw; ++__k) |
| 6263 | __p_.push_back(__fw(__xmin + __k * __d + __d2)); |
| 6264 | __init(); |
| 6265 | } |
| 6266 | } |
| 6267 | |
| 6268 | template<class _IntType> |
| 6269 | void |
| 6270 | discrete_distribution<_IntType>::param_type::__init() |
| 6271 | { |
| 6272 | if (!__p_.empty()) |
| 6273 | { |
| 6274 | if (__p_.size() > 1) |
| 6275 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6276 | double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0); |
| 6277 | for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end(); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6278 | __i < __e; ++__i) |
| 6279 | *__i /= __s; |
| 6280 | vector<double> __t(__p_.size() - 1); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6281 | _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin()); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6282 | swap(__p_, __t); |
| 6283 | } |
| 6284 | else |
| 6285 | { |
| 6286 | __p_.clear(); |
| 6287 | __p_.shrink_to_fit(); |
| 6288 | } |
| 6289 | } |
| 6290 | } |
| 6291 | |
| 6292 | template<class _IntType> |
| 6293 | vector<double> |
| 6294 | discrete_distribution<_IntType>::param_type::probabilities() const |
| 6295 | { |
| 6296 | size_t __n = __p_.size(); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6297 | _VSTD::vector<double> __p(__n+1); |
| 6298 | _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin()); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6299 | if (__n > 0) |
| 6300 | __p[__n] = 1 - __p_[__n-1]; |
| 6301 | else |
| 6302 | __p[0] = 1; |
| 6303 | return __p; |
| 6304 | } |
| 6305 | |
| 6306 | template<class _IntType> |
| 6307 | template<class _URNG> |
| 6308 | _IntType |
| 6309 | discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) |
| 6310 | { |
| 6311 | uniform_real_distribution<double> __gen; |
| 6312 | return static_cast<_IntType>( |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6313 | _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) - |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6314 | __p.__p_.begin()); |
| 6315 | } |
| 6316 | |
| 6317 | template <class _CharT, class _Traits, class _IT> |
| 6318 | basic_ostream<_CharT, _Traits>& |
| 6319 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6320 | const discrete_distribution<_IT>& __x) |
| 6321 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6322 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6323 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 6324 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 6325 | _OStream::scientific); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6326 | _CharT __sp = __os.widen(' '); |
| 6327 | __os.fill(__sp); |
| 6328 | size_t __n = __x.__p_.__p_.size(); |
| 6329 | __os << __n; |
| 6330 | for (size_t __i = 0; __i < __n; ++__i) |
| 6331 | __os << __sp << __x.__p_.__p_[__i]; |
| 6332 | return __os; |
| 6333 | } |
| 6334 | |
| 6335 | template <class _CharT, class _Traits, class _IT> |
| 6336 | basic_istream<_CharT, _Traits>& |
| 6337 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6338 | discrete_distribution<_IT>& __x) |
| 6339 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6340 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6341 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 6342 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6343 | size_t __n; |
| 6344 | __is >> __n; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6345 | vector<double> __p(__n); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6346 | for (size_t __i = 0; __i < __n; ++__i) |
| 6347 | __is >> __p[__i]; |
| 6348 | if (!__is.fail()) |
| 6349 | swap(__x.__p_.__p_, __p); |
| 6350 | return __is; |
| 6351 | } |
| 6352 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6353 | // piecewise_constant_distribution |
| 6354 | |
| 6355 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6356 | class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6357 | { |
| 6358 | public: |
| 6359 | // types |
| 6360 | typedef _RealType result_type; |
| 6361 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6362 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6363 | { |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6364 | vector<result_type> __b_; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6365 | vector<result_type> __densities_; |
| 6366 | vector<result_type> __areas_; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6367 | public: |
| 6368 | typedef piecewise_constant_distribution distribution_type; |
| 6369 | |
| 6370 | param_type(); |
| 6371 | template<class _InputIteratorB, class _InputIteratorW> |
| 6372 | param_type(_InputIteratorB __fB, _InputIteratorB __lB, |
| 6373 | _InputIteratorW __fW); |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6374 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6375 | template<class _UnaryOperation> |
| 6376 | param_type(initializer_list<result_type> __bl, _UnaryOperation __fw); |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6377 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6378 | template<class _UnaryOperation> |
| 6379 | param_type(size_t __nw, result_type __xmin, result_type __xmax, |
| 6380 | _UnaryOperation __fw); |
Eric Fiselier | b9f37ca | 2019-12-12 20:48:11 -0500 | [diff] [blame] | 6381 | param_type(param_type const&) = default; |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6382 | param_type & operator=(const param_type& __rhs); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6383 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6384 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6385 | vector<result_type> intervals() const {return __b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6386 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6387 | vector<result_type> densities() const {return __densities_;} |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6388 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6389 | friend _LIBCPP_INLINE_VISIBILITY |
| 6390 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6391 | {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6392 | friend _LIBCPP_INLINE_VISIBILITY |
| 6393 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6394 | {return !(__x == __y);} |
| 6395 | |
| 6396 | private: |
| 6397 | void __init(); |
| 6398 | |
| 6399 | friend class piecewise_constant_distribution; |
| 6400 | |
| 6401 | template <class _CharT, class _Traits, class _RT> |
| 6402 | friend |
| 6403 | basic_ostream<_CharT, _Traits>& |
| 6404 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6405 | const piecewise_constant_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6406 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6407 | template <class _CharT, class _Traits, class _RT> |
| 6408 | friend |
| 6409 | basic_istream<_CharT, _Traits>& |
| 6410 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6411 | piecewise_constant_distribution<_RT>& __x); |
| 6412 | }; |
| 6413 | |
| 6414 | private: |
| 6415 | param_type __p_; |
| 6416 | |
| 6417 | public: |
| 6418 | // constructor and reset functions |
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() {} |
| 6421 | template<class _InputIteratorB, class _InputIteratorW> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6422 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6423 | piecewise_constant_distribution(_InputIteratorB __fB, |
| 6424 | _InputIteratorB __lB, |
| 6425 | _InputIteratorW __fW) |
| 6426 | : __p_(__fB, __lB, __fW) {} |
| 6427 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6428 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6429 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6430 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6431 | piecewise_constant_distribution(initializer_list<result_type> __bl, |
| 6432 | _UnaryOperation __fw) |
| 6433 | : __p_(__bl, __fw) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6434 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6435 | |
| 6436 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6437 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6438 | piecewise_constant_distribution(size_t __nw, result_type __xmin, |
| 6439 | result_type __xmax, _UnaryOperation __fw) |
| 6440 | : __p_(__nw, __xmin, __xmax, __fw) {} |
| 6441 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6442 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6443 | explicit piecewise_constant_distribution(const param_type& __p) |
| 6444 | : __p_(__p) {} |
| 6445 | |
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 reset() {} |
| 6448 | |
| 6449 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6450 | template<class _URNG> |
| 6451 | _LIBCPP_INLINE_VISIBILITY |
| 6452 | result_type operator()(_URNG& __g) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6453 | {return (*this)(__g, __p_);} |
| 6454 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6455 | |
| 6456 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6457 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6458 | vector<result_type> intervals() const {return __p_.intervals();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6459 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6460 | vector<result_type> densities() const {return __p_.densities();} |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6461 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6462 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6463 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6464 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6465 | void param(const param_type& __p) {__p_ = __p;} |
| 6466 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6467 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6468 | result_type min() const {return __p_.__b_.front();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6469 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6470 | result_type max() const {return __p_.__b_.back();} |
| 6471 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6472 | friend _LIBCPP_INLINE_VISIBILITY |
| 6473 | bool operator==(const piecewise_constant_distribution& __x, |
| 6474 | const piecewise_constant_distribution& __y) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6475 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6476 | friend _LIBCPP_INLINE_VISIBILITY |
| 6477 | bool operator!=(const piecewise_constant_distribution& __x, |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6478 | const piecewise_constant_distribution& __y) |
| 6479 | {return !(__x == __y);} |
| 6480 | |
| 6481 | template <class _CharT, class _Traits, class _RT> |
| 6482 | friend |
| 6483 | basic_ostream<_CharT, _Traits>& |
| 6484 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6485 | const piecewise_constant_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6486 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6487 | template <class _CharT, class _Traits, class _RT> |
| 6488 | friend |
| 6489 | basic_istream<_CharT, _Traits>& |
| 6490 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6491 | piecewise_constant_distribution<_RT>& __x); |
| 6492 | }; |
| 6493 | |
| 6494 | template<class _RealType> |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6495 | typename piecewise_constant_distribution<_RealType>::param_type & |
| 6496 | piecewise_constant_distribution<_RealType>::param_type::operator= |
| 6497 | (const param_type& __rhs) |
| 6498 | { |
| 6499 | // These can throw |
| 6500 | __b_.reserve (__rhs.__b_.size ()); |
| 6501 | __densities_.reserve(__rhs.__densities_.size()); |
| 6502 | __areas_.reserve (__rhs.__areas_.size()); |
| 6503 | |
| 6504 | // These can not throw |
| 6505 | __b_ = __rhs.__b_; |
| 6506 | __densities_ = __rhs.__densities_; |
| 6507 | __areas_ = __rhs.__areas_; |
| 6508 | return *this; |
| 6509 | } |
| 6510 | |
| 6511 | template<class _RealType> |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6512 | void |
| 6513 | piecewise_constant_distribution<_RealType>::param_type::__init() |
| 6514 | { |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6515 | // __densities_ contains non-normalized areas |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6516 | result_type __total_area = _VSTD::accumulate(__densities_.begin(), |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6517 | __densities_.end(), |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6518 | result_type()); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6519 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
| 6520 | __densities_[__i] /= __total_area; |
| 6521 | // __densities_ contains normalized areas |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6522 | __areas_.assign(__densities_.size(), result_type()); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6523 | _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1, |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6524 | __areas_.begin() + 1); |
| 6525 | // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1] |
| 6526 | __densities_.back() = 1 - __areas_.back(); // correct round off error |
| 6527 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
| 6528 | __densities_[__i] /= (__b_[__i+1] - __b_[__i]); |
| 6529 | // __densities_ now contains __densities_ |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6530 | } |
| 6531 | |
| 6532 | template<class _RealType> |
| 6533 | piecewise_constant_distribution<_RealType>::param_type::param_type() |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6534 | : __b_(2), |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6535 | __densities_(1, 1.0), |
| 6536 | __areas_(1, 0.0) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6537 | { |
| 6538 | __b_[1] = 1; |
| 6539 | } |
| 6540 | |
| 6541 | template<class _RealType> |
| 6542 | template<class _InputIteratorB, class _InputIteratorW> |
| 6543 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6544 | _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW) |
| 6545 | : __b_(__fB, __lB) |
| 6546 | { |
| 6547 | if (__b_.size() < 2) |
| 6548 | { |
| 6549 | __b_.resize(2); |
| 6550 | __b_[0] = 0; |
| 6551 | __b_[1] = 1; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6552 | __densities_.assign(1, 1.0); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6553 | __areas_.assign(1, 0.0); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6554 | } |
| 6555 | else |
| 6556 | { |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6557 | __densities_.reserve(__b_.size() - 1); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6558 | for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6559 | __densities_.push_back(*__fW); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6560 | __init(); |
| 6561 | } |
| 6562 | } |
| 6563 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6564 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6565 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6566 | template<class _RealType> |
| 6567 | template<class _UnaryOperation> |
| 6568 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6569 | initializer_list<result_type> __bl, _UnaryOperation __fw) |
| 6570 | : __b_(__bl.begin(), __bl.end()) |
| 6571 | { |
| 6572 | if (__b_.size() < 2) |
| 6573 | { |
| 6574 | __b_.resize(2); |
| 6575 | __b_[0] = 0; |
| 6576 | __b_[1] = 1; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6577 | __densities_.assign(1, 1.0); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6578 | __areas_.assign(1, 0.0); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6579 | } |
| 6580 | else |
| 6581 | { |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6582 | __densities_.reserve(__b_.size() - 1); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6583 | for (size_t __i = 0; __i < __b_.size() - 1; ++__i) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6584 | __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5)); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6585 | __init(); |
| 6586 | } |
| 6587 | } |
| 6588 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6589 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6590 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6591 | template<class _RealType> |
| 6592 | template<class _UnaryOperation> |
| 6593 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6594 | size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw) |
| 6595 | : __b_(__nw == 0 ? 2 : __nw + 1) |
| 6596 | { |
| 6597 | size_t __n = __b_.size() - 1; |
| 6598 | result_type __d = (__xmax - __xmin) / __n; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6599 | __densities_.reserve(__n); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6600 | for (size_t __i = 0; __i < __n; ++__i) |
| 6601 | { |
| 6602 | __b_[__i] = __xmin + __i * __d; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6603 | __densities_.push_back(__fw(__b_[__i] + __d*.5)); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6604 | } |
| 6605 | __b_[__n] = __xmax; |
| 6606 | __init(); |
| 6607 | } |
| 6608 | |
| 6609 | template<class _RealType> |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6610 | template<class _URNG> |
| 6611 | _RealType |
| 6612 | piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6613 | { |
| 6614 | typedef uniform_real_distribution<result_type> _Gen; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6615 | result_type __u = _Gen()(__g); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6616 | ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6617 | __u) - __p.__areas_.begin() - 1; |
| 6618 | return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k]; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6619 | } |
| 6620 | |
| 6621 | template <class _CharT, class _Traits, class _RT> |
| 6622 | basic_ostream<_CharT, _Traits>& |
| 6623 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6624 | const piecewise_constant_distribution<_RT>& __x) |
| 6625 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6626 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6627 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 6628 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 6629 | _OStream::scientific); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6630 | _CharT __sp = __os.widen(' '); |
| 6631 | __os.fill(__sp); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6632 | size_t __n = __x.__p_.__b_.size(); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6633 | __os << __n; |
| 6634 | for (size_t __i = 0; __i < __n; ++__i) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6635 | __os << __sp << __x.__p_.__b_[__i]; |
| 6636 | __n = __x.__p_.__densities_.size(); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6637 | __os << __sp << __n; |
| 6638 | for (size_t __i = 0; __i < __n; ++__i) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6639 | __os << __sp << __x.__p_.__densities_[__i]; |
| 6640 | __n = __x.__p_.__areas_.size(); |
| 6641 | __os << __sp << __n; |
| 6642 | for (size_t __i = 0; __i < __n; ++__i) |
| 6643 | __os << __sp << __x.__p_.__areas_[__i]; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6644 | return __os; |
| 6645 | } |
| 6646 | |
| 6647 | template <class _CharT, class _Traits, class _RT> |
| 6648 | basic_istream<_CharT, _Traits>& |
| 6649 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6650 | piecewise_constant_distribution<_RT>& __x) |
| 6651 | { |
| 6652 | typedef piecewise_constant_distribution<_RT> _Eng; |
| 6653 | typedef typename _Eng::result_type result_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6654 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6655 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 6656 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6657 | size_t __n; |
| 6658 | __is >> __n; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6659 | vector<result_type> __b(__n); |
| 6660 | for (size_t __i = 0; __i < __n; ++__i) |
| 6661 | __is >> __b[__i]; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6662 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6663 | vector<result_type> __densities(__n); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6664 | for (size_t __i = 0; __i < __n; ++__i) |
| 6665 | __is >> __densities[__i]; |
| 6666 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6667 | vector<result_type> __areas(__n); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6668 | for (size_t __i = 0; __i < __n; ++__i) |
| 6669 | __is >> __areas[__i]; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6670 | if (!__is.fail()) |
| 6671 | { |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6672 | swap(__x.__p_.__b_, __b); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6673 | swap(__x.__p_.__densities_, __densities); |
| 6674 | swap(__x.__p_.__areas_, __areas); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6675 | } |
| 6676 | return __is; |
| 6677 | } |
| 6678 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6679 | // piecewise_linear_distribution |
| 6680 | |
| 6681 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6682 | class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6683 | { |
| 6684 | public: |
| 6685 | // types |
| 6686 | typedef _RealType result_type; |
| 6687 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6688 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6689 | { |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6690 | vector<result_type> __b_; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6691 | vector<result_type> __densities_; |
| 6692 | vector<result_type> __areas_; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6693 | public: |
| 6694 | typedef piecewise_linear_distribution distribution_type; |
| 6695 | |
| 6696 | param_type(); |
| 6697 | template<class _InputIteratorB, class _InputIteratorW> |
| 6698 | param_type(_InputIteratorB __fB, _InputIteratorB __lB, |
| 6699 | _InputIteratorW __fW); |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6700 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6701 | template<class _UnaryOperation> |
| 6702 | param_type(initializer_list<result_type> __bl, _UnaryOperation __fw); |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6703 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6704 | template<class _UnaryOperation> |
| 6705 | param_type(size_t __nw, result_type __xmin, result_type __xmax, |
| 6706 | _UnaryOperation __fw); |
Eric Fiselier | b9f37ca | 2019-12-12 20:48:11 -0500 | [diff] [blame] | 6707 | param_type(param_type const&) = default; |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6708 | param_type & operator=(const param_type& __rhs); |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 6709 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6710 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6711 | vector<result_type> intervals() const {return __b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6712 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6713 | vector<result_type> densities() const {return __densities_;} |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6714 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6715 | friend _LIBCPP_INLINE_VISIBILITY |
| 6716 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6717 | {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6718 | friend _LIBCPP_INLINE_VISIBILITY |
| 6719 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6720 | {return !(__x == __y);} |
| 6721 | |
| 6722 | private: |
| 6723 | void __init(); |
| 6724 | |
| 6725 | friend class piecewise_linear_distribution; |
| 6726 | |
| 6727 | template <class _CharT, class _Traits, class _RT> |
| 6728 | friend |
| 6729 | basic_ostream<_CharT, _Traits>& |
| 6730 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6731 | const piecewise_linear_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6732 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6733 | template <class _CharT, class _Traits, class _RT> |
| 6734 | friend |
| 6735 | basic_istream<_CharT, _Traits>& |
| 6736 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6737 | piecewise_linear_distribution<_RT>& __x); |
| 6738 | }; |
| 6739 | |
| 6740 | private: |
| 6741 | param_type __p_; |
| 6742 | |
| 6743 | public: |
| 6744 | // constructor and reset functions |
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() {} |
| 6747 | template<class _InputIteratorB, class _InputIteratorW> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6748 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6749 | piecewise_linear_distribution(_InputIteratorB __fB, |
| 6750 | _InputIteratorB __lB, |
| 6751 | _InputIteratorW __fW) |
| 6752 | : __p_(__fB, __lB, __fW) {} |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6753 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6754 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6755 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6756 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6757 | piecewise_linear_distribution(initializer_list<result_type> __bl, |
| 6758 | _UnaryOperation __fw) |
| 6759 | : __p_(__bl, __fw) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6760 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6761 | |
| 6762 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6763 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6764 | piecewise_linear_distribution(size_t __nw, result_type __xmin, |
| 6765 | result_type __xmax, _UnaryOperation __fw) |
| 6766 | : __p_(__nw, __xmin, __xmax, __fw) {} |
| 6767 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6768 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6769 | explicit piecewise_linear_distribution(const param_type& __p) |
| 6770 | : __p_(__p) {} |
| 6771 | |
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 reset() {} |
| 6774 | |
| 6775 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6776 | template<class _URNG> |
| 6777 | _LIBCPP_INLINE_VISIBILITY |
| 6778 | result_type operator()(_URNG& __g) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6779 | {return (*this)(__g, __p_);} |
| 6780 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6781 | |
| 6782 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6783 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6784 | vector<result_type> intervals() const {return __p_.intervals();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6785 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6786 | vector<result_type> densities() const {return __p_.densities();} |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6787 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6788 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6789 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6790 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6791 | void param(const param_type& __p) {__p_ = __p;} |
| 6792 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6793 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6794 | result_type min() const {return __p_.__b_.front();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6795 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6796 | result_type max() const {return __p_.__b_.back();} |
| 6797 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6798 | friend _LIBCPP_INLINE_VISIBILITY |
| 6799 | bool operator==(const piecewise_linear_distribution& __x, |
| 6800 | const piecewise_linear_distribution& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6801 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6802 | friend _LIBCPP_INLINE_VISIBILITY |
| 6803 | bool operator!=(const piecewise_linear_distribution& __x, |
| 6804 | const piecewise_linear_distribution& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6805 | {return !(__x == __y);} |
| 6806 | |
| 6807 | template <class _CharT, class _Traits, class _RT> |
| 6808 | friend |
| 6809 | basic_ostream<_CharT, _Traits>& |
| 6810 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6811 | const piecewise_linear_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6812 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6813 | template <class _CharT, class _Traits, class _RT> |
| 6814 | friend |
| 6815 | basic_istream<_CharT, _Traits>& |
| 6816 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6817 | piecewise_linear_distribution<_RT>& __x); |
| 6818 | }; |
| 6819 | |
| 6820 | template<class _RealType> |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6821 | typename piecewise_linear_distribution<_RealType>::param_type & |
| 6822 | piecewise_linear_distribution<_RealType>::param_type::operator= |
| 6823 | (const param_type& __rhs) |
| 6824 | { |
| 6825 | // These can throw |
| 6826 | __b_.reserve (__rhs.__b_.size ()); |
| 6827 | __densities_.reserve(__rhs.__densities_.size()); |
| 6828 | __areas_.reserve (__rhs.__areas_.size()); |
| 6829 | |
| 6830 | // These can not throw |
| 6831 | __b_ = __rhs.__b_; |
| 6832 | __densities_ = __rhs.__densities_; |
| 6833 | __areas_ = __rhs.__areas_; |
| 6834 | return *this; |
| 6835 | } |
| 6836 | |
| 6837 | |
| 6838 | template<class _RealType> |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6839 | void |
| 6840 | piecewise_linear_distribution<_RealType>::param_type::__init() |
| 6841 | { |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6842 | __areas_.assign(__densities_.size() - 1, result_type()); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6843 | result_type _Sp = 0; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6844 | for (size_t __i = 0; __i < __areas_.size(); ++__i) |
| 6845 | { |
| 6846 | __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) * |
| 6847 | (__b_[__i+1] - __b_[__i]) * .5; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6848 | _Sp += __areas_[__i]; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6849 | } |
| 6850 | for (size_t __i = __areas_.size(); __i > 1;) |
| 6851 | { |
| 6852 | --__i; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6853 | __areas_[__i] = __areas_[__i-1] / _Sp; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6854 | } |
| 6855 | __areas_[0] = 0; |
| 6856 | for (size_t __i = 1; __i < __areas_.size(); ++__i) |
| 6857 | __areas_[__i] += __areas_[__i-1]; |
| 6858 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6859 | __densities_[__i] /= _Sp; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6860 | } |
| 6861 | |
| 6862 | template<class _RealType> |
| 6863 | piecewise_linear_distribution<_RealType>::param_type::param_type() |
| 6864 | : __b_(2), |
| 6865 | __densities_(2, 1.0), |
| 6866 | __areas_(1, 0.0) |
| 6867 | { |
| 6868 | __b_[1] = 1; |
| 6869 | } |
| 6870 | |
| 6871 | template<class _RealType> |
| 6872 | template<class _InputIteratorB, class _InputIteratorW> |
| 6873 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6874 | _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW) |
| 6875 | : __b_(__fB, __lB) |
| 6876 | { |
| 6877 | if (__b_.size() < 2) |
| 6878 | { |
| 6879 | __b_.resize(2); |
| 6880 | __b_[0] = 0; |
| 6881 | __b_[1] = 1; |
| 6882 | __densities_.assign(2, 1.0); |
| 6883 | __areas_.assign(1, 0.0); |
| 6884 | } |
| 6885 | else |
| 6886 | { |
| 6887 | __densities_.reserve(__b_.size()); |
| 6888 | for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW) |
| 6889 | __densities_.push_back(*__fW); |
| 6890 | __init(); |
| 6891 | } |
| 6892 | } |
| 6893 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6894 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6895 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6896 | template<class _RealType> |
| 6897 | template<class _UnaryOperation> |
| 6898 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6899 | initializer_list<result_type> __bl, _UnaryOperation __fw) |
| 6900 | : __b_(__bl.begin(), __bl.end()) |
| 6901 | { |
| 6902 | if (__b_.size() < 2) |
| 6903 | { |
| 6904 | __b_.resize(2); |
| 6905 | __b_[0] = 0; |
| 6906 | __b_[1] = 1; |
| 6907 | __densities_.assign(2, 1.0); |
| 6908 | __areas_.assign(1, 0.0); |
| 6909 | } |
| 6910 | else |
| 6911 | { |
| 6912 | __densities_.reserve(__b_.size()); |
| 6913 | for (size_t __i = 0; __i < __b_.size(); ++__i) |
| 6914 | __densities_.push_back(__fw(__b_[__i])); |
| 6915 | __init(); |
| 6916 | } |
| 6917 | } |
| 6918 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6919 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6920 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6921 | template<class _RealType> |
| 6922 | template<class _UnaryOperation> |
| 6923 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6924 | size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw) |
| 6925 | : __b_(__nw == 0 ? 2 : __nw + 1) |
| 6926 | { |
| 6927 | size_t __n = __b_.size() - 1; |
| 6928 | result_type __d = (__xmax - __xmin) / __n; |
| 6929 | __densities_.reserve(__b_.size()); |
| 6930 | for (size_t __i = 0; __i < __n; ++__i) |
| 6931 | { |
| 6932 | __b_[__i] = __xmin + __i * __d; |
| 6933 | __densities_.push_back(__fw(__b_[__i])); |
| 6934 | } |
| 6935 | __b_[__n] = __xmax; |
| 6936 | __densities_.push_back(__fw(__b_[__n])); |
| 6937 | __init(); |
| 6938 | } |
| 6939 | |
| 6940 | template<class _RealType> |
| 6941 | template<class _URNG> |
| 6942 | _RealType |
| 6943 | piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6944 | { |
| 6945 | typedef uniform_real_distribution<result_type> _Gen; |
| 6946 | result_type __u = _Gen()(__g); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6947 | ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6948 | __u) - __p.__areas_.begin() - 1; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6949 | __u -= __p.__areas_[__k]; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6950 | const result_type __dk = __p.__densities_[__k]; |
| 6951 | const result_type __dk1 = __p.__densities_[__k+1]; |
| 6952 | const result_type __deltad = __dk1 - __dk; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6953 | const result_type __bk = __p.__b_[__k]; |
| 6954 | if (__deltad == 0) |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6955 | return __u / __dk + __bk; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6956 | const result_type __bk1 = __p.__b_[__k+1]; |
| 6957 | const result_type __deltab = __bk1 - __bk; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6958 | return (__bk * __dk1 - __bk1 * __dk + |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6959 | _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) / |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6960 | __deltad; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6961 | } |
| 6962 | |
| 6963 | template <class _CharT, class _Traits, class _RT> |
| 6964 | basic_ostream<_CharT, _Traits>& |
| 6965 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6966 | const piecewise_linear_distribution<_RT>& __x) |
| 6967 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6968 | __save_flags<_CharT, _Traits> __lx(__os); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6969 | typedef basic_ostream<_CharT, _Traits> _OStream; |
| 6970 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | |
| 6971 | _OStream::scientific); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6972 | _CharT __sp = __os.widen(' '); |
| 6973 | __os.fill(__sp); |
| 6974 | size_t __n = __x.__p_.__b_.size(); |
| 6975 | __os << __n; |
| 6976 | for (size_t __i = 0; __i < __n; ++__i) |
| 6977 | __os << __sp << __x.__p_.__b_[__i]; |
| 6978 | __n = __x.__p_.__densities_.size(); |
| 6979 | __os << __sp << __n; |
| 6980 | for (size_t __i = 0; __i < __n; ++__i) |
| 6981 | __os << __sp << __x.__p_.__densities_[__i]; |
| 6982 | __n = __x.__p_.__areas_.size(); |
| 6983 | __os << __sp << __n; |
| 6984 | for (size_t __i = 0; __i < __n; ++__i) |
| 6985 | __os << __sp << __x.__p_.__areas_[__i]; |
| 6986 | return __os; |
| 6987 | } |
| 6988 | |
| 6989 | template <class _CharT, class _Traits, class _RT> |
| 6990 | basic_istream<_CharT, _Traits>& |
| 6991 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6992 | piecewise_linear_distribution<_RT>& __x) |
| 6993 | { |
| 6994 | typedef piecewise_linear_distribution<_RT> _Eng; |
| 6995 | typedef typename _Eng::result_type result_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6996 | __save_flags<_CharT, _Traits> __lx(__is); |
Louis Dionne | 3df65ce | 2020-10-15 13:27:27 -0400 | [diff] [blame] | 6997 | typedef basic_istream<_CharT, _Traits> _Istream; |
| 6998 | __is.flags(_Istream::dec | _Istream::skipws); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6999 | size_t __n; |
| 7000 | __is >> __n; |
| 7001 | vector<result_type> __b(__n); |
| 7002 | for (size_t __i = 0; __i < __n; ++__i) |
| 7003 | __is >> __b[__i]; |
| 7004 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 7005 | vector<result_type> __densities(__n); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 7006 | for (size_t __i = 0; __i < __n; ++__i) |
| 7007 | __is >> __densities[__i]; |
| 7008 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 7009 | vector<result_type> __areas(__n); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 7010 | for (size_t __i = 0; __i < __n; ++__i) |
| 7011 | __is >> __areas[__i]; |
| 7012 | if (!__is.fail()) |
| 7013 | { |
| 7014 | swap(__x.__p_.__b_, __b); |
| 7015 | swap(__x.__p_.__densities_, __densities); |
| 7016 | swap(__x.__p_.__areas_, __areas); |
| 7017 | } |
| 7018 | return __is; |
| 7019 | } |
| 7020 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7021 | _LIBCPP_END_NAMESPACE_STD |
| 7022 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 7023 | _LIBCPP_POP_MACROS |
| 7024 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7025 | #endif // _LIBCPP_RANDOM |