Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- random -----------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_RANDOM |
| 12 | #define _LIBCPP_RANDOM |
| 13 | |
| 14 | /* |
| 15 | random synopsis |
| 16 | |
| 17 | #include <initializer_list> |
| 18 | |
| 19 | namespace std |
| 20 | { |
| 21 | |
| 22 | // Engines |
| 23 | |
| 24 | template <class UIntType, UIntType a, UIntType c, UIntType m> |
| 25 | class linear_congruential_engine |
| 26 | { |
| 27 | public: |
| 28 | // types |
| 29 | typedef UIntType result_type; |
| 30 | |
| 31 | // engine characteristics |
| 32 | static constexpr result_type multiplier = a; |
| 33 | static constexpr result_type increment = c; |
| 34 | static constexpr result_type modulus = m; |
| 35 | static constexpr result_type min() { return c == 0u ? 1u: 0u;} |
| 36 | static constexpr result_type max() { return m - 1u;} |
| 37 | static constexpr result_type default_seed = 1u; |
| 38 | |
| 39 | // constructors and seeding functions |
| 40 | explicit linear_congruential_engine(result_type s = default_seed); |
| 41 | template<class Sseq> explicit linear_congruential_engine(Sseq& q); |
| 42 | void seed(result_type s = default_seed); |
| 43 | template<class Sseq> void seed(Sseq& q); |
| 44 | |
| 45 | // generating functions |
| 46 | result_type operator()(); |
| 47 | void discard(unsigned long long z); |
| 48 | }; |
| 49 | |
| 50 | template <class UIntType, UIntType a, UIntType c, UIntType m> |
| 51 | bool |
| 52 | operator==(const linear_congruential_engine<UIntType, a, c, m>& x, |
| 53 | const linear_congruential_engine<UIntType, a, c, m>& y); |
| 54 | |
| 55 | template <class UIntType, UIntType a, UIntType c, UIntType m> |
| 56 | bool |
| 57 | operator!=(const linear_congruential_engine<UIntType, a, c, m>& x, |
| 58 | const linear_congruential_engine<UIntType, a, c, m>& y); |
| 59 | |
| 60 | template <class charT, class traits, |
| 61 | class UIntType, UIntType a, UIntType c, UIntType m> |
| 62 | basic_ostream<charT, traits>& |
| 63 | operator<<(basic_ostream<charT, traits>& os, |
| 64 | const linear_congruential_engine<UIntType, a, c, m>& x); |
| 65 | |
| 66 | template <class charT, class traits, |
| 67 | class UIntType, UIntType a, UIntType c, UIntType m> |
| 68 | basic_istream<charT, traits>& |
| 69 | operator>>(basic_istream<charT, traits>& is, |
| 70 | linear_congruential_engine<UIntType, a, c, m>& x); |
| 71 | |
| 72 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 73 | UIntType a, size_t u, UIntType d, size_t s, |
| 74 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 75 | class mersenne_twister_engine |
| 76 | { |
| 77 | public: |
| 78 | // types |
| 79 | typedef UIntType result_type; |
| 80 | |
| 81 | // engine characteristics |
| 82 | static constexpr size_t word_size = w; |
| 83 | static constexpr size_t state_size = n; |
| 84 | static constexpr size_t shift_size = m; |
| 85 | static constexpr size_t mask_bits = r; |
| 86 | static constexpr result_type xor_mask = a; |
| 87 | static constexpr size_t tempering_u = u; |
| 88 | static constexpr result_type tempering_d = d; |
| 89 | static constexpr size_t tempering_s = s; |
| 90 | static constexpr result_type tempering_b = b; |
| 91 | static constexpr size_t tempering_t = t; |
| 92 | static constexpr result_type tempering_c = c; |
| 93 | static constexpr size_t tempering_l = l; |
| 94 | static constexpr result_type initialization_multiplier = f; |
| 95 | static constexpr result_type min () { return 0; } |
| 96 | static constexpr result_type max() { return 2^w - 1; } |
| 97 | static constexpr result_type default_seed = 5489u; |
| 98 | |
| 99 | // constructors and seeding functions |
| 100 | explicit mersenne_twister_engine(result_type value = default_seed); |
| 101 | template<class Sseq> explicit mersenne_twister_engine(Sseq& q); |
| 102 | void seed(result_type value = default_seed); |
| 103 | template<class Sseq> void seed(Sseq& q); |
| 104 | |
| 105 | // generating functions |
| 106 | result_type operator()(); |
| 107 | void discard(unsigned long long z); |
| 108 | }; |
| 109 | |
| 110 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 111 | UIntType a, size_t u, UIntType d, size_t s, |
| 112 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 113 | bool |
| 114 | operator==( |
| 115 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x, |
| 116 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y); |
| 117 | |
| 118 | template <class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 119 | UIntType a, size_t u, UIntType d, size_t s, |
| 120 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 121 | bool |
| 122 | operator!=( |
| 123 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x, |
| 124 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y); |
| 125 | |
| 126 | template <class charT, class traits, |
| 127 | class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 128 | UIntType a, size_t u, UIntType d, size_t s, |
| 129 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 130 | basic_ostream<charT, traits>& |
| 131 | operator<<(basic_ostream<charT, traits>& os, |
| 132 | const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x); |
| 133 | |
| 134 | template <class charT, class traits, |
| 135 | class UIntType, size_t w, size_t n, size_t m, size_t r, |
| 136 | UIntType a, size_t u, UIntType d, size_t s, |
| 137 | UIntType b, size_t t, UIntType c, size_t l, UIntType f> |
| 138 | basic_istream<charT, traits>& |
| 139 | operator>>(basic_istream<charT, traits>& is, |
| 140 | mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x); |
| 141 | |
| 142 | template<class UIntType, size_t w, size_t s, size_t r> |
| 143 | class subtract_with_carry_engine |
| 144 | { |
| 145 | public: |
| 146 | // types |
| 147 | typedef UIntType result_type; |
| 148 | |
| 149 | // engine characteristics |
| 150 | static constexpr size_t word_size = w; |
| 151 | static constexpr size_t short_lag = s; |
| 152 | static constexpr size_t long_lag = r; |
| 153 | static constexpr result_type min() { return 0; } |
| 154 | static constexpr result_type max() { return m-1; } |
| 155 | static constexpr result_type default_seed = 19780503u; |
| 156 | |
| 157 | // constructors and seeding functions |
| 158 | explicit subtract_with_carry_engine(result_type value = default_seed); |
| 159 | template<class Sseq> explicit subtract_with_carry_engine(Sseq& q); |
| 160 | void seed(result_type value = default_seed); |
| 161 | template<class Sseq> void seed(Sseq& q); |
| 162 | |
| 163 | // generating functions |
| 164 | result_type operator()(); |
| 165 | void discard(unsigned long long z); |
| 166 | }; |
| 167 | |
| 168 | template<class UIntType, size_t w, size_t s, size_t r> |
| 169 | bool |
| 170 | operator==( |
| 171 | const subtract_with_carry_engine<UIntType, w, s, r>& x, |
| 172 | const subtract_with_carry_engine<UIntType, w, s, r>& y); |
| 173 | |
| 174 | template<class UIntType, size_t w, size_t s, size_t r> |
| 175 | bool |
| 176 | operator!=( |
| 177 | const subtract_with_carry_engine<UIntType, w, s, r>& x, |
| 178 | const subtract_with_carry_engine<UIntType, w, s, r>& y); |
| 179 | |
| 180 | template <class charT, class traits, |
| 181 | class UIntType, size_t w, size_t s, size_t r> |
| 182 | basic_ostream<charT, traits>& |
| 183 | operator<<(basic_ostream<charT, traits>& os, |
| 184 | const subtract_with_carry_engine<UIntType, w, s, r>& x); |
| 185 | |
| 186 | template <class charT, class traits, |
| 187 | class UIntType, size_t w, size_t s, size_t r> |
| 188 | basic_istream<charT, traits>& |
| 189 | operator>>(basic_istream<charT, traits>& is, |
| 190 | subtract_with_carry_engine<UIntType, w, s, r>& x); |
| 191 | |
| 192 | template<class Engine, size_t p, size_t r> |
| 193 | class discard_block_engine |
| 194 | { |
| 195 | public: |
| 196 | // types |
| 197 | typedef typename Engine::result_type result_type; |
| 198 | |
| 199 | // engine characteristics |
| 200 | static constexpr size_t block_size = p; |
| 201 | static constexpr size_t used_block = r; |
| 202 | static constexpr result_type min() { return Engine::min(); } |
| 203 | static constexpr result_type max() { return Engine::max(); } |
| 204 | |
| 205 | // constructors and seeding functions |
| 206 | discard_block_engine(); |
| 207 | explicit discard_block_engine(const Engine& e); |
| 208 | explicit discard_block_engine(Engine&& e); |
| 209 | explicit discard_block_engine(result_type s); |
| 210 | template<class Sseq> explicit discard_block_engine(Sseq& q); |
| 211 | void seed(); |
| 212 | void seed(result_type s); |
| 213 | template<class Sseq> void seed(Sseq& q); |
| 214 | |
| 215 | // generating functions |
| 216 | result_type operator()(); |
| 217 | void discard(unsigned long long z); |
| 218 | |
| 219 | // property functions |
| 220 | const Engine& base() const; |
| 221 | }; |
| 222 | |
| 223 | template<class Engine, size_t p, size_t r> |
| 224 | bool |
| 225 | operator==( |
| 226 | const discard_block_engine<Engine, p, r>& x, |
| 227 | const discard_block_engine<Engine, p, r>& y); |
| 228 | |
| 229 | template<class Engine, size_t p, size_t r> |
| 230 | bool |
| 231 | operator!=( |
| 232 | const discard_block_engine<Engine, p, r>& x, |
| 233 | const discard_block_engine<Engine, p, r>& y); |
| 234 | |
| 235 | template <class charT, class traits, |
| 236 | class Engine, size_t p, size_t r> |
| 237 | basic_ostream<charT, traits>& |
| 238 | operator<<(basic_ostream<charT, traits>& os, |
| 239 | const discard_block_engine<Engine, p, r>& x); |
| 240 | |
| 241 | template <class charT, class traits, |
| 242 | class Engine, size_t p, size_t r> |
| 243 | basic_istream<charT, traits>& |
| 244 | operator>>(basic_istream<charT, traits>& is, |
| 245 | discard_block_engine<Engine, p, r>& x); |
| 246 | |
| 247 | template<class Engine, size_t w, class UIntType> |
| 248 | class independent_bits_engine |
| 249 | { |
| 250 | public: |
| 251 | // types |
| 252 | typedef UIntType result_type; |
| 253 | |
| 254 | // engine characteristics |
| 255 | static constexpr result_type min() { return 0; } |
| 256 | static constexpr result_type max() { return 2^w - 1; } |
| 257 | |
| 258 | // constructors and seeding functions |
| 259 | independent_bits_engine(); |
| 260 | explicit independent_bits_engine(const Engine& e); |
| 261 | explicit independent_bits_engine(Engine&& e); |
| 262 | explicit independent_bits_engine(result_type s); |
| 263 | template<class Sseq> explicit independent_bits_engine(Sseq& q); |
| 264 | void seed(); |
| 265 | void seed(result_type s); |
| 266 | template<class Sseq> void seed(Sseq& q); |
| 267 | |
| 268 | // generating functions |
| 269 | result_type operator()(); void discard(unsigned long long z); |
| 270 | |
| 271 | // property functions |
| 272 | const Engine& base() const; |
| 273 | }; |
| 274 | |
| 275 | template<class Engine, size_t w, class UIntType> |
| 276 | bool |
| 277 | operator==( |
| 278 | const independent_bits_engine<Engine, w, UIntType>& x, |
| 279 | const independent_bits_engine<Engine, w, UIntType>& y); |
| 280 | |
| 281 | template<class Engine, size_t w, class UIntType> |
| 282 | bool |
| 283 | operator!=( |
| 284 | const independent_bits_engine<Engine, w, UIntType>& x, |
| 285 | const independent_bits_engine<Engine, w, UIntType>& y); |
| 286 | |
| 287 | template <class charT, class traits, |
| 288 | class Engine, size_t w, class UIntType> |
| 289 | basic_ostream<charT, traits>& |
| 290 | operator<<(basic_ostream<charT, traits>& os, |
| 291 | const independent_bits_engine<Engine, w, UIntType>& x); |
| 292 | |
| 293 | template <class charT, class traits, |
| 294 | class Engine, size_t w, class UIntType> |
| 295 | basic_istream<charT, traits>& |
| 296 | operator>>(basic_istream<charT, traits>& is, |
| 297 | independent_bits_engine<Engine, w, UIntType>& x); |
| 298 | |
| 299 | template<class Engine, size_t k> |
| 300 | class shuffle_order_engine |
| 301 | { |
| 302 | public: |
| 303 | // types |
| 304 | typedef typename Engine::result_type result_type; |
| 305 | |
| 306 | // engine characteristics |
| 307 | static constexpr size_t table_size = k; |
| 308 | static constexpr result_type min() { return Engine::min; } |
| 309 | static constexpr result_type max() { return Engine::max; } |
| 310 | |
| 311 | // constructors and seeding functions |
| 312 | shuffle_order_engine(); |
| 313 | explicit shuffle_order_engine(const Engine& e); |
| 314 | explicit shuffle_order_engine(Engine&& e); |
| 315 | explicit shuffle_order_engine(result_type s); |
| 316 | template<class Sseq> explicit shuffle_order_engine(Sseq& q); |
| 317 | void seed(); |
| 318 | void seed(result_type s); |
| 319 | template<class Sseq> void seed(Sseq& q); |
| 320 | |
| 321 | // generating functions |
| 322 | result_type operator()(); |
| 323 | void discard(unsigned long long z); |
| 324 | |
| 325 | // property functions |
| 326 | const Engine& base() const; |
| 327 | }; |
| 328 | |
| 329 | template<class Engine, size_t k> |
| 330 | bool |
| 331 | operator==( |
| 332 | const shuffle_order_engine<Engine, k>& x, |
| 333 | const shuffle_order_engine<Engine, k>& y); |
| 334 | |
| 335 | template<class Engine, size_t k> |
| 336 | bool |
| 337 | operator!=( |
| 338 | const shuffle_order_engine<Engine, k>& x, |
| 339 | const shuffle_order_engine<Engine, k>& y); |
| 340 | |
| 341 | template <class charT, class traits, |
| 342 | class Engine, size_t k> |
| 343 | basic_ostream<charT, traits>& |
| 344 | operator<<(basic_ostream<charT, traits>& os, |
| 345 | const shuffle_order_engine<Engine, k>& x); |
| 346 | |
| 347 | template <class charT, class traits, |
| 348 | class Engine, size_t k> |
| 349 | basic_istream<charT, traits>& |
| 350 | operator>>(basic_istream<charT, traits>& is, |
| 351 | shuffle_order_engine<Engine, k>& x); |
| 352 | |
| 353 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> |
| 354 | minstd_rand0; |
| 355 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> |
| 356 | minstd_rand; |
| 357 | typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, |
| 358 | 0x9908b0df, |
| 359 | 11, 0xffffffff, |
| 360 | 7, 0x9d2c5680, |
| 361 | 15, 0xefc60000, |
| 362 | 18, 1812433253> mt19937; |
| 363 | typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31, |
| 364 | 0xb5026f5aa96619e9, |
| 365 | 29, 0x5555555555555555, |
| 366 | 17, 0x71d67fffeda60000, |
| 367 | 37, 0xfff7eee000000000, |
| 368 | 43, 6364136223846793005> mt19937_64; |
| 369 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base; |
| 370 | typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base; |
| 371 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
| 372 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
| 373 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
| 374 | typedef minstd_rand0 default_random_engine; |
| 375 | |
| 376 | // Generators |
| 377 | |
| 378 | class random_device |
| 379 | { |
| 380 | public: |
| 381 | // types |
| 382 | typedef unsigned int result_type; |
| 383 | |
| 384 | // generator characteristics |
| 385 | static constexpr result_type min() { return numeric_limits<result_type>::min(); } |
| 386 | static constexpr result_type max() { return numeric_limits<result_type>::max(); } |
| 387 | |
| 388 | // constructors |
| 389 | explicit random_device(const string& token = "/dev/urandom"); |
| 390 | |
| 391 | // generating functions |
| 392 | result_type operator()(); |
| 393 | |
| 394 | // property functions |
| 395 | double entropy() const; |
| 396 | |
| 397 | // no copy functions |
| 398 | random_device(const random_device& ) = delete; |
| 399 | void operator=(const random_device& ) = delete; |
| 400 | }; |
| 401 | |
| 402 | // Utilities |
| 403 | |
| 404 | class seed_seq |
| 405 | { |
| 406 | public: |
| 407 | // types |
| 408 | typedef uint_least32_t result_type; |
| 409 | |
| 410 | // constructors |
| 411 | seed_seq(); |
| 412 | template<class T> |
| 413 | seed_seq(initializer_list<T> il); |
| 414 | template<class InputIterator> |
| 415 | seed_seq(InputIterator begin, InputIterator end); |
| 416 | |
| 417 | // generating functions |
| 418 | template<class RandomAccessIterator> |
| 419 | void generate(RandomAccessIterator begin, RandomAccessIterator end); |
| 420 | |
| 421 | // property functions |
| 422 | size_t size() const; |
| 423 | template<class OutputIterator> |
| 424 | void param(OutputIterator dest) const; |
| 425 | |
| 426 | // no copy functions |
| 427 | seed_seq(const seed_seq&) = delete; |
| 428 | void operator=(const seed_seq& ) = delete; |
| 429 | }; |
| 430 | |
| 431 | template<class RealType, size_t bits, class URNG> |
| 432 | RealType generate_canonical(URNG& g); |
| 433 | |
| 434 | // Distributions |
| 435 | |
| 436 | template<class IntType = int> |
| 437 | class uniform_int_distribution |
| 438 | { |
| 439 | public: |
| 440 | // types |
| 441 | typedef IntType result_type; |
| 442 | |
| 443 | class param_type |
| 444 | { |
| 445 | public: |
| 446 | typedef uniform_int_distribution distribution_type; |
| 447 | |
| 448 | explicit param_type(IntType a = 0, |
| 449 | IntType b = numeric_limits<IntType>::max()); |
| 450 | |
| 451 | result_type a() const; |
| 452 | result_type b() const; |
| 453 | |
| 454 | friend bool operator==(const param_type& x, const param_type& y); |
| 455 | friend bool operator!=(const param_type& x, const param_type& y); |
| 456 | }; |
| 457 | |
| 458 | // constructors and reset functions |
| 459 | explicit uniform_int_distribution(IntType a = 0, |
| 460 | IntType b = numeric_limits<IntType>::max()); |
| 461 | explicit uniform_int_distribution(const param_type& parm); |
| 462 | void reset(); |
| 463 | |
| 464 | // generating functions |
| 465 | template<class URNG> result_type operator()(URNG& g); |
| 466 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 467 | |
| 468 | // property functions |
| 469 | result_type a() const; |
| 470 | result_type b() const; |
| 471 | |
| 472 | param_type param() const; |
| 473 | void param(const param_type& parm); |
| 474 | |
| 475 | result_type min() const; |
| 476 | result_type max() const; |
| 477 | |
| 478 | friend bool operator==(const uniform_int_distribution& x, |
| 479 | const uniform_int_distribution& y); |
| 480 | friend bool operator!=(const uniform_int_distribution& x, |
| 481 | const uniform_int_distribution& y); |
| 482 | |
| 483 | template <class charT, class traits> |
| 484 | friend |
| 485 | basic_ostream<charT, traits>& |
| 486 | operator<<(basic_ostream<charT, traits>& os, |
| 487 | const uniform_int_distribution& x); |
| 488 | |
| 489 | template <class charT, class traits> |
| 490 | friend |
| 491 | basic_istream<charT, traits>& |
| 492 | operator>>(basic_istream<charT, traits>& is, |
| 493 | uniform_int_distribution& x); |
| 494 | }; |
| 495 | |
| 496 | template<class RealType = double> |
| 497 | class uniform_real_distribution |
| 498 | { |
| 499 | public: |
| 500 | // types |
| 501 | typedef RealType result_type; |
| 502 | |
| 503 | class param_type |
| 504 | { |
| 505 | public: |
| 506 | typedef uniform_real_distribution distribution_type; |
| 507 | |
| 508 | explicit param_type(RealType a = 0, |
| 509 | RealType b = 1); |
| 510 | |
| 511 | result_type a() const; |
| 512 | result_type b() const; |
| 513 | |
| 514 | friend bool operator==(const param_type& x, const param_type& y); |
| 515 | friend bool operator!=(const param_type& x, const param_type& y); |
| 516 | }; |
| 517 | |
| 518 | // constructors and reset functions |
| 519 | explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0); |
| 520 | explicit uniform_real_distribution(const param_type& parm); |
| 521 | void reset(); |
| 522 | |
| 523 | // generating functions |
| 524 | template<class URNG> result_type operator()(URNG& g); |
| 525 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 526 | |
| 527 | // property functions |
| 528 | result_type a() const; |
| 529 | result_type b() const; |
| 530 | |
| 531 | param_type param() const; |
| 532 | void param(const param_type& parm); |
| 533 | |
| 534 | result_type min() const; |
| 535 | result_type max() const; |
| 536 | |
| 537 | friend bool operator==(const uniform_real_distribution& x, |
| 538 | const uniform_real_distribution& y); |
| 539 | friend bool operator!=(const uniform_real_distribution& x, |
| 540 | const uniform_real_distribution& y); |
| 541 | |
| 542 | template <class charT, class traits> |
| 543 | friend |
| 544 | basic_ostream<charT, traits>& |
| 545 | operator<<(basic_ostream<charT, traits>& os, |
| 546 | const uniform_real_distribution& x); |
| 547 | |
| 548 | template <class charT, class traits> |
| 549 | friend |
| 550 | basic_istream<charT, traits>& |
| 551 | operator>>(basic_istream<charT, traits>& is, |
| 552 | uniform_real_distribution& x); |
| 553 | }; |
| 554 | |
| 555 | class bernoulli_distribution |
| 556 | { |
| 557 | public: |
| 558 | // types |
| 559 | typedef bool result_type; |
| 560 | |
| 561 | class param_type |
| 562 | { |
| 563 | public: |
| 564 | typedef bernoulli_distribution distribution_type; |
| 565 | |
| 566 | explicit param_type(double p = 0.5); |
| 567 | |
| 568 | double p() const; |
| 569 | |
| 570 | friend bool operator==(const param_type& x, const param_type& y); |
| 571 | friend bool operator!=(const param_type& x, const param_type& y); |
| 572 | }; |
| 573 | |
| 574 | // constructors and reset functions |
| 575 | explicit bernoulli_distribution(double p = 0.5); |
| 576 | explicit bernoulli_distribution(const param_type& parm); |
| 577 | void reset(); |
| 578 | |
| 579 | // generating functions |
| 580 | template<class URNG> result_type operator()(URNG& g); |
| 581 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 582 | |
| 583 | // property functions |
| 584 | double p() const; |
| 585 | |
| 586 | param_type param() const; |
| 587 | void param(const param_type& parm); |
| 588 | |
| 589 | result_type min() const; |
| 590 | result_type max() const; |
| 591 | |
| 592 | friend bool operator==(const bernoulli_distribution& x, |
| 593 | const bernoulli_distribution& y); |
| 594 | friend bool operator!=(const bernoulli_distribution& x, |
| 595 | const bernoulli_distribution& y); |
| 596 | |
| 597 | template <class charT, class traits> |
| 598 | friend |
| 599 | basic_ostream<charT, traits>& |
| 600 | operator<<(basic_ostream<charT, traits>& os, |
| 601 | const bernoulli_distribution& x); |
| 602 | |
| 603 | template <class charT, class traits> |
| 604 | friend |
| 605 | basic_istream<charT, traits>& |
| 606 | operator>>(basic_istream<charT, traits>& is, |
| 607 | bernoulli_distribution& x); |
| 608 | }; |
| 609 | |
| 610 | template<class IntType = int> |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 611 | class binomial_distribution |
| 612 | { |
| 613 | public: |
| 614 | // types |
| 615 | typedef IntType result_type; |
| 616 | |
| 617 | class param_type |
| 618 | { |
| 619 | public: |
| 620 | typedef binomial_distribution distribution_type; |
| 621 | |
| 622 | explicit param_type(IntType t = 1, double p = 0.5); |
| 623 | |
| 624 | IntType t() const; |
| 625 | double p() const; |
| 626 | |
| 627 | friend bool operator==(const param_type& x, const param_type& y); |
| 628 | friend bool operator!=(const param_type& x, const param_type& y); |
| 629 | }; |
| 630 | |
| 631 | // constructors and reset functions |
| 632 | explicit binomial_distribution(IntType t = 1, double p = 0.5); |
| 633 | explicit binomial_distribution(const param_type& parm); |
| 634 | void reset(); |
| 635 | |
| 636 | // generating functions |
| 637 | template<class URNG> result_type operator()(URNG& g); |
| 638 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 639 | |
| 640 | // property functions |
| 641 | IntType t() const; |
| 642 | double p() const; |
| 643 | |
| 644 | param_type param() const; |
| 645 | void param(const param_type& parm); |
| 646 | |
| 647 | result_type min() const; |
| 648 | result_type max() const; |
| 649 | |
| 650 | friend bool operator==(const binomial_distribution& x, |
| 651 | const binomial_distribution& y); |
| 652 | friend bool operator!=(const binomial_distribution& x, |
| 653 | const binomial_distribution& y); |
| 654 | |
| 655 | template <class charT, class traits> |
| 656 | friend |
| 657 | basic_ostream<charT, traits>& |
| 658 | operator<<(basic_ostream<charT, traits>& os, |
| 659 | const binomial_distribution& x); |
| 660 | |
| 661 | template <class charT, class traits> |
| 662 | friend |
| 663 | basic_istream<charT, traits>& |
| 664 | operator>>(basic_istream<charT, traits>& is, |
| 665 | binomial_distribution& x); |
| 666 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 667 | |
| 668 | template<class IntType = int> |
| 669 | class geometric_distribution; |
| 670 | |
| 671 | template<class IntType = int> |
| 672 | class negative_binomial_distribution; |
| 673 | |
| 674 | template<class IntType = int> |
Howard Hinnant | 4ff556c | 2010-05-14 21:38:54 +0000 | [diff] [blame^] | 675 | class poisson_distribution |
| 676 | { |
| 677 | public: |
| 678 | // types |
| 679 | typedef IntType result_type; |
| 680 | |
| 681 | class param_type |
| 682 | { |
| 683 | public: |
| 684 | typedef poisson_distribution distribution_type; |
| 685 | |
| 686 | explicit param_type(double mean = 1.0); |
| 687 | |
| 688 | double mean() const; |
| 689 | |
| 690 | friend bool operator==(const param_type& x, const param_type& y); |
| 691 | friend bool operator!=(const param_type& x, const param_type& y); |
| 692 | }; |
| 693 | |
| 694 | // constructors and reset functions |
| 695 | explicit poisson_distribution(double mean = 1.0); |
| 696 | explicit poisson_distribution(const param_type& parm); |
| 697 | void reset(); |
| 698 | |
| 699 | // generating functions |
| 700 | template<class URNG> result_type operator()(URNG& g); |
| 701 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 702 | |
| 703 | // property functions |
| 704 | double mean() const; |
| 705 | |
| 706 | param_type param() const; |
| 707 | void param(const param_type& parm); |
| 708 | |
| 709 | result_type min() const; |
| 710 | result_type max() const; |
| 711 | |
| 712 | friend bool operator==(const poisson_distribution& x, |
| 713 | const poisson_distribution& y); |
| 714 | friend bool operator!=(const poisson_distribution& x, |
| 715 | const poisson_distribution& y); |
| 716 | |
| 717 | template <class charT, class traits> |
| 718 | friend |
| 719 | basic_ostream<charT, traits>& |
| 720 | operator<<(basic_ostream<charT, traits>& os, |
| 721 | const poisson_distribution& x); |
| 722 | |
| 723 | template <class charT, class traits> |
| 724 | friend |
| 725 | basic_istream<charT, traits>& |
| 726 | operator>>(basic_istream<charT, traits>& is, |
| 727 | poisson_distribution& x); |
| 728 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 729 | |
| 730 | template<class RealType = double> |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 731 | class exponential_distribution |
| 732 | { |
| 733 | public: |
| 734 | // types |
| 735 | typedef RealType result_type; |
| 736 | |
| 737 | class param_type |
| 738 | { |
| 739 | public: |
| 740 | typedef exponential_distribution distribution_type; |
| 741 | |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 742 | explicit param_type(result_type lambda = 1.0); |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 743 | |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 744 | result_type lambda() const; |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 745 | |
| 746 | friend bool operator==(const param_type& x, const param_type& y); |
| 747 | friend bool operator!=(const param_type& x, const param_type& y); |
| 748 | }; |
| 749 | |
| 750 | // constructors and reset functions |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 751 | explicit exponential_distribution(result_type lambda = 1.0); |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 752 | explicit exponential_distribution(const param_type& parm); |
| 753 | void reset(); |
| 754 | |
| 755 | // generating functions |
| 756 | template<class URNG> result_type operator()(URNG& g); |
| 757 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 758 | |
| 759 | // property functions |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 760 | result_type lambda() const; |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 761 | |
| 762 | param_type param() const; |
| 763 | void param(const param_type& parm); |
| 764 | |
| 765 | result_type min() const; |
| 766 | result_type max() const; |
| 767 | |
| 768 | friend bool operator==(const exponential_distribution& x, |
| 769 | const exponential_distribution& y); |
| 770 | friend bool operator!=(const exponential_distribution& x, |
| 771 | const exponential_distribution& y); |
| 772 | |
| 773 | template <class charT, class traits> |
| 774 | friend |
| 775 | basic_ostream<charT, traits>& |
| 776 | operator<<(basic_ostream<charT, traits>& os, |
| 777 | const exponential_distribution& x); |
| 778 | |
| 779 | template <class charT, class traits> |
| 780 | friend |
| 781 | basic_istream<charT, traits>& |
| 782 | operator>>(basic_istream<charT, traits>& is, |
| 783 | exponential_distribution& x); |
| 784 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 785 | |
| 786 | template<class RealType = double> |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 787 | class gamma_distribution |
| 788 | { |
| 789 | public: |
| 790 | // types |
| 791 | typedef RealType result_type; |
| 792 | |
| 793 | class param_type |
| 794 | { |
| 795 | public: |
| 796 | typedef gamma_distribution distribution_type; |
| 797 | |
| 798 | explicit param_type(result_type alpha = 1, result_type beta = 1); |
| 799 | |
| 800 | result_type alpha() const; |
| 801 | result_type beta() const; |
| 802 | |
| 803 | friend bool operator==(const param_type& x, const param_type& y); |
| 804 | friend bool operator!=(const param_type& x, const param_type& y); |
| 805 | }; |
| 806 | |
| 807 | // constructors and reset functions |
| 808 | explicit gamma_distribution(result_type alpha = 1, result_type beta = 1); |
| 809 | explicit gamma_distribution(const param_type& parm); |
| 810 | void reset(); |
| 811 | |
| 812 | // generating functions |
| 813 | template<class URNG> result_type operator()(URNG& g); |
| 814 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 815 | |
| 816 | // property functions |
| 817 | result_type alpha() const; |
| 818 | result_type beta() const; |
| 819 | |
| 820 | param_type param() const; |
| 821 | void param(const param_type& parm); |
| 822 | |
| 823 | result_type min() const; |
| 824 | result_type max() const; |
| 825 | |
| 826 | friend bool operator==(const gamma_distribution& x, |
| 827 | const gamma_distribution& y); |
| 828 | friend bool operator!=(const gamma_distribution& x, |
| 829 | const gamma_distribution& y); |
| 830 | |
| 831 | template <class charT, class traits> |
| 832 | friend |
| 833 | basic_ostream<charT, traits>& |
| 834 | operator<<(basic_ostream<charT, traits>& os, |
| 835 | const gamma_distribution& x); |
| 836 | |
| 837 | template <class charT, class traits> |
| 838 | friend |
| 839 | basic_istream<charT, traits>& |
| 840 | operator>>(basic_istream<charT, traits>& is, |
| 841 | gamma_distribution& x); |
| 842 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 843 | |
| 844 | template<class RealType = double> |
| 845 | class weibull_distribution; |
| 846 | |
| 847 | template<class RealType = double> |
| 848 | class extreme_value_distribution; |
| 849 | |
| 850 | template<class RealType = double> |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 851 | class normal_distribution |
| 852 | { |
| 853 | public: |
| 854 | // types |
| 855 | typedef RealType result_type; |
| 856 | |
| 857 | class param_type |
| 858 | { |
| 859 | public: |
| 860 | typedef normal_distribution distribution_type; |
| 861 | |
| 862 | explicit param_type(result_type mean = 0, result_type stddev = 1); |
| 863 | |
| 864 | result_type mean() const; |
| 865 | result_type stddev() const; |
| 866 | |
| 867 | friend bool operator==(const param_type& x, const param_type& y); |
| 868 | friend bool operator!=(const param_type& x, const param_type& y); |
| 869 | }; |
| 870 | |
| 871 | // constructors and reset functions |
| 872 | explicit normal_distribution(result_type mean = 0, result_type stddev = 1); |
| 873 | explicit normal_distribution(const param_type& parm); |
| 874 | void reset(); |
| 875 | |
| 876 | // generating functions |
| 877 | template<class URNG> result_type operator()(URNG& g); |
| 878 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 879 | |
| 880 | // property functions |
| 881 | result_type mean() const; |
| 882 | result_type stddev() const; |
| 883 | |
| 884 | param_type param() const; |
| 885 | void param(const param_type& parm); |
| 886 | |
| 887 | result_type min() const; |
| 888 | result_type max() const; |
| 889 | |
| 890 | friend bool operator==(const normal_distribution& x, |
| 891 | const normal_distribution& y); |
| 892 | friend bool operator!=(const normal_distribution& x, |
| 893 | const normal_distribution& y); |
| 894 | |
| 895 | template <class charT, class traits> |
| 896 | friend |
| 897 | basic_ostream<charT, traits>& |
| 898 | operator<<(basic_ostream<charT, traits>& os, |
| 899 | const normal_distribution& x); |
| 900 | |
| 901 | template <class charT, class traits> |
| 902 | friend |
| 903 | basic_istream<charT, traits>& |
| 904 | operator>>(basic_istream<charT, traits>& is, |
| 905 | normal_distribution& x); |
| 906 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 907 | |
| 908 | template<class RealType = double> |
| 909 | class lognormal_distribution; |
| 910 | |
| 911 | template<class RealType = double> |
| 912 | class chi_squared_distribution; |
| 913 | |
| 914 | template<class RealType = double> |
| 915 | class cauchy_distribution; |
| 916 | |
| 917 | template<class RealType = double> |
| 918 | class fisher_f_distribution; |
| 919 | |
| 920 | template<class RealType = double> |
| 921 | class student_t_distribution; |
| 922 | |
| 923 | template<class IntType = int> |
| 924 | class discrete_distribution; |
| 925 | |
| 926 | template<class RealType = double> |
| 927 | class piecewise_constant_distribution; |
| 928 | |
| 929 | template<class RealType = double> |
| 930 | class piecewise_linear_distribution; |
| 931 | |
| 932 | } // std |
| 933 | */ |
| 934 | |
| 935 | #include <__config> |
| 936 | #include <cstddef> |
| 937 | #include <type_traits> |
| 938 | #include <initializer_list> |
| 939 | #include <cstdint> |
| 940 | #include <limits> |
| 941 | #include <algorithm> |
| 942 | #include <vector> |
| 943 | #include <string> |
| 944 | #include <istream> |
| 945 | #include <ostream> |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 946 | #include <cmath> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 947 | |
| 948 | #pragma GCC system_header |
| 949 | |
| 950 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 951 | |
| 952 | // linear_congruential_engine |
| 953 | |
| 954 | template <unsigned long long __a, unsigned long long __c, |
| 955 | unsigned long long __m, unsigned long long _M, |
| 956 | bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)> |
| 957 | struct __lce_ta; |
| 958 | |
| 959 | // 64 |
| 960 | |
| 961 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 962 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true> |
| 963 | { |
| 964 | typedef unsigned long long result_type; |
| 965 | static result_type next(result_type __x) |
| 966 | { |
| 967 | // Schrage's algorithm |
| 968 | const result_type __q = __m / __a; |
| 969 | const result_type __r = __m % __a; |
| 970 | const result_type __t0 = __a * (__x % __q); |
| 971 | const result_type __t1 = __r * (__x / __q); |
| 972 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 973 | __x += __c - (__x >= __m - __c) * __m; |
| 974 | return __x; |
| 975 | } |
| 976 | }; |
| 977 | |
| 978 | template <unsigned long long __a, unsigned long long __m> |
| 979 | struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true> |
| 980 | { |
| 981 | typedef unsigned long long result_type; |
| 982 | static result_type next(result_type __x) |
| 983 | { |
| 984 | // Schrage's algorithm |
| 985 | const result_type __q = __m / __a; |
| 986 | const result_type __r = __m % __a; |
| 987 | const result_type __t0 = __a * (__x % __q); |
| 988 | const result_type __t1 = __r * (__x / __q); |
| 989 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 990 | return __x; |
| 991 | } |
| 992 | }; |
| 993 | |
| 994 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 995 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false> |
| 996 | { |
| 997 | typedef unsigned long long result_type; |
| 998 | static result_type next(result_type __x) |
| 999 | { |
| 1000 | return (__a * __x + __c) % __m; |
| 1001 | } |
| 1002 | }; |
| 1003 | |
| 1004 | template <unsigned long long __a, unsigned long long __c> |
| 1005 | struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false> |
| 1006 | { |
| 1007 | typedef unsigned long long result_type; |
| 1008 | static result_type next(result_type __x) |
| 1009 | { |
| 1010 | return __a * __x + __c; |
| 1011 | } |
| 1012 | }; |
| 1013 | |
| 1014 | // 32 |
| 1015 | |
| 1016 | template <unsigned long long _A, unsigned long long _C, unsigned long long _M> |
| 1017 | struct __lce_ta<_A, _C, _M, unsigned(~0), true> |
| 1018 | { |
| 1019 | typedef unsigned result_type; |
| 1020 | static result_type next(result_type __x) |
| 1021 | { |
| 1022 | const result_type __a = static_cast<result_type>(_A); |
| 1023 | const result_type __c = static_cast<result_type>(_C); |
| 1024 | const result_type __m = static_cast<result_type>(_M); |
| 1025 | // Schrage's algorithm |
| 1026 | const result_type __q = __m / __a; |
| 1027 | const result_type __r = __m % __a; |
| 1028 | const result_type __t0 = __a * (__x % __q); |
| 1029 | const result_type __t1 = __r * (__x / __q); |
| 1030 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1031 | __x += __c - (__x >= __m - __c) * __m; |
| 1032 | return __x; |
| 1033 | } |
| 1034 | }; |
| 1035 | |
| 1036 | template <unsigned long long _A, unsigned long long _M> |
| 1037 | struct __lce_ta<_A, 0, _M, unsigned(~0), true> |
| 1038 | { |
| 1039 | typedef unsigned result_type; |
| 1040 | static result_type next(result_type __x) |
| 1041 | { |
| 1042 | const result_type __a = static_cast<result_type>(_A); |
| 1043 | const result_type __m = static_cast<result_type>(_M); |
| 1044 | // Schrage's algorithm |
| 1045 | const result_type __q = __m / __a; |
| 1046 | const result_type __r = __m % __a; |
| 1047 | const result_type __t0 = __a * (__x % __q); |
| 1048 | const result_type __t1 = __r * (__x / __q); |
| 1049 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1050 | return __x; |
| 1051 | } |
| 1052 | }; |
| 1053 | |
| 1054 | template <unsigned long long _A, unsigned long long _C, unsigned long long _M> |
| 1055 | struct __lce_ta<_A, _C, _M, unsigned(~0), false> |
| 1056 | { |
| 1057 | typedef unsigned result_type; |
| 1058 | static result_type next(result_type __x) |
| 1059 | { |
| 1060 | const result_type __a = static_cast<result_type>(_A); |
| 1061 | const result_type __c = static_cast<result_type>(_C); |
| 1062 | const result_type __m = static_cast<result_type>(_M); |
| 1063 | return (__a * __x + __c) % __m; |
| 1064 | } |
| 1065 | }; |
| 1066 | |
| 1067 | template <unsigned long long _A, unsigned long long _C> |
| 1068 | struct __lce_ta<_A, _C, 0, unsigned(~0), false> |
| 1069 | { |
| 1070 | typedef unsigned result_type; |
| 1071 | static result_type next(result_type __x) |
| 1072 | { |
| 1073 | const result_type __a = static_cast<result_type>(_A); |
| 1074 | const result_type __c = static_cast<result_type>(_C); |
| 1075 | return __a * __x + __c; |
| 1076 | } |
| 1077 | }; |
| 1078 | |
| 1079 | // 16 |
| 1080 | |
| 1081 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b> |
| 1082 | struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b> |
| 1083 | { |
| 1084 | typedef unsigned short result_type; |
| 1085 | static result_type next(result_type __x) |
| 1086 | { |
| 1087 | return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x)); |
| 1088 | } |
| 1089 | }; |
| 1090 | |
| 1091 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1092 | class linear_congruential_engine; |
| 1093 | |
| 1094 | template <class _CharT, class _Traits, |
| 1095 | class _U, _U _A, _U _C, _U _N> |
| 1096 | basic_ostream<_CharT, _Traits>& |
| 1097 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1098 | const linear_congruential_engine<_U, _A, _C, _N>&); |
| 1099 | |
| 1100 | template <class _CharT, class _Traits, |
| 1101 | class _U, _U _A, _U _C, _U _N> |
| 1102 | basic_istream<_CharT, _Traits>& |
| 1103 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1104 | linear_congruential_engine<_U, _A, _C, _N>& __x); |
| 1105 | |
| 1106 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1107 | class linear_congruential_engine |
| 1108 | { |
| 1109 | public: |
| 1110 | // types |
| 1111 | typedef _UIntType result_type; |
| 1112 | |
| 1113 | private: |
| 1114 | result_type __x_; |
| 1115 | |
| 1116 | static const result_type _M = result_type(~0); |
| 1117 | |
| 1118 | static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters"); |
| 1119 | static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters"); |
| 1120 | public: |
| 1121 | static const result_type _Min = __c == 0u ? 1u: 0u; |
| 1122 | static const result_type _Max = __m - 1u; |
| 1123 | static_assert(_Min < _Max, "linear_congruential_engine invalid parameters"); |
| 1124 | |
| 1125 | // engine characteristics |
| 1126 | static const/*expr*/ result_type multiplier = __a; |
| 1127 | static const/*expr*/ result_type increment = __c; |
| 1128 | static const/*expr*/ result_type modulus = __m; |
| 1129 | static const/*expr*/ result_type min() {return _Min;} |
| 1130 | static const/*expr*/ result_type max() {return _Max;} |
| 1131 | static const/*expr*/ result_type default_seed = 1u; |
| 1132 | |
| 1133 | // constructors and seeding functions |
| 1134 | explicit linear_congruential_engine(result_type __s = default_seed) |
| 1135 | {seed(__s);} |
| 1136 | template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q) |
| 1137 | {seed(__q);} |
| 1138 | void seed(result_type __s = default_seed) |
| 1139 | {seed(integral_constant<bool, __m == 0>(), |
| 1140 | integral_constant<bool, __c == 0>(), __s);} |
| 1141 | template<class _Sseq> |
| 1142 | typename enable_if |
| 1143 | < |
| 1144 | !is_convertible<_Sseq, result_type>::value, |
| 1145 | void |
| 1146 | >::type |
| 1147 | seed(_Sseq& __q) |
| 1148 | {__seed(__q, integral_constant<unsigned, |
| 1149 | 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32 |
| 1150 | : (__m-1) / 0x100000000ull)>());} |
| 1151 | |
| 1152 | // generating functions |
| 1153 | result_type operator()() |
| 1154 | {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));} |
| 1155 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 1156 | |
| 1157 | friend bool operator==(const linear_congruential_engine& __x, |
| 1158 | const linear_congruential_engine& __y) |
| 1159 | {return __x.__x_ == __y.__x_;} |
| 1160 | friend bool operator!=(const linear_congruential_engine& __x, |
| 1161 | const linear_congruential_engine& __y) |
| 1162 | {return !(__x == __y);} |
| 1163 | |
| 1164 | private: |
| 1165 | |
| 1166 | void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;} |
| 1167 | void seed(true_type, false_type, result_type __s) {__x_ = __s;} |
| 1168 | void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ? |
| 1169 | 1 : __s % __m;} |
| 1170 | void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;} |
| 1171 | |
| 1172 | template<class _Sseq> |
| 1173 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 1174 | template<class _Sseq> |
| 1175 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 1176 | |
| 1177 | template <class _CharT, class _Traits, |
| 1178 | class _U, _U _A, _U _C, _U _N> |
| 1179 | friend |
| 1180 | basic_ostream<_CharT, _Traits>& |
| 1181 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1182 | const linear_congruential_engine<_U, _A, _C, _N>&); |
| 1183 | |
| 1184 | template <class _CharT, class _Traits, |
| 1185 | class _U, _U _A, _U _C, _U _N> |
| 1186 | friend |
| 1187 | basic_istream<_CharT, _Traits>& |
| 1188 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1189 | linear_congruential_engine<_U, _A, _C, _N>& __x); |
| 1190 | }; |
| 1191 | |
| 1192 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1193 | template<class _Sseq> |
| 1194 | void |
| 1195 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 1196 | integral_constant<unsigned, 1>) |
| 1197 | { |
| 1198 | const unsigned __k = 1; |
| 1199 | uint32_t __ar[__k+3]; |
| 1200 | __q.generate(__ar, __ar + __k + 3); |
| 1201 | result_type __s = static_cast<result_type>(__ar[3] % __m); |
| 1202 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 1203 | } |
| 1204 | |
| 1205 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1206 | template<class _Sseq> |
| 1207 | void |
| 1208 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 1209 | integral_constant<unsigned, 2>) |
| 1210 | { |
| 1211 | const unsigned __k = 2; |
| 1212 | uint32_t __ar[__k+3]; |
| 1213 | __q.generate(__ar, __ar + __k + 3); |
| 1214 | result_type __s = static_cast<result_type>((__ar[3] + |
| 1215 | (uint64_t)__ar[4] << 32) % __m); |
| 1216 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 1217 | } |
| 1218 | |
| 1219 | template <class _CharT, class _Traits> |
| 1220 | class __save_flags |
| 1221 | { |
| 1222 | typedef basic_ios<_CharT, _Traits> __stream_type; |
| 1223 | typedef typename __stream_type::fmtflags fmtflags; |
| 1224 | |
| 1225 | __stream_type& __stream_; |
| 1226 | fmtflags __fmtflags_; |
| 1227 | _CharT __fill_; |
| 1228 | |
| 1229 | __save_flags(const __save_flags&); |
| 1230 | __save_flags& operator=(const __save_flags&); |
| 1231 | public: |
| 1232 | explicit __save_flags(__stream_type& __stream) |
| 1233 | : __stream_(__stream), |
| 1234 | __fmtflags_(__stream.flags()), |
| 1235 | __fill_(__stream.fill()) |
| 1236 | {} |
| 1237 | ~__save_flags() |
| 1238 | { |
| 1239 | __stream_.flags(__fmtflags_); |
| 1240 | __stream_.fill(__fill_); |
| 1241 | } |
| 1242 | }; |
| 1243 | |
| 1244 | template <class _CharT, class _Traits, |
| 1245 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1246 | inline |
| 1247 | basic_ostream<_CharT, _Traits>& |
| 1248 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1249 | const linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 1250 | { |
| 1251 | __save_flags<_CharT, _Traits> _(__os); |
| 1252 | __os.flags(ios_base::dec | ios_base::left); |
| 1253 | __os.fill(__os.widen(' ')); |
| 1254 | return __os << __x.__x_; |
| 1255 | } |
| 1256 | |
| 1257 | template <class _CharT, class _Traits, |
| 1258 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1259 | basic_istream<_CharT, _Traits>& |
| 1260 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1261 | linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 1262 | { |
| 1263 | __save_flags<_CharT, _Traits> _(__is); |
| 1264 | __is.flags(ios_base::dec | ios_base::skipws); |
| 1265 | _UIntType __t; |
| 1266 | __is >> __t; |
| 1267 | if (!__is.fail()) |
| 1268 | __x.__x_ = __t; |
| 1269 | return __is; |
| 1270 | } |
| 1271 | |
| 1272 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> |
| 1273 | minstd_rand0; |
| 1274 | typedef minstd_rand0 default_random_engine; |
| 1275 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> |
| 1276 | minstd_rand; |
| 1277 | // mersenne_twister_engine |
| 1278 | |
| 1279 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1280 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1281 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1282 | class mersenne_twister_engine; |
| 1283 | |
| 1284 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1285 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1286 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1287 | bool |
| 1288 | operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1289 | _B, _T, _C, _L, _F>& __x, |
| 1290 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1291 | _B, _T, _C, _L, _F>& __y); |
| 1292 | |
| 1293 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1294 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1295 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1296 | bool |
| 1297 | operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1298 | _B, _T, _C, _L, _F>& __x, |
| 1299 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1300 | _B, _T, _C, _L, _F>& __y); |
| 1301 | |
| 1302 | template <class _CharT, class _Traits, |
| 1303 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1304 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1305 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1306 | basic_ostream<_CharT, _Traits>& |
| 1307 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1308 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1309 | _B, _T, _C, _L, _F>& __x); |
| 1310 | |
| 1311 | template <class _CharT, class _Traits, |
| 1312 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1313 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1314 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1315 | basic_istream<_CharT, _Traits>& |
| 1316 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1317 | mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1318 | _B, _T, _C, _L, _F>& __x); |
| 1319 | |
| 1320 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1321 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1322 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1323 | class mersenne_twister_engine |
| 1324 | { |
| 1325 | public: |
| 1326 | // types |
| 1327 | typedef _UIntType result_type; |
| 1328 | |
| 1329 | private: |
| 1330 | result_type __x_[__n]; |
| 1331 | size_t __i_; |
| 1332 | |
| 1333 | static_assert( 0 < __m, "mersenne_twister_engine invalid parameters"); |
| 1334 | static_assert(__m <= __n, "mersenne_twister_engine invalid parameters"); |
| 1335 | static const result_type _Dt = numeric_limits<result_type>::digits; |
| 1336 | static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters"); |
| 1337 | static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters"); |
| 1338 | static_assert(__r <= __w, "mersenne_twister_engine invalid parameters"); |
| 1339 | static_assert(__u <= __w, "mersenne_twister_engine invalid parameters"); |
| 1340 | static_assert(__s <= __w, "mersenne_twister_engine invalid parameters"); |
| 1341 | static_assert(__t <= __w, "mersenne_twister_engine invalid parameters"); |
| 1342 | static_assert(__l <= __w, "mersenne_twister_engine invalid parameters"); |
| 1343 | public: |
| 1344 | static const result_type _Min = 0; |
| 1345 | static const result_type _Max = __w == _Dt ? result_type(~0) : |
| 1346 | (result_type(1) << __w) - result_type(1); |
| 1347 | static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters"); |
| 1348 | static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters"); |
| 1349 | static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters"); |
| 1350 | static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters"); |
| 1351 | static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters"); |
| 1352 | static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters"); |
| 1353 | |
| 1354 | // engine characteristics |
| 1355 | static const/*expr*/ size_t word_size = __w; |
| 1356 | static const/*expr*/ size_t state_size = __n; |
| 1357 | static const/*expr*/ size_t shift_size = __m; |
| 1358 | static const/*expr*/ size_t mask_bits = __r; |
| 1359 | static const/*expr*/ result_type xor_mask = __a; |
| 1360 | static const/*expr*/ size_t tempering_u = __u; |
| 1361 | static const/*expr*/ result_type tempering_d = __d; |
| 1362 | static const/*expr*/ size_t tempering_s = __s; |
| 1363 | static const/*expr*/ result_type tempering_b = __b; |
| 1364 | static const/*expr*/ size_t tempering_t = __t; |
| 1365 | static const/*expr*/ result_type tempering_c = __c; |
| 1366 | static const/*expr*/ size_t tempering_l = __l; |
| 1367 | static const/*expr*/ result_type initialization_multiplier = __f; |
| 1368 | static const/*expr*/ result_type min() { return _Min; } |
| 1369 | static const/*expr*/ result_type max() { return _Max; } |
| 1370 | static const/*expr*/ result_type default_seed = 5489u; |
| 1371 | |
| 1372 | // constructors and seeding functions |
| 1373 | explicit mersenne_twister_engine(result_type __sd = default_seed) |
| 1374 | {seed(__sd);} |
| 1375 | template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q) |
| 1376 | {seed(__q);} |
| 1377 | void seed(result_type __sd = default_seed); |
| 1378 | template<class _Sseq> |
| 1379 | typename enable_if |
| 1380 | < |
| 1381 | !is_convertible<_Sseq, result_type>::value, |
| 1382 | void |
| 1383 | >::type |
| 1384 | seed(_Sseq& __q) |
| 1385 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 1386 | |
| 1387 | // generating functions |
| 1388 | result_type operator()(); |
| 1389 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 1390 | |
| 1391 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1392 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1393 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1394 | friend |
| 1395 | bool |
| 1396 | operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1397 | _B, _T, _C, _L, _F>& __x, |
| 1398 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1399 | _B, _T, _C, _L, _F>& __y); |
| 1400 | |
| 1401 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1402 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1403 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1404 | friend |
| 1405 | bool |
| 1406 | operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1407 | _B, _T, _C, _L, _F>& __x, |
| 1408 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1409 | _B, _T, _C, _L, _F>& __y); |
| 1410 | |
| 1411 | template <class _CharT, class _Traits, |
| 1412 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1413 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1414 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1415 | friend |
| 1416 | basic_ostream<_CharT, _Traits>& |
| 1417 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1418 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1419 | _B, _T, _C, _L, _F>& __x); |
| 1420 | |
| 1421 | template <class _CharT, class _Traits, |
| 1422 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1423 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1424 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1425 | friend |
| 1426 | basic_istream<_CharT, _Traits>& |
| 1427 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1428 | mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1429 | _B, _T, _C, _L, _F>& __x); |
| 1430 | private: |
| 1431 | |
| 1432 | template<class _Sseq> |
| 1433 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 1434 | template<class _Sseq> |
| 1435 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 1436 | |
| 1437 | template <size_t __count> |
| 1438 | static |
| 1439 | typename enable_if |
| 1440 | < |
| 1441 | __count < __w, |
| 1442 | result_type |
| 1443 | >::type |
| 1444 | __lshift(result_type __x) {return (__x << __count) & _Max;} |
| 1445 | |
| 1446 | template <size_t __count> |
| 1447 | static |
| 1448 | typename enable_if |
| 1449 | < |
| 1450 | (__count >= __w), |
| 1451 | result_type |
| 1452 | >::type |
| 1453 | __lshift(result_type __x) {return result_type(0);} |
| 1454 | |
| 1455 | template <size_t __count> |
| 1456 | static |
| 1457 | typename enable_if |
| 1458 | < |
| 1459 | __count < _Dt, |
| 1460 | result_type |
| 1461 | >::type |
| 1462 | __rshift(result_type __x) {return __x >> __count;} |
| 1463 | |
| 1464 | template <size_t __count> |
| 1465 | static |
| 1466 | typename enable_if |
| 1467 | < |
| 1468 | (__count >= _Dt), |
| 1469 | result_type |
| 1470 | >::type |
| 1471 | __rshift(result_type __x) {return result_type(0);} |
| 1472 | }; |
| 1473 | |
| 1474 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1475 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1476 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1477 | void |
| 1478 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 1479 | __t, __c, __l, __f>::seed(result_type __sd) |
| 1480 | { // __w >= 2 |
| 1481 | __x_[0] = __sd & _Max; |
| 1482 | for (size_t __i = 1; __i < __n; ++__i) |
| 1483 | __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max; |
| 1484 | __i_ = 0; |
| 1485 | } |
| 1486 | |
| 1487 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1488 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1489 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1490 | template<class _Sseq> |
| 1491 | void |
| 1492 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 1493 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>) |
| 1494 | { |
| 1495 | const unsigned __k = 1; |
| 1496 | uint32_t __ar[__n * __k]; |
| 1497 | __q.generate(__ar, __ar + __n * __k); |
| 1498 | for (size_t __i = 0; __i < __n; ++__i) |
| 1499 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 1500 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 1501 | (result_type(1) << __r) - result_type(1); |
| 1502 | __i_ = 0; |
| 1503 | if ((__x_[0] & ~__mask) == 0) |
| 1504 | { |
| 1505 | for (size_t __i = 1; __i < __n; ++__i) |
| 1506 | if (__x_[__i] != 0) |
| 1507 | return; |
| 1508 | __x_[0] = _Max; |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1513 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1514 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1515 | template<class _Sseq> |
| 1516 | void |
| 1517 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 1518 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>) |
| 1519 | { |
| 1520 | const unsigned __k = 2; |
| 1521 | uint32_t __ar[__n * __k]; |
| 1522 | __q.generate(__ar, __ar + __n * __k); |
| 1523 | for (size_t __i = 0; __i < __n; ++__i) |
| 1524 | __x_[__i] = static_cast<result_type>( |
| 1525 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 1526 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 1527 | (result_type(1) << __r) - result_type(1); |
| 1528 | __i_ = 0; |
| 1529 | if ((__x_[0] & ~__mask) == 0) |
| 1530 | { |
| 1531 | for (size_t __i = 1; __i < __n; ++__i) |
| 1532 | if (__x_[__i] != 0) |
| 1533 | return; |
| 1534 | __x_[0] = _Max; |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1539 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1540 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1541 | _UIntType |
| 1542 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 1543 | __t, __c, __l, __f>::operator()() |
| 1544 | { |
| 1545 | const size_t __j = (__i_ + 1) % __n; |
| 1546 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 1547 | (result_type(1) << __r) - result_type(1); |
| 1548 | const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask); |
| 1549 | const size_t __k = (__i_ + __m) % __n; |
| 1550 | __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1)); |
| 1551 | result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d); |
| 1552 | __i_ = __j; |
| 1553 | __z ^= __lshift<__s>(__z) & __b; |
| 1554 | __z ^= __lshift<__t>(__z) & __c; |
| 1555 | return __z ^ __rshift<__l>(__z); |
| 1556 | } |
| 1557 | |
| 1558 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1559 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1560 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1561 | bool |
| 1562 | operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1563 | _B, _T, _C, _L, _F>& __x, |
| 1564 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1565 | _B, _T, _C, _L, _F>& __y) |
| 1566 | { |
| 1567 | if (__x.__i_ == __y.__i_) |
| 1568 | return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_); |
| 1569 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 1570 | { |
| 1571 | size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_); |
| 1572 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
| 1573 | __y.__x_ + __y.__i_)) |
| 1574 | return false; |
| 1575 | if (__x.__i_ == 0) |
| 1576 | return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_); |
| 1577 | return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j); |
| 1578 | } |
| 1579 | if (__x.__i_ < __y.__i_) |
| 1580 | { |
| 1581 | size_t __j = _N - __y.__i_; |
| 1582 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
| 1583 | __y.__x_ + __y.__i_)) |
| 1584 | return false; |
| 1585 | if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N, |
| 1586 | __y.__x_)) |
| 1587 | return false; |
| 1588 | return _STD::equal(__x.__x_, __x.__x_ + __x.__i_, |
| 1589 | __y.__x_ + (_N - (__x.__i_ + __j))); |
| 1590 | } |
| 1591 | size_t __j = _N - __x.__i_; |
| 1592 | if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
| 1593 | __x.__x_ + __x.__i_)) |
| 1594 | return false; |
| 1595 | if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N, |
| 1596 | __x.__x_)) |
| 1597 | return false; |
| 1598 | return _STD::equal(__y.__x_, __y.__x_ + __y.__i_, |
| 1599 | __x.__x_ + (_N - (__y.__i_ + __j))); |
| 1600 | } |
| 1601 | |
| 1602 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1603 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1604 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1605 | inline |
| 1606 | bool |
| 1607 | operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1608 | _B, _T, _C, _L, _F>& __x, |
| 1609 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1610 | _B, _T, _C, _L, _F>& __y) |
| 1611 | { |
| 1612 | return !(__x == __y); |
| 1613 | } |
| 1614 | |
| 1615 | template <class _CharT, class _Traits, |
| 1616 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1617 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1618 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1619 | basic_ostream<_CharT, _Traits>& |
| 1620 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1621 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1622 | _B, _T, _C, _L, _F>& __x) |
| 1623 | { |
| 1624 | __save_flags<_CharT, _Traits> _(__os); |
| 1625 | __os.flags(ios_base::dec | ios_base::left); |
| 1626 | _CharT __sp = __os.widen(' '); |
| 1627 | __os.fill(__sp); |
| 1628 | __os << __x.__x_[__x.__i_]; |
| 1629 | for (size_t __j = __x.__i_ + 1; __j < _N; ++__j) |
| 1630 | __os << __sp << __x.__x_[__j]; |
| 1631 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 1632 | __os << __sp << __x.__x_[__j]; |
| 1633 | return __os; |
| 1634 | } |
| 1635 | |
| 1636 | template <class _CharT, class _Traits, |
| 1637 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1638 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1639 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1640 | basic_istream<_CharT, _Traits>& |
| 1641 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1642 | mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1643 | _B, _T, _C, _L, _F>& __x) |
| 1644 | { |
| 1645 | __save_flags<_CharT, _Traits> _(__is); |
| 1646 | __is.flags(ios_base::dec | ios_base::skipws); |
| 1647 | _UI __t[_N]; |
| 1648 | for (size_t __i = 0; __i < _N; ++__i) |
| 1649 | __is >> __t[__i]; |
| 1650 | if (!__is.fail()) |
| 1651 | { |
| 1652 | for (size_t __i = 0; __i < _N; ++__i) |
| 1653 | __x.__x_[__i] = __t[__i]; |
| 1654 | __x.__i_ = 0; |
| 1655 | } |
| 1656 | return __is; |
| 1657 | } |
| 1658 | |
| 1659 | typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, |
| 1660 | 0x9908b0df, 11, 0xffffffff, |
| 1661 | 7, 0x9d2c5680, |
| 1662 | 15, 0xefc60000, |
| 1663 | 18, 1812433253> mt19937; |
| 1664 | typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31, |
| 1665 | 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL, |
| 1666 | 17, 0x71d67fffeda60000ULL, |
| 1667 | 37, 0xfff7eee000000000ULL, |
| 1668 | 43, 6364136223846793005ULL> mt19937_64; |
| 1669 | |
| 1670 | // subtract_with_carry_engine |
| 1671 | |
| 1672 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 1673 | class subtract_with_carry_engine; |
| 1674 | |
| 1675 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 1676 | bool |
| 1677 | operator==( |
| 1678 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 1679 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
| 1680 | |
| 1681 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 1682 | bool |
| 1683 | operator!=( |
| 1684 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 1685 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
| 1686 | |
| 1687 | template <class _CharT, class _Traits, |
| 1688 | class _UI, size_t _W, size_t _S, size_t _R> |
| 1689 | basic_ostream<_CharT, _Traits>& |
| 1690 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1691 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
| 1692 | |
| 1693 | template <class _CharT, class _Traits, |
| 1694 | class _UI, size_t _W, size_t _S, size_t _R> |
| 1695 | basic_istream<_CharT, _Traits>& |
| 1696 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1697 | subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
| 1698 | |
| 1699 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 1700 | class subtract_with_carry_engine |
| 1701 | { |
| 1702 | public: |
| 1703 | // types |
| 1704 | typedef _UIntType result_type; |
| 1705 | |
| 1706 | private: |
| 1707 | result_type __x_[__r]; |
| 1708 | result_type __c_; |
| 1709 | size_t __i_; |
| 1710 | |
| 1711 | static const result_type _Dt = numeric_limits<result_type>::digits; |
| 1712 | static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters"); |
| 1713 | static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters"); |
| 1714 | static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters"); |
| 1715 | static_assert(__s < __r, "subtract_with_carry_engine invalid parameters"); |
| 1716 | public: |
| 1717 | static const result_type _Min = 0; |
| 1718 | static const result_type _Max = __w == _Dt ? result_type(~0) : |
| 1719 | (result_type(1) << __w) - result_type(1); |
| 1720 | static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters"); |
| 1721 | |
| 1722 | // engine characteristics |
| 1723 | static const/*expr*/ size_t word_size = __w; |
| 1724 | static const/*expr*/ size_t short_lag = __s; |
| 1725 | static const/*expr*/ size_t long_lag = __r; |
| 1726 | static const/*expr*/ result_type min() { return _Min; } |
| 1727 | static const/*expr*/ result_type max() { return _Max; } |
| 1728 | static const/*expr*/ result_type default_seed = 19780503u; |
| 1729 | |
| 1730 | // constructors and seeding functions |
| 1731 | explicit subtract_with_carry_engine(result_type __sd = default_seed) |
| 1732 | {seed(__sd);} |
| 1733 | template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q) |
| 1734 | {seed(__q);} |
| 1735 | void seed(result_type __sd = default_seed) |
| 1736 | {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 1737 | template<class _Sseq> |
| 1738 | typename enable_if |
| 1739 | < |
| 1740 | !is_convertible<_Sseq, result_type>::value, |
| 1741 | void |
| 1742 | >::type |
| 1743 | seed(_Sseq& __q) |
| 1744 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 1745 | |
| 1746 | // generating functions |
| 1747 | result_type operator()(); |
| 1748 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 1749 | |
| 1750 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 1751 | friend |
| 1752 | bool |
| 1753 | operator==( |
| 1754 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 1755 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
| 1756 | |
| 1757 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 1758 | friend |
| 1759 | bool |
| 1760 | operator!=( |
| 1761 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 1762 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
| 1763 | |
| 1764 | template <class _CharT, class _Traits, |
| 1765 | class _UI, size_t _W, size_t _S, size_t _R> |
| 1766 | friend |
| 1767 | basic_ostream<_CharT, _Traits>& |
| 1768 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1769 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
| 1770 | |
| 1771 | template <class _CharT, class _Traits, |
| 1772 | class _UI, size_t _W, size_t _S, size_t _R> |
| 1773 | friend |
| 1774 | basic_istream<_CharT, _Traits>& |
| 1775 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1776 | subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
| 1777 | |
| 1778 | private: |
| 1779 | |
| 1780 | void seed(result_type __sd, integral_constant<unsigned, 1>); |
| 1781 | void seed(result_type __sd, integral_constant<unsigned, 2>); |
| 1782 | template<class _Sseq> |
| 1783 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 1784 | template<class _Sseq> |
| 1785 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 1786 | }; |
| 1787 | |
| 1788 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 1789 | void |
| 1790 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 1791 | integral_constant<unsigned, 1>) |
| 1792 | { |
| 1793 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 1794 | __e(__sd == 0u ? default_seed : __sd); |
| 1795 | for (size_t __i = 0; __i < __r; ++__i) |
| 1796 | __x_[__i] = static_cast<result_type>(__e() & _Max); |
| 1797 | __c_ = __x_[__r-1] == 0; |
| 1798 | __i_ = 0; |
| 1799 | } |
| 1800 | |
| 1801 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 1802 | void |
| 1803 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 1804 | integral_constant<unsigned, 2>) |
| 1805 | { |
| 1806 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 1807 | __e(__sd == 0u ? default_seed : __sd); |
| 1808 | for (size_t __i = 0; __i < __r; ++__i) |
| 1809 | __x_[__i] = static_cast<result_type>( |
| 1810 | (__e() + ((uint64_t)__e() << 32)) & _Max); |
| 1811 | __c_ = __x_[__r-1] == 0; |
| 1812 | __i_ = 0; |
| 1813 | } |
| 1814 | |
| 1815 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 1816 | template<class _Sseq> |
| 1817 | void |
| 1818 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 1819 | integral_constant<unsigned, 1>) |
| 1820 | { |
| 1821 | const unsigned __k = 1; |
| 1822 | uint32_t __ar[__r * __k]; |
| 1823 | __q.generate(__ar, __ar + __r * __k); |
| 1824 | for (size_t __i = 0; __i < __r; ++__i) |
| 1825 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 1826 | __c_ = __x_[__r-1] == 0; |
| 1827 | __i_ = 0; |
| 1828 | } |
| 1829 | |
| 1830 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 1831 | template<class _Sseq> |
| 1832 | void |
| 1833 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 1834 | integral_constant<unsigned, 2>) |
| 1835 | { |
| 1836 | const unsigned __k = 2; |
| 1837 | uint32_t __ar[__r * __k]; |
| 1838 | __q.generate(__ar, __ar + __r * __k); |
| 1839 | for (size_t __i = 0; __i < __r; ++__i) |
| 1840 | __x_[__i] = static_cast<result_type>( |
| 1841 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 1842 | __c_ = __x_[__r-1] == 0; |
| 1843 | __i_ = 0; |
| 1844 | } |
| 1845 | |
| 1846 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 1847 | _UIntType |
| 1848 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()() |
| 1849 | { |
| 1850 | const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r]; |
| 1851 | result_type& __xr = __x_[__i_]; |
| 1852 | result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1; |
| 1853 | __xr = (__xs - __xr - __c_) & _Max; |
| 1854 | __c_ = __new_c; |
| 1855 | __i_ = (__i_ + 1) % __r; |
| 1856 | return __xr; |
| 1857 | } |
| 1858 | |
| 1859 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 1860 | bool |
| 1861 | operator==( |
| 1862 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 1863 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y) |
| 1864 | { |
| 1865 | if (__x.__c_ != __y.__c_) |
| 1866 | return false; |
| 1867 | if (__x.__i_ == __y.__i_) |
| 1868 | return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_); |
| 1869 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 1870 | { |
| 1871 | size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_); |
| 1872 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
| 1873 | __y.__x_ + __y.__i_)) |
| 1874 | return false; |
| 1875 | if (__x.__i_ == 0) |
| 1876 | return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_); |
| 1877 | return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j); |
| 1878 | } |
| 1879 | if (__x.__i_ < __y.__i_) |
| 1880 | { |
| 1881 | size_t __j = _R - __y.__i_; |
| 1882 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
| 1883 | __y.__x_ + __y.__i_)) |
| 1884 | return false; |
| 1885 | if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R, |
| 1886 | __y.__x_)) |
| 1887 | return false; |
| 1888 | return _STD::equal(__x.__x_, __x.__x_ + __x.__i_, |
| 1889 | __y.__x_ + (_R - (__x.__i_ + __j))); |
| 1890 | } |
| 1891 | size_t __j = _R - __x.__i_; |
| 1892 | if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
| 1893 | __x.__x_ + __x.__i_)) |
| 1894 | return false; |
| 1895 | if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R, |
| 1896 | __x.__x_)) |
| 1897 | return false; |
| 1898 | return _STD::equal(__y.__x_, __y.__x_ + __y.__i_, |
| 1899 | __x.__x_ + (_R - (__y.__i_ + __j))); |
| 1900 | } |
| 1901 | |
| 1902 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 1903 | inline |
| 1904 | bool |
| 1905 | operator!=( |
| 1906 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 1907 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y) |
| 1908 | { |
| 1909 | return !(__x == __y); |
| 1910 | } |
| 1911 | |
| 1912 | template <class _CharT, class _Traits, |
| 1913 | class _UI, size_t _W, size_t _S, size_t _R> |
| 1914 | basic_ostream<_CharT, _Traits>& |
| 1915 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1916 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x) |
| 1917 | { |
| 1918 | __save_flags<_CharT, _Traits> _(__os); |
| 1919 | __os.flags(ios_base::dec | ios_base::left); |
| 1920 | _CharT __sp = __os.widen(' '); |
| 1921 | __os.fill(__sp); |
| 1922 | __os << __x.__x_[__x.__i_]; |
| 1923 | for (size_t __j = __x.__i_ + 1; __j < _R; ++__j) |
| 1924 | __os << __sp << __x.__x_[__j]; |
| 1925 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 1926 | __os << __sp << __x.__x_[__j]; |
| 1927 | __os << __sp << __x.__c_; |
| 1928 | return __os; |
| 1929 | } |
| 1930 | |
| 1931 | template <class _CharT, class _Traits, |
| 1932 | class _UI, size_t _W, size_t _S, size_t _R> |
| 1933 | basic_istream<_CharT, _Traits>& |
| 1934 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1935 | subtract_with_carry_engine<_UI, _W, _S, _R>& __x) |
| 1936 | { |
| 1937 | __save_flags<_CharT, _Traits> _(__is); |
| 1938 | __is.flags(ios_base::dec | ios_base::skipws); |
| 1939 | _UI __t[_R+1]; |
| 1940 | for (size_t __i = 0; __i < _R+1; ++__i) |
| 1941 | __is >> __t[__i]; |
| 1942 | if (!__is.fail()) |
| 1943 | { |
| 1944 | for (size_t __i = 0; __i < _R; ++__i) |
| 1945 | __x.__x_[__i] = __t[__i]; |
| 1946 | __x.__c_ = __t[_R]; |
| 1947 | __x.__i_ = 0; |
| 1948 | } |
| 1949 | return __is; |
| 1950 | } |
| 1951 | |
| 1952 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base; |
| 1953 | typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base; |
| 1954 | |
| 1955 | // discard_block_engine |
| 1956 | |
| 1957 | template<class _Engine, size_t __p, size_t __r> |
| 1958 | class discard_block_engine |
| 1959 | { |
| 1960 | _Engine __e_; |
| 1961 | int __n_; |
| 1962 | |
| 1963 | static_assert( 0 < __r, "discard_block_engine invalid parameters"); |
| 1964 | static_assert(__r <= __p, "discard_block_engine invalid parameters"); |
| 1965 | public: |
| 1966 | // types |
| 1967 | typedef typename _Engine::result_type result_type; |
| 1968 | |
| 1969 | // engine characteristics |
| 1970 | static const/*expr*/ size_t block_size = __p; |
| 1971 | static const/*expr*/ size_t used_block = __r; |
| 1972 | |
| 1973 | // Temporary work around for lack of constexpr |
| 1974 | static const result_type _Min = _Engine::_Min; |
| 1975 | static const result_type _Max = _Engine::_Max; |
| 1976 | |
| 1977 | static const/*expr*/ result_type min() { return _Engine::min(); } |
| 1978 | static const/*expr*/ result_type max() { return _Engine::max(); } |
| 1979 | |
| 1980 | // constructors and seeding functions |
| 1981 | discard_block_engine() : __n_(0) {} |
| 1982 | // explicit discard_block_engine(const _Engine& __e); |
| 1983 | // explicit discard_block_engine(_Engine&& __e); |
| 1984 | explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {} |
| 1985 | template<class _Sseq> explicit discard_block_engine(_Sseq& __q) |
| 1986 | : __e_(__q), __n_(0) {} |
| 1987 | void seed() {__e_.seed(); __n_ = 0;} |
| 1988 | void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;} |
| 1989 | template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;} |
| 1990 | |
| 1991 | // generating functions |
| 1992 | result_type operator()(); |
| 1993 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 1994 | |
| 1995 | // property functions |
| 1996 | const _Engine& base() const {return __e_;} |
| 1997 | |
| 1998 | template<class _Eng, size_t _P, size_t _R> |
| 1999 | friend |
| 2000 | bool |
| 2001 | operator==( |
| 2002 | const discard_block_engine<_Eng, _P, _R>& __x, |
| 2003 | const discard_block_engine<_Eng, _P, _R>& __y); |
| 2004 | |
| 2005 | template<class _Eng, size_t _P, size_t _R> |
| 2006 | friend |
| 2007 | bool |
| 2008 | operator!=( |
| 2009 | const discard_block_engine<_Eng, _P, _R>& __x, |
| 2010 | const discard_block_engine<_Eng, _P, _R>& __y); |
| 2011 | |
| 2012 | template <class _CharT, class _Traits, |
| 2013 | class _Eng, size_t _P, size_t _R> |
| 2014 | friend |
| 2015 | basic_ostream<_CharT, _Traits>& |
| 2016 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2017 | const discard_block_engine<_Eng, _P, _R>& __x); |
| 2018 | |
| 2019 | template <class _CharT, class _Traits, |
| 2020 | class _Eng, size_t _P, size_t _R> |
| 2021 | friend |
| 2022 | basic_istream<_CharT, _Traits>& |
| 2023 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2024 | discard_block_engine<_Eng, _P, _R>& __x); |
| 2025 | }; |
| 2026 | |
| 2027 | template<class _Engine, size_t __p, size_t __r> |
| 2028 | typename discard_block_engine<_Engine, __p, __r>::result_type |
| 2029 | discard_block_engine<_Engine, __p, __r>::operator()() |
| 2030 | { |
| 2031 | if (__n_ >= __r) |
| 2032 | { |
| 2033 | __e_.discard(__p - __r); |
| 2034 | __n_ = 0; |
| 2035 | } |
| 2036 | ++__n_; |
| 2037 | return __e_(); |
| 2038 | } |
| 2039 | |
| 2040 | template<class _Eng, size_t _P, size_t _R> |
| 2041 | inline |
| 2042 | bool |
| 2043 | operator==(const discard_block_engine<_Eng, _P, _R>& __x, |
| 2044 | const discard_block_engine<_Eng, _P, _R>& __y) |
| 2045 | { |
| 2046 | return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_; |
| 2047 | } |
| 2048 | |
| 2049 | template<class _Eng, size_t _P, size_t _R> |
| 2050 | inline |
| 2051 | bool |
| 2052 | operator!=(const discard_block_engine<_Eng, _P, _R>& __x, |
| 2053 | const discard_block_engine<_Eng, _P, _R>& __y) |
| 2054 | { |
| 2055 | return !(__x == __y); |
| 2056 | } |
| 2057 | |
| 2058 | template <class _CharT, class _Traits, |
| 2059 | class _Eng, size_t _P, size_t _R> |
| 2060 | basic_ostream<_CharT, _Traits>& |
| 2061 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2062 | const discard_block_engine<_Eng, _P, _R>& __x) |
| 2063 | { |
| 2064 | __save_flags<_CharT, _Traits> _(__os); |
| 2065 | __os.flags(ios_base::dec | ios_base::left); |
| 2066 | _CharT __sp = __os.widen(' '); |
| 2067 | __os.fill(__sp); |
| 2068 | return __os << __x.__e_ << __sp << __x.__n_; |
| 2069 | } |
| 2070 | |
| 2071 | template <class _CharT, class _Traits, |
| 2072 | class _Eng, size_t _P, size_t _R> |
| 2073 | basic_istream<_CharT, _Traits>& |
| 2074 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2075 | discard_block_engine<_Eng, _P, _R>& __x) |
| 2076 | { |
| 2077 | __save_flags<_CharT, _Traits> _(__is); |
| 2078 | __is.flags(ios_base::dec | ios_base::skipws); |
| 2079 | _Eng __e; |
| 2080 | int __n; |
| 2081 | __is >> __e >> __n; |
| 2082 | if (!__is.fail()) |
| 2083 | { |
| 2084 | __x.__e_ = __e; |
| 2085 | __x.__n_ = __n; |
| 2086 | } |
| 2087 | return __is; |
| 2088 | } |
| 2089 | |
| 2090 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
| 2091 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
| 2092 | |
| 2093 | // independent_bits_engine |
| 2094 | |
| 2095 | template <unsigned long long _X, size_t _R> |
| 2096 | struct __log2_imp |
| 2097 | { |
| 2098 | static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R |
| 2099 | : __log2_imp<_X, _R - 1>::value; |
| 2100 | }; |
| 2101 | |
| 2102 | template <unsigned long long _X> |
| 2103 | struct __log2_imp<_X, 0> |
| 2104 | { |
| 2105 | static const size_t value = 0; |
| 2106 | }; |
| 2107 | |
| 2108 | template <size_t _R> |
| 2109 | struct __log2_imp<0, _R> |
| 2110 | { |
| 2111 | static const size_t value = _R + 1; |
| 2112 | }; |
| 2113 | |
| 2114 | template <class _UI, _UI _X> |
| 2115 | struct __log2 |
| 2116 | { |
| 2117 | static const size_t value = __log2_imp<_X, |
| 2118 | sizeof(_UI) * __CHAR_BIT__ - 1>::value; |
| 2119 | }; |
| 2120 | |
| 2121 | template<class _Engine, size_t __w, class _UIntType> |
| 2122 | class independent_bits_engine |
| 2123 | { |
| 2124 | template <class _UI, _UI _R0, size_t _W, size_t _M> |
| 2125 | class __get_n |
| 2126 | { |
| 2127 | static const size_t _Dt = numeric_limits<_UI>::digits; |
| 2128 | static const size_t _N = _W / _M + (_W % _M != 0); |
| 2129 | static const size_t _W0 = _W / _N; |
| 2130 | static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0; |
| 2131 | public: |
| 2132 | static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N; |
| 2133 | }; |
| 2134 | public: |
| 2135 | // types |
| 2136 | typedef _UIntType result_type; |
| 2137 | |
| 2138 | private: |
| 2139 | _Engine __e_; |
| 2140 | |
| 2141 | static const result_type _Dt = numeric_limits<result_type>::digits; |
| 2142 | static_assert( 0 < __w, "independent_bits_engine invalid parameters"); |
| 2143 | static_assert(__w <= _Dt, "independent_bits_engine invalid parameters"); |
| 2144 | |
| 2145 | typedef typename _Engine::result_type _Engine_result_type; |
| 2146 | typedef typename conditional |
| 2147 | < |
| 2148 | sizeof(_Engine_result_type) <= sizeof(result_type), |
| 2149 | result_type, |
| 2150 | _Engine_result_type |
| 2151 | >::type _Working_result_type; |
| 2152 | // Temporary work around for lack of constexpr |
| 2153 | static const _Working_result_type _R = _Engine::_Max - _Engine::_Min |
| 2154 | + _Working_result_type(1); |
| 2155 | static const size_t __m = __log2<_Working_result_type, _R>::value; |
| 2156 | static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value; |
| 2157 | static const size_t __w0 = __w / __n; |
| 2158 | static const size_t __n0 = __n - __w % __n; |
| 2159 | static const size_t _WDt = numeric_limits<_Working_result_type>::digits; |
| 2160 | static const size_t _EDt = numeric_limits<_Engine_result_type>::digits; |
| 2161 | static const _Working_result_type __y0 = __w0 >= _WDt ? 0 : |
| 2162 | (_R >> __w0) << __w0; |
| 2163 | static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : |
| 2164 | (_R >> (__w0+1)) << (__w0+1); |
| 2165 | static const _Engine_result_type __mask0 = __w0 > 0 ? |
| 2166 | _Engine_result_type(~0) >> (_EDt - __w0) : |
| 2167 | _Engine_result_type(0); |
| 2168 | static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ? |
| 2169 | _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : |
| 2170 | _Engine_result_type(~0); |
| 2171 | public: |
| 2172 | static const result_type _Min = 0; |
| 2173 | static const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2174 | (result_type(1) << __w) - result_type(1); |
| 2175 | static_assert(_Min < _Max, "independent_bits_engine invalid parameters"); |
| 2176 | |
| 2177 | // engine characteristics |
| 2178 | static const/*expr*/ result_type min() { return _Min; } |
| 2179 | static const/*expr*/ result_type max() { return _Max; } |
| 2180 | |
| 2181 | // constructors and seeding functions |
| 2182 | independent_bits_engine() {} |
| 2183 | // explicit independent_bits_engine(const _Engine& __e); |
| 2184 | // explicit independent_bits_engine(_Engine&& __e); |
| 2185 | explicit independent_bits_engine(result_type __sd) : __e_(__sd) {} |
| 2186 | template<class _Sseq> explicit independent_bits_engine(_Sseq& __q) |
| 2187 | : __e_(__q) {} |
| 2188 | void seed() {__e_.seed();} |
| 2189 | void seed(result_type __sd) {__e_.seed(__sd);} |
| 2190 | template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);} |
| 2191 | |
| 2192 | // generating functions |
| 2193 | result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} |
| 2194 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2195 | |
| 2196 | // property functions |
| 2197 | const _Engine& base() const {return __e_;} |
| 2198 | |
| 2199 | template<class _Eng, size_t _W, class _UI> |
| 2200 | friend |
| 2201 | bool |
| 2202 | operator==( |
| 2203 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 2204 | const independent_bits_engine<_Eng, _W, _UI>& __y); |
| 2205 | |
| 2206 | template<class _Eng, size_t _W, class _UI> |
| 2207 | friend |
| 2208 | bool |
| 2209 | operator!=( |
| 2210 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 2211 | const independent_bits_engine<_Eng, _W, _UI>& __y); |
| 2212 | |
| 2213 | template <class _CharT, class _Traits, |
| 2214 | class _Eng, size_t _W, class _UI> |
| 2215 | friend |
| 2216 | basic_ostream<_CharT, _Traits>& |
| 2217 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2218 | const independent_bits_engine<_Eng, _W, _UI>& __x); |
| 2219 | |
| 2220 | template <class _CharT, class _Traits, |
| 2221 | class _Eng, size_t _W, class _UI> |
| 2222 | friend |
| 2223 | basic_istream<_CharT, _Traits>& |
| 2224 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2225 | independent_bits_engine<_Eng, _W, _UI>& __x); |
| 2226 | |
| 2227 | private: |
| 2228 | result_type __eval(false_type); |
| 2229 | result_type __eval(true_type); |
| 2230 | |
| 2231 | template <size_t __count> |
| 2232 | static |
| 2233 | typename enable_if |
| 2234 | < |
| 2235 | __count < _Dt, |
| 2236 | result_type |
| 2237 | >::type |
| 2238 | __lshift(result_type __x) {return __x << __count;} |
| 2239 | |
| 2240 | template <size_t __count> |
| 2241 | static |
| 2242 | typename enable_if |
| 2243 | < |
| 2244 | (__count >= _Dt), |
| 2245 | result_type |
| 2246 | >::type |
| 2247 | __lshift(result_type __x) {return result_type(0);} |
| 2248 | }; |
| 2249 | |
| 2250 | template<class _Engine, size_t __w, class _UIntType> |
| 2251 | inline |
| 2252 | _UIntType |
| 2253 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) |
| 2254 | { |
| 2255 | return static_cast<result_type>(__e_() & __mask0); |
| 2256 | } |
| 2257 | |
| 2258 | template<class _Engine, size_t __w, class _UIntType> |
| 2259 | _UIntType |
| 2260 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) |
| 2261 | { |
| 2262 | result_type _S = 0; |
| 2263 | for (size_t __k = 0; __k < __n0; ++__k) |
| 2264 | { |
| 2265 | _Engine_result_type __u; |
| 2266 | do |
| 2267 | { |
| 2268 | __u = __e_() - _Engine::min(); |
| 2269 | } while (__u >= __y0); |
| 2270 | _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0)); |
| 2271 | } |
| 2272 | for (size_t __k = __n0; __k < __n; ++__k) |
| 2273 | { |
| 2274 | _Engine_result_type __u; |
| 2275 | do |
| 2276 | { |
| 2277 | __u = __e_() - _Engine::min(); |
| 2278 | } while (__u >= __y1); |
| 2279 | _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1)); |
| 2280 | } |
| 2281 | return _S; |
| 2282 | } |
| 2283 | |
| 2284 | template<class _Eng, size_t _W, class _UI> |
| 2285 | inline |
| 2286 | bool |
| 2287 | operator==( |
| 2288 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 2289 | const independent_bits_engine<_Eng, _W, _UI>& __y) |
| 2290 | { |
| 2291 | return __x.base() == __y.base(); |
| 2292 | } |
| 2293 | |
| 2294 | template<class _Eng, size_t _W, class _UI> |
| 2295 | inline |
| 2296 | bool |
| 2297 | operator!=( |
| 2298 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 2299 | const independent_bits_engine<_Eng, _W, _UI>& __y) |
| 2300 | { |
| 2301 | return !(__x == __y); |
| 2302 | } |
| 2303 | |
| 2304 | template <class _CharT, class _Traits, |
| 2305 | class _Eng, size_t _W, class _UI> |
| 2306 | basic_ostream<_CharT, _Traits>& |
| 2307 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2308 | const independent_bits_engine<_Eng, _W, _UI>& __x) |
| 2309 | { |
| 2310 | return __os << __x.base(); |
| 2311 | } |
| 2312 | |
| 2313 | template <class _CharT, class _Traits, |
| 2314 | class _Eng, size_t _W, class _UI> |
| 2315 | basic_istream<_CharT, _Traits>& |
| 2316 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2317 | independent_bits_engine<_Eng, _W, _UI>& __x) |
| 2318 | { |
| 2319 | _Eng __e; |
| 2320 | __is >> __e; |
| 2321 | if (!__is.fail()) |
| 2322 | __x.__e_ = __e; |
| 2323 | return __is; |
| 2324 | } |
| 2325 | |
| 2326 | // shuffle_order_engine |
| 2327 | |
| 2328 | template <uint64_t _Xp, uint64_t _Yp> |
| 2329 | struct __ugcd |
| 2330 | { |
| 2331 | static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value; |
| 2332 | }; |
| 2333 | |
| 2334 | template <uint64_t _Xp> |
| 2335 | struct __ugcd<_Xp, 0> |
| 2336 | { |
| 2337 | static const uint64_t value = _Xp; |
| 2338 | }; |
| 2339 | |
| 2340 | template <uint64_t _N, uint64_t _D> |
| 2341 | class __uratio |
| 2342 | { |
| 2343 | static_assert(_D != 0, "__uratio divide by 0"); |
| 2344 | static const uint64_t __gcd = __ugcd<_N, _D>::value; |
| 2345 | public: |
| 2346 | static const uint64_t num = _N / __gcd; |
| 2347 | static const uint64_t den = _D / __gcd; |
| 2348 | |
| 2349 | typedef __uratio<num, den> type; |
| 2350 | }; |
| 2351 | |
| 2352 | template<class _Engine, size_t __k> |
| 2353 | class shuffle_order_engine |
| 2354 | { |
| 2355 | static_assert(0 < __k, "shuffle_order_engine invalid parameters"); |
| 2356 | public: |
| 2357 | // types |
| 2358 | typedef typename _Engine::result_type result_type; |
| 2359 | |
| 2360 | private: |
| 2361 | _Engine __e_; |
| 2362 | result_type _V_[__k]; |
| 2363 | result_type _Y_; |
| 2364 | |
| 2365 | public: |
| 2366 | // engine characteristics |
| 2367 | static const/*expr*/ size_t table_size = __k; |
| 2368 | |
| 2369 | static const result_type _Min = _Engine::_Min; |
| 2370 | static const result_type _Max = _Engine::_Max; |
| 2371 | static_assert(_Min < _Max, "shuffle_order_engine invalid parameters"); |
| 2372 | static const/*expr*/ result_type min() { return _Min; } |
| 2373 | static const/*expr*/ result_type max() { return _Max; } |
| 2374 | |
| 2375 | static const unsigned long long _R = _Max - _Min + 1ull; |
| 2376 | |
| 2377 | // constructors and seeding functions |
| 2378 | shuffle_order_engine() {__init();} |
| 2379 | // explicit shuffle_order_engine(const _Engine& __e); |
| 2380 | // explicit shuffle_order_engine(_Engine&& e); |
| 2381 | explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();} |
| 2382 | template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q) |
| 2383 | : __e_(__q) {__init();} |
| 2384 | void seed() {__e_.seed(); __init();} |
| 2385 | void seed(result_type __sd) {__e_.seed(__sd); __init();} |
| 2386 | template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();} |
| 2387 | |
| 2388 | // generating functions |
| 2389 | result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} |
| 2390 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2391 | |
| 2392 | // property functions |
| 2393 | const _Engine& base() const {return __e_;} |
| 2394 | |
| 2395 | private: |
| 2396 | template<class _Eng, size_t _K> |
| 2397 | friend |
| 2398 | bool |
| 2399 | operator==( |
| 2400 | const shuffle_order_engine<_Eng, _K>& __x, |
| 2401 | const shuffle_order_engine<_Eng, _K>& __y); |
| 2402 | |
| 2403 | template<class _Eng, size_t _K> |
| 2404 | friend |
| 2405 | bool |
| 2406 | operator!=( |
| 2407 | const shuffle_order_engine<_Eng, _K>& __x, |
| 2408 | const shuffle_order_engine<_Eng, _K>& __y); |
| 2409 | |
| 2410 | template <class _CharT, class _Traits, |
| 2411 | class _Eng, size_t _K> |
| 2412 | friend |
| 2413 | basic_ostream<_CharT, _Traits>& |
| 2414 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2415 | const shuffle_order_engine<_Eng, _K>& __x); |
| 2416 | |
| 2417 | template <class _CharT, class _Traits, |
| 2418 | class _Eng, size_t _K> |
| 2419 | friend |
| 2420 | basic_istream<_CharT, _Traits>& |
| 2421 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2422 | shuffle_order_engine<_Eng, _K>& __x); |
| 2423 | |
| 2424 | void __init() |
| 2425 | { |
| 2426 | for (size_t __i = 0; __i < __k; ++__i) |
| 2427 | _V_[__i] = __e_(); |
| 2428 | _Y_ = __e_(); |
| 2429 | } |
| 2430 | |
| 2431 | result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());} |
| 2432 | result_type __eval(true_type) {return __eval(__uratio<__k, _R>());} |
| 2433 | |
| 2434 | result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());} |
| 2435 | result_type __eval2(true_type) {return __evalf<__k, 0>();} |
| 2436 | |
| 2437 | template <uint64_t _N, uint64_t _D> |
| 2438 | typename enable_if |
| 2439 | < |
| 2440 | (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)), |
| 2441 | result_type |
| 2442 | >::type |
| 2443 | __eval(__uratio<_N, _D>) |
| 2444 | {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();} |
| 2445 | |
| 2446 | template <uint64_t _N, uint64_t _D> |
| 2447 | typename enable_if |
| 2448 | < |
| 2449 | __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min), |
| 2450 | result_type |
| 2451 | >::type |
| 2452 | __eval(__uratio<_N, _D>) |
| 2453 | { |
| 2454 | const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min) |
| 2455 | / __uratio<_N, _D>::den); |
| 2456 | _Y_ = _V_[__j]; |
| 2457 | _V_[__j] = __e_(); |
| 2458 | return _Y_; |
| 2459 | } |
| 2460 | |
| 2461 | template <uint64_t __n, uint64_t __d> |
| 2462 | result_type __evalf() |
| 2463 | { |
| 2464 | const double _F = __d == 0 ? |
| 2465 | __n / (2. * 0x8000000000000000ull) : |
| 2466 | __n / (double)__d; |
| 2467 | const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min)); |
| 2468 | _Y_ = _V_[__j]; |
| 2469 | _V_[__j] = __e_(); |
| 2470 | return _Y_; |
| 2471 | } |
| 2472 | }; |
| 2473 | |
| 2474 | template<class _Eng, size_t _K> |
| 2475 | bool |
| 2476 | operator==( |
| 2477 | const shuffle_order_engine<_Eng, _K>& __x, |
| 2478 | const shuffle_order_engine<_Eng, _K>& __y) |
| 2479 | { |
| 2480 | return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) && |
| 2481 | __x.__e_ == __y.__e_; |
| 2482 | } |
| 2483 | |
| 2484 | template<class _Eng, size_t _K> |
| 2485 | inline |
| 2486 | bool |
| 2487 | operator!=( |
| 2488 | const shuffle_order_engine<_Eng, _K>& __x, |
| 2489 | const shuffle_order_engine<_Eng, _K>& __y) |
| 2490 | { |
| 2491 | return !(__x == __y); |
| 2492 | } |
| 2493 | |
| 2494 | template <class _CharT, class _Traits, |
| 2495 | class _Eng, size_t _K> |
| 2496 | basic_ostream<_CharT, _Traits>& |
| 2497 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2498 | const shuffle_order_engine<_Eng, _K>& __x) |
| 2499 | { |
| 2500 | __save_flags<_CharT, _Traits> _(__os); |
| 2501 | __os.flags(ios_base::dec | ios_base::left); |
| 2502 | _CharT __sp = __os.widen(' '); |
| 2503 | __os.fill(__sp); |
| 2504 | __os << __x.__e_ << __sp << __x._V_[0]; |
| 2505 | for (size_t __i = 1; __i < _K; ++__i) |
| 2506 | __os << __sp << __x._V_[__i]; |
| 2507 | return __os << __sp << __x._Y_; |
| 2508 | } |
| 2509 | |
| 2510 | template <class _CharT, class _Traits, |
| 2511 | class _Eng, size_t _K> |
| 2512 | basic_istream<_CharT, _Traits>& |
| 2513 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2514 | shuffle_order_engine<_Eng, _K>& __x) |
| 2515 | { |
| 2516 | typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type; |
| 2517 | __save_flags<_CharT, _Traits> _(__is); |
| 2518 | __is.flags(ios_base::dec | ios_base::skipws); |
| 2519 | _Eng __e; |
| 2520 | result_type _V[_K+1]; |
| 2521 | __is >> __e; |
| 2522 | for (size_t __i = 0; __i < _K+1; ++__i) |
| 2523 | __is >> _V[__i]; |
| 2524 | if (!__is.fail()) |
| 2525 | { |
| 2526 | __x.__e_ = __e; |
| 2527 | for (size_t __i = 0; __i < _K; ++__i) |
| 2528 | __x._V_[__i] = _V[__i]; |
| 2529 | __x._Y_ = _V[_K]; |
| 2530 | } |
| 2531 | return __is; |
| 2532 | } |
| 2533 | |
| 2534 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
| 2535 | |
| 2536 | // random_device |
| 2537 | |
| 2538 | class random_device |
| 2539 | { |
| 2540 | int __f_; |
| 2541 | public: |
| 2542 | // types |
| 2543 | typedef unsigned result_type; |
| 2544 | |
| 2545 | // generator characteristics |
| 2546 | static const result_type _Min = 0; |
| 2547 | static const result_type _Max = 0xFFFFFFFFu; |
| 2548 | |
| 2549 | static const/*expr*/ result_type min() { return _Min;} |
| 2550 | static const/*expr*/ result_type max() { return _Max;} |
| 2551 | |
| 2552 | // constructors |
| 2553 | explicit random_device(const string& __token = "/dev/urandom"); |
| 2554 | ~random_device(); |
| 2555 | |
| 2556 | // generating functions |
| 2557 | result_type operator()(); |
| 2558 | |
| 2559 | // property functions |
| 2560 | double entropy() const; |
| 2561 | |
| 2562 | private: |
| 2563 | // no copy functions |
| 2564 | random_device(const random_device&); // = delete; |
| 2565 | random_device& operator=(const random_device&); // = delete; |
| 2566 | }; |
| 2567 | |
| 2568 | // seed_seq |
| 2569 | |
| 2570 | class seed_seq |
| 2571 | { |
| 2572 | public: |
| 2573 | // types |
| 2574 | typedef uint32_t result_type; |
| 2575 | |
| 2576 | private: |
| 2577 | vector<result_type> __v_; |
| 2578 | |
| 2579 | template<class _InputIterator> |
| 2580 | void init(_InputIterator __first, _InputIterator __last); |
| 2581 | public: |
| 2582 | // constructors |
| 2583 | seed_seq() {} |
| 2584 | template<class _Tp> |
| 2585 | seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());} |
| 2586 | |
| 2587 | template<class _InputIterator> |
| 2588 | seed_seq(_InputIterator __first, _InputIterator __last) |
| 2589 | {init(__first, __last);} |
| 2590 | |
| 2591 | // generating functions |
| 2592 | template<class _RandomAccessIterator> |
| 2593 | void generate(_RandomAccessIterator __first, _RandomAccessIterator __last); |
| 2594 | |
| 2595 | // property functions |
| 2596 | size_t size() const {return __v_.size();} |
| 2597 | template<class _OutputIterator> |
| 2598 | void param(_OutputIterator __dest) const |
| 2599 | {_STD::copy(__v_.begin(), __v_.end(), __dest);} |
| 2600 | |
| 2601 | private: |
| 2602 | // no copy functions |
| 2603 | seed_seq(const seed_seq&); // = delete; |
| 2604 | void operator=(const seed_seq&); // = delete; |
| 2605 | |
| 2606 | static result_type _T(result_type __x) {return __x ^ (__x >> 27);} |
| 2607 | }; |
| 2608 | |
| 2609 | template<class _InputIterator> |
| 2610 | void |
| 2611 | seed_seq::init(_InputIterator __first, _InputIterator __last) |
| 2612 | { |
| 2613 | for (_InputIterator __s = __first; __s != __last; ++__s) |
| 2614 | __v_.push_back(*__s & 0xFFFFFFFF); |
| 2615 | } |
| 2616 | |
| 2617 | template<class _RandomAccessIterator> |
| 2618 | void |
| 2619 | seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last) |
| 2620 | { |
| 2621 | if (__first != __last) |
| 2622 | { |
| 2623 | _STD::fill(__first, __last, 0x8b8b8b8b); |
| 2624 | const size_t __n = static_cast<size_t>(__last - __first); |
| 2625 | const size_t __s = __v_.size(); |
| 2626 | const size_t __t = (__n >= 623) ? 11 |
| 2627 | : (__n >= 68) ? 7 |
| 2628 | : (__n >= 39) ? 5 |
| 2629 | : (__n >= 7) ? 3 |
| 2630 | : (__n - 1) / 2; |
| 2631 | const size_t __p = (__n - __t) / 2; |
| 2632 | const size_t __q = __p + __t; |
| 2633 | const size_t __m = _STD::max(__s + 1, __n); |
| 2634 | // __k = 0; |
| 2635 | { |
| 2636 | result_type __r = 1664525 * _T(__first[0] ^ __first[__p] |
| 2637 | ^ __first[__n - 1]); |
| 2638 | __first[__p] += __r; |
| 2639 | __r += __s; |
| 2640 | __first[__q] += __r; |
| 2641 | __first[0] = __r; |
| 2642 | } |
| 2643 | for (size_t __k = 1; __k <= __s; ++__k) |
| 2644 | { |
| 2645 | const size_t __kmodn = __k % __n; |
| 2646 | const size_t __kpmodn = (__k + __p) % __n; |
| 2647 | result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn] |
| 2648 | ^ __first[(__k - 1) % __n]); |
| 2649 | __first[__kpmodn] += __r; |
| 2650 | __r += __kmodn + __v_[__k-1]; |
| 2651 | __first[(__k + __q) % __n] += __r; |
| 2652 | __first[__kmodn] = __r; |
| 2653 | } |
| 2654 | for (size_t __k = __s + 1; __k < __m; ++__k) |
| 2655 | { |
| 2656 | const size_t __kmodn = __k % __n; |
| 2657 | const size_t __kpmodn = (__k + __p) % __n; |
| 2658 | result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn] |
| 2659 | ^ __first[(__k - 1) % __n]); |
| 2660 | __first[__kpmodn] += __r; |
| 2661 | __r += __kmodn; |
| 2662 | __first[(__k + __q) % __n] += __r; |
| 2663 | __first[__kmodn] = __r; |
| 2664 | } |
| 2665 | for (size_t __k = __m; __k < __m + __n; ++__k) |
| 2666 | { |
| 2667 | const size_t __kmodn = __k % __n; |
| 2668 | const size_t __kpmodn = (__k + __p) % __n; |
| 2669 | result_type __r = 1566083941 * _T(__first[__kmodn] + |
| 2670 | __first[__kpmodn] + |
| 2671 | __first[(__k - 1) % __n]); |
| 2672 | __first[__kpmodn] ^= __r; |
| 2673 | __r -= __kmodn; |
| 2674 | __first[(__k + __q) % __n] ^= __r; |
| 2675 | __first[__kmodn] = __r; |
| 2676 | } |
| 2677 | } |
| 2678 | } |
| 2679 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 2680 | // generate_canonical |
| 2681 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2682 | template<class _RealType, size_t __bits, class _URNG> |
| 2683 | _RealType |
| 2684 | generate_canonical(_URNG& __g) |
| 2685 | { |
| 2686 | const size_t _Dt = numeric_limits<_RealType>::digits; |
| 2687 | const size_t __b = _Dt < __bits ? _Dt : __bits; |
| 2688 | const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value; |
| 2689 | const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0); |
| 2690 | const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1); |
| 2691 | _RealType __base = _R; |
| 2692 | _RealType _S = __g() - _URNG::_Min; |
| 2693 | for (size_t __i = 1; __i < __k; ++__i, __base *= _R) |
| 2694 | _S += (__g() - _URNG::_Min) * __base; |
| 2695 | return _S / __base; |
| 2696 | } |
| 2697 | |
| 2698 | // __independent_bits_engine |
| 2699 | |
| 2700 | template<class _Engine, class _UIntType> |
| 2701 | class __independent_bits_engine |
| 2702 | { |
| 2703 | public: |
| 2704 | // types |
| 2705 | typedef _UIntType result_type; |
| 2706 | |
| 2707 | private: |
| 2708 | typedef typename _Engine::result_type _Engine_result_type; |
| 2709 | typedef typename conditional |
| 2710 | < |
| 2711 | sizeof(_Engine_result_type) <= sizeof(result_type), |
| 2712 | result_type, |
| 2713 | _Engine_result_type |
| 2714 | >::type _Working_result_type; |
| 2715 | |
| 2716 | _Engine& __e_; |
| 2717 | size_t __w_; |
| 2718 | size_t __w0_; |
| 2719 | size_t __n_; |
| 2720 | size_t __n0_; |
| 2721 | _Working_result_type __y0_; |
| 2722 | _Working_result_type __y1_; |
| 2723 | _Engine_result_type __mask0_; |
| 2724 | _Engine_result_type __mask1_; |
| 2725 | |
| 2726 | static const _Working_result_type _R = _Engine::_Max - _Engine::_Min |
| 2727 | + _Working_result_type(1); |
| 2728 | static const size_t __m = __log2<_Working_result_type, _R>::value; |
| 2729 | static const size_t _WDt = numeric_limits<_Working_result_type>::digits; |
| 2730 | static const size_t _EDt = numeric_limits<_Engine_result_type>::digits; |
| 2731 | |
| 2732 | public: |
| 2733 | // constructors and seeding functions |
| 2734 | __independent_bits_engine(_Engine& __e, size_t __w); |
| 2735 | |
| 2736 | // generating functions |
| 2737 | result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} |
| 2738 | |
| 2739 | private: |
| 2740 | result_type __eval(false_type); |
| 2741 | result_type __eval(true_type); |
| 2742 | }; |
| 2743 | |
| 2744 | template<class _Engine, class _UIntType> |
| 2745 | __independent_bits_engine<_Engine, _UIntType> |
| 2746 | ::__independent_bits_engine(_Engine& __e, size_t __w) |
| 2747 | : __e_(__e), |
| 2748 | __w_(__w) |
| 2749 | { |
| 2750 | __n_ = __w_ / __m + (__w_ % __m != 0); |
| 2751 | __w0_ = __w_ / __n_; |
| 2752 | if (_R == 0) |
| 2753 | __y0_ = _R; |
| 2754 | else if (__w0_ < _WDt) |
| 2755 | __y0_ = (_R >> __w0_) << __w0_; |
| 2756 | else |
| 2757 | __y0_ = 0; |
| 2758 | if (_R - __y0_ > __y0_ / __n_) |
| 2759 | { |
| 2760 | ++__n_; |
| 2761 | __w0_ = __w_ / __n_; |
| 2762 | if (__w0_ < _WDt) |
| 2763 | __y0_ = (_R >> __w0_) << __w0_; |
| 2764 | else |
| 2765 | __y0_ = 0; |
| 2766 | } |
| 2767 | __n0_ = __n_ - __w_ % __n_; |
| 2768 | if (__w0_ < _WDt - 1) |
| 2769 | __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1); |
| 2770 | else |
| 2771 | __y1_ = 0; |
| 2772 | __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) : |
| 2773 | _Engine_result_type(0); |
| 2774 | __mask1_ = __w0_ < _EDt - 1 ? |
| 2775 | _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) : |
| 2776 | _Engine_result_type(~0); |
| 2777 | } |
| 2778 | |
| 2779 | template<class _Engine, class _UIntType> |
| 2780 | inline |
| 2781 | _UIntType |
| 2782 | __independent_bits_engine<_Engine, _UIntType>::__eval(false_type) |
| 2783 | { |
| 2784 | return static_cast<result_type>(__e_() & __mask0_); |
| 2785 | } |
| 2786 | |
| 2787 | template<class _Engine, class _UIntType> |
| 2788 | _UIntType |
| 2789 | __independent_bits_engine<_Engine, _UIntType>::__eval(true_type) |
| 2790 | { |
| 2791 | result_type _S = 0; |
| 2792 | for (size_t __k = 0; __k < __n0_; ++__k) |
| 2793 | { |
| 2794 | _Engine_result_type __u; |
| 2795 | do |
| 2796 | { |
| 2797 | __u = __e_() - _Engine::min(); |
| 2798 | } while (__u >= __y0_); |
| 2799 | if (__w0_ < _EDt) |
| 2800 | _S <<= __w0_; |
| 2801 | else |
| 2802 | _S = 0; |
| 2803 | _S += __u & __mask0_; |
| 2804 | } |
| 2805 | for (size_t __k = __n0_; __k < __n_; ++__k) |
| 2806 | { |
| 2807 | _Engine_result_type __u; |
| 2808 | do |
| 2809 | { |
| 2810 | __u = __e_() - _Engine::min(); |
| 2811 | } while (__u >= __y1_); |
| 2812 | if (__w0_ < _EDt - 1) |
| 2813 | _S <<= __w0_ + 1; |
| 2814 | else |
| 2815 | _S = 0; |
| 2816 | _S += __u & __mask1_; |
| 2817 | } |
| 2818 | return _S; |
| 2819 | } |
| 2820 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 2821 | // uniform_int_distribution |
| 2822 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2823 | template<class _IntType = int> |
| 2824 | class uniform_int_distribution |
| 2825 | { |
| 2826 | public: |
| 2827 | // types |
| 2828 | typedef _IntType result_type; |
| 2829 | |
| 2830 | class param_type |
| 2831 | { |
| 2832 | result_type __a_; |
| 2833 | result_type __b_; |
| 2834 | public: |
| 2835 | typedef uniform_int_distribution distribution_type; |
| 2836 | |
| 2837 | explicit param_type(result_type __a = 0, |
| 2838 | result_type __b = numeric_limits<result_type>::max()) |
| 2839 | : __a_(__a), __b_(__b) {} |
| 2840 | |
| 2841 | result_type a() const {return __a_;} |
| 2842 | result_type b() const {return __b_;} |
| 2843 | |
| 2844 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 2845 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
| 2846 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 2847 | {return !(__x == __y);} |
| 2848 | }; |
| 2849 | |
| 2850 | private: |
| 2851 | param_type __p_; |
| 2852 | |
| 2853 | public: |
| 2854 | // constructors and reset functions |
| 2855 | explicit uniform_int_distribution(result_type __a = 0, |
| 2856 | result_type __b = numeric_limits<result_type>::max()) |
| 2857 | : __p_(param_type(__a, __b)) {} |
| 2858 | explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {} |
| 2859 | void reset() {} |
| 2860 | |
| 2861 | // generating functions |
| 2862 | template<class _URNG> result_type operator()(_URNG& __g) |
| 2863 | {return (*this)(__g, __p_);} |
| 2864 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 2865 | |
| 2866 | // property functions |
| 2867 | result_type a() const {return __p_.a();} |
| 2868 | result_type b() const {return __p_.b();} |
| 2869 | |
| 2870 | param_type param() const {return __p_;} |
| 2871 | void param(const param_type& __p) {__p_ = __p;} |
| 2872 | |
| 2873 | result_type min() const {return a();} |
| 2874 | result_type max() const {return b();} |
| 2875 | |
| 2876 | friend bool operator==(const uniform_int_distribution& __x, |
| 2877 | const uniform_int_distribution& __y) |
| 2878 | {return __x.__p_ == __y.__p_;} |
| 2879 | friend bool operator!=(const uniform_int_distribution& __x, |
| 2880 | const uniform_int_distribution& __y) |
| 2881 | {return !(__x == __y);} |
| 2882 | }; |
| 2883 | |
| 2884 | template<class _IntType> |
| 2885 | template<class _URNG> |
| 2886 | typename uniform_int_distribution<_IntType>::result_type |
| 2887 | uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) |
| 2888 | { |
| 2889 | typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t), |
| 2890 | uint32_t, uint64_t>::type _UIntType; |
| 2891 | const _UIntType _R = __p.b() - __p.a() + _UIntType(1); |
| 2892 | if (_R == 1) |
| 2893 | return __p.a(); |
| 2894 | const size_t _Dt = numeric_limits<_UIntType>::digits; |
| 2895 | typedef __independent_bits_engine<_URNG, _UIntType> _Eng; |
| 2896 | if (_R == 0) |
| 2897 | return static_cast<result_type>(_Eng(__g, _Dt)()); |
| 2898 | size_t __w = _Dt - __clz(_R) - 1; |
| 2899 | if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0) |
| 2900 | ++__w; |
| 2901 | _Eng __e(__g, __w); |
| 2902 | _UIntType __u; |
| 2903 | do |
| 2904 | { |
| 2905 | __u = __e(); |
| 2906 | } while (__u >= _R); |
| 2907 | return static_cast<result_type>(__u + __p.a()); |
| 2908 | } |
| 2909 | |
| 2910 | template <class _CharT, class _Traits, class _IT> |
| 2911 | basic_ostream<_CharT, _Traits>& |
| 2912 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2913 | const uniform_int_distribution<_IT>& __x) |
| 2914 | { |
| 2915 | __save_flags<_CharT, _Traits> _(__os); |
| 2916 | __os.flags(ios_base::dec | ios_base::left); |
| 2917 | _CharT __sp = __os.widen(' '); |
| 2918 | __os.fill(__sp); |
| 2919 | return __os << __x.a() << __sp << __x.b(); |
| 2920 | } |
| 2921 | |
| 2922 | template <class _CharT, class _Traits, class _IT> |
| 2923 | basic_istream<_CharT, _Traits>& |
| 2924 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2925 | uniform_int_distribution<_IT>& __x) |
| 2926 | { |
| 2927 | typedef uniform_int_distribution<_IT> _Eng; |
| 2928 | typedef typename _Eng::result_type result_type; |
| 2929 | typedef typename _Eng::param_type param_type; |
| 2930 | __save_flags<_CharT, _Traits> _(__is); |
| 2931 | __is.flags(ios_base::dec | ios_base::skipws); |
| 2932 | result_type __a; |
| 2933 | result_type __b; |
| 2934 | __is >> __a >> __b; |
| 2935 | if (!__is.fail()) |
| 2936 | __x.param(param_type(__a, __b)); |
| 2937 | return __is; |
| 2938 | } |
| 2939 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 2940 | // uniform_real_distribution |
| 2941 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2942 | template<class _RealType = double> |
| 2943 | class uniform_real_distribution |
| 2944 | { |
| 2945 | public: |
| 2946 | // types |
| 2947 | typedef _RealType result_type; |
| 2948 | |
| 2949 | class param_type |
| 2950 | { |
| 2951 | result_type __a_; |
| 2952 | result_type __b_; |
| 2953 | public: |
| 2954 | typedef uniform_real_distribution distribution_type; |
| 2955 | |
| 2956 | explicit param_type(result_type __a = 0, |
| 2957 | result_type __b = 1) |
| 2958 | : __a_(__a), __b_(__b) {} |
| 2959 | |
| 2960 | result_type a() const {return __a_;} |
| 2961 | result_type b() const {return __b_;} |
| 2962 | |
| 2963 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 2964 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
| 2965 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 2966 | {return !(__x == __y);} |
| 2967 | }; |
| 2968 | |
| 2969 | private: |
| 2970 | param_type __p_; |
| 2971 | |
| 2972 | public: |
| 2973 | // constructors and reset functions |
| 2974 | explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1) |
| 2975 | : __p_(param_type(__a, __b)) {} |
| 2976 | explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {} |
| 2977 | void reset() {} |
| 2978 | |
| 2979 | // generating functions |
| 2980 | template<class _URNG> result_type operator()(_URNG& __g) |
| 2981 | {return (*this)(__g, __p_);} |
| 2982 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 2983 | |
| 2984 | // property functions |
| 2985 | result_type a() const {return __p_.a();} |
| 2986 | result_type b() const {return __p_.b();} |
| 2987 | |
| 2988 | param_type param() const {return __p_;} |
| 2989 | void param(const param_type& __p) {__p_ = __p;} |
| 2990 | |
| 2991 | result_type min() const {return a();} |
| 2992 | result_type max() const {return b();} |
| 2993 | |
| 2994 | friend bool operator==(const uniform_real_distribution& __x, |
| 2995 | const uniform_real_distribution& __y) |
| 2996 | {return __x.__p_ == __y.__p_;} |
| 2997 | friend bool operator!=(const uniform_real_distribution& __x, |
| 2998 | const uniform_real_distribution& __y) |
| 2999 | {return !(__x == __y);} |
| 3000 | }; |
| 3001 | |
| 3002 | template<class _RealType> |
| 3003 | template<class _URNG> |
| 3004 | inline |
| 3005 | typename uniform_real_distribution<_RealType>::result_type |
| 3006 | uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3007 | { |
| 3008 | return (__p.b() - __p.a()) |
| 3009 | * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) |
| 3010 | + __p.a(); |
| 3011 | } |
| 3012 | |
| 3013 | template <class _CharT, class _Traits, class _RT> |
| 3014 | basic_ostream<_CharT, _Traits>& |
| 3015 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3016 | const uniform_real_distribution<_RT>& __x) |
| 3017 | { |
| 3018 | __save_flags<_CharT, _Traits> _(__os); |
| 3019 | __os.flags(ios_base::dec | ios_base::left); |
| 3020 | _CharT __sp = __os.widen(' '); |
| 3021 | __os.fill(__sp); |
| 3022 | return __os << __x.a() << __sp << __x.b(); |
| 3023 | } |
| 3024 | |
| 3025 | template <class _CharT, class _Traits, class _RT> |
| 3026 | basic_istream<_CharT, _Traits>& |
| 3027 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3028 | uniform_real_distribution<_RT>& __x) |
| 3029 | { |
| 3030 | typedef uniform_real_distribution<_RT> _Eng; |
| 3031 | typedef typename _Eng::result_type result_type; |
| 3032 | typedef typename _Eng::param_type param_type; |
| 3033 | __save_flags<_CharT, _Traits> _(__is); |
| 3034 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3035 | result_type __a; |
| 3036 | result_type __b; |
| 3037 | __is >> __a >> __b; |
| 3038 | if (!__is.fail()) |
| 3039 | __x.param(param_type(__a, __b)); |
| 3040 | return __is; |
| 3041 | } |
| 3042 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3043 | // bernoulli_distribution |
| 3044 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3045 | class bernoulli_distribution |
| 3046 | { |
| 3047 | public: |
| 3048 | // types |
| 3049 | typedef bool result_type; |
| 3050 | |
| 3051 | class param_type |
| 3052 | { |
| 3053 | double __p_; |
| 3054 | public: |
| 3055 | typedef bernoulli_distribution distribution_type; |
| 3056 | |
| 3057 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 3058 | |
| 3059 | double p() const {return __p_;} |
| 3060 | |
| 3061 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3062 | {return __x.__p_ == __y.__p_;} |
| 3063 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3064 | {return !(__x == __y);} |
| 3065 | }; |
| 3066 | |
| 3067 | private: |
| 3068 | param_type __p_; |
| 3069 | |
| 3070 | public: |
| 3071 | // constructors and reset functions |
| 3072 | explicit bernoulli_distribution(double __p = 0.5) |
| 3073 | : __p_(param_type(__p)) {} |
| 3074 | explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {} |
| 3075 | void reset() {} |
| 3076 | |
| 3077 | // generating functions |
| 3078 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3079 | {return (*this)(__g, __p_);} |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3080 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3081 | |
| 3082 | // property functions |
| 3083 | double p() const {return __p_.p();} |
| 3084 | |
| 3085 | param_type param() const {return __p_;} |
| 3086 | void param(const param_type& __p) {__p_ = __p;} |
| 3087 | |
| 3088 | result_type min() const {return false;} |
| 3089 | result_type max() const {return true;} |
| 3090 | |
| 3091 | friend bool operator==(const bernoulli_distribution& __x, |
| 3092 | const bernoulli_distribution& __y) |
| 3093 | {return __x.__p_ == __y.__p_;} |
| 3094 | friend bool operator!=(const bernoulli_distribution& __x, |
| 3095 | const bernoulli_distribution& __y) |
| 3096 | {return !(__x == __y);} |
| 3097 | }; |
| 3098 | |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3099 | template<class _URNG> |
| 3100 | inline |
| 3101 | bernoulli_distribution::result_type |
| 3102 | bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) |
| 3103 | { |
| 3104 | return (__g() - __g.min()) < __p.p() * (__g.max() - __g.min() + 1.); |
| 3105 | } |
| 3106 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3107 | template <class _CharT, class _Traits> |
| 3108 | basic_ostream<_CharT, _Traits>& |
| 3109 | operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) |
| 3110 | { |
| 3111 | __save_flags<_CharT, _Traits> _(__os); |
| 3112 | __os.flags(ios_base::dec | ios_base::left); |
| 3113 | _CharT __sp = __os.widen(' '); |
| 3114 | __os.fill(__sp); |
| 3115 | return __os << __x.p(); |
| 3116 | } |
| 3117 | |
| 3118 | template <class _CharT, class _Traits> |
| 3119 | basic_istream<_CharT, _Traits>& |
| 3120 | operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) |
| 3121 | { |
| 3122 | typedef bernoulli_distribution _Eng; |
| 3123 | typedef typename _Eng::param_type param_type; |
| 3124 | __save_flags<_CharT, _Traits> _(__is); |
| 3125 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3126 | double __p; |
| 3127 | __is >> __p; |
| 3128 | if (!__is.fail()) |
| 3129 | __x.param(param_type(__p)); |
| 3130 | return __is; |
| 3131 | } |
| 3132 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3133 | // binomial_distribution |
| 3134 | |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3135 | template<class _IntType = int> |
| 3136 | class binomial_distribution |
| 3137 | { |
| 3138 | public: |
| 3139 | // types |
| 3140 | typedef _IntType result_type; |
| 3141 | |
| 3142 | class param_type |
| 3143 | { |
| 3144 | result_type __t_; |
| 3145 | double __p_; |
| 3146 | public: |
| 3147 | typedef binomial_distribution distribution_type; |
| 3148 | |
| 3149 | explicit param_type(result_type __t = 1, double __p = 0.5) |
| 3150 | : __t_(__t), __p_(__p) {} |
| 3151 | |
| 3152 | result_type t() const {return __t_;} |
| 3153 | double p() const {return __p_;} |
| 3154 | |
| 3155 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3156 | {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;} |
| 3157 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3158 | {return !(__x == __y);} |
| 3159 | }; |
| 3160 | |
| 3161 | private: |
| 3162 | param_type __p_; |
| 3163 | |
| 3164 | public: |
| 3165 | // constructors and reset functions |
| 3166 | explicit binomial_distribution(result_type __t = 1, double __p = 0.5) |
| 3167 | : __p_(param_type(__t, __p)) {} |
| 3168 | explicit binomial_distribution(const param_type& __p) : __p_(__p) {} |
| 3169 | void reset() {} |
| 3170 | |
| 3171 | // generating functions |
| 3172 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3173 | {return (*this)(__g, __p_);} |
| 3174 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3175 | |
| 3176 | // property functions |
| 3177 | result_type t() const {return __p_.t();} |
| 3178 | double p() const {return __p_.p();} |
| 3179 | |
| 3180 | param_type param() const {return __p_;} |
| 3181 | void param(const param_type& __p) {__p_ = __p;} |
| 3182 | |
| 3183 | result_type min() const {return 0;} |
| 3184 | result_type max() const {return t();} |
| 3185 | |
| 3186 | friend bool operator==(const binomial_distribution& __x, |
| 3187 | const binomial_distribution& __y) |
| 3188 | {return __x.__p_ == __y.__p_;} |
| 3189 | friend bool operator!=(const binomial_distribution& __x, |
| 3190 | const binomial_distribution& __y) |
| 3191 | {return !(__x == __y);} |
| 3192 | }; |
| 3193 | |
| 3194 | template<class _IntType> |
| 3195 | template<class _URNG> |
| 3196 | _IntType |
| 3197 | binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) |
| 3198 | { |
| 3199 | bernoulli_distribution __bd(__p.p()); |
| 3200 | _IntType __r = 0; |
| 3201 | _IntType __t = __p.t(); |
| 3202 | for (_IntType __i = 0; __i < __t; ++__i) |
| 3203 | __r += __bd(__g); |
| 3204 | return __r; |
| 3205 | } |
| 3206 | |
| 3207 | template <class _CharT, class _Traits, class _IntType> |
| 3208 | basic_ostream<_CharT, _Traits>& |
| 3209 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3210 | const binomial_distribution<_IntType>& __x) |
| 3211 | { |
| 3212 | __save_flags<_CharT, _Traits> _(__os); |
| 3213 | __os.flags(ios_base::dec | ios_base::left); |
| 3214 | _CharT __sp = __os.widen(' '); |
| 3215 | __os.fill(__sp); |
| 3216 | return __os << __x.t() << __sp << __x.p(); |
| 3217 | } |
| 3218 | |
| 3219 | template <class _CharT, class _Traits, class _IntType> |
| 3220 | basic_istream<_CharT, _Traits>& |
| 3221 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3222 | binomial_distribution<_IntType>& __x) |
| 3223 | { |
| 3224 | typedef binomial_distribution<_IntType> _Eng; |
| 3225 | typedef typename _Eng::result_type result_type; |
| 3226 | typedef typename _Eng::param_type param_type; |
| 3227 | __save_flags<_CharT, _Traits> _(__is); |
| 3228 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3229 | result_type __t; |
| 3230 | double __p; |
| 3231 | __is >> __t >> __p; |
| 3232 | if (!__is.fail()) |
| 3233 | __x.param(param_type(__t, __p)); |
| 3234 | return __is; |
| 3235 | } |
| 3236 | |
Howard Hinnant | 4ff556c | 2010-05-14 21:38:54 +0000 | [diff] [blame^] | 3237 | // poisson_distribution |
| 3238 | |
| 3239 | template<class _IntType = int> |
| 3240 | class poisson_distribution |
| 3241 | { |
| 3242 | public: |
| 3243 | // types |
| 3244 | typedef _IntType result_type; |
| 3245 | |
| 3246 | class param_type |
| 3247 | { |
| 3248 | double __mean_; |
| 3249 | double __sq_; |
| 3250 | double __alxm_; |
| 3251 | double __g_; |
| 3252 | public: |
| 3253 | typedef poisson_distribution distribution_type; |
| 3254 | |
| 3255 | explicit param_type(double __mean = 1.0); |
| 3256 | |
| 3257 | double mean() const {return __mean_;} |
| 3258 | |
| 3259 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3260 | {return __x.__mean_ == __y.__mean_;} |
| 3261 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3262 | {return !(__x == __y);} |
| 3263 | |
| 3264 | friend class poisson_distribution; |
| 3265 | }; |
| 3266 | |
| 3267 | private: |
| 3268 | param_type __p_; |
| 3269 | |
| 3270 | public: |
| 3271 | // constructors and reset functions |
| 3272 | explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {} |
| 3273 | explicit poisson_distribution(const param_type& __p) : __p_(__p) {} |
| 3274 | void reset() {} |
| 3275 | |
| 3276 | // generating functions |
| 3277 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3278 | {return (*this)(__g, __p_);} |
| 3279 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3280 | |
| 3281 | // property functions |
| 3282 | double mean() const {return __p_.mean();} |
| 3283 | |
| 3284 | param_type param() const {return __p_;} |
| 3285 | void param(const param_type& __p) {__p_ = __p;} |
| 3286 | |
| 3287 | result_type min() const {return 0;} |
| 3288 | result_type max() const {return numeric_limits<result_type>::max();} |
| 3289 | |
| 3290 | friend bool operator==(const poisson_distribution& __x, |
| 3291 | const poisson_distribution& __y) |
| 3292 | {return __x.__p_ == __y.__p_;} |
| 3293 | friend bool operator!=(const poisson_distribution& __x, |
| 3294 | const poisson_distribution& __y) |
| 3295 | {return !(__x == __y);} |
| 3296 | }; |
| 3297 | |
| 3298 | template<class _IntType> |
| 3299 | poisson_distribution<_IntType>::param_type::param_type(double __mean) |
| 3300 | : __mean_(__mean) |
| 3301 | { |
| 3302 | if (__mean_ < 12.0) |
| 3303 | { |
| 3304 | __g_ = _STD::exp(-__mean_); |
| 3305 | __alxm_ = 0; |
| 3306 | __sq_ = 0; |
| 3307 | } |
| 3308 | else |
| 3309 | { |
| 3310 | __sq_ = _STD::sqrt(2.0 * __mean_); |
| 3311 | __alxm_ = _STD::log(__mean_); |
| 3312 | __g_ = __mean_ * __alxm_ - _STD::lgamma(__mean_ + 1); |
| 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | template <class _IntType> |
| 3317 | template<class _URNG> |
| 3318 | _IntType |
| 3319 | poisson_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) |
| 3320 | { |
| 3321 | result_type __x; |
| 3322 | uniform_real_distribution<double> __gen; |
| 3323 | if (__p.__mean_ < 12.0) |
| 3324 | { |
| 3325 | __x = result_type(~0); |
| 3326 | double __t = 1; |
| 3327 | do |
| 3328 | { |
| 3329 | ++__x; |
| 3330 | __t *= __gen(__g); |
| 3331 | } while (__t > __p.__g_); |
| 3332 | } |
| 3333 | else |
| 3334 | { |
| 3335 | double __t; |
| 3336 | const double __pi = 3.14159265358979323846264338328; |
| 3337 | do |
| 3338 | { |
| 3339 | double _X; |
| 3340 | double __y; |
| 3341 | do |
| 3342 | { |
| 3343 | __y = _STD::tan(__pi * __gen(__g)); |
| 3344 | _X = __p.__sq_ * __y + __p.__mean_; |
| 3345 | } while (_X < 0); |
| 3346 | __x = static_cast<result_type>(_X); |
| 3347 | __t = 0.9 * (1 + __y * __y) * _STD::exp(__x * __p.__alxm_ - |
| 3348 | _STD::lgamma(__x + 1.0) - __p.__g_); |
| 3349 | } while (__gen(__g) > __t); |
| 3350 | } |
| 3351 | return __x; |
| 3352 | } |
| 3353 | |
| 3354 | template <class _CharT, class _Traits, class _IntType> |
| 3355 | basic_ostream<_CharT, _Traits>& |
| 3356 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3357 | const poisson_distribution<_IntType>& __x) |
| 3358 | { |
| 3359 | __save_flags<_CharT, _Traits> _(__os); |
| 3360 | __os.flags(ios_base::dec | ios_base::left); |
| 3361 | return __os << __x.mean(); |
| 3362 | } |
| 3363 | |
| 3364 | template <class _CharT, class _Traits, class _IntType> |
| 3365 | basic_istream<_CharT, _Traits>& |
| 3366 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3367 | poisson_distribution<_IntType>& __x) |
| 3368 | { |
| 3369 | typedef poisson_distribution<_IntType> _Eng; |
| 3370 | typedef typename _Eng::param_type param_type; |
| 3371 | __save_flags<_CharT, _Traits> _(__is); |
| 3372 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3373 | double __mean; |
| 3374 | __is >> __mean; |
| 3375 | if (!__is.fail()) |
| 3376 | __x.param(param_type(__mean)); |
| 3377 | return __is; |
| 3378 | } |
| 3379 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3380 | // exponential_distribution |
| 3381 | |
| 3382 | template<class _RealType = double> |
| 3383 | class exponential_distribution |
| 3384 | { |
| 3385 | public: |
| 3386 | // types |
| 3387 | typedef _RealType result_type; |
| 3388 | |
| 3389 | class param_type |
| 3390 | { |
| 3391 | result_type __lambda_; |
| 3392 | public: |
| 3393 | typedef exponential_distribution distribution_type; |
| 3394 | |
| 3395 | explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {} |
| 3396 | |
| 3397 | result_type lambda() const {return __lambda_;} |
| 3398 | |
| 3399 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3400 | {return __x.__lambda_ == __y.__lambda_;} |
| 3401 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3402 | {return !(__x == __y);} |
| 3403 | }; |
| 3404 | |
| 3405 | private: |
| 3406 | param_type __p_; |
| 3407 | |
| 3408 | public: |
| 3409 | // constructors and reset functions |
| 3410 | explicit exponential_distribution(result_type __lambda = 1) |
| 3411 | : __p_(param_type(__lambda)) {} |
| 3412 | explicit exponential_distribution(const param_type& __p) : __p_(__p) {} |
| 3413 | void reset() {} |
| 3414 | |
| 3415 | // generating functions |
| 3416 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3417 | {return (*this)(__g, __p_);} |
| 3418 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3419 | |
| 3420 | // property functions |
| 3421 | result_type lambda() const {return __p_.lambda();} |
| 3422 | |
| 3423 | param_type param() const {return __p_;} |
| 3424 | void param(const param_type& __p) {__p_ = __p;} |
| 3425 | |
| 3426 | result_type min() const {return 0;} |
| 3427 | result_type max() const |
| 3428 | {return -std::log(1-std::nextafter(result_type(1), result_type(-1))) / |
| 3429 | __p_.lambda();} |
| 3430 | |
| 3431 | friend bool operator==(const exponential_distribution& __x, |
| 3432 | const exponential_distribution& __y) |
| 3433 | {return __x.__p_ == __y.__p_;} |
| 3434 | friend bool operator!=(const exponential_distribution& __x, |
| 3435 | const exponential_distribution& __y) |
| 3436 | {return !(__x == __y);} |
| 3437 | }; |
| 3438 | |
| 3439 | template <class _RealType> |
| 3440 | template<class _URNG> |
| 3441 | _RealType |
| 3442 | exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3443 | { |
| 3444 | return -_STD::log |
| 3445 | ( |
| 3446 | result_type(1) - |
| 3447 | _STD::generate_canonical<result_type, |
| 3448 | numeric_limits<result_type>::digits>(__g) |
| 3449 | ) |
| 3450 | / __p.lambda(); |
| 3451 | } |
| 3452 | |
| 3453 | template <class _CharT, class _Traits, class _RealType> |
| 3454 | basic_ostream<_CharT, _Traits>& |
| 3455 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3456 | const exponential_distribution<_RealType>& __x) |
| 3457 | { |
| 3458 | __save_flags<_CharT, _Traits> _(__os); |
| 3459 | __os.flags(ios_base::dec | ios_base::left); |
| 3460 | return __os << __x.lambda(); |
| 3461 | } |
| 3462 | |
| 3463 | template <class _CharT, class _Traits, class _RealType> |
| 3464 | basic_istream<_CharT, _Traits>& |
| 3465 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3466 | exponential_distribution<_RealType>& __x) |
| 3467 | { |
| 3468 | typedef exponential_distribution<_RealType> _Eng; |
| 3469 | typedef typename _Eng::result_type result_type; |
| 3470 | typedef typename _Eng::param_type param_type; |
| 3471 | __save_flags<_CharT, _Traits> _(__is); |
| 3472 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3473 | result_type __lambda; |
| 3474 | __is >> __lambda; |
| 3475 | if (!__is.fail()) |
| 3476 | __x.param(param_type(__lambda)); |
| 3477 | return __is; |
| 3478 | } |
| 3479 | |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3480 | // gamma_distribution |
| 3481 | |
| 3482 | template<class _RealType = double> |
| 3483 | class gamma_distribution |
| 3484 | { |
| 3485 | public: |
| 3486 | // types |
| 3487 | typedef _RealType result_type; |
| 3488 | |
| 3489 | class param_type |
| 3490 | { |
| 3491 | result_type __alpha_; |
| 3492 | result_type __beta_; |
| 3493 | public: |
| 3494 | typedef gamma_distribution distribution_type; |
| 3495 | |
| 3496 | explicit param_type(result_type __alpha = 1, result_type __beta = 1) |
| 3497 | : __alpha_(__alpha), __beta_(__beta) {} |
| 3498 | |
| 3499 | result_type alpha() const {return __alpha_;} |
| 3500 | result_type beta() const {return __beta_;} |
| 3501 | |
| 3502 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3503 | {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;} |
| 3504 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3505 | {return !(__x == __y);} |
| 3506 | }; |
| 3507 | |
| 3508 | private: |
| 3509 | param_type __p_; |
| 3510 | |
| 3511 | public: |
| 3512 | // constructors and reset functions |
| 3513 | explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1) |
| 3514 | : __p_(param_type(__alpha, __beta)) {} |
| 3515 | explicit gamma_distribution(const param_type& __p) |
| 3516 | : __p_(__p) {} |
| 3517 | void reset() {} |
| 3518 | |
| 3519 | // generating functions |
| 3520 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3521 | {return (*this)(__g, __p_);} |
| 3522 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3523 | |
| 3524 | // property functions |
| 3525 | result_type alpha() const {return __p_.alpha();} |
| 3526 | result_type beta() const {return __p_.beta();} |
| 3527 | |
| 3528 | param_type param() const {return __p_;} |
| 3529 | void param(const param_type& __p) {__p_ = __p;} |
| 3530 | |
| 3531 | result_type min() const {return 0;} |
| 3532 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 3533 | |
| 3534 | friend bool operator==(const gamma_distribution& __x, |
| 3535 | const gamma_distribution& __y) |
| 3536 | {return __x.__p_ == __y.__p_;} |
| 3537 | friend bool operator!=(const gamma_distribution& __x, |
| 3538 | const gamma_distribution& __y) |
| 3539 | {return !(__x == __y);} |
| 3540 | }; |
| 3541 | |
| 3542 | template <class _RealType> |
| 3543 | template<class _URNG> |
| 3544 | _RealType |
| 3545 | gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3546 | { |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 3547 | result_type __a = __p.alpha(); |
| 3548 | uniform_real_distribution<result_type> __gen(0, 1); |
| 3549 | exponential_distribution<result_type> __egen; |
| 3550 | result_type __x; |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3551 | if (__a == 1) |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 3552 | __x = __egen(__g); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3553 | else if (__a > 1) |
| 3554 | { |
| 3555 | const result_type __b = __a - 1; |
| 3556 | const result_type __c = 3 * __a - result_type(0.75); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3557 | while (true) |
| 3558 | { |
| 3559 | const result_type __u = __gen(__g); |
| 3560 | const result_type __v = __gen(__g); |
| 3561 | const result_type __w = __u * (1 - __u); |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 3562 | if (__w != 0) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3563 | { |
| 3564 | const result_type __y = _STD::sqrt(__c / __w) * |
| 3565 | (__u - result_type(0.5)); |
| 3566 | __x = __b + __y; |
| 3567 | if (__x >= 0) |
| 3568 | { |
| 3569 | const result_type __z = 64 * __w * __w * __w * __v * __v; |
| 3570 | if (__z <= 1 - 2 * __y * __y / __x) |
| 3571 | break; |
| 3572 | if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y)) |
| 3573 | break; |
| 3574 | } |
| 3575 | } |
| 3576 | } |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3577 | } |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 3578 | else // __a < 1 |
| 3579 | { |
| 3580 | while (true) |
| 3581 | { |
| 3582 | const result_type __u = __gen(__g); |
| 3583 | const result_type __es = __egen(__g); |
| 3584 | if (__u <= 1 - __a) |
| 3585 | { |
| 3586 | __x = _STD::pow(__u, 1 / __a); |
| 3587 | if (__x <= __es) |
| 3588 | break; |
| 3589 | } |
| 3590 | else |
| 3591 | { |
| 3592 | const result_type __e = -_STD::log((1-__u)/__a); |
| 3593 | __x = _STD::pow(1 - __a + __a * __e, 1 / __a); |
| 3594 | if (__x <= __e + __es) |
| 3595 | break; |
| 3596 | } |
| 3597 | } |
| 3598 | } |
| 3599 | return __x * __p.beta(); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | template <class _CharT, class _Traits, class _RT> |
| 3603 | basic_ostream<_CharT, _Traits>& |
| 3604 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3605 | const gamma_distribution<_RT>& __x) |
| 3606 | { |
| 3607 | __save_flags<_CharT, _Traits> _(__os); |
| 3608 | __os.flags(ios_base::dec | ios_base::left); |
| 3609 | _CharT __sp = __os.widen(' '); |
| 3610 | __os.fill(__sp); |
| 3611 | __os << __x.alpha() << __sp << __x.beta(); |
| 3612 | return __os; |
| 3613 | } |
| 3614 | |
| 3615 | template <class _CharT, class _Traits, class _RT> |
| 3616 | basic_istream<_CharT, _Traits>& |
| 3617 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3618 | gamma_distribution<_RT>& __x) |
| 3619 | { |
| 3620 | typedef gamma_distribution<_RT> _Eng; |
| 3621 | typedef typename _Eng::result_type result_type; |
| 3622 | typedef typename _Eng::param_type param_type; |
| 3623 | __save_flags<_CharT, _Traits> _(__is); |
| 3624 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3625 | result_type __alpha; |
| 3626 | result_type __beta; |
| 3627 | __is >> __alpha >> __beta; |
| 3628 | if (!__is.fail()) |
| 3629 | __x.param(param_type(__alpha, __beta)); |
| 3630 | return __is; |
| 3631 | } |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 3632 | // normal_distribution |
| 3633 | |
| 3634 | template<class _RealType = double> |
| 3635 | class normal_distribution |
| 3636 | { |
| 3637 | public: |
| 3638 | // types |
| 3639 | typedef _RealType result_type; |
| 3640 | |
| 3641 | class param_type |
| 3642 | { |
| 3643 | result_type __mean_; |
| 3644 | result_type __stddev_; |
| 3645 | public: |
| 3646 | typedef normal_distribution distribution_type; |
| 3647 | |
| 3648 | explicit param_type(result_type __mean = 0, result_type __stddev = 1) |
| 3649 | : __mean_(__mean), __stddev_(__stddev) {} |
| 3650 | |
| 3651 | result_type mean() const {return __mean_;} |
| 3652 | result_type stddev() const {return __stddev_;} |
| 3653 | |
| 3654 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3655 | {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;} |
| 3656 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3657 | {return !(__x == __y);} |
| 3658 | }; |
| 3659 | |
| 3660 | private: |
| 3661 | param_type __p_; |
| 3662 | result_type _V_; |
| 3663 | bool _V_hot_; |
| 3664 | |
| 3665 | public: |
| 3666 | // constructors and reset functions |
| 3667 | explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1) |
| 3668 | : __p_(param_type(__mean, __stddev)), _V_hot_(false) {} |
| 3669 | explicit normal_distribution(const param_type& __p) |
| 3670 | : __p_(__p), _V_hot_(false) {} |
| 3671 | void reset() {_V_hot_ = false;} |
| 3672 | |
| 3673 | // generating functions |
| 3674 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3675 | {return (*this)(__g, __p_);} |
| 3676 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3677 | |
| 3678 | // property functions |
| 3679 | result_type mean() const {return __p_.mean();} |
| 3680 | result_type stddev() const {return __p_.stddev();} |
| 3681 | |
| 3682 | param_type param() const {return __p_;} |
| 3683 | void param(const param_type& __p) {__p_ = __p;} |
| 3684 | |
| 3685 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
| 3686 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 3687 | |
| 3688 | friend bool operator==(const normal_distribution& __x, |
| 3689 | const normal_distribution& __y) |
| 3690 | {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ && |
| 3691 | (!__x._V_hot_ || __x._V_ == __y._V_);} |
| 3692 | friend bool operator!=(const normal_distribution& __x, |
| 3693 | const normal_distribution& __y) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 3694 | {return !(__x == __y);} |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 3695 | |
| 3696 | template <class _CharT, class _Traits, class _RT> |
| 3697 | friend |
| 3698 | basic_ostream<_CharT, _Traits>& |
| 3699 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3700 | const normal_distribution<_RT>& __x); |
| 3701 | |
| 3702 | template <class _CharT, class _Traits, class _RT> |
| 3703 | friend |
| 3704 | basic_istream<_CharT, _Traits>& |
| 3705 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3706 | normal_distribution<_RT>& __x); |
| 3707 | }; |
| 3708 | |
| 3709 | template <class _RealType> |
| 3710 | template<class _URNG> |
| 3711 | _RealType |
| 3712 | normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3713 | { |
| 3714 | result_type _U; |
| 3715 | if (_V_hot_) |
| 3716 | { |
| 3717 | _V_hot_ = false; |
| 3718 | _U = _V_; |
| 3719 | } |
| 3720 | else |
| 3721 | { |
| 3722 | uniform_real_distribution<result_type> _Uni(-1, 1); |
| 3723 | result_type __u; |
| 3724 | result_type __v; |
| 3725 | result_type __s; |
| 3726 | do |
| 3727 | { |
| 3728 | __u = _Uni(__g); |
| 3729 | __v = _Uni(__g); |
| 3730 | __s = __u * __u + __v * __v; |
| 3731 | } while (__s > 1 || __s == 0); |
| 3732 | result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s); |
| 3733 | _V_ = __v * _F; |
| 3734 | _V_hot_ = true; |
| 3735 | _U = __u * _F; |
| 3736 | } |
| 3737 | return _U * __p.stddev() + __p.mean(); |
| 3738 | } |
| 3739 | |
| 3740 | template <class _CharT, class _Traits, class _RT> |
| 3741 | basic_ostream<_CharT, _Traits>& |
| 3742 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3743 | const normal_distribution<_RT>& __x) |
| 3744 | { |
| 3745 | __save_flags<_CharT, _Traits> _(__os); |
| 3746 | __os.flags(ios_base::dec | ios_base::left); |
| 3747 | _CharT __sp = __os.widen(' '); |
| 3748 | __os.fill(__sp); |
| 3749 | __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_; |
| 3750 | if (__x._V_hot_) |
| 3751 | __os << __sp << __x._V_; |
| 3752 | return __os; |
| 3753 | } |
| 3754 | |
| 3755 | template <class _CharT, class _Traits, class _RT> |
| 3756 | basic_istream<_CharT, _Traits>& |
| 3757 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3758 | normal_distribution<_RT>& __x) |
| 3759 | { |
| 3760 | typedef normal_distribution<_RT> _Eng; |
| 3761 | typedef typename _Eng::result_type result_type; |
| 3762 | typedef typename _Eng::param_type param_type; |
| 3763 | __save_flags<_CharT, _Traits> _(__is); |
| 3764 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3765 | result_type __mean; |
| 3766 | result_type __stddev; |
| 3767 | result_type _V = 0; |
| 3768 | bool _V_hot = false; |
| 3769 | __is >> __mean >> __stddev >> _V_hot; |
| 3770 | if (_V_hot) |
| 3771 | __is >> _V; |
| 3772 | if (!__is.fail()) |
| 3773 | { |
| 3774 | __x.param(param_type(__mean, __stddev)); |
| 3775 | __x._V_hot_ = _V_hot; |
| 3776 | __x._V_ = _V; |
| 3777 | } |
| 3778 | return __is; |
| 3779 | } |
| 3780 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3781 | _LIBCPP_END_NAMESPACE_STD |
| 3782 | |
| 3783 | #endif // _LIBCPP_RANDOM |