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 |
| 39 | explicit linear_congruential_engine(result_type s = default_seed); |
| 40 | template<class Sseq> explicit linear_congruential_engine(Sseq& q); |
| 41 | void seed(result_type s = default_seed); |
| 42 | template<class Sseq> void seed(Sseq& q); |
| 43 | |
| 44 | // generating functions |
| 45 | result_type operator()(); |
| 46 | void discard(unsigned long long z); |
| 47 | }; |
| 48 | |
| 49 | template <class UIntType, UIntType a, UIntType c, UIntType m> |
| 50 | bool |
| 51 | operator==(const linear_congruential_engine<UIntType, a, c, m>& x, |
| 52 | const linear_congruential_engine<UIntType, a, c, m>& y); |
| 53 | |
| 54 | template <class UIntType, UIntType a, UIntType c, UIntType m> |
| 55 | bool |
| 56 | operator!=(const linear_congruential_engine<UIntType, a, c, m>& x, |
| 57 | const linear_congruential_engine<UIntType, a, c, m>& y); |
| 58 | |
| 59 | template <class charT, class traits, |
| 60 | class UIntType, UIntType a, UIntType c, UIntType m> |
| 61 | basic_ostream<charT, traits>& |
| 62 | operator<<(basic_ostream<charT, traits>& os, |
| 63 | const linear_congruential_engine<UIntType, a, c, m>& x); |
| 64 | |
| 65 | template <class charT, class traits, |
| 66 | class UIntType, UIntType a, UIntType c, UIntType m> |
| 67 | basic_istream<charT, traits>& |
| 68 | operator>>(basic_istream<charT, traits>& is, |
| 69 | linear_congruential_engine<UIntType, a, c, m>& x); |
| 70 | |
| 71 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 72 | UIntType a, size_t u, UIntType d, size_t s, |
| 73 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 74 | class mersenne_twister_engine |
| 75 | { |
| 76 | public: |
| 77 | // types |
| 78 | typedef UIntType result_type; |
| 79 | |
| 80 | // engine characteristics |
| 81 | static constexpr size_t word_size = w; |
| 82 | static constexpr size_t state_size = n; |
| 83 | static constexpr size_t shift_size = m; |
| 84 | static constexpr size_t mask_bits = r; |
| 85 | static constexpr result_type xor_mask = a; |
| 86 | static constexpr size_t tempering_u = u; |
| 87 | static constexpr result_type tempering_d = d; |
| 88 | static constexpr size_t tempering_s = s; |
| 89 | static constexpr result_type tempering_b = b; |
| 90 | static constexpr size_t tempering_t = t; |
| 91 | static constexpr result_type tempering_c = c; |
| 92 | static constexpr size_t tempering_l = l; |
| 93 | static constexpr result_type initialization_multiplier = f; |
| 94 | static constexpr result_type min () { return 0; } |
| 95 | static constexpr result_type max() { return 2^w - 1; } |
| 96 | static constexpr result_type default_seed = 5489u; |
| 97 | |
| 98 | // constructors and seeding functions |
| 99 | explicit mersenne_twister_engine(result_type value = default_seed); |
| 100 | template<class Sseq> explicit mersenne_twister_engine(Sseq& q); |
| 101 | void seed(result_type value = default_seed); |
| 102 | template<class Sseq> void seed(Sseq& q); |
| 103 | |
| 104 | // generating functions |
| 105 | result_type operator()(); |
| 106 | void discard(unsigned long long z); |
| 107 | }; |
| 108 | |
| 109 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 110 | UIntType a, size_t u, UIntType d, size_t s, |
| 111 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 112 | bool |
| 113 | operator==( |
| 114 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x, |
| 115 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y); |
| 116 | |
| 117 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 118 | UIntType a, size_t u, UIntType d, size_t s, |
| 119 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 120 | bool |
| 121 | operator!=( |
| 122 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x, |
| 123 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y); |
| 124 | |
| 125 | template <class charT, class traits, |
| 126 | class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 127 | UIntType a, size_t u, UIntType d, size_t s, |
| 128 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 129 | basic_ostream<charT, traits>& |
| 130 | operator<<(basic_ostream<charT, traits>& os, |
| 131 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x); |
| 132 | |
| 133 | template <class charT, class traits, |
| 134 | class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 135 | UIntType a, size_t u, UIntType d, size_t s, |
| 136 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 137 | basic_istream<charT, traits>& |
| 138 | operator>>(basic_istream<charT, traits>& is, |
| 139 | mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x); |
| 140 | |
| 141 | template<class UIntType, size_t w, size_t s, size_t r> |
| 142 | class subtract_with_carry_engine |
| 143 | { |
| 144 | public: |
| 145 | // types |
| 146 | typedef UIntType result_type; |
| 147 | |
| 148 | // engine characteristics |
| 149 | static constexpr size_t word_size = w; |
| 150 | static constexpr size_t short_lag = s; |
| 151 | static constexpr size_t long_lag = r; |
| 152 | static constexpr result_type min() { return 0; } |
| 153 | static constexpr result_type max() { return m-1; } |
| 154 | static constexpr result_type default_seed = 19780503u; |
| 155 | |
| 156 | // constructors and seeding functions |
| 157 | explicit subtract_with_carry_engine(result_type value = default_seed); |
| 158 | template<class Sseq> explicit subtract_with_carry_engine(Sseq& q); |
| 159 | void seed(result_type value = default_seed); |
| 160 | template<class Sseq> void seed(Sseq& q); |
| 161 | |
| 162 | // generating functions |
| 163 | result_type operator()(); |
| 164 | void discard(unsigned long long z); |
| 165 | }; |
| 166 | |
| 167 | template<class UIntType, size_t w, size_t s, size_t r> |
| 168 | bool |
| 169 | operator==( |
| 170 | const subtract_with_carry_engine<UIntType, w, s, r>& x, |
| 171 | const subtract_with_carry_engine<UIntType, w, s, r>& y); |
| 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 charT, class traits, |
| 180 | class UIntType, size_t w, size_t s, size_t r> |
| 181 | basic_ostream<charT, traits>& |
| 182 | operator<<(basic_ostream<charT, traits>& os, |
| 183 | const subtract_with_carry_engine<UIntType, w, s, r>& x); |
| 184 | |
| 185 | template <class charT, class traits, |
| 186 | class UIntType, size_t w, size_t s, size_t r> |
| 187 | basic_istream<charT, traits>& |
| 188 | operator>>(basic_istream<charT, traits>& is, |
| 189 | subtract_with_carry_engine<UIntType, w, s, r>& x); |
| 190 | |
| 191 | template<class Engine, size_t p, size_t r> |
| 192 | class discard_block_engine |
| 193 | { |
| 194 | public: |
| 195 | // types |
| 196 | typedef typename Engine::result_type result_type; |
| 197 | |
| 198 | // engine characteristics |
| 199 | static constexpr size_t block_size = p; |
| 200 | static constexpr size_t used_block = r; |
| 201 | static constexpr result_type min() { return Engine::min(); } |
| 202 | static constexpr result_type max() { return Engine::max(); } |
| 203 | |
| 204 | // constructors and seeding functions |
| 205 | discard_block_engine(); |
| 206 | explicit discard_block_engine(const Engine& e); |
| 207 | explicit discard_block_engine(Engine&& e); |
| 208 | explicit discard_block_engine(result_type s); |
| 209 | template<class Sseq> explicit discard_block_engine(Sseq& q); |
| 210 | void seed(); |
| 211 | void seed(result_type s); |
| 212 | template<class Sseq> void seed(Sseq& q); |
| 213 | |
| 214 | // generating functions |
| 215 | result_type operator()(); |
| 216 | void discard(unsigned long long z); |
| 217 | |
| 218 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 219 | const Engine& base() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | template<class Engine, size_t p, size_t r> |
| 223 | bool |
| 224 | operator==( |
| 225 | const discard_block_engine<Engine, p, r>& x, |
| 226 | const discard_block_engine<Engine, p, r>& y); |
| 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 charT, class traits, |
| 235 | class Engine, size_t p, size_t r> |
| 236 | basic_ostream<charT, traits>& |
| 237 | operator<<(basic_ostream<charT, traits>& os, |
| 238 | const discard_block_engine<Engine, p, r>& x); |
| 239 | |
| 240 | template <class charT, class traits, |
| 241 | class Engine, size_t p, size_t r> |
| 242 | basic_istream<charT, traits>& |
| 243 | operator>>(basic_istream<charT, traits>& is, |
| 244 | discard_block_engine<Engine, p, r>& x); |
| 245 | |
| 246 | template<class Engine, size_t w, class UIntType> |
| 247 | class independent_bits_engine |
| 248 | { |
| 249 | public: |
| 250 | // types |
| 251 | typedef UIntType result_type; |
| 252 | |
| 253 | // engine characteristics |
| 254 | static constexpr result_type min() { return 0; } |
| 255 | static constexpr result_type max() { return 2^w - 1; } |
| 256 | |
| 257 | // constructors and seeding functions |
| 258 | independent_bits_engine(); |
| 259 | explicit independent_bits_engine(const Engine& e); |
| 260 | explicit independent_bits_engine(Engine&& e); |
| 261 | explicit independent_bits_engine(result_type s); |
| 262 | template<class Sseq> explicit independent_bits_engine(Sseq& q); |
| 263 | void seed(); |
| 264 | void seed(result_type s); |
| 265 | template<class Sseq> void seed(Sseq& q); |
| 266 | |
| 267 | // generating functions |
| 268 | result_type operator()(); void discard(unsigned long long z); |
| 269 | |
| 270 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 271 | const Engine& base() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | template<class Engine, size_t w, class UIntType> |
| 275 | bool |
| 276 | operator==( |
| 277 | const independent_bits_engine<Engine, w, UIntType>& x, |
| 278 | const independent_bits_engine<Engine, w, UIntType>& y); |
| 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 charT, class traits, |
| 287 | class Engine, size_t w, class UIntType> |
| 288 | basic_ostream<charT, traits>& |
| 289 | operator<<(basic_ostream<charT, traits>& os, |
| 290 | const independent_bits_engine<Engine, w, UIntType>& x); |
| 291 | |
| 292 | template <class charT, class traits, |
| 293 | class Engine, size_t w, class UIntType> |
| 294 | basic_istream<charT, traits>& |
| 295 | operator>>(basic_istream<charT, traits>& is, |
| 296 | independent_bits_engine<Engine, w, UIntType>& x); |
| 297 | |
| 298 | template<class Engine, size_t k> |
| 299 | class shuffle_order_engine |
| 300 | { |
| 301 | public: |
| 302 | // types |
| 303 | typedef typename Engine::result_type result_type; |
| 304 | |
| 305 | // engine characteristics |
| 306 | static constexpr size_t table_size = k; |
| 307 | static constexpr result_type min() { return Engine::min; } |
| 308 | static constexpr result_type max() { return Engine::max; } |
| 309 | |
| 310 | // constructors and seeding functions |
| 311 | shuffle_order_engine(); |
| 312 | explicit shuffle_order_engine(const Engine& e); |
| 313 | explicit shuffle_order_engine(Engine&& e); |
| 314 | explicit shuffle_order_engine(result_type s); |
| 315 | template<class Sseq> explicit shuffle_order_engine(Sseq& q); |
| 316 | void seed(); |
| 317 | void seed(result_type s); |
| 318 | template<class Sseq> void seed(Sseq& q); |
| 319 | |
| 320 | // generating functions |
| 321 | result_type operator()(); |
| 322 | void discard(unsigned long long z); |
| 323 | |
| 324 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 325 | const Engine& base() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 326 | }; |
| 327 | |
| 328 | template<class Engine, size_t k> |
| 329 | bool |
| 330 | operator==( |
| 331 | const shuffle_order_engine<Engine, k>& x, |
| 332 | const shuffle_order_engine<Engine, k>& y); |
| 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 charT, class traits, |
| 341 | class Engine, size_t k> |
| 342 | basic_ostream<charT, traits>& |
| 343 | operator<<(basic_ostream<charT, traits>& os, |
| 344 | const shuffle_order_engine<Engine, k>& x); |
| 345 | |
| 346 | template <class charT, class traits, |
| 347 | class Engine, size_t k> |
| 348 | basic_istream<charT, traits>& |
| 349 | operator>>(basic_istream<charT, traits>& is, |
| 350 | shuffle_order_engine<Engine, k>& x); |
| 351 | |
| 352 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> |
| 353 | minstd_rand0; |
| 354 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> |
| 355 | minstd_rand; |
| 356 | typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, |
| 357 | 0x9908b0df, |
| 358 | 11, 0xffffffff, |
| 359 | 7, 0x9d2c5680, |
| 360 | 15, 0xefc60000, |
| 361 | 18, 1812433253> mt19937; |
| 362 | typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31, |
| 363 | 0xb5026f5aa96619e9, |
| 364 | 29, 0x5555555555555555, |
| 365 | 17, 0x71d67fffeda60000, |
| 366 | 37, 0xfff7eee000000000, |
| 367 | 43, 6364136223846793005> mt19937_64; |
| 368 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base; |
| 369 | typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base; |
| 370 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
| 371 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
| 372 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 373 | typedef minstd_rand default_random_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 374 | |
| 375 | // Generators |
| 376 | |
| 377 | class random_device |
| 378 | { |
| 379 | public: |
| 380 | // types |
| 381 | typedef unsigned int result_type; |
| 382 | |
| 383 | // generator characteristics |
| 384 | static constexpr result_type min() { return numeric_limits<result_type>::min(); } |
| 385 | static constexpr result_type max() { return numeric_limits<result_type>::max(); } |
| 386 | |
| 387 | // constructors |
| 388 | explicit random_device(const string& token = "/dev/urandom"); |
| 389 | |
| 390 | // generating functions |
| 391 | result_type operator()(); |
| 392 | |
| 393 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 394 | double entropy() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 395 | |
| 396 | // no copy functions |
| 397 | random_device(const random_device& ) = delete; |
| 398 | void operator=(const random_device& ) = delete; |
| 399 | }; |
| 400 | |
| 401 | // Utilities |
| 402 | |
| 403 | class seed_seq |
| 404 | { |
| 405 | public: |
| 406 | // types |
| 407 | typedef uint_least32_t result_type; |
| 408 | |
| 409 | // constructors |
| 410 | seed_seq(); |
| 411 | template<class T> |
| 412 | seed_seq(initializer_list<T> il); |
| 413 | template<class InputIterator> |
| 414 | seed_seq(InputIterator begin, InputIterator end); |
| 415 | |
| 416 | // generating functions |
| 417 | template<class RandomAccessIterator> |
| 418 | void generate(RandomAccessIterator begin, RandomAccessIterator end); |
| 419 | |
| 420 | // property functions |
| 421 | size_t size() const; |
| 422 | template<class OutputIterator> |
| 423 | void param(OutputIterator dest) const; |
| 424 | |
| 425 | // no copy functions |
| 426 | seed_seq(const seed_seq&) = delete; |
| 427 | void operator=(const seed_seq& ) = delete; |
| 428 | }; |
| 429 | |
| 430 | template<class RealType, size_t bits, class URNG> |
| 431 | RealType generate_canonical(URNG& g); |
| 432 | |
| 433 | // Distributions |
| 434 | |
| 435 | template<class IntType = int> |
| 436 | class uniform_int_distribution |
| 437 | { |
| 438 | public: |
| 439 | // types |
| 440 | typedef IntType result_type; |
| 441 | |
| 442 | class param_type |
| 443 | { |
| 444 | public: |
| 445 | typedef uniform_int_distribution distribution_type; |
| 446 | |
| 447 | explicit param_type(IntType a = 0, |
| 448 | IntType b = numeric_limits<IntType>::max()); |
| 449 | |
| 450 | result_type a() const; |
| 451 | result_type b() const; |
| 452 | |
| 453 | friend bool operator==(const param_type& x, const param_type& y); |
| 454 | friend bool operator!=(const param_type& x, const param_type& y); |
| 455 | }; |
| 456 | |
| 457 | // constructors and reset functions |
| 458 | explicit uniform_int_distribution(IntType a = 0, |
| 459 | IntType b = numeric_limits<IntType>::max()); |
| 460 | explicit uniform_int_distribution(const param_type& parm); |
| 461 | void reset(); |
| 462 | |
| 463 | // generating functions |
| 464 | template<class URNG> result_type operator()(URNG& g); |
| 465 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 466 | |
| 467 | // property functions |
| 468 | result_type a() const; |
| 469 | result_type b() const; |
| 470 | |
| 471 | param_type param() const; |
| 472 | void param(const param_type& parm); |
| 473 | |
| 474 | result_type min() const; |
| 475 | result_type max() const; |
| 476 | |
| 477 | friend bool operator==(const uniform_int_distribution& x, |
| 478 | const uniform_int_distribution& y); |
| 479 | friend bool operator!=(const uniform_int_distribution& x, |
| 480 | const uniform_int_distribution& y); |
| 481 | |
| 482 | template <class charT, class traits> |
| 483 | friend |
| 484 | basic_ostream<charT, traits>& |
| 485 | operator<<(basic_ostream<charT, traits>& os, |
| 486 | const uniform_int_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 487 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 488 | template <class charT, class traits> |
| 489 | friend |
| 490 | basic_istream<charT, traits>& |
| 491 | operator>>(basic_istream<charT, traits>& is, |
| 492 | uniform_int_distribution& x); |
| 493 | }; |
| 494 | |
| 495 | template<class RealType = double> |
| 496 | class uniform_real_distribution |
| 497 | { |
| 498 | public: |
| 499 | // types |
| 500 | typedef RealType result_type; |
| 501 | |
| 502 | class param_type |
| 503 | { |
| 504 | public: |
| 505 | typedef uniform_real_distribution distribution_type; |
| 506 | |
| 507 | explicit param_type(RealType a = 0, |
| 508 | RealType b = 1); |
| 509 | |
| 510 | result_type a() const; |
| 511 | result_type b() const; |
| 512 | |
| 513 | friend bool operator==(const param_type& x, const param_type& y); |
| 514 | friend bool operator!=(const param_type& x, const param_type& y); |
| 515 | }; |
| 516 | |
| 517 | // constructors and reset functions |
| 518 | explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0); |
| 519 | explicit uniform_real_distribution(const param_type& parm); |
| 520 | void reset(); |
| 521 | |
| 522 | // generating functions |
| 523 | template<class URNG> result_type operator()(URNG& g); |
| 524 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 525 | |
| 526 | // property functions |
| 527 | result_type a() const; |
| 528 | result_type b() const; |
| 529 | |
| 530 | param_type param() const; |
| 531 | void param(const param_type& parm); |
| 532 | |
| 533 | result_type min() const; |
| 534 | result_type max() const; |
| 535 | |
| 536 | friend bool operator==(const uniform_real_distribution& x, |
| 537 | const uniform_real_distribution& y); |
| 538 | friend bool operator!=(const uniform_real_distribution& x, |
| 539 | const uniform_real_distribution& y); |
| 540 | |
| 541 | template <class charT, class traits> |
| 542 | friend |
| 543 | basic_ostream<charT, traits>& |
| 544 | operator<<(basic_ostream<charT, traits>& os, |
| 545 | const uniform_real_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 546 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 547 | template <class charT, class traits> |
| 548 | friend |
| 549 | basic_istream<charT, traits>& |
| 550 | operator>>(basic_istream<charT, traits>& is, |
| 551 | uniform_real_distribution& x); |
| 552 | }; |
| 553 | |
| 554 | class bernoulli_distribution |
| 555 | { |
| 556 | public: |
| 557 | // types |
| 558 | typedef bool result_type; |
| 559 | |
| 560 | class param_type |
| 561 | { |
| 562 | public: |
| 563 | typedef bernoulli_distribution distribution_type; |
| 564 | |
| 565 | explicit param_type(double p = 0.5); |
| 566 | |
| 567 | double p() const; |
| 568 | |
| 569 | friend bool operator==(const param_type& x, const param_type& y); |
| 570 | friend bool operator!=(const param_type& x, const param_type& y); |
| 571 | }; |
| 572 | |
| 573 | // constructors and reset functions |
| 574 | explicit bernoulli_distribution(double p = 0.5); |
| 575 | explicit bernoulli_distribution(const param_type& parm); |
| 576 | void reset(); |
| 577 | |
| 578 | // generating functions |
| 579 | template<class URNG> result_type operator()(URNG& g); |
| 580 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 581 | |
| 582 | // property functions |
| 583 | double p() const; |
| 584 | |
| 585 | param_type param() const; |
| 586 | void param(const param_type& parm); |
| 587 | |
| 588 | result_type min() const; |
| 589 | result_type max() const; |
| 590 | |
| 591 | friend bool operator==(const bernoulli_distribution& x, |
| 592 | const bernoulli_distribution& y); |
| 593 | friend bool operator!=(const bernoulli_distribution& x, |
| 594 | const bernoulli_distribution& y); |
| 595 | |
| 596 | template <class charT, class traits> |
| 597 | friend |
| 598 | basic_ostream<charT, traits>& |
| 599 | operator<<(basic_ostream<charT, traits>& os, |
| 600 | const bernoulli_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 601 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 602 | template <class charT, class traits> |
| 603 | friend |
| 604 | basic_istream<charT, traits>& |
| 605 | operator>>(basic_istream<charT, traits>& is, |
| 606 | bernoulli_distribution& x); |
| 607 | }; |
| 608 | |
| 609 | template<class IntType = int> |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 610 | class binomial_distribution |
| 611 | { |
| 612 | public: |
| 613 | // types |
| 614 | typedef IntType result_type; |
| 615 | |
| 616 | class param_type |
| 617 | { |
| 618 | public: |
| 619 | typedef binomial_distribution distribution_type; |
| 620 | |
| 621 | explicit param_type(IntType t = 1, double p = 0.5); |
| 622 | |
| 623 | IntType t() const; |
| 624 | double p() const; |
| 625 | |
| 626 | friend bool operator==(const param_type& x, const param_type& y); |
| 627 | friend bool operator!=(const param_type& x, const param_type& y); |
| 628 | }; |
| 629 | |
| 630 | // constructors and reset functions |
| 631 | explicit binomial_distribution(IntType t = 1, double p = 0.5); |
| 632 | explicit binomial_distribution(const param_type& parm); |
| 633 | void reset(); |
| 634 | |
| 635 | // generating functions |
| 636 | template<class URNG> result_type operator()(URNG& g); |
| 637 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 638 | |
| 639 | // property functions |
| 640 | IntType t() const; |
| 641 | double p() const; |
| 642 | |
| 643 | param_type param() const; |
| 644 | void param(const param_type& parm); |
| 645 | |
| 646 | result_type min() const; |
| 647 | result_type max() const; |
| 648 | |
| 649 | friend bool operator==(const binomial_distribution& x, |
| 650 | const binomial_distribution& y); |
| 651 | friend bool operator!=(const binomial_distribution& x, |
| 652 | const binomial_distribution& y); |
| 653 | |
| 654 | template <class charT, class traits> |
| 655 | friend |
| 656 | basic_ostream<charT, traits>& |
| 657 | operator<<(basic_ostream<charT, traits>& os, |
| 658 | const binomial_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 659 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 660 | template <class charT, class traits> |
| 661 | friend |
| 662 | basic_istream<charT, traits>& |
| 663 | operator>>(basic_istream<charT, traits>& is, |
| 664 | binomial_distribution& x); |
| 665 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 666 | |
| 667 | template<class IntType = int> |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 668 | class geometric_distribution |
| 669 | { |
| 670 | public: |
| 671 | // types |
| 672 | typedef IntType result_type; |
| 673 | |
| 674 | class param_type |
| 675 | { |
| 676 | public: |
| 677 | typedef geometric_distribution distribution_type; |
| 678 | |
| 679 | explicit param_type(double p = 0.5); |
| 680 | |
| 681 | double p() const; |
| 682 | |
| 683 | friend bool operator==(const param_type& x, const param_type& y); |
| 684 | friend bool operator!=(const param_type& x, const param_type& y); |
| 685 | }; |
| 686 | |
| 687 | // constructors and reset functions |
| 688 | explicit geometric_distribution(double p = 0.5); |
| 689 | explicit geometric_distribution(const param_type& parm); |
| 690 | void reset(); |
| 691 | |
| 692 | // generating functions |
| 693 | template<class URNG> result_type operator()(URNG& g); |
| 694 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 695 | |
| 696 | // property functions |
| 697 | double p() const; |
| 698 | |
| 699 | param_type param() const; |
| 700 | void param(const param_type& parm); |
| 701 | |
| 702 | result_type min() const; |
| 703 | result_type max() const; |
| 704 | |
| 705 | friend bool operator==(const geometric_distribution& x, |
| 706 | const geometric_distribution& y); |
| 707 | friend bool operator!=(const geometric_distribution& x, |
| 708 | const geometric_distribution& y); |
| 709 | |
| 710 | template <class charT, class traits> |
| 711 | friend |
| 712 | basic_ostream<charT, traits>& |
| 713 | operator<<(basic_ostream<charT, traits>& os, |
| 714 | const geometric_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 715 | |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 716 | template <class charT, class traits> |
| 717 | friend |
| 718 | basic_istream<charT, traits>& |
| 719 | operator>>(basic_istream<charT, traits>& is, |
| 720 | geometric_distribution& x); |
| 721 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 722 | |
| 723 | template<class IntType = int> |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 724 | class negative_binomial_distribution |
| 725 | { |
| 726 | public: |
| 727 | // types |
| 728 | typedef IntType result_type; |
| 729 | |
| 730 | class param_type |
| 731 | { |
| 732 | public: |
| 733 | typedef negative_binomial_distribution distribution_type; |
| 734 | |
| 735 | explicit param_type(result_type k = 1, double p = 0.5); |
| 736 | |
| 737 | result_type k() const; |
| 738 | double p() const; |
| 739 | |
| 740 | friend bool operator==(const param_type& x, const param_type& y); |
| 741 | friend bool operator!=(const param_type& x, const param_type& y); |
| 742 | }; |
| 743 | |
| 744 | // constructor and reset functions |
| 745 | explicit negative_binomial_distribution(result_type k = 1, double p = 0.5); |
| 746 | explicit negative_binomial_distribution(const param_type& parm); |
| 747 | void reset(); |
| 748 | |
| 749 | // generating functions |
| 750 | template<class URNG> result_type operator()(URNG& g); |
| 751 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 752 | |
| 753 | // property functions |
| 754 | result_type k() const; |
| 755 | double p() const; |
| 756 | |
| 757 | param_type param() const; |
| 758 | void param(const param_type& parm); |
| 759 | |
| 760 | result_type min() const; |
| 761 | result_type max() const; |
| 762 | |
| 763 | friend bool operator==(const negative_binomial_distribution& x, |
| 764 | const negative_binomial_distribution& y); |
| 765 | friend bool operator!=(const negative_binomial_distribution& x, |
| 766 | const negative_binomial_distribution& y); |
| 767 | |
| 768 | template <class charT, class traits> |
| 769 | friend |
| 770 | basic_ostream<charT, traits>& |
| 771 | operator<<(basic_ostream<charT, traits>& os, |
| 772 | const negative_binomial_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 773 | |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 774 | template <class charT, class traits> |
| 775 | friend |
| 776 | basic_istream<charT, traits>& |
| 777 | operator>>(basic_istream<charT, traits>& is, |
| 778 | negative_binomial_distribution& x); |
| 779 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 780 | |
| 781 | template<class IntType = int> |
Howard Hinnant | 24a5f2a | 2010-05-14 21:38:54 +0000 | [diff] [blame] | 782 | class poisson_distribution |
| 783 | { |
| 784 | public: |
| 785 | // types |
| 786 | typedef IntType result_type; |
| 787 | |
| 788 | class param_type |
| 789 | { |
| 790 | public: |
| 791 | typedef poisson_distribution distribution_type; |
| 792 | |
| 793 | explicit param_type(double mean = 1.0); |
| 794 | |
| 795 | double mean() const; |
| 796 | |
| 797 | friend bool operator==(const param_type& x, const param_type& y); |
| 798 | friend bool operator!=(const param_type& x, const param_type& y); |
| 799 | }; |
| 800 | |
| 801 | // constructors and reset functions |
| 802 | explicit poisson_distribution(double mean = 1.0); |
| 803 | explicit poisson_distribution(const param_type& parm); |
| 804 | void reset(); |
| 805 | |
| 806 | // generating functions |
| 807 | template<class URNG> result_type operator()(URNG& g); |
| 808 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 809 | |
| 810 | // property functions |
| 811 | double mean() const; |
| 812 | |
| 813 | param_type param() const; |
| 814 | void param(const param_type& parm); |
| 815 | |
| 816 | result_type min() const; |
| 817 | result_type max() const; |
| 818 | |
| 819 | friend bool operator==(const poisson_distribution& x, |
| 820 | const poisson_distribution& y); |
| 821 | friend bool operator!=(const poisson_distribution& x, |
| 822 | const poisson_distribution& y); |
| 823 | |
| 824 | template <class charT, class traits> |
| 825 | friend |
| 826 | basic_ostream<charT, traits>& |
| 827 | operator<<(basic_ostream<charT, traits>& os, |
| 828 | const poisson_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 829 | |
Howard Hinnant | 24a5f2a | 2010-05-14 21:38:54 +0000 | [diff] [blame] | 830 | template <class charT, class traits> |
| 831 | friend |
| 832 | basic_istream<charT, traits>& |
| 833 | operator>>(basic_istream<charT, traits>& is, |
| 834 | poisson_distribution& x); |
| 835 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 836 | |
| 837 | template<class RealType = double> |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 838 | class exponential_distribution |
| 839 | { |
| 840 | public: |
| 841 | // types |
| 842 | typedef RealType result_type; |
| 843 | |
| 844 | class param_type |
| 845 | { |
| 846 | public: |
| 847 | typedef exponential_distribution distribution_type; |
| 848 | |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 849 | explicit param_type(result_type lambda = 1.0); |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 850 | |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 851 | result_type lambda() const; |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 852 | |
| 853 | friend bool operator==(const param_type& x, const param_type& y); |
| 854 | friend bool operator!=(const param_type& x, const param_type& y); |
| 855 | }; |
| 856 | |
| 857 | // constructors and reset functions |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 858 | explicit exponential_distribution(result_type lambda = 1.0); |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 859 | explicit exponential_distribution(const param_type& parm); |
| 860 | void reset(); |
| 861 | |
| 862 | // generating functions |
| 863 | template<class URNG> result_type operator()(URNG& g); |
| 864 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 865 | |
| 866 | // property functions |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 867 | result_type lambda() const; |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 868 | |
| 869 | param_type param() const; |
| 870 | void param(const param_type& parm); |
| 871 | |
| 872 | result_type min() const; |
| 873 | result_type max() const; |
| 874 | |
| 875 | friend bool operator==(const exponential_distribution& x, |
| 876 | const exponential_distribution& y); |
| 877 | friend bool operator!=(const exponential_distribution& x, |
| 878 | const exponential_distribution& y); |
| 879 | |
| 880 | template <class charT, class traits> |
| 881 | friend |
| 882 | basic_ostream<charT, traits>& |
| 883 | operator<<(basic_ostream<charT, traits>& os, |
| 884 | const exponential_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 885 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 886 | template <class charT, class traits> |
| 887 | friend |
| 888 | basic_istream<charT, traits>& |
| 889 | operator>>(basic_istream<charT, traits>& is, |
| 890 | exponential_distribution& x); |
| 891 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 892 | |
| 893 | template<class RealType = double> |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 894 | class gamma_distribution |
| 895 | { |
| 896 | public: |
| 897 | // types |
| 898 | typedef RealType result_type; |
| 899 | |
| 900 | class param_type |
| 901 | { |
| 902 | public: |
| 903 | typedef gamma_distribution distribution_type; |
| 904 | |
| 905 | explicit param_type(result_type alpha = 1, result_type beta = 1); |
| 906 | |
| 907 | result_type alpha() const; |
| 908 | result_type beta() const; |
| 909 | |
| 910 | friend bool operator==(const param_type& x, const param_type& y); |
| 911 | friend bool operator!=(const param_type& x, const param_type& y); |
| 912 | }; |
| 913 | |
| 914 | // constructors and reset functions |
| 915 | explicit gamma_distribution(result_type alpha = 1, result_type beta = 1); |
| 916 | explicit gamma_distribution(const param_type& parm); |
| 917 | void reset(); |
| 918 | |
| 919 | // generating functions |
| 920 | template<class URNG> result_type operator()(URNG& g); |
| 921 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 922 | |
| 923 | // property functions |
| 924 | result_type alpha() const; |
| 925 | result_type beta() const; |
| 926 | |
| 927 | param_type param() const; |
| 928 | void param(const param_type& parm); |
| 929 | |
| 930 | result_type min() const; |
| 931 | result_type max() const; |
| 932 | |
| 933 | friend bool operator==(const gamma_distribution& x, |
| 934 | const gamma_distribution& y); |
| 935 | friend bool operator!=(const gamma_distribution& x, |
| 936 | const gamma_distribution& y); |
| 937 | |
| 938 | template <class charT, class traits> |
| 939 | friend |
| 940 | basic_ostream<charT, traits>& |
| 941 | operator<<(basic_ostream<charT, traits>& os, |
| 942 | const gamma_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 943 | |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 944 | template <class charT, class traits> |
| 945 | friend |
| 946 | basic_istream<charT, traits>& |
| 947 | operator>>(basic_istream<charT, traits>& is, |
| 948 | gamma_distribution& x); |
| 949 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 950 | |
| 951 | template<class RealType = double> |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 952 | class weibull_distribution |
| 953 | { |
| 954 | public: |
| 955 | // types |
| 956 | typedef RealType result_type; |
| 957 | |
| 958 | class param_type |
| 959 | { |
| 960 | public: |
| 961 | typedef weibull_distribution distribution_type; |
| 962 | |
| 963 | explicit param_type(result_type alpha = 1, result_type beta = 1); |
| 964 | |
| 965 | result_type a() const; |
| 966 | result_type b() const; |
| 967 | |
| 968 | friend bool operator==(const param_type& x, const param_type& y); |
| 969 | friend bool operator!=(const param_type& x, const param_type& y); |
| 970 | }; |
| 971 | |
| 972 | // constructor and reset functions |
| 973 | explicit weibull_distribution(result_type a = 1, result_type b = 1); |
| 974 | explicit weibull_distribution(const param_type& parm); |
| 975 | void reset(); |
| 976 | |
| 977 | // generating functions |
| 978 | template<class URNG> result_type operator()(URNG& g); |
| 979 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 980 | |
| 981 | // property functions |
| 982 | result_type a() const; |
| 983 | result_type b() const; |
| 984 | |
| 985 | param_type param() const; |
| 986 | void param(const param_type& parm); |
| 987 | |
| 988 | result_type min() const; |
| 989 | result_type max() const; |
| 990 | |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 991 | friend bool operator==(const weibull_distribution& x, |
| 992 | const weibull_distribution& y); |
| 993 | friend bool operator!=(const weibull_distribution& x, |
| 994 | const weibull_distribution& y); |
| 995 | |
| 996 | template <class charT, class traits> |
| 997 | friend |
| 998 | basic_ostream<charT, traits>& |
| 999 | operator<<(basic_ostream<charT, traits>& os, |
| 1000 | const weibull_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1001 | |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 1002 | template <class charT, class traits> |
| 1003 | friend |
| 1004 | basic_istream<charT, traits>& |
| 1005 | operator>>(basic_istream<charT, traits>& is, |
| 1006 | weibull_distribution& x); |
| 1007 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1008 | |
| 1009 | template<class RealType = double> |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 1010 | class extreme_value_distribution |
| 1011 | { |
| 1012 | public: |
| 1013 | // types |
| 1014 | typedef RealType result_type; |
| 1015 | |
| 1016 | class param_type |
| 1017 | { |
| 1018 | public: |
| 1019 | typedef extreme_value_distribution distribution_type; |
| 1020 | |
| 1021 | explicit param_type(result_type a = 0, result_type b = 1); |
| 1022 | |
| 1023 | result_type a() const; |
| 1024 | result_type b() const; |
| 1025 | |
| 1026 | friend bool operator==(const param_type& x, const param_type& y); |
| 1027 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1028 | }; |
| 1029 | |
| 1030 | // constructor and reset functions |
| 1031 | explicit extreme_value_distribution(result_type a = 0, result_type b = 1); |
| 1032 | explicit extreme_value_distribution(const param_type& parm); |
| 1033 | void reset(); |
| 1034 | |
| 1035 | // generating functions |
| 1036 | template<class URNG> result_type operator()(URNG& g); |
| 1037 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1038 | |
| 1039 | // property functions |
| 1040 | result_type a() const; |
| 1041 | result_type b() const; |
| 1042 | |
| 1043 | param_type param() const; |
| 1044 | void param(const param_type& parm); |
| 1045 | |
| 1046 | result_type min() const; |
| 1047 | result_type max() const; |
| 1048 | |
| 1049 | friend bool operator==(const extreme_value_distribution& x, |
| 1050 | const extreme_value_distribution& y); |
| 1051 | friend bool operator!=(const extreme_value_distribution& x, |
| 1052 | const extreme_value_distribution& y); |
| 1053 | |
| 1054 | template <class charT, class traits> |
| 1055 | friend |
| 1056 | basic_ostream<charT, traits>& |
| 1057 | operator<<(basic_ostream<charT, traits>& os, |
| 1058 | const extreme_value_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1059 | |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 1060 | template <class charT, class traits> |
| 1061 | friend |
| 1062 | basic_istream<charT, traits>& |
| 1063 | operator>>(basic_istream<charT, traits>& is, |
| 1064 | extreme_value_distribution& x); |
| 1065 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1066 | |
| 1067 | template<class RealType = double> |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 1068 | class normal_distribution |
| 1069 | { |
| 1070 | public: |
| 1071 | // types |
| 1072 | typedef RealType result_type; |
| 1073 | |
| 1074 | class param_type |
| 1075 | { |
| 1076 | public: |
| 1077 | typedef normal_distribution distribution_type; |
| 1078 | |
| 1079 | explicit param_type(result_type mean = 0, result_type stddev = 1); |
| 1080 | |
| 1081 | result_type mean() const; |
| 1082 | result_type stddev() const; |
| 1083 | |
| 1084 | friend bool operator==(const param_type& x, const param_type& y); |
| 1085 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1086 | }; |
| 1087 | |
| 1088 | // constructors and reset functions |
| 1089 | explicit normal_distribution(result_type mean = 0, result_type stddev = 1); |
| 1090 | explicit normal_distribution(const param_type& parm); |
| 1091 | void reset(); |
| 1092 | |
| 1093 | // generating functions |
| 1094 | template<class URNG> result_type operator()(URNG& g); |
| 1095 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1096 | |
| 1097 | // property functions |
| 1098 | result_type mean() const; |
| 1099 | result_type stddev() const; |
| 1100 | |
| 1101 | param_type param() const; |
| 1102 | void param(const param_type& parm); |
| 1103 | |
| 1104 | result_type min() const; |
| 1105 | result_type max() const; |
| 1106 | |
| 1107 | friend bool operator==(const normal_distribution& x, |
| 1108 | const normal_distribution& y); |
| 1109 | friend bool operator!=(const normal_distribution& x, |
| 1110 | const normal_distribution& y); |
| 1111 | |
| 1112 | template <class charT, class traits> |
| 1113 | friend |
| 1114 | basic_ostream<charT, traits>& |
| 1115 | operator<<(basic_ostream<charT, traits>& os, |
| 1116 | const normal_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1117 | |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 1118 | template <class charT, class traits> |
| 1119 | friend |
| 1120 | basic_istream<charT, traits>& |
| 1121 | operator>>(basic_istream<charT, traits>& is, |
| 1122 | normal_distribution& x); |
| 1123 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1124 | |
| 1125 | template<class RealType = double> |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 1126 | class lognormal_distribution |
| 1127 | { |
| 1128 | public: |
| 1129 | // types |
| 1130 | typedef RealType result_type; |
| 1131 | |
| 1132 | class param_type |
| 1133 | { |
| 1134 | public: |
| 1135 | typedef lognormal_distribution distribution_type; |
| 1136 | |
| 1137 | explicit param_type(result_type m = 0, result_type s = 1); |
| 1138 | |
| 1139 | result_type m() const; |
| 1140 | result_type s() const; |
| 1141 | |
| 1142 | friend bool operator==(const param_type& x, const param_type& y); |
| 1143 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1144 | }; |
| 1145 | |
| 1146 | // constructor and reset functions |
| 1147 | explicit lognormal_distribution(result_type m = 0, result_type s = 1); |
| 1148 | explicit lognormal_distribution(const param_type& parm); |
| 1149 | void reset(); |
| 1150 | |
| 1151 | // generating functions |
| 1152 | template<class URNG> result_type operator()(URNG& g); |
| 1153 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1154 | |
| 1155 | // property functions |
| 1156 | result_type m() const; |
| 1157 | result_type s() const; |
| 1158 | |
| 1159 | param_type param() const; |
| 1160 | void param(const param_type& parm); |
| 1161 | |
| 1162 | result_type min() const; |
| 1163 | result_type max() const; |
| 1164 | |
| 1165 | friend bool operator==(const lognormal_distribution& x, |
| 1166 | const lognormal_distribution& y); |
| 1167 | friend bool operator!=(const lognormal_distribution& x, |
| 1168 | const lognormal_distribution& y); |
| 1169 | |
| 1170 | template <class charT, class traits> |
| 1171 | friend |
| 1172 | basic_ostream<charT, traits>& |
| 1173 | operator<<(basic_ostream<charT, traits>& os, |
| 1174 | const lognormal_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1175 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 1176 | template <class charT, class traits> |
| 1177 | friend |
| 1178 | basic_istream<charT, traits>& |
| 1179 | operator>>(basic_istream<charT, traits>& is, |
| 1180 | lognormal_distribution& x); |
| 1181 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1182 | |
| 1183 | template<class RealType = double> |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1184 | class chi_squared_distribution |
| 1185 | { |
| 1186 | public: |
| 1187 | // types |
| 1188 | typedef RealType result_type; |
| 1189 | |
| 1190 | class param_type |
| 1191 | { |
| 1192 | public: |
| 1193 | typedef chi_squared_distribution distribution_type; |
| 1194 | |
| 1195 | explicit param_type(result_type n = 1); |
| 1196 | |
| 1197 | result_type n() const; |
| 1198 | |
| 1199 | friend bool operator==(const param_type& x, const param_type& y); |
| 1200 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1201 | }; |
| 1202 | |
| 1203 | // constructor and reset functions |
| 1204 | explicit chi_squared_distribution(result_type n = 1); |
| 1205 | explicit chi_squared_distribution(const param_type& parm); |
| 1206 | void reset(); |
| 1207 | |
| 1208 | // generating functions |
| 1209 | template<class URNG> result_type operator()(URNG& g); |
| 1210 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1211 | |
| 1212 | // property functions |
| 1213 | result_type n() const; |
| 1214 | |
| 1215 | param_type param() const; |
| 1216 | void param(const param_type& parm); |
| 1217 | |
| 1218 | result_type min() const; |
| 1219 | result_type max() const; |
| 1220 | |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1221 | friend bool operator==(const chi_squared_distribution& x, |
| 1222 | const chi_squared_distribution& y); |
| 1223 | friend bool operator!=(const chi_squared_distribution& x, |
| 1224 | const chi_squared_distribution& y); |
| 1225 | |
| 1226 | template <class charT, class traits> |
| 1227 | friend |
| 1228 | basic_ostream<charT, traits>& |
| 1229 | operator<<(basic_ostream<charT, traits>& os, |
| 1230 | const chi_squared_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1231 | |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1232 | template <class charT, class traits> |
| 1233 | friend |
| 1234 | basic_istream<charT, traits>& |
| 1235 | operator>>(basic_istream<charT, traits>& is, |
| 1236 | chi_squared_distribution& x); |
| 1237 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1238 | |
| 1239 | template<class RealType = double> |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 1240 | class cauchy_distribution |
| 1241 | { |
| 1242 | public: |
| 1243 | // types |
| 1244 | typedef RealType result_type; |
| 1245 | |
| 1246 | class param_type |
| 1247 | { |
| 1248 | public: |
| 1249 | typedef cauchy_distribution distribution_type; |
| 1250 | |
| 1251 | explicit param_type(result_type a = 0, result_type b = 1); |
| 1252 | |
| 1253 | result_type a() const; |
| 1254 | result_type b() const; |
| 1255 | |
| 1256 | friend bool operator==(const param_type& x, const param_type& y); |
| 1257 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1258 | }; |
| 1259 | |
| 1260 | // constructor and reset functions |
| 1261 | explicit cauchy_distribution(result_type a = 0, result_type b = 1); |
| 1262 | explicit cauchy_distribution(const param_type& parm); |
| 1263 | void reset(); |
| 1264 | |
| 1265 | // generating functions |
| 1266 | template<class URNG> result_type operator()(URNG& g); |
| 1267 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1268 | |
| 1269 | // property functions |
| 1270 | result_type a() const; |
| 1271 | result_type b() const; |
| 1272 | |
| 1273 | param_type param() const; |
| 1274 | void param(const param_type& parm); |
| 1275 | |
| 1276 | result_type min() const; |
| 1277 | result_type max() const; |
| 1278 | |
| 1279 | friend bool operator==(const cauchy_distribution& x, |
| 1280 | const cauchy_distribution& y); |
| 1281 | friend bool operator!=(const cauchy_distribution& x, |
| 1282 | const cauchy_distribution& y); |
| 1283 | |
| 1284 | template <class charT, class traits> |
| 1285 | friend |
| 1286 | basic_ostream<charT, traits>& |
| 1287 | operator<<(basic_ostream<charT, traits>& os, |
| 1288 | const cauchy_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1289 | |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 1290 | template <class charT, class traits> |
| 1291 | friend |
| 1292 | basic_istream<charT, traits>& |
| 1293 | operator>>(basic_istream<charT, traits>& is, |
| 1294 | cauchy_distribution& x); |
| 1295 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1296 | |
| 1297 | template<class RealType = double> |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1298 | class fisher_f_distribution |
| 1299 | { |
| 1300 | public: |
| 1301 | // types |
| 1302 | typedef RealType result_type; |
| 1303 | |
| 1304 | class param_type |
| 1305 | { |
| 1306 | public: |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1307 | typedef fisher_f_distribution distribution_type; |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1308 | |
| 1309 | explicit param_type(result_type m = 1, result_type n = 1); |
| 1310 | |
| 1311 | result_type m() const; |
| 1312 | result_type n() const; |
| 1313 | |
| 1314 | friend bool operator==(const param_type& x, const param_type& y); |
| 1315 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1316 | }; |
| 1317 | |
| 1318 | // constructor and reset functions |
| 1319 | explicit fisher_f_distribution(result_type m = 1, result_type n = 1); |
| 1320 | explicit fisher_f_distribution(const param_type& parm); |
| 1321 | void reset(); |
| 1322 | |
| 1323 | // generating functions |
| 1324 | template<class URNG> result_type operator()(URNG& g); |
| 1325 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1326 | |
| 1327 | // property functions |
| 1328 | result_type m() const; |
| 1329 | result_type n() const; |
| 1330 | |
| 1331 | param_type param() const; |
| 1332 | void param(const param_type& parm); |
| 1333 | |
| 1334 | result_type min() const; |
| 1335 | result_type max() const; |
| 1336 | |
| 1337 | friend bool operator==(const fisher_f_distribution& x, |
| 1338 | const fisher_f_distribution& y); |
| 1339 | friend bool operator!=(const fisher_f_distribution& x, |
| 1340 | const fisher_f_distribution& y); |
| 1341 | |
| 1342 | template <class charT, class traits> |
| 1343 | friend |
| 1344 | basic_ostream<charT, traits>& |
| 1345 | operator<<(basic_ostream<charT, traits>& os, |
| 1346 | const fisher_f_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1347 | |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 1348 | template <class charT, class traits> |
| 1349 | friend |
| 1350 | basic_istream<charT, traits>& |
| 1351 | operator>>(basic_istream<charT, traits>& is, |
| 1352 | fisher_f_distribution& x); |
| 1353 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1354 | |
| 1355 | template<class RealType = double> |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1356 | class student_t_distribution |
| 1357 | { |
| 1358 | public: |
| 1359 | // types |
| 1360 | typedef RealType result_type; |
| 1361 | |
| 1362 | class param_type |
| 1363 | { |
| 1364 | public: |
| 1365 | typedef student_t_distribution distribution_type; |
| 1366 | |
| 1367 | explicit param_type(result_type n = 1); |
| 1368 | |
| 1369 | result_type n() const; |
| 1370 | |
| 1371 | friend bool operator==(const param_type& x, const param_type& y); |
| 1372 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1373 | }; |
| 1374 | |
| 1375 | // constructor and reset functions |
| 1376 | explicit student_t_distribution(result_type n = 1); |
| 1377 | explicit student_t_distribution(const param_type& parm); |
| 1378 | void reset(); |
| 1379 | |
| 1380 | // generating functions |
| 1381 | template<class URNG> result_type operator()(URNG& g); |
| 1382 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1383 | |
| 1384 | // property functions |
| 1385 | result_type n() const; |
| 1386 | |
| 1387 | param_type param() const; |
| 1388 | void param(const param_type& parm); |
| 1389 | |
| 1390 | result_type min() const; |
| 1391 | result_type max() const; |
| 1392 | |
| 1393 | friend bool operator==(const student_t_distribution& x, |
| 1394 | const student_t_distribution& y); |
| 1395 | friend bool operator!=(const student_t_distribution& x, |
| 1396 | const student_t_distribution& y); |
| 1397 | |
| 1398 | template <class charT, class traits> |
| 1399 | friend |
| 1400 | basic_ostream<charT, traits>& |
| 1401 | operator<<(basic_ostream<charT, traits>& os, |
| 1402 | const student_t_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1403 | |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 1404 | template <class charT, class traits> |
| 1405 | friend |
| 1406 | basic_istream<charT, traits>& |
| 1407 | operator>>(basic_istream<charT, traits>& is, |
| 1408 | student_t_distribution& x); |
| 1409 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1410 | |
| 1411 | template<class IntType = int> |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1412 | class discrete_distribution |
| 1413 | { |
| 1414 | public: |
| 1415 | // types |
| 1416 | typedef IntType result_type; |
| 1417 | |
| 1418 | class param_type |
| 1419 | { |
| 1420 | public: |
| 1421 | typedef discrete_distribution distribution_type; |
| 1422 | |
| 1423 | param_type(); |
| 1424 | template<class InputIterator> |
| 1425 | param_type(InputIterator firstW, InputIterator lastW); |
| 1426 | param_type(initializer_list<double> wl); |
| 1427 | template<class UnaryOperation> |
| 1428 | param_type(size_t nw, double xmin, double xmax, UnaryOperation fw); |
| 1429 | |
| 1430 | vector<double> probabilities() const; |
| 1431 | |
| 1432 | friend bool operator==(const param_type& x, const param_type& y); |
| 1433 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1434 | }; |
| 1435 | |
| 1436 | // constructor and reset functions |
| 1437 | discrete_distribution(); |
| 1438 | template<class InputIterator> |
| 1439 | discrete_distribution(InputIterator firstW, InputIterator lastW); |
| 1440 | discrete_distribution(initializer_list<double> wl); |
| 1441 | template<class UnaryOperation> |
| 1442 | discrete_distribution(size_t nw, double xmin, double xmax, |
| 1443 | UnaryOperation fw); |
| 1444 | explicit discrete_distribution(const param_type& parm); |
| 1445 | void reset(); |
| 1446 | |
| 1447 | // generating functions |
| 1448 | template<class URNG> result_type operator()(URNG& g); |
| 1449 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1450 | |
| 1451 | // property functions |
| 1452 | vector<double> probabilities() const; |
| 1453 | |
| 1454 | param_type param() const; |
| 1455 | void param(const param_type& parm); |
| 1456 | |
| 1457 | result_type min() const; |
| 1458 | result_type max() const; |
| 1459 | |
| 1460 | friend bool operator==(const discrete_distribution& x, |
| 1461 | const discrete_distribution& y); |
| 1462 | friend bool operator!=(const discrete_distribution& x, |
| 1463 | const discrete_distribution& y); |
| 1464 | |
| 1465 | template <class charT, class traits> |
| 1466 | friend |
| 1467 | basic_ostream<charT, traits>& |
| 1468 | operator<<(basic_ostream<charT, traits>& os, |
| 1469 | const discrete_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1470 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1471 | template <class charT, class traits> |
| 1472 | friend |
| 1473 | basic_istream<charT, traits>& |
| 1474 | operator>>(basic_istream<charT, traits>& is, |
| 1475 | discrete_distribution& x); |
| 1476 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1477 | |
| 1478 | template<class RealType = double> |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1479 | class piecewise_constant_distribution |
| 1480 | { |
| 1481 | // types |
| 1482 | typedef RealType result_type; |
| 1483 | |
| 1484 | class param_type |
| 1485 | { |
| 1486 | public: |
| 1487 | typedef piecewise_constant_distribution distribution_type; |
| 1488 | |
| 1489 | param_type(); |
| 1490 | template<class InputIteratorB, class InputIteratorW> |
| 1491 | param_type(InputIteratorB firstB, InputIteratorB lastB, |
| 1492 | InputIteratorW firstW); |
| 1493 | template<class UnaryOperation> |
| 1494 | param_type(initializer_list<result_type> bl, UnaryOperation fw); |
| 1495 | template<class UnaryOperation> |
| 1496 | param_type(size_t nw, result_type xmin, result_type xmax, |
| 1497 | UnaryOperation fw); |
| 1498 | |
| 1499 | vector<result_type> intervals() const; |
Howard Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1500 | vector<result_type> densities() const; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1501 | |
| 1502 | friend bool operator==(const param_type& x, const param_type& y); |
| 1503 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1504 | }; |
| 1505 | |
| 1506 | // constructor and reset functions |
| 1507 | piecewise_constant_distribution(); |
| 1508 | template<class InputIteratorB, class InputIteratorW> |
| 1509 | piecewise_constant_distribution(InputIteratorB firstB, |
| 1510 | InputIteratorB lastB, |
| 1511 | InputIteratorW firstW); |
| 1512 | template<class UnaryOperation> |
| 1513 | piecewise_constant_distribution(initializer_list<result_type> bl, |
| 1514 | UnaryOperation fw); |
| 1515 | template<class UnaryOperation> |
| 1516 | piecewise_constant_distribution(size_t nw, result_type xmin, |
| 1517 | result_type xmax, UnaryOperation fw); |
| 1518 | explicit piecewise_constant_distribution(const param_type& parm); |
| 1519 | void reset(); |
| 1520 | |
| 1521 | // generating functions |
| 1522 | template<class URNG> result_type operator()(URNG& g); |
| 1523 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1524 | |
| 1525 | // property functions |
| 1526 | vector<result_type> intervals() const; |
Howard Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1527 | vector<result_type> densities() const; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1528 | |
| 1529 | param_type param() const; |
| 1530 | void param(const param_type& parm); |
| 1531 | |
| 1532 | result_type min() const; |
| 1533 | result_type max() const; |
| 1534 | |
| 1535 | friend bool operator==(const piecewise_constant_distribution& x, |
| 1536 | const piecewise_constant_distribution& y); |
| 1537 | friend bool operator!=(const piecewise_constant_distribution& x, |
| 1538 | const piecewise_constant_distribution& y); |
| 1539 | |
| 1540 | template <class charT, class traits> |
| 1541 | friend |
| 1542 | basic_ostream<charT, traits>& |
| 1543 | operator<<(basic_ostream<charT, traits>& os, |
| 1544 | const piecewise_constant_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1545 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 1546 | template <class charT, class traits> |
| 1547 | friend |
| 1548 | basic_istream<charT, traits>& |
| 1549 | operator>>(basic_istream<charT, traits>& is, |
| 1550 | piecewise_constant_distribution& x); |
| 1551 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1552 | |
| 1553 | template<class RealType = double> |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1554 | class piecewise_linear_distribution |
| 1555 | { |
| 1556 | // types |
| 1557 | typedef RealType result_type; |
| 1558 | |
| 1559 | class param_type |
| 1560 | { |
| 1561 | public: |
| 1562 | typedef piecewise_linear_distribution distribution_type; |
| 1563 | |
| 1564 | param_type(); |
| 1565 | template<class InputIteratorB, class InputIteratorW> |
| 1566 | param_type(InputIteratorB firstB, InputIteratorB lastB, |
| 1567 | InputIteratorW firstW); |
| 1568 | template<class UnaryOperation> |
| 1569 | param_type(initializer_list<result_type> bl, UnaryOperation fw); |
| 1570 | template<class UnaryOperation> |
| 1571 | param_type(size_t nw, result_type xmin, result_type xmax, |
| 1572 | UnaryOperation fw); |
| 1573 | |
| 1574 | vector<result_type> intervals() const; |
Howard Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1575 | vector<result_type> densities() const; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1576 | |
| 1577 | friend bool operator==(const param_type& x, const param_type& y); |
| 1578 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1579 | }; |
| 1580 | |
| 1581 | // constructor and reset functions |
| 1582 | piecewise_linear_distribution(); |
| 1583 | template<class InputIteratorB, class InputIteratorW> |
| 1584 | piecewise_linear_distribution(InputIteratorB firstB, |
| 1585 | InputIteratorB lastB, |
| 1586 | InputIteratorW firstW); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1587 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1588 | template<class UnaryOperation> |
| 1589 | piecewise_linear_distribution(initializer_list<result_type> bl, |
| 1590 | UnaryOperation fw); |
| 1591 | |
| 1592 | template<class UnaryOperation> |
| 1593 | piecewise_linear_distribution(size_t nw, result_type xmin, |
| 1594 | result_type xmax, UnaryOperation fw); |
| 1595 | |
| 1596 | explicit piecewise_linear_distribution(const param_type& parm); |
| 1597 | void reset(); |
| 1598 | |
| 1599 | // generating functions |
| 1600 | template<class URNG> result_type operator()(URNG& g); |
| 1601 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1602 | |
| 1603 | // property functions |
| 1604 | vector<result_type> intervals() const; |
Howard Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 1605 | vector<result_type> densities() const; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1606 | |
| 1607 | param_type param() const; |
| 1608 | void param(const param_type& parm); |
| 1609 | |
| 1610 | result_type min() const; |
| 1611 | result_type max() const; |
| 1612 | |
| 1613 | friend bool operator==(const piecewise_linear_distribution& x, |
| 1614 | const piecewise_linear_distribution& y); |
| 1615 | friend bool operator!=(const piecewise_linear_distribution& x, |
| 1616 | const piecewise_linear_distribution& y); |
| 1617 | |
| 1618 | template <class charT, class traits> |
| 1619 | friend |
| 1620 | basic_ostream<charT, traits>& |
| 1621 | operator<<(basic_ostream<charT, traits>& os, |
| 1622 | const piecewise_linear_distribution& x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1623 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 1624 | template <class charT, class traits> |
| 1625 | friend |
| 1626 | basic_istream<charT, traits>& |
| 1627 | operator>>(basic_istream<charT, traits>& is, |
| 1628 | piecewise_linear_distribution& x); |
| 1629 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1630 | |
| 1631 | } // std |
| 1632 | */ |
| 1633 | |
| 1634 | #include <__config> |
| 1635 | #include <cstddef> |
Eric Fiselier | 90adc20 | 2015-01-23 22:22:36 +0000 | [diff] [blame] | 1636 | #include <cstdint> |
| 1637 | #include <cmath> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1638 | #include <type_traits> |
| 1639 | #include <initializer_list> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1640 | #include <limits> |
| 1641 | #include <algorithm> |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 1642 | #include <numeric> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1643 | #include <vector> |
| 1644 | #include <string> |
| 1645 | #include <istream> |
| 1646 | #include <ostream> |
| 1647 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 1648 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1649 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 1650 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1651 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 1652 | _LIBCPP_PUSH_MACROS |
| 1653 | #include <__undef_macros> |
| 1654 | |
| 1655 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1656 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 1657 | |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1658 | // __is_seed_sequence |
| 1659 | |
| 1660 | template <class _Sseq, class _Engine> |
| 1661 | struct __is_seed_sequence |
| 1662 | { |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1663 | static _LIBCPP_CONSTEXPR const bool value = |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1664 | !is_convertible<_Sseq, typename _Engine::result_type>::value && |
| 1665 | !is_same<typename remove_cv<_Sseq>::type, _Engine>::value; |
| 1666 | }; |
| 1667 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1668 | // linear_congruential_engine |
| 1669 | |
| 1670 | template <unsigned long long __a, unsigned long long __c, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1671 | unsigned long long __m, unsigned long long _Mp, |
| 1672 | bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_Mp-__c)/__a)> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1673 | struct __lce_ta; |
| 1674 | |
| 1675 | // 64 |
| 1676 | |
| 1677 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 1678 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true> |
| 1679 | { |
| 1680 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1681 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1682 | static result_type next(result_type __x) |
| 1683 | { |
| 1684 | // Schrage's algorithm |
| 1685 | const result_type __q = __m / __a; |
| 1686 | const result_type __r = __m % __a; |
| 1687 | const result_type __t0 = __a * (__x % __q); |
| 1688 | const result_type __t1 = __r * (__x / __q); |
| 1689 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1690 | __x += __c - (__x >= __m - __c) * __m; |
| 1691 | return __x; |
| 1692 | } |
| 1693 | }; |
| 1694 | |
| 1695 | template <unsigned long long __a, unsigned long long __m> |
| 1696 | struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true> |
| 1697 | { |
| 1698 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1699 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1700 | static result_type next(result_type __x) |
| 1701 | { |
| 1702 | // Schrage's algorithm |
| 1703 | const result_type __q = __m / __a; |
| 1704 | const result_type __r = __m % __a; |
| 1705 | const result_type __t0 = __a * (__x % __q); |
| 1706 | const result_type __t1 = __r * (__x / __q); |
| 1707 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1708 | return __x; |
| 1709 | } |
| 1710 | }; |
| 1711 | |
| 1712 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 1713 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false> |
| 1714 | { |
| 1715 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1716 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1717 | static result_type next(result_type __x) |
| 1718 | { |
| 1719 | return (__a * __x + __c) % __m; |
| 1720 | } |
| 1721 | }; |
| 1722 | |
| 1723 | template <unsigned long long __a, unsigned long long __c> |
| 1724 | struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false> |
| 1725 | { |
| 1726 | typedef unsigned long long result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1727 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1728 | static result_type next(result_type __x) |
| 1729 | { |
| 1730 | return __a * __x + __c; |
| 1731 | } |
| 1732 | }; |
| 1733 | |
| 1734 | // 32 |
| 1735 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1736 | template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp> |
| 1737 | struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), true> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1738 | { |
| 1739 | typedef unsigned result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1740 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1741 | static result_type next(result_type __x) |
| 1742 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1743 | const result_type __a = static_cast<result_type>(_Ap); |
| 1744 | const result_type __c = static_cast<result_type>(_Cp); |
| 1745 | const result_type __m = static_cast<result_type>(_Mp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1746 | // Schrage's algorithm |
| 1747 | const result_type __q = __m / __a; |
| 1748 | const result_type __r = __m % __a; |
| 1749 | const result_type __t0 = __a * (__x % __q); |
| 1750 | const result_type __t1 = __r * (__x / __q); |
| 1751 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1752 | __x += __c - (__x >= __m - __c) * __m; |
| 1753 | return __x; |
| 1754 | } |
| 1755 | }; |
| 1756 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1757 | template <unsigned long long _Ap, unsigned long long _Mp> |
| 1758 | struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1759 | { |
| 1760 | typedef unsigned result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1761 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1762 | static result_type next(result_type __x) |
| 1763 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1764 | const result_type __a = static_cast<result_type>(_Ap); |
| 1765 | const result_type __m = static_cast<result_type>(_Mp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1766 | // Schrage's algorithm |
| 1767 | const result_type __q = __m / __a; |
| 1768 | const result_type __r = __m % __a; |
| 1769 | const result_type __t0 = __a * (__x % __q); |
| 1770 | const result_type __t1 = __r * (__x / __q); |
| 1771 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1772 | return __x; |
| 1773 | } |
| 1774 | }; |
| 1775 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1776 | template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp> |
| 1777 | struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), false> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1778 | { |
| 1779 | typedef unsigned result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1780 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1781 | static result_type next(result_type __x) |
| 1782 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1783 | const result_type __a = static_cast<result_type>(_Ap); |
| 1784 | const result_type __c = static_cast<result_type>(_Cp); |
| 1785 | const result_type __m = static_cast<result_type>(_Mp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1786 | return (__a * __x + __c) % __m; |
| 1787 | } |
| 1788 | }; |
| 1789 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1790 | template <unsigned long long _Ap, unsigned long long _Cp> |
| 1791 | struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1792 | { |
| 1793 | typedef unsigned result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1794 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1795 | static result_type next(result_type __x) |
| 1796 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1797 | const result_type __a = static_cast<result_type>(_Ap); |
| 1798 | const result_type __c = static_cast<result_type>(_Cp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1799 | return __a * __x + __c; |
| 1800 | } |
| 1801 | }; |
| 1802 | |
| 1803 | // 16 |
| 1804 | |
| 1805 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b> |
| 1806 | struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b> |
| 1807 | { |
| 1808 | typedef unsigned short result_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1809 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1810 | static result_type next(result_type __x) |
| 1811 | { |
| 1812 | return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x)); |
| 1813 | } |
| 1814 | }; |
| 1815 | |
| 1816 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1817 | class _LIBCPP_TEMPLATE_VIS linear_congruential_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1818 | |
| 1819 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1820 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 1821 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1822 | basic_ostream<_CharT, _Traits>& |
| 1823 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1824 | const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1825 | |
| 1826 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1827 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1828 | basic_istream<_CharT, _Traits>& |
| 1829 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1830 | linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1831 | |
| 1832 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1833 | class _LIBCPP_TEMPLATE_VIS linear_congruential_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1834 | { |
| 1835 | public: |
| 1836 | // types |
| 1837 | typedef _UIntType result_type; |
| 1838 | |
Howard Hinnant | a741b24 | 2013-05-21 21:19:35 +0000 | [diff] [blame] | 1839 | private: |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1840 | result_type __x_; |
| 1841 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1842 | static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1843 | |
| 1844 | static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters"); |
| 1845 | static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters"); |
| 1846 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1847 | static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u: 0u; |
| 1848 | static _LIBCPP_CONSTEXPR const result_type _Max = __m - 1u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1849 | static_assert(_Min < _Max, "linear_congruential_engine invalid parameters"); |
| 1850 | |
| 1851 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1852 | static _LIBCPP_CONSTEXPR const result_type multiplier = __a; |
| 1853 | static _LIBCPP_CONSTEXPR const result_type increment = __c; |
| 1854 | static _LIBCPP_CONSTEXPR const result_type modulus = __m; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1855 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1856 | static _LIBCPP_CONSTEXPR result_type min() {return _Min;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1857 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 1858 | static _LIBCPP_CONSTEXPR result_type max() {return _Max;} |
| 1859 | static _LIBCPP_CONSTEXPR const result_type default_seed = 1u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1860 | |
| 1861 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1862 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1863 | explicit linear_congruential_engine(result_type __s = default_seed) |
| 1864 | {seed(__s);} |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1865 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1866 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1867 | explicit linear_congruential_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1868 | 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] | 1869 | {seed(__q);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1870 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1871 | void seed(result_type __s = default_seed) |
| 1872 | {seed(integral_constant<bool, __m == 0>(), |
| 1873 | integral_constant<bool, __c == 0>(), __s);} |
| 1874 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1875 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1876 | typename enable_if |
| 1877 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 1878 | __is_seed_sequence<_Sseq, linear_congruential_engine>::value, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1879 | void |
| 1880 | >::type |
| 1881 | seed(_Sseq& __q) |
| 1882 | {__seed(__q, integral_constant<unsigned, |
| 1883 | 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32 |
Howard Hinnant | a0c4f7c | 2013-05-21 21:05:12 +0000 | [diff] [blame] | 1884 | : (__m > 0x100000000ull))>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1885 | |
| 1886 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1887 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1888 | result_type operator()() |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1889 | {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] | 1890 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1891 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 1892 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1893 | friend _LIBCPP_INLINE_VISIBILITY |
| 1894 | bool operator==(const linear_congruential_engine& __x, |
| 1895 | const linear_congruential_engine& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1896 | {return __x.__x_ == __y.__x_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1897 | friend _LIBCPP_INLINE_VISIBILITY |
| 1898 | bool operator!=(const linear_congruential_engine& __x, |
| 1899 | const linear_congruential_engine& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1900 | {return !(__x == __y);} |
| 1901 | |
| 1902 | private: |
| 1903 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1904 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1905 | 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] | 1906 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1907 | void seed(true_type, false_type, result_type __s) {__x_ = __s;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1908 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1909 | void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ? |
| 1910 | 1 : __s % __m;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1911 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1912 | void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;} |
| 1913 | |
| 1914 | template<class _Sseq> |
| 1915 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 1916 | template<class _Sseq> |
| 1917 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 1918 | |
| 1919 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1920 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1921 | friend |
| 1922 | basic_ostream<_CharT, _Traits>& |
| 1923 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1924 | const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1925 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1926 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1927 | class _Up, _Up _Ap, _Up _Cp, _Up _Np> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1928 | friend |
| 1929 | basic_istream<_CharT, _Traits>& |
| 1930 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1931 | linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1932 | }; |
| 1933 | |
| 1934 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 1935 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 1936 | linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier; |
| 1937 | |
| 1938 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1939 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 1940 | linear_congruential_engine<_UIntType, __a, __c, __m>::increment; |
| 1941 | |
| 1942 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1943 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 1944 | linear_congruential_engine<_UIntType, __a, __c, __m>::modulus; |
| 1945 | |
| 1946 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1947 | _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type |
| 1948 | linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed; |
| 1949 | |
| 1950 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1951 | template<class _Sseq> |
| 1952 | void |
| 1953 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 1954 | integral_constant<unsigned, 1>) |
| 1955 | { |
| 1956 | const unsigned __k = 1; |
| 1957 | uint32_t __ar[__k+3]; |
| 1958 | __q.generate(__ar, __ar + __k + 3); |
| 1959 | result_type __s = static_cast<result_type>(__ar[3] % __m); |
| 1960 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 1961 | } |
| 1962 | |
| 1963 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1964 | template<class _Sseq> |
| 1965 | void |
| 1966 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 1967 | integral_constant<unsigned, 2>) |
| 1968 | { |
| 1969 | const unsigned __k = 2; |
| 1970 | uint32_t __ar[__k+3]; |
| 1971 | __q.generate(__ar, __ar + __k + 3); |
| 1972 | result_type __s = static_cast<result_type>((__ar[3] + |
Howard Hinnant | a0c4f7c | 2013-05-21 21:05:12 +0000 | [diff] [blame] | 1973 | ((uint64_t)__ar[4] << 32)) % __m); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1974 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 1975 | } |
| 1976 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1977 | template <class _CharT, class _Traits, |
| 1978 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 1979 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1980 | basic_ostream<_CharT, _Traits>& |
| 1981 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1982 | const linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 1983 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 1984 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1985 | __os.flags(ios_base::dec | ios_base::left); |
| 1986 | __os.fill(__os.widen(' ')); |
| 1987 | return __os << __x.__x_; |
| 1988 | } |
| 1989 | |
| 1990 | template <class _CharT, class _Traits, |
| 1991 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1992 | basic_istream<_CharT, _Traits>& |
| 1993 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1994 | linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 1995 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 1996 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1997 | __is.flags(ios_base::dec | ios_base::skipws); |
| 1998 | _UIntType __t; |
| 1999 | __is >> __t; |
| 2000 | if (!__is.fail()) |
| 2001 | __x.__x_ = __t; |
| 2002 | return __is; |
| 2003 | } |
| 2004 | |
| 2005 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> |
| 2006 | minstd_rand0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2007 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> |
| 2008 | minstd_rand; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 2009 | typedef minstd_rand default_random_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2010 | // mersenne_twister_engine |
| 2011 | |
| 2012 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2013 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2014 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2015 | class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2016 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2017 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2018 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2019 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2020 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2021 | 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] | 2022 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2023 | 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] | 2024 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2025 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2026 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2027 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2028 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 2029 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2030 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2031 | 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] | 2032 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2033 | 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] | 2034 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2035 | |
| 2036 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2037 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2038 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2039 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2040 | basic_ostream<_CharT, _Traits>& |
| 2041 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2042 | 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] | 2043 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2044 | |
| 2045 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2046 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2047 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2048 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2049 | basic_istream<_CharT, _Traits>& |
| 2050 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2051 | mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2052 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2053 | |
| 2054 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2055 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2056 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2057 | class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2058 | { |
| 2059 | public: |
| 2060 | // types |
| 2061 | typedef _UIntType result_type; |
| 2062 | |
| 2063 | private: |
| 2064 | result_type __x_[__n]; |
| 2065 | size_t __i_; |
| 2066 | |
| 2067 | static_assert( 0 < __m, "mersenne_twister_engine invalid parameters"); |
| 2068 | static_assert(__m <= __n, "mersenne_twister_engine invalid parameters"); |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2069 | static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2070 | static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters"); |
| 2071 | static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters"); |
| 2072 | static_assert(__r <= __w, "mersenne_twister_engine invalid parameters"); |
| 2073 | static_assert(__u <= __w, "mersenne_twister_engine invalid parameters"); |
| 2074 | static_assert(__s <= __w, "mersenne_twister_engine invalid parameters"); |
| 2075 | static_assert(__t <= __w, "mersenne_twister_engine invalid parameters"); |
| 2076 | static_assert(__l <= __w, "mersenne_twister_engine invalid parameters"); |
| 2077 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2078 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 2079 | static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2080 | (result_type(1) << __w) - result_type(1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2081 | static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters"); |
| 2082 | static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2083 | static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2084 | static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2085 | static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2086 | static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters"); |
| 2087 | |
| 2088 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2089 | static _LIBCPP_CONSTEXPR const size_t word_size = __w; |
| 2090 | static _LIBCPP_CONSTEXPR const size_t state_size = __n; |
| 2091 | static _LIBCPP_CONSTEXPR const size_t shift_size = __m; |
| 2092 | static _LIBCPP_CONSTEXPR const size_t mask_bits = __r; |
| 2093 | static _LIBCPP_CONSTEXPR const result_type xor_mask = __a; |
| 2094 | static _LIBCPP_CONSTEXPR const size_t tempering_u = __u; |
| 2095 | static _LIBCPP_CONSTEXPR const result_type tempering_d = __d; |
| 2096 | static _LIBCPP_CONSTEXPR const size_t tempering_s = __s; |
| 2097 | static _LIBCPP_CONSTEXPR const result_type tempering_b = __b; |
| 2098 | static _LIBCPP_CONSTEXPR const size_t tempering_t = __t; |
| 2099 | static _LIBCPP_CONSTEXPR const result_type tempering_c = __c; |
| 2100 | static _LIBCPP_CONSTEXPR const size_t tempering_l = __l; |
| 2101 | static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2102 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2103 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2104 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2105 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
| 2106 | static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2107 | |
| 2108 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2109 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2110 | explicit mersenne_twister_engine(result_type __sd = default_seed) |
| 2111 | {seed(__sd);} |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2112 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2113 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2114 | explicit mersenne_twister_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2115 | 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] | 2116 | {seed(__q);} |
| 2117 | void seed(result_type __sd = default_seed); |
| 2118 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2119 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2120 | typename enable_if |
| 2121 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2122 | __is_seed_sequence<_Sseq, mersenne_twister_engine>::value, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2123 | void |
| 2124 | >::type |
| 2125 | seed(_Sseq& __q) |
| 2126 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2127 | |
| 2128 | // generating functions |
| 2129 | result_type operator()(); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2130 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2131 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2132 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2133 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2134 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2135 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2136 | friend |
| 2137 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2138 | 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] | 2139 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2140 | 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] | 2141 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2142 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2143 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2144 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2145 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2146 | friend |
| 2147 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2148 | 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] | 2149 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2150 | 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] | 2151 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2152 | |
| 2153 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2154 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2155 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2156 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2157 | friend |
| 2158 | basic_ostream<_CharT, _Traits>& |
| 2159 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2160 | 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] | 2161 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2162 | |
| 2163 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2164 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2165 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2166 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2167 | friend |
| 2168 | basic_istream<_CharT, _Traits>& |
| 2169 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2170 | mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2171 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2172 | private: |
| 2173 | |
| 2174 | template<class _Sseq> |
| 2175 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 2176 | template<class _Sseq> |
| 2177 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 2178 | |
| 2179 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2180 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2181 | static |
| 2182 | typename enable_if |
| 2183 | < |
| 2184 | __count < __w, |
| 2185 | result_type |
| 2186 | >::type |
| 2187 | __lshift(result_type __x) {return (__x << __count) & _Max;} |
| 2188 | |
| 2189 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2190 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2191 | static |
| 2192 | typename enable_if |
| 2193 | < |
| 2194 | (__count >= __w), |
| 2195 | result_type |
| 2196 | >::type |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2197 | __lshift(result_type) {return result_type(0);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2198 | |
| 2199 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2200 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2201 | static |
| 2202 | typename enable_if |
| 2203 | < |
| 2204 | __count < _Dt, |
| 2205 | result_type |
| 2206 | >::type |
| 2207 | __rshift(result_type __x) {return __x >> __count;} |
| 2208 | |
| 2209 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2210 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2211 | static |
| 2212 | typename enable_if |
| 2213 | < |
| 2214 | (__count >= _Dt), |
| 2215 | result_type |
| 2216 | >::type |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2217 | __rshift(result_type) {return result_type(0);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2218 | }; |
| 2219 | |
| 2220 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2221 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2222 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2223 | _LIBCPP_CONSTEXPR const size_t |
| 2224 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size; |
| 2225 | |
| 2226 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2227 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2228 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2229 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2230 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size; |
| 2231 | |
| 2232 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2233 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2234 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2235 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2236 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size; |
| 2237 | |
| 2238 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2239 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2240 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2241 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2242 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits; |
| 2243 | |
| 2244 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2245 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2246 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2247 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2248 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask; |
| 2249 | |
| 2250 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2251 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2252 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2253 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2254 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u; |
| 2255 | |
| 2256 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2257 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2258 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2259 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2260 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d; |
| 2261 | |
| 2262 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2263 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2264 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2265 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2266 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s; |
| 2267 | |
| 2268 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2269 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2270 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2271 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2272 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b; |
| 2273 | |
| 2274 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2275 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2276 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2277 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2278 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t; |
| 2279 | |
| 2280 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2281 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2282 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2283 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2284 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c; |
| 2285 | |
| 2286 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2287 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2288 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2289 | _LIBCPP_CONSTEXPR const size_t |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2290 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l; |
| 2291 | |
| 2292 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2293 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2294 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2295 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2296 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::initialization_multiplier; |
| 2297 | |
| 2298 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2299 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2300 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2301 | _LIBCPP_CONSTEXPR const typename mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::result_type |
| 2302 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed; |
| 2303 | |
| 2304 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2305 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2306 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2307 | void |
| 2308 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2309 | __t, __c, __l, __f>::seed(result_type __sd) |
Marshall Clow | c189463 | 2017-09-11 18:10:33 +0000 | [diff] [blame] | 2310 | _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2311 | { // __w >= 2 |
| 2312 | __x_[0] = __sd & _Max; |
| 2313 | for (size_t __i = 1; __i < __n; ++__i) |
| 2314 | __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max; |
| 2315 | __i_ = 0; |
| 2316 | } |
| 2317 | |
| 2318 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2319 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2320 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2321 | template<class _Sseq> |
| 2322 | void |
| 2323 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2324 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>) |
| 2325 | { |
| 2326 | const unsigned __k = 1; |
| 2327 | uint32_t __ar[__n * __k]; |
| 2328 | __q.generate(__ar, __ar + __n * __k); |
| 2329 | for (size_t __i = 0; __i < __n; ++__i) |
| 2330 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 2331 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2332 | (result_type(1) << __r) - result_type(1); |
| 2333 | __i_ = 0; |
| 2334 | if ((__x_[0] & ~__mask) == 0) |
| 2335 | { |
| 2336 | for (size_t __i = 1; __i < __n; ++__i) |
| 2337 | if (__x_[__i] != 0) |
| 2338 | return; |
Hubert Tong | ca5b776 | 2018-08-16 23:56:54 +0000 | [diff] [blame] | 2339 | __x_[0] = result_type(1) << (__w - 1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 2344 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 2345 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 2346 | template<class _Sseq> |
| 2347 | void |
| 2348 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2349 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>) |
| 2350 | { |
| 2351 | const unsigned __k = 2; |
| 2352 | uint32_t __ar[__n * __k]; |
| 2353 | __q.generate(__ar, __ar + __n * __k); |
| 2354 | for (size_t __i = 0; __i < __n; ++__i) |
| 2355 | __x_[__i] = static_cast<result_type>( |
| 2356 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 2357 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2358 | (result_type(1) << __r) - result_type(1); |
| 2359 | __i_ = 0; |
| 2360 | if ((__x_[0] & ~__mask) == 0) |
| 2361 | { |
| 2362 | for (size_t __i = 1; __i < __n; ++__i) |
| 2363 | if (__x_[__i] != 0) |
| 2364 | return; |
Hubert Tong | ca5b776 | 2018-08-16 23:56:54 +0000 | [diff] [blame] | 2365 | __x_[0] = result_type(1) << (__w - 1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2366 | } |
| 2367 | } |
| 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 | _UIntType |
| 2373 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 2374 | __t, __c, __l, __f>::operator()() |
| 2375 | { |
| 2376 | const size_t __j = (__i_ + 1) % __n; |
| 2377 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 2378 | (result_type(1) << __r) - result_type(1); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2379 | const result_type _Yp = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2380 | const size_t __k = (__i_ + __m) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2381 | __x_[__i_] = __x_[__k] ^ __rshift<1>(_Yp) ^ (__a * (_Yp & 1)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2382 | result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d); |
| 2383 | __i_ = __j; |
| 2384 | __z ^= __lshift<__s>(__z) & __b; |
| 2385 | __z ^= __lshift<__t>(__z) & __c; |
| 2386 | return __z ^ __rshift<__l>(__z); |
| 2387 | } |
| 2388 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2389 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2390 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2391 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2392 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2393 | 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] | 2394 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2395 | 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] | 2396 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2397 | { |
| 2398 | if (__x.__i_ == __y.__i_) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2399 | return _VSTD::equal(__x.__x_, __x.__x_ + _Np, __y.__x_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2400 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 2401 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2402 | size_t __j = _VSTD::min(_Np - __x.__i_, _Np - __y.__i_); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2403 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2404 | __y.__x_ + __y.__i_)) |
| 2405 | return false; |
| 2406 | if (__x.__i_ == 0) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2407 | return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Np, __y.__x_); |
| 2408 | return _VSTD::equal(__x.__x_, __x.__x_ + (_Np - __j), __y.__x_ + __j); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2409 | } |
| 2410 | if (__x.__i_ < __y.__i_) |
| 2411 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2412 | size_t __j = _Np - __y.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2413 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2414 | __y.__x_ + __y.__i_)) |
| 2415 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2416 | if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Np, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2417 | __y.__x_)) |
| 2418 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2419 | return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2420 | __y.__x_ + (_Np - (__x.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2421 | } |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2422 | size_t __j = _Np - __x.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2423 | if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2424 | __x.__x_ + __x.__i_)) |
| 2425 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2426 | if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Np, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2427 | __x.__x_)) |
| 2428 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2429 | return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2430 | __x.__x_ + (_Np - (__y.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2433 | template <class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2434 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2435 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2436 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2437 | bool |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2438 | 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] | 2439 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2440 | 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] | 2441 | _Bp, _Tp, _Cp, _Lp, _Fp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2442 | { |
| 2443 | return !(__x == __y); |
| 2444 | } |
| 2445 | |
| 2446 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2447 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2448 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2449 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2450 | basic_ostream<_CharT, _Traits>& |
| 2451 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2452 | 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] | 2453 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2454 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2455 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2456 | __os.flags(ios_base::dec | ios_base::left); |
| 2457 | _CharT __sp = __os.widen(' '); |
| 2458 | __os.fill(__sp); |
| 2459 | __os << __x.__x_[__x.__i_]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2460 | for (size_t __j = __x.__i_ + 1; __j < _Np; ++__j) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2461 | __os << __sp << __x.__x_[__j]; |
| 2462 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 2463 | __os << __sp << __x.__x_[__j]; |
| 2464 | return __os; |
| 2465 | } |
| 2466 | |
| 2467 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2468 | class _UInt, size_t _Wp, size_t _Np, size_t _Mp, size_t _Rp, |
| 2469 | _UInt _Ap, size_t _Up, _UInt _Dp, size_t _Sp, |
| 2470 | _UInt _Bp, size_t _Tp, _UInt _Cp, size_t _Lp, _UInt _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2471 | basic_istream<_CharT, _Traits>& |
| 2472 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2473 | mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2474 | _Bp, _Tp, _Cp, _Lp, _Fp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2475 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2476 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2477 | __is.flags(ios_base::dec | ios_base::skipws); |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2478 | _UInt __t[_Np]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2479 | for (size_t __i = 0; __i < _Np; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2480 | __is >> __t[__i]; |
| 2481 | if (!__is.fail()) |
| 2482 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2483 | for (size_t __i = 0; __i < _Np; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2484 | __x.__x_[__i] = __t[__i]; |
| 2485 | __x.__i_ = 0; |
| 2486 | } |
| 2487 | return __is; |
| 2488 | } |
| 2489 | |
| 2490 | typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, |
| 2491 | 0x9908b0df, 11, 0xffffffff, |
| 2492 | 7, 0x9d2c5680, |
| 2493 | 15, 0xefc60000, |
| 2494 | 18, 1812433253> mt19937; |
| 2495 | typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31, |
| 2496 | 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL, |
| 2497 | 17, 0x71d67fffeda60000ULL, |
| 2498 | 37, 0xfff7eee000000000ULL, |
| 2499 | 43, 6364136223846793005ULL> mt19937_64; |
| 2500 | |
| 2501 | // subtract_with_carry_engine |
| 2502 | |
| 2503 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2504 | class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2505 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2506 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2507 | bool |
| 2508 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2509 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2510 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2511 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2512 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 2513 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2514 | bool |
| 2515 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2516 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2517 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2518 | |
| 2519 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2520 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2521 | basic_ostream<_CharT, _Traits>& |
| 2522 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2523 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2524 | |
| 2525 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2526 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2527 | basic_istream<_CharT, _Traits>& |
| 2528 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2529 | subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2530 | |
| 2531 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2532 | class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2533 | { |
| 2534 | public: |
| 2535 | // types |
| 2536 | typedef _UIntType result_type; |
| 2537 | |
| 2538 | private: |
| 2539 | result_type __x_[__r]; |
| 2540 | result_type __c_; |
| 2541 | size_t __i_; |
| 2542 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2543 | static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2544 | static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters"); |
| 2545 | static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters"); |
| 2546 | static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters"); |
| 2547 | static_assert(__s < __r, "subtract_with_carry_engine invalid parameters"); |
| 2548 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2549 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 2550 | static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2551 | (result_type(1) << __w) - result_type(1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2552 | static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters"); |
| 2553 | |
| 2554 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2555 | static _LIBCPP_CONSTEXPR const size_t word_size = __w; |
| 2556 | static _LIBCPP_CONSTEXPR const size_t short_lag = __s; |
| 2557 | static _LIBCPP_CONSTEXPR const size_t long_lag = __r; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2558 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2559 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2560 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2561 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
| 2562 | static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2563 | |
| 2564 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2565 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2566 | explicit subtract_with_carry_engine(result_type __sd = default_seed) |
| 2567 | {seed(__sd);} |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2568 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2569 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 2570 | explicit subtract_with_carry_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2571 | 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] | 2572 | {seed(__q);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2573 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2574 | void seed(result_type __sd = default_seed) |
| 2575 | {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2576 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2577 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2578 | typename enable_if |
| 2579 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2580 | __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2581 | void |
| 2582 | >::type |
| 2583 | seed(_Sseq& __q) |
| 2584 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2585 | |
| 2586 | // generating functions |
| 2587 | result_type operator()(); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2588 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2589 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 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 | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2592 | friend |
| 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 | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2597 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2598 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2599 | friend |
| 2600 | bool |
| 2601 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2602 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2603 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2604 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2605 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2606 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2607 | friend |
| 2608 | basic_ostream<_CharT, _Traits>& |
| 2609 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2610 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2611 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2612 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2613 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2614 | friend |
| 2615 | basic_istream<_CharT, _Traits>& |
| 2616 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2617 | subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2618 | |
| 2619 | private: |
| 2620 | |
| 2621 | void seed(result_type __sd, integral_constant<unsigned, 1>); |
| 2622 | void seed(result_type __sd, integral_constant<unsigned, 2>); |
| 2623 | template<class _Sseq> |
| 2624 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 2625 | template<class _Sseq> |
| 2626 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 2627 | }; |
| 2628 | |
| 2629 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2630 | _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size; |
| 2631 | |
| 2632 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2633 | _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag; |
| 2634 | |
| 2635 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2636 | _LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag; |
| 2637 | |
| 2638 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2639 | _LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type |
| 2640 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed; |
| 2641 | |
| 2642 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2643 | void |
| 2644 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 2645 | integral_constant<unsigned, 1>) |
| 2646 | { |
| 2647 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 2648 | __e(__sd == 0u ? default_seed : __sd); |
| 2649 | for (size_t __i = 0; __i < __r; ++__i) |
| 2650 | __x_[__i] = static_cast<result_type>(__e() & _Max); |
| 2651 | __c_ = __x_[__r-1] == 0; |
| 2652 | __i_ = 0; |
| 2653 | } |
| 2654 | |
| 2655 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2656 | void |
| 2657 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 2658 | integral_constant<unsigned, 2>) |
| 2659 | { |
| 2660 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 2661 | __e(__sd == 0u ? default_seed : __sd); |
| 2662 | for (size_t __i = 0; __i < __r; ++__i) |
Howard Hinnant | 93072c1 | 2011-08-15 17:22:22 +0000 | [diff] [blame] | 2663 | { |
| 2664 | result_type __e0 = __e(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2665 | __x_[__i] = static_cast<result_type>( |
Howard Hinnant | 93072c1 | 2011-08-15 17:22:22 +0000 | [diff] [blame] | 2666 | (__e0 + ((uint64_t)__e() << 32)) & _Max); |
| 2667 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2668 | __c_ = __x_[__r-1] == 0; |
| 2669 | __i_ = 0; |
| 2670 | } |
| 2671 | |
| 2672 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2673 | template<class _Sseq> |
| 2674 | void |
| 2675 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 2676 | integral_constant<unsigned, 1>) |
| 2677 | { |
| 2678 | const unsigned __k = 1; |
| 2679 | uint32_t __ar[__r * __k]; |
| 2680 | __q.generate(__ar, __ar + __r * __k); |
| 2681 | for (size_t __i = 0; __i < __r; ++__i) |
| 2682 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 2683 | __c_ = __x_[__r-1] == 0; |
| 2684 | __i_ = 0; |
| 2685 | } |
| 2686 | |
| 2687 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2688 | template<class _Sseq> |
| 2689 | void |
| 2690 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 2691 | integral_constant<unsigned, 2>) |
| 2692 | { |
| 2693 | const unsigned __k = 2; |
| 2694 | uint32_t __ar[__r * __k]; |
| 2695 | __q.generate(__ar, __ar + __r * __k); |
| 2696 | for (size_t __i = 0; __i < __r; ++__i) |
| 2697 | __x_[__i] = static_cast<result_type>( |
| 2698 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 2699 | __c_ = __x_[__r-1] == 0; |
| 2700 | __i_ = 0; |
| 2701 | } |
| 2702 | |
| 2703 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2704 | _UIntType |
| 2705 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()() |
| 2706 | { |
| 2707 | const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r]; |
| 2708 | result_type& __xr = __x_[__i_]; |
| 2709 | result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1; |
| 2710 | __xr = (__xs - __xr - __c_) & _Max; |
| 2711 | __c_ = __new_c; |
| 2712 | __i_ = (__i_ + 1) % __r; |
| 2713 | return __xr; |
| 2714 | } |
| 2715 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2716 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2717 | bool |
| 2718 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2719 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2720 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2721 | { |
| 2722 | if (__x.__c_ != __y.__c_) |
| 2723 | return false; |
| 2724 | if (__x.__i_ == __y.__i_) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2725 | return _VSTD::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2726 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 2727 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2728 | size_t __j = _VSTD::min(_Rp - __x.__i_, _Rp - __y.__i_); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2729 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2730 | __y.__x_ + __y.__i_)) |
| 2731 | return false; |
| 2732 | if (__x.__i_ == 0) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2733 | return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_); |
| 2734 | return _VSTD::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2735 | } |
| 2736 | if (__x.__i_ < __y.__i_) |
| 2737 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2738 | size_t __j = _Rp - __y.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2739 | if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2740 | __y.__x_ + __y.__i_)) |
| 2741 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2742 | if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2743 | __y.__x_)) |
| 2744 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2745 | return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2746 | __y.__x_ + (_Rp - (__x.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2747 | } |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2748 | size_t __j = _Rp - __x.__i_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2749 | if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2750 | __x.__x_ + __x.__i_)) |
| 2751 | return false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2752 | if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2753 | __x.__x_)) |
| 2754 | return false; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2755 | return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2756 | __x.__x_ + (_Rp - (__y.__i_ + __j))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2759 | template<class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2760 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2761 | bool |
| 2762 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2763 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x, |
| 2764 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2765 | { |
| 2766 | return !(__x == __y); |
| 2767 | } |
| 2768 | |
| 2769 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2770 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2771 | basic_ostream<_CharT, _Traits>& |
| 2772 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2773 | const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2774 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2775 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2776 | __os.flags(ios_base::dec | ios_base::left); |
| 2777 | _CharT __sp = __os.widen(' '); |
| 2778 | __os.fill(__sp); |
| 2779 | __os << __x.__x_[__x.__i_]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2780 | for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2781 | __os << __sp << __x.__x_[__j]; |
| 2782 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 2783 | __os << __sp << __x.__x_[__j]; |
| 2784 | __os << __sp << __x.__c_; |
| 2785 | return __os; |
| 2786 | } |
| 2787 | |
| 2788 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2789 | class _UInt, size_t _Wp, size_t _Sp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2790 | basic_istream<_CharT, _Traits>& |
| 2791 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2792 | subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2793 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2794 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2795 | __is.flags(ios_base::dec | ios_base::skipws); |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2796 | _UInt __t[_Rp+1]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2797 | for (size_t __i = 0; __i < _Rp+1; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2798 | __is >> __t[__i]; |
| 2799 | if (!__is.fail()) |
| 2800 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2801 | for (size_t __i = 0; __i < _Rp; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2802 | __x.__x_[__i] = __t[__i]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2803 | __x.__c_ = __t[_Rp]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2804 | __x.__i_ = 0; |
| 2805 | } |
| 2806 | return __is; |
| 2807 | } |
| 2808 | |
| 2809 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base; |
| 2810 | typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base; |
| 2811 | |
| 2812 | // discard_block_engine |
| 2813 | |
| 2814 | template<class _Engine, size_t __p, size_t __r> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2815 | class _LIBCPP_TEMPLATE_VIS discard_block_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2816 | { |
| 2817 | _Engine __e_; |
| 2818 | int __n_; |
| 2819 | |
| 2820 | static_assert( 0 < __r, "discard_block_engine invalid parameters"); |
| 2821 | static_assert(__r <= __p, "discard_block_engine invalid parameters"); |
Eric Fiselier | 37c2215 | 2016-12-24 00:24:44 +0000 | [diff] [blame] | 2822 | static_assert(__r <= INT_MAX, "discard_block_engine invalid parameters"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2823 | public: |
| 2824 | // types |
| 2825 | typedef typename _Engine::result_type result_type; |
| 2826 | |
| 2827 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2828 | static _LIBCPP_CONSTEXPR const size_t block_size = __p; |
| 2829 | static _LIBCPP_CONSTEXPR const size_t used_block = __r; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2830 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 2831 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2832 | static const result_type _Min = _Engine::_Min; |
| 2833 | static const result_type _Max = _Engine::_Max; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2834 | #else |
| 2835 | static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min(); |
| 2836 | static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max(); |
| 2837 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2838 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2839 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2840 | static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2841 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2842 | static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2843 | |
| 2844 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2845 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2846 | discard_block_engine() : __n_(0) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2847 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2848 | explicit discard_block_engine(const _Engine& __e) |
| 2849 | : __e_(__e), __n_(0) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 2850 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2851 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2852 | explicit discard_block_engine(_Engine&& __e) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2853 | : __e_(_VSTD::move(__e)), __n_(0) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 2854 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2855 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2856 | explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2857 | template<class _Sseq> |
| 2858 | _LIBCPP_INLINE_VISIBILITY |
| 2859 | explicit discard_block_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2860 | typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value && |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2861 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2862 | : __e_(__q), __n_(0) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2863 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2864 | void seed() {__e_.seed(); __n_ = 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2865 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2866 | void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;} |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2867 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2868 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2869 | typename enable_if |
| 2870 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 2871 | __is_seed_sequence<_Sseq, discard_block_engine>::value, |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 2872 | void |
| 2873 | >::type |
| 2874 | seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2875 | |
| 2876 | // generating functions |
| 2877 | result_type operator()(); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2878 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2879 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2880 | |
| 2881 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2882 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 2883 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2884 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2885 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2886 | friend |
| 2887 | bool |
| 2888 | operator==( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2889 | const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 2890 | const discard_block_engine<_Eng, _Pp, _Rp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2891 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2892 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2893 | friend |
| 2894 | bool |
| 2895 | operator!=( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2896 | const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 2897 | const discard_block_engine<_Eng, _Pp, _Rp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2898 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2899 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2900 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2901 | friend |
| 2902 | basic_ostream<_CharT, _Traits>& |
| 2903 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2904 | const discard_block_engine<_Eng, _Pp, _Rp>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2905 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2906 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2907 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2908 | friend |
| 2909 | basic_istream<_CharT, _Traits>& |
| 2910 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2911 | discard_block_engine<_Eng, _Pp, _Rp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2912 | }; |
| 2913 | |
| 2914 | template<class _Engine, size_t __p, size_t __r> |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 2915 | _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size; |
| 2916 | |
| 2917 | template<class _Engine, size_t __p, size_t __r> |
| 2918 | _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block; |
| 2919 | |
| 2920 | template<class _Engine, size_t __p, size_t __r> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2921 | typename discard_block_engine<_Engine, __p, __r>::result_type |
| 2922 | discard_block_engine<_Engine, __p, __r>::operator()() |
| 2923 | { |
Eric Fiselier | 37c2215 | 2016-12-24 00:24:44 +0000 | [diff] [blame] | 2924 | if (__n_ >= static_cast<int>(__r)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2925 | { |
| 2926 | __e_.discard(__p - __r); |
| 2927 | __n_ = 0; |
| 2928 | } |
| 2929 | ++__n_; |
| 2930 | return __e_(); |
| 2931 | } |
| 2932 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2933 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2934 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2935 | bool |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2936 | operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 2937 | const discard_block_engine<_Eng, _Pp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2938 | { |
| 2939 | return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_; |
| 2940 | } |
| 2941 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2942 | template<class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 2943 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2944 | bool |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2945 | operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, |
| 2946 | const discard_block_engine<_Eng, _Pp, _Rp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2947 | { |
| 2948 | return !(__x == __y); |
| 2949 | } |
| 2950 | |
| 2951 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2952 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2953 | basic_ostream<_CharT, _Traits>& |
| 2954 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2955 | const discard_block_engine<_Eng, _Pp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2956 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2957 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2958 | __os.flags(ios_base::dec | ios_base::left); |
| 2959 | _CharT __sp = __os.widen(' '); |
| 2960 | __os.fill(__sp); |
| 2961 | return __os << __x.__e_ << __sp << __x.__n_; |
| 2962 | } |
| 2963 | |
| 2964 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2965 | class _Eng, size_t _Pp, size_t _Rp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2966 | basic_istream<_CharT, _Traits>& |
| 2967 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2968 | discard_block_engine<_Eng, _Pp, _Rp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2969 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 2970 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2971 | __is.flags(ios_base::dec | ios_base::skipws); |
| 2972 | _Eng __e; |
| 2973 | int __n; |
| 2974 | __is >> __e >> __n; |
| 2975 | if (!__is.fail()) |
| 2976 | { |
| 2977 | __x.__e_ = __e; |
| 2978 | __x.__n_ = __n; |
| 2979 | } |
| 2980 | return __is; |
| 2981 | } |
| 2982 | |
| 2983 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
| 2984 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
| 2985 | |
| 2986 | // independent_bits_engine |
| 2987 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2988 | template<class _Engine, size_t __w, class _UIntType> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2989 | class _LIBCPP_TEMPLATE_VIS independent_bits_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2990 | { |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2991 | template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2992 | class __get_n |
| 2993 | { |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2994 | static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2995 | static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0); |
| 2996 | static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np; |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 2997 | static _LIBCPP_CONSTEXPR const _UInt _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2998 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 2999 | 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] | 3000 | }; |
| 3001 | public: |
| 3002 | // types |
| 3003 | typedef _UIntType result_type; |
| 3004 | |
| 3005 | private: |
| 3006 | _Engine __e_; |
| 3007 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3008 | static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3009 | static_assert( 0 < __w, "independent_bits_engine invalid parameters"); |
| 3010 | static_assert(__w <= _Dt, "independent_bits_engine invalid parameters"); |
| 3011 | |
| 3012 | typedef typename _Engine::result_type _Engine_result_type; |
| 3013 | typedef typename conditional |
| 3014 | < |
| 3015 | sizeof(_Engine_result_type) <= sizeof(result_type), |
| 3016 | result_type, |
| 3017 | _Engine_result_type |
| 3018 | >::type _Working_result_type; |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3019 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3020 | static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3021 | + _Working_result_type(1); |
| 3022 | #else |
| 3023 | static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min() |
| 3024 | + _Working_result_type(1); |
| 3025 | #endif |
| 3026 | static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value; |
| 3027 | static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value; |
| 3028 | static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n; |
| 3029 | static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n; |
| 3030 | static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits; |
| 3031 | static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits; |
| 3032 | static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 : |
| 3033 | (_Rp >> __w0) << __w0; |
| 3034 | static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : |
| 3035 | (_Rp >> (__w0+1)) << (__w0+1); |
| 3036 | static _LIBCPP_CONSTEXPR const _Engine_result_type __mask0 = __w0 > 0 ? |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3037 | _Engine_result_type(~0) >> (_EDt - __w0) : |
| 3038 | _Engine_result_type(0); |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3039 | static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 = __w0 < _EDt - 1 ? |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3040 | _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : |
| 3041 | _Engine_result_type(~0); |
| 3042 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3043 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 3044 | static _LIBCPP_CONSTEXPR const result_type _Max = __w == _Dt ? result_type(~0) : |
| 3045 | (result_type(1) << __w) - result_type(1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3046 | static_assert(_Min < _Max, "independent_bits_engine invalid parameters"); |
| 3047 | |
| 3048 | // engine characteristics |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3049 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3050 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3051 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3052 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3053 | |
| 3054 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3055 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3056 | independent_bits_engine() {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3057 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3058 | explicit independent_bits_engine(const _Engine& __e) |
| 3059 | : __e_(__e) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3060 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3061 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3062 | explicit independent_bits_engine(_Engine&& __e) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3063 | : __e_(_VSTD::move(__e)) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3064 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3065 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3066 | explicit independent_bits_engine(result_type __sd) : __e_(__sd) {} |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3067 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3068 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3069 | explicit independent_bits_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3070 | typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value && |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3071 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3072 | : __e_(__q) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3073 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3074 | void seed() {__e_.seed();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3075 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3076 | void seed(result_type __sd) {__e_.seed(__sd);} |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3077 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3078 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3079 | typename enable_if |
| 3080 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3081 | __is_seed_sequence<_Sseq, independent_bits_engine>::value, |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3082 | void |
| 3083 | >::type |
| 3084 | seed(_Sseq& __q) {__e_.seed(__q);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3085 | |
| 3086 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3087 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3088 | result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3089 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3090 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 3091 | |
| 3092 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3093 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3094 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3095 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3096 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3097 | friend |
| 3098 | bool |
| 3099 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3100 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x, |
| 3101 | const independent_bits_engine<_Eng, _Wp, _UInt>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3102 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3103 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3104 | friend |
| 3105 | bool |
| 3106 | operator!=( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3107 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x, |
| 3108 | const independent_bits_engine<_Eng, _Wp, _UInt>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3109 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3110 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3111 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3112 | friend |
| 3113 | basic_ostream<_CharT, _Traits>& |
| 3114 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3115 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3116 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3117 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3118 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3119 | friend |
| 3120 | basic_istream<_CharT, _Traits>& |
| 3121 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3122 | independent_bits_engine<_Eng, _Wp, _UInt>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3123 | |
| 3124 | private: |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3125 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | fe77858 | 2017-09-20 19:38:43 +0000 | [diff] [blame] | 3126 | result_type __eval(false_type); |
| 3127 | result_type __eval(true_type); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3128 | |
| 3129 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3130 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3131 | static |
| 3132 | typename enable_if |
| 3133 | < |
| 3134 | __count < _Dt, |
| 3135 | result_type |
| 3136 | >::type |
| 3137 | __lshift(result_type __x) {return __x << __count;} |
| 3138 | |
| 3139 | template <size_t __count> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3140 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3141 | static |
| 3142 | typename enable_if |
| 3143 | < |
| 3144 | (__count >= _Dt), |
| 3145 | result_type |
| 3146 | >::type |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3147 | __lshift(result_type) {return result_type(0);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3148 | }; |
| 3149 | |
| 3150 | template<class _Engine, size_t __w, class _UIntType> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3151 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3152 | _UIntType |
Marshall Clow | fe77858 | 2017-09-20 19:38:43 +0000 | [diff] [blame] | 3153 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3154 | { |
| 3155 | return static_cast<result_type>(__e_() & __mask0); |
| 3156 | } |
| 3157 | |
| 3158 | template<class _Engine, size_t __w, class _UIntType> |
| 3159 | _UIntType |
Marshall Clow | fe77858 | 2017-09-20 19:38:43 +0000 | [diff] [blame] | 3160 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3161 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3162 | result_type _Sp = 0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3163 | for (size_t __k = 0; __k < __n0; ++__k) |
| 3164 | { |
| 3165 | _Engine_result_type __u; |
| 3166 | do |
| 3167 | { |
| 3168 | __u = __e_() - _Engine::min(); |
| 3169 | } while (__u >= __y0); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3170 | _Sp = static_cast<result_type>(__lshift<__w0>(_Sp) + (__u & __mask0)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3171 | } |
| 3172 | for (size_t __k = __n0; __k < __n; ++__k) |
| 3173 | { |
| 3174 | _Engine_result_type __u; |
| 3175 | do |
| 3176 | { |
| 3177 | __u = __e_() - _Engine::min(); |
| 3178 | } while (__u >= __y1); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3179 | _Sp = static_cast<result_type>(__lshift<__w0+1>(_Sp) + (__u & __mask1)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3180 | } |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3181 | return _Sp; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3184 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3185 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3186 | bool |
| 3187 | operator==( |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3188 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x, |
| 3189 | const independent_bits_engine<_Eng, _Wp, _UInt>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3190 | { |
| 3191 | return __x.base() == __y.base(); |
| 3192 | } |
| 3193 | |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3194 | template<class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3195 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3200 | { |
| 3201 | return !(__x == __y); |
| 3202 | } |
| 3203 | |
| 3204 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3205 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3206 | basic_ostream<_CharT, _Traits>& |
| 3207 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3208 | const independent_bits_engine<_Eng, _Wp, _UInt>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3209 | { |
| 3210 | return __os << __x.base(); |
| 3211 | } |
| 3212 | |
| 3213 | template <class _CharT, class _Traits, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3214 | class _Eng, size_t _Wp, class _UInt> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3215 | basic_istream<_CharT, _Traits>& |
| 3216 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Eric Fiselier | 4638fca | 2017-05-31 21:20:18 +0000 | [diff] [blame] | 3217 | independent_bits_engine<_Eng, _Wp, _UInt>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3218 | { |
| 3219 | _Eng __e; |
| 3220 | __is >> __e; |
| 3221 | if (!__is.fail()) |
| 3222 | __x.__e_ = __e; |
| 3223 | return __is; |
| 3224 | } |
| 3225 | |
| 3226 | // shuffle_order_engine |
| 3227 | |
| 3228 | template <uint64_t _Xp, uint64_t _Yp> |
| 3229 | struct __ugcd |
| 3230 | { |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3231 | static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3232 | }; |
| 3233 | |
| 3234 | template <uint64_t _Xp> |
| 3235 | struct __ugcd<_Xp, 0> |
| 3236 | { |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3237 | static _LIBCPP_CONSTEXPR const uint64_t value = _Xp; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3238 | }; |
| 3239 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3240 | template <uint64_t _Np, uint64_t _Dp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3241 | class __uratio |
| 3242 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3243 | static_assert(_Dp != 0, "__uratio divide by 0"); |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3244 | static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3245 | public: |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3246 | static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd; |
| 3247 | static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3248 | |
| 3249 | typedef __uratio<num, den> type; |
| 3250 | }; |
| 3251 | |
| 3252 | template<class _Engine, size_t __k> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3253 | class _LIBCPP_TEMPLATE_VIS shuffle_order_engine |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3254 | { |
| 3255 | static_assert(0 < __k, "shuffle_order_engine invalid parameters"); |
| 3256 | public: |
| 3257 | // types |
| 3258 | typedef typename _Engine::result_type result_type; |
| 3259 | |
| 3260 | private: |
| 3261 | _Engine __e_; |
| 3262 | result_type _V_[__k]; |
| 3263 | result_type _Y_; |
| 3264 | |
| 3265 | public: |
| 3266 | // engine characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3267 | static _LIBCPP_CONSTEXPR const size_t table_size = __k; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3268 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3269 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3270 | static const result_type _Min = _Engine::_Min; |
| 3271 | static const result_type _Max = _Engine::_Max; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3272 | #else |
| 3273 | static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min(); |
| 3274 | static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max(); |
| 3275 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3276 | static_assert(_Min < _Max, "shuffle_order_engine invalid parameters"); |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3277 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3278 | static _LIBCPP_CONSTEXPR result_type min() { return _Min; } |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3279 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3280 | static _LIBCPP_CONSTEXPR result_type max() { return _Max; } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3281 | |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3282 | static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3283 | |
| 3284 | // constructors and seeding functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3285 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3286 | shuffle_order_engine() {__init();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3287 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3288 | explicit shuffle_order_engine(const _Engine& __e) |
| 3289 | : __e_(__e) {__init();} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3290 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3291 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3292 | explicit shuffle_order_engine(_Engine&& __e) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3293 | : __e_(_VSTD::move(__e)) {__init();} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3294 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3295 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3296 | explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();} |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3297 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3298 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 3299 | explicit shuffle_order_engine(_Sseq& __q, |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3300 | typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value && |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3301 | !is_convertible<_Sseq, _Engine>::value>::type* = 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3302 | : __e_(__q) {__init();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3303 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3304 | void seed() {__e_.seed(); __init();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3305 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3306 | void seed(result_type __sd) {__e_.seed(__sd); __init();} |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3307 | template<class _Sseq> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3308 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3309 | typename enable_if |
| 3310 | < |
Howard Hinnant | c8c7399 | 2011-04-11 18:22:12 +0000 | [diff] [blame] | 3311 | __is_seed_sequence<_Sseq, shuffle_order_engine>::value, |
Howard Hinnant | 6047d5b | 2010-05-28 15:49:54 +0000 | [diff] [blame] | 3312 | void |
| 3313 | >::type |
| 3314 | seed(_Sseq& __q) {__e_.seed(__q); __init();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3315 | |
| 3316 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3317 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3318 | result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3319 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3320 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 3321 | |
| 3322 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3323 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3324 | const _Engine& base() const _NOEXCEPT {return __e_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3325 | |
| 3326 | private: |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3327 | template<class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3328 | friend |
| 3329 | bool |
| 3330 | operator==( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3331 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3332 | const shuffle_order_engine<_Eng, _Kp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3333 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3334 | template<class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3335 | friend |
| 3336 | bool |
| 3337 | operator!=( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3338 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3339 | const shuffle_order_engine<_Eng, _Kp>& __y); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3340 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3341 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3342 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3343 | friend |
| 3344 | basic_ostream<_CharT, _Traits>& |
| 3345 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3346 | const shuffle_order_engine<_Eng, _Kp>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3347 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3348 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3349 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3350 | friend |
| 3351 | basic_istream<_CharT, _Traits>& |
| 3352 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3353 | shuffle_order_engine<_Eng, _Kp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3354 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3355 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3356 | void __init() |
| 3357 | { |
| 3358 | for (size_t __i = 0; __i < __k; ++__i) |
| 3359 | _V_[__i] = __e_(); |
| 3360 | _Y_ = __e_(); |
| 3361 | } |
| 3362 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3363 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3364 | result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3365 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3366 | result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3367 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3368 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3369 | result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3370 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3371 | result_type __eval2(true_type) {return __evalf<__k, 0>();} |
| 3372 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3373 | template <uint64_t _Np, uint64_t _Dp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3374 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3375 | typename enable_if |
| 3376 | < |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3377 | (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3378 | result_type |
| 3379 | >::type |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3380 | __eval(__uratio<_Np, _Dp>) |
| 3381 | {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3382 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3383 | template <uint64_t _Np, uint64_t _Dp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3384 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3385 | typename enable_if |
| 3386 | < |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3387 | __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3388 | result_type |
| 3389 | >::type |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3390 | __eval(__uratio<_Np, _Dp>) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3391 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3392 | const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min) |
| 3393 | / __uratio<_Np, _Dp>::den); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3394 | _Y_ = _V_[__j]; |
| 3395 | _V_[__j] = __e_(); |
| 3396 | return _Y_; |
| 3397 | } |
| 3398 | |
| 3399 | template <uint64_t __n, uint64_t __d> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3400 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3401 | result_type __evalf() |
| 3402 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3403 | const double _Fp = __d == 0 ? |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3404 | __n / (2. * 0x8000000000000000ull) : |
| 3405 | __n / (double)__d; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3406 | const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3407 | _Y_ = _V_[__j]; |
| 3408 | _V_[__j] = __e_(); |
| 3409 | return _Y_; |
| 3410 | } |
| 3411 | }; |
| 3412 | |
Howard Hinnant | 2c45cb4 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 3413 | template<class _Engine, size_t __k> |
| 3414 | _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size; |
| 3415 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3416 | template<class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3417 | bool |
| 3418 | operator==( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3419 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3420 | const shuffle_order_engine<_Eng, _Kp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3421 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3422 | 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] | 3423 | __x.__e_ == __y.__e_; |
| 3424 | } |
| 3425 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3426 | template<class _Eng, size_t _Kp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3427 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3428 | bool |
| 3429 | operator!=( |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3430 | const shuffle_order_engine<_Eng, _Kp>& __x, |
| 3431 | const shuffle_order_engine<_Eng, _Kp>& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3432 | { |
| 3433 | return !(__x == __y); |
| 3434 | } |
| 3435 | |
| 3436 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3437 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3438 | basic_ostream<_CharT, _Traits>& |
| 3439 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3440 | const shuffle_order_engine<_Eng, _Kp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3441 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3442 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3443 | __os.flags(ios_base::dec | ios_base::left); |
| 3444 | _CharT __sp = __os.widen(' '); |
| 3445 | __os.fill(__sp); |
| 3446 | __os << __x.__e_ << __sp << __x._V_[0]; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3447 | for (size_t __i = 1; __i < _Kp; ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3448 | __os << __sp << __x._V_[__i]; |
| 3449 | return __os << __sp << __x._Y_; |
| 3450 | } |
| 3451 | |
| 3452 | template <class _CharT, class _Traits, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3453 | class _Eng, size_t _Kp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3454 | basic_istream<_CharT, _Traits>& |
| 3455 | operator>>(basic_istream<_CharT, _Traits>& __is, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3456 | shuffle_order_engine<_Eng, _Kp>& __x) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3457 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3458 | typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3459 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3460 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3461 | _Eng __e; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3462 | result_type _Vp[_Kp+1]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3463 | __is >> __e; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3464 | for (size_t __i = 0; __i < _Kp+1; ++__i) |
| 3465 | __is >> _Vp[__i]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3466 | if (!__is.fail()) |
| 3467 | { |
| 3468 | __x.__e_ = __e; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3469 | for (size_t __i = 0; __i < _Kp; ++__i) |
| 3470 | __x._V_[__i] = _Vp[__i]; |
| 3471 | __x._Y_ = _Vp[_Kp]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3472 | } |
| 3473 | return __is; |
| 3474 | } |
| 3475 | |
| 3476 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
| 3477 | |
| 3478 | // random_device |
| 3479 | |
Louis Dionne | 3777e68 | 2020-10-15 10:32:09 -0400 | [diff] [blame^] | 3480 | #if !defined(_LIBCPP_HAS_NO_RANDOM_DEVICE) |
| 3481 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 3482 | class _LIBCPP_TYPE_VIS random_device |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3483 | { |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 3484 | #ifdef _LIBCPP_USING_DEV_RANDOM |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3485 | int __f_; |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 3486 | #endif // defined(_LIBCPP_USING_DEV_RANDOM) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3487 | public: |
| 3488 | // types |
| 3489 | typedef unsigned result_type; |
| 3490 | |
| 3491 | // generator characteristics |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3492 | static _LIBCPP_CONSTEXPR const result_type _Min = 0; |
| 3493 | static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3494 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3495 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 664183b | 2012-04-02 00:40:41 +0000 | [diff] [blame] | 3496 | static _LIBCPP_CONSTEXPR result_type min() { return _Min;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3497 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 664183b | 2012-04-02 00:40:41 +0000 | [diff] [blame] | 3498 | static _LIBCPP_CONSTEXPR result_type max() { return _Max;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3499 | |
| 3500 | // constructors |
| 3501 | explicit random_device(const string& __token = "/dev/urandom"); |
| 3502 | ~random_device(); |
| 3503 | |
| 3504 | // generating functions |
| 3505 | result_type operator()(); |
| 3506 | |
| 3507 | // property functions |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 3508 | double entropy() const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3509 | |
| 3510 | private: |
| 3511 | // no copy functions |
| 3512 | random_device(const random_device&); // = delete; |
| 3513 | random_device& operator=(const random_device&); // = delete; |
| 3514 | }; |
| 3515 | |
Louis Dionne | 3777e68 | 2020-10-15 10:32:09 -0400 | [diff] [blame^] | 3516 | #endif // !_LIBCPP_HAS_NO_RANDOM_DEVICE |
| 3517 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3518 | // seed_seq |
| 3519 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3520 | class _LIBCPP_TEMPLATE_VIS seed_seq |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3521 | { |
| 3522 | public: |
| 3523 | // types |
| 3524 | typedef uint32_t result_type; |
| 3525 | |
| 3526 | private: |
| 3527 | vector<result_type> __v_; |
| 3528 | |
| 3529 | template<class _InputIterator> |
| 3530 | void init(_InputIterator __first, _InputIterator __last); |
| 3531 | public: |
| 3532 | // constructors |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3533 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 4b6b809 | 2013-10-23 05:56:47 +0000 | [diff] [blame] | 3534 | seed_seq() _NOEXCEPT {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3535 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3536 | template<class _Tp> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3537 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3538 | seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3539 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3540 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3541 | template<class _InputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3542 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3543 | seed_seq(_InputIterator __first, _InputIterator __last) |
| 3544 | {init(__first, __last);} |
| 3545 | |
| 3546 | // generating functions |
| 3547 | template<class _RandomAccessIterator> |
| 3548 | void generate(_RandomAccessIterator __first, _RandomAccessIterator __last); |
| 3549 | |
| 3550 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3551 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 4b6b809 | 2013-10-23 05:56:47 +0000 | [diff] [blame] | 3552 | size_t size() const _NOEXCEPT {return __v_.size();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3553 | template<class _OutputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3554 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3555 | void param(_OutputIterator __dest) const |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3556 | {_VSTD::copy(__v_.begin(), __v_.end(), __dest);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3557 | |
| 3558 | private: |
| 3559 | // no copy functions |
| 3560 | seed_seq(const seed_seq&); // = delete; |
| 3561 | void operator=(const seed_seq&); // = delete; |
| 3562 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3563 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3564 | static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3565 | }; |
| 3566 | |
| 3567 | template<class _InputIterator> |
| 3568 | void |
| 3569 | seed_seq::init(_InputIterator __first, _InputIterator __last) |
| 3570 | { |
| 3571 | for (_InputIterator __s = __first; __s != __last; ++__s) |
| 3572 | __v_.push_back(*__s & 0xFFFFFFFF); |
| 3573 | } |
| 3574 | |
| 3575 | template<class _RandomAccessIterator> |
| 3576 | void |
| 3577 | seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last) |
| 3578 | { |
| 3579 | if (__first != __last) |
| 3580 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3581 | _VSTD::fill(__first, __last, 0x8b8b8b8b); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3582 | const size_t __n = static_cast<size_t>(__last - __first); |
| 3583 | const size_t __s = __v_.size(); |
| 3584 | const size_t __t = (__n >= 623) ? 11 |
| 3585 | : (__n >= 68) ? 7 |
| 3586 | : (__n >= 39) ? 5 |
| 3587 | : (__n >= 7) ? 3 |
| 3588 | : (__n - 1) / 2; |
| 3589 | const size_t __p = (__n - __t) / 2; |
| 3590 | const size_t __q = __p + __t; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3591 | const size_t __m = _VSTD::max(__s + 1, __n); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3592 | // __k = 0; |
| 3593 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3594 | result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p] |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3595 | ^ __first[__n - 1]); |
| 3596 | __first[__p] += __r; |
| 3597 | __r += __s; |
| 3598 | __first[__q] += __r; |
| 3599 | __first[0] = __r; |
| 3600 | } |
| 3601 | for (size_t __k = 1; __k <= __s; ++__k) |
| 3602 | { |
| 3603 | const size_t __kmodn = __k % __n; |
| 3604 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3605 | result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3606 | ^ __first[(__k - 1) % __n]); |
| 3607 | __first[__kpmodn] += __r; |
| 3608 | __r += __kmodn + __v_[__k-1]; |
| 3609 | __first[(__k + __q) % __n] += __r; |
| 3610 | __first[__kmodn] = __r; |
| 3611 | } |
| 3612 | for (size_t __k = __s + 1; __k < __m; ++__k) |
| 3613 | { |
| 3614 | const size_t __kmodn = __k % __n; |
| 3615 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3616 | result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3617 | ^ __first[(__k - 1) % __n]); |
| 3618 | __first[__kpmodn] += __r; |
| 3619 | __r += __kmodn; |
| 3620 | __first[(__k + __q) % __n] += __r; |
| 3621 | __first[__kmodn] = __r; |
| 3622 | } |
| 3623 | for (size_t __k = __m; __k < __m + __n; ++__k) |
| 3624 | { |
| 3625 | const size_t __kmodn = __k % __n; |
| 3626 | const size_t __kpmodn = (__k + __p) % __n; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3627 | result_type __r = 1566083941 * _Tp(__first[__kmodn] + |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3628 | __first[__kpmodn] + |
| 3629 | __first[(__k - 1) % __n]); |
| 3630 | __first[__kpmodn] ^= __r; |
| 3631 | __r -= __kmodn; |
| 3632 | __first[(__k + __q) % __n] ^= __r; |
| 3633 | __first[__kmodn] = __r; |
| 3634 | } |
| 3635 | } |
| 3636 | } |
| 3637 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3638 | // generate_canonical |
| 3639 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3640 | template<class _RealType, size_t __bits, class _URNG> |
| 3641 | _RealType |
| 3642 | generate_canonical(_URNG& __g) |
| 3643 | { |
| 3644 | const size_t _Dt = numeric_limits<_RealType>::digits; |
| 3645 | const size_t __b = _Dt < __bits ? _Dt : __bits; |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 3646 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3647 | 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] | 3648 | #else |
| 3649 | const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value; |
| 3650 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3651 | const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0); |
Louis Dionne | 2422bdb | 2019-08-20 15:39:20 +0000 | [diff] [blame] | 3652 | const _RealType _Rp = static_cast<_RealType>(_URNG::max() - _URNG::min()) + _RealType(1); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3653 | _RealType __base = _Rp; |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3654 | _RealType _Sp = __g() - _URNG::min(); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3655 | for (size_t __i = 1; __i < __k; ++__i, __base *= _Rp) |
Howard Hinnant | 5a64685 | 2012-04-02 21:00:45 +0000 | [diff] [blame] | 3656 | _Sp += (__g() - _URNG::min()) * __base; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3657 | return _Sp / __base; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3660 | // uniform_int_distribution |
| 3661 | |
Howard Hinnant | 578ac0f | 2010-05-26 17:49:34 +0000 | [diff] [blame] | 3662 | // in <algorithm> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3663 | |
| 3664 | template <class _CharT, class _Traits, class _IT> |
| 3665 | basic_ostream<_CharT, _Traits>& |
| 3666 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3667 | const uniform_int_distribution<_IT>& __x) |
| 3668 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3669 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3670 | __os.flags(ios_base::dec | ios_base::left); |
| 3671 | _CharT __sp = __os.widen(' '); |
| 3672 | __os.fill(__sp); |
| 3673 | return __os << __x.a() << __sp << __x.b(); |
| 3674 | } |
| 3675 | |
| 3676 | template <class _CharT, class _Traits, class _IT> |
| 3677 | basic_istream<_CharT, _Traits>& |
| 3678 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3679 | uniform_int_distribution<_IT>& __x) |
| 3680 | { |
| 3681 | typedef uniform_int_distribution<_IT> _Eng; |
| 3682 | typedef typename _Eng::result_type result_type; |
| 3683 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3684 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3685 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3686 | result_type __a; |
| 3687 | result_type __b; |
| 3688 | __is >> __a >> __b; |
| 3689 | if (!__is.fail()) |
| 3690 | __x.param(param_type(__a, __b)); |
| 3691 | return __is; |
| 3692 | } |
| 3693 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3694 | // uniform_real_distribution |
| 3695 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3696 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3697 | class _LIBCPP_TEMPLATE_VIS uniform_real_distribution |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3698 | { |
| 3699 | public: |
| 3700 | // types |
| 3701 | typedef _RealType result_type; |
| 3702 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3703 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3704 | { |
| 3705 | result_type __a_; |
| 3706 | result_type __b_; |
| 3707 | public: |
| 3708 | typedef uniform_real_distribution distribution_type; |
| 3709 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3710 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3711 | explicit param_type(result_type __a = 0, |
| 3712 | result_type __b = 1) |
| 3713 | : __a_(__a), __b_(__b) {} |
| 3714 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3715 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3716 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3717 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3718 | result_type b() const {return __b_;} |
| 3719 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3720 | friend _LIBCPP_INLINE_VISIBILITY |
| 3721 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3722 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3723 | friend _LIBCPP_INLINE_VISIBILITY |
| 3724 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3725 | {return !(__x == __y);} |
| 3726 | }; |
| 3727 | |
| 3728 | private: |
| 3729 | param_type __p_; |
| 3730 | |
| 3731 | public: |
| 3732 | // constructors and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3733 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3734 | explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1) |
| 3735 | : __p_(param_type(__a, __b)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3736 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3737 | explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3738 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3739 | void reset() {} |
| 3740 | |
| 3741 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3742 | template<class _URNG> |
| 3743 | _LIBCPP_INLINE_VISIBILITY |
| 3744 | result_type operator()(_URNG& __g) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3745 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3746 | 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] | 3747 | |
| 3748 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3749 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3750 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3751 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3752 | result_type b() const {return __p_.b();} |
| 3753 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3754 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3755 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3756 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3757 | void param(const param_type& __p) {__p_ = __p;} |
| 3758 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3759 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3760 | result_type min() const {return a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3761 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3762 | result_type max() const {return b();} |
| 3763 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3764 | friend _LIBCPP_INLINE_VISIBILITY |
| 3765 | bool operator==(const uniform_real_distribution& __x, |
| 3766 | const uniform_real_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3767 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3768 | friend _LIBCPP_INLINE_VISIBILITY |
| 3769 | bool operator!=(const uniform_real_distribution& __x, |
| 3770 | const uniform_real_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3771 | {return !(__x == __y);} |
| 3772 | }; |
| 3773 | |
| 3774 | template<class _RealType> |
| 3775 | template<class _URNG> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3776 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3777 | typename uniform_real_distribution<_RealType>::result_type |
| 3778 | uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3779 | { |
| 3780 | return (__p.b() - __p.a()) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3781 | * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3782 | + __p.a(); |
| 3783 | } |
| 3784 | |
| 3785 | template <class _CharT, class _Traits, class _RT> |
| 3786 | basic_ostream<_CharT, _Traits>& |
| 3787 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3788 | const uniform_real_distribution<_RT>& __x) |
| 3789 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3790 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 3791 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 3792 | ios_base::scientific); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3793 | _CharT __sp = __os.widen(' '); |
| 3794 | __os.fill(__sp); |
| 3795 | return __os << __x.a() << __sp << __x.b(); |
| 3796 | } |
| 3797 | |
| 3798 | template <class _CharT, class _Traits, class _RT> |
| 3799 | basic_istream<_CharT, _Traits>& |
| 3800 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3801 | uniform_real_distribution<_RT>& __x) |
| 3802 | { |
| 3803 | typedef uniform_real_distribution<_RT> _Eng; |
| 3804 | typedef typename _Eng::result_type result_type; |
| 3805 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3806 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3807 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3808 | result_type __a; |
| 3809 | result_type __b; |
| 3810 | __is >> __a >> __b; |
| 3811 | if (!__is.fail()) |
| 3812 | __x.param(param_type(__a, __b)); |
| 3813 | return __is; |
| 3814 | } |
| 3815 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3816 | // bernoulli_distribution |
| 3817 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3818 | class _LIBCPP_TEMPLATE_VIS bernoulli_distribution |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3819 | { |
| 3820 | public: |
| 3821 | // types |
| 3822 | typedef bool result_type; |
| 3823 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3824 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3825 | { |
| 3826 | double __p_; |
| 3827 | public: |
| 3828 | typedef bernoulli_distribution distribution_type; |
| 3829 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3830 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3831 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 3832 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3833 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3834 | double p() const {return __p_;} |
| 3835 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3836 | friend _LIBCPP_INLINE_VISIBILITY |
| 3837 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3838 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3839 | friend _LIBCPP_INLINE_VISIBILITY |
| 3840 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3841 | {return !(__x == __y);} |
| 3842 | }; |
| 3843 | |
| 3844 | private: |
| 3845 | param_type __p_; |
| 3846 | |
| 3847 | public: |
| 3848 | // constructors and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3849 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3850 | explicit bernoulli_distribution(double __p = 0.5) |
| 3851 | : __p_(param_type(__p)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3852 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3853 | explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3854 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3855 | void reset() {} |
| 3856 | |
| 3857 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3858 | template<class _URNG> |
| 3859 | _LIBCPP_INLINE_VISIBILITY |
| 3860 | result_type operator()(_URNG& __g) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3861 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3862 | 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] | 3863 | |
| 3864 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3865 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3866 | double p() const {return __p_.p();} |
| 3867 | |
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 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3870 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3871 | void param(const param_type& __p) {__p_ = __p;} |
| 3872 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3873 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3874 | result_type min() const {return false;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3875 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3876 | result_type max() const {return true;} |
| 3877 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3878 | friend _LIBCPP_INLINE_VISIBILITY |
| 3879 | bool operator==(const bernoulli_distribution& __x, |
| 3880 | const bernoulli_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3881 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3882 | friend _LIBCPP_INLINE_VISIBILITY |
| 3883 | bool operator!=(const bernoulli_distribution& __x, |
| 3884 | const bernoulli_distribution& __y) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3885 | {return !(__x == __y);} |
| 3886 | }; |
| 3887 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3888 | template<class _URNG> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3889 | inline |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3890 | bernoulli_distribution::result_type |
| 3891 | bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) |
| 3892 | { |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 3893 | uniform_real_distribution<double> __gen; |
| 3894 | return __gen(__g) < __p.p(); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3897 | template <class _CharT, class _Traits> |
| 3898 | basic_ostream<_CharT, _Traits>& |
| 3899 | operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) |
| 3900 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3901 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 3902 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 3903 | ios_base::scientific); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3904 | _CharT __sp = __os.widen(' '); |
| 3905 | __os.fill(__sp); |
| 3906 | return __os << __x.p(); |
| 3907 | } |
| 3908 | |
| 3909 | template <class _CharT, class _Traits> |
| 3910 | basic_istream<_CharT, _Traits>& |
| 3911 | operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) |
| 3912 | { |
| 3913 | typedef bernoulli_distribution _Eng; |
| 3914 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3915 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3916 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3917 | double __p; |
| 3918 | __is >> __p; |
| 3919 | if (!__is.fail()) |
| 3920 | __x.param(param_type(__p)); |
| 3921 | return __is; |
| 3922 | } |
| 3923 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3924 | // binomial_distribution |
| 3925 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3926 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3927 | class _LIBCPP_TEMPLATE_VIS binomial_distribution |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3928 | { |
| 3929 | public: |
| 3930 | // types |
| 3931 | typedef _IntType result_type; |
| 3932 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3933 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3934 | { |
| 3935 | result_type __t_; |
| 3936 | double __p_; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3937 | double __pr_; |
| 3938 | double __odds_ratio_; |
| 3939 | result_type __r0_; |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3940 | public: |
| 3941 | typedef binomial_distribution distribution_type; |
| 3942 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3943 | explicit param_type(result_type __t = 1, double __p = 0.5); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3944 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3945 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3946 | result_type t() const {return __t_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3947 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3948 | double p() const {return __p_;} |
| 3949 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3950 | friend _LIBCPP_INLINE_VISIBILITY |
| 3951 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3952 | {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3953 | friend _LIBCPP_INLINE_VISIBILITY |
| 3954 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3955 | {return !(__x == __y);} |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3956 | |
| 3957 | friend class binomial_distribution; |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3958 | }; |
| 3959 | |
| 3960 | private: |
| 3961 | param_type __p_; |
| 3962 | |
| 3963 | public: |
| 3964 | // constructors and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3965 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3966 | explicit binomial_distribution(result_type __t = 1, double __p = 0.5) |
| 3967 | : __p_(param_type(__t, __p)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3968 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3969 | explicit binomial_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3970 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3971 | void reset() {} |
| 3972 | |
| 3973 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3974 | template<class _URNG> |
| 3975 | _LIBCPP_INLINE_VISIBILITY |
| 3976 | result_type operator()(_URNG& __g) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3977 | {return (*this)(__g, __p_);} |
| 3978 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3979 | |
| 3980 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3981 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3982 | result_type t() const {return __p_.t();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3983 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3984 | double p() const {return __p_.p();} |
| 3985 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3986 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3987 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3988 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3989 | void param(const param_type& __p) {__p_ = __p;} |
| 3990 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3991 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3992 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3993 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3994 | result_type max() const {return t();} |
| 3995 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 3996 | friend _LIBCPP_INLINE_VISIBILITY |
| 3997 | bool operator==(const binomial_distribution& __x, |
| 3998 | const binomial_distribution& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3999 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4000 | friend _LIBCPP_INLINE_VISIBILITY |
| 4001 | bool operator!=(const binomial_distribution& __x, |
| 4002 | const binomial_distribution& __y) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4003 | {return !(__x == __y);} |
| 4004 | }; |
| 4005 | |
Eric Fiselier | 651ca6e | 2017-05-06 02:58:43 +0000 | [diff] [blame] | 4006 | #ifndef _LIBCPP_MSVCRT |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4007 | extern "C" double lgamma_r(double, int *); |
Eric Fiselier | 651ca6e | 2017-05-06 02:58:43 +0000 | [diff] [blame] | 4008 | #endif |
| 4009 | |
| 4010 | inline _LIBCPP_INLINE_VISIBILITY double __libcpp_lgamma(double __d) { |
| 4011 | #if defined(_LIBCPP_MSVCRT) |
| 4012 | return lgamma(__d); |
| 4013 | #else |
| 4014 | int __sign; |
| 4015 | return lgamma_r(__d, &__sign); |
| 4016 | #endif |
| 4017 | } |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4018 | |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4019 | template<class _IntType> |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4020 | 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] | 4021 | : __t_(__t), __p_(__p) |
| 4022 | { |
| 4023 | if (0 < __p_ && __p_ < 1) |
| 4024 | { |
| 4025 | __r0_ = static_cast<result_type>((__t_ + 1) * __p_); |
Eric Fiselier | 651ca6e | 2017-05-06 02:58:43 +0000 | [diff] [blame] | 4026 | __pr_ = _VSTD::exp(__libcpp_lgamma(__t_ + 1.) - |
| 4027 | __libcpp_lgamma(__r0_ + 1.) - |
| 4028 | __libcpp_lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) + |
Marshall Clow | 7db2857 | 2017-05-04 16:36:39 +0000 | [diff] [blame] | 4029 | (__t_ - __r0_) * _VSTD::log(1 - __p_)); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4030 | __odds_ratio_ = __p_ / (1 - __p_); |
| 4031 | } |
| 4032 | } |
| 4033 | |
Marshall Clow | f97a84f | 2014-09-17 18:33:58 +0000 | [diff] [blame] | 4034 | // Reference: Kemp, C.D. (1986). `A modal method for generating binomial |
| 4035 | // variables', Commun. Statist. - Theor. Meth. 15(3), 805-813. |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4036 | template<class _IntType> |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4037 | template<class _URNG> |
| 4038 | _IntType |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4039 | binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4040 | { |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4041 | if (__pr.__t_ == 0 || __pr.__p_ == 0) |
| 4042 | return 0; |
| 4043 | if (__pr.__p_ == 1) |
| 4044 | return __pr.__t_; |
| 4045 | uniform_real_distribution<double> __gen; |
| 4046 | double __u = __gen(__g) - __pr.__pr_; |
| 4047 | if (__u < 0) |
| 4048 | return __pr.__r0_; |
| 4049 | double __pu = __pr.__pr_; |
| 4050 | double __pd = __pu; |
| 4051 | result_type __ru = __pr.__r0_; |
| 4052 | result_type __rd = __ru; |
| 4053 | while (true) |
| 4054 | { |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4055 | bool __break = true; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4056 | if (__rd >= 1) |
| 4057 | { |
| 4058 | __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1)); |
| 4059 | __u -= __pd; |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4060 | __break = false; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4061 | if (__u < 0) |
| 4062 | return __rd - 1; |
| 4063 | } |
Marshall Clow | f97a84f | 2014-09-17 18:33:58 +0000 | [diff] [blame] | 4064 | if ( __rd != 0 ) |
| 4065 | --__rd; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4066 | ++__ru; |
| 4067 | if (__ru <= __pr.__t_) |
| 4068 | { |
| 4069 | __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru; |
| 4070 | __u -= __pu; |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4071 | __break = false; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4072 | if (__u < 0) |
| 4073 | return __ru; |
| 4074 | } |
Atmn Patel | da0b1b6 | 2020-03-17 15:52:36 -0400 | [diff] [blame] | 4075 | if (__break) |
| 4076 | return 0; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4077 | } |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | template <class _CharT, class _Traits, class _IntType> |
| 4081 | basic_ostream<_CharT, _Traits>& |
| 4082 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4083 | const binomial_distribution<_IntType>& __x) |
| 4084 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4085 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4086 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4087 | ios_base::scientific); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4088 | _CharT __sp = __os.widen(' '); |
| 4089 | __os.fill(__sp); |
| 4090 | return __os << __x.t() << __sp << __x.p(); |
| 4091 | } |
| 4092 | |
| 4093 | template <class _CharT, class _Traits, class _IntType> |
| 4094 | basic_istream<_CharT, _Traits>& |
| 4095 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4096 | binomial_distribution<_IntType>& __x) |
| 4097 | { |
| 4098 | typedef binomial_distribution<_IntType> _Eng; |
| 4099 | typedef typename _Eng::result_type result_type; |
| 4100 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4101 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 53f28fa | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 4102 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4103 | result_type __t; |
| 4104 | double __p; |
| 4105 | __is >> __t >> __p; |
| 4106 | if (!__is.fail()) |
| 4107 | __x.param(param_type(__t, __p)); |
| 4108 | return __is; |
| 4109 | } |
| 4110 | |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4111 | // exponential_distribution |
| 4112 | |
| 4113 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4114 | class _LIBCPP_TEMPLATE_VIS exponential_distribution |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4115 | { |
| 4116 | public: |
| 4117 | // types |
| 4118 | typedef _RealType result_type; |
| 4119 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4120 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4121 | { |
| 4122 | result_type __lambda_; |
| 4123 | public: |
| 4124 | typedef exponential_distribution distribution_type; |
| 4125 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4126 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4127 | explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {} |
| 4128 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4129 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4130 | result_type lambda() const {return __lambda_;} |
| 4131 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4132 | friend _LIBCPP_INLINE_VISIBILITY |
| 4133 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4134 | {return __x.__lambda_ == __y.__lambda_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4135 | friend _LIBCPP_INLINE_VISIBILITY |
| 4136 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4137 | {return !(__x == __y);} |
| 4138 | }; |
| 4139 | |
| 4140 | private: |
| 4141 | param_type __p_; |
| 4142 | |
| 4143 | public: |
| 4144 | // constructors and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4145 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4146 | explicit exponential_distribution(result_type __lambda = 1) |
| 4147 | : __p_(param_type(__lambda)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4148 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4149 | explicit exponential_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4150 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4151 | void reset() {} |
| 4152 | |
| 4153 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4154 | template<class _URNG> |
| 4155 | _LIBCPP_INLINE_VISIBILITY |
| 4156 | result_type operator()(_URNG& __g) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4157 | {return (*this)(__g, __p_);} |
| 4158 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4159 | |
| 4160 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4161 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4162 | result_type lambda() const {return __p_.lambda();} |
| 4163 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4164 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4165 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4166 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4167 | void param(const param_type& __p) {__p_ = __p;} |
| 4168 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4169 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4170 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4171 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 021f990 | 2010-05-16 17:56:20 +0000 | [diff] [blame] | 4172 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4173 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4174 | friend _LIBCPP_INLINE_VISIBILITY |
| 4175 | bool operator==(const exponential_distribution& __x, |
| 4176 | const exponential_distribution& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4177 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4178 | friend _LIBCPP_INLINE_VISIBILITY |
| 4179 | bool operator!=(const exponential_distribution& __x, |
| 4180 | const exponential_distribution& __y) |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4181 | {return !(__x == __y);} |
| 4182 | }; |
| 4183 | |
| 4184 | template <class _RealType> |
| 4185 | template<class _URNG> |
| 4186 | _RealType |
| 4187 | exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4188 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4189 | return -_VSTD::log |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4190 | ( |
| 4191 | result_type(1) - |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4192 | _VSTD::generate_canonical<result_type, |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4193 | numeric_limits<result_type>::digits>(__g) |
| 4194 | ) |
| 4195 | / __p.lambda(); |
| 4196 | } |
| 4197 | |
| 4198 | template <class _CharT, class _Traits, class _RealType> |
| 4199 | basic_ostream<_CharT, _Traits>& |
| 4200 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4201 | const exponential_distribution<_RealType>& __x) |
| 4202 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4203 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4204 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4205 | ios_base::scientific); |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4206 | return __os << __x.lambda(); |
| 4207 | } |
| 4208 | |
| 4209 | template <class _CharT, class _Traits, class _RealType> |
| 4210 | basic_istream<_CharT, _Traits>& |
| 4211 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4212 | exponential_distribution<_RealType>& __x) |
| 4213 | { |
| 4214 | typedef exponential_distribution<_RealType> _Eng; |
| 4215 | typedef typename _Eng::result_type result_type; |
| 4216 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4217 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | d31dfb5 | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 4218 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4219 | result_type __lambda; |
| 4220 | __is >> __lambda; |
| 4221 | if (!__is.fail()) |
| 4222 | __x.param(param_type(__lambda)); |
| 4223 | return __is; |
| 4224 | } |
| 4225 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4226 | // normal_distribution |
| 4227 | |
| 4228 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4229 | class _LIBCPP_TEMPLATE_VIS normal_distribution |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4230 | { |
| 4231 | public: |
| 4232 | // types |
| 4233 | typedef _RealType result_type; |
| 4234 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4235 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4236 | { |
| 4237 | result_type __mean_; |
| 4238 | result_type __stddev_; |
| 4239 | public: |
| 4240 | typedef normal_distribution distribution_type; |
| 4241 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4242 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4243 | explicit param_type(result_type __mean = 0, result_type __stddev = 1) |
| 4244 | : __mean_(__mean), __stddev_(__stddev) {} |
| 4245 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4246 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4247 | result_type mean() const {return __mean_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4248 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4249 | result_type stddev() const {return __stddev_;} |
| 4250 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4251 | friend _LIBCPP_INLINE_VISIBILITY |
| 4252 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4253 | {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4254 | friend _LIBCPP_INLINE_VISIBILITY |
| 4255 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4256 | {return !(__x == __y);} |
| 4257 | }; |
| 4258 | |
| 4259 | private: |
| 4260 | param_type __p_; |
| 4261 | result_type _V_; |
| 4262 | bool _V_hot_; |
| 4263 | |
| 4264 | public: |
| 4265 | // constructors and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4266 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4267 | explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1) |
| 4268 | : __p_(param_type(__mean, __stddev)), _V_hot_(false) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4269 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4270 | explicit normal_distribution(const param_type& __p) |
| 4271 | : __p_(__p), _V_hot_(false) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4272 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4273 | void reset() {_V_hot_ = false;} |
| 4274 | |
| 4275 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4276 | template<class _URNG> |
| 4277 | _LIBCPP_INLINE_VISIBILITY |
| 4278 | result_type operator()(_URNG& __g) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4279 | {return (*this)(__g, __p_);} |
| 4280 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4281 | |
| 4282 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4283 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4284 | result_type mean() const {return __p_.mean();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4285 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4286 | result_type stddev() const {return __p_.stddev();} |
| 4287 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4288 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4289 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4290 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4291 | void param(const param_type& __p) {__p_ = __p;} |
| 4292 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4293 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4294 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4295 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4296 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4297 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4298 | friend _LIBCPP_INLINE_VISIBILITY |
| 4299 | bool operator==(const normal_distribution& __x, |
| 4300 | const normal_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4301 | {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ && |
| 4302 | (!__x._V_hot_ || __x._V_ == __y._V_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4303 | friend _LIBCPP_INLINE_VISIBILITY |
| 4304 | bool operator!=(const normal_distribution& __x, |
| 4305 | const normal_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4306 | {return !(__x == __y);} |
| 4307 | |
| 4308 | template <class _CharT, class _Traits, class _RT> |
| 4309 | friend |
| 4310 | basic_ostream<_CharT, _Traits>& |
| 4311 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4312 | const normal_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4313 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4314 | template <class _CharT, class _Traits, class _RT> |
| 4315 | friend |
| 4316 | basic_istream<_CharT, _Traits>& |
| 4317 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4318 | normal_distribution<_RT>& __x); |
| 4319 | }; |
| 4320 | |
| 4321 | template <class _RealType> |
| 4322 | template<class _URNG> |
| 4323 | _RealType |
| 4324 | normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4325 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4326 | result_type _Up; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4327 | if (_V_hot_) |
| 4328 | { |
| 4329 | _V_hot_ = false; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4330 | _Up = _V_; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4331 | } |
| 4332 | else |
| 4333 | { |
| 4334 | uniform_real_distribution<result_type> _Uni(-1, 1); |
| 4335 | result_type __u; |
| 4336 | result_type __v; |
| 4337 | result_type __s; |
| 4338 | do |
| 4339 | { |
| 4340 | __u = _Uni(__g); |
| 4341 | __v = _Uni(__g); |
| 4342 | __s = __u * __u + __v * __v; |
| 4343 | } while (__s > 1 || __s == 0); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4344 | result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s); |
| 4345 | _V_ = __v * _Fp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4346 | _V_hot_ = true; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4347 | _Up = __u * _Fp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4348 | } |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4349 | return _Up * __p.stddev() + __p.mean(); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | template <class _CharT, class _Traits, class _RT> |
| 4353 | basic_ostream<_CharT, _Traits>& |
| 4354 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4355 | const normal_distribution<_RT>& __x) |
| 4356 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4357 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4358 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4359 | ios_base::scientific); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4360 | _CharT __sp = __os.widen(' '); |
| 4361 | __os.fill(__sp); |
| 4362 | __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_; |
| 4363 | if (__x._V_hot_) |
| 4364 | __os << __sp << __x._V_; |
| 4365 | return __os; |
| 4366 | } |
| 4367 | |
| 4368 | template <class _CharT, class _Traits, class _RT> |
| 4369 | basic_istream<_CharT, _Traits>& |
| 4370 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4371 | normal_distribution<_RT>& __x) |
| 4372 | { |
| 4373 | typedef normal_distribution<_RT> _Eng; |
| 4374 | typedef typename _Eng::result_type result_type; |
| 4375 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4376 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4377 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4378 | result_type __mean; |
| 4379 | result_type __stddev; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4380 | result_type _Vp = 0; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4381 | bool _V_hot = false; |
| 4382 | __is >> __mean >> __stddev >> _V_hot; |
| 4383 | if (_V_hot) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4384 | __is >> _Vp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4385 | if (!__is.fail()) |
| 4386 | { |
| 4387 | __x.param(param_type(__mean, __stddev)); |
| 4388 | __x._V_hot_ = _V_hot; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 4389 | __x._V_ = _Vp; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4390 | } |
| 4391 | return __is; |
| 4392 | } |
| 4393 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4394 | // lognormal_distribution |
| 4395 | |
| 4396 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4397 | class _LIBCPP_TEMPLATE_VIS lognormal_distribution |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4398 | { |
| 4399 | public: |
| 4400 | // types |
| 4401 | typedef _RealType result_type; |
| 4402 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4403 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4404 | { |
| 4405 | normal_distribution<result_type> __nd_; |
| 4406 | public: |
| 4407 | typedef lognormal_distribution distribution_type; |
| 4408 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4409 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4410 | explicit param_type(result_type __m = 0, result_type __s = 1) |
| 4411 | : __nd_(__m, __s) {} |
| 4412 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4413 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4414 | result_type m() const {return __nd_.mean();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4415 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4416 | result_type s() const {return __nd_.stddev();} |
| 4417 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4418 | friend _LIBCPP_INLINE_VISIBILITY |
| 4419 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4420 | {return __x.__nd_ == __y.__nd_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4421 | friend _LIBCPP_INLINE_VISIBILITY |
| 4422 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4423 | {return !(__x == __y);} |
| 4424 | friend class lognormal_distribution; |
| 4425 | |
| 4426 | template <class _CharT, class _Traits, class _RT> |
| 4427 | friend |
| 4428 | basic_ostream<_CharT, _Traits>& |
| 4429 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4430 | const lognormal_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4431 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4432 | template <class _CharT, class _Traits, class _RT> |
| 4433 | friend |
| 4434 | basic_istream<_CharT, _Traits>& |
| 4435 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4436 | lognormal_distribution<_RT>& __x); |
| 4437 | }; |
| 4438 | |
| 4439 | private: |
| 4440 | param_type __p_; |
| 4441 | |
| 4442 | public: |
| 4443 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4444 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4445 | explicit lognormal_distribution(result_type __m = 0, result_type __s = 1) |
| 4446 | : __p_(param_type(__m, __s)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4447 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4448 | explicit lognormal_distribution(const param_type& __p) |
| 4449 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4450 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4451 | void reset() {__p_.__nd_.reset();} |
| 4452 | |
| 4453 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4454 | template<class _URNG> |
| 4455 | _LIBCPP_INLINE_VISIBILITY |
| 4456 | result_type operator()(_URNG& __g) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4457 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4458 | template<class _URNG> |
| 4459 | _LIBCPP_INLINE_VISIBILITY |
| 4460 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4461 | {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));} |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4462 | |
| 4463 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4464 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4465 | result_type m() const {return __p_.m();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4466 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4467 | result_type s() const {return __p_.s();} |
| 4468 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4469 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4470 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4471 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 4472 | void param(const param_type& __p) {__p_ = __p;} |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4473 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4474 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4475 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4476 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4477 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4478 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4479 | friend _LIBCPP_INLINE_VISIBILITY |
| 4480 | bool operator==(const lognormal_distribution& __x, |
| 4481 | const lognormal_distribution& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4482 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4483 | friend _LIBCPP_INLINE_VISIBILITY |
| 4484 | bool operator!=(const lognormal_distribution& __x, |
| 4485 | const lognormal_distribution& __y) |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4486 | {return !(__x == __y);} |
| 4487 | |
| 4488 | template <class _CharT, class _Traits, class _RT> |
| 4489 | friend |
| 4490 | basic_ostream<_CharT, _Traits>& |
| 4491 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4492 | const lognormal_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4493 | |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4494 | template <class _CharT, class _Traits, class _RT> |
| 4495 | friend |
| 4496 | basic_istream<_CharT, _Traits>& |
| 4497 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4498 | lognormal_distribution<_RT>& __x); |
| 4499 | }; |
| 4500 | |
| 4501 | template <class _CharT, class _Traits, class _RT> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4502 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4503 | basic_ostream<_CharT, _Traits>& |
| 4504 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4505 | const lognormal_distribution<_RT>& __x) |
| 4506 | { |
| 4507 | return __os << __x.__p_.__nd_; |
| 4508 | } |
| 4509 | |
| 4510 | template <class _CharT, class _Traits, class _RT> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4511 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | df3bed6 | 2010-05-17 18:31:53 +0000 | [diff] [blame] | 4512 | basic_istream<_CharT, _Traits>& |
| 4513 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4514 | lognormal_distribution<_RT>& __x) |
| 4515 | { |
| 4516 | return __is >> __x.__p_.__nd_; |
| 4517 | } |
| 4518 | |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4519 | // poisson_distribution |
| 4520 | |
| 4521 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4522 | class _LIBCPP_TEMPLATE_VIS poisson_distribution |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4523 | { |
| 4524 | public: |
| 4525 | // types |
| 4526 | typedef _IntType result_type; |
| 4527 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4528 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4529 | { |
| 4530 | double __mean_; |
| 4531 | double __s_; |
| 4532 | double __d_; |
| 4533 | double __l_; |
| 4534 | double __omega_; |
| 4535 | double __c0_; |
| 4536 | double __c1_; |
| 4537 | double __c2_; |
| 4538 | double __c3_; |
| 4539 | double __c_; |
| 4540 | |
| 4541 | public: |
| 4542 | typedef poisson_distribution distribution_type; |
| 4543 | |
| 4544 | explicit param_type(double __mean = 1.0); |
| 4545 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4546 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4547 | double mean() const {return __mean_;} |
| 4548 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4549 | friend _LIBCPP_INLINE_VISIBILITY |
| 4550 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4551 | {return __x.__mean_ == __y.__mean_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4552 | friend _LIBCPP_INLINE_VISIBILITY |
| 4553 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4554 | {return !(__x == __y);} |
| 4555 | |
| 4556 | friend class poisson_distribution; |
| 4557 | }; |
| 4558 | |
| 4559 | private: |
| 4560 | param_type __p_; |
| 4561 | |
| 4562 | public: |
| 4563 | // constructors and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4564 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4565 | explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4566 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4567 | explicit poisson_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4568 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4569 | void reset() {} |
| 4570 | |
| 4571 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4572 | template<class _URNG> |
| 4573 | _LIBCPP_INLINE_VISIBILITY |
| 4574 | result_type operator()(_URNG& __g) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4575 | {return (*this)(__g, __p_);} |
| 4576 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4577 | |
| 4578 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4579 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4580 | double mean() const {return __p_.mean();} |
| 4581 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4582 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4583 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4584 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4585 | void param(const param_type& __p) {__p_ = __p;} |
| 4586 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4587 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4588 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4589 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4590 | result_type max() const {return numeric_limits<result_type>::max();} |
| 4591 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4592 | friend _LIBCPP_INLINE_VISIBILITY |
| 4593 | bool operator==(const poisson_distribution& __x, |
| 4594 | const poisson_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4595 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4596 | friend _LIBCPP_INLINE_VISIBILITY |
| 4597 | bool operator!=(const poisson_distribution& __x, |
| 4598 | const poisson_distribution& __y) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4599 | {return !(__x == __y);} |
| 4600 | }; |
| 4601 | |
| 4602 | template<class _IntType> |
| 4603 | poisson_distribution<_IntType>::param_type::param_type(double __mean) |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4604 | // According to the standard `inf` is a valid input, but it causes the |
| 4605 | // distribution to hang, so we replace it with the maximum representable |
| 4606 | // mean. |
| 4607 | : __mean_(isinf(__mean) ? numeric_limits<double>::max() : __mean) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4608 | { |
| 4609 | if (__mean_ < 10) |
| 4610 | { |
| 4611 | __s_ = 0; |
| 4612 | __d_ = 0; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4613 | __l_ = _VSTD::exp(-__mean_); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4614 | __omega_ = 0; |
| 4615 | __c3_ = 0; |
| 4616 | __c2_ = 0; |
| 4617 | __c1_ = 0; |
| 4618 | __c0_ = 0; |
| 4619 | __c_ = 0; |
| 4620 | } |
| 4621 | else |
| 4622 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4623 | __s_ = _VSTD::sqrt(__mean_); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4624 | __d_ = 6 * __mean_ * __mean_; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4625 | __l_ = std::trunc(__mean_ - 1.1484); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4626 | __omega_ = .3989423 / __s_; |
| 4627 | double __b1_ = .4166667E-1 / __mean_; |
| 4628 | double __b2_ = .3 * __b1_ * __b1_; |
| 4629 | __c3_ = .1428571 * __b1_ * __b2_; |
| 4630 | __c2_ = __b2_ - 15. * __c3_; |
| 4631 | __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_; |
| 4632 | __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_; |
| 4633 | __c_ = .1069 / __mean_; |
| 4634 | } |
| 4635 | } |
| 4636 | |
| 4637 | template <class _IntType> |
| 4638 | template<class _URNG> |
| 4639 | _IntType |
| 4640 | poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 4641 | { |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4642 | double __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4643 | uniform_real_distribution<double> __urd; |
Howard Hinnant | a5fb7ac | 2011-04-14 15:59:22 +0000 | [diff] [blame] | 4644 | if (__pr.__mean_ < 10) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4645 | { |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4646 | __tx = 0; |
| 4647 | for (double __p = __urd(__urng); __p > __pr.__l_; ++__tx) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4648 | __p *= __urd(__urng); |
| 4649 | } |
| 4650 | else |
| 4651 | { |
| 4652 | double __difmuk; |
| 4653 | double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng); |
| 4654 | double __u; |
| 4655 | if (__g > 0) |
| 4656 | { |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4657 | __tx = std::trunc(__g); |
| 4658 | if (__tx >= __pr.__l_) |
| 4659 | return std::__clamp_to_integral<result_type>(__tx); |
| 4660 | __difmuk = __pr.__mean_ - __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4661 | __u = __urd(__urng); |
| 4662 | if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk) |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4663 | return std::__clamp_to_integral<result_type>(__tx); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4664 | } |
| 4665 | exponential_distribution<double> __edist; |
| 4666 | for (bool __using_exp_dist = false; true; __using_exp_dist = true) |
| 4667 | { |
| 4668 | double __e; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4669 | if (__using_exp_dist || __g <= 0) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4670 | { |
| 4671 | double __t; |
| 4672 | do |
| 4673 | { |
| 4674 | __e = __edist(__urng); |
| 4675 | __u = __urd(__urng); |
| 4676 | __u += __u - 1; |
| 4677 | __t = 1.8 + (__u < 0 ? -__e : __e); |
| 4678 | } while (__t <= -.6744); |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4679 | __tx = std::trunc(__pr.__mean_ + __pr.__s_ * __t); |
| 4680 | __difmuk = __pr.__mean_ - __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4681 | __using_exp_dist = true; |
| 4682 | } |
| 4683 | double __px; |
| 4684 | double __py; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4685 | if (__tx < 10 && __tx >= 0) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4686 | { |
Marshall Clow | c7e36e8 | 2018-01-16 14:54:36 +0000 | [diff] [blame] | 4687 | const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4688 | 40320, 362880}; |
| 4689 | __px = -__pr.__mean_; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4690 | __py = _VSTD::pow(__pr.__mean_, (double)__tx) / __fac[static_cast<int>(__tx)]; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4691 | } |
| 4692 | else |
| 4693 | { |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4694 | double __del = .8333333E-1 / __tx; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4695 | __del -= 4.8 * __del * __del * __del; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4696 | double __v = __difmuk / __tx; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4697 | if (_VSTD::abs(__v) > 0.25) |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4698 | __px = __tx * _VSTD::log(1 + __v) - __difmuk - __del; |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4699 | else |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4700 | __px = __tx * __v * __v * (((((((.1250060 * __v + -.1384794) * |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4701 | __v + .1421878) * __v + -.1661269) * __v + .2000118) * |
| 4702 | __v + -.2500068) * __v + .3333333) * __v + -.5) - __del; |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4703 | __py = .3989423 / _VSTD::sqrt(__tx); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4704 | } |
| 4705 | double __r = (0.5 - __difmuk) / __pr.__s_; |
| 4706 | double __r2 = __r * __r; |
| 4707 | double __fx = -0.5 * __r2; |
| 4708 | double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) * |
| 4709 | __r2 + __pr.__c1_) * __r2 + __pr.__c0_); |
| 4710 | if (__using_exp_dist) |
| 4711 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4712 | if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) - |
| 4713 | __fy * _VSTD::exp(__fx + __e)) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4714 | break; |
| 4715 | } |
| 4716 | else |
| 4717 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4718 | if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx)) |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4719 | break; |
| 4720 | } |
| 4721 | } |
| 4722 | } |
Louis Dionne | 7d19d1c | 2019-11-07 12:06:14 +0000 | [diff] [blame] | 4723 | return std::__clamp_to_integral<result_type>(__tx); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | template <class _CharT, class _Traits, class _IntType> |
| 4727 | basic_ostream<_CharT, _Traits>& |
| 4728 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4729 | const poisson_distribution<_IntType>& __x) |
| 4730 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4731 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4732 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4733 | ios_base::scientific); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4734 | return __os << __x.mean(); |
| 4735 | } |
| 4736 | |
| 4737 | template <class _CharT, class _Traits, class _IntType> |
| 4738 | basic_istream<_CharT, _Traits>& |
| 4739 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4740 | poisson_distribution<_IntType>& __x) |
| 4741 | { |
| 4742 | typedef poisson_distribution<_IntType> _Eng; |
| 4743 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4744 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 7631035 | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 4745 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4746 | double __mean; |
| 4747 | __is >> __mean; |
| 4748 | if (!__is.fail()) |
| 4749 | __x.param(param_type(__mean)); |
| 4750 | return __is; |
| 4751 | } |
| 4752 | |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4753 | // weibull_distribution |
| 4754 | |
| 4755 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4756 | class _LIBCPP_TEMPLATE_VIS weibull_distribution |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4757 | { |
| 4758 | public: |
| 4759 | // types |
| 4760 | typedef _RealType result_type; |
| 4761 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4762 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4763 | { |
| 4764 | result_type __a_; |
| 4765 | result_type __b_; |
| 4766 | public: |
| 4767 | typedef weibull_distribution distribution_type; |
| 4768 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4769 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4770 | explicit param_type(result_type __a = 1, result_type __b = 1) |
| 4771 | : __a_(__a), __b_(__b) {} |
| 4772 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4773 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4774 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4775 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4776 | result_type b() const {return __b_;} |
| 4777 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4778 | friend _LIBCPP_INLINE_VISIBILITY |
| 4779 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4780 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4781 | friend _LIBCPP_INLINE_VISIBILITY |
| 4782 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4783 | {return !(__x == __y);} |
| 4784 | }; |
| 4785 | |
| 4786 | private: |
| 4787 | param_type __p_; |
| 4788 | |
| 4789 | public: |
| 4790 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4791 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4792 | explicit weibull_distribution(result_type __a = 1, result_type __b = 1) |
| 4793 | : __p_(param_type(__a, __b)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4794 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4795 | explicit weibull_distribution(const param_type& __p) |
| 4796 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4797 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4798 | void reset() {} |
| 4799 | |
| 4800 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4801 | template<class _URNG> |
| 4802 | _LIBCPP_INLINE_VISIBILITY |
| 4803 | result_type operator()(_URNG& __g) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4804 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4805 | template<class _URNG> |
| 4806 | _LIBCPP_INLINE_VISIBILITY |
| 4807 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4808 | {return __p.b() * |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4809 | _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());} |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4810 | |
| 4811 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4812 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4813 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4814 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4815 | result_type b() const {return __p_.b();} |
| 4816 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4817 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4818 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4819 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4820 | void param(const param_type& __p) {__p_ = __p;} |
| 4821 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4822 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4823 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4824 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4825 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4826 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4827 | friend _LIBCPP_INLINE_VISIBILITY |
| 4828 | bool operator==(const weibull_distribution& __x, |
| 4829 | const weibull_distribution& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4830 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4831 | friend _LIBCPP_INLINE_VISIBILITY |
| 4832 | bool operator!=(const weibull_distribution& __x, |
| 4833 | const weibull_distribution& __y) |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4834 | {return !(__x == __y);} |
| 4835 | }; |
| 4836 | |
| 4837 | template <class _CharT, class _Traits, class _RT> |
| 4838 | basic_ostream<_CharT, _Traits>& |
| 4839 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4840 | const weibull_distribution<_RT>& __x) |
| 4841 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4842 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4843 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4844 | ios_base::scientific); |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4845 | _CharT __sp = __os.widen(' '); |
| 4846 | __os.fill(__sp); |
| 4847 | __os << __x.a() << __sp << __x.b(); |
| 4848 | return __os; |
| 4849 | } |
| 4850 | |
| 4851 | template <class _CharT, class _Traits, class _RT> |
| 4852 | basic_istream<_CharT, _Traits>& |
| 4853 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4854 | weibull_distribution<_RT>& __x) |
| 4855 | { |
| 4856 | typedef weibull_distribution<_RT> _Eng; |
| 4857 | typedef typename _Eng::result_type result_type; |
| 4858 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4859 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | db6b97b | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4860 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4861 | result_type __a; |
| 4862 | result_type __b; |
| 4863 | __is >> __a >> __b; |
| 4864 | if (!__is.fail()) |
| 4865 | __x.param(param_type(__a, __b)); |
| 4866 | return __is; |
| 4867 | } |
| 4868 | |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4869 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4870 | class _LIBCPP_TEMPLATE_VIS extreme_value_distribution |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4871 | { |
| 4872 | public: |
| 4873 | // types |
| 4874 | typedef _RealType result_type; |
| 4875 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4876 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4877 | { |
| 4878 | result_type __a_; |
| 4879 | result_type __b_; |
| 4880 | public: |
| 4881 | typedef extreme_value_distribution distribution_type; |
| 4882 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4883 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4884 | explicit param_type(result_type __a = 0, result_type __b = 1) |
| 4885 | : __a_(__a), __b_(__b) {} |
| 4886 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4887 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4888 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4889 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4890 | result_type b() const {return __b_;} |
| 4891 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4892 | friend _LIBCPP_INLINE_VISIBILITY |
| 4893 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4894 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4895 | friend _LIBCPP_INLINE_VISIBILITY |
| 4896 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4897 | {return !(__x == __y);} |
| 4898 | }; |
| 4899 | |
| 4900 | private: |
| 4901 | param_type __p_; |
| 4902 | |
| 4903 | public: |
| 4904 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4905 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4906 | explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1) |
| 4907 | : __p_(param_type(__a, __b)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4908 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4909 | explicit extreme_value_distribution(const param_type& __p) |
| 4910 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4911 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4912 | void reset() {} |
| 4913 | |
| 4914 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4915 | template<class _URNG> |
| 4916 | _LIBCPP_INLINE_VISIBILITY |
| 4917 | result_type operator()(_URNG& __g) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4918 | {return (*this)(__g, __p_);} |
| 4919 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4920 | |
| 4921 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4922 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4923 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4924 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4925 | result_type b() const {return __p_.b();} |
| 4926 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4927 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4928 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4929 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4930 | void param(const param_type& __p) {__p_ = __p;} |
| 4931 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4932 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4933 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4934 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4935 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4936 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4937 | friend _LIBCPP_INLINE_VISIBILITY |
| 4938 | bool operator==(const extreme_value_distribution& __x, |
| 4939 | const extreme_value_distribution& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4940 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 4941 | friend _LIBCPP_INLINE_VISIBILITY |
| 4942 | bool operator!=(const extreme_value_distribution& __x, |
| 4943 | const extreme_value_distribution& __y) |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4944 | {return !(__x == __y);} |
| 4945 | }; |
| 4946 | |
| 4947 | template<class _RealType> |
| 4948 | template<class _URNG> |
| 4949 | _RealType |
| 4950 | extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4951 | { |
| 4952 | return __p.a() - __p.b() * |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 4953 | _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g))); |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4954 | } |
| 4955 | |
| 4956 | template <class _CharT, class _Traits, class _RT> |
| 4957 | basic_ostream<_CharT, _Traits>& |
| 4958 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4959 | const extreme_value_distribution<_RT>& __x) |
| 4960 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4961 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 4962 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 4963 | ios_base::scientific); |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4964 | _CharT __sp = __os.widen(' '); |
| 4965 | __os.fill(__sp); |
| 4966 | __os << __x.a() << __sp << __x.b(); |
| 4967 | return __os; |
| 4968 | } |
| 4969 | |
| 4970 | template <class _CharT, class _Traits, class _RT> |
| 4971 | basic_istream<_CharT, _Traits>& |
| 4972 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4973 | extreme_value_distribution<_RT>& __x) |
| 4974 | { |
| 4975 | typedef extreme_value_distribution<_RT> _Eng; |
| 4976 | typedef typename _Eng::result_type result_type; |
| 4977 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4978 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 8e4ddf4 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4979 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4980 | result_type __a; |
| 4981 | result_type __b; |
| 4982 | __is >> __a >> __b; |
| 4983 | if (!__is.fail()) |
| 4984 | __x.param(param_type(__a, __b)); |
| 4985 | return __is; |
| 4986 | } |
| 4987 | |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4988 | // gamma_distribution |
| 4989 | |
| 4990 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4991 | class _LIBCPP_TEMPLATE_VIS gamma_distribution |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4992 | { |
| 4993 | public: |
| 4994 | // types |
| 4995 | typedef _RealType result_type; |
| 4996 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 4997 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4998 | { |
| 4999 | result_type __alpha_; |
| 5000 | result_type __beta_; |
| 5001 | public: |
| 5002 | typedef gamma_distribution distribution_type; |
| 5003 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5004 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5005 | explicit param_type(result_type __alpha = 1, result_type __beta = 1) |
| 5006 | : __alpha_(__alpha), __beta_(__beta) {} |
| 5007 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5008 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5009 | result_type alpha() const {return __alpha_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5010 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5011 | result_type beta() const {return __beta_;} |
| 5012 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5013 | friend _LIBCPP_INLINE_VISIBILITY |
| 5014 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5015 | {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5016 | friend _LIBCPP_INLINE_VISIBILITY |
| 5017 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5018 | {return !(__x == __y);} |
| 5019 | }; |
| 5020 | |
| 5021 | private: |
| 5022 | param_type __p_; |
| 5023 | |
| 5024 | public: |
| 5025 | // constructors and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5026 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5027 | explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1) |
| 5028 | : __p_(param_type(__alpha, __beta)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5029 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5030 | explicit gamma_distribution(const param_type& __p) |
| 5031 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5032 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5033 | void reset() {} |
| 5034 | |
| 5035 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5036 | template<class _URNG> |
| 5037 | _LIBCPP_INLINE_VISIBILITY |
| 5038 | result_type operator()(_URNG& __g) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5039 | {return (*this)(__g, __p_);} |
| 5040 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5041 | |
| 5042 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5043 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5044 | result_type alpha() const {return __p_.alpha();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5045 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5046 | result_type beta() const {return __p_.beta();} |
| 5047 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5048 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5049 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5050 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5051 | void param(const param_type& __p) {__p_ = __p;} |
| 5052 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5053 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5054 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5055 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5056 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5057 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5058 | friend _LIBCPP_INLINE_VISIBILITY |
| 5059 | bool operator==(const gamma_distribution& __x, |
| 5060 | const gamma_distribution& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5061 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5062 | friend _LIBCPP_INLINE_VISIBILITY |
| 5063 | bool operator!=(const gamma_distribution& __x, |
| 5064 | const gamma_distribution& __y) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5065 | {return !(__x == __y);} |
| 5066 | }; |
| 5067 | |
| 5068 | template <class _RealType> |
| 5069 | template<class _URNG> |
| 5070 | _RealType |
| 5071 | gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5072 | { |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5073 | result_type __a = __p.alpha(); |
| 5074 | uniform_real_distribution<result_type> __gen(0, 1); |
| 5075 | exponential_distribution<result_type> __egen; |
| 5076 | result_type __x; |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5077 | if (__a == 1) |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5078 | __x = __egen(__g); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5079 | else if (__a > 1) |
| 5080 | { |
| 5081 | const result_type __b = __a - 1; |
| 5082 | const result_type __c = 3 * __a - result_type(0.75); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5083 | while (true) |
| 5084 | { |
| 5085 | const result_type __u = __gen(__g); |
| 5086 | const result_type __v = __gen(__g); |
| 5087 | const result_type __w = __u * (1 - __u); |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5088 | if (__w != 0) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5089 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5090 | const result_type __y = _VSTD::sqrt(__c / __w) * |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5091 | (__u - result_type(0.5)); |
| 5092 | __x = __b + __y; |
| 5093 | if (__x >= 0) |
| 5094 | { |
| 5095 | const result_type __z = 64 * __w * __w * __w * __v * __v; |
| 5096 | if (__z <= 1 - 2 * __y * __y / __x) |
| 5097 | break; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5098 | if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y)) |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5099 | break; |
| 5100 | } |
| 5101 | } |
| 5102 | } |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5103 | } |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5104 | else // __a < 1 |
| 5105 | { |
| 5106 | while (true) |
| 5107 | { |
| 5108 | const result_type __u = __gen(__g); |
| 5109 | const result_type __es = __egen(__g); |
| 5110 | if (__u <= 1 - __a) |
| 5111 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5112 | __x = _VSTD::pow(__u, 1 / __a); |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5113 | if (__x <= __es) |
| 5114 | break; |
| 5115 | } |
| 5116 | else |
| 5117 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5118 | const result_type __e = -_VSTD::log((1-__u)/__a); |
| 5119 | __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a); |
Howard Hinnant | 71c6b0b | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 5120 | if (__x <= __e + __es) |
| 5121 | break; |
| 5122 | } |
| 5123 | } |
| 5124 | } |
| 5125 | return __x * __p.beta(); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5126 | } |
| 5127 | |
| 5128 | template <class _CharT, class _Traits, class _RT> |
| 5129 | basic_ostream<_CharT, _Traits>& |
| 5130 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5131 | const gamma_distribution<_RT>& __x) |
| 5132 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5133 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5134 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5135 | ios_base::scientific); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5136 | _CharT __sp = __os.widen(' '); |
| 5137 | __os.fill(__sp); |
| 5138 | __os << __x.alpha() << __sp << __x.beta(); |
| 5139 | return __os; |
| 5140 | } |
| 5141 | |
| 5142 | template <class _CharT, class _Traits, class _RT> |
| 5143 | basic_istream<_CharT, _Traits>& |
| 5144 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5145 | gamma_distribution<_RT>& __x) |
| 5146 | { |
| 5147 | typedef gamma_distribution<_RT> _Eng; |
| 5148 | typedef typename _Eng::result_type result_type; |
| 5149 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5150 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | bb6d202 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 5151 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5152 | result_type __alpha; |
| 5153 | result_type __beta; |
| 5154 | __is >> __alpha >> __beta; |
| 5155 | if (!__is.fail()) |
| 5156 | __x.param(param_type(__alpha, __beta)); |
| 5157 | return __is; |
| 5158 | } |
Howard Hinnant | 0b95bc9 | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 5159 | |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5160 | // negative_binomial_distribution |
| 5161 | |
| 5162 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5163 | class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5164 | { |
| 5165 | public: |
| 5166 | // types |
| 5167 | typedef _IntType result_type; |
| 5168 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5169 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5170 | { |
| 5171 | result_type __k_; |
| 5172 | double __p_; |
| 5173 | public: |
| 5174 | typedef negative_binomial_distribution distribution_type; |
| 5175 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5176 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5177 | explicit param_type(result_type __k = 1, double __p = 0.5) |
| 5178 | : __k_(__k), __p_(__p) {} |
| 5179 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5180 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5181 | result_type k() const {return __k_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5182 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5183 | double p() const {return __p_;} |
| 5184 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5185 | friend _LIBCPP_INLINE_VISIBILITY |
| 5186 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5187 | {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5188 | friend _LIBCPP_INLINE_VISIBILITY |
| 5189 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5190 | {return !(__x == __y);} |
| 5191 | }; |
| 5192 | |
| 5193 | private: |
| 5194 | param_type __p_; |
| 5195 | |
| 5196 | public: |
| 5197 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5198 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5199 | explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5) |
| 5200 | : __p_(__k, __p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5201 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5202 | explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5203 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5204 | void reset() {} |
| 5205 | |
| 5206 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5207 | template<class _URNG> |
| 5208 | _LIBCPP_INLINE_VISIBILITY |
| 5209 | result_type operator()(_URNG& __g) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5210 | {return (*this)(__g, __p_);} |
| 5211 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5212 | |
| 5213 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5214 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5215 | result_type k() const {return __p_.k();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5216 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5217 | double p() const {return __p_.p();} |
| 5218 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5219 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5220 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5221 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5222 | void param(const param_type& __p) {__p_ = __p;} |
| 5223 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5224 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5225 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5226 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5227 | result_type max() const {return numeric_limits<result_type>::max();} |
| 5228 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5229 | friend _LIBCPP_INLINE_VISIBILITY |
| 5230 | bool operator==(const negative_binomial_distribution& __x, |
| 5231 | const negative_binomial_distribution& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5232 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5233 | friend _LIBCPP_INLINE_VISIBILITY |
| 5234 | bool operator!=(const negative_binomial_distribution& __x, |
| 5235 | const negative_binomial_distribution& __y) |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5236 | {return !(__x == __y);} |
| 5237 | }; |
| 5238 | |
| 5239 | template <class _IntType> |
| 5240 | template<class _URNG> |
| 5241 | _IntType |
| 5242 | negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 5243 | { |
| 5244 | result_type __k = __pr.k(); |
| 5245 | double __p = __pr.p(); |
| 5246 | if (__k <= 21 * __p) |
| 5247 | { |
| 5248 | bernoulli_distribution __gen(__p); |
| 5249 | result_type __f = 0; |
| 5250 | result_type __s = 0; |
| 5251 | while (__s < __k) |
| 5252 | { |
| 5253 | if (__gen(__urng)) |
| 5254 | ++__s; |
| 5255 | else |
| 5256 | ++__f; |
| 5257 | } |
| 5258 | return __f; |
| 5259 | } |
| 5260 | return poisson_distribution<result_type>(gamma_distribution<double> |
| 5261 | (__k, (1-__p)/__p)(__urng))(__urng); |
| 5262 | } |
| 5263 | |
| 5264 | template <class _CharT, class _Traits, class _IntType> |
| 5265 | basic_ostream<_CharT, _Traits>& |
| 5266 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5267 | const negative_binomial_distribution<_IntType>& __x) |
| 5268 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5269 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5270 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5271 | ios_base::scientific); |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5272 | _CharT __sp = __os.widen(' '); |
| 5273 | __os.fill(__sp); |
| 5274 | return __os << __x.k() << __sp << __x.p(); |
| 5275 | } |
| 5276 | |
| 5277 | template <class _CharT, class _Traits, class _IntType> |
| 5278 | basic_istream<_CharT, _Traits>& |
| 5279 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5280 | negative_binomial_distribution<_IntType>& __x) |
| 5281 | { |
| 5282 | typedef negative_binomial_distribution<_IntType> _Eng; |
| 5283 | typedef typename _Eng::result_type result_type; |
| 5284 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5285 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 0f43ce6 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 5286 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5287 | result_type __k; |
| 5288 | double __p; |
| 5289 | __is >> __k >> __p; |
| 5290 | if (!__is.fail()) |
| 5291 | __x.param(param_type(__k, __p)); |
| 5292 | return __is; |
| 5293 | } |
| 5294 | |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5295 | // geometric_distribution |
| 5296 | |
| 5297 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5298 | class _LIBCPP_TEMPLATE_VIS geometric_distribution |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5299 | { |
| 5300 | public: |
| 5301 | // types |
| 5302 | typedef _IntType result_type; |
| 5303 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5304 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5305 | { |
| 5306 | double __p_; |
| 5307 | public: |
| 5308 | typedef geometric_distribution distribution_type; |
| 5309 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5310 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5311 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 5312 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5313 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5314 | double p() const {return __p_;} |
| 5315 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5316 | friend _LIBCPP_INLINE_VISIBILITY |
| 5317 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5318 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5319 | friend _LIBCPP_INLINE_VISIBILITY |
| 5320 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5321 | {return !(__x == __y);} |
| 5322 | }; |
| 5323 | |
| 5324 | private: |
| 5325 | param_type __p_; |
| 5326 | |
| 5327 | public: |
| 5328 | // constructors and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5329 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5330 | explicit geometric_distribution(double __p = 0.5) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5331 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5332 | explicit geometric_distribution(const param_type& __p) : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5333 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5334 | void reset() {} |
| 5335 | |
| 5336 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5337 | template<class _URNG> |
| 5338 | _LIBCPP_INLINE_VISIBILITY |
| 5339 | result_type operator()(_URNG& __g) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5340 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5341 | template<class _URNG> |
| 5342 | _LIBCPP_INLINE_VISIBILITY |
| 5343 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5344 | {return negative_binomial_distribution<result_type>(1, __p.p())(__g);} |
| 5345 | |
| 5346 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5347 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5348 | double p() const {return __p_.p();} |
| 5349 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5350 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5351 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5352 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5353 | void param(const param_type& __p) {__p_ = __p;} |
| 5354 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5355 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5356 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5357 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5358 | result_type max() const {return numeric_limits<result_type>::max();} |
| 5359 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5360 | friend _LIBCPP_INLINE_VISIBILITY |
| 5361 | bool operator==(const geometric_distribution& __x, |
| 5362 | const geometric_distribution& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5363 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5364 | friend _LIBCPP_INLINE_VISIBILITY |
| 5365 | bool operator!=(const geometric_distribution& __x, |
| 5366 | const geometric_distribution& __y) |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5367 | {return !(__x == __y);} |
| 5368 | }; |
| 5369 | |
| 5370 | template <class _CharT, class _Traits, class _IntType> |
| 5371 | basic_ostream<_CharT, _Traits>& |
| 5372 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5373 | const geometric_distribution<_IntType>& __x) |
| 5374 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5375 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5376 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5377 | ios_base::scientific); |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5378 | return __os << __x.p(); |
| 5379 | } |
| 5380 | |
| 5381 | template <class _CharT, class _Traits, class _IntType> |
| 5382 | basic_istream<_CharT, _Traits>& |
| 5383 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5384 | geometric_distribution<_IntType>& __x) |
| 5385 | { |
| 5386 | typedef geometric_distribution<_IntType> _Eng; |
| 5387 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5388 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 28beb16 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 5389 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5390 | double __p; |
| 5391 | __is >> __p; |
| 5392 | if (!__is.fail()) |
| 5393 | __x.param(param_type(__p)); |
| 5394 | return __is; |
| 5395 | } |
| 5396 | |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5397 | // chi_squared_distribution |
| 5398 | |
| 5399 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5400 | class _LIBCPP_TEMPLATE_VIS chi_squared_distribution |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5401 | { |
| 5402 | public: |
| 5403 | // types |
| 5404 | typedef _RealType result_type; |
| 5405 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5406 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5407 | { |
| 5408 | result_type __n_; |
| 5409 | public: |
| 5410 | typedef chi_squared_distribution distribution_type; |
| 5411 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5412 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5413 | explicit param_type(result_type __n = 1) : __n_(__n) {} |
| 5414 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5415 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5416 | result_type n() const {return __n_;} |
| 5417 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5418 | friend _LIBCPP_INLINE_VISIBILITY |
| 5419 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5420 | {return __x.__n_ == __y.__n_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5421 | friend _LIBCPP_INLINE_VISIBILITY |
| 5422 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5423 | {return !(__x == __y);} |
| 5424 | }; |
| 5425 | |
| 5426 | private: |
| 5427 | param_type __p_; |
| 5428 | |
| 5429 | public: |
| 5430 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5431 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5432 | explicit chi_squared_distribution(result_type __n = 1) |
| 5433 | : __p_(param_type(__n)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5434 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5435 | explicit chi_squared_distribution(const param_type& __p) |
| 5436 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5437 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5438 | void reset() {} |
| 5439 | |
| 5440 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5441 | template<class _URNG> |
| 5442 | _LIBCPP_INLINE_VISIBILITY |
| 5443 | result_type operator()(_URNG& __g) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5444 | {return (*this)(__g, __p_);} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5445 | template<class _URNG> |
| 5446 | _LIBCPP_INLINE_VISIBILITY |
| 5447 | result_type operator()(_URNG& __g, const param_type& __p) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5448 | {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);} |
| 5449 | |
| 5450 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5451 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5452 | result_type n() const {return __p_.n();} |
| 5453 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5454 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5455 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5456 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5457 | void param(const param_type& __p) {__p_ = __p;} |
| 5458 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5459 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5460 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5461 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5462 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5463 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5464 | friend _LIBCPP_INLINE_VISIBILITY |
| 5465 | bool operator==(const chi_squared_distribution& __x, |
| 5466 | const chi_squared_distribution& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5467 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5468 | friend _LIBCPP_INLINE_VISIBILITY |
| 5469 | bool operator!=(const chi_squared_distribution& __x, |
| 5470 | const chi_squared_distribution& __y) |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5471 | {return !(__x == __y);} |
| 5472 | }; |
| 5473 | |
| 5474 | template <class _CharT, class _Traits, class _RT> |
| 5475 | basic_ostream<_CharT, _Traits>& |
| 5476 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5477 | const chi_squared_distribution<_RT>& __x) |
| 5478 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5479 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5480 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5481 | ios_base::scientific); |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5482 | __os << __x.n(); |
| 5483 | return __os; |
| 5484 | } |
| 5485 | |
| 5486 | template <class _CharT, class _Traits, class _RT> |
| 5487 | basic_istream<_CharT, _Traits>& |
| 5488 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5489 | chi_squared_distribution<_RT>& __x) |
| 5490 | { |
| 5491 | typedef chi_squared_distribution<_RT> _Eng; |
| 5492 | typedef typename _Eng::result_type result_type; |
| 5493 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5494 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 252ebf0 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 5495 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5496 | result_type __n; |
| 5497 | __is >> __n; |
| 5498 | if (!__is.fail()) |
| 5499 | __x.param(param_type(__n)); |
| 5500 | return __is; |
| 5501 | } |
| 5502 | |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5503 | // cauchy_distribution |
| 5504 | |
| 5505 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5506 | class _LIBCPP_TEMPLATE_VIS cauchy_distribution |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5507 | { |
| 5508 | public: |
| 5509 | // types |
| 5510 | typedef _RealType result_type; |
| 5511 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5512 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5513 | { |
| 5514 | result_type __a_; |
| 5515 | result_type __b_; |
| 5516 | public: |
| 5517 | typedef cauchy_distribution distribution_type; |
| 5518 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5519 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5520 | explicit param_type(result_type __a = 0, result_type __b = 1) |
| 5521 | : __a_(__a), __b_(__b) {} |
| 5522 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5523 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5524 | result_type a() const {return __a_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5525 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5526 | result_type b() const {return __b_;} |
| 5527 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5528 | friend _LIBCPP_INLINE_VISIBILITY |
| 5529 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5530 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5531 | friend _LIBCPP_INLINE_VISIBILITY |
| 5532 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5533 | {return !(__x == __y);} |
| 5534 | }; |
| 5535 | |
| 5536 | private: |
| 5537 | param_type __p_; |
| 5538 | |
| 5539 | public: |
| 5540 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5541 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5542 | explicit cauchy_distribution(result_type __a = 0, result_type __b = 1) |
| 5543 | : __p_(param_type(__a, __b)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5544 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5545 | explicit cauchy_distribution(const param_type& __p) |
| 5546 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5547 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5548 | void reset() {} |
| 5549 | |
| 5550 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5551 | template<class _URNG> |
| 5552 | _LIBCPP_INLINE_VISIBILITY |
| 5553 | result_type operator()(_URNG& __g) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5554 | {return (*this)(__g, __p_);} |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 5555 | 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] | 5556 | |
| 5557 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5558 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5559 | result_type a() const {return __p_.a();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5560 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5561 | result_type b() const {return __p_.b();} |
| 5562 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5563 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5564 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5565 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5566 | void param(const param_type& __p) {__p_ = __p;} |
| 5567 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5568 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5569 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5570 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5571 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5572 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5573 | friend _LIBCPP_INLINE_VISIBILITY |
| 5574 | bool operator==(const cauchy_distribution& __x, |
| 5575 | const cauchy_distribution& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5576 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5577 | friend _LIBCPP_INLINE_VISIBILITY |
| 5578 | bool operator!=(const cauchy_distribution& __x, |
| 5579 | const cauchy_distribution& __y) |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5580 | {return !(__x == __y);} |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5581 | }; |
| 5582 | |
| 5583 | template <class _RealType> |
| 5584 | template<class _URNG> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 5585 | inline |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5586 | _RealType |
| 5587 | cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5588 | { |
| 5589 | uniform_real_distribution<result_type> __gen; |
| 5590 | // 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] | 5591 | return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g)); |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5592 | } |
| 5593 | |
| 5594 | template <class _CharT, class _Traits, class _RT> |
| 5595 | basic_ostream<_CharT, _Traits>& |
| 5596 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5597 | const cauchy_distribution<_RT>& __x) |
| 5598 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5599 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5600 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5601 | ios_base::scientific); |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5602 | _CharT __sp = __os.widen(' '); |
| 5603 | __os.fill(__sp); |
| 5604 | __os << __x.a() << __sp << __x.b(); |
| 5605 | return __os; |
| 5606 | } |
| 5607 | |
| 5608 | template <class _CharT, class _Traits, class _RT> |
| 5609 | basic_istream<_CharT, _Traits>& |
| 5610 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5611 | cauchy_distribution<_RT>& __x) |
| 5612 | { |
| 5613 | typedef cauchy_distribution<_RT> _Eng; |
| 5614 | typedef typename _Eng::result_type result_type; |
| 5615 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5616 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | f329256 | 2010-05-17 21:55:46 +0000 | [diff] [blame] | 5617 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5618 | result_type __a; |
| 5619 | result_type __b; |
| 5620 | __is >> __a >> __b; |
| 5621 | if (!__is.fail()) |
| 5622 | __x.param(param_type(__a, __b)); |
| 5623 | return __is; |
| 5624 | } |
| 5625 | |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5626 | // fisher_f_distribution |
| 5627 | |
| 5628 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5629 | class _LIBCPP_TEMPLATE_VIS fisher_f_distribution |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5630 | { |
| 5631 | public: |
| 5632 | // types |
| 5633 | typedef _RealType result_type; |
| 5634 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5635 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5636 | { |
| 5637 | result_type __m_; |
| 5638 | result_type __n_; |
| 5639 | public: |
| 5640 | typedef fisher_f_distribution distribution_type; |
| 5641 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5642 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5643 | explicit param_type(result_type __m = 1, result_type __n = 1) |
| 5644 | : __m_(__m), __n_(__n) {} |
| 5645 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5646 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5647 | result_type m() const {return __m_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5648 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5649 | result_type n() const {return __n_;} |
| 5650 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5651 | friend _LIBCPP_INLINE_VISIBILITY |
| 5652 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5653 | {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5654 | friend _LIBCPP_INLINE_VISIBILITY |
| 5655 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5656 | {return !(__x == __y);} |
| 5657 | }; |
| 5658 | |
| 5659 | private: |
| 5660 | param_type __p_; |
| 5661 | |
| 5662 | public: |
| 5663 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5664 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5665 | explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1) |
| 5666 | : __p_(param_type(__m, __n)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5667 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5668 | explicit fisher_f_distribution(const param_type& __p) |
| 5669 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5670 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5671 | void reset() {} |
| 5672 | |
| 5673 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5674 | template<class _URNG> |
| 5675 | _LIBCPP_INLINE_VISIBILITY |
| 5676 | result_type operator()(_URNG& __g) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5677 | {return (*this)(__g, __p_);} |
| 5678 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5679 | |
| 5680 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5681 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5682 | result_type m() const {return __p_.m();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5683 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5684 | result_type n() const {return __p_.n();} |
| 5685 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5686 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5687 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5688 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5689 | void param(const param_type& __p) {__p_ = __p;} |
| 5690 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5691 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5692 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5693 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5694 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5695 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5696 | friend _LIBCPP_INLINE_VISIBILITY |
| 5697 | bool operator==(const fisher_f_distribution& __x, |
| 5698 | const fisher_f_distribution& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5699 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5700 | friend _LIBCPP_INLINE_VISIBILITY |
| 5701 | bool operator!=(const fisher_f_distribution& __x, |
| 5702 | const fisher_f_distribution& __y) |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5703 | {return !(__x == __y);} |
| 5704 | }; |
| 5705 | |
| 5706 | template <class _RealType> |
| 5707 | template<class _URNG> |
| 5708 | _RealType |
| 5709 | fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5710 | { |
| 5711 | gamma_distribution<result_type> __gdm(__p.m() * result_type(.5)); |
| 5712 | gamma_distribution<result_type> __gdn(__p.n() * result_type(.5)); |
| 5713 | return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g)); |
| 5714 | } |
| 5715 | |
| 5716 | template <class _CharT, class _Traits, class _RT> |
| 5717 | basic_ostream<_CharT, _Traits>& |
| 5718 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5719 | const fisher_f_distribution<_RT>& __x) |
| 5720 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5721 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5722 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5723 | ios_base::scientific); |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5724 | _CharT __sp = __os.widen(' '); |
| 5725 | __os.fill(__sp); |
| 5726 | __os << __x.m() << __sp << __x.n(); |
| 5727 | return __os; |
| 5728 | } |
| 5729 | |
| 5730 | template <class _CharT, class _Traits, class _RT> |
| 5731 | basic_istream<_CharT, _Traits>& |
| 5732 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5733 | fisher_f_distribution<_RT>& __x) |
| 5734 | { |
| 5735 | typedef fisher_f_distribution<_RT> _Eng; |
| 5736 | typedef typename _Eng::result_type result_type; |
| 5737 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5738 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 5739 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5740 | result_type __m; |
| 5741 | result_type __n; |
| 5742 | __is >> __m >> __n; |
| 5743 | if (!__is.fail()) |
| 5744 | __x.param(param_type(__m, __n)); |
| 5745 | return __is; |
| 5746 | } |
| 5747 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5748 | // student_t_distribution |
| 5749 | |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5750 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5751 | class _LIBCPP_TEMPLATE_VIS student_t_distribution |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5752 | { |
| 5753 | public: |
| 5754 | // types |
| 5755 | typedef _RealType result_type; |
| 5756 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5757 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5758 | { |
| 5759 | result_type __n_; |
| 5760 | public: |
| 5761 | typedef student_t_distribution distribution_type; |
| 5762 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5763 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5764 | explicit param_type(result_type __n = 1) : __n_(__n) {} |
| 5765 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5766 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5767 | result_type n() const {return __n_;} |
| 5768 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5769 | friend _LIBCPP_INLINE_VISIBILITY |
| 5770 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5771 | {return __x.__n_ == __y.__n_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5772 | friend _LIBCPP_INLINE_VISIBILITY |
| 5773 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5774 | {return !(__x == __y);} |
| 5775 | }; |
| 5776 | |
| 5777 | private: |
| 5778 | param_type __p_; |
| 5779 | normal_distribution<result_type> __nd_; |
| 5780 | |
| 5781 | public: |
| 5782 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5783 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5784 | explicit student_t_distribution(result_type __n = 1) |
| 5785 | : __p_(param_type(__n)) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5786 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5787 | explicit student_t_distribution(const param_type& __p) |
| 5788 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5789 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5790 | void reset() {__nd_.reset();} |
| 5791 | |
| 5792 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5793 | template<class _URNG> |
| 5794 | _LIBCPP_INLINE_VISIBILITY |
| 5795 | result_type operator()(_URNG& __g) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5796 | {return (*this)(__g, __p_);} |
| 5797 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5798 | |
| 5799 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5800 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5801 | result_type n() const {return __p_.n();} |
| 5802 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5803 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +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 | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5806 | void param(const param_type& __p) {__p_ = __p;} |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5807 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5808 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +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 | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5811 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 5812 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5813 | friend _LIBCPP_INLINE_VISIBILITY |
| 5814 | bool operator==(const student_t_distribution& __x, |
| 5815 | const student_t_distribution& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +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 student_t_distribution& __x, |
| 5819 | const student_t_distribution& __y) |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5820 | {return !(__x == __y);} |
| 5821 | }; |
| 5822 | |
| 5823 | template <class _RealType> |
| 5824 | template<class _URNG> |
| 5825 | _RealType |
| 5826 | student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 5827 | { |
| 5828 | gamma_distribution<result_type> __gd(__p.n() * .5, 2); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 5829 | return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g)); |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5830 | } |
| 5831 | |
| 5832 | template <class _CharT, class _Traits, class _RT> |
| 5833 | basic_ostream<_CharT, _Traits>& |
| 5834 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5835 | const student_t_distribution<_RT>& __x) |
| 5836 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5837 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 5838 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 5839 | ios_base::scientific); |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5840 | __os << __x.n(); |
| 5841 | return __os; |
| 5842 | } |
| 5843 | |
| 5844 | template <class _CharT, class _Traits, class _RT> |
| 5845 | basic_istream<_CharT, _Traits>& |
| 5846 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5847 | student_t_distribution<_RT>& __x) |
| 5848 | { |
| 5849 | typedef student_t_distribution<_RT> _Eng; |
| 5850 | typedef typename _Eng::result_type result_type; |
| 5851 | typedef typename _Eng::param_type param_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 5852 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 20c5088 | 2010-05-18 20:08:04 +0000 | [diff] [blame] | 5853 | __is.flags(ios_base::dec | ios_base::skipws); |
| 5854 | result_type __n; |
| 5855 | __is >> __n; |
| 5856 | if (!__is.fail()) |
| 5857 | __x.param(param_type(__n)); |
| 5858 | return __is; |
| 5859 | } |
| 5860 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5861 | // discrete_distribution |
| 5862 | |
| 5863 | template<class _IntType = int> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5864 | class _LIBCPP_TEMPLATE_VIS discrete_distribution |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5865 | { |
| 5866 | public: |
| 5867 | // types |
| 5868 | typedef _IntType result_type; |
| 5869 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 5870 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5871 | { |
| 5872 | vector<double> __p_; |
| 5873 | public: |
| 5874 | typedef discrete_distribution distribution_type; |
| 5875 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5876 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5877 | param_type() {} |
| 5878 | template<class _InputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5879 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5880 | param_type(_InputIterator __f, _InputIterator __l) |
| 5881 | : __p_(__f, __l) {__init();} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 5882 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5883 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5884 | param_type(initializer_list<double> __wl) |
| 5885 | : __p_(__wl.begin(), __wl.end()) {__init();} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 5886 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5887 | template<class _UnaryOperation> |
| 5888 | param_type(size_t __nw, double __xmin, double __xmax, |
| 5889 | _UnaryOperation __fw); |
| 5890 | |
| 5891 | vector<double> probabilities() const; |
| 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 | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5895 | {return __x.__p_ == __y.__p_;} |
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 | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5898 | {return !(__x == __y);} |
| 5899 | |
| 5900 | private: |
| 5901 | void __init(); |
| 5902 | |
| 5903 | friend class discrete_distribution; |
| 5904 | |
| 5905 | template <class _CharT, class _Traits, class _IT> |
| 5906 | friend |
| 5907 | basic_ostream<_CharT, _Traits>& |
| 5908 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5909 | const discrete_distribution<_IT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5910 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5911 | template <class _CharT, class _Traits, class _IT> |
| 5912 | friend |
| 5913 | basic_istream<_CharT, _Traits>& |
| 5914 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5915 | discrete_distribution<_IT>& __x); |
| 5916 | }; |
| 5917 | |
| 5918 | private: |
| 5919 | param_type __p_; |
| 5920 | |
| 5921 | public: |
| 5922 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5923 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5924 | discrete_distribution() {} |
| 5925 | template<class _InputIterator> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5926 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5927 | discrete_distribution(_InputIterator __f, _InputIterator __l) |
| 5928 | : __p_(__f, __l) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 5929 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5930 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5931 | discrete_distribution(initializer_list<double> __wl) |
| 5932 | : __p_(__wl) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 5933 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5934 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5935 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5936 | discrete_distribution(size_t __nw, double __xmin, double __xmax, |
| 5937 | _UnaryOperation __fw) |
| 5938 | : __p_(__nw, __xmin, __xmax, __fw) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5939 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ea38295 | 2013-08-14 18:00:20 +0000 | [diff] [blame] | 5940 | explicit discrete_distribution(const param_type& __p) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5941 | : __p_(__p) {} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5942 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5943 | void reset() {} |
| 5944 | |
| 5945 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5946 | template<class _URNG> |
| 5947 | _LIBCPP_INLINE_VISIBILITY |
| 5948 | result_type operator()(_URNG& __g) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5949 | {return (*this)(__g, __p_);} |
| 5950 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 5951 | |
| 5952 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5953 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5954 | vector<double> probabilities() const {return __p_.probabilities();} |
| 5955 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5956 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5957 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5958 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5959 | void param(const param_type& __p) {__p_ = __p;} |
| 5960 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5961 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5962 | result_type min() const {return 0;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5963 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5964 | result_type max() const {return __p_.__p_.size();} |
| 5965 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5966 | friend _LIBCPP_INLINE_VISIBILITY |
| 5967 | bool operator==(const discrete_distribution& __x, |
| 5968 | const discrete_distribution& __y) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5969 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 5970 | friend _LIBCPP_INLINE_VISIBILITY |
| 5971 | bool operator!=(const discrete_distribution& __x, |
| 5972 | const discrete_distribution& __y) |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5973 | {return !(__x == __y);} |
| 5974 | |
| 5975 | template <class _CharT, class _Traits, class _IT> |
| 5976 | friend |
| 5977 | basic_ostream<_CharT, _Traits>& |
| 5978 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 5979 | const discrete_distribution<_IT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 5980 | |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 5981 | template <class _CharT, class _Traits, class _IT> |
| 5982 | friend |
| 5983 | basic_istream<_CharT, _Traits>& |
| 5984 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 5985 | discrete_distribution<_IT>& __x); |
| 5986 | }; |
| 5987 | |
| 5988 | template<class _IntType> |
| 5989 | template<class _UnaryOperation> |
| 5990 | discrete_distribution<_IntType>::param_type::param_type(size_t __nw, |
| 5991 | double __xmin, |
| 5992 | double __xmax, |
| 5993 | _UnaryOperation __fw) |
| 5994 | { |
| 5995 | if (__nw > 1) |
| 5996 | { |
| 5997 | __p_.reserve(__nw - 1); |
| 5998 | double __d = (__xmax - __xmin) / __nw; |
| 5999 | double __d2 = __d / 2; |
| 6000 | for (size_t __k = 0; __k < __nw; ++__k) |
| 6001 | __p_.push_back(__fw(__xmin + __k * __d + __d2)); |
| 6002 | __init(); |
| 6003 | } |
| 6004 | } |
| 6005 | |
| 6006 | template<class _IntType> |
| 6007 | void |
| 6008 | discrete_distribution<_IntType>::param_type::__init() |
| 6009 | { |
| 6010 | if (!__p_.empty()) |
| 6011 | { |
| 6012 | if (__p_.size() > 1) |
| 6013 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6014 | double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0); |
| 6015 | for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end(); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6016 | __i < __e; ++__i) |
| 6017 | *__i /= __s; |
| 6018 | vector<double> __t(__p_.size() - 1); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6019 | _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin()); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6020 | swap(__p_, __t); |
| 6021 | } |
| 6022 | else |
| 6023 | { |
| 6024 | __p_.clear(); |
| 6025 | __p_.shrink_to_fit(); |
| 6026 | } |
| 6027 | } |
| 6028 | } |
| 6029 | |
| 6030 | template<class _IntType> |
| 6031 | vector<double> |
| 6032 | discrete_distribution<_IntType>::param_type::probabilities() const |
| 6033 | { |
| 6034 | size_t __n = __p_.size(); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6035 | _VSTD::vector<double> __p(__n+1); |
| 6036 | _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin()); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6037 | if (__n > 0) |
| 6038 | __p[__n] = 1 - __p_[__n-1]; |
| 6039 | else |
| 6040 | __p[0] = 1; |
| 6041 | return __p; |
| 6042 | } |
| 6043 | |
| 6044 | template<class _IntType> |
| 6045 | template<class _URNG> |
| 6046 | _IntType |
| 6047 | discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) |
| 6048 | { |
| 6049 | uniform_real_distribution<double> __gen; |
| 6050 | return static_cast<_IntType>( |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6051 | _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) - |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6052 | __p.__p_.begin()); |
| 6053 | } |
| 6054 | |
| 6055 | template <class _CharT, class _Traits, class _IT> |
| 6056 | basic_ostream<_CharT, _Traits>& |
| 6057 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6058 | const discrete_distribution<_IT>& __x) |
| 6059 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6060 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6061 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 6062 | ios_base::scientific); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6063 | _CharT __sp = __os.widen(' '); |
| 6064 | __os.fill(__sp); |
| 6065 | size_t __n = __x.__p_.__p_.size(); |
| 6066 | __os << __n; |
| 6067 | for (size_t __i = 0; __i < __n; ++__i) |
| 6068 | __os << __sp << __x.__p_.__p_[__i]; |
| 6069 | return __os; |
| 6070 | } |
| 6071 | |
| 6072 | template <class _CharT, class _Traits, class _IT> |
| 6073 | basic_istream<_CharT, _Traits>& |
| 6074 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6075 | discrete_distribution<_IT>& __x) |
| 6076 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6077 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6078 | __is.flags(ios_base::dec | ios_base::skipws); |
| 6079 | size_t __n; |
| 6080 | __is >> __n; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6081 | vector<double> __p(__n); |
Howard Hinnant | 7fdb18d | 2010-05-19 01:53:57 +0000 | [diff] [blame] | 6082 | for (size_t __i = 0; __i < __n; ++__i) |
| 6083 | __is >> __p[__i]; |
| 6084 | if (!__is.fail()) |
| 6085 | swap(__x.__p_.__p_, __p); |
| 6086 | return __is; |
| 6087 | } |
| 6088 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6089 | // piecewise_constant_distribution |
| 6090 | |
| 6091 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6092 | class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6093 | { |
| 6094 | public: |
| 6095 | // types |
| 6096 | typedef _RealType result_type; |
| 6097 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6098 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6099 | { |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6100 | vector<result_type> __b_; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6101 | vector<result_type> __densities_; |
| 6102 | vector<result_type> __areas_; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6103 | public: |
| 6104 | typedef piecewise_constant_distribution distribution_type; |
| 6105 | |
| 6106 | param_type(); |
| 6107 | template<class _InputIteratorB, class _InputIteratorW> |
| 6108 | param_type(_InputIteratorB __fB, _InputIteratorB __lB, |
| 6109 | _InputIteratorW __fW); |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6110 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6111 | template<class _UnaryOperation> |
| 6112 | param_type(initializer_list<result_type> __bl, _UnaryOperation __fw); |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6113 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6114 | template<class _UnaryOperation> |
| 6115 | param_type(size_t __nw, result_type __xmin, result_type __xmax, |
| 6116 | _UnaryOperation __fw); |
Eric Fiselier | b9f37ca | 2019-12-12 20:48:11 -0500 | [diff] [blame] | 6117 | param_type(param_type const&) = default; |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6118 | param_type & operator=(const param_type& __rhs); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6119 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6120 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6121 | vector<result_type> intervals() const {return __b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6122 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6123 | vector<result_type> densities() const {return __densities_;} |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6124 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6125 | friend _LIBCPP_INLINE_VISIBILITY |
| 6126 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6127 | {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6128 | friend _LIBCPP_INLINE_VISIBILITY |
| 6129 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6130 | {return !(__x == __y);} |
| 6131 | |
| 6132 | private: |
| 6133 | void __init(); |
| 6134 | |
| 6135 | friend class piecewise_constant_distribution; |
| 6136 | |
| 6137 | template <class _CharT, class _Traits, class _RT> |
| 6138 | friend |
| 6139 | basic_ostream<_CharT, _Traits>& |
| 6140 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6141 | const piecewise_constant_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6142 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6143 | template <class _CharT, class _Traits, class _RT> |
| 6144 | friend |
| 6145 | basic_istream<_CharT, _Traits>& |
| 6146 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6147 | piecewise_constant_distribution<_RT>& __x); |
| 6148 | }; |
| 6149 | |
| 6150 | private: |
| 6151 | param_type __p_; |
| 6152 | |
| 6153 | public: |
| 6154 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6155 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6156 | piecewise_constant_distribution() {} |
| 6157 | template<class _InputIteratorB, class _InputIteratorW> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6158 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6159 | piecewise_constant_distribution(_InputIteratorB __fB, |
| 6160 | _InputIteratorB __lB, |
| 6161 | _InputIteratorW __fW) |
| 6162 | : __p_(__fB, __lB, __fW) {} |
| 6163 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6164 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6165 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6166 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6167 | piecewise_constant_distribution(initializer_list<result_type> __bl, |
| 6168 | _UnaryOperation __fw) |
| 6169 | : __p_(__bl, __fw) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6170 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6171 | |
| 6172 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6173 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6174 | piecewise_constant_distribution(size_t __nw, result_type __xmin, |
| 6175 | result_type __xmax, _UnaryOperation __fw) |
| 6176 | : __p_(__nw, __xmin, __xmax, __fw) {} |
| 6177 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6178 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6179 | explicit piecewise_constant_distribution(const param_type& __p) |
| 6180 | : __p_(__p) {} |
| 6181 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6182 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6183 | void reset() {} |
| 6184 | |
| 6185 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6186 | template<class _URNG> |
| 6187 | _LIBCPP_INLINE_VISIBILITY |
| 6188 | result_type operator()(_URNG& __g) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6189 | {return (*this)(__g, __p_);} |
| 6190 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6191 | |
| 6192 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6193 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6194 | vector<result_type> intervals() const {return __p_.intervals();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6195 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6196 | vector<result_type> densities() const {return __p_.densities();} |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6197 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6198 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6199 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6200 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6201 | void param(const param_type& __p) {__p_ = __p;} |
| 6202 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6203 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6204 | result_type min() const {return __p_.__b_.front();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6205 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6206 | result_type max() const {return __p_.__b_.back();} |
| 6207 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6208 | friend _LIBCPP_INLINE_VISIBILITY |
| 6209 | bool operator==(const piecewise_constant_distribution& __x, |
| 6210 | const piecewise_constant_distribution& __y) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6211 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6212 | friend _LIBCPP_INLINE_VISIBILITY |
| 6213 | bool operator!=(const piecewise_constant_distribution& __x, |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6214 | const piecewise_constant_distribution& __y) |
| 6215 | {return !(__x == __y);} |
| 6216 | |
| 6217 | template <class _CharT, class _Traits, class _RT> |
| 6218 | friend |
| 6219 | basic_ostream<_CharT, _Traits>& |
| 6220 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6221 | const piecewise_constant_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6222 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6223 | template <class _CharT, class _Traits, class _RT> |
| 6224 | friend |
| 6225 | basic_istream<_CharT, _Traits>& |
| 6226 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6227 | piecewise_constant_distribution<_RT>& __x); |
| 6228 | }; |
| 6229 | |
| 6230 | template<class _RealType> |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6231 | typename piecewise_constant_distribution<_RealType>::param_type & |
| 6232 | piecewise_constant_distribution<_RealType>::param_type::operator= |
| 6233 | (const param_type& __rhs) |
| 6234 | { |
| 6235 | // These can throw |
| 6236 | __b_.reserve (__rhs.__b_.size ()); |
| 6237 | __densities_.reserve(__rhs.__densities_.size()); |
| 6238 | __areas_.reserve (__rhs.__areas_.size()); |
| 6239 | |
| 6240 | // These can not throw |
| 6241 | __b_ = __rhs.__b_; |
| 6242 | __densities_ = __rhs.__densities_; |
| 6243 | __areas_ = __rhs.__areas_; |
| 6244 | return *this; |
| 6245 | } |
| 6246 | |
| 6247 | template<class _RealType> |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6248 | void |
| 6249 | piecewise_constant_distribution<_RealType>::param_type::__init() |
| 6250 | { |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6251 | // __densities_ contains non-normalized areas |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6252 | result_type __total_area = _VSTD::accumulate(__densities_.begin(), |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6253 | __densities_.end(), |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6254 | result_type()); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6255 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
| 6256 | __densities_[__i] /= __total_area; |
| 6257 | // __densities_ contains normalized areas |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6258 | __areas_.assign(__densities_.size(), result_type()); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6259 | _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1, |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6260 | __areas_.begin() + 1); |
| 6261 | // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1] |
| 6262 | __densities_.back() = 1 - __areas_.back(); // correct round off error |
| 6263 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
| 6264 | __densities_[__i] /= (__b_[__i+1] - __b_[__i]); |
| 6265 | // __densities_ now contains __densities_ |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6266 | } |
| 6267 | |
| 6268 | template<class _RealType> |
| 6269 | piecewise_constant_distribution<_RealType>::param_type::param_type() |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6270 | : __b_(2), |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6271 | __densities_(1, 1.0), |
| 6272 | __areas_(1, 0.0) |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6273 | { |
| 6274 | __b_[1] = 1; |
| 6275 | } |
| 6276 | |
| 6277 | template<class _RealType> |
| 6278 | template<class _InputIteratorB, class _InputIteratorW> |
| 6279 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6280 | _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW) |
| 6281 | : __b_(__fB, __lB) |
| 6282 | { |
| 6283 | if (__b_.size() < 2) |
| 6284 | { |
| 6285 | __b_.resize(2); |
| 6286 | __b_[0] = 0; |
| 6287 | __b_[1] = 1; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6288 | __densities_.assign(1, 1.0); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6289 | __areas_.assign(1, 0.0); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6290 | } |
| 6291 | else |
| 6292 | { |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6293 | __densities_.reserve(__b_.size() - 1); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6294 | for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6295 | __densities_.push_back(*__fW); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6296 | __init(); |
| 6297 | } |
| 6298 | } |
| 6299 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6300 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6301 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6302 | template<class _RealType> |
| 6303 | template<class _UnaryOperation> |
| 6304 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6305 | initializer_list<result_type> __bl, _UnaryOperation __fw) |
| 6306 | : __b_(__bl.begin(), __bl.end()) |
| 6307 | { |
| 6308 | if (__b_.size() < 2) |
| 6309 | { |
| 6310 | __b_.resize(2); |
| 6311 | __b_[0] = 0; |
| 6312 | __b_[1] = 1; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6313 | __densities_.assign(1, 1.0); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6314 | __areas_.assign(1, 0.0); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6315 | } |
| 6316 | else |
| 6317 | { |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6318 | __densities_.reserve(__b_.size() - 1); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6319 | for (size_t __i = 0; __i < __b_.size() - 1; ++__i) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6320 | __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5)); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6321 | __init(); |
| 6322 | } |
| 6323 | } |
| 6324 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6325 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6326 | |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6327 | template<class _RealType> |
| 6328 | template<class _UnaryOperation> |
| 6329 | piecewise_constant_distribution<_RealType>::param_type::param_type( |
| 6330 | size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw) |
| 6331 | : __b_(__nw == 0 ? 2 : __nw + 1) |
| 6332 | { |
| 6333 | size_t __n = __b_.size() - 1; |
| 6334 | result_type __d = (__xmax - __xmin) / __n; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6335 | __densities_.reserve(__n); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6336 | for (size_t __i = 0; __i < __n; ++__i) |
| 6337 | { |
| 6338 | __b_[__i] = __xmin + __i * __d; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6339 | __densities_.push_back(__fw(__b_[__i] + __d*.5)); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6340 | } |
| 6341 | __b_[__n] = __xmax; |
| 6342 | __init(); |
| 6343 | } |
| 6344 | |
| 6345 | template<class _RealType> |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6346 | template<class _URNG> |
| 6347 | _RealType |
| 6348 | piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6349 | { |
| 6350 | typedef uniform_real_distribution<result_type> _Gen; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6351 | result_type __u = _Gen()(__g); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6352 | ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6353 | __u) - __p.__areas_.begin() - 1; |
| 6354 | return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k]; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
| 6357 | template <class _CharT, class _Traits, class _RT> |
| 6358 | basic_ostream<_CharT, _Traits>& |
| 6359 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6360 | const piecewise_constant_distribution<_RT>& __x) |
| 6361 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6362 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6363 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 6364 | ios_base::scientific); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6365 | _CharT __sp = __os.widen(' '); |
| 6366 | __os.fill(__sp); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6367 | size_t __n = __x.__p_.__b_.size(); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6368 | __os << __n; |
| 6369 | for (size_t __i = 0; __i < __n; ++__i) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6370 | __os << __sp << __x.__p_.__b_[__i]; |
| 6371 | __n = __x.__p_.__densities_.size(); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6372 | __os << __sp << __n; |
| 6373 | for (size_t __i = 0; __i < __n; ++__i) |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6374 | __os << __sp << __x.__p_.__densities_[__i]; |
| 6375 | __n = __x.__p_.__areas_.size(); |
| 6376 | __os << __sp << __n; |
| 6377 | for (size_t __i = 0; __i < __n; ++__i) |
| 6378 | __os << __sp << __x.__p_.__areas_[__i]; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6379 | return __os; |
| 6380 | } |
| 6381 | |
| 6382 | template <class _CharT, class _Traits, class _RT> |
| 6383 | basic_istream<_CharT, _Traits>& |
| 6384 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6385 | piecewise_constant_distribution<_RT>& __x) |
| 6386 | { |
| 6387 | typedef piecewise_constant_distribution<_RT> _Eng; |
| 6388 | typedef typename _Eng::result_type result_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6389 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6390 | __is.flags(ios_base::dec | ios_base::skipws); |
| 6391 | size_t __n; |
| 6392 | __is >> __n; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6393 | vector<result_type> __b(__n); |
| 6394 | for (size_t __i = 0; __i < __n; ++__i) |
| 6395 | __is >> __b[__i]; |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6396 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6397 | vector<result_type> __densities(__n); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6398 | for (size_t __i = 0; __i < __n; ++__i) |
| 6399 | __is >> __densities[__i]; |
| 6400 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6401 | vector<result_type> __areas(__n); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6402 | for (size_t __i = 0; __i < __n; ++__i) |
| 6403 | __is >> __areas[__i]; |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6404 | if (!__is.fail()) |
| 6405 | { |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6406 | swap(__x.__p_.__b_, __b); |
Howard Hinnant | 35d83df | 2010-05-24 00:35:40 +0000 | [diff] [blame] | 6407 | swap(__x.__p_.__densities_, __densities); |
| 6408 | swap(__x.__p_.__areas_, __areas); |
Howard Hinnant | 481ba38 | 2010-05-20 15:11:46 +0000 | [diff] [blame] | 6409 | } |
| 6410 | return __is; |
| 6411 | } |
| 6412 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6413 | // piecewise_linear_distribution |
| 6414 | |
| 6415 | template<class _RealType = double> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6416 | class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6417 | { |
| 6418 | public: |
| 6419 | // types |
| 6420 | typedef _RealType result_type; |
| 6421 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 6422 | class _LIBCPP_TEMPLATE_VIS param_type |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6423 | { |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6424 | vector<result_type> __b_; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6425 | vector<result_type> __densities_; |
| 6426 | vector<result_type> __areas_; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6427 | public: |
| 6428 | typedef piecewise_linear_distribution distribution_type; |
| 6429 | |
| 6430 | param_type(); |
| 6431 | template<class _InputIteratorB, class _InputIteratorW> |
| 6432 | param_type(_InputIteratorB __fB, _InputIteratorB __lB, |
| 6433 | _InputIteratorW __fW); |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6434 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6435 | template<class _UnaryOperation> |
| 6436 | param_type(initializer_list<result_type> __bl, _UnaryOperation __fw); |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6437 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6438 | template<class _UnaryOperation> |
| 6439 | param_type(size_t __nw, result_type __xmin, result_type __xmax, |
| 6440 | _UnaryOperation __fw); |
Eric Fiselier | b9f37ca | 2019-12-12 20:48:11 -0500 | [diff] [blame] | 6441 | param_type(param_type const&) = default; |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6442 | param_type & operator=(const param_type& __rhs); |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 6443 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6444 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6445 | vector<result_type> intervals() const {return __b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6446 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6447 | vector<result_type> densities() const {return __densities_;} |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6448 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6449 | friend _LIBCPP_INLINE_VISIBILITY |
| 6450 | bool operator==(const param_type& __x, const param_type& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6451 | {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6452 | friend _LIBCPP_INLINE_VISIBILITY |
| 6453 | bool operator!=(const param_type& __x, const param_type& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6454 | {return !(__x == __y);} |
| 6455 | |
| 6456 | private: |
| 6457 | void __init(); |
| 6458 | |
| 6459 | friend class piecewise_linear_distribution; |
| 6460 | |
| 6461 | template <class _CharT, class _Traits, class _RT> |
| 6462 | friend |
| 6463 | basic_ostream<_CharT, _Traits>& |
| 6464 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6465 | const piecewise_linear_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6466 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6467 | template <class _CharT, class _Traits, class _RT> |
| 6468 | friend |
| 6469 | basic_istream<_CharT, _Traits>& |
| 6470 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6471 | piecewise_linear_distribution<_RT>& __x); |
| 6472 | }; |
| 6473 | |
| 6474 | private: |
| 6475 | param_type __p_; |
| 6476 | |
| 6477 | public: |
| 6478 | // constructor and reset functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6479 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6480 | piecewise_linear_distribution() {} |
| 6481 | template<class _InputIteratorB, class _InputIteratorW> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6482 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6483 | piecewise_linear_distribution(_InputIteratorB __fB, |
| 6484 | _InputIteratorB __lB, |
| 6485 | _InputIteratorW __fW) |
| 6486 | : __p_(__fB, __lB, __fW) {} |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6487 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6488 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6489 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6490 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6491 | piecewise_linear_distribution(initializer_list<result_type> __bl, |
| 6492 | _UnaryOperation __fw) |
| 6493 | : __p_(__bl, __fw) {} |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6494 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6495 | |
| 6496 | template<class _UnaryOperation> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6497 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6498 | piecewise_linear_distribution(size_t __nw, result_type __xmin, |
| 6499 | result_type __xmax, _UnaryOperation __fw) |
| 6500 | : __p_(__nw, __xmin, __xmax, __fw) {} |
| 6501 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6502 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6503 | explicit piecewise_linear_distribution(const param_type& __p) |
| 6504 | : __p_(__p) {} |
| 6505 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6506 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6507 | void reset() {} |
| 6508 | |
| 6509 | // generating functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6510 | template<class _URNG> |
| 6511 | _LIBCPP_INLINE_VISIBILITY |
| 6512 | result_type operator()(_URNG& __g) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6513 | {return (*this)(__g, __p_);} |
| 6514 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 6515 | |
| 6516 | // property functions |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6517 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6518 | vector<result_type> intervals() const {return __p_.intervals();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6519 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6520 | vector<result_type> densities() const {return __p_.densities();} |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6521 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6522 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6523 | param_type param() const {return __p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6524 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6525 | void param(const param_type& __p) {__p_ = __p;} |
| 6526 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6527 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6528 | result_type min() const {return __p_.__b_.front();} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6529 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6530 | result_type max() const {return __p_.__b_.back();} |
| 6531 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6532 | friend _LIBCPP_INLINE_VISIBILITY |
| 6533 | bool operator==(const piecewise_linear_distribution& __x, |
| 6534 | const piecewise_linear_distribution& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6535 | {return __x.__p_ == __y.__p_;} |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 6536 | friend _LIBCPP_INLINE_VISIBILITY |
| 6537 | bool operator!=(const piecewise_linear_distribution& __x, |
| 6538 | const piecewise_linear_distribution& __y) |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6539 | {return !(__x == __y);} |
| 6540 | |
| 6541 | template <class _CharT, class _Traits, class _RT> |
| 6542 | friend |
| 6543 | basic_ostream<_CharT, _Traits>& |
| 6544 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6545 | const piecewise_linear_distribution<_RT>& __x); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 6546 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6547 | template <class _CharT, class _Traits, class _RT> |
| 6548 | friend |
| 6549 | basic_istream<_CharT, _Traits>& |
| 6550 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6551 | piecewise_linear_distribution<_RT>& __x); |
| 6552 | }; |
| 6553 | |
| 6554 | template<class _RealType> |
Howard Hinnant | 71c410b | 2010-10-13 14:37:09 +0000 | [diff] [blame] | 6555 | typename piecewise_linear_distribution<_RealType>::param_type & |
| 6556 | piecewise_linear_distribution<_RealType>::param_type::operator= |
| 6557 | (const param_type& __rhs) |
| 6558 | { |
| 6559 | // These can throw |
| 6560 | __b_.reserve (__rhs.__b_.size ()); |
| 6561 | __densities_.reserve(__rhs.__densities_.size()); |
| 6562 | __areas_.reserve (__rhs.__areas_.size()); |
| 6563 | |
| 6564 | // These can not throw |
| 6565 | __b_ = __rhs.__b_; |
| 6566 | __densities_ = __rhs.__densities_; |
| 6567 | __areas_ = __rhs.__areas_; |
| 6568 | return *this; |
| 6569 | } |
| 6570 | |
| 6571 | |
| 6572 | template<class _RealType> |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6573 | void |
| 6574 | piecewise_linear_distribution<_RealType>::param_type::__init() |
| 6575 | { |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6576 | __areas_.assign(__densities_.size() - 1, result_type()); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6577 | result_type _Sp = 0; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6578 | for (size_t __i = 0; __i < __areas_.size(); ++__i) |
| 6579 | { |
| 6580 | __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) * |
| 6581 | (__b_[__i+1] - __b_[__i]) * .5; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6582 | _Sp += __areas_[__i]; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6583 | } |
| 6584 | for (size_t __i = __areas_.size(); __i > 1;) |
| 6585 | { |
| 6586 | --__i; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6587 | __areas_[__i] = __areas_[__i-1] / _Sp; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6588 | } |
| 6589 | __areas_[0] = 0; |
| 6590 | for (size_t __i = 1; __i < __areas_.size(); ++__i) |
| 6591 | __areas_[__i] += __areas_[__i-1]; |
| 6592 | for (size_t __i = 0; __i < __densities_.size(); ++__i) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 6593 | __densities_[__i] /= _Sp; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | template<class _RealType> |
| 6597 | piecewise_linear_distribution<_RealType>::param_type::param_type() |
| 6598 | : __b_(2), |
| 6599 | __densities_(2, 1.0), |
| 6600 | __areas_(1, 0.0) |
| 6601 | { |
| 6602 | __b_[1] = 1; |
| 6603 | } |
| 6604 | |
| 6605 | template<class _RealType> |
| 6606 | template<class _InputIteratorB, class _InputIteratorW> |
| 6607 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6608 | _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW) |
| 6609 | : __b_(__fB, __lB) |
| 6610 | { |
| 6611 | if (__b_.size() < 2) |
| 6612 | { |
| 6613 | __b_.resize(2); |
| 6614 | __b_[0] = 0; |
| 6615 | __b_[1] = 1; |
| 6616 | __densities_.assign(2, 1.0); |
| 6617 | __areas_.assign(1, 0.0); |
| 6618 | } |
| 6619 | else |
| 6620 | { |
| 6621 | __densities_.reserve(__b_.size()); |
| 6622 | for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW) |
| 6623 | __densities_.push_back(*__fW); |
| 6624 | __init(); |
| 6625 | } |
| 6626 | } |
| 6627 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6628 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6629 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6630 | template<class _RealType> |
| 6631 | template<class _UnaryOperation> |
| 6632 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6633 | initializer_list<result_type> __bl, _UnaryOperation __fw) |
| 6634 | : __b_(__bl.begin(), __bl.end()) |
| 6635 | { |
| 6636 | if (__b_.size() < 2) |
| 6637 | { |
| 6638 | __b_.resize(2); |
| 6639 | __b_[0] = 0; |
| 6640 | __b_[1] = 1; |
| 6641 | __densities_.assign(2, 1.0); |
| 6642 | __areas_.assign(1, 0.0); |
| 6643 | } |
| 6644 | else |
| 6645 | { |
| 6646 | __densities_.reserve(__b_.size()); |
| 6647 | for (size_t __i = 0; __i < __b_.size(); ++__i) |
| 6648 | __densities_.push_back(__fw(__b_[__i])); |
| 6649 | __init(); |
| 6650 | } |
| 6651 | } |
| 6652 | |
Eric Fiselier | 3b0b81f | 2017-04-19 00:23:45 +0000 | [diff] [blame] | 6653 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 6654 | |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6655 | template<class _RealType> |
| 6656 | template<class _UnaryOperation> |
| 6657 | piecewise_linear_distribution<_RealType>::param_type::param_type( |
| 6658 | size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw) |
| 6659 | : __b_(__nw == 0 ? 2 : __nw + 1) |
| 6660 | { |
| 6661 | size_t __n = __b_.size() - 1; |
| 6662 | result_type __d = (__xmax - __xmin) / __n; |
| 6663 | __densities_.reserve(__b_.size()); |
| 6664 | for (size_t __i = 0; __i < __n; ++__i) |
| 6665 | { |
| 6666 | __b_[__i] = __xmin + __i * __d; |
| 6667 | __densities_.push_back(__fw(__b_[__i])); |
| 6668 | } |
| 6669 | __b_[__n] = __xmax; |
| 6670 | __densities_.push_back(__fw(__b_[__n])); |
| 6671 | __init(); |
| 6672 | } |
| 6673 | |
| 6674 | template<class _RealType> |
| 6675 | template<class _URNG> |
| 6676 | _RealType |
| 6677 | piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 6678 | { |
| 6679 | typedef uniform_real_distribution<result_type> _Gen; |
| 6680 | result_type __u = _Gen()(__g); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6681 | ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6682 | __u) - __p.__areas_.begin() - 1; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6683 | __u -= __p.__areas_[__k]; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6684 | const result_type __dk = __p.__densities_[__k]; |
| 6685 | const result_type __dk1 = __p.__densities_[__k+1]; |
| 6686 | const result_type __deltad = __dk1 - __dk; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6687 | const result_type __bk = __p.__b_[__k]; |
| 6688 | if (__deltad == 0) |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6689 | return __u / __dk + __bk; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6690 | const result_type __bk1 = __p.__b_[__k+1]; |
| 6691 | const result_type __deltab = __bk1 - __bk; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6692 | return (__bk * __dk1 - __bk1 * __dk + |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 6693 | _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) / |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6694 | __deltad; |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6695 | } |
| 6696 | |
| 6697 | template <class _CharT, class _Traits, class _RT> |
| 6698 | basic_ostream<_CharT, _Traits>& |
| 6699 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 6700 | const piecewise_linear_distribution<_RT>& __x) |
| 6701 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6702 | __save_flags<_CharT, _Traits> __lx(__os); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6703 | __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | |
| 6704 | ios_base::scientific); |
| 6705 | _CharT __sp = __os.widen(' '); |
| 6706 | __os.fill(__sp); |
| 6707 | size_t __n = __x.__p_.__b_.size(); |
| 6708 | __os << __n; |
| 6709 | for (size_t __i = 0; __i < __n; ++__i) |
| 6710 | __os << __sp << __x.__p_.__b_[__i]; |
| 6711 | __n = __x.__p_.__densities_.size(); |
| 6712 | __os << __sp << __n; |
| 6713 | for (size_t __i = 0; __i < __n; ++__i) |
| 6714 | __os << __sp << __x.__p_.__densities_[__i]; |
| 6715 | __n = __x.__p_.__areas_.size(); |
| 6716 | __os << __sp << __n; |
| 6717 | for (size_t __i = 0; __i < __n; ++__i) |
| 6718 | __os << __sp << __x.__p_.__areas_[__i]; |
| 6719 | return __os; |
| 6720 | } |
| 6721 | |
| 6722 | template <class _CharT, class _Traits, class _RT> |
| 6723 | basic_istream<_CharT, _Traits>& |
| 6724 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 6725 | piecewise_linear_distribution<_RT>& __x) |
| 6726 | { |
| 6727 | typedef piecewise_linear_distribution<_RT> _Eng; |
| 6728 | typedef typename _Eng::result_type result_type; |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 6729 | __save_flags<_CharT, _Traits> __lx(__is); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6730 | __is.flags(ios_base::dec | ios_base::skipws); |
| 6731 | size_t __n; |
| 6732 | __is >> __n; |
| 6733 | vector<result_type> __b(__n); |
| 6734 | for (size_t __i = 0; __i < __n; ++__i) |
| 6735 | __is >> __b[__i]; |
| 6736 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6737 | vector<result_type> __densities(__n); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6738 | for (size_t __i = 0; __i < __n; ++__i) |
| 6739 | __is >> __densities[__i]; |
| 6740 | __is >> __n; |
Howard Hinnant | 167159f | 2010-11-18 17:01:36 +0000 | [diff] [blame] | 6741 | vector<result_type> __areas(__n); |
Howard Hinnant | a001ef3 | 2010-05-25 00:27:34 +0000 | [diff] [blame] | 6742 | for (size_t __i = 0; __i < __n; ++__i) |
| 6743 | __is >> __areas[__i]; |
| 6744 | if (!__is.fail()) |
| 6745 | { |
| 6746 | swap(__x.__p_.__b_, __b); |
| 6747 | swap(__x.__p_.__densities_, __densities); |
| 6748 | swap(__x.__p_.__areas_, __areas); |
| 6749 | } |
| 6750 | return __is; |
| 6751 | } |
| 6752 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6753 | _LIBCPP_END_NAMESPACE_STD |
| 6754 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 6755 | _LIBCPP_POP_MACROS |
| 6756 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6757 | #endif // _LIBCPP_RANDOM |