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> |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 669 | class geometric_distribution |
| 670 | { |
| 671 | public: |
| 672 | // types |
| 673 | typedef IntType result_type; |
| 674 | |
| 675 | class param_type |
| 676 | { |
| 677 | public: |
| 678 | typedef geometric_distribution distribution_type; |
| 679 | |
| 680 | explicit param_type(double p = 0.5); |
| 681 | |
| 682 | double p() const; |
| 683 | |
| 684 | friend bool operator==(const param_type& x, const param_type& y); |
| 685 | friend bool operator!=(const param_type& x, const param_type& y); |
| 686 | }; |
| 687 | |
| 688 | // constructors and reset functions |
| 689 | explicit geometric_distribution(double p = 0.5); |
| 690 | explicit geometric_distribution(const param_type& parm); |
| 691 | void reset(); |
| 692 | |
| 693 | // generating functions |
| 694 | template<class URNG> result_type operator()(URNG& g); |
| 695 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 696 | |
| 697 | // property functions |
| 698 | double p() const; |
| 699 | |
| 700 | param_type param() const; |
| 701 | void param(const param_type& parm); |
| 702 | |
| 703 | result_type min() const; |
| 704 | result_type max() const; |
| 705 | |
| 706 | friend bool operator==(const geometric_distribution& x, |
| 707 | const geometric_distribution& y); |
| 708 | friend bool operator!=(const geometric_distribution& x, |
| 709 | const geometric_distribution& y); |
| 710 | |
| 711 | template <class charT, class traits> |
| 712 | friend |
| 713 | basic_ostream<charT, traits>& |
| 714 | operator<<(basic_ostream<charT, traits>& os, |
| 715 | const geometric_distribution& x); |
| 716 | |
| 717 | template <class charT, class traits> |
| 718 | friend |
| 719 | basic_istream<charT, traits>& |
| 720 | operator>>(basic_istream<charT, traits>& is, |
| 721 | geometric_distribution& x); |
| 722 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 723 | |
| 724 | template<class IntType = int> |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 725 | class negative_binomial_distribution |
| 726 | { |
| 727 | public: |
| 728 | // types |
| 729 | typedef IntType result_type; |
| 730 | |
| 731 | class param_type |
| 732 | { |
| 733 | public: |
| 734 | typedef negative_binomial_distribution distribution_type; |
| 735 | |
| 736 | explicit param_type(result_type k = 1, double p = 0.5); |
| 737 | |
| 738 | result_type k() const; |
| 739 | double p() const; |
| 740 | |
| 741 | friend bool operator==(const param_type& x, const param_type& y); |
| 742 | friend bool operator!=(const param_type& x, const param_type& y); |
| 743 | }; |
| 744 | |
| 745 | // constructor and reset functions |
| 746 | explicit negative_binomial_distribution(result_type k = 1, double p = 0.5); |
| 747 | explicit negative_binomial_distribution(const param_type& parm); |
| 748 | void reset(); |
| 749 | |
| 750 | // generating functions |
| 751 | template<class URNG> result_type operator()(URNG& g); |
| 752 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 753 | |
| 754 | // property functions |
| 755 | result_type k() const; |
| 756 | double p() const; |
| 757 | |
| 758 | param_type param() const; |
| 759 | void param(const param_type& parm); |
| 760 | |
| 761 | result_type min() const; |
| 762 | result_type max() const; |
| 763 | |
| 764 | friend bool operator==(const negative_binomial_distribution& x, |
| 765 | const negative_binomial_distribution& y); |
| 766 | friend bool operator!=(const negative_binomial_distribution& x, |
| 767 | const negative_binomial_distribution& y); |
| 768 | |
| 769 | template <class charT, class traits> |
| 770 | friend |
| 771 | basic_ostream<charT, traits>& |
| 772 | operator<<(basic_ostream<charT, traits>& os, |
| 773 | const negative_binomial_distribution& x); |
| 774 | |
| 775 | template <class charT, class traits> |
| 776 | friend |
| 777 | basic_istream<charT, traits>& |
| 778 | operator>>(basic_istream<charT, traits>& is, |
| 779 | negative_binomial_distribution& x); |
| 780 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 781 | |
| 782 | template<class IntType = int> |
Howard Hinnant | 4ff556c | 2010-05-14 21:38:54 +0000 | [diff] [blame] | 783 | class poisson_distribution |
| 784 | { |
| 785 | public: |
| 786 | // types |
| 787 | typedef IntType result_type; |
| 788 | |
| 789 | class param_type |
| 790 | { |
| 791 | public: |
| 792 | typedef poisson_distribution distribution_type; |
| 793 | |
| 794 | explicit param_type(double mean = 1.0); |
| 795 | |
| 796 | double mean() const; |
| 797 | |
| 798 | friend bool operator==(const param_type& x, const param_type& y); |
| 799 | friend bool operator!=(const param_type& x, const param_type& y); |
| 800 | }; |
| 801 | |
| 802 | // constructors and reset functions |
| 803 | explicit poisson_distribution(double mean = 1.0); |
| 804 | explicit poisson_distribution(const param_type& parm); |
| 805 | void reset(); |
| 806 | |
| 807 | // generating functions |
| 808 | template<class URNG> result_type operator()(URNG& g); |
| 809 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 810 | |
| 811 | // property functions |
| 812 | double mean() const; |
| 813 | |
| 814 | param_type param() const; |
| 815 | void param(const param_type& parm); |
| 816 | |
| 817 | result_type min() const; |
| 818 | result_type max() const; |
| 819 | |
| 820 | friend bool operator==(const poisson_distribution& x, |
| 821 | const poisson_distribution& y); |
| 822 | friend bool operator!=(const poisson_distribution& x, |
| 823 | const poisson_distribution& y); |
| 824 | |
| 825 | template <class charT, class traits> |
| 826 | friend |
| 827 | basic_ostream<charT, traits>& |
| 828 | operator<<(basic_ostream<charT, traits>& os, |
| 829 | const poisson_distribution& x); |
| 830 | |
| 831 | template <class charT, class traits> |
| 832 | friend |
| 833 | basic_istream<charT, traits>& |
| 834 | operator>>(basic_istream<charT, traits>& is, |
| 835 | poisson_distribution& x); |
| 836 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 837 | |
| 838 | template<class RealType = double> |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 839 | class exponential_distribution |
| 840 | { |
| 841 | public: |
| 842 | // types |
| 843 | typedef RealType result_type; |
| 844 | |
| 845 | class param_type |
| 846 | { |
| 847 | public: |
| 848 | typedef exponential_distribution distribution_type; |
| 849 | |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 850 | explicit param_type(result_type lambda = 1.0); |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 851 | |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 852 | result_type lambda() const; |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 853 | |
| 854 | friend bool operator==(const param_type& x, const param_type& y); |
| 855 | friend bool operator!=(const param_type& x, const param_type& y); |
| 856 | }; |
| 857 | |
| 858 | // constructors and reset functions |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 859 | explicit exponential_distribution(result_type lambda = 1.0); |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 860 | explicit exponential_distribution(const param_type& parm); |
| 861 | void reset(); |
| 862 | |
| 863 | // generating functions |
| 864 | template<class URNG> result_type operator()(URNG& g); |
| 865 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 866 | |
| 867 | // property functions |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 868 | result_type lambda() const; |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 869 | |
| 870 | param_type param() const; |
| 871 | void param(const param_type& parm); |
| 872 | |
| 873 | result_type min() const; |
| 874 | result_type max() const; |
| 875 | |
| 876 | friend bool operator==(const exponential_distribution& x, |
| 877 | const exponential_distribution& y); |
| 878 | friend bool operator!=(const exponential_distribution& x, |
| 879 | const exponential_distribution& y); |
| 880 | |
| 881 | template <class charT, class traits> |
| 882 | friend |
| 883 | basic_ostream<charT, traits>& |
| 884 | operator<<(basic_ostream<charT, traits>& os, |
| 885 | const exponential_distribution& x); |
| 886 | |
| 887 | template <class charT, class traits> |
| 888 | friend |
| 889 | basic_istream<charT, traits>& |
| 890 | operator>>(basic_istream<charT, traits>& is, |
| 891 | exponential_distribution& x); |
| 892 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 893 | |
| 894 | template<class RealType = double> |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 895 | class gamma_distribution |
| 896 | { |
| 897 | public: |
| 898 | // types |
| 899 | typedef RealType result_type; |
| 900 | |
| 901 | class param_type |
| 902 | { |
| 903 | public: |
| 904 | typedef gamma_distribution distribution_type; |
| 905 | |
| 906 | explicit param_type(result_type alpha = 1, result_type beta = 1); |
| 907 | |
| 908 | result_type alpha() const; |
| 909 | result_type beta() const; |
| 910 | |
| 911 | friend bool operator==(const param_type& x, const param_type& y); |
| 912 | friend bool operator!=(const param_type& x, const param_type& y); |
| 913 | }; |
| 914 | |
| 915 | // constructors and reset functions |
| 916 | explicit gamma_distribution(result_type alpha = 1, result_type beta = 1); |
| 917 | explicit gamma_distribution(const param_type& parm); |
| 918 | void reset(); |
| 919 | |
| 920 | // generating functions |
| 921 | template<class URNG> result_type operator()(URNG& g); |
| 922 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 923 | |
| 924 | // property functions |
| 925 | result_type alpha() const; |
| 926 | result_type beta() const; |
| 927 | |
| 928 | param_type param() const; |
| 929 | void param(const param_type& parm); |
| 930 | |
| 931 | result_type min() const; |
| 932 | result_type max() const; |
| 933 | |
| 934 | friend bool operator==(const gamma_distribution& x, |
| 935 | const gamma_distribution& y); |
| 936 | friend bool operator!=(const gamma_distribution& x, |
| 937 | const gamma_distribution& y); |
| 938 | |
| 939 | template <class charT, class traits> |
| 940 | friend |
| 941 | basic_ostream<charT, traits>& |
| 942 | operator<<(basic_ostream<charT, traits>& os, |
| 943 | const gamma_distribution& x); |
| 944 | |
| 945 | template <class charT, class traits> |
| 946 | friend |
| 947 | basic_istream<charT, traits>& |
| 948 | operator>>(basic_istream<charT, traits>& is, |
| 949 | gamma_distribution& x); |
| 950 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 951 | |
| 952 | template<class RealType = double> |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 953 | class weibull_distribution |
| 954 | { |
| 955 | public: |
| 956 | // types |
| 957 | typedef RealType result_type; |
| 958 | |
| 959 | class param_type |
| 960 | { |
| 961 | public: |
| 962 | typedef weibull_distribution distribution_type; |
| 963 | |
| 964 | explicit param_type(result_type alpha = 1, result_type beta = 1); |
| 965 | |
| 966 | result_type a() const; |
| 967 | result_type b() const; |
| 968 | |
| 969 | friend bool operator==(const param_type& x, const param_type& y); |
| 970 | friend bool operator!=(const param_type& x, const param_type& y); |
| 971 | }; |
| 972 | |
| 973 | // constructor and reset functions |
| 974 | explicit weibull_distribution(result_type a = 1, result_type b = 1); |
| 975 | explicit weibull_distribution(const param_type& parm); |
| 976 | void reset(); |
| 977 | |
| 978 | // generating functions |
| 979 | template<class URNG> result_type operator()(URNG& g); |
| 980 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 981 | |
| 982 | // property functions |
| 983 | result_type a() const; |
| 984 | result_type b() const; |
| 985 | |
| 986 | param_type param() const; |
| 987 | void param(const param_type& parm); |
| 988 | |
| 989 | result_type min() const; |
| 990 | result_type max() const; |
| 991 | |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 992 | friend bool operator==(const weibull_distribution& x, |
| 993 | const weibull_distribution& y); |
| 994 | friend bool operator!=(const weibull_distribution& x, |
| 995 | const weibull_distribution& y); |
| 996 | |
| 997 | template <class charT, class traits> |
| 998 | friend |
| 999 | basic_ostream<charT, traits>& |
| 1000 | operator<<(basic_ostream<charT, traits>& os, |
| 1001 | const weibull_distribution& x); |
| 1002 | |
| 1003 | template <class charT, class traits> |
| 1004 | friend |
| 1005 | basic_istream<charT, traits>& |
| 1006 | operator>>(basic_istream<charT, traits>& is, |
| 1007 | weibull_distribution& x); |
| 1008 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1009 | |
| 1010 | template<class RealType = double> |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 1011 | class extreme_value_distribution |
| 1012 | { |
| 1013 | public: |
| 1014 | // types |
| 1015 | typedef RealType result_type; |
| 1016 | |
| 1017 | class param_type |
| 1018 | { |
| 1019 | public: |
| 1020 | typedef extreme_value_distribution distribution_type; |
| 1021 | |
| 1022 | explicit param_type(result_type a = 0, result_type b = 1); |
| 1023 | |
| 1024 | result_type a() const; |
| 1025 | result_type b() const; |
| 1026 | |
| 1027 | friend bool operator==(const param_type& x, const param_type& y); |
| 1028 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1029 | }; |
| 1030 | |
| 1031 | // constructor and reset functions |
| 1032 | explicit extreme_value_distribution(result_type a = 0, result_type b = 1); |
| 1033 | explicit extreme_value_distribution(const param_type& parm); |
| 1034 | void reset(); |
| 1035 | |
| 1036 | // generating functions |
| 1037 | template<class URNG> result_type operator()(URNG& g); |
| 1038 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1039 | |
| 1040 | // property functions |
| 1041 | result_type a() const; |
| 1042 | result_type b() const; |
| 1043 | |
| 1044 | param_type param() const; |
| 1045 | void param(const param_type& parm); |
| 1046 | |
| 1047 | result_type min() const; |
| 1048 | result_type max() const; |
| 1049 | |
| 1050 | friend bool operator==(const extreme_value_distribution& x, |
| 1051 | const extreme_value_distribution& y); |
| 1052 | friend bool operator!=(const extreme_value_distribution& x, |
| 1053 | const extreme_value_distribution& y); |
| 1054 | |
| 1055 | template <class charT, class traits> |
| 1056 | friend |
| 1057 | basic_ostream<charT, traits>& |
| 1058 | operator<<(basic_ostream<charT, traits>& os, |
| 1059 | const extreme_value_distribution& x); |
| 1060 | |
| 1061 | template <class charT, class traits> |
| 1062 | friend |
| 1063 | basic_istream<charT, traits>& |
| 1064 | operator>>(basic_istream<charT, traits>& is, |
| 1065 | extreme_value_distribution& x); |
| 1066 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1067 | |
| 1068 | template<class RealType = double> |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 1069 | class normal_distribution |
| 1070 | { |
| 1071 | public: |
| 1072 | // types |
| 1073 | typedef RealType result_type; |
| 1074 | |
| 1075 | class param_type |
| 1076 | { |
| 1077 | public: |
| 1078 | typedef normal_distribution distribution_type; |
| 1079 | |
| 1080 | explicit param_type(result_type mean = 0, result_type stddev = 1); |
| 1081 | |
| 1082 | result_type mean() const; |
| 1083 | result_type stddev() const; |
| 1084 | |
| 1085 | friend bool operator==(const param_type& x, const param_type& y); |
| 1086 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1087 | }; |
| 1088 | |
| 1089 | // constructors and reset functions |
| 1090 | explicit normal_distribution(result_type mean = 0, result_type stddev = 1); |
| 1091 | explicit normal_distribution(const param_type& parm); |
| 1092 | void reset(); |
| 1093 | |
| 1094 | // generating functions |
| 1095 | template<class URNG> result_type operator()(URNG& g); |
| 1096 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1097 | |
| 1098 | // property functions |
| 1099 | result_type mean() const; |
| 1100 | result_type stddev() const; |
| 1101 | |
| 1102 | param_type param() const; |
| 1103 | void param(const param_type& parm); |
| 1104 | |
| 1105 | result_type min() const; |
| 1106 | result_type max() const; |
| 1107 | |
| 1108 | friend bool operator==(const normal_distribution& x, |
| 1109 | const normal_distribution& y); |
| 1110 | friend bool operator!=(const normal_distribution& x, |
| 1111 | const normal_distribution& y); |
| 1112 | |
| 1113 | template <class charT, class traits> |
| 1114 | friend |
| 1115 | basic_ostream<charT, traits>& |
| 1116 | operator<<(basic_ostream<charT, traits>& os, |
| 1117 | const normal_distribution& x); |
| 1118 | |
| 1119 | template <class charT, class traits> |
| 1120 | friend |
| 1121 | basic_istream<charT, traits>& |
| 1122 | operator>>(basic_istream<charT, traits>& is, |
| 1123 | normal_distribution& x); |
| 1124 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1125 | |
| 1126 | template<class RealType = double> |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame^] | 1127 | class lognormal_distribution |
| 1128 | { |
| 1129 | public: |
| 1130 | // types |
| 1131 | typedef RealType result_type; |
| 1132 | |
| 1133 | class param_type |
| 1134 | { |
| 1135 | public: |
| 1136 | typedef lognormal_distribution distribution_type; |
| 1137 | |
| 1138 | explicit param_type(result_type m = 0, result_type s = 1); |
| 1139 | |
| 1140 | result_type m() const; |
| 1141 | result_type s() const; |
| 1142 | |
| 1143 | friend bool operator==(const param_type& x, const param_type& y); |
| 1144 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1145 | }; |
| 1146 | |
| 1147 | // constructor and reset functions |
| 1148 | explicit lognormal_distribution(result_type m = 0, result_type s = 1); |
| 1149 | explicit lognormal_distribution(const param_type& parm); |
| 1150 | void reset(); |
| 1151 | |
| 1152 | // generating functions |
| 1153 | template<class URNG> result_type operator()(URNG& g); |
| 1154 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1155 | |
| 1156 | // property functions |
| 1157 | result_type m() const; |
| 1158 | result_type s() const; |
| 1159 | |
| 1160 | param_type param() const; |
| 1161 | void param(const param_type& parm); |
| 1162 | |
| 1163 | result_type min() const; |
| 1164 | result_type max() const; |
| 1165 | |
| 1166 | friend bool operator==(const lognormal_distribution& x, |
| 1167 | const lognormal_distribution& y); |
| 1168 | friend bool operator!=(const lognormal_distribution& x, |
| 1169 | const lognormal_distribution& y); |
| 1170 | |
| 1171 | template <class charT, class traits> |
| 1172 | friend |
| 1173 | basic_ostream<charT, traits>& |
| 1174 | operator<<(basic_ostream<charT, traits>& os, |
| 1175 | const lognormal_distribution& x); |
| 1176 | |
| 1177 | template <class charT, class traits> |
| 1178 | friend |
| 1179 | basic_istream<charT, traits>& |
| 1180 | operator>>(basic_istream<charT, traits>& is, |
| 1181 | lognormal_distribution& x); |
| 1182 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1183 | |
| 1184 | template<class RealType = double> |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 1185 | class chi_squared_distribution |
| 1186 | { |
| 1187 | public: |
| 1188 | // types |
| 1189 | typedef RealType result_type; |
| 1190 | |
| 1191 | class param_type |
| 1192 | { |
| 1193 | public: |
| 1194 | typedef chi_squared_distribution distribution_type; |
| 1195 | |
| 1196 | explicit param_type(result_type n = 1); |
| 1197 | |
| 1198 | result_type n() const; |
| 1199 | |
| 1200 | friend bool operator==(const param_type& x, const param_type& y); |
| 1201 | friend bool operator!=(const param_type& x, const param_type& y); |
| 1202 | }; |
| 1203 | |
| 1204 | // constructor and reset functions |
| 1205 | explicit chi_squared_distribution(result_type n = 1); |
| 1206 | explicit chi_squared_distribution(const param_type& parm); |
| 1207 | void reset(); |
| 1208 | |
| 1209 | // generating functions |
| 1210 | template<class URNG> result_type operator()(URNG& g); |
| 1211 | template<class URNG> result_type operator()(URNG& g, const param_type& parm); |
| 1212 | |
| 1213 | // property functions |
| 1214 | result_type n() const; |
| 1215 | |
| 1216 | param_type param() const; |
| 1217 | void param(const param_type& parm); |
| 1218 | |
| 1219 | result_type min() const; |
| 1220 | result_type max() const; |
| 1221 | |
| 1222 | |
| 1223 | friend bool operator==(const chi_squared_distribution& x, |
| 1224 | const chi_squared_distribution& y); |
| 1225 | friend bool operator!=(const chi_squared_distribution& x, |
| 1226 | const chi_squared_distribution& y); |
| 1227 | |
| 1228 | template <class charT, class traits> |
| 1229 | friend |
| 1230 | basic_ostream<charT, traits>& |
| 1231 | operator<<(basic_ostream<charT, traits>& os, |
| 1232 | const chi_squared_distribution& x); |
| 1233 | |
| 1234 | template <class charT, class traits> |
| 1235 | friend |
| 1236 | basic_istream<charT, traits>& |
| 1237 | operator>>(basic_istream<charT, traits>& is, |
| 1238 | chi_squared_distribution& x); |
| 1239 | }; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1240 | |
| 1241 | template<class RealType = double> |
| 1242 | class cauchy_distribution; |
| 1243 | |
| 1244 | template<class RealType = double> |
| 1245 | class fisher_f_distribution; |
| 1246 | |
| 1247 | template<class RealType = double> |
| 1248 | class student_t_distribution; |
| 1249 | |
| 1250 | template<class IntType = int> |
| 1251 | class discrete_distribution; |
| 1252 | |
| 1253 | template<class RealType = double> |
| 1254 | class piecewise_constant_distribution; |
| 1255 | |
| 1256 | template<class RealType = double> |
| 1257 | class piecewise_linear_distribution; |
| 1258 | |
| 1259 | } // std |
| 1260 | */ |
| 1261 | |
| 1262 | #include <__config> |
| 1263 | #include <cstddef> |
| 1264 | #include <type_traits> |
| 1265 | #include <initializer_list> |
| 1266 | #include <cstdint> |
| 1267 | #include <limits> |
| 1268 | #include <algorithm> |
| 1269 | #include <vector> |
| 1270 | #include <string> |
| 1271 | #include <istream> |
| 1272 | #include <ostream> |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 1273 | #include <cmath> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1274 | |
| 1275 | #pragma GCC system_header |
| 1276 | |
| 1277 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 1278 | |
| 1279 | // linear_congruential_engine |
| 1280 | |
| 1281 | template <unsigned long long __a, unsigned long long __c, |
| 1282 | unsigned long long __m, unsigned long long _M, |
| 1283 | bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)> |
| 1284 | struct __lce_ta; |
| 1285 | |
| 1286 | // 64 |
| 1287 | |
| 1288 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 1289 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true> |
| 1290 | { |
| 1291 | typedef unsigned long long result_type; |
| 1292 | static result_type next(result_type __x) |
| 1293 | { |
| 1294 | // Schrage's algorithm |
| 1295 | const result_type __q = __m / __a; |
| 1296 | const result_type __r = __m % __a; |
| 1297 | const result_type __t0 = __a * (__x % __q); |
| 1298 | const result_type __t1 = __r * (__x / __q); |
| 1299 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1300 | __x += __c - (__x >= __m - __c) * __m; |
| 1301 | return __x; |
| 1302 | } |
| 1303 | }; |
| 1304 | |
| 1305 | template <unsigned long long __a, unsigned long long __m> |
| 1306 | struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true> |
| 1307 | { |
| 1308 | typedef unsigned long long result_type; |
| 1309 | static result_type next(result_type __x) |
| 1310 | { |
| 1311 | // Schrage's algorithm |
| 1312 | const result_type __q = __m / __a; |
| 1313 | const result_type __r = __m % __a; |
| 1314 | const result_type __t0 = __a * (__x % __q); |
| 1315 | const result_type __t1 = __r * (__x / __q); |
| 1316 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1317 | return __x; |
| 1318 | } |
| 1319 | }; |
| 1320 | |
| 1321 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m> |
| 1322 | struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false> |
| 1323 | { |
| 1324 | typedef unsigned long long result_type; |
| 1325 | static result_type next(result_type __x) |
| 1326 | { |
| 1327 | return (__a * __x + __c) % __m; |
| 1328 | } |
| 1329 | }; |
| 1330 | |
| 1331 | template <unsigned long long __a, unsigned long long __c> |
| 1332 | struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false> |
| 1333 | { |
| 1334 | typedef unsigned long long result_type; |
| 1335 | static result_type next(result_type __x) |
| 1336 | { |
| 1337 | return __a * __x + __c; |
| 1338 | } |
| 1339 | }; |
| 1340 | |
| 1341 | // 32 |
| 1342 | |
| 1343 | template <unsigned long long _A, unsigned long long _C, unsigned long long _M> |
| 1344 | struct __lce_ta<_A, _C, _M, unsigned(~0), true> |
| 1345 | { |
| 1346 | typedef unsigned result_type; |
| 1347 | static result_type next(result_type __x) |
| 1348 | { |
| 1349 | const result_type __a = static_cast<result_type>(_A); |
| 1350 | const result_type __c = static_cast<result_type>(_C); |
| 1351 | const result_type __m = static_cast<result_type>(_M); |
| 1352 | // Schrage's algorithm |
| 1353 | const result_type __q = __m / __a; |
| 1354 | const result_type __r = __m % __a; |
| 1355 | const result_type __t0 = __a * (__x % __q); |
| 1356 | const result_type __t1 = __r * (__x / __q); |
| 1357 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1358 | __x += __c - (__x >= __m - __c) * __m; |
| 1359 | return __x; |
| 1360 | } |
| 1361 | }; |
| 1362 | |
| 1363 | template <unsigned long long _A, unsigned long long _M> |
| 1364 | struct __lce_ta<_A, 0, _M, unsigned(~0), true> |
| 1365 | { |
| 1366 | typedef unsigned result_type; |
| 1367 | static result_type next(result_type __x) |
| 1368 | { |
| 1369 | const result_type __a = static_cast<result_type>(_A); |
| 1370 | const result_type __m = static_cast<result_type>(_M); |
| 1371 | // Schrage's algorithm |
| 1372 | const result_type __q = __m / __a; |
| 1373 | const result_type __r = __m % __a; |
| 1374 | const result_type __t0 = __a * (__x % __q); |
| 1375 | const result_type __t1 = __r * (__x / __q); |
| 1376 | __x = __t0 + (__t0 < __t1) * __m - __t1; |
| 1377 | return __x; |
| 1378 | } |
| 1379 | }; |
| 1380 | |
| 1381 | template <unsigned long long _A, unsigned long long _C, unsigned long long _M> |
| 1382 | struct __lce_ta<_A, _C, _M, unsigned(~0), false> |
| 1383 | { |
| 1384 | typedef unsigned result_type; |
| 1385 | static result_type next(result_type __x) |
| 1386 | { |
| 1387 | const result_type __a = static_cast<result_type>(_A); |
| 1388 | const result_type __c = static_cast<result_type>(_C); |
| 1389 | const result_type __m = static_cast<result_type>(_M); |
| 1390 | return (__a * __x + __c) % __m; |
| 1391 | } |
| 1392 | }; |
| 1393 | |
| 1394 | template <unsigned long long _A, unsigned long long _C> |
| 1395 | struct __lce_ta<_A, _C, 0, unsigned(~0), false> |
| 1396 | { |
| 1397 | typedef unsigned result_type; |
| 1398 | static result_type next(result_type __x) |
| 1399 | { |
| 1400 | const result_type __a = static_cast<result_type>(_A); |
| 1401 | const result_type __c = static_cast<result_type>(_C); |
| 1402 | return __a * __x + __c; |
| 1403 | } |
| 1404 | }; |
| 1405 | |
| 1406 | // 16 |
| 1407 | |
| 1408 | template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b> |
| 1409 | struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b> |
| 1410 | { |
| 1411 | typedef unsigned short result_type; |
| 1412 | static result_type next(result_type __x) |
| 1413 | { |
| 1414 | return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x)); |
| 1415 | } |
| 1416 | }; |
| 1417 | |
| 1418 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1419 | class linear_congruential_engine; |
| 1420 | |
| 1421 | template <class _CharT, class _Traits, |
| 1422 | class _U, _U _A, _U _C, _U _N> |
| 1423 | basic_ostream<_CharT, _Traits>& |
| 1424 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1425 | const linear_congruential_engine<_U, _A, _C, _N>&); |
| 1426 | |
| 1427 | template <class _CharT, class _Traits, |
| 1428 | class _U, _U _A, _U _C, _U _N> |
| 1429 | basic_istream<_CharT, _Traits>& |
| 1430 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1431 | linear_congruential_engine<_U, _A, _C, _N>& __x); |
| 1432 | |
| 1433 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1434 | class linear_congruential_engine |
| 1435 | { |
| 1436 | public: |
| 1437 | // types |
| 1438 | typedef _UIntType result_type; |
| 1439 | |
| 1440 | private: |
| 1441 | result_type __x_; |
| 1442 | |
| 1443 | static const result_type _M = result_type(~0); |
| 1444 | |
| 1445 | static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters"); |
| 1446 | static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters"); |
| 1447 | public: |
| 1448 | static const result_type _Min = __c == 0u ? 1u: 0u; |
| 1449 | static const result_type _Max = __m - 1u; |
| 1450 | static_assert(_Min < _Max, "linear_congruential_engine invalid parameters"); |
| 1451 | |
| 1452 | // engine characteristics |
| 1453 | static const/*expr*/ result_type multiplier = __a; |
| 1454 | static const/*expr*/ result_type increment = __c; |
| 1455 | static const/*expr*/ result_type modulus = __m; |
| 1456 | static const/*expr*/ result_type min() {return _Min;} |
| 1457 | static const/*expr*/ result_type max() {return _Max;} |
| 1458 | static const/*expr*/ result_type default_seed = 1u; |
| 1459 | |
| 1460 | // constructors and seeding functions |
| 1461 | explicit linear_congruential_engine(result_type __s = default_seed) |
| 1462 | {seed(__s);} |
| 1463 | template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q) |
| 1464 | {seed(__q);} |
| 1465 | void seed(result_type __s = default_seed) |
| 1466 | {seed(integral_constant<bool, __m == 0>(), |
| 1467 | integral_constant<bool, __c == 0>(), __s);} |
| 1468 | template<class _Sseq> |
| 1469 | typename enable_if |
| 1470 | < |
| 1471 | !is_convertible<_Sseq, result_type>::value, |
| 1472 | void |
| 1473 | >::type |
| 1474 | seed(_Sseq& __q) |
| 1475 | {__seed(__q, integral_constant<unsigned, |
| 1476 | 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32 |
| 1477 | : (__m-1) / 0x100000000ull)>());} |
| 1478 | |
| 1479 | // generating functions |
| 1480 | result_type operator()() |
| 1481 | {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));} |
| 1482 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 1483 | |
| 1484 | friend bool operator==(const linear_congruential_engine& __x, |
| 1485 | const linear_congruential_engine& __y) |
| 1486 | {return __x.__x_ == __y.__x_;} |
| 1487 | friend bool operator!=(const linear_congruential_engine& __x, |
| 1488 | const linear_congruential_engine& __y) |
| 1489 | {return !(__x == __y);} |
| 1490 | |
| 1491 | private: |
| 1492 | |
| 1493 | void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;} |
| 1494 | void seed(true_type, false_type, result_type __s) {__x_ = __s;} |
| 1495 | void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ? |
| 1496 | 1 : __s % __m;} |
| 1497 | void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;} |
| 1498 | |
| 1499 | template<class _Sseq> |
| 1500 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 1501 | template<class _Sseq> |
| 1502 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 1503 | |
| 1504 | template <class _CharT, class _Traits, |
| 1505 | class _U, _U _A, _U _C, _U _N> |
| 1506 | friend |
| 1507 | basic_ostream<_CharT, _Traits>& |
| 1508 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1509 | const linear_congruential_engine<_U, _A, _C, _N>&); |
| 1510 | |
| 1511 | template <class _CharT, class _Traits, |
| 1512 | class _U, _U _A, _U _C, _U _N> |
| 1513 | friend |
| 1514 | basic_istream<_CharT, _Traits>& |
| 1515 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1516 | linear_congruential_engine<_U, _A, _C, _N>& __x); |
| 1517 | }; |
| 1518 | |
| 1519 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1520 | template<class _Sseq> |
| 1521 | void |
| 1522 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 1523 | integral_constant<unsigned, 1>) |
| 1524 | { |
| 1525 | const unsigned __k = 1; |
| 1526 | uint32_t __ar[__k+3]; |
| 1527 | __q.generate(__ar, __ar + __k + 3); |
| 1528 | result_type __s = static_cast<result_type>(__ar[3] % __m); |
| 1529 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 1530 | } |
| 1531 | |
| 1532 | template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1533 | template<class _Sseq> |
| 1534 | void |
| 1535 | linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, |
| 1536 | integral_constant<unsigned, 2>) |
| 1537 | { |
| 1538 | const unsigned __k = 2; |
| 1539 | uint32_t __ar[__k+3]; |
| 1540 | __q.generate(__ar, __ar + __k + 3); |
| 1541 | result_type __s = static_cast<result_type>((__ar[3] + |
| 1542 | (uint64_t)__ar[4] << 32) % __m); |
| 1543 | __x_ = __c == 0 && __s == 0 ? result_type(1) : __s; |
| 1544 | } |
| 1545 | |
| 1546 | template <class _CharT, class _Traits> |
| 1547 | class __save_flags |
| 1548 | { |
| 1549 | typedef basic_ios<_CharT, _Traits> __stream_type; |
| 1550 | typedef typename __stream_type::fmtflags fmtflags; |
| 1551 | |
| 1552 | __stream_type& __stream_; |
| 1553 | fmtflags __fmtflags_; |
| 1554 | _CharT __fill_; |
| 1555 | |
| 1556 | __save_flags(const __save_flags&); |
| 1557 | __save_flags& operator=(const __save_flags&); |
| 1558 | public: |
| 1559 | explicit __save_flags(__stream_type& __stream) |
| 1560 | : __stream_(__stream), |
| 1561 | __fmtflags_(__stream.flags()), |
| 1562 | __fill_(__stream.fill()) |
| 1563 | {} |
| 1564 | ~__save_flags() |
| 1565 | { |
| 1566 | __stream_.flags(__fmtflags_); |
| 1567 | __stream_.fill(__fill_); |
| 1568 | } |
| 1569 | }; |
| 1570 | |
| 1571 | template <class _CharT, class _Traits, |
| 1572 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1573 | inline |
| 1574 | basic_ostream<_CharT, _Traits>& |
| 1575 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1576 | const linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 1577 | { |
| 1578 | __save_flags<_CharT, _Traits> _(__os); |
| 1579 | __os.flags(ios_base::dec | ios_base::left); |
| 1580 | __os.fill(__os.widen(' ')); |
| 1581 | return __os << __x.__x_; |
| 1582 | } |
| 1583 | |
| 1584 | template <class _CharT, class _Traits, |
| 1585 | class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
| 1586 | basic_istream<_CharT, _Traits>& |
| 1587 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1588 | linear_congruential_engine<_UIntType, __a, __c, __m>& __x) |
| 1589 | { |
| 1590 | __save_flags<_CharT, _Traits> _(__is); |
| 1591 | __is.flags(ios_base::dec | ios_base::skipws); |
| 1592 | _UIntType __t; |
| 1593 | __is >> __t; |
| 1594 | if (!__is.fail()) |
| 1595 | __x.__x_ = __t; |
| 1596 | return __is; |
| 1597 | } |
| 1598 | |
| 1599 | typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> |
| 1600 | minstd_rand0; |
| 1601 | typedef minstd_rand0 default_random_engine; |
| 1602 | typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> |
| 1603 | minstd_rand; |
| 1604 | // mersenne_twister_engine |
| 1605 | |
| 1606 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1607 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1608 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1609 | class mersenne_twister_engine; |
| 1610 | |
| 1611 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1612 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1613 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1614 | bool |
| 1615 | operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1616 | _B, _T, _C, _L, _F>& __x, |
| 1617 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1618 | _B, _T, _C, _L, _F>& __y); |
| 1619 | |
| 1620 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1621 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1622 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1623 | bool |
| 1624 | operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1625 | _B, _T, _C, _L, _F>& __x, |
| 1626 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1627 | _B, _T, _C, _L, _F>& __y); |
| 1628 | |
| 1629 | template <class _CharT, class _Traits, |
| 1630 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1631 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1632 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1633 | basic_ostream<_CharT, _Traits>& |
| 1634 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1635 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1636 | _B, _T, _C, _L, _F>& __x); |
| 1637 | |
| 1638 | template <class _CharT, class _Traits, |
| 1639 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1640 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1641 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1642 | basic_istream<_CharT, _Traits>& |
| 1643 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1644 | mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1645 | _B, _T, _C, _L, _F>& __x); |
| 1646 | |
| 1647 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1648 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1649 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1650 | class mersenne_twister_engine |
| 1651 | { |
| 1652 | public: |
| 1653 | // types |
| 1654 | typedef _UIntType result_type; |
| 1655 | |
| 1656 | private: |
| 1657 | result_type __x_[__n]; |
| 1658 | size_t __i_; |
| 1659 | |
| 1660 | static_assert( 0 < __m, "mersenne_twister_engine invalid parameters"); |
| 1661 | static_assert(__m <= __n, "mersenne_twister_engine invalid parameters"); |
| 1662 | static const result_type _Dt = numeric_limits<result_type>::digits; |
| 1663 | static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters"); |
| 1664 | static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters"); |
| 1665 | static_assert(__r <= __w, "mersenne_twister_engine invalid parameters"); |
| 1666 | static_assert(__u <= __w, "mersenne_twister_engine invalid parameters"); |
| 1667 | static_assert(__s <= __w, "mersenne_twister_engine invalid parameters"); |
| 1668 | static_assert(__t <= __w, "mersenne_twister_engine invalid parameters"); |
| 1669 | static_assert(__l <= __w, "mersenne_twister_engine invalid parameters"); |
| 1670 | public: |
| 1671 | static const result_type _Min = 0; |
| 1672 | static const result_type _Max = __w == _Dt ? result_type(~0) : |
| 1673 | (result_type(1) << __w) - result_type(1); |
| 1674 | static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters"); |
| 1675 | static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters"); |
| 1676 | static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters"); |
| 1677 | static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters"); |
| 1678 | static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters"); |
| 1679 | static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters"); |
| 1680 | |
| 1681 | // engine characteristics |
| 1682 | static const/*expr*/ size_t word_size = __w; |
| 1683 | static const/*expr*/ size_t state_size = __n; |
| 1684 | static const/*expr*/ size_t shift_size = __m; |
| 1685 | static const/*expr*/ size_t mask_bits = __r; |
| 1686 | static const/*expr*/ result_type xor_mask = __a; |
| 1687 | static const/*expr*/ size_t tempering_u = __u; |
| 1688 | static const/*expr*/ result_type tempering_d = __d; |
| 1689 | static const/*expr*/ size_t tempering_s = __s; |
| 1690 | static const/*expr*/ result_type tempering_b = __b; |
| 1691 | static const/*expr*/ size_t tempering_t = __t; |
| 1692 | static const/*expr*/ result_type tempering_c = __c; |
| 1693 | static const/*expr*/ size_t tempering_l = __l; |
| 1694 | static const/*expr*/ result_type initialization_multiplier = __f; |
| 1695 | static const/*expr*/ result_type min() { return _Min; } |
| 1696 | static const/*expr*/ result_type max() { return _Max; } |
| 1697 | static const/*expr*/ result_type default_seed = 5489u; |
| 1698 | |
| 1699 | // constructors and seeding functions |
| 1700 | explicit mersenne_twister_engine(result_type __sd = default_seed) |
| 1701 | {seed(__sd);} |
| 1702 | template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q) |
| 1703 | {seed(__q);} |
| 1704 | void seed(result_type __sd = default_seed); |
| 1705 | template<class _Sseq> |
| 1706 | typename enable_if |
| 1707 | < |
| 1708 | !is_convertible<_Sseq, result_type>::value, |
| 1709 | void |
| 1710 | >::type |
| 1711 | seed(_Sseq& __q) |
| 1712 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 1713 | |
| 1714 | // generating functions |
| 1715 | result_type operator()(); |
| 1716 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 1717 | |
| 1718 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1719 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1720 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1721 | friend |
| 1722 | bool |
| 1723 | operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1724 | _B, _T, _C, _L, _F>& __x, |
| 1725 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1726 | _B, _T, _C, _L, _F>& __y); |
| 1727 | |
| 1728 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1729 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1730 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1731 | friend |
| 1732 | bool |
| 1733 | operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1734 | _B, _T, _C, _L, _F>& __x, |
| 1735 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1736 | _B, _T, _C, _L, _F>& __y); |
| 1737 | |
| 1738 | template <class _CharT, class _Traits, |
| 1739 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1740 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1741 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1742 | friend |
| 1743 | basic_ostream<_CharT, _Traits>& |
| 1744 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1745 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1746 | _B, _T, _C, _L, _F>& __x); |
| 1747 | |
| 1748 | template <class _CharT, class _Traits, |
| 1749 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1750 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1751 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1752 | friend |
| 1753 | basic_istream<_CharT, _Traits>& |
| 1754 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1755 | mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1756 | _B, _T, _C, _L, _F>& __x); |
| 1757 | private: |
| 1758 | |
| 1759 | template<class _Sseq> |
| 1760 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 1761 | template<class _Sseq> |
| 1762 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 1763 | |
| 1764 | template <size_t __count> |
| 1765 | static |
| 1766 | typename enable_if |
| 1767 | < |
| 1768 | __count < __w, |
| 1769 | result_type |
| 1770 | >::type |
| 1771 | __lshift(result_type __x) {return (__x << __count) & _Max;} |
| 1772 | |
| 1773 | template <size_t __count> |
| 1774 | static |
| 1775 | typename enable_if |
| 1776 | < |
| 1777 | (__count >= __w), |
| 1778 | result_type |
| 1779 | >::type |
| 1780 | __lshift(result_type __x) {return result_type(0);} |
| 1781 | |
| 1782 | template <size_t __count> |
| 1783 | static |
| 1784 | typename enable_if |
| 1785 | < |
| 1786 | __count < _Dt, |
| 1787 | result_type |
| 1788 | >::type |
| 1789 | __rshift(result_type __x) {return __x >> __count;} |
| 1790 | |
| 1791 | template <size_t __count> |
| 1792 | static |
| 1793 | typename enable_if |
| 1794 | < |
| 1795 | (__count >= _Dt), |
| 1796 | result_type |
| 1797 | >::type |
| 1798 | __rshift(result_type __x) {return result_type(0);} |
| 1799 | }; |
| 1800 | |
| 1801 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1802 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1803 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1804 | void |
| 1805 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 1806 | __t, __c, __l, __f>::seed(result_type __sd) |
| 1807 | { // __w >= 2 |
| 1808 | __x_[0] = __sd & _Max; |
| 1809 | for (size_t __i = 1; __i < __n; ++__i) |
| 1810 | __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max; |
| 1811 | __i_ = 0; |
| 1812 | } |
| 1813 | |
| 1814 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1815 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1816 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1817 | template<class _Sseq> |
| 1818 | void |
| 1819 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 1820 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>) |
| 1821 | { |
| 1822 | const unsigned __k = 1; |
| 1823 | uint32_t __ar[__n * __k]; |
| 1824 | __q.generate(__ar, __ar + __n * __k); |
| 1825 | for (size_t __i = 0; __i < __n; ++__i) |
| 1826 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 1827 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 1828 | (result_type(1) << __r) - result_type(1); |
| 1829 | __i_ = 0; |
| 1830 | if ((__x_[0] & ~__mask) == 0) |
| 1831 | { |
| 1832 | for (size_t __i = 1; __i < __n; ++__i) |
| 1833 | if (__x_[__i] != 0) |
| 1834 | return; |
| 1835 | __x_[0] = _Max; |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1840 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1841 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1842 | template<class _Sseq> |
| 1843 | void |
| 1844 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 1845 | __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>) |
| 1846 | { |
| 1847 | const unsigned __k = 2; |
| 1848 | uint32_t __ar[__n * __k]; |
| 1849 | __q.generate(__ar, __ar + __n * __k); |
| 1850 | for (size_t __i = 0; __i < __n; ++__i) |
| 1851 | __x_[__i] = static_cast<result_type>( |
| 1852 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 1853 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 1854 | (result_type(1) << __r) - result_type(1); |
| 1855 | __i_ = 0; |
| 1856 | if ((__x_[0] & ~__mask) == 0) |
| 1857 | { |
| 1858 | for (size_t __i = 1; __i < __n; ++__i) |
| 1859 | if (__x_[__i] != 0) |
| 1860 | return; |
| 1861 | __x_[0] = _Max; |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r, |
| 1866 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, |
| 1867 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f> |
| 1868 | _UIntType |
| 1869 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, |
| 1870 | __t, __c, __l, __f>::operator()() |
| 1871 | { |
| 1872 | const size_t __j = (__i_ + 1) % __n; |
| 1873 | const result_type __mask = __r == _Dt ? result_type(~0) : |
| 1874 | (result_type(1) << __r) - result_type(1); |
| 1875 | const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask); |
| 1876 | const size_t __k = (__i_ + __m) % __n; |
| 1877 | __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1)); |
| 1878 | result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d); |
| 1879 | __i_ = __j; |
| 1880 | __z ^= __lshift<__s>(__z) & __b; |
| 1881 | __z ^= __lshift<__t>(__z) & __c; |
| 1882 | return __z ^ __rshift<__l>(__z); |
| 1883 | } |
| 1884 | |
| 1885 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1886 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1887 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1888 | bool |
| 1889 | operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1890 | _B, _T, _C, _L, _F>& __x, |
| 1891 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1892 | _B, _T, _C, _L, _F>& __y) |
| 1893 | { |
| 1894 | if (__x.__i_ == __y.__i_) |
| 1895 | return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_); |
| 1896 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 1897 | { |
| 1898 | size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_); |
| 1899 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
| 1900 | __y.__x_ + __y.__i_)) |
| 1901 | return false; |
| 1902 | if (__x.__i_ == 0) |
| 1903 | return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_); |
| 1904 | return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j); |
| 1905 | } |
| 1906 | if (__x.__i_ < __y.__i_) |
| 1907 | { |
| 1908 | size_t __j = _N - __y.__i_; |
| 1909 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
| 1910 | __y.__x_ + __y.__i_)) |
| 1911 | return false; |
| 1912 | if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N, |
| 1913 | __y.__x_)) |
| 1914 | return false; |
| 1915 | return _STD::equal(__x.__x_, __x.__x_ + __x.__i_, |
| 1916 | __y.__x_ + (_N - (__x.__i_ + __j))); |
| 1917 | } |
| 1918 | size_t __j = _N - __x.__i_; |
| 1919 | if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
| 1920 | __x.__x_ + __x.__i_)) |
| 1921 | return false; |
| 1922 | if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N, |
| 1923 | __x.__x_)) |
| 1924 | return false; |
| 1925 | return _STD::equal(__y.__x_, __y.__x_ + __y.__i_, |
| 1926 | __x.__x_ + (_N - (__y.__i_ + __j))); |
| 1927 | } |
| 1928 | |
| 1929 | template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1930 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1931 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1932 | inline |
| 1933 | bool |
| 1934 | operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1935 | _B, _T, _C, _L, _F>& __x, |
| 1936 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1937 | _B, _T, _C, _L, _F>& __y) |
| 1938 | { |
| 1939 | return !(__x == __y); |
| 1940 | } |
| 1941 | |
| 1942 | template <class _CharT, class _Traits, |
| 1943 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1944 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1945 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1946 | basic_ostream<_CharT, _Traits>& |
| 1947 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 1948 | const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1949 | _B, _T, _C, _L, _F>& __x) |
| 1950 | { |
| 1951 | __save_flags<_CharT, _Traits> _(__os); |
| 1952 | __os.flags(ios_base::dec | ios_base::left); |
| 1953 | _CharT __sp = __os.widen(' '); |
| 1954 | __os.fill(__sp); |
| 1955 | __os << __x.__x_[__x.__i_]; |
| 1956 | for (size_t __j = __x.__i_ + 1; __j < _N; ++__j) |
| 1957 | __os << __sp << __x.__x_[__j]; |
| 1958 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 1959 | __os << __sp << __x.__x_[__j]; |
| 1960 | return __os; |
| 1961 | } |
| 1962 | |
| 1963 | template <class _CharT, class _Traits, |
| 1964 | class _UI, size_t _W, size_t _N, size_t _M, size_t _R, |
| 1965 | _UI _A, size_t _U, _UI _D, size_t _S, |
| 1966 | _UI _B, size_t _T, _UI _C, size_t _L, _UI _F> |
| 1967 | basic_istream<_CharT, _Traits>& |
| 1968 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 1969 | mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S, |
| 1970 | _B, _T, _C, _L, _F>& __x) |
| 1971 | { |
| 1972 | __save_flags<_CharT, _Traits> _(__is); |
| 1973 | __is.flags(ios_base::dec | ios_base::skipws); |
| 1974 | _UI __t[_N]; |
| 1975 | for (size_t __i = 0; __i < _N; ++__i) |
| 1976 | __is >> __t[__i]; |
| 1977 | if (!__is.fail()) |
| 1978 | { |
| 1979 | for (size_t __i = 0; __i < _N; ++__i) |
| 1980 | __x.__x_[__i] = __t[__i]; |
| 1981 | __x.__i_ = 0; |
| 1982 | } |
| 1983 | return __is; |
| 1984 | } |
| 1985 | |
| 1986 | typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, |
| 1987 | 0x9908b0df, 11, 0xffffffff, |
| 1988 | 7, 0x9d2c5680, |
| 1989 | 15, 0xefc60000, |
| 1990 | 18, 1812433253> mt19937; |
| 1991 | typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31, |
| 1992 | 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL, |
| 1993 | 17, 0x71d67fffeda60000ULL, |
| 1994 | 37, 0xfff7eee000000000ULL, |
| 1995 | 43, 6364136223846793005ULL> mt19937_64; |
| 1996 | |
| 1997 | // subtract_with_carry_engine |
| 1998 | |
| 1999 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2000 | class subtract_with_carry_engine; |
| 2001 | |
| 2002 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 2003 | bool |
| 2004 | operator==( |
| 2005 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2006 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
| 2007 | |
| 2008 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 2009 | bool |
| 2010 | operator!=( |
| 2011 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2012 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
| 2013 | |
| 2014 | template <class _CharT, class _Traits, |
| 2015 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2016 | basic_ostream<_CharT, _Traits>& |
| 2017 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2018 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
| 2019 | |
| 2020 | template <class _CharT, class _Traits, |
| 2021 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2022 | basic_istream<_CharT, _Traits>& |
| 2023 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2024 | subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
| 2025 | |
| 2026 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2027 | class subtract_with_carry_engine |
| 2028 | { |
| 2029 | public: |
| 2030 | // types |
| 2031 | typedef _UIntType result_type; |
| 2032 | |
| 2033 | private: |
| 2034 | result_type __x_[__r]; |
| 2035 | result_type __c_; |
| 2036 | size_t __i_; |
| 2037 | |
| 2038 | static const result_type _Dt = numeric_limits<result_type>::digits; |
| 2039 | static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters"); |
| 2040 | static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters"); |
| 2041 | static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters"); |
| 2042 | static_assert(__s < __r, "subtract_with_carry_engine invalid parameters"); |
| 2043 | public: |
| 2044 | static const result_type _Min = 0; |
| 2045 | static const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2046 | (result_type(1) << __w) - result_type(1); |
| 2047 | static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters"); |
| 2048 | |
| 2049 | // engine characteristics |
| 2050 | static const/*expr*/ size_t word_size = __w; |
| 2051 | static const/*expr*/ size_t short_lag = __s; |
| 2052 | static const/*expr*/ size_t long_lag = __r; |
| 2053 | static const/*expr*/ result_type min() { return _Min; } |
| 2054 | static const/*expr*/ result_type max() { return _Max; } |
| 2055 | static const/*expr*/ result_type default_seed = 19780503u; |
| 2056 | |
| 2057 | // constructors and seeding functions |
| 2058 | explicit subtract_with_carry_engine(result_type __sd = default_seed) |
| 2059 | {seed(__sd);} |
| 2060 | template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q) |
| 2061 | {seed(__q);} |
| 2062 | void seed(result_type __sd = default_seed) |
| 2063 | {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2064 | template<class _Sseq> |
| 2065 | typename enable_if |
| 2066 | < |
| 2067 | !is_convertible<_Sseq, result_type>::value, |
| 2068 | void |
| 2069 | >::type |
| 2070 | seed(_Sseq& __q) |
| 2071 | {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());} |
| 2072 | |
| 2073 | // generating functions |
| 2074 | result_type operator()(); |
| 2075 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2076 | |
| 2077 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 2078 | friend |
| 2079 | bool |
| 2080 | operator==( |
| 2081 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2082 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
| 2083 | |
| 2084 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 2085 | friend |
| 2086 | bool |
| 2087 | operator!=( |
| 2088 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2089 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y); |
| 2090 | |
| 2091 | template <class _CharT, class _Traits, |
| 2092 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2093 | friend |
| 2094 | basic_ostream<_CharT, _Traits>& |
| 2095 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2096 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
| 2097 | |
| 2098 | template <class _CharT, class _Traits, |
| 2099 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2100 | friend |
| 2101 | basic_istream<_CharT, _Traits>& |
| 2102 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2103 | subtract_with_carry_engine<_UI, _W, _S, _R>& __x); |
| 2104 | |
| 2105 | private: |
| 2106 | |
| 2107 | void seed(result_type __sd, integral_constant<unsigned, 1>); |
| 2108 | void seed(result_type __sd, integral_constant<unsigned, 2>); |
| 2109 | template<class _Sseq> |
| 2110 | void __seed(_Sseq& __q, integral_constant<unsigned, 1>); |
| 2111 | template<class _Sseq> |
| 2112 | void __seed(_Sseq& __q, integral_constant<unsigned, 2>); |
| 2113 | }; |
| 2114 | |
| 2115 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2116 | void |
| 2117 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 2118 | integral_constant<unsigned, 1>) |
| 2119 | { |
| 2120 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 2121 | __e(__sd == 0u ? default_seed : __sd); |
| 2122 | for (size_t __i = 0; __i < __r; ++__i) |
| 2123 | __x_[__i] = static_cast<result_type>(__e() & _Max); |
| 2124 | __c_ = __x_[__r-1] == 0; |
| 2125 | __i_ = 0; |
| 2126 | } |
| 2127 | |
| 2128 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2129 | void |
| 2130 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, |
| 2131 | integral_constant<unsigned, 2>) |
| 2132 | { |
| 2133 | linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
| 2134 | __e(__sd == 0u ? default_seed : __sd); |
| 2135 | for (size_t __i = 0; __i < __r; ++__i) |
| 2136 | __x_[__i] = static_cast<result_type>( |
| 2137 | (__e() + ((uint64_t)__e() << 32)) & _Max); |
| 2138 | __c_ = __x_[__r-1] == 0; |
| 2139 | __i_ = 0; |
| 2140 | } |
| 2141 | |
| 2142 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2143 | template<class _Sseq> |
| 2144 | void |
| 2145 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 2146 | integral_constant<unsigned, 1>) |
| 2147 | { |
| 2148 | const unsigned __k = 1; |
| 2149 | uint32_t __ar[__r * __k]; |
| 2150 | __q.generate(__ar, __ar + __r * __k); |
| 2151 | for (size_t __i = 0; __i < __r; ++__i) |
| 2152 | __x_[__i] = static_cast<result_type>(__ar[__i] & _Max); |
| 2153 | __c_ = __x_[__r-1] == 0; |
| 2154 | __i_ = 0; |
| 2155 | } |
| 2156 | |
| 2157 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2158 | template<class _Sseq> |
| 2159 | void |
| 2160 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, |
| 2161 | integral_constant<unsigned, 2>) |
| 2162 | { |
| 2163 | const unsigned __k = 2; |
| 2164 | uint32_t __ar[__r * __k]; |
| 2165 | __q.generate(__ar, __ar + __r * __k); |
| 2166 | for (size_t __i = 0; __i < __r; ++__i) |
| 2167 | __x_[__i] = static_cast<result_type>( |
| 2168 | (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max); |
| 2169 | __c_ = __x_[__r-1] == 0; |
| 2170 | __i_ = 0; |
| 2171 | } |
| 2172 | |
| 2173 | template<class _UIntType, size_t __w, size_t __s, size_t __r> |
| 2174 | _UIntType |
| 2175 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()() |
| 2176 | { |
| 2177 | const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r]; |
| 2178 | result_type& __xr = __x_[__i_]; |
| 2179 | result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1; |
| 2180 | __xr = (__xs - __xr - __c_) & _Max; |
| 2181 | __c_ = __new_c; |
| 2182 | __i_ = (__i_ + 1) % __r; |
| 2183 | return __xr; |
| 2184 | } |
| 2185 | |
| 2186 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 2187 | bool |
| 2188 | operator==( |
| 2189 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2190 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y) |
| 2191 | { |
| 2192 | if (__x.__c_ != __y.__c_) |
| 2193 | return false; |
| 2194 | if (__x.__i_ == __y.__i_) |
| 2195 | return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_); |
| 2196 | if (__x.__i_ == 0 || __y.__i_ == 0) |
| 2197 | { |
| 2198 | size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_); |
| 2199 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, |
| 2200 | __y.__x_ + __y.__i_)) |
| 2201 | return false; |
| 2202 | if (__x.__i_ == 0) |
| 2203 | return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_); |
| 2204 | return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j); |
| 2205 | } |
| 2206 | if (__x.__i_ < __y.__i_) |
| 2207 | { |
| 2208 | size_t __j = _R - __y.__i_; |
| 2209 | if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), |
| 2210 | __y.__x_ + __y.__i_)) |
| 2211 | return false; |
| 2212 | if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R, |
| 2213 | __y.__x_)) |
| 2214 | return false; |
| 2215 | return _STD::equal(__x.__x_, __x.__x_ + __x.__i_, |
| 2216 | __y.__x_ + (_R - (__x.__i_ + __j))); |
| 2217 | } |
| 2218 | size_t __j = _R - __x.__i_; |
| 2219 | if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), |
| 2220 | __x.__x_ + __x.__i_)) |
| 2221 | return false; |
| 2222 | if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R, |
| 2223 | __x.__x_)) |
| 2224 | return false; |
| 2225 | return _STD::equal(__y.__x_, __y.__x_ + __y.__i_, |
| 2226 | __x.__x_ + (_R - (__y.__i_ + __j))); |
| 2227 | } |
| 2228 | |
| 2229 | template<class _UI, size_t _W, size_t _S, size_t _R> |
| 2230 | inline |
| 2231 | bool |
| 2232 | operator!=( |
| 2233 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x, |
| 2234 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __y) |
| 2235 | { |
| 2236 | return !(__x == __y); |
| 2237 | } |
| 2238 | |
| 2239 | template <class _CharT, class _Traits, |
| 2240 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2241 | basic_ostream<_CharT, _Traits>& |
| 2242 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2243 | const subtract_with_carry_engine<_UI, _W, _S, _R>& __x) |
| 2244 | { |
| 2245 | __save_flags<_CharT, _Traits> _(__os); |
| 2246 | __os.flags(ios_base::dec | ios_base::left); |
| 2247 | _CharT __sp = __os.widen(' '); |
| 2248 | __os.fill(__sp); |
| 2249 | __os << __x.__x_[__x.__i_]; |
| 2250 | for (size_t __j = __x.__i_ + 1; __j < _R; ++__j) |
| 2251 | __os << __sp << __x.__x_[__j]; |
| 2252 | for (size_t __j = 0; __j < __x.__i_; ++__j) |
| 2253 | __os << __sp << __x.__x_[__j]; |
| 2254 | __os << __sp << __x.__c_; |
| 2255 | return __os; |
| 2256 | } |
| 2257 | |
| 2258 | template <class _CharT, class _Traits, |
| 2259 | class _UI, size_t _W, size_t _S, size_t _R> |
| 2260 | basic_istream<_CharT, _Traits>& |
| 2261 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2262 | subtract_with_carry_engine<_UI, _W, _S, _R>& __x) |
| 2263 | { |
| 2264 | __save_flags<_CharT, _Traits> _(__is); |
| 2265 | __is.flags(ios_base::dec | ios_base::skipws); |
| 2266 | _UI __t[_R+1]; |
| 2267 | for (size_t __i = 0; __i < _R+1; ++__i) |
| 2268 | __is >> __t[__i]; |
| 2269 | if (!__is.fail()) |
| 2270 | { |
| 2271 | for (size_t __i = 0; __i < _R; ++__i) |
| 2272 | __x.__x_[__i] = __t[__i]; |
| 2273 | __x.__c_ = __t[_R]; |
| 2274 | __x.__i_ = 0; |
| 2275 | } |
| 2276 | return __is; |
| 2277 | } |
| 2278 | |
| 2279 | typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base; |
| 2280 | typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base; |
| 2281 | |
| 2282 | // discard_block_engine |
| 2283 | |
| 2284 | template<class _Engine, size_t __p, size_t __r> |
| 2285 | class discard_block_engine |
| 2286 | { |
| 2287 | _Engine __e_; |
| 2288 | int __n_; |
| 2289 | |
| 2290 | static_assert( 0 < __r, "discard_block_engine invalid parameters"); |
| 2291 | static_assert(__r <= __p, "discard_block_engine invalid parameters"); |
| 2292 | public: |
| 2293 | // types |
| 2294 | typedef typename _Engine::result_type result_type; |
| 2295 | |
| 2296 | // engine characteristics |
| 2297 | static const/*expr*/ size_t block_size = __p; |
| 2298 | static const/*expr*/ size_t used_block = __r; |
| 2299 | |
| 2300 | // Temporary work around for lack of constexpr |
| 2301 | static const result_type _Min = _Engine::_Min; |
| 2302 | static const result_type _Max = _Engine::_Max; |
| 2303 | |
| 2304 | static const/*expr*/ result_type min() { return _Engine::min(); } |
| 2305 | static const/*expr*/ result_type max() { return _Engine::max(); } |
| 2306 | |
| 2307 | // constructors and seeding functions |
| 2308 | discard_block_engine() : __n_(0) {} |
| 2309 | // explicit discard_block_engine(const _Engine& __e); |
| 2310 | // explicit discard_block_engine(_Engine&& __e); |
| 2311 | explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {} |
| 2312 | template<class _Sseq> explicit discard_block_engine(_Sseq& __q) |
| 2313 | : __e_(__q), __n_(0) {} |
| 2314 | void seed() {__e_.seed(); __n_ = 0;} |
| 2315 | void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;} |
| 2316 | template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;} |
| 2317 | |
| 2318 | // generating functions |
| 2319 | result_type operator()(); |
| 2320 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2321 | |
| 2322 | // property functions |
| 2323 | const _Engine& base() const {return __e_;} |
| 2324 | |
| 2325 | template<class _Eng, size_t _P, size_t _R> |
| 2326 | friend |
| 2327 | bool |
| 2328 | operator==( |
| 2329 | const discard_block_engine<_Eng, _P, _R>& __x, |
| 2330 | const discard_block_engine<_Eng, _P, _R>& __y); |
| 2331 | |
| 2332 | template<class _Eng, size_t _P, size_t _R> |
| 2333 | friend |
| 2334 | bool |
| 2335 | operator!=( |
| 2336 | const discard_block_engine<_Eng, _P, _R>& __x, |
| 2337 | const discard_block_engine<_Eng, _P, _R>& __y); |
| 2338 | |
| 2339 | template <class _CharT, class _Traits, |
| 2340 | class _Eng, size_t _P, size_t _R> |
| 2341 | friend |
| 2342 | basic_ostream<_CharT, _Traits>& |
| 2343 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2344 | const discard_block_engine<_Eng, _P, _R>& __x); |
| 2345 | |
| 2346 | template <class _CharT, class _Traits, |
| 2347 | class _Eng, size_t _P, size_t _R> |
| 2348 | friend |
| 2349 | basic_istream<_CharT, _Traits>& |
| 2350 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2351 | discard_block_engine<_Eng, _P, _R>& __x); |
| 2352 | }; |
| 2353 | |
| 2354 | template<class _Engine, size_t __p, size_t __r> |
| 2355 | typename discard_block_engine<_Engine, __p, __r>::result_type |
| 2356 | discard_block_engine<_Engine, __p, __r>::operator()() |
| 2357 | { |
| 2358 | if (__n_ >= __r) |
| 2359 | { |
| 2360 | __e_.discard(__p - __r); |
| 2361 | __n_ = 0; |
| 2362 | } |
| 2363 | ++__n_; |
| 2364 | return __e_(); |
| 2365 | } |
| 2366 | |
| 2367 | template<class _Eng, size_t _P, size_t _R> |
| 2368 | inline |
| 2369 | bool |
| 2370 | operator==(const discard_block_engine<_Eng, _P, _R>& __x, |
| 2371 | const discard_block_engine<_Eng, _P, _R>& __y) |
| 2372 | { |
| 2373 | return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_; |
| 2374 | } |
| 2375 | |
| 2376 | template<class _Eng, size_t _P, size_t _R> |
| 2377 | inline |
| 2378 | bool |
| 2379 | operator!=(const discard_block_engine<_Eng, _P, _R>& __x, |
| 2380 | const discard_block_engine<_Eng, _P, _R>& __y) |
| 2381 | { |
| 2382 | return !(__x == __y); |
| 2383 | } |
| 2384 | |
| 2385 | template <class _CharT, class _Traits, |
| 2386 | class _Eng, size_t _P, size_t _R> |
| 2387 | basic_ostream<_CharT, _Traits>& |
| 2388 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2389 | const discard_block_engine<_Eng, _P, _R>& __x) |
| 2390 | { |
| 2391 | __save_flags<_CharT, _Traits> _(__os); |
| 2392 | __os.flags(ios_base::dec | ios_base::left); |
| 2393 | _CharT __sp = __os.widen(' '); |
| 2394 | __os.fill(__sp); |
| 2395 | return __os << __x.__e_ << __sp << __x.__n_; |
| 2396 | } |
| 2397 | |
| 2398 | template <class _CharT, class _Traits, |
| 2399 | class _Eng, size_t _P, size_t _R> |
| 2400 | basic_istream<_CharT, _Traits>& |
| 2401 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2402 | discard_block_engine<_Eng, _P, _R>& __x) |
| 2403 | { |
| 2404 | __save_flags<_CharT, _Traits> _(__is); |
| 2405 | __is.flags(ios_base::dec | ios_base::skipws); |
| 2406 | _Eng __e; |
| 2407 | int __n; |
| 2408 | __is >> __e >> __n; |
| 2409 | if (!__is.fail()) |
| 2410 | { |
| 2411 | __x.__e_ = __e; |
| 2412 | __x.__n_ = __n; |
| 2413 | } |
| 2414 | return __is; |
| 2415 | } |
| 2416 | |
| 2417 | typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24; |
| 2418 | typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48; |
| 2419 | |
| 2420 | // independent_bits_engine |
| 2421 | |
| 2422 | template <unsigned long long _X, size_t _R> |
| 2423 | struct __log2_imp |
| 2424 | { |
| 2425 | static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R |
| 2426 | : __log2_imp<_X, _R - 1>::value; |
| 2427 | }; |
| 2428 | |
| 2429 | template <unsigned long long _X> |
| 2430 | struct __log2_imp<_X, 0> |
| 2431 | { |
| 2432 | static const size_t value = 0; |
| 2433 | }; |
| 2434 | |
| 2435 | template <size_t _R> |
| 2436 | struct __log2_imp<0, _R> |
| 2437 | { |
| 2438 | static const size_t value = _R + 1; |
| 2439 | }; |
| 2440 | |
| 2441 | template <class _UI, _UI _X> |
| 2442 | struct __log2 |
| 2443 | { |
| 2444 | static const size_t value = __log2_imp<_X, |
| 2445 | sizeof(_UI) * __CHAR_BIT__ - 1>::value; |
| 2446 | }; |
| 2447 | |
| 2448 | template<class _Engine, size_t __w, class _UIntType> |
| 2449 | class independent_bits_engine |
| 2450 | { |
| 2451 | template <class _UI, _UI _R0, size_t _W, size_t _M> |
| 2452 | class __get_n |
| 2453 | { |
| 2454 | static const size_t _Dt = numeric_limits<_UI>::digits; |
| 2455 | static const size_t _N = _W / _M + (_W % _M != 0); |
| 2456 | static const size_t _W0 = _W / _N; |
| 2457 | static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0; |
| 2458 | public: |
| 2459 | static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N; |
| 2460 | }; |
| 2461 | public: |
| 2462 | // types |
| 2463 | typedef _UIntType result_type; |
| 2464 | |
| 2465 | private: |
| 2466 | _Engine __e_; |
| 2467 | |
| 2468 | static const result_type _Dt = numeric_limits<result_type>::digits; |
| 2469 | static_assert( 0 < __w, "independent_bits_engine invalid parameters"); |
| 2470 | static_assert(__w <= _Dt, "independent_bits_engine invalid parameters"); |
| 2471 | |
| 2472 | typedef typename _Engine::result_type _Engine_result_type; |
| 2473 | typedef typename conditional |
| 2474 | < |
| 2475 | sizeof(_Engine_result_type) <= sizeof(result_type), |
| 2476 | result_type, |
| 2477 | _Engine_result_type |
| 2478 | >::type _Working_result_type; |
| 2479 | // Temporary work around for lack of constexpr |
| 2480 | static const _Working_result_type _R = _Engine::_Max - _Engine::_Min |
| 2481 | + _Working_result_type(1); |
| 2482 | static const size_t __m = __log2<_Working_result_type, _R>::value; |
| 2483 | static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value; |
| 2484 | static const size_t __w0 = __w / __n; |
| 2485 | static const size_t __n0 = __n - __w % __n; |
| 2486 | static const size_t _WDt = numeric_limits<_Working_result_type>::digits; |
| 2487 | static const size_t _EDt = numeric_limits<_Engine_result_type>::digits; |
| 2488 | static const _Working_result_type __y0 = __w0 >= _WDt ? 0 : |
| 2489 | (_R >> __w0) << __w0; |
| 2490 | static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : |
| 2491 | (_R >> (__w0+1)) << (__w0+1); |
| 2492 | static const _Engine_result_type __mask0 = __w0 > 0 ? |
| 2493 | _Engine_result_type(~0) >> (_EDt - __w0) : |
| 2494 | _Engine_result_type(0); |
| 2495 | static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ? |
| 2496 | _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : |
| 2497 | _Engine_result_type(~0); |
| 2498 | public: |
| 2499 | static const result_type _Min = 0; |
| 2500 | static const result_type _Max = __w == _Dt ? result_type(~0) : |
| 2501 | (result_type(1) << __w) - result_type(1); |
| 2502 | static_assert(_Min < _Max, "independent_bits_engine invalid parameters"); |
| 2503 | |
| 2504 | // engine characteristics |
| 2505 | static const/*expr*/ result_type min() { return _Min; } |
| 2506 | static const/*expr*/ result_type max() { return _Max; } |
| 2507 | |
| 2508 | // constructors and seeding functions |
| 2509 | independent_bits_engine() {} |
| 2510 | // explicit independent_bits_engine(const _Engine& __e); |
| 2511 | // explicit independent_bits_engine(_Engine&& __e); |
| 2512 | explicit independent_bits_engine(result_type __sd) : __e_(__sd) {} |
| 2513 | template<class _Sseq> explicit independent_bits_engine(_Sseq& __q) |
| 2514 | : __e_(__q) {} |
| 2515 | void seed() {__e_.seed();} |
| 2516 | void seed(result_type __sd) {__e_.seed(__sd);} |
| 2517 | template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);} |
| 2518 | |
| 2519 | // generating functions |
| 2520 | result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} |
| 2521 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2522 | |
| 2523 | // property functions |
| 2524 | const _Engine& base() const {return __e_;} |
| 2525 | |
| 2526 | template<class _Eng, size_t _W, class _UI> |
| 2527 | friend |
| 2528 | bool |
| 2529 | operator==( |
| 2530 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 2531 | const independent_bits_engine<_Eng, _W, _UI>& __y); |
| 2532 | |
| 2533 | template<class _Eng, size_t _W, class _UI> |
| 2534 | friend |
| 2535 | bool |
| 2536 | operator!=( |
| 2537 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 2538 | const independent_bits_engine<_Eng, _W, _UI>& __y); |
| 2539 | |
| 2540 | template <class _CharT, class _Traits, |
| 2541 | class _Eng, size_t _W, class _UI> |
| 2542 | friend |
| 2543 | basic_ostream<_CharT, _Traits>& |
| 2544 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2545 | const independent_bits_engine<_Eng, _W, _UI>& __x); |
| 2546 | |
| 2547 | template <class _CharT, class _Traits, |
| 2548 | class _Eng, size_t _W, class _UI> |
| 2549 | friend |
| 2550 | basic_istream<_CharT, _Traits>& |
| 2551 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2552 | independent_bits_engine<_Eng, _W, _UI>& __x); |
| 2553 | |
| 2554 | private: |
| 2555 | result_type __eval(false_type); |
| 2556 | result_type __eval(true_type); |
| 2557 | |
| 2558 | template <size_t __count> |
| 2559 | static |
| 2560 | typename enable_if |
| 2561 | < |
| 2562 | __count < _Dt, |
| 2563 | result_type |
| 2564 | >::type |
| 2565 | __lshift(result_type __x) {return __x << __count;} |
| 2566 | |
| 2567 | template <size_t __count> |
| 2568 | static |
| 2569 | typename enable_if |
| 2570 | < |
| 2571 | (__count >= _Dt), |
| 2572 | result_type |
| 2573 | >::type |
| 2574 | __lshift(result_type __x) {return result_type(0);} |
| 2575 | }; |
| 2576 | |
| 2577 | template<class _Engine, size_t __w, class _UIntType> |
| 2578 | inline |
| 2579 | _UIntType |
| 2580 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) |
| 2581 | { |
| 2582 | return static_cast<result_type>(__e_() & __mask0); |
| 2583 | } |
| 2584 | |
| 2585 | template<class _Engine, size_t __w, class _UIntType> |
| 2586 | _UIntType |
| 2587 | independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) |
| 2588 | { |
| 2589 | result_type _S = 0; |
| 2590 | for (size_t __k = 0; __k < __n0; ++__k) |
| 2591 | { |
| 2592 | _Engine_result_type __u; |
| 2593 | do |
| 2594 | { |
| 2595 | __u = __e_() - _Engine::min(); |
| 2596 | } while (__u >= __y0); |
| 2597 | _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0)); |
| 2598 | } |
| 2599 | for (size_t __k = __n0; __k < __n; ++__k) |
| 2600 | { |
| 2601 | _Engine_result_type __u; |
| 2602 | do |
| 2603 | { |
| 2604 | __u = __e_() - _Engine::min(); |
| 2605 | } while (__u >= __y1); |
| 2606 | _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1)); |
| 2607 | } |
| 2608 | return _S; |
| 2609 | } |
| 2610 | |
| 2611 | template<class _Eng, size_t _W, class _UI> |
| 2612 | inline |
| 2613 | bool |
| 2614 | operator==( |
| 2615 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 2616 | const independent_bits_engine<_Eng, _W, _UI>& __y) |
| 2617 | { |
| 2618 | return __x.base() == __y.base(); |
| 2619 | } |
| 2620 | |
| 2621 | template<class _Eng, size_t _W, class _UI> |
| 2622 | inline |
| 2623 | bool |
| 2624 | operator!=( |
| 2625 | const independent_bits_engine<_Eng, _W, _UI>& __x, |
| 2626 | const independent_bits_engine<_Eng, _W, _UI>& __y) |
| 2627 | { |
| 2628 | return !(__x == __y); |
| 2629 | } |
| 2630 | |
| 2631 | template <class _CharT, class _Traits, |
| 2632 | class _Eng, size_t _W, class _UI> |
| 2633 | basic_ostream<_CharT, _Traits>& |
| 2634 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2635 | const independent_bits_engine<_Eng, _W, _UI>& __x) |
| 2636 | { |
| 2637 | return __os << __x.base(); |
| 2638 | } |
| 2639 | |
| 2640 | template <class _CharT, class _Traits, |
| 2641 | class _Eng, size_t _W, class _UI> |
| 2642 | basic_istream<_CharT, _Traits>& |
| 2643 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2644 | independent_bits_engine<_Eng, _W, _UI>& __x) |
| 2645 | { |
| 2646 | _Eng __e; |
| 2647 | __is >> __e; |
| 2648 | if (!__is.fail()) |
| 2649 | __x.__e_ = __e; |
| 2650 | return __is; |
| 2651 | } |
| 2652 | |
| 2653 | // shuffle_order_engine |
| 2654 | |
| 2655 | template <uint64_t _Xp, uint64_t _Yp> |
| 2656 | struct __ugcd |
| 2657 | { |
| 2658 | static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value; |
| 2659 | }; |
| 2660 | |
| 2661 | template <uint64_t _Xp> |
| 2662 | struct __ugcd<_Xp, 0> |
| 2663 | { |
| 2664 | static const uint64_t value = _Xp; |
| 2665 | }; |
| 2666 | |
| 2667 | template <uint64_t _N, uint64_t _D> |
| 2668 | class __uratio |
| 2669 | { |
| 2670 | static_assert(_D != 0, "__uratio divide by 0"); |
| 2671 | static const uint64_t __gcd = __ugcd<_N, _D>::value; |
| 2672 | public: |
| 2673 | static const uint64_t num = _N / __gcd; |
| 2674 | static const uint64_t den = _D / __gcd; |
| 2675 | |
| 2676 | typedef __uratio<num, den> type; |
| 2677 | }; |
| 2678 | |
| 2679 | template<class _Engine, size_t __k> |
| 2680 | class shuffle_order_engine |
| 2681 | { |
| 2682 | static_assert(0 < __k, "shuffle_order_engine invalid parameters"); |
| 2683 | public: |
| 2684 | // types |
| 2685 | typedef typename _Engine::result_type result_type; |
| 2686 | |
| 2687 | private: |
| 2688 | _Engine __e_; |
| 2689 | result_type _V_[__k]; |
| 2690 | result_type _Y_; |
| 2691 | |
| 2692 | public: |
| 2693 | // engine characteristics |
| 2694 | static const/*expr*/ size_t table_size = __k; |
| 2695 | |
| 2696 | static const result_type _Min = _Engine::_Min; |
| 2697 | static const result_type _Max = _Engine::_Max; |
| 2698 | static_assert(_Min < _Max, "shuffle_order_engine invalid parameters"); |
| 2699 | static const/*expr*/ result_type min() { return _Min; } |
| 2700 | static const/*expr*/ result_type max() { return _Max; } |
| 2701 | |
| 2702 | static const unsigned long long _R = _Max - _Min + 1ull; |
| 2703 | |
| 2704 | // constructors and seeding functions |
| 2705 | shuffle_order_engine() {__init();} |
| 2706 | // explicit shuffle_order_engine(const _Engine& __e); |
| 2707 | // explicit shuffle_order_engine(_Engine&& e); |
| 2708 | explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();} |
| 2709 | template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q) |
| 2710 | : __e_(__q) {__init();} |
| 2711 | void seed() {__e_.seed(); __init();} |
| 2712 | void seed(result_type __sd) {__e_.seed(__sd); __init();} |
| 2713 | template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();} |
| 2714 | |
| 2715 | // generating functions |
| 2716 | result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} |
| 2717 | void discard(unsigned long long __z) {for (; __z; --__z) operator()();} |
| 2718 | |
| 2719 | // property functions |
| 2720 | const _Engine& base() const {return __e_;} |
| 2721 | |
| 2722 | private: |
| 2723 | template<class _Eng, size_t _K> |
| 2724 | friend |
| 2725 | bool |
| 2726 | operator==( |
| 2727 | const shuffle_order_engine<_Eng, _K>& __x, |
| 2728 | const shuffle_order_engine<_Eng, _K>& __y); |
| 2729 | |
| 2730 | template<class _Eng, size_t _K> |
| 2731 | friend |
| 2732 | bool |
| 2733 | operator!=( |
| 2734 | const shuffle_order_engine<_Eng, _K>& __x, |
| 2735 | const shuffle_order_engine<_Eng, _K>& __y); |
| 2736 | |
| 2737 | template <class _CharT, class _Traits, |
| 2738 | class _Eng, size_t _K> |
| 2739 | friend |
| 2740 | basic_ostream<_CharT, _Traits>& |
| 2741 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2742 | const shuffle_order_engine<_Eng, _K>& __x); |
| 2743 | |
| 2744 | template <class _CharT, class _Traits, |
| 2745 | class _Eng, size_t _K> |
| 2746 | friend |
| 2747 | basic_istream<_CharT, _Traits>& |
| 2748 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2749 | shuffle_order_engine<_Eng, _K>& __x); |
| 2750 | |
| 2751 | void __init() |
| 2752 | { |
| 2753 | for (size_t __i = 0; __i < __k; ++__i) |
| 2754 | _V_[__i] = __e_(); |
| 2755 | _Y_ = __e_(); |
| 2756 | } |
| 2757 | |
| 2758 | result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());} |
| 2759 | result_type __eval(true_type) {return __eval(__uratio<__k, _R>());} |
| 2760 | |
| 2761 | result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());} |
| 2762 | result_type __eval2(true_type) {return __evalf<__k, 0>();} |
| 2763 | |
| 2764 | template <uint64_t _N, uint64_t _D> |
| 2765 | typename enable_if |
| 2766 | < |
| 2767 | (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)), |
| 2768 | result_type |
| 2769 | >::type |
| 2770 | __eval(__uratio<_N, _D>) |
| 2771 | {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();} |
| 2772 | |
| 2773 | template <uint64_t _N, uint64_t _D> |
| 2774 | typename enable_if |
| 2775 | < |
| 2776 | __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min), |
| 2777 | result_type |
| 2778 | >::type |
| 2779 | __eval(__uratio<_N, _D>) |
| 2780 | { |
| 2781 | const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min) |
| 2782 | / __uratio<_N, _D>::den); |
| 2783 | _Y_ = _V_[__j]; |
| 2784 | _V_[__j] = __e_(); |
| 2785 | return _Y_; |
| 2786 | } |
| 2787 | |
| 2788 | template <uint64_t __n, uint64_t __d> |
| 2789 | result_type __evalf() |
| 2790 | { |
| 2791 | const double _F = __d == 0 ? |
| 2792 | __n / (2. * 0x8000000000000000ull) : |
| 2793 | __n / (double)__d; |
| 2794 | const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min)); |
| 2795 | _Y_ = _V_[__j]; |
| 2796 | _V_[__j] = __e_(); |
| 2797 | return _Y_; |
| 2798 | } |
| 2799 | }; |
| 2800 | |
| 2801 | template<class _Eng, size_t _K> |
| 2802 | bool |
| 2803 | operator==( |
| 2804 | const shuffle_order_engine<_Eng, _K>& __x, |
| 2805 | const shuffle_order_engine<_Eng, _K>& __y) |
| 2806 | { |
| 2807 | return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) && |
| 2808 | __x.__e_ == __y.__e_; |
| 2809 | } |
| 2810 | |
| 2811 | template<class _Eng, size_t _K> |
| 2812 | inline |
| 2813 | bool |
| 2814 | operator!=( |
| 2815 | const shuffle_order_engine<_Eng, _K>& __x, |
| 2816 | const shuffle_order_engine<_Eng, _K>& __y) |
| 2817 | { |
| 2818 | return !(__x == __y); |
| 2819 | } |
| 2820 | |
| 2821 | template <class _CharT, class _Traits, |
| 2822 | class _Eng, size_t _K> |
| 2823 | basic_ostream<_CharT, _Traits>& |
| 2824 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 2825 | const shuffle_order_engine<_Eng, _K>& __x) |
| 2826 | { |
| 2827 | __save_flags<_CharT, _Traits> _(__os); |
| 2828 | __os.flags(ios_base::dec | ios_base::left); |
| 2829 | _CharT __sp = __os.widen(' '); |
| 2830 | __os.fill(__sp); |
| 2831 | __os << __x.__e_ << __sp << __x._V_[0]; |
| 2832 | for (size_t __i = 1; __i < _K; ++__i) |
| 2833 | __os << __sp << __x._V_[__i]; |
| 2834 | return __os << __sp << __x._Y_; |
| 2835 | } |
| 2836 | |
| 2837 | template <class _CharT, class _Traits, |
| 2838 | class _Eng, size_t _K> |
| 2839 | basic_istream<_CharT, _Traits>& |
| 2840 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 2841 | shuffle_order_engine<_Eng, _K>& __x) |
| 2842 | { |
| 2843 | typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type; |
| 2844 | __save_flags<_CharT, _Traits> _(__is); |
| 2845 | __is.flags(ios_base::dec | ios_base::skipws); |
| 2846 | _Eng __e; |
| 2847 | result_type _V[_K+1]; |
| 2848 | __is >> __e; |
| 2849 | for (size_t __i = 0; __i < _K+1; ++__i) |
| 2850 | __is >> _V[__i]; |
| 2851 | if (!__is.fail()) |
| 2852 | { |
| 2853 | __x.__e_ = __e; |
| 2854 | for (size_t __i = 0; __i < _K; ++__i) |
| 2855 | __x._V_[__i] = _V[__i]; |
| 2856 | __x._Y_ = _V[_K]; |
| 2857 | } |
| 2858 | return __is; |
| 2859 | } |
| 2860 | |
| 2861 | typedef shuffle_order_engine<minstd_rand0, 256> knuth_b; |
| 2862 | |
| 2863 | // random_device |
| 2864 | |
| 2865 | class random_device |
| 2866 | { |
| 2867 | int __f_; |
| 2868 | public: |
| 2869 | // types |
| 2870 | typedef unsigned result_type; |
| 2871 | |
| 2872 | // generator characteristics |
| 2873 | static const result_type _Min = 0; |
| 2874 | static const result_type _Max = 0xFFFFFFFFu; |
| 2875 | |
| 2876 | static const/*expr*/ result_type min() { return _Min;} |
| 2877 | static const/*expr*/ result_type max() { return _Max;} |
| 2878 | |
| 2879 | // constructors |
| 2880 | explicit random_device(const string& __token = "/dev/urandom"); |
| 2881 | ~random_device(); |
| 2882 | |
| 2883 | // generating functions |
| 2884 | result_type operator()(); |
| 2885 | |
| 2886 | // property functions |
| 2887 | double entropy() const; |
| 2888 | |
| 2889 | private: |
| 2890 | // no copy functions |
| 2891 | random_device(const random_device&); // = delete; |
| 2892 | random_device& operator=(const random_device&); // = delete; |
| 2893 | }; |
| 2894 | |
| 2895 | // seed_seq |
| 2896 | |
| 2897 | class seed_seq |
| 2898 | { |
| 2899 | public: |
| 2900 | // types |
| 2901 | typedef uint32_t result_type; |
| 2902 | |
| 2903 | private: |
| 2904 | vector<result_type> __v_; |
| 2905 | |
| 2906 | template<class _InputIterator> |
| 2907 | void init(_InputIterator __first, _InputIterator __last); |
| 2908 | public: |
| 2909 | // constructors |
| 2910 | seed_seq() {} |
| 2911 | template<class _Tp> |
| 2912 | seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());} |
| 2913 | |
| 2914 | template<class _InputIterator> |
| 2915 | seed_seq(_InputIterator __first, _InputIterator __last) |
| 2916 | {init(__first, __last);} |
| 2917 | |
| 2918 | // generating functions |
| 2919 | template<class _RandomAccessIterator> |
| 2920 | void generate(_RandomAccessIterator __first, _RandomAccessIterator __last); |
| 2921 | |
| 2922 | // property functions |
| 2923 | size_t size() const {return __v_.size();} |
| 2924 | template<class _OutputIterator> |
| 2925 | void param(_OutputIterator __dest) const |
| 2926 | {_STD::copy(__v_.begin(), __v_.end(), __dest);} |
| 2927 | |
| 2928 | private: |
| 2929 | // no copy functions |
| 2930 | seed_seq(const seed_seq&); // = delete; |
| 2931 | void operator=(const seed_seq&); // = delete; |
| 2932 | |
| 2933 | static result_type _T(result_type __x) {return __x ^ (__x >> 27);} |
| 2934 | }; |
| 2935 | |
| 2936 | template<class _InputIterator> |
| 2937 | void |
| 2938 | seed_seq::init(_InputIterator __first, _InputIterator __last) |
| 2939 | { |
| 2940 | for (_InputIterator __s = __first; __s != __last; ++__s) |
| 2941 | __v_.push_back(*__s & 0xFFFFFFFF); |
| 2942 | } |
| 2943 | |
| 2944 | template<class _RandomAccessIterator> |
| 2945 | void |
| 2946 | seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last) |
| 2947 | { |
| 2948 | if (__first != __last) |
| 2949 | { |
| 2950 | _STD::fill(__first, __last, 0x8b8b8b8b); |
| 2951 | const size_t __n = static_cast<size_t>(__last - __first); |
| 2952 | const size_t __s = __v_.size(); |
| 2953 | const size_t __t = (__n >= 623) ? 11 |
| 2954 | : (__n >= 68) ? 7 |
| 2955 | : (__n >= 39) ? 5 |
| 2956 | : (__n >= 7) ? 3 |
| 2957 | : (__n - 1) / 2; |
| 2958 | const size_t __p = (__n - __t) / 2; |
| 2959 | const size_t __q = __p + __t; |
| 2960 | const size_t __m = _STD::max(__s + 1, __n); |
| 2961 | // __k = 0; |
| 2962 | { |
| 2963 | result_type __r = 1664525 * _T(__first[0] ^ __first[__p] |
| 2964 | ^ __first[__n - 1]); |
| 2965 | __first[__p] += __r; |
| 2966 | __r += __s; |
| 2967 | __first[__q] += __r; |
| 2968 | __first[0] = __r; |
| 2969 | } |
| 2970 | for (size_t __k = 1; __k <= __s; ++__k) |
| 2971 | { |
| 2972 | const size_t __kmodn = __k % __n; |
| 2973 | const size_t __kpmodn = (__k + __p) % __n; |
| 2974 | result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn] |
| 2975 | ^ __first[(__k - 1) % __n]); |
| 2976 | __first[__kpmodn] += __r; |
| 2977 | __r += __kmodn + __v_[__k-1]; |
| 2978 | __first[(__k + __q) % __n] += __r; |
| 2979 | __first[__kmodn] = __r; |
| 2980 | } |
| 2981 | for (size_t __k = __s + 1; __k < __m; ++__k) |
| 2982 | { |
| 2983 | const size_t __kmodn = __k % __n; |
| 2984 | const size_t __kpmodn = (__k + __p) % __n; |
| 2985 | result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn] |
| 2986 | ^ __first[(__k - 1) % __n]); |
| 2987 | __first[__kpmodn] += __r; |
| 2988 | __r += __kmodn; |
| 2989 | __first[(__k + __q) % __n] += __r; |
| 2990 | __first[__kmodn] = __r; |
| 2991 | } |
| 2992 | for (size_t __k = __m; __k < __m + __n; ++__k) |
| 2993 | { |
| 2994 | const size_t __kmodn = __k % __n; |
| 2995 | const size_t __kpmodn = (__k + __p) % __n; |
| 2996 | result_type __r = 1566083941 * _T(__first[__kmodn] + |
| 2997 | __first[__kpmodn] + |
| 2998 | __first[(__k - 1) % __n]); |
| 2999 | __first[__kpmodn] ^= __r; |
| 3000 | __r -= __kmodn; |
| 3001 | __first[(__k + __q) % __n] ^= __r; |
| 3002 | __first[__kmodn] = __r; |
| 3003 | } |
| 3004 | } |
| 3005 | } |
| 3006 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3007 | // generate_canonical |
| 3008 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3009 | template<class _RealType, size_t __bits, class _URNG> |
| 3010 | _RealType |
| 3011 | generate_canonical(_URNG& __g) |
| 3012 | { |
| 3013 | const size_t _Dt = numeric_limits<_RealType>::digits; |
| 3014 | const size_t __b = _Dt < __bits ? _Dt : __bits; |
| 3015 | const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value; |
| 3016 | const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0); |
| 3017 | const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1); |
| 3018 | _RealType __base = _R; |
| 3019 | _RealType _S = __g() - _URNG::_Min; |
| 3020 | for (size_t __i = 1; __i < __k; ++__i, __base *= _R) |
| 3021 | _S += (__g() - _URNG::_Min) * __base; |
| 3022 | return _S / __base; |
| 3023 | } |
| 3024 | |
| 3025 | // __independent_bits_engine |
| 3026 | |
| 3027 | template<class _Engine, class _UIntType> |
| 3028 | class __independent_bits_engine |
| 3029 | { |
| 3030 | public: |
| 3031 | // types |
| 3032 | typedef _UIntType result_type; |
| 3033 | |
| 3034 | private: |
| 3035 | typedef typename _Engine::result_type _Engine_result_type; |
| 3036 | typedef typename conditional |
| 3037 | < |
| 3038 | sizeof(_Engine_result_type) <= sizeof(result_type), |
| 3039 | result_type, |
| 3040 | _Engine_result_type |
| 3041 | >::type _Working_result_type; |
| 3042 | |
| 3043 | _Engine& __e_; |
| 3044 | size_t __w_; |
| 3045 | size_t __w0_; |
| 3046 | size_t __n_; |
| 3047 | size_t __n0_; |
| 3048 | _Working_result_type __y0_; |
| 3049 | _Working_result_type __y1_; |
| 3050 | _Engine_result_type __mask0_; |
| 3051 | _Engine_result_type __mask1_; |
| 3052 | |
| 3053 | static const _Working_result_type _R = _Engine::_Max - _Engine::_Min |
| 3054 | + _Working_result_type(1); |
| 3055 | static const size_t __m = __log2<_Working_result_type, _R>::value; |
| 3056 | static const size_t _WDt = numeric_limits<_Working_result_type>::digits; |
| 3057 | static const size_t _EDt = numeric_limits<_Engine_result_type>::digits; |
| 3058 | |
| 3059 | public: |
| 3060 | // constructors and seeding functions |
| 3061 | __independent_bits_engine(_Engine& __e, size_t __w); |
| 3062 | |
| 3063 | // generating functions |
| 3064 | result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} |
| 3065 | |
| 3066 | private: |
| 3067 | result_type __eval(false_type); |
| 3068 | result_type __eval(true_type); |
| 3069 | }; |
| 3070 | |
| 3071 | template<class _Engine, class _UIntType> |
| 3072 | __independent_bits_engine<_Engine, _UIntType> |
| 3073 | ::__independent_bits_engine(_Engine& __e, size_t __w) |
| 3074 | : __e_(__e), |
| 3075 | __w_(__w) |
| 3076 | { |
| 3077 | __n_ = __w_ / __m + (__w_ % __m != 0); |
| 3078 | __w0_ = __w_ / __n_; |
| 3079 | if (_R == 0) |
| 3080 | __y0_ = _R; |
| 3081 | else if (__w0_ < _WDt) |
| 3082 | __y0_ = (_R >> __w0_) << __w0_; |
| 3083 | else |
| 3084 | __y0_ = 0; |
| 3085 | if (_R - __y0_ > __y0_ / __n_) |
| 3086 | { |
| 3087 | ++__n_; |
| 3088 | __w0_ = __w_ / __n_; |
| 3089 | if (__w0_ < _WDt) |
| 3090 | __y0_ = (_R >> __w0_) << __w0_; |
| 3091 | else |
| 3092 | __y0_ = 0; |
| 3093 | } |
| 3094 | __n0_ = __n_ - __w_ % __n_; |
| 3095 | if (__w0_ < _WDt - 1) |
| 3096 | __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1); |
| 3097 | else |
| 3098 | __y1_ = 0; |
| 3099 | __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) : |
| 3100 | _Engine_result_type(0); |
| 3101 | __mask1_ = __w0_ < _EDt - 1 ? |
| 3102 | _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) : |
| 3103 | _Engine_result_type(~0); |
| 3104 | } |
| 3105 | |
| 3106 | template<class _Engine, class _UIntType> |
| 3107 | inline |
| 3108 | _UIntType |
| 3109 | __independent_bits_engine<_Engine, _UIntType>::__eval(false_type) |
| 3110 | { |
| 3111 | return static_cast<result_type>(__e_() & __mask0_); |
| 3112 | } |
| 3113 | |
| 3114 | template<class _Engine, class _UIntType> |
| 3115 | _UIntType |
| 3116 | __independent_bits_engine<_Engine, _UIntType>::__eval(true_type) |
| 3117 | { |
| 3118 | result_type _S = 0; |
| 3119 | for (size_t __k = 0; __k < __n0_; ++__k) |
| 3120 | { |
| 3121 | _Engine_result_type __u; |
| 3122 | do |
| 3123 | { |
| 3124 | __u = __e_() - _Engine::min(); |
| 3125 | } while (__u >= __y0_); |
| 3126 | if (__w0_ < _EDt) |
| 3127 | _S <<= __w0_; |
| 3128 | else |
| 3129 | _S = 0; |
| 3130 | _S += __u & __mask0_; |
| 3131 | } |
| 3132 | for (size_t __k = __n0_; __k < __n_; ++__k) |
| 3133 | { |
| 3134 | _Engine_result_type __u; |
| 3135 | do |
| 3136 | { |
| 3137 | __u = __e_() - _Engine::min(); |
| 3138 | } while (__u >= __y1_); |
| 3139 | if (__w0_ < _EDt - 1) |
| 3140 | _S <<= __w0_ + 1; |
| 3141 | else |
| 3142 | _S = 0; |
| 3143 | _S += __u & __mask1_; |
| 3144 | } |
| 3145 | return _S; |
| 3146 | } |
| 3147 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3148 | // uniform_int_distribution |
| 3149 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3150 | template<class _IntType = int> |
| 3151 | class uniform_int_distribution |
| 3152 | { |
| 3153 | public: |
| 3154 | // types |
| 3155 | typedef _IntType result_type; |
| 3156 | |
| 3157 | class param_type |
| 3158 | { |
| 3159 | result_type __a_; |
| 3160 | result_type __b_; |
| 3161 | public: |
| 3162 | typedef uniform_int_distribution distribution_type; |
| 3163 | |
| 3164 | explicit param_type(result_type __a = 0, |
| 3165 | result_type __b = numeric_limits<result_type>::max()) |
| 3166 | : __a_(__a), __b_(__b) {} |
| 3167 | |
| 3168 | result_type a() const {return __a_;} |
| 3169 | result_type b() const {return __b_;} |
| 3170 | |
| 3171 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3172 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
| 3173 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3174 | {return !(__x == __y);} |
| 3175 | }; |
| 3176 | |
| 3177 | private: |
| 3178 | param_type __p_; |
| 3179 | |
| 3180 | public: |
| 3181 | // constructors and reset functions |
| 3182 | explicit uniform_int_distribution(result_type __a = 0, |
| 3183 | result_type __b = numeric_limits<result_type>::max()) |
| 3184 | : __p_(param_type(__a, __b)) {} |
| 3185 | explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {} |
| 3186 | void reset() {} |
| 3187 | |
| 3188 | // generating functions |
| 3189 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3190 | {return (*this)(__g, __p_);} |
| 3191 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3192 | |
| 3193 | // property functions |
| 3194 | result_type a() const {return __p_.a();} |
| 3195 | result_type b() const {return __p_.b();} |
| 3196 | |
| 3197 | param_type param() const {return __p_;} |
| 3198 | void param(const param_type& __p) {__p_ = __p;} |
| 3199 | |
| 3200 | result_type min() const {return a();} |
| 3201 | result_type max() const {return b();} |
| 3202 | |
| 3203 | friend bool operator==(const uniform_int_distribution& __x, |
| 3204 | const uniform_int_distribution& __y) |
| 3205 | {return __x.__p_ == __y.__p_;} |
| 3206 | friend bool operator!=(const uniform_int_distribution& __x, |
| 3207 | const uniform_int_distribution& __y) |
| 3208 | {return !(__x == __y);} |
| 3209 | }; |
| 3210 | |
| 3211 | template<class _IntType> |
| 3212 | template<class _URNG> |
| 3213 | typename uniform_int_distribution<_IntType>::result_type |
| 3214 | uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) |
| 3215 | { |
| 3216 | typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t), |
| 3217 | uint32_t, uint64_t>::type _UIntType; |
| 3218 | const _UIntType _R = __p.b() - __p.a() + _UIntType(1); |
| 3219 | if (_R == 1) |
| 3220 | return __p.a(); |
| 3221 | const size_t _Dt = numeric_limits<_UIntType>::digits; |
| 3222 | typedef __independent_bits_engine<_URNG, _UIntType> _Eng; |
| 3223 | if (_R == 0) |
| 3224 | return static_cast<result_type>(_Eng(__g, _Dt)()); |
| 3225 | size_t __w = _Dt - __clz(_R) - 1; |
| 3226 | if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0) |
| 3227 | ++__w; |
| 3228 | _Eng __e(__g, __w); |
| 3229 | _UIntType __u; |
| 3230 | do |
| 3231 | { |
| 3232 | __u = __e(); |
| 3233 | } while (__u >= _R); |
| 3234 | return static_cast<result_type>(__u + __p.a()); |
| 3235 | } |
| 3236 | |
| 3237 | template <class _CharT, class _Traits, class _IT> |
| 3238 | basic_ostream<_CharT, _Traits>& |
| 3239 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3240 | const uniform_int_distribution<_IT>& __x) |
| 3241 | { |
| 3242 | __save_flags<_CharT, _Traits> _(__os); |
| 3243 | __os.flags(ios_base::dec | ios_base::left); |
| 3244 | _CharT __sp = __os.widen(' '); |
| 3245 | __os.fill(__sp); |
| 3246 | return __os << __x.a() << __sp << __x.b(); |
| 3247 | } |
| 3248 | |
| 3249 | template <class _CharT, class _Traits, class _IT> |
| 3250 | basic_istream<_CharT, _Traits>& |
| 3251 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3252 | uniform_int_distribution<_IT>& __x) |
| 3253 | { |
| 3254 | typedef uniform_int_distribution<_IT> _Eng; |
| 3255 | typedef typename _Eng::result_type result_type; |
| 3256 | typedef typename _Eng::param_type param_type; |
| 3257 | __save_flags<_CharT, _Traits> _(__is); |
| 3258 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3259 | result_type __a; |
| 3260 | result_type __b; |
| 3261 | __is >> __a >> __b; |
| 3262 | if (!__is.fail()) |
| 3263 | __x.param(param_type(__a, __b)); |
| 3264 | return __is; |
| 3265 | } |
| 3266 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3267 | // uniform_real_distribution |
| 3268 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3269 | template<class _RealType = double> |
| 3270 | class uniform_real_distribution |
| 3271 | { |
| 3272 | public: |
| 3273 | // types |
| 3274 | typedef _RealType result_type; |
| 3275 | |
| 3276 | class param_type |
| 3277 | { |
| 3278 | result_type __a_; |
| 3279 | result_type __b_; |
| 3280 | public: |
| 3281 | typedef uniform_real_distribution distribution_type; |
| 3282 | |
| 3283 | explicit param_type(result_type __a = 0, |
| 3284 | result_type __b = 1) |
| 3285 | : __a_(__a), __b_(__b) {} |
| 3286 | |
| 3287 | result_type a() const {return __a_;} |
| 3288 | result_type b() const {return __b_;} |
| 3289 | |
| 3290 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3291 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
| 3292 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3293 | {return !(__x == __y);} |
| 3294 | }; |
| 3295 | |
| 3296 | private: |
| 3297 | param_type __p_; |
| 3298 | |
| 3299 | public: |
| 3300 | // constructors and reset functions |
| 3301 | explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1) |
| 3302 | : __p_(param_type(__a, __b)) {} |
| 3303 | explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {} |
| 3304 | void reset() {} |
| 3305 | |
| 3306 | // generating functions |
| 3307 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3308 | {return (*this)(__g, __p_);} |
| 3309 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3310 | |
| 3311 | // property functions |
| 3312 | result_type a() const {return __p_.a();} |
| 3313 | result_type b() const {return __p_.b();} |
| 3314 | |
| 3315 | param_type param() const {return __p_;} |
| 3316 | void param(const param_type& __p) {__p_ = __p;} |
| 3317 | |
| 3318 | result_type min() const {return a();} |
| 3319 | result_type max() const {return b();} |
| 3320 | |
| 3321 | friend bool operator==(const uniform_real_distribution& __x, |
| 3322 | const uniform_real_distribution& __y) |
| 3323 | {return __x.__p_ == __y.__p_;} |
| 3324 | friend bool operator!=(const uniform_real_distribution& __x, |
| 3325 | const uniform_real_distribution& __y) |
| 3326 | {return !(__x == __y);} |
| 3327 | }; |
| 3328 | |
| 3329 | template<class _RealType> |
| 3330 | template<class _URNG> |
| 3331 | inline |
| 3332 | typename uniform_real_distribution<_RealType>::result_type |
| 3333 | uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3334 | { |
| 3335 | return (__p.b() - __p.a()) |
| 3336 | * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) |
| 3337 | + __p.a(); |
| 3338 | } |
| 3339 | |
| 3340 | template <class _CharT, class _Traits, class _RT> |
| 3341 | basic_ostream<_CharT, _Traits>& |
| 3342 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3343 | const uniform_real_distribution<_RT>& __x) |
| 3344 | { |
| 3345 | __save_flags<_CharT, _Traits> _(__os); |
| 3346 | __os.flags(ios_base::dec | ios_base::left); |
| 3347 | _CharT __sp = __os.widen(' '); |
| 3348 | __os.fill(__sp); |
| 3349 | return __os << __x.a() << __sp << __x.b(); |
| 3350 | } |
| 3351 | |
| 3352 | template <class _CharT, class _Traits, class _RT> |
| 3353 | basic_istream<_CharT, _Traits>& |
| 3354 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3355 | uniform_real_distribution<_RT>& __x) |
| 3356 | { |
| 3357 | typedef uniform_real_distribution<_RT> _Eng; |
| 3358 | typedef typename _Eng::result_type result_type; |
| 3359 | typedef typename _Eng::param_type param_type; |
| 3360 | __save_flags<_CharT, _Traits> _(__is); |
| 3361 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3362 | result_type __a; |
| 3363 | result_type __b; |
| 3364 | __is >> __a >> __b; |
| 3365 | if (!__is.fail()) |
| 3366 | __x.param(param_type(__a, __b)); |
| 3367 | return __is; |
| 3368 | } |
| 3369 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3370 | // bernoulli_distribution |
| 3371 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3372 | class bernoulli_distribution |
| 3373 | { |
| 3374 | public: |
| 3375 | // types |
| 3376 | typedef bool result_type; |
| 3377 | |
| 3378 | class param_type |
| 3379 | { |
| 3380 | double __p_; |
| 3381 | public: |
| 3382 | typedef bernoulli_distribution distribution_type; |
| 3383 | |
| 3384 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 3385 | |
| 3386 | double p() const {return __p_;} |
| 3387 | |
| 3388 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3389 | {return __x.__p_ == __y.__p_;} |
| 3390 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3391 | {return !(__x == __y);} |
| 3392 | }; |
| 3393 | |
| 3394 | private: |
| 3395 | param_type __p_; |
| 3396 | |
| 3397 | public: |
| 3398 | // constructors and reset functions |
| 3399 | explicit bernoulli_distribution(double __p = 0.5) |
| 3400 | : __p_(param_type(__p)) {} |
| 3401 | explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {} |
| 3402 | void reset() {} |
| 3403 | |
| 3404 | // generating functions |
| 3405 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3406 | {return (*this)(__g, __p_);} |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3407 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3408 | |
| 3409 | // property functions |
| 3410 | double p() const {return __p_.p();} |
| 3411 | |
| 3412 | param_type param() const {return __p_;} |
| 3413 | void param(const param_type& __p) {__p_ = __p;} |
| 3414 | |
| 3415 | result_type min() const {return false;} |
| 3416 | result_type max() const {return true;} |
| 3417 | |
| 3418 | friend bool operator==(const bernoulli_distribution& __x, |
| 3419 | const bernoulli_distribution& __y) |
| 3420 | {return __x.__p_ == __y.__p_;} |
| 3421 | friend bool operator!=(const bernoulli_distribution& __x, |
| 3422 | const bernoulli_distribution& __y) |
| 3423 | {return !(__x == __y);} |
| 3424 | }; |
| 3425 | |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3426 | template<class _URNG> |
| 3427 | inline |
| 3428 | bernoulli_distribution::result_type |
| 3429 | bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) |
| 3430 | { |
| 3431 | return (__g() - __g.min()) < __p.p() * (__g.max() - __g.min() + 1.); |
| 3432 | } |
| 3433 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3434 | template <class _CharT, class _Traits> |
| 3435 | basic_ostream<_CharT, _Traits>& |
| 3436 | operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) |
| 3437 | { |
| 3438 | __save_flags<_CharT, _Traits> _(__os); |
| 3439 | __os.flags(ios_base::dec | ios_base::left); |
| 3440 | _CharT __sp = __os.widen(' '); |
| 3441 | __os.fill(__sp); |
| 3442 | return __os << __x.p(); |
| 3443 | } |
| 3444 | |
| 3445 | template <class _CharT, class _Traits> |
| 3446 | basic_istream<_CharT, _Traits>& |
| 3447 | operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) |
| 3448 | { |
| 3449 | typedef bernoulli_distribution _Eng; |
| 3450 | typedef typename _Eng::param_type param_type; |
| 3451 | __save_flags<_CharT, _Traits> _(__is); |
| 3452 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3453 | double __p; |
| 3454 | __is >> __p; |
| 3455 | if (!__is.fail()) |
| 3456 | __x.param(param_type(__p)); |
| 3457 | return __is; |
| 3458 | } |
| 3459 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3460 | // binomial_distribution |
| 3461 | |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3462 | template<class _IntType = int> |
| 3463 | class binomial_distribution |
| 3464 | { |
| 3465 | public: |
| 3466 | // types |
| 3467 | typedef _IntType result_type; |
| 3468 | |
| 3469 | class param_type |
| 3470 | { |
| 3471 | result_type __t_; |
| 3472 | double __p_; |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3473 | double __pr_; |
| 3474 | double __odds_ratio_; |
| 3475 | result_type __r0_; |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3476 | public: |
| 3477 | typedef binomial_distribution distribution_type; |
| 3478 | |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3479 | explicit param_type(result_type __t = 1, double __p = 0.5); |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3480 | |
| 3481 | result_type t() const {return __t_;} |
| 3482 | double p() const {return __p_;} |
| 3483 | |
| 3484 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3485 | {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;} |
| 3486 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3487 | {return !(__x == __y);} |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3488 | |
| 3489 | friend class binomial_distribution; |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3490 | }; |
| 3491 | |
| 3492 | private: |
| 3493 | param_type __p_; |
| 3494 | |
| 3495 | public: |
| 3496 | // constructors and reset functions |
| 3497 | explicit binomial_distribution(result_type __t = 1, double __p = 0.5) |
| 3498 | : __p_(param_type(__t, __p)) {} |
| 3499 | explicit binomial_distribution(const param_type& __p) : __p_(__p) {} |
| 3500 | void reset() {} |
| 3501 | |
| 3502 | // generating functions |
| 3503 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3504 | {return (*this)(__g, __p_);} |
| 3505 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3506 | |
| 3507 | // property functions |
| 3508 | result_type t() const {return __p_.t();} |
| 3509 | double p() const {return __p_.p();} |
| 3510 | |
| 3511 | param_type param() const {return __p_;} |
| 3512 | void param(const param_type& __p) {__p_ = __p;} |
| 3513 | |
| 3514 | result_type min() const {return 0;} |
| 3515 | result_type max() const {return t();} |
| 3516 | |
| 3517 | friend bool operator==(const binomial_distribution& __x, |
| 3518 | const binomial_distribution& __y) |
| 3519 | {return __x.__p_ == __y.__p_;} |
| 3520 | friend bool operator!=(const binomial_distribution& __x, |
| 3521 | const binomial_distribution& __y) |
| 3522 | {return !(__x == __y);} |
| 3523 | }; |
| 3524 | |
| 3525 | template<class _IntType> |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3526 | binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) |
| 3527 | : __t_(__t), __p_(__p) |
| 3528 | { |
| 3529 | if (0 < __p_ && __p_ < 1) |
| 3530 | { |
| 3531 | __r0_ = static_cast<result_type>((__t_ + 1) * __p_); |
| 3532 | __pr_ = _STD::exp(_STD::lgamma(__t_ + 1.) - _STD::lgamma(__r0_ + 1.) - |
| 3533 | _STD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _STD::log(__p_) + |
| 3534 | (__t_ - __r0_) * _STD::log(1 - __p_)); |
| 3535 | __odds_ratio_ = __p_ / (1 - __p_); |
| 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | template<class _IntType> |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3540 | template<class _URNG> |
| 3541 | _IntType |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3542 | binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3543 | { |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3544 | if (__pr.__t_ == 0 || __pr.__p_ == 0) |
| 3545 | return 0; |
| 3546 | if (__pr.__p_ == 1) |
| 3547 | return __pr.__t_; |
| 3548 | uniform_real_distribution<double> __gen; |
| 3549 | double __u = __gen(__g) - __pr.__pr_; |
| 3550 | if (__u < 0) |
| 3551 | return __pr.__r0_; |
| 3552 | double __pu = __pr.__pr_; |
| 3553 | double __pd = __pu; |
| 3554 | result_type __ru = __pr.__r0_; |
| 3555 | result_type __rd = __ru; |
| 3556 | while (true) |
| 3557 | { |
| 3558 | if (__rd >= 1) |
| 3559 | { |
| 3560 | __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1)); |
| 3561 | __u -= __pd; |
| 3562 | if (__u < 0) |
| 3563 | return __rd - 1; |
| 3564 | } |
| 3565 | --__rd; |
| 3566 | ++__ru; |
| 3567 | if (__ru <= __pr.__t_) |
| 3568 | { |
| 3569 | __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru; |
| 3570 | __u -= __pu; |
| 3571 | if (__u < 0) |
| 3572 | return __ru; |
| 3573 | } |
| 3574 | } |
Howard Hinnant | 03aad81 | 2010-05-11 23:26:59 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | template <class _CharT, class _Traits, class _IntType> |
| 3578 | basic_ostream<_CharT, _Traits>& |
| 3579 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3580 | const binomial_distribution<_IntType>& __x) |
| 3581 | { |
| 3582 | __save_flags<_CharT, _Traits> _(__os); |
| 3583 | __os.flags(ios_base::dec | ios_base::left); |
| 3584 | _CharT __sp = __os.widen(' '); |
| 3585 | __os.fill(__sp); |
| 3586 | return __os << __x.t() << __sp << __x.p(); |
| 3587 | } |
| 3588 | |
| 3589 | template <class _CharT, class _Traits, class _IntType> |
| 3590 | basic_istream<_CharT, _Traits>& |
| 3591 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3592 | binomial_distribution<_IntType>& __x) |
| 3593 | { |
| 3594 | typedef binomial_distribution<_IntType> _Eng; |
| 3595 | typedef typename _Eng::result_type result_type; |
| 3596 | typedef typename _Eng::param_type param_type; |
| 3597 | __save_flags<_CharT, _Traits> _(__is); |
| 3598 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3599 | result_type __t; |
| 3600 | double __p; |
| 3601 | __is >> __t >> __p; |
| 3602 | if (!__is.fail()) |
| 3603 | __x.param(param_type(__t, __p)); |
| 3604 | return __is; |
| 3605 | } |
| 3606 | |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3607 | // exponential_distribution |
| 3608 | |
| 3609 | template<class _RealType = double> |
| 3610 | class exponential_distribution |
| 3611 | { |
| 3612 | public: |
| 3613 | // types |
| 3614 | typedef _RealType result_type; |
| 3615 | |
| 3616 | class param_type |
| 3617 | { |
| 3618 | result_type __lambda_; |
| 3619 | public: |
| 3620 | typedef exponential_distribution distribution_type; |
| 3621 | |
| 3622 | explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {} |
| 3623 | |
| 3624 | result_type lambda() const {return __lambda_;} |
| 3625 | |
| 3626 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3627 | {return __x.__lambda_ == __y.__lambda_;} |
| 3628 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3629 | {return !(__x == __y);} |
| 3630 | }; |
| 3631 | |
| 3632 | private: |
| 3633 | param_type __p_; |
| 3634 | |
| 3635 | public: |
| 3636 | // constructors and reset functions |
| 3637 | explicit exponential_distribution(result_type __lambda = 1) |
| 3638 | : __p_(param_type(__lambda)) {} |
| 3639 | explicit exponential_distribution(const param_type& __p) : __p_(__p) {} |
| 3640 | void reset() {} |
| 3641 | |
| 3642 | // generating functions |
| 3643 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3644 | {return (*this)(__g, __p_);} |
| 3645 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3646 | |
| 3647 | // property functions |
| 3648 | result_type lambda() const {return __p_.lambda();} |
| 3649 | |
| 3650 | param_type param() const {return __p_;} |
| 3651 | void param(const param_type& __p) {__p_ = __p;} |
| 3652 | |
| 3653 | result_type min() const {return 0;} |
Howard Hinnant | df40dc6 | 2010-05-16 17:56:20 +0000 | [diff] [blame] | 3654 | result_type max() const {return numeric_limits<result_type>::infinity();} |
Howard Hinnant | 30a840f | 2010-05-12 17:08:57 +0000 | [diff] [blame] | 3655 | |
| 3656 | friend bool operator==(const exponential_distribution& __x, |
| 3657 | const exponential_distribution& __y) |
| 3658 | {return __x.__p_ == __y.__p_;} |
| 3659 | friend bool operator!=(const exponential_distribution& __x, |
| 3660 | const exponential_distribution& __y) |
| 3661 | {return !(__x == __y);} |
| 3662 | }; |
| 3663 | |
| 3664 | template <class _RealType> |
| 3665 | template<class _URNG> |
| 3666 | _RealType |
| 3667 | exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3668 | { |
| 3669 | return -_STD::log |
| 3670 | ( |
| 3671 | result_type(1) - |
| 3672 | _STD::generate_canonical<result_type, |
| 3673 | numeric_limits<result_type>::digits>(__g) |
| 3674 | ) |
| 3675 | / __p.lambda(); |
| 3676 | } |
| 3677 | |
| 3678 | template <class _CharT, class _Traits, class _RealType> |
| 3679 | basic_ostream<_CharT, _Traits>& |
| 3680 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3681 | const exponential_distribution<_RealType>& __x) |
| 3682 | { |
| 3683 | __save_flags<_CharT, _Traits> _(__os); |
| 3684 | __os.flags(ios_base::dec | ios_base::left); |
| 3685 | return __os << __x.lambda(); |
| 3686 | } |
| 3687 | |
| 3688 | template <class _CharT, class _Traits, class _RealType> |
| 3689 | basic_istream<_CharT, _Traits>& |
| 3690 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3691 | exponential_distribution<_RealType>& __x) |
| 3692 | { |
| 3693 | typedef exponential_distribution<_RealType> _Eng; |
| 3694 | typedef typename _Eng::result_type result_type; |
| 3695 | typedef typename _Eng::param_type param_type; |
| 3696 | __save_flags<_CharT, _Traits> _(__is); |
| 3697 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3698 | result_type __lambda; |
| 3699 | __is >> __lambda; |
| 3700 | if (!__is.fail()) |
| 3701 | __x.param(param_type(__lambda)); |
| 3702 | return __is; |
| 3703 | } |
| 3704 | |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3705 | // normal_distribution |
| 3706 | |
| 3707 | template<class _RealType = double> |
| 3708 | class normal_distribution |
| 3709 | { |
| 3710 | public: |
| 3711 | // types |
| 3712 | typedef _RealType result_type; |
| 3713 | |
| 3714 | class param_type |
| 3715 | { |
| 3716 | result_type __mean_; |
| 3717 | result_type __stddev_; |
| 3718 | public: |
| 3719 | typedef normal_distribution distribution_type; |
| 3720 | |
| 3721 | explicit param_type(result_type __mean = 0, result_type __stddev = 1) |
| 3722 | : __mean_(__mean), __stddev_(__stddev) {} |
| 3723 | |
| 3724 | result_type mean() const {return __mean_;} |
| 3725 | result_type stddev() const {return __stddev_;} |
| 3726 | |
| 3727 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3728 | {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;} |
| 3729 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3730 | {return !(__x == __y);} |
| 3731 | }; |
| 3732 | |
| 3733 | private: |
| 3734 | param_type __p_; |
| 3735 | result_type _V_; |
| 3736 | bool _V_hot_; |
| 3737 | |
| 3738 | public: |
| 3739 | // constructors and reset functions |
| 3740 | explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1) |
| 3741 | : __p_(param_type(__mean, __stddev)), _V_hot_(false) {} |
| 3742 | explicit normal_distribution(const param_type& __p) |
| 3743 | : __p_(__p), _V_hot_(false) {} |
| 3744 | void reset() {_V_hot_ = false;} |
| 3745 | |
| 3746 | // generating functions |
| 3747 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3748 | {return (*this)(__g, __p_);} |
| 3749 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 3750 | |
| 3751 | // property functions |
| 3752 | result_type mean() const {return __p_.mean();} |
| 3753 | result_type stddev() const {return __p_.stddev();} |
| 3754 | |
| 3755 | param_type param() const {return __p_;} |
| 3756 | void param(const param_type& __p) {__p_ = __p;} |
| 3757 | |
| 3758 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
| 3759 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 3760 | |
| 3761 | friend bool operator==(const normal_distribution& __x, |
| 3762 | const normal_distribution& __y) |
| 3763 | {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ && |
| 3764 | (!__x._V_hot_ || __x._V_ == __y._V_);} |
| 3765 | friend bool operator!=(const normal_distribution& __x, |
| 3766 | const normal_distribution& __y) |
| 3767 | {return !(__x == __y);} |
| 3768 | |
| 3769 | template <class _CharT, class _Traits, class _RT> |
| 3770 | friend |
| 3771 | basic_ostream<_CharT, _Traits>& |
| 3772 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3773 | const normal_distribution<_RT>& __x); |
| 3774 | |
| 3775 | template <class _CharT, class _Traits, class _RT> |
| 3776 | friend |
| 3777 | basic_istream<_CharT, _Traits>& |
| 3778 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3779 | normal_distribution<_RT>& __x); |
| 3780 | }; |
| 3781 | |
| 3782 | template <class _RealType> |
| 3783 | template<class _URNG> |
| 3784 | _RealType |
| 3785 | normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 3786 | { |
| 3787 | result_type _U; |
| 3788 | if (_V_hot_) |
| 3789 | { |
| 3790 | _V_hot_ = false; |
| 3791 | _U = _V_; |
| 3792 | } |
| 3793 | else |
| 3794 | { |
| 3795 | uniform_real_distribution<result_type> _Uni(-1, 1); |
| 3796 | result_type __u; |
| 3797 | result_type __v; |
| 3798 | result_type __s; |
| 3799 | do |
| 3800 | { |
| 3801 | __u = _Uni(__g); |
| 3802 | __v = _Uni(__g); |
| 3803 | __s = __u * __u + __v * __v; |
| 3804 | } while (__s > 1 || __s == 0); |
| 3805 | result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s); |
| 3806 | _V_ = __v * _F; |
| 3807 | _V_hot_ = true; |
| 3808 | _U = __u * _F; |
| 3809 | } |
| 3810 | return _U * __p.stddev() + __p.mean(); |
| 3811 | } |
| 3812 | |
| 3813 | template <class _CharT, class _Traits, class _RT> |
| 3814 | basic_ostream<_CharT, _Traits>& |
| 3815 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3816 | const normal_distribution<_RT>& __x) |
| 3817 | { |
| 3818 | __save_flags<_CharT, _Traits> _(__os); |
| 3819 | __os.flags(ios_base::dec | ios_base::left); |
| 3820 | _CharT __sp = __os.widen(' '); |
| 3821 | __os.fill(__sp); |
| 3822 | __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_; |
| 3823 | if (__x._V_hot_) |
| 3824 | __os << __sp << __x._V_; |
| 3825 | return __os; |
| 3826 | } |
| 3827 | |
| 3828 | template <class _CharT, class _Traits, class _RT> |
| 3829 | basic_istream<_CharT, _Traits>& |
| 3830 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3831 | normal_distribution<_RT>& __x) |
| 3832 | { |
| 3833 | typedef normal_distribution<_RT> _Eng; |
| 3834 | typedef typename _Eng::result_type result_type; |
| 3835 | typedef typename _Eng::param_type param_type; |
| 3836 | __save_flags<_CharT, _Traits> _(__is); |
| 3837 | __is.flags(ios_base::dec | ios_base::skipws); |
| 3838 | result_type __mean; |
| 3839 | result_type __stddev; |
| 3840 | result_type _V = 0; |
| 3841 | bool _V_hot = false; |
| 3842 | __is >> __mean >> __stddev >> _V_hot; |
| 3843 | if (_V_hot) |
| 3844 | __is >> _V; |
| 3845 | if (!__is.fail()) |
| 3846 | { |
| 3847 | __x.param(param_type(__mean, __stddev)); |
| 3848 | __x._V_hot_ = _V_hot; |
| 3849 | __x._V_ = _V; |
| 3850 | } |
| 3851 | return __is; |
| 3852 | } |
| 3853 | |
Howard Hinnant | 2bc36fc | 2010-05-17 18:31:53 +0000 | [diff] [blame^] | 3854 | // lognormal_distribution |
| 3855 | |
| 3856 | template<class _RealType = double> |
| 3857 | class lognormal_distribution |
| 3858 | { |
| 3859 | public: |
| 3860 | // types |
| 3861 | typedef _RealType result_type; |
| 3862 | |
| 3863 | class param_type |
| 3864 | { |
| 3865 | normal_distribution<result_type> __nd_; |
| 3866 | public: |
| 3867 | typedef lognormal_distribution distribution_type; |
| 3868 | |
| 3869 | explicit param_type(result_type __m = 0, result_type __s = 1) |
| 3870 | : __nd_(__m, __s) {} |
| 3871 | |
| 3872 | result_type m() const {return __nd_.mean();} |
| 3873 | result_type s() const {return __nd_.stddev();} |
| 3874 | |
| 3875 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3876 | {return __x.__nd_ == __y.__nd_;} |
| 3877 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3878 | {return !(__x == __y);} |
| 3879 | friend class lognormal_distribution; |
| 3880 | |
| 3881 | template <class _CharT, class _Traits, class _RT> |
| 3882 | friend |
| 3883 | basic_ostream<_CharT, _Traits>& |
| 3884 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3885 | const lognormal_distribution<_RT>& __x); |
| 3886 | |
| 3887 | template <class _CharT, class _Traits, class _RT> |
| 3888 | friend |
| 3889 | basic_istream<_CharT, _Traits>& |
| 3890 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3891 | lognormal_distribution<_RT>& __x); |
| 3892 | }; |
| 3893 | |
| 3894 | private: |
| 3895 | param_type __p_; |
| 3896 | |
| 3897 | public: |
| 3898 | // constructor and reset functions |
| 3899 | explicit lognormal_distribution(result_type __m = 0, result_type __s = 1) |
| 3900 | : __p_(param_type(__m, __s)) {} |
| 3901 | explicit lognormal_distribution(const param_type& __p) |
| 3902 | : __p_(__p) {} |
| 3903 | void reset() {__p_.__nd_.reset();} |
| 3904 | |
| 3905 | // generating functions |
| 3906 | template<class _URNG> result_type operator()(_URNG& __g) |
| 3907 | {return (*this)(__g, __p_);} |
| 3908 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p) |
| 3909 | {return _STD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));} |
| 3910 | |
| 3911 | // property functions |
| 3912 | result_type m() const {return __p_.m();} |
| 3913 | result_type s() const {return __p_.s();} |
| 3914 | |
| 3915 | param_type param() const {return __p_;} |
| 3916 | void param(const param_type& __p) {return __p_ = __p;} |
| 3917 | |
| 3918 | result_type min() const {return 0;} |
| 3919 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 3920 | |
| 3921 | friend bool operator==(const lognormal_distribution& __x, |
| 3922 | const lognormal_distribution& __y) |
| 3923 | {return __x.__p_ == __y.__p_;} |
| 3924 | friend bool operator!=(const lognormal_distribution& __x, |
| 3925 | const lognormal_distribution& __y) |
| 3926 | {return !(__x == __y);} |
| 3927 | |
| 3928 | template <class _CharT, class _Traits, class _RT> |
| 3929 | friend |
| 3930 | basic_ostream<_CharT, _Traits>& |
| 3931 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3932 | const lognormal_distribution<_RT>& __x); |
| 3933 | |
| 3934 | template <class _CharT, class _Traits, class _RT> |
| 3935 | friend |
| 3936 | basic_istream<_CharT, _Traits>& |
| 3937 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3938 | lognormal_distribution<_RT>& __x); |
| 3939 | }; |
| 3940 | |
| 3941 | template <class _CharT, class _Traits, class _RT> |
| 3942 | inline |
| 3943 | basic_ostream<_CharT, _Traits>& |
| 3944 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 3945 | const lognormal_distribution<_RT>& __x) |
| 3946 | { |
| 3947 | return __os << __x.__p_.__nd_; |
| 3948 | } |
| 3949 | |
| 3950 | template <class _CharT, class _Traits, class _RT> |
| 3951 | inline |
| 3952 | basic_istream<_CharT, _Traits>& |
| 3953 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 3954 | lognormal_distribution<_RT>& __x) |
| 3955 | { |
| 3956 | return __is >> __x.__p_.__nd_; |
| 3957 | } |
| 3958 | |
Howard Hinnant | 6add8dd | 2010-05-15 21:36:23 +0000 | [diff] [blame] | 3959 | // poisson_distribution |
| 3960 | |
| 3961 | template<class _IntType = int> |
| 3962 | class poisson_distribution |
| 3963 | { |
| 3964 | public: |
| 3965 | // types |
| 3966 | typedef _IntType result_type; |
| 3967 | |
| 3968 | class param_type |
| 3969 | { |
| 3970 | double __mean_; |
| 3971 | double __s_; |
| 3972 | double __d_; |
| 3973 | double __l_; |
| 3974 | double __omega_; |
| 3975 | double __c0_; |
| 3976 | double __c1_; |
| 3977 | double __c2_; |
| 3978 | double __c3_; |
| 3979 | double __c_; |
| 3980 | |
| 3981 | public: |
| 3982 | typedef poisson_distribution distribution_type; |
| 3983 | |
| 3984 | explicit param_type(double __mean = 1.0); |
| 3985 | |
| 3986 | double mean() const {return __mean_;} |
| 3987 | |
| 3988 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 3989 | {return __x.__mean_ == __y.__mean_;} |
| 3990 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 3991 | {return !(__x == __y);} |
| 3992 | |
| 3993 | friend class poisson_distribution; |
| 3994 | }; |
| 3995 | |
| 3996 | private: |
| 3997 | param_type __p_; |
| 3998 | |
| 3999 | public: |
| 4000 | // constructors and reset functions |
| 4001 | explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {} |
| 4002 | explicit poisson_distribution(const param_type& __p) : __p_(__p) {} |
| 4003 | void reset() {} |
| 4004 | |
| 4005 | // generating functions |
| 4006 | template<class _URNG> result_type operator()(_URNG& __g) |
| 4007 | {return (*this)(__g, __p_);} |
| 4008 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4009 | |
| 4010 | // property functions |
| 4011 | double mean() const {return __p_.mean();} |
| 4012 | |
| 4013 | param_type param() const {return __p_;} |
| 4014 | void param(const param_type& __p) {__p_ = __p;} |
| 4015 | |
| 4016 | result_type min() const {return 0;} |
| 4017 | result_type max() const {return numeric_limits<result_type>::max();} |
| 4018 | |
| 4019 | friend bool operator==(const poisson_distribution& __x, |
| 4020 | const poisson_distribution& __y) |
| 4021 | {return __x.__p_ == __y.__p_;} |
| 4022 | friend bool operator!=(const poisson_distribution& __x, |
| 4023 | const poisson_distribution& __y) |
| 4024 | {return !(__x == __y);} |
| 4025 | }; |
| 4026 | |
| 4027 | template<class _IntType> |
| 4028 | poisson_distribution<_IntType>::param_type::param_type(double __mean) |
| 4029 | : __mean_(__mean) |
| 4030 | { |
| 4031 | if (__mean_ < 10) |
| 4032 | { |
| 4033 | __s_ = 0; |
| 4034 | __d_ = 0; |
| 4035 | __l_ = _STD::exp(-__mean_); |
| 4036 | __omega_ = 0; |
| 4037 | __c3_ = 0; |
| 4038 | __c2_ = 0; |
| 4039 | __c1_ = 0; |
| 4040 | __c0_ = 0; |
| 4041 | __c_ = 0; |
| 4042 | } |
| 4043 | else |
| 4044 | { |
| 4045 | __s_ = _STD::sqrt(__mean_); |
| 4046 | __d_ = 6 * __mean_ * __mean_; |
| 4047 | __l_ = static_cast<result_type>(__mean_ - 1.1484); |
| 4048 | __omega_ = .3989423 / __s_; |
| 4049 | double __b1_ = .4166667E-1 / __mean_; |
| 4050 | double __b2_ = .3 * __b1_ * __b1_; |
| 4051 | __c3_ = .1428571 * __b1_ * __b2_; |
| 4052 | __c2_ = __b2_ - 15. * __c3_; |
| 4053 | __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_; |
| 4054 | __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_; |
| 4055 | __c_ = .1069 / __mean_; |
| 4056 | } |
| 4057 | } |
| 4058 | |
| 4059 | template <class _IntType> |
| 4060 | template<class _URNG> |
| 4061 | _IntType |
| 4062 | poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 4063 | { |
| 4064 | result_type __x; |
| 4065 | uniform_real_distribution<double> __urd; |
| 4066 | if (__pr.__mean_ <= 10) |
| 4067 | { |
| 4068 | __x = 0; |
| 4069 | for (double __p = __urd(__urng); __p > __pr.__l_; ++__x) |
| 4070 | __p *= __urd(__urng); |
| 4071 | } |
| 4072 | else |
| 4073 | { |
| 4074 | double __difmuk; |
| 4075 | double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng); |
| 4076 | double __u; |
| 4077 | if (__g > 0) |
| 4078 | { |
| 4079 | __x = static_cast<result_type>(__g); |
| 4080 | if (__x >= __pr.__l_) |
| 4081 | return __x; |
| 4082 | __difmuk = __pr.__mean_ - __x; |
| 4083 | __u = __urd(__urng); |
| 4084 | if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk) |
| 4085 | return __x; |
| 4086 | } |
| 4087 | exponential_distribution<double> __edist; |
| 4088 | for (bool __using_exp_dist = false; true; __using_exp_dist = true) |
| 4089 | { |
| 4090 | double __e; |
| 4091 | if (__using_exp_dist || __g < 0) |
| 4092 | { |
| 4093 | double __t; |
| 4094 | do |
| 4095 | { |
| 4096 | __e = __edist(__urng); |
| 4097 | __u = __urd(__urng); |
| 4098 | __u += __u - 1; |
| 4099 | __t = 1.8 + (__u < 0 ? -__e : __e); |
| 4100 | } while (__t <= -.6744); |
| 4101 | __x = __pr.__mean_ + __pr.__s_ * __t; |
| 4102 | __difmuk = __pr.__mean_ - __x; |
| 4103 | __using_exp_dist = true; |
| 4104 | } |
| 4105 | double __px; |
| 4106 | double __py; |
| 4107 | if (__x < 10) |
| 4108 | { |
| 4109 | const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, |
| 4110 | 40320, 362880}; |
| 4111 | __px = -__pr.__mean_; |
| 4112 | __py = _STD::pow(__pr.__mean_, (double)__x) / __fac[__x]; |
| 4113 | } |
| 4114 | else |
| 4115 | { |
| 4116 | double __del = .8333333E-1 / __x; |
| 4117 | __del -= 4.8 * __del * __del * __del; |
| 4118 | double __v = __difmuk / __x; |
| 4119 | if (_STD::abs(__v) > 0.25) |
| 4120 | __px = __x * _STD::log(1 + __v) - __difmuk - __del; |
| 4121 | else |
| 4122 | __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) * |
| 4123 | __v + .1421878) * __v + -.1661269) * __v + .2000118) * |
| 4124 | __v + -.2500068) * __v + .3333333) * __v + -.5) - __del; |
| 4125 | __py = .3989423 / _STD::sqrt(__x); |
| 4126 | } |
| 4127 | double __r = (0.5 - __difmuk) / __pr.__s_; |
| 4128 | double __r2 = __r * __r; |
| 4129 | double __fx = -0.5 * __r2; |
| 4130 | double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) * |
| 4131 | __r2 + __pr.__c1_) * __r2 + __pr.__c0_); |
| 4132 | if (__using_exp_dist) |
| 4133 | { |
| 4134 | if (__pr.__c_ * _STD::abs(__u) <= __py * _STD::exp(__px + __e) - |
| 4135 | __fy * _STD::exp(__fx + __e)) |
| 4136 | break; |
| 4137 | } |
| 4138 | else |
| 4139 | { |
| 4140 | if (__fy - __u * __fy <= __py * _STD::exp(__px - __fx)) |
| 4141 | break; |
| 4142 | } |
| 4143 | } |
| 4144 | } |
| 4145 | return __x; |
| 4146 | } |
| 4147 | |
| 4148 | template <class _CharT, class _Traits, class _IntType> |
| 4149 | basic_ostream<_CharT, _Traits>& |
| 4150 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4151 | const poisson_distribution<_IntType>& __x) |
| 4152 | { |
| 4153 | __save_flags<_CharT, _Traits> _(__os); |
| 4154 | __os.flags(ios_base::dec | ios_base::left); |
| 4155 | return __os << __x.mean(); |
| 4156 | } |
| 4157 | |
| 4158 | template <class _CharT, class _Traits, class _IntType> |
| 4159 | basic_istream<_CharT, _Traits>& |
| 4160 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4161 | poisson_distribution<_IntType>& __x) |
| 4162 | { |
| 4163 | typedef poisson_distribution<_IntType> _Eng; |
| 4164 | typedef typename _Eng::param_type param_type; |
| 4165 | __save_flags<_CharT, _Traits> _(__is); |
| 4166 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4167 | double __mean; |
| 4168 | __is >> __mean; |
| 4169 | if (!__is.fail()) |
| 4170 | __x.param(param_type(__mean)); |
| 4171 | return __is; |
| 4172 | } |
| 4173 | |
Howard Hinnant | 9de6e30 | 2010-05-16 01:09:02 +0000 | [diff] [blame] | 4174 | // weibull_distribution |
| 4175 | |
| 4176 | template<class _RealType = double> |
| 4177 | class weibull_distribution |
| 4178 | { |
| 4179 | public: |
| 4180 | // types |
| 4181 | typedef _RealType result_type; |
| 4182 | |
| 4183 | class param_type |
| 4184 | { |
| 4185 | result_type __a_; |
| 4186 | result_type __b_; |
| 4187 | public: |
| 4188 | typedef weibull_distribution distribution_type; |
| 4189 | |
| 4190 | explicit param_type(result_type __a = 1, result_type __b = 1) |
| 4191 | : __a_(__a), __b_(__b) {} |
| 4192 | |
| 4193 | result_type a() const {return __a_;} |
| 4194 | result_type b() const {return __b_;} |
| 4195 | |
| 4196 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 4197 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
| 4198 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 4199 | {return !(__x == __y);} |
| 4200 | }; |
| 4201 | |
| 4202 | private: |
| 4203 | param_type __p_; |
| 4204 | |
| 4205 | public: |
| 4206 | // constructor and reset functions |
| 4207 | explicit weibull_distribution(result_type __a = 1, result_type __b = 1) |
| 4208 | : __p_(param_type(__a, __b)) {} |
| 4209 | explicit weibull_distribution(const param_type& __p) |
| 4210 | : __p_(__p) {} |
| 4211 | void reset() {} |
| 4212 | |
| 4213 | // generating functions |
| 4214 | template<class _URNG> result_type operator()(_URNG& __g) |
| 4215 | {return (*this)(__g, __p_);} |
| 4216 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p) |
| 4217 | {return __p.b() * |
| 4218 | _STD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());} |
| 4219 | |
| 4220 | // property functions |
| 4221 | result_type a() const {return __p_.a();} |
| 4222 | result_type b() const {return __p_.b();} |
| 4223 | |
| 4224 | param_type param() const {return __p_;} |
| 4225 | void param(const param_type& __p) {__p_ = __p;} |
| 4226 | |
| 4227 | result_type min() const {return 0;} |
| 4228 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4229 | |
| 4230 | |
| 4231 | friend bool operator==(const weibull_distribution& __x, |
| 4232 | const weibull_distribution& __y) |
| 4233 | {return __x.__p_ == __y.__p_;} |
| 4234 | friend bool operator!=(const weibull_distribution& __x, |
| 4235 | const weibull_distribution& __y) |
| 4236 | {return !(__x == __y);} |
| 4237 | }; |
| 4238 | |
| 4239 | template <class _CharT, class _Traits, class _RT> |
| 4240 | basic_ostream<_CharT, _Traits>& |
| 4241 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4242 | const weibull_distribution<_RT>& __x) |
| 4243 | { |
| 4244 | __save_flags<_CharT, _Traits> _(__os); |
| 4245 | __os.flags(ios_base::dec | ios_base::left); |
| 4246 | _CharT __sp = __os.widen(' '); |
| 4247 | __os.fill(__sp); |
| 4248 | __os << __x.a() << __sp << __x.b(); |
| 4249 | return __os; |
| 4250 | } |
| 4251 | |
| 4252 | template <class _CharT, class _Traits, class _RT> |
| 4253 | basic_istream<_CharT, _Traits>& |
| 4254 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4255 | weibull_distribution<_RT>& __x) |
| 4256 | { |
| 4257 | typedef weibull_distribution<_RT> _Eng; |
| 4258 | typedef typename _Eng::result_type result_type; |
| 4259 | typedef typename _Eng::param_type param_type; |
| 4260 | __save_flags<_CharT, _Traits> _(__is); |
| 4261 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4262 | result_type __a; |
| 4263 | result_type __b; |
| 4264 | __is >> __a >> __b; |
| 4265 | if (!__is.fail()) |
| 4266 | __x.param(param_type(__a, __b)); |
| 4267 | return __is; |
| 4268 | } |
| 4269 | |
Howard Hinnant | c2b0dc7 | 2010-05-17 16:21:56 +0000 | [diff] [blame] | 4270 | template<class _RealType = double> |
| 4271 | class extreme_value_distribution |
| 4272 | { |
| 4273 | public: |
| 4274 | // types |
| 4275 | typedef _RealType result_type; |
| 4276 | |
| 4277 | class param_type |
| 4278 | { |
| 4279 | result_type __a_; |
| 4280 | result_type __b_; |
| 4281 | public: |
| 4282 | typedef extreme_value_distribution distribution_type; |
| 4283 | |
| 4284 | explicit param_type(result_type __a = 0, result_type __b = 1) |
| 4285 | : __a_(__a), __b_(__b) {} |
| 4286 | |
| 4287 | result_type a() const {return __a_;} |
| 4288 | result_type b() const {return __b_;} |
| 4289 | |
| 4290 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 4291 | {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} |
| 4292 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 4293 | {return !(__x == __y);} |
| 4294 | }; |
| 4295 | |
| 4296 | private: |
| 4297 | param_type __p_; |
| 4298 | |
| 4299 | public: |
| 4300 | // constructor and reset functions |
| 4301 | explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1) |
| 4302 | : __p_(param_type(__a, __b)) {} |
| 4303 | explicit extreme_value_distribution(const param_type& __p) |
| 4304 | : __p_(__p) {} |
| 4305 | void reset() {} |
| 4306 | |
| 4307 | // generating functions |
| 4308 | template<class _URNG> result_type operator()(_URNG& __g) |
| 4309 | {return (*this)(__g, __p_);} |
| 4310 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4311 | |
| 4312 | // property functions |
| 4313 | result_type a() const {return __p_.a();} |
| 4314 | result_type b() const {return __p_.b();} |
| 4315 | |
| 4316 | param_type param() const {return __p_;} |
| 4317 | void param(const param_type& __p) {__p_ = __p;} |
| 4318 | |
| 4319 | result_type min() const {return -numeric_limits<result_type>::infinity();} |
| 4320 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4321 | |
| 4322 | friend bool operator==(const extreme_value_distribution& __x, |
| 4323 | const extreme_value_distribution& __y) |
| 4324 | {return __x.__p_ == __y.__p_;} |
| 4325 | friend bool operator!=(const extreme_value_distribution& __x, |
| 4326 | const extreme_value_distribution& __y) |
| 4327 | {return !(__x == __y);} |
| 4328 | }; |
| 4329 | |
| 4330 | template<class _RealType> |
| 4331 | template<class _URNG> |
| 4332 | _RealType |
| 4333 | extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4334 | { |
| 4335 | return __p.a() - __p.b() * |
| 4336 | _STD::log(-_STD::log(1-uniform_real_distribution<result_type>()(__g))); |
| 4337 | } |
| 4338 | |
| 4339 | template <class _CharT, class _Traits, class _RT> |
| 4340 | basic_ostream<_CharT, _Traits>& |
| 4341 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4342 | const extreme_value_distribution<_RT>& __x) |
| 4343 | { |
| 4344 | __save_flags<_CharT, _Traits> _(__os); |
| 4345 | __os.flags(ios_base::dec | ios_base::left); |
| 4346 | _CharT __sp = __os.widen(' '); |
| 4347 | __os.fill(__sp); |
| 4348 | __os << __x.a() << __sp << __x.b(); |
| 4349 | return __os; |
| 4350 | } |
| 4351 | |
| 4352 | template <class _CharT, class _Traits, class _RT> |
| 4353 | basic_istream<_CharT, _Traits>& |
| 4354 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4355 | extreme_value_distribution<_RT>& __x) |
| 4356 | { |
| 4357 | typedef extreme_value_distribution<_RT> _Eng; |
| 4358 | typedef typename _Eng::result_type result_type; |
| 4359 | typedef typename _Eng::param_type param_type; |
| 4360 | __save_flags<_CharT, _Traits> _(__is); |
| 4361 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4362 | result_type __a; |
| 4363 | result_type __b; |
| 4364 | __is >> __a >> __b; |
| 4365 | if (!__is.fail()) |
| 4366 | __x.param(param_type(__a, __b)); |
| 4367 | return __is; |
| 4368 | } |
| 4369 | |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4370 | // gamma_distribution |
| 4371 | |
| 4372 | template<class _RealType = double> |
| 4373 | class gamma_distribution |
| 4374 | { |
| 4375 | public: |
| 4376 | // types |
| 4377 | typedef _RealType result_type; |
| 4378 | |
| 4379 | class param_type |
| 4380 | { |
| 4381 | result_type __alpha_; |
| 4382 | result_type __beta_; |
| 4383 | public: |
| 4384 | typedef gamma_distribution distribution_type; |
| 4385 | |
| 4386 | explicit param_type(result_type __alpha = 1, result_type __beta = 1) |
| 4387 | : __alpha_(__alpha), __beta_(__beta) {} |
| 4388 | |
| 4389 | result_type alpha() const {return __alpha_;} |
| 4390 | result_type beta() const {return __beta_;} |
| 4391 | |
| 4392 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 4393 | {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;} |
| 4394 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 4395 | {return !(__x == __y);} |
| 4396 | }; |
| 4397 | |
| 4398 | private: |
| 4399 | param_type __p_; |
| 4400 | |
| 4401 | public: |
| 4402 | // constructors and reset functions |
| 4403 | explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1) |
| 4404 | : __p_(param_type(__alpha, __beta)) {} |
| 4405 | explicit gamma_distribution(const param_type& __p) |
| 4406 | : __p_(__p) {} |
| 4407 | void reset() {} |
| 4408 | |
| 4409 | // generating functions |
| 4410 | template<class _URNG> result_type operator()(_URNG& __g) |
| 4411 | {return (*this)(__g, __p_);} |
| 4412 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4413 | |
| 4414 | // property functions |
| 4415 | result_type alpha() const {return __p_.alpha();} |
| 4416 | result_type beta() const {return __p_.beta();} |
| 4417 | |
| 4418 | param_type param() const {return __p_;} |
| 4419 | void param(const param_type& __p) {__p_ = __p;} |
| 4420 | |
| 4421 | result_type min() const {return 0;} |
| 4422 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4423 | |
| 4424 | friend bool operator==(const gamma_distribution& __x, |
| 4425 | const gamma_distribution& __y) |
| 4426 | {return __x.__p_ == __y.__p_;} |
| 4427 | friend bool operator!=(const gamma_distribution& __x, |
| 4428 | const gamma_distribution& __y) |
| 4429 | {return !(__x == __y);} |
| 4430 | }; |
| 4431 | |
| 4432 | template <class _RealType> |
| 4433 | template<class _URNG> |
| 4434 | _RealType |
| 4435 | gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) |
| 4436 | { |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 4437 | result_type __a = __p.alpha(); |
| 4438 | uniform_real_distribution<result_type> __gen(0, 1); |
| 4439 | exponential_distribution<result_type> __egen; |
| 4440 | result_type __x; |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4441 | if (__a == 1) |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 4442 | __x = __egen(__g); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4443 | else if (__a > 1) |
| 4444 | { |
| 4445 | const result_type __b = __a - 1; |
| 4446 | const result_type __c = 3 * __a - result_type(0.75); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4447 | while (true) |
| 4448 | { |
| 4449 | const result_type __u = __gen(__g); |
| 4450 | const result_type __v = __gen(__g); |
| 4451 | const result_type __w = __u * (1 - __u); |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 4452 | if (__w != 0) |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4453 | { |
| 4454 | const result_type __y = _STD::sqrt(__c / __w) * |
| 4455 | (__u - result_type(0.5)); |
| 4456 | __x = __b + __y; |
| 4457 | if (__x >= 0) |
| 4458 | { |
| 4459 | const result_type __z = 64 * __w * __w * __w * __v * __v; |
| 4460 | if (__z <= 1 - 2 * __y * __y / __x) |
| 4461 | break; |
| 4462 | if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y)) |
| 4463 | break; |
| 4464 | } |
| 4465 | } |
| 4466 | } |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4467 | } |
Howard Hinnant | f417abe | 2010-05-14 18:43:10 +0000 | [diff] [blame] | 4468 | else // __a < 1 |
| 4469 | { |
| 4470 | while (true) |
| 4471 | { |
| 4472 | const result_type __u = __gen(__g); |
| 4473 | const result_type __es = __egen(__g); |
| 4474 | if (__u <= 1 - __a) |
| 4475 | { |
| 4476 | __x = _STD::pow(__u, 1 / __a); |
| 4477 | if (__x <= __es) |
| 4478 | break; |
| 4479 | } |
| 4480 | else |
| 4481 | { |
| 4482 | const result_type __e = -_STD::log((1-__u)/__a); |
| 4483 | __x = _STD::pow(1 - __a + __a * __e, 1 / __a); |
| 4484 | if (__x <= __e + __es) |
| 4485 | break; |
| 4486 | } |
| 4487 | } |
| 4488 | } |
| 4489 | return __x * __p.beta(); |
Howard Hinnant | c7c4913 | 2010-05-13 17:58:28 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | template <class _CharT, class _Traits, class _RT> |
| 4493 | basic_ostream<_CharT, _Traits>& |
| 4494 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4495 | const gamma_distribution<_RT>& __x) |
| 4496 | { |
| 4497 | __save_flags<_CharT, _Traits> _(__os); |
| 4498 | __os.flags(ios_base::dec | ios_base::left); |
| 4499 | _CharT __sp = __os.widen(' '); |
| 4500 | __os.fill(__sp); |
| 4501 | __os << __x.alpha() << __sp << __x.beta(); |
| 4502 | return __os; |
| 4503 | } |
| 4504 | |
| 4505 | template <class _CharT, class _Traits, class _RT> |
| 4506 | basic_istream<_CharT, _Traits>& |
| 4507 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4508 | gamma_distribution<_RT>& __x) |
| 4509 | { |
| 4510 | typedef gamma_distribution<_RT> _Eng; |
| 4511 | typedef typename _Eng::result_type result_type; |
| 4512 | typedef typename _Eng::param_type param_type; |
| 4513 | __save_flags<_CharT, _Traits> _(__is); |
| 4514 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4515 | result_type __alpha; |
| 4516 | result_type __beta; |
| 4517 | __is >> __alpha >> __beta; |
| 4518 | if (!__is.fail()) |
| 4519 | __x.param(param_type(__alpha, __beta)); |
| 4520 | return __is; |
| 4521 | } |
Howard Hinnant | a64111c | 2010-05-12 21:02:31 +0000 | [diff] [blame] | 4522 | |
Howard Hinnant | f2fe5d5 | 2010-05-17 00:09:38 +0000 | [diff] [blame] | 4523 | // negative_binomial_distribution |
| 4524 | |
| 4525 | template<class _IntType = int> |
| 4526 | class negative_binomial_distribution |
| 4527 | { |
| 4528 | public: |
| 4529 | // types |
| 4530 | typedef _IntType result_type; |
| 4531 | |
| 4532 | class param_type |
| 4533 | { |
| 4534 | result_type __k_; |
| 4535 | double __p_; |
| 4536 | public: |
| 4537 | typedef negative_binomial_distribution distribution_type; |
| 4538 | |
| 4539 | explicit param_type(result_type __k = 1, double __p = 0.5) |
| 4540 | : __k_(__k), __p_(__p) {} |
| 4541 | |
| 4542 | result_type k() const {return __k_;} |
| 4543 | double p() const {return __p_;} |
| 4544 | |
| 4545 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 4546 | {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;} |
| 4547 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 4548 | {return !(__x == __y);} |
| 4549 | }; |
| 4550 | |
| 4551 | private: |
| 4552 | param_type __p_; |
| 4553 | |
| 4554 | public: |
| 4555 | // constructor and reset functions |
| 4556 | explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5) |
| 4557 | : __p_(__k, __p) {} |
| 4558 | explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {} |
| 4559 | void reset() {} |
| 4560 | |
| 4561 | // generating functions |
| 4562 | template<class _URNG> result_type operator()(_URNG& __g) |
| 4563 | {return (*this)(__g, __p_);} |
| 4564 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); |
| 4565 | |
| 4566 | // property functions |
| 4567 | result_type k() const {return __p_.k();} |
| 4568 | double p() const {return __p_.p();} |
| 4569 | |
| 4570 | param_type param() const {return __p_;} |
| 4571 | void param(const param_type& __p) {__p_ = __p;} |
| 4572 | |
| 4573 | result_type min() const {return 0;} |
| 4574 | result_type max() const {return numeric_limits<result_type>::max();} |
| 4575 | |
| 4576 | friend bool operator==(const negative_binomial_distribution& __x, |
| 4577 | const negative_binomial_distribution& __y) |
| 4578 | {return __x.__p_ == __y.__p_;} |
| 4579 | friend bool operator!=(const negative_binomial_distribution& __x, |
| 4580 | const negative_binomial_distribution& __y) |
| 4581 | {return !(__x == __y);} |
| 4582 | }; |
| 4583 | |
| 4584 | template <class _IntType> |
| 4585 | template<class _URNG> |
| 4586 | _IntType |
| 4587 | negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr) |
| 4588 | { |
| 4589 | result_type __k = __pr.k(); |
| 4590 | double __p = __pr.p(); |
| 4591 | if (__k <= 21 * __p) |
| 4592 | { |
| 4593 | bernoulli_distribution __gen(__p); |
| 4594 | result_type __f = 0; |
| 4595 | result_type __s = 0; |
| 4596 | while (__s < __k) |
| 4597 | { |
| 4598 | if (__gen(__urng)) |
| 4599 | ++__s; |
| 4600 | else |
| 4601 | ++__f; |
| 4602 | } |
| 4603 | return __f; |
| 4604 | } |
| 4605 | return poisson_distribution<result_type>(gamma_distribution<double> |
| 4606 | (__k, (1-__p)/__p)(__urng))(__urng); |
| 4607 | } |
| 4608 | |
| 4609 | template <class _CharT, class _Traits, class _IntType> |
| 4610 | basic_ostream<_CharT, _Traits>& |
| 4611 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4612 | const negative_binomial_distribution<_IntType>& __x) |
| 4613 | { |
| 4614 | __save_flags<_CharT, _Traits> _(__os); |
| 4615 | __os.flags(ios_base::dec | ios_base::left); |
| 4616 | _CharT __sp = __os.widen(' '); |
| 4617 | __os.fill(__sp); |
| 4618 | return __os << __x.k() << __sp << __x.p(); |
| 4619 | } |
| 4620 | |
| 4621 | template <class _CharT, class _Traits, class _IntType> |
| 4622 | basic_istream<_CharT, _Traits>& |
| 4623 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4624 | negative_binomial_distribution<_IntType>& __x) |
| 4625 | { |
| 4626 | typedef negative_binomial_distribution<_IntType> _Eng; |
| 4627 | typedef typename _Eng::result_type result_type; |
| 4628 | typedef typename _Eng::param_type param_type; |
| 4629 | __save_flags<_CharT, _Traits> _(__is); |
| 4630 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4631 | result_type __k; |
| 4632 | double __p; |
| 4633 | __is >> __k >> __p; |
| 4634 | if (!__is.fail()) |
| 4635 | __x.param(param_type(__k, __p)); |
| 4636 | return __is; |
| 4637 | } |
| 4638 | |
Howard Hinnant | 34e8a57 | 2010-05-17 13:44:27 +0000 | [diff] [blame] | 4639 | // geometric_distribution |
| 4640 | |
| 4641 | template<class _IntType = int> |
| 4642 | class geometric_distribution |
| 4643 | { |
| 4644 | public: |
| 4645 | // types |
| 4646 | typedef _IntType result_type; |
| 4647 | |
| 4648 | class param_type |
| 4649 | { |
| 4650 | double __p_; |
| 4651 | public: |
| 4652 | typedef geometric_distribution distribution_type; |
| 4653 | |
| 4654 | explicit param_type(double __p = 0.5) : __p_(__p) {} |
| 4655 | |
| 4656 | double p() const {return __p_;} |
| 4657 | |
| 4658 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 4659 | {return __x.__p_ == __y.__p_;} |
| 4660 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 4661 | {return !(__x == __y);} |
| 4662 | }; |
| 4663 | |
| 4664 | private: |
| 4665 | param_type __p_; |
| 4666 | |
| 4667 | public: |
| 4668 | // constructors and reset functions |
| 4669 | explicit geometric_distribution(double __p = 0.5) : __p_(__p) {} |
| 4670 | explicit geometric_distribution(const param_type& __p) : __p_(__p) {} |
| 4671 | void reset() {} |
| 4672 | |
| 4673 | // generating functions |
| 4674 | template<class _URNG> result_type operator()(_URNG& __g) |
| 4675 | {return (*this)(__g, __p_);} |
| 4676 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p) |
| 4677 | {return negative_binomial_distribution<result_type>(1, __p.p())(__g);} |
| 4678 | |
| 4679 | // property functions |
| 4680 | double p() const {return __p_.p();} |
| 4681 | |
| 4682 | param_type param() const {return __p_;} |
| 4683 | void param(const param_type& __p) {__p_ = __p;} |
| 4684 | |
| 4685 | result_type min() const {return 0;} |
| 4686 | result_type max() const {return numeric_limits<result_type>::max();} |
| 4687 | |
| 4688 | friend bool operator==(const geometric_distribution& __x, |
| 4689 | const geometric_distribution& __y) |
| 4690 | {return __x.__p_ == __y.__p_;} |
| 4691 | friend bool operator!=(const geometric_distribution& __x, |
| 4692 | const geometric_distribution& __y) |
| 4693 | {return !(__x == __y);} |
| 4694 | }; |
| 4695 | |
| 4696 | template <class _CharT, class _Traits, class _IntType> |
| 4697 | basic_ostream<_CharT, _Traits>& |
| 4698 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4699 | const geometric_distribution<_IntType>& __x) |
| 4700 | { |
| 4701 | __save_flags<_CharT, _Traits> _(__os); |
| 4702 | __os.flags(ios_base::dec | ios_base::left); |
| 4703 | return __os << __x.p(); |
| 4704 | } |
| 4705 | |
| 4706 | template <class _CharT, class _Traits, class _IntType> |
| 4707 | basic_istream<_CharT, _Traits>& |
| 4708 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4709 | geometric_distribution<_IntType>& __x) |
| 4710 | { |
| 4711 | typedef geometric_distribution<_IntType> _Eng; |
| 4712 | typedef typename _Eng::param_type param_type; |
| 4713 | __save_flags<_CharT, _Traits> _(__is); |
| 4714 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4715 | double __p; |
| 4716 | __is >> __p; |
| 4717 | if (!__is.fail()) |
| 4718 | __x.param(param_type(__p)); |
| 4719 | return __is; |
| 4720 | } |
| 4721 | |
Howard Hinnant | 97dc2f3 | 2010-05-15 23:36:00 +0000 | [diff] [blame] | 4722 | // chi_squared_distribution |
| 4723 | |
| 4724 | template<class _RealType = double> |
| 4725 | class chi_squared_distribution |
| 4726 | { |
| 4727 | public: |
| 4728 | // types |
| 4729 | typedef _RealType result_type; |
| 4730 | |
| 4731 | class param_type |
| 4732 | { |
| 4733 | result_type __n_; |
| 4734 | public: |
| 4735 | typedef chi_squared_distribution distribution_type; |
| 4736 | |
| 4737 | explicit param_type(result_type __n = 1) : __n_(__n) {} |
| 4738 | |
| 4739 | result_type n() const {return __n_;} |
| 4740 | |
| 4741 | friend bool operator==(const param_type& __x, const param_type& __y) |
| 4742 | {return __x.__n_ == __y.__n_;} |
| 4743 | friend bool operator!=(const param_type& __x, const param_type& __y) |
| 4744 | {return !(__x == __y);} |
| 4745 | }; |
| 4746 | |
| 4747 | private: |
| 4748 | param_type __p_; |
| 4749 | |
| 4750 | public: |
| 4751 | // constructor and reset functions |
| 4752 | explicit chi_squared_distribution(result_type __n = 1) |
| 4753 | : __p_(param_type(__n)) {} |
| 4754 | explicit chi_squared_distribution(const param_type& __p) |
| 4755 | : __p_(__p) {} |
| 4756 | void reset() {} |
| 4757 | |
| 4758 | // generating functions |
| 4759 | template<class _URNG> result_type operator()(_URNG& __g) |
| 4760 | {return (*this)(__g, __p_);} |
| 4761 | template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p) |
| 4762 | {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);} |
| 4763 | |
| 4764 | // property functions |
| 4765 | result_type n() const {return __p_.n();} |
| 4766 | |
| 4767 | param_type param() const {return __p_;} |
| 4768 | void param(const param_type& __p) {__p_ = __p;} |
| 4769 | |
| 4770 | result_type min() const {return 0;} |
| 4771 | result_type max() const {return numeric_limits<result_type>::infinity();} |
| 4772 | |
| 4773 | |
| 4774 | friend bool operator==(const chi_squared_distribution& __x, |
| 4775 | const chi_squared_distribution& __y) |
| 4776 | {return __x.__p_ == __y.__p_;} |
| 4777 | friend bool operator!=(const chi_squared_distribution& __x, |
| 4778 | const chi_squared_distribution& __y) |
| 4779 | {return !(__x == __y);} |
| 4780 | }; |
| 4781 | |
| 4782 | template <class _CharT, class _Traits, class _RT> |
| 4783 | basic_ostream<_CharT, _Traits>& |
| 4784 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4785 | const chi_squared_distribution<_RT>& __x) |
| 4786 | { |
| 4787 | __save_flags<_CharT, _Traits> _(__os); |
| 4788 | __os.flags(ios_base::dec | ios_base::left); |
| 4789 | __os << __x.n(); |
| 4790 | return __os; |
| 4791 | } |
| 4792 | |
| 4793 | template <class _CharT, class _Traits, class _RT> |
| 4794 | basic_istream<_CharT, _Traits>& |
| 4795 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4796 | chi_squared_distribution<_RT>& __x) |
| 4797 | { |
| 4798 | typedef chi_squared_distribution<_RT> _Eng; |
| 4799 | typedef typename _Eng::result_type result_type; |
| 4800 | typedef typename _Eng::param_type param_type; |
| 4801 | __save_flags<_CharT, _Traits> _(__is); |
| 4802 | __is.flags(ios_base::dec | ios_base::skipws); |
| 4803 | result_type __n; |
| 4804 | __is >> __n; |
| 4805 | if (!__is.fail()) |
| 4806 | __x.param(param_type(__n)); |
| 4807 | return __is; |
| 4808 | } |
| 4809 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4810 | _LIBCPP_END_NAMESPACE_STD |
| 4811 | |
| 4812 | #endif // _LIBCPP_RANDOM |